Ard Biesheuvel | 7f9b088 | 2019-11-08 13:22:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 OR MIT |
| 2 | /* |
| 3 | * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. |
| 4 | */ |
| 5 | |
| 6 | #include <crypto/internal/blake2s.h> |
Ard Biesheuvel | 7f9b088 | 2019-11-08 13:22:30 +0100 | [diff] [blame] | 7 | #include <crypto/internal/hash.h> |
| 8 | |
| 9 | #include <linux/types.h> |
Ard Biesheuvel | 7f9b088 | 2019-11-08 13:22:30 +0100 | [diff] [blame] | 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
| 12 | |
| 13 | static int crypto_blake2s_setkey(struct crypto_shash *tfm, const u8 *key, |
| 14 | unsigned int keylen) |
| 15 | { |
| 16 | struct blake2s_tfm_ctx *tctx = crypto_shash_ctx(tfm); |
| 17 | |
Eric Biggers | 674f368 | 2019-12-30 21:19:36 -0600 | [diff] [blame] | 18 | if (keylen == 0 || keylen > BLAKE2S_KEY_SIZE) |
Ard Biesheuvel | 7f9b088 | 2019-11-08 13:22:30 +0100 | [diff] [blame] | 19 | return -EINVAL; |
Ard Biesheuvel | 7f9b088 | 2019-11-08 13:22:30 +0100 | [diff] [blame] | 20 | |
| 21 | memcpy(tctx->key, key, keylen); |
| 22 | tctx->keylen = keylen; |
| 23 | |
| 24 | return 0; |
| 25 | } |
| 26 | |
| 27 | static int crypto_blake2s_init(struct shash_desc *desc) |
| 28 | { |
| 29 | struct blake2s_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm); |
| 30 | struct blake2s_state *state = shash_desc_ctx(desc); |
| 31 | const int outlen = crypto_shash_digestsize(desc->tfm); |
| 32 | |
| 33 | if (tctx->keylen) |
| 34 | blake2s_init_key(state, outlen, tctx->key, tctx->keylen); |
| 35 | else |
| 36 | blake2s_init(state, outlen); |
| 37 | |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | static int crypto_blake2s_update(struct shash_desc *desc, const u8 *in, |
| 42 | unsigned int inlen) |
| 43 | { |
| 44 | struct blake2s_state *state = shash_desc_ctx(desc); |
| 45 | const size_t fill = BLAKE2S_BLOCK_SIZE - state->buflen; |
| 46 | |
| 47 | if (unlikely(!inlen)) |
| 48 | return 0; |
| 49 | if (inlen > fill) { |
| 50 | memcpy(state->buf + state->buflen, in, fill); |
| 51 | blake2s_compress_generic(state, state->buf, 1, BLAKE2S_BLOCK_SIZE); |
| 52 | state->buflen = 0; |
| 53 | in += fill; |
| 54 | inlen -= fill; |
| 55 | } |
| 56 | if (inlen > BLAKE2S_BLOCK_SIZE) { |
| 57 | const size_t nblocks = DIV_ROUND_UP(inlen, BLAKE2S_BLOCK_SIZE); |
| 58 | /* Hash one less (full) block than strictly possible */ |
| 59 | blake2s_compress_generic(state, in, nblocks - 1, BLAKE2S_BLOCK_SIZE); |
| 60 | in += BLAKE2S_BLOCK_SIZE * (nblocks - 1); |
| 61 | inlen -= BLAKE2S_BLOCK_SIZE * (nblocks - 1); |
| 62 | } |
| 63 | memcpy(state->buf + state->buflen, in, inlen); |
| 64 | state->buflen += inlen; |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static int crypto_blake2s_final(struct shash_desc *desc, u8 *out) |
| 70 | { |
| 71 | struct blake2s_state *state = shash_desc_ctx(desc); |
| 72 | |
| 73 | blake2s_set_lastblock(state); |
| 74 | memset(state->buf + state->buflen, 0, |
| 75 | BLAKE2S_BLOCK_SIZE - state->buflen); /* Padding */ |
| 76 | blake2s_compress_generic(state, state->buf, 1, state->buflen); |
| 77 | cpu_to_le32_array(state->h, ARRAY_SIZE(state->h)); |
| 78 | memcpy(out, state->h, state->outlen); |
| 79 | memzero_explicit(state, sizeof(*state)); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Eric Biggers | 0d39605 | 2020-12-23 00:09:50 -0800 | [diff] [blame] | 84 | #define BLAKE2S_ALG(name, driver_name, digest_size) \ |
| 85 | { \ |
| 86 | .base.cra_name = name, \ |
| 87 | .base.cra_driver_name = driver_name, \ |
| 88 | .base.cra_priority = 100, \ |
| 89 | .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY, \ |
| 90 | .base.cra_blocksize = BLAKE2S_BLOCK_SIZE, \ |
| 91 | .base.cra_ctxsize = sizeof(struct blake2s_tfm_ctx), \ |
| 92 | .base.cra_module = THIS_MODULE, \ |
| 93 | .digestsize = digest_size, \ |
| 94 | .setkey = crypto_blake2s_setkey, \ |
| 95 | .init = crypto_blake2s_init, \ |
| 96 | .update = crypto_blake2s_update, \ |
| 97 | .final = crypto_blake2s_final, \ |
| 98 | .descsize = sizeof(struct blake2s_state), \ |
| 99 | } |
Ard Biesheuvel | 7f9b088 | 2019-11-08 13:22:30 +0100 | [diff] [blame] | 100 | |
Eric Biggers | 0d39605 | 2020-12-23 00:09:50 -0800 | [diff] [blame] | 101 | static struct shash_alg blake2s_algs[] = { |
| 102 | BLAKE2S_ALG("blake2s-128", "blake2s-128-generic", |
| 103 | BLAKE2S_128_HASH_SIZE), |
| 104 | BLAKE2S_ALG("blake2s-160", "blake2s-160-generic", |
| 105 | BLAKE2S_160_HASH_SIZE), |
| 106 | BLAKE2S_ALG("blake2s-224", "blake2s-224-generic", |
| 107 | BLAKE2S_224_HASH_SIZE), |
| 108 | BLAKE2S_ALG("blake2s-256", "blake2s-256-generic", |
| 109 | BLAKE2S_256_HASH_SIZE), |
| 110 | }; |
Ard Biesheuvel | 7f9b088 | 2019-11-08 13:22:30 +0100 | [diff] [blame] | 111 | |
| 112 | static int __init blake2s_mod_init(void) |
| 113 | { |
| 114 | return crypto_register_shashes(blake2s_algs, ARRAY_SIZE(blake2s_algs)); |
| 115 | } |
| 116 | |
| 117 | static void __exit blake2s_mod_exit(void) |
| 118 | { |
| 119 | crypto_unregister_shashes(blake2s_algs, ARRAY_SIZE(blake2s_algs)); |
| 120 | } |
| 121 | |
| 122 | subsys_initcall(blake2s_mod_init); |
| 123 | module_exit(blake2s_mod_exit); |
| 124 | |
| 125 | MODULE_ALIAS_CRYPTO("blake2s-128"); |
| 126 | MODULE_ALIAS_CRYPTO("blake2s-128-generic"); |
| 127 | MODULE_ALIAS_CRYPTO("blake2s-160"); |
| 128 | MODULE_ALIAS_CRYPTO("blake2s-160-generic"); |
| 129 | MODULE_ALIAS_CRYPTO("blake2s-224"); |
| 130 | MODULE_ALIAS_CRYPTO("blake2s-224-generic"); |
| 131 | MODULE_ALIAS_CRYPTO("blake2s-256"); |
| 132 | MODULE_ALIAS_CRYPTO("blake2s-256-generic"); |
| 133 | MODULE_LICENSE("GPL v2"); |