Skip to content

Commit

Permalink
Fix Blake2b for BigEndian (#1235)
Browse files Browse the repository at this point in the history
* Fix Blake2b for BigEndian

* Use BLAKE2B256_DIGEST_LENGTH constant

* Use CRYPTO_store_u64_le
  • Loading branch information
justsmth authored Oct 10, 2023
1 parent 1ba686a commit 297babd
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion crypto/blake2/blake2.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ static uint64_t blake2b_load(const uint8_t block[BLAKE2B_CBLOCK], size_t i) {
return CRYPTO_load_u64_le(block + 8 * i);
}

static void copy_digest_words_to_dest(uint8_t* dest, uint64_t* src, size_t word_count) {
#ifdef OPENSSL_BIG_ENDIAN
for(size_t i = 0; i < word_count; i++) {
CRYPTO_store_u64_le(&dest[i * sizeof(uint64_t)], src[i]);
}
#else
OPENSSL_memcpy(dest, src, word_count * sizeof(uint64_t));
#endif
}

static void blake2b_transform(BLAKE2B_CTX *b2b,
const uint8_t block[BLAKE2B_CBLOCK],
size_t num_bytes, int is_final_block) {
Expand Down Expand Up @@ -158,7 +168,7 @@ void BLAKE2B256_Final(uint8_t out[BLAKE2B256_DIGEST_LENGTH], BLAKE2B_CTX *b2b) {
blake2b_transform(b2b, b2b->block, b2b->block_used,
/*is_final_block=*/1);
OPENSSL_STATIC_ASSERT(BLAKE2B256_DIGEST_LENGTH <= sizeof(b2b->h), _)
memcpy(out, b2b->h, BLAKE2B256_DIGEST_LENGTH);
copy_digest_words_to_dest(out, b2b->h, BLAKE2B256_DIGEST_LENGTH / 8);
}

void BLAKE2B256(const uint8_t *data, size_t len,
Expand Down

0 comments on commit 297babd

Please sign in to comment.