From 297babd42778c7453070a73324db21ab4a35df6b Mon Sep 17 00:00:00 2001 From: Justin W Smith <103147162+justsmth@users.noreply.github.com> Date: Tue, 10 Oct 2023 15:41:37 -0400 Subject: [PATCH] Fix Blake2b for BigEndian (#1235) * Fix Blake2b for BigEndian * Use BLAKE2B256_DIGEST_LENGTH constant * Use CRYPTO_store_u64_le --- crypto/blake2/blake2.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crypto/blake2/blake2.c b/crypto/blake2/blake2.c index 38a320d2ce..96f65eb95d 100644 --- a/crypto/blake2/blake2.c +++ b/crypto/blake2/blake2.c @@ -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) { @@ -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,