X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fsha256.c;h=0ad9444351fbff98706303bdb41a48e99a441732;hb=9fbf8e1a391ab49dca9c608a32af9a42831f3445;hp=4a632c9fbdc7d192a0412b7356868e1124aab690;hpb=c0e92a8bc2db0f1ea4df59b1d6812323b0e58233;p=gnulib.git diff --git a/lib/sha256.c b/lib/sha256.c index 4a632c9fb..0ad944435 100644 --- a/lib/sha256.c +++ b/lib/sha256.c @@ -126,7 +126,7 @@ static void sha256_conclude_ctx (struct sha256_ctx *ctx) { /* Take yet unprocessed bytes into account. */ - uint32_t bytes = ctx->buflen; + size_t bytes = ctx->buflen; size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4; /* Now count remaining bytes. */ @@ -134,9 +134,13 @@ sha256_conclude_ctx (struct sha256_ctx *ctx) if (ctx->total[0] < bytes) ++ctx->total[1]; - /* Put the 64-bit file length in *bits* at the end of the buffer. */ - ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29)); - ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3); + /* Put the 64-bit file length in *bits* at the end of the buffer. + Use set_uint32 rather than a simple assignment, to avoid risk of + unaligned access. */ + set_uint32 ((char *) &ctx->buffer[size - 2], + SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29))); + set_uint32 ((char *) &ctx->buffer[size - 1], + SWAP (ctx->total[0] << 3)); memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);