Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: blake2s - implement generic shash driver

Wire up our newly added Blake2s implementation via the shash API.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Ard Biesheuvel and committed by
Herbert Xu
7f9b0880 17e1df67

+195
+18
crypto/Kconfig
··· 656 656 657 657 See https://blake2.net for further information. 658 658 659 + config CRYPTO_BLAKE2S 660 + tristate "BLAKE2s digest algorithm" 661 + select CRYPTO_LIB_BLAKE2S_GENERIC 662 + select CRYPTO_HASH 663 + help 664 + Implementation of cryptographic hash function BLAKE2s 665 + optimized for 8-32bit platforms and can produce digests of any size 666 + between 1 to 32. The keyed hash is also implemented. 667 + 668 + This module provides the following algorithms: 669 + 670 + - blake2s-128 671 + - blake2s-160 672 + - blake2s-224 673 + - blake2s-256 674 + 675 + See https://blake2.net for further information. 676 + 659 677 config CRYPTO_CRCT10DIF 660 678 tristate "CRCT10DIF algorithm" 661 679 select CRYPTO_HASH
+1
crypto/Makefile
··· 75 75 CFLAGS_wp512.o := $(call cc-option,-fno-schedule-insns) # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79149 76 76 obj-$(CONFIG_CRYPTO_TGR192) += tgr192.o 77 77 obj-$(CONFIG_CRYPTO_BLAKE2B) += blake2b_generic.o 78 + obj-$(CONFIG_CRYPTO_BLAKE2S) += blake2s_generic.o 78 79 obj-$(CONFIG_CRYPTO_GF128MUL) += gf128mul.o 79 80 obj-$(CONFIG_CRYPTO_ECB) += ecb.o 80 81 obj-$(CONFIG_CRYPTO_CBC) += cbc.o
+171
crypto/blake2s_generic.c
··· 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> 7 + #include <crypto/internal/simd.h> 8 + #include <crypto/internal/hash.h> 9 + 10 + #include <linux/types.h> 11 + #include <linux/jump_label.h> 12 + #include <linux/kernel.h> 13 + #include <linux/module.h> 14 + 15 + static int crypto_blake2s_setkey(struct crypto_shash *tfm, const u8 *key, 16 + unsigned int keylen) 17 + { 18 + struct blake2s_tfm_ctx *tctx = crypto_shash_ctx(tfm); 19 + 20 + if (keylen == 0 || keylen > BLAKE2S_KEY_SIZE) { 21 + crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); 22 + return -EINVAL; 23 + } 24 + 25 + memcpy(tctx->key, key, keylen); 26 + tctx->keylen = keylen; 27 + 28 + return 0; 29 + } 30 + 31 + static int crypto_blake2s_init(struct shash_desc *desc) 32 + { 33 + struct blake2s_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm); 34 + struct blake2s_state *state = shash_desc_ctx(desc); 35 + const int outlen = crypto_shash_digestsize(desc->tfm); 36 + 37 + if (tctx->keylen) 38 + blake2s_init_key(state, outlen, tctx->key, tctx->keylen); 39 + else 40 + blake2s_init(state, outlen); 41 + 42 + return 0; 43 + } 44 + 45 + static int crypto_blake2s_update(struct shash_desc *desc, const u8 *in, 46 + unsigned int inlen) 47 + { 48 + struct blake2s_state *state = shash_desc_ctx(desc); 49 + const size_t fill = BLAKE2S_BLOCK_SIZE - state->buflen; 50 + 51 + if (unlikely(!inlen)) 52 + return 0; 53 + if (inlen > fill) { 54 + memcpy(state->buf + state->buflen, in, fill); 55 + blake2s_compress_generic(state, state->buf, 1, BLAKE2S_BLOCK_SIZE); 56 + state->buflen = 0; 57 + in += fill; 58 + inlen -= fill; 59 + } 60 + if (inlen > BLAKE2S_BLOCK_SIZE) { 61 + const size_t nblocks = DIV_ROUND_UP(inlen, BLAKE2S_BLOCK_SIZE); 62 + /* Hash one less (full) block than strictly possible */ 63 + blake2s_compress_generic(state, in, nblocks - 1, BLAKE2S_BLOCK_SIZE); 64 + in += BLAKE2S_BLOCK_SIZE * (nblocks - 1); 65 + inlen -= BLAKE2S_BLOCK_SIZE * (nblocks - 1); 66 + } 67 + memcpy(state->buf + state->buflen, in, inlen); 68 + state->buflen += inlen; 69 + 70 + return 0; 71 + } 72 + 73 + static int crypto_blake2s_final(struct shash_desc *desc, u8 *out) 74 + { 75 + struct blake2s_state *state = shash_desc_ctx(desc); 76 + 77 + blake2s_set_lastblock(state); 78 + memset(state->buf + state->buflen, 0, 79 + BLAKE2S_BLOCK_SIZE - state->buflen); /* Padding */ 80 + blake2s_compress_generic(state, state->buf, 1, state->buflen); 81 + cpu_to_le32_array(state->h, ARRAY_SIZE(state->h)); 82 + memcpy(out, state->h, state->outlen); 83 + memzero_explicit(state, sizeof(*state)); 84 + 85 + return 0; 86 + } 87 + 88 + static struct shash_alg blake2s_algs[] = {{ 89 + .base.cra_name = "blake2s-128", 90 + .base.cra_driver_name = "blake2s-128-generic", 91 + .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY, 92 + .base.cra_ctxsize = sizeof(struct blake2s_tfm_ctx), 93 + .base.cra_priority = 200, 94 + .base.cra_blocksize = BLAKE2S_BLOCK_SIZE, 95 + .base.cra_module = THIS_MODULE, 96 + 97 + .digestsize = BLAKE2S_128_HASH_SIZE, 98 + .setkey = crypto_blake2s_setkey, 99 + .init = crypto_blake2s_init, 100 + .update = crypto_blake2s_update, 101 + .final = crypto_blake2s_final, 102 + .descsize = sizeof(struct blake2s_state), 103 + }, { 104 + .base.cra_name = "blake2s-160", 105 + .base.cra_driver_name = "blake2s-160-generic", 106 + .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY, 107 + .base.cra_ctxsize = sizeof(struct blake2s_tfm_ctx), 108 + .base.cra_priority = 200, 109 + .base.cra_blocksize = BLAKE2S_BLOCK_SIZE, 110 + .base.cra_module = THIS_MODULE, 111 + 112 + .digestsize = BLAKE2S_160_HASH_SIZE, 113 + .setkey = crypto_blake2s_setkey, 114 + .init = crypto_blake2s_init, 115 + .update = crypto_blake2s_update, 116 + .final = crypto_blake2s_final, 117 + .descsize = sizeof(struct blake2s_state), 118 + }, { 119 + .base.cra_name = "blake2s-224", 120 + .base.cra_driver_name = "blake2s-224-generic", 121 + .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY, 122 + .base.cra_ctxsize = sizeof(struct blake2s_tfm_ctx), 123 + .base.cra_priority = 200, 124 + .base.cra_blocksize = BLAKE2S_BLOCK_SIZE, 125 + .base.cra_module = THIS_MODULE, 126 + 127 + .digestsize = BLAKE2S_224_HASH_SIZE, 128 + .setkey = crypto_blake2s_setkey, 129 + .init = crypto_blake2s_init, 130 + .update = crypto_blake2s_update, 131 + .final = crypto_blake2s_final, 132 + .descsize = sizeof(struct blake2s_state), 133 + }, { 134 + .base.cra_name = "blake2s-256", 135 + .base.cra_driver_name = "blake2s-256-generic", 136 + .base.cra_flags = CRYPTO_ALG_OPTIONAL_KEY, 137 + .base.cra_ctxsize = sizeof(struct blake2s_tfm_ctx), 138 + .base.cra_priority = 200, 139 + .base.cra_blocksize = BLAKE2S_BLOCK_SIZE, 140 + .base.cra_module = THIS_MODULE, 141 + 142 + .digestsize = BLAKE2S_256_HASH_SIZE, 143 + .setkey = crypto_blake2s_setkey, 144 + .init = crypto_blake2s_init, 145 + .update = crypto_blake2s_update, 146 + .final = crypto_blake2s_final, 147 + .descsize = sizeof(struct blake2s_state), 148 + }}; 149 + 150 + static int __init blake2s_mod_init(void) 151 + { 152 + return crypto_register_shashes(blake2s_algs, ARRAY_SIZE(blake2s_algs)); 153 + } 154 + 155 + static void __exit blake2s_mod_exit(void) 156 + { 157 + crypto_unregister_shashes(blake2s_algs, ARRAY_SIZE(blake2s_algs)); 158 + } 159 + 160 + subsys_initcall(blake2s_mod_init); 161 + module_exit(blake2s_mod_exit); 162 + 163 + MODULE_ALIAS_CRYPTO("blake2s-128"); 164 + MODULE_ALIAS_CRYPTO("blake2s-128-generic"); 165 + MODULE_ALIAS_CRYPTO("blake2s-160"); 166 + MODULE_ALIAS_CRYPTO("blake2s-160-generic"); 167 + MODULE_ALIAS_CRYPTO("blake2s-224"); 168 + MODULE_ALIAS_CRYPTO("blake2s-224-generic"); 169 + MODULE_ALIAS_CRYPTO("blake2s-256"); 170 + MODULE_ALIAS_CRYPTO("blake2s-256-generic"); 171 + MODULE_LICENSE("GPL v2");
+5
include/crypto/internal/blake2s.h
··· 5 5 6 6 #include <crypto/blake2s.h> 7 7 8 + struct blake2s_tfm_ctx { 9 + u8 key[BLAKE2S_KEY_SIZE]; 10 + unsigned int keylen; 11 + }; 12 + 8 13 void blake2s_compress_generic(struct blake2s_state *state,const u8 *block, 9 14 size_t nblocks, const u32 inc); 10 15