Skip to content

Commit

Permalink
Fix SHA256 bug due to strict aliasing violations with newer GCC optim…
Browse files Browse the repository at this point in the history
…izations (#4).
  • Loading branch information
archiecobbs committed Jun 7, 2023
1 parent 16ceccd commit 864c1cf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Version Next

- Fixed SHA256 bug due to strict aliasing violations with newer GCC optimizations (#4).
- Add hash test vectors (contributed by Aleksa Sarai)

Version 1.0.1 released August 29, 2020

- Avoid defining PACKAGE, etc. in installed header files
Expand Down
6 changes: 3 additions & 3 deletions sha2.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ void SHA256_Final(sha2_byte digest[SHA256_DIGEST_LENGTH], SHA256_CTX* context) {
*context->buffer = 0x80;
}
/* Set the bit count: */
*(sha2_word64*)(void *)&context->buffer[SHA256_SHORT_BLOCK_LENGTH] = context->bitcount;
memcpy(&context->buffer[SHA256_SHORT_BLOCK_LENGTH], &context->bitcount, sizeof(context->bitcount));

/* Final transform: */
SHA256_Transform(context, (sha2_word32*)(void *)context->buffer);
Expand Down Expand Up @@ -870,8 +870,8 @@ static void SHA512_Last(SHA512_CTX* context) {
*context->buffer = 0x80;
}
/* Store the length of input data (in bits): */
*(sha2_word64*)(void *)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];
*(sha2_word64*)(void *)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];
memcpy(&context->buffer[SHA512_SHORT_BLOCK_LENGTH], &context->bitcount[1], sizeof(context->bitcount[1]));
memcpy(&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8], &context->bitcount[0], sizeof(context->bitcount[0]));

/* Final transform: */
SHA512_Transform(context, (sha2_word64*)(void *)context->buffer);
Expand Down

0 comments on commit 864c1cf

Please sign in to comment.