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