aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack O'Connor <[email protected]>2020-09-29 11:25:53 -0400
committerJack O'Connor <[email protected]>2020-09-29 16:09:28 -0400
commit0b13637ae31c2e7e1a471e39258606fabb01685e (patch)
tree3bdf24a9fe41bcc7ba1f11b3af4b8168bbdadce5
parent3817999f17a249f58678c5aa0f5508c24367b821 (diff)
fix a couple of big-endianness mistakes in blake3.c
Kudos to @pascal-cuoq and @jakub-zwolakowski from TrustInSoft for catching these bugs. Original report: https://github.com/BLAKE3-team/BLAKE3/pull/118
-rw-r--r--c/blake3.c4
-rw-r--r--c/blake3_impl.h19
-rw-r--r--c/blake3_portable.c10
3 files changed, 22 insertions, 11 deletions
diff --git a/c/blake3.c b/c/blake3.c
index 71aed71..741a76d 100644
--- a/c/blake3.c
+++ b/c/blake3.c
@@ -81,7 +81,7 @@ INLINE void output_chaining_value(const output_t *self, uint8_t cv[32]) {
memcpy(cv_words, self->input_cv, 32);
blake3_compress_in_place(cv_words, self->block, self->block_len,
self->counter, self->flags);
- memcpy(cv, cv_words, 32);
+ store_cv_words(cv, cv_words);
}
INLINE void output_root_bytes(const output_t *self, uint64_t seek, uint8_t *out,
@@ -367,7 +367,7 @@ void blake3_hasher_init_keyed(blake3_hasher *self,
hasher_init_base(self, key_words, KEYED_HASH);
}
-void blake3_hasher_init_derive_key_raw(blake3_hasher *self, const void *context,
+void blake3_hasher_init_derive_key_raw(blake3_hasher *self, const void *context,
size_t context_len) {
blake3_hasher context_hasher;
hasher_init_base(&context_hasher, IV, DERIVE_KEY_CONTEXT);
diff --git a/c/blake3_impl.h b/c/blake3_impl.h
index b4a38c7..86ab6aa 100644
--- a/c/blake3_impl.h
+++ b/c/blake3_impl.h
@@ -146,6 +146,25 @@ INLINE void load_key_words(const uint8_t key[BLAKE3_KEY_LEN],
key_words[7] = load32(&key[7 * 4]);
}
+INLINE void store32(void *dst, uint32_t w) {
+ uint8_t *p = (uint8_t *)dst;
+ p[0] = (uint8_t)(w >> 0);
+ p[1] = (uint8_t)(w >> 8);
+ p[2] = (uint8_t)(w >> 16);
+ p[3] = (uint8_t)(w >> 24);
+}
+
+INLINE void store_cv_words(uint8_t bytes_out[32], uint32_t cv_words[8]) {
+ store32(&bytes_out[0 * 4], cv_words[0]);
+ store32(&bytes_out[1 * 4], cv_words[1]);
+ store32(&bytes_out[2 * 4], cv_words[2]);
+ store32(&bytes_out[3 * 4], cv_words[3]);
+ store32(&bytes_out[4 * 4], cv_words[4]);
+ store32(&bytes_out[5 * 4], cv_words[5]);
+ store32(&bytes_out[6 * 4], cv_words[6]);
+ store32(&bytes_out[7 * 4], cv_words[7]);
+}
+
void blake3_compress_in_place(uint32_t cv[8],
const uint8_t block[BLAKE3_BLOCK_LEN],
uint8_t block_len, uint64_t counter,
diff --git a/c/blake3_portable.c b/c/blake3_portable.c
index 9ee2f4a..062dd1b 100644
--- a/c/blake3_portable.c
+++ b/c/blake3_portable.c
@@ -1,14 +1,6 @@
#include "blake3_impl.h"
#include <string.h>
-INLINE void store32(void *dst, uint32_t w) {
- uint8_t *p = (uint8_t *)dst;
- p[0] = (uint8_t)(w >> 0);
- p[1] = (uint8_t)(w >> 8);
- p[2] = (uint8_t)(w >> 16);
- p[3] = (uint8_t)(w >> 24);
-}
-
INLINE uint32_t rotr32(uint32_t w, uint32_t c) {
return (w >> c) | (w << (32 - c));
}
@@ -147,7 +139,7 @@ INLINE void hash_one_portable(const uint8_t *input, size_t blocks,
blocks -= 1;
block_flags = flags;
}
- memcpy(out, cv, 32);
+ store_cv_words(out, cv);
}
void blake3_hash_many_portable(const uint8_t *const *inputs, size_t num_inputs,