diff options
-rw-r--r-- | crypto/sha256_generic.c | 65 |
1 files changed, 30 insertions, 35 deletions
diff --git a/crypto/sha256_generic.c b/crypto/sha256_generic.c index 6349d8339d37..e58c71bdf333 100644 --- a/crypto/sha256_generic.c +++ b/crypto/sha256_generic.c | |||
@@ -26,7 +26,7 @@ | |||
26 | #include <asm/byteorder.h> | 26 | #include <asm/byteorder.h> |
27 | 27 | ||
28 | struct sha256_ctx { | 28 | struct sha256_ctx { |
29 | u32 count[2]; | 29 | u64 count; |
30 | u32 state[8]; | 30 | u32 state[8]; |
31 | u8 buf[128]; | 31 | u8 buf[128]; |
32 | }; | 32 | }; |
@@ -231,8 +231,7 @@ static int sha224_init(struct shash_desc *desc) | |||
231 | sctx->state[5] = SHA224_H5; | 231 | sctx->state[5] = SHA224_H5; |
232 | sctx->state[6] = SHA224_H6; | 232 | sctx->state[6] = SHA224_H6; |
233 | sctx->state[7] = SHA224_H7; | 233 | sctx->state[7] = SHA224_H7; |
234 | sctx->count[0] = 0; | 234 | sctx->count = 0; |
235 | sctx->count[1] = 0; | ||
236 | 235 | ||
237 | return 0; | 236 | return 0; |
238 | } | 237 | } |
@@ -248,7 +247,7 @@ static int sha256_init(struct shash_desc *desc) | |||
248 | sctx->state[5] = SHA256_H5; | 247 | sctx->state[5] = SHA256_H5; |
249 | sctx->state[6] = SHA256_H6; | 248 | sctx->state[6] = SHA256_H6; |
250 | sctx->state[7] = SHA256_H7; | 249 | sctx->state[7] = SHA256_H7; |
251 | sctx->count[0] = sctx->count[1] = 0; | 250 | sctx->count = 0; |
252 | 251 | ||
253 | return 0; | 252 | return 0; |
254 | } | 253 | } |
@@ -257,33 +256,30 @@ static int sha256_update(struct shash_desc *desc, const u8 *data, | |||
257 | unsigned int len) | 256 | unsigned int len) |
258 | { | 257 | { |
259 | struct sha256_ctx *sctx = shash_desc_ctx(desc); | 258 | struct sha256_ctx *sctx = shash_desc_ctx(desc); |
260 | unsigned int i, index, part_len; | 259 | unsigned int partial, done; |
261 | 260 | const u8 *src; | |
262 | /* Compute number of bytes mod 128 */ | 261 | |
263 | index = (unsigned int)((sctx->count[0] >> 3) & 0x3f); | 262 | partial = sctx->count & 0x3f; |
264 | 263 | sctx->count += len; | |
265 | /* Update number of bits */ | 264 | done = 0; |
266 | if ((sctx->count[0] += (len << 3)) < (len << 3)) { | 265 | src = data; |
267 | sctx->count[1]++; | 266 | |
268 | sctx->count[1] += (len >> 29); | 267 | if ((partial + len) > 63) { |
268 | if (partial) { | ||
269 | done = -partial; | ||
270 | memcpy(sctx->buf + partial, data, done + 64); | ||
271 | src = sctx->buf; | ||
272 | } | ||
273 | |||
274 | do { | ||
275 | sha256_transform(sctx->state, src); | ||
276 | done += 64; | ||
277 | src = data + done; | ||
278 | } while (done + 63 < len); | ||
279 | |||
280 | partial = 0; | ||
269 | } | 281 | } |
270 | 282 | memcpy(sctx->buf + partial, src, len - done); | |
271 | part_len = 64 - index; | ||
272 | |||
273 | /* Transform as many times as possible. */ | ||
274 | if (len >= part_len) { | ||
275 | memcpy(&sctx->buf[index], data, part_len); | ||
276 | sha256_transform(sctx->state, sctx->buf); | ||
277 | |||
278 | for (i = part_len; i + 63 < len; i += 64) | ||
279 | sha256_transform(sctx->state, &data[i]); | ||
280 | index = 0; | ||
281 | } else { | ||
282 | i = 0; | ||
283 | } | ||
284 | |||
285 | /* Buffer remaining input */ | ||
286 | memcpy(&sctx->buf[index], &data[i], len-i); | ||
287 | 283 | ||
288 | return 0; | 284 | return 0; |
289 | } | 285 | } |
@@ -292,22 +288,21 @@ static int sha256_final(struct shash_desc *desc, u8 *out) | |||
292 | { | 288 | { |
293 | struct sha256_ctx *sctx = shash_desc_ctx(desc); | 289 | struct sha256_ctx *sctx = shash_desc_ctx(desc); |
294 | __be32 *dst = (__be32 *)out; | 290 | __be32 *dst = (__be32 *)out; |
295 | __be32 bits[2]; | 291 | __be64 bits; |
296 | unsigned int index, pad_len; | 292 | unsigned int index, pad_len; |
297 | int i; | 293 | int i; |
298 | static const u8 padding[64] = { 0x80, }; | 294 | static const u8 padding[64] = { 0x80, }; |
299 | 295 | ||
300 | /* Save number of bits */ | 296 | /* Save number of bits */ |
301 | bits[1] = cpu_to_be32(sctx->count[0]); | 297 | bits = cpu_to_be64(sctx->count << 3); |
302 | bits[0] = cpu_to_be32(sctx->count[1]); | ||
303 | 298 | ||
304 | /* Pad out to 56 mod 64. */ | 299 | /* Pad out to 56 mod 64. */ |
305 | index = (sctx->count[0] >> 3) & 0x3f; | 300 | index = sctx->count & 0x3f; |
306 | pad_len = (index < 56) ? (56 - index) : ((64+56) - index); | 301 | pad_len = (index < 56) ? (56 - index) : ((64+56) - index); |
307 | sha256_update(desc, padding, pad_len); | 302 | sha256_update(desc, padding, pad_len); |
308 | 303 | ||
309 | /* Append length (before padding) */ | 304 | /* Append length (before padding) */ |
310 | sha256_update(desc, (const u8 *)bits, sizeof(bits)); | 305 | sha256_update(desc, (const u8 *)&bits, sizeof(bits)); |
311 | 306 | ||
312 | /* Store state in digest */ | 307 | /* Store state in digest */ |
313 | for (i = 0; i < 8; i++) | 308 | for (i = 0; i < 8; i++) |