diff options
Diffstat (limited to 'src/microhttpd/sha1.c')
-rw-r--r-- | src/microhttpd/sha1.c | 44 |
1 files changed, 37 insertions, 7 deletions
diff --git a/src/microhttpd/sha1.c b/src/microhttpd/sha1.c index 51deabaa..0d5fef72 100644 --- a/src/microhttpd/sha1.c +++ b/src/microhttpd/sha1.c | |||
@@ -113,6 +113,18 @@ sha1_transform (uint32_t H[_SHA1_DIGEST_LENGTH], | |||
113 | #define GET_W_FROM_DATA(buf,t) \ | 113 | #define GET_W_FROM_DATA(buf,t) \ |
114 | _MHD_GET_32BIT_BE (((const uint8_t*) (buf)) + (t) * SHA1_BYTES_IN_WORD) | 114 | _MHD_GET_32BIT_BE (((const uint8_t*) (buf)) + (t) * SHA1_BYTES_IN_WORD) |
115 | 115 | ||
116 | #ifndef _MHD_GET_32BIT_BE_UNALIGNED | ||
117 | if (0 != ((size_t) data % _MHD_UINT32_ALIGN)) | ||
118 | { | ||
119 | /* Copy the unaligned input data to the aligned buffer */ | ||
120 | memcpy (W, data, SHA1_BLOCK_SIZE); | ||
121 | /* The W[] buffer itself will be used as the source of the data, | ||
122 | * but data will be reloaded in correct bytes order during | ||
123 | * the next steps */ | ||
124 | data = (uint8_t*) W; | ||
125 | } | ||
126 | #endif /* _MHD_GET_32BIT_BE_UNALIGNED */ | ||
127 | |||
116 | /* SHA-1 values of Kt for t=0..19, see FIPS PUB 180-4 paragraph 4.2.1. */ | 128 | /* SHA-1 values of Kt for t=0..19, see FIPS PUB 180-4 paragraph 4.2.1. */ |
117 | #define K00 0x5a827999UL | 129 | #define K00 0x5a827999UL |
118 | /* SHA-1 values of Kt for t=20..39, see FIPS PUB 180-4 paragraph 4.2.1.*/ | 130 | /* SHA-1 values of Kt for t=20..39, see FIPS PUB 180-4 paragraph 4.2.1.*/ |
@@ -336,17 +348,35 @@ MHD_SHA1_finish (void *ctx_, | |||
336 | memset (ctx->buffer + bytes_have, 0, | 348 | memset (ctx->buffer + bytes_have, 0, |
337 | SHA1_BLOCK_SIZE - SHA1_SIZE_OF_LEN_ADD - bytes_have); | 349 | SHA1_BLOCK_SIZE - SHA1_SIZE_OF_LEN_ADD - bytes_have); |
338 | /* Put the number of bits in the processed message as a big-endian value. */ | 350 | /* Put the number of bits in the processed message as a big-endian value. */ |
339 | _MHD_PUT_64BIT_BE (ctx->buffer + SHA1_BLOCK_SIZE - SHA1_SIZE_OF_LEN_ADD, | 351 | _MHD_PUT_64BIT_BE_SAFE (ctx->buffer + SHA1_BLOCK_SIZE - SHA1_SIZE_OF_LEN_ADD, |
340 | num_bits); | 352 | num_bits); |
341 | /* Process the full final block. */ | 353 | /* Process the full final block. */ |
342 | sha1_transform (ctx->H, ctx->buffer); | 354 | sha1_transform (ctx->H, ctx->buffer); |
343 | 355 | ||
344 | /* Put final hash/digest in BE mode */ | 356 | /* Put final hash/digest in BE mode */ |
345 | _MHD_PUT_32BIT_BE (digest + 0 * SHA1_BYTES_IN_WORD, ctx->H[0]); | 357 | #ifndef _MHD_PUT_32BIT_BE_UNALIGNED |
346 | _MHD_PUT_32BIT_BE (digest + 1 * SHA1_BYTES_IN_WORD, ctx->H[1]); | 358 | if (0 != ((uintptr_t) digest) % _MHD_UINT32_ALIGN) |
347 | _MHD_PUT_32BIT_BE (digest + 2 * SHA1_BYTES_IN_WORD, ctx->H[2]); | 359 | { |
348 | _MHD_PUT_32BIT_BE (digest + 3 * SHA1_BYTES_IN_WORD, ctx->H[3]); | 360 | uint32_t alig_dgst[_SHA1_DIGEST_LENGTH]; |
349 | _MHD_PUT_32BIT_BE (digest + 4 * SHA1_BYTES_IN_WORD, ctx->H[4]); | 361 | _MHD_PUT_32BIT_BE (alig_dgst + 0, ctx->H[0]); |
362 | _MHD_PUT_32BIT_BE (alig_dgst + 1, ctx->H[1]); | ||
363 | _MHD_PUT_32BIT_BE (alig_dgst + 2, ctx->H[2]); | ||
364 | _MHD_PUT_32BIT_BE (alig_dgst + 3, ctx->H[3]); | ||
365 | _MHD_PUT_32BIT_BE (alig_dgst + 4, ctx->H[4]); | ||
366 | /* Copy result to unaligned destination address */ | ||
367 | memcpy (digest, alig_dgst, SHA1_DIGEST_SIZE); | ||
368 | } | ||
369 | else | ||
370 | #else /* _MHD_PUT_32BIT_BE_UNALIGNED */ | ||
371 | if (1) | ||
372 | #endif /* _MHD_PUT_32BIT_BE_UNALIGNED */ | ||
373 | { | ||
374 | _MHD_PUT_32BIT_BE (digest + 0 * SHA1_BYTES_IN_WORD, ctx->H[0]); | ||
375 | _MHD_PUT_32BIT_BE (digest + 1 * SHA1_BYTES_IN_WORD, ctx->H[1]); | ||
376 | _MHD_PUT_32BIT_BE (digest + 2 * SHA1_BYTES_IN_WORD, ctx->H[2]); | ||
377 | _MHD_PUT_32BIT_BE (digest + 3 * SHA1_BYTES_IN_WORD, ctx->H[3]); | ||
378 | _MHD_PUT_32BIT_BE (digest + 4 * SHA1_BYTES_IN_WORD, ctx->H[4]); | ||
379 | } | ||
350 | 380 | ||
351 | /* Erase potentially sensitive data. */ | 381 | /* Erase potentially sensitive data. */ |
352 | memset (ctx, 0, sizeof(struct sha1_ctx)); | 382 | memset (ctx, 0, sizeof(struct sha1_ctx)); |