--- a/src/sha.c +++ b/src/sha.c @@ -78,6 +78,7 @@ sha_finish_ctx (struct sha_ctx *ctx, voi { /* Take yet unprocessed bytes into account. */ md5_uint32 bytes = ctx->buflen; + md5_uint32 notswap_bytes; size_t pad; /* Now count remaining bytes. */ @@ -88,10 +89,13 @@ sha_finish_ctx (struct sha_ctx *ctx, voi pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes; memcpy (&ctx->buffer[bytes], fillbuf, pad); - /* Put the 64-bit file length in *bits* at the end of the buffer. */ - *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = NOTSWAP (ctx->total[0] << 3); - *(md5_uint32 *) &ctx->buffer[bytes + pad] = NOTSWAP ((ctx->total[1] << 3) | - (ctx->total[0] >> 29)); + /* Put the 64-bit file length in *bits* at the end of the buffer. + Use memcpy to avoid aliasing problems. On most systems, this + will be optimized away to the same code. */ + notswap_bytes = NOTSWAP (ctx->total[0] << 3); + memcpy (&ctx->buffer[bytes + pad + 4], notswap_bytes, sizeof (notswap_bytes)); + notswap_bytes = NOTSWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29)); + memcpy (&ctx->buffer[bytes + pad], notswap_bytes, sizeof (notswap_bytes)); /* Process last bytes. */ sha_process_block (ctx->buffer, bytes + pad + 8, ctx);