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

Configure Feed

Select the types of activity you want to include in your feed.

crypto: sm3-generic - Use API partial block handling

Use the Crypto API partial block handling.

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

+52 -25
+8 -23
crypto/sm3_generic.c
··· 9 9 */ 10 10 11 11 #include <crypto/internal/hash.h> 12 - #include <linux/init.h> 13 - #include <linux/module.h> 14 - #include <linux/mm.h> 15 - #include <linux/types.h> 16 12 #include <crypto/sm3.h> 17 13 #include <crypto/sm3_base.h> 18 - #include <linux/bitops.h> 19 - #include <asm/byteorder.h> 20 - #include <linux/unaligned.h> 14 + #include <linux/kernel.h> 15 + #include <linux/module.h> 21 16 22 17 const u8 sm3_zero_message_hash[SM3_DIGEST_SIZE] = { 23 18 0x1A, 0xB2, 0x1D, 0x83, 0x55, 0xCF, 0xA1, 0x7F, ··· 25 30 static int crypto_sm3_update(struct shash_desc *desc, const u8 *data, 26 31 unsigned int len) 27 32 { 28 - sm3_update(shash_desc_ctx(desc), data, len); 29 - return 0; 30 - } 31 - 32 - static int crypto_sm3_final(struct shash_desc *desc, u8 *out) 33 - { 34 - sm3_final(shash_desc_ctx(desc), out); 35 - return 0; 33 + return sm3_base_do_update_blocks(desc, data, len, sm3_block_generic); 36 34 } 37 35 38 36 static int crypto_sm3_finup(struct shash_desc *desc, const u8 *data, 39 37 unsigned int len, u8 *hash) 40 38 { 41 - struct sm3_state *sctx = shash_desc_ctx(desc); 42 - 43 - if (len) 44 - sm3_update(sctx, data, len); 45 - sm3_final(sctx, hash); 46 - return 0; 39 + sm3_base_do_finup(desc, data, len, sm3_block_generic); 40 + return sm3_base_finish(desc, hash); 47 41 } 48 42 49 43 static struct shash_alg sm3_alg = { 50 44 .digestsize = SM3_DIGEST_SIZE, 51 45 .init = sm3_base_init, 52 46 .update = crypto_sm3_update, 53 - .final = crypto_sm3_final, 54 47 .finup = crypto_sm3_finup, 55 - .descsize = sizeof(struct sm3_state), 48 + .descsize = SM3_STATE_SIZE, 56 49 .base = { 57 50 .cra_name = "sm3", 58 51 .cra_driver_name = "sm3-generic", 59 52 .cra_priority = 100, 53 + .cra_flags = CRYPTO_AHASH_ALG_BLOCK_ONLY | 54 + CRYPTO_AHASH_ALG_FINUP_MAX, 60 55 .cra_blocksize = SM3_BLOCK_SIZE, 61 56 .cra_module = THIS_MODULE, 62 57 }
+1
include/crypto/sm3.h
··· 14 14 15 15 #define SM3_DIGEST_SIZE 32 16 16 #define SM3_BLOCK_SIZE 64 17 + #define SM3_STATE_SIZE 40 17 18 18 19 #define SM3_T1 0x79CC4519 19 20 #define SM3_T2 0x7A879D8A
+43 -2
include/crypto/sm3_base.h
··· 62 62 return 0; 63 63 } 64 64 65 + static inline int sm3_base_do_update_blocks(struct shash_desc *desc, 66 + const u8 *data, unsigned int len, 67 + sm3_block_fn *block_fn) 68 + { 69 + unsigned int remain = len - round_down(len, SM3_BLOCK_SIZE); 70 + struct sm3_state *sctx = shash_desc_ctx(desc); 71 + 72 + sctx->count += len - remain; 73 + block_fn(sctx, data, len / SM3_BLOCK_SIZE); 74 + return remain; 75 + } 76 + 77 + static inline int sm3_base_do_finup(struct shash_desc *desc, 78 + const u8 *src, unsigned int len, 79 + sm3_block_fn *block_fn) 80 + { 81 + unsigned int bit_offset = SM3_BLOCK_SIZE / 8 - 1; 82 + struct sm3_state *sctx = shash_desc_ctx(desc); 83 + union { 84 + __be64 b64[SM3_BLOCK_SIZE / 4]; 85 + u8 u8[SM3_BLOCK_SIZE * 2]; 86 + } block = {}; 87 + 88 + if (len >= SM3_BLOCK_SIZE) { 89 + int remain; 90 + 91 + remain = sm3_base_do_update_blocks(desc, src, len, block_fn); 92 + src += len - remain; 93 + len = remain; 94 + } 95 + 96 + if (len >= bit_offset * 8) 97 + bit_offset += SM3_BLOCK_SIZE / 8; 98 + memcpy(&block, src, len); 99 + block.u8[len] = 0x80; 100 + sctx->count += len; 101 + block.b64[bit_offset] = cpu_to_be64(sctx->count << 3); 102 + block_fn(sctx, block.u8, (bit_offset + 1) * 8 / SM3_BLOCK_SIZE); 103 + memzero_explicit(&block, sizeof(block)); 104 + 105 + return 0; 106 + } 107 + 65 108 static inline int sm3_base_do_finalize(struct shash_desc *desc, 66 109 sm3_block_fn *block_fn) 67 110 { ··· 136 93 137 94 for (i = 0; i < SM3_DIGEST_SIZE / sizeof(__be32); i++) 138 95 put_unaligned_be32(sctx->state[i], digest++); 139 - 140 - memzero_explicit(sctx, sizeof(*sctx)); 141 96 return 0; 142 97 } 143 98