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

crypto: lib/sha256 - Use generic code from sha256_base

Instead of duplicating the sha256 block processing code, reuse
the common code from crypto/sha256_base.h.

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

+55 -68
+36 -14
include/crypto/sha256_base.h
··· 8 8 #ifndef _CRYPTO_SHA256_BASE_H 9 9 #define _CRYPTO_SHA256_BASE_H 10 10 11 + #include <asm/byteorder.h> 12 + #include <asm/unaligned.h> 11 13 #include <crypto/internal/hash.h> 12 14 #include <crypto/sha2.h> 13 - #include <linux/crypto.h> 14 - #include <linux/module.h> 15 15 #include <linux/string.h> 16 - 17 - #include <asm/unaligned.h> 16 + #include <linux/types.h> 18 17 19 18 typedef void (sha256_block_fn)(struct sha256_state *sst, u8 const *src, 20 19 int blocks); ··· 34 35 return 0; 35 36 } 36 37 37 - static inline int sha256_base_do_update(struct shash_desc *desc, 38 - const u8 *data, 39 - unsigned int len, 40 - sha256_block_fn *block_fn) 38 + static inline int lib_sha256_base_do_update(struct sha256_state *sctx, 39 + const u8 *data, 40 + unsigned int len, 41 + sha256_block_fn *block_fn) 41 42 { 42 - struct sha256_state *sctx = shash_desc_ctx(desc); 43 43 unsigned int partial = sctx->count % SHA256_BLOCK_SIZE; 44 44 45 45 sctx->count += len; ··· 71 73 return 0; 72 74 } 73 75 74 - static inline int sha256_base_do_finalize(struct shash_desc *desc, 75 - sha256_block_fn *block_fn) 76 + static inline int sha256_base_do_update(struct shash_desc *desc, 77 + const u8 *data, 78 + unsigned int len, 79 + sha256_block_fn *block_fn) 80 + { 81 + struct sha256_state *sctx = shash_desc_ctx(desc); 82 + 83 + return lib_sha256_base_do_update(sctx, data, len, block_fn); 84 + } 85 + 86 + static inline int lib_sha256_base_do_finalize(struct sha256_state *sctx, 87 + sha256_block_fn *block_fn) 76 88 { 77 89 const int bit_offset = SHA256_BLOCK_SIZE - sizeof(__be64); 78 - struct sha256_state *sctx = shash_desc_ctx(desc); 79 90 __be64 *bits = (__be64 *)(sctx->buf + bit_offset); 80 91 unsigned int partial = sctx->count % SHA256_BLOCK_SIZE; 81 92 ··· 103 96 return 0; 104 97 } 105 98 106 - static inline int sha256_base_finish(struct shash_desc *desc, u8 *out) 99 + static inline int sha256_base_do_finalize(struct shash_desc *desc, 100 + sha256_block_fn *block_fn) 107 101 { 108 - unsigned int digest_size = crypto_shash_digestsize(desc->tfm); 109 102 struct sha256_state *sctx = shash_desc_ctx(desc); 103 + 104 + return lib_sha256_base_do_finalize(sctx, block_fn); 105 + } 106 + 107 + static inline int lib_sha256_base_finish(struct sha256_state *sctx, u8 *out, 108 + unsigned int digest_size) 109 + { 110 110 __be32 *digest = (__be32 *)out; 111 111 int i; 112 112 ··· 122 108 123 109 memzero_explicit(sctx, sizeof(*sctx)); 124 110 return 0; 111 + } 112 + 113 + static inline int sha256_base_finish(struct shash_desc *desc, u8 *out) 114 + { 115 + unsigned int digest_size = crypto_shash_digestsize(desc->tfm); 116 + struct sha256_state *sctx = shash_desc_ctx(desc); 117 + 118 + return lib_sha256_base_finish(sctx, out, digest_size); 125 119 } 126 120 127 121 #endif /* _CRYPTO_SHA256_BASE_H */
+19 -54
lib/crypto/sha256.c
··· 11 11 * Copyright (c) 2014 Red Hat Inc. 12 12 */ 13 13 14 - #include <linux/bitops.h> 15 - #include <linux/export.h> 14 + #include <asm/unaligned.h> 15 + #include <crypto/sha256_base.h> 16 + #include <linux/kernel.h> 16 17 #include <linux/module.h> 17 18 #include <linux/string.h> 18 - #include <crypto/sha2.h> 19 - #include <asm/unaligned.h> 20 19 21 20 static const u32 SHA256_K[] = { 22 21 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, ··· 118 119 state[4] += e; state[5] += f; state[6] += g; state[7] += h; 119 120 } 120 121 121 - void sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len) 122 + static void sha256_transform_blocks(struct sha256_state *sctx, 123 + const u8 *input, int blocks) 122 124 { 123 - unsigned int partial, done; 124 - const u8 *src; 125 125 u32 W[64]; 126 126 127 - partial = sctx->count & 0x3f; 128 - sctx->count += len; 129 - done = 0; 130 - src = data; 127 + do { 128 + sha256_transform(sctx->state, input, W); 129 + input += SHA256_BLOCK_SIZE; 130 + } while (--blocks); 131 131 132 - if ((partial + len) > 63) { 133 - if (partial) { 134 - done = -partial; 135 - memcpy(sctx->buf + partial, data, done + 64); 136 - src = sctx->buf; 137 - } 132 + memzero_explicit(W, sizeof(W)); 133 + } 138 134 139 - do { 140 - sha256_transform(sctx->state, src, W); 141 - done += 64; 142 - src = data + done; 143 - } while (done + 63 < len); 144 - 145 - memzero_explicit(W, sizeof(W)); 146 - 147 - partial = 0; 148 - } 149 - memcpy(sctx->buf + partial, src, len - done); 135 + void sha256_update(struct sha256_state *sctx, const u8 *data, unsigned int len) 136 + { 137 + lib_sha256_base_do_update(sctx, data, len, sha256_transform_blocks); 150 138 } 151 139 EXPORT_SYMBOL(sha256_update); 152 140 153 - static void __sha256_final(struct sha256_state *sctx, u8 *out, int digest_words) 141 + static void __sha256_final(struct sha256_state *sctx, u8 *out, int digest_size) 154 142 { 155 - __be32 *dst = (__be32 *)out; 156 - __be64 bits; 157 - unsigned int index, pad_len; 158 - int i; 159 - static const u8 padding[64] = { 0x80, }; 160 - 161 - /* Save number of bits */ 162 - bits = cpu_to_be64(sctx->count << 3); 163 - 164 - /* Pad out to 56 mod 64. */ 165 - index = sctx->count & 0x3f; 166 - pad_len = (index < 56) ? (56 - index) : ((64+56) - index); 167 - sha256_update(sctx, padding, pad_len); 168 - 169 - /* Append length (before padding) */ 170 - sha256_update(sctx, (const u8 *)&bits, sizeof(bits)); 171 - 172 - /* Store state in digest */ 173 - for (i = 0; i < digest_words; i++) 174 - put_unaligned_be32(sctx->state[i], &dst[i]); 175 - 176 - /* Zeroize sensitive information. */ 177 - memzero_explicit(sctx, sizeof(*sctx)); 143 + lib_sha256_base_do_finalize(sctx, sha256_transform_blocks); 144 + lib_sha256_base_finish(sctx, out, digest_size); 178 145 } 179 146 180 147 void sha256_final(struct sha256_state *sctx, u8 *out) 181 148 { 182 - __sha256_final(sctx, out, 8); 149 + __sha256_final(sctx, out, 32); 183 150 } 184 151 EXPORT_SYMBOL(sha256_final); 185 152 186 153 void sha224_final(struct sha256_state *sctx, u8 *out) 187 154 { 188 - __sha256_final(sctx, out, 7); 155 + __sha256_final(sctx, out, 28); 189 156 } 190 157 EXPORT_SYMBOL(sha224_final); 191 158