aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRenzo Carbonara <[email protected]>2020-08-30 12:27:33 +0300
committerRenzo Carbonara <[email protected]>2020-08-30 12:27:33 +0300
commit31e4080aa2a4a5759247cbefe8c1d34ca2bac315 (patch)
tree4c572dcb2b45495f86316c99d42fcb37343a3662
parentc8a5b53e1d21589529e33f418aac3efc4f70213b (diff)
C: Add blake3_hasher_init_derive_key_len
blake3_hasher_init_derive_key_len is an alternative version of blake3_hasher_init_derive_key which takes the context and its length as separate parameters, and not together as a C string. The motivation for this addition is making it easier for bindings to this C library to call this function without having to first copy over the context bytes just to add one 0x00 byte at the end. Notice that contrary to blake3_hasher_init_derive_key, blake3_hasher_init_derive_key_len allows the inclusion of a 0x00 byte in the context. Given the rules about context string selection, this byte is unlikely to be used as part of a context string. But if for some reason it is ever given, it will be included in the context string and processed like any other non-alphanumeric byte would. For compatibility with blake3_hasher_init_derive_key, bindings should still check for the absence of 0x00 bytes.
-rw-r--r--c/README.md12
-rw-r--r--c/blake3.c9
-rw-r--r--c/blake3.h2
3 files changed, 21 insertions, 2 deletions
diff --git a/c/README.md b/c/README.md
index b1b18a4..139ce2a 100644
--- a/c/README.md
+++ b/c/README.md
@@ -117,6 +117,18 @@ is `"[application] [commit timestamp] [purpose]"`, e.g., `"example.com
2019-12-25 16:18:03 session tokens v1"`.
```c
+void blake3_hasher_init_derive_key_len(
+ blake3_hasher *self,
+ const void *context,
+ size_t context_len);
+```
+
+Like `blake3_hasher_init_derive_key`, except the `context` and its
+length are given as separate parameters, and not together as a C string.
+This can be more convenient than the C string version when writing
+bindings to other languages.
+
+```c
void blake3_hasher_finalize_seek(
const blake3_hasher *self,
uint64_t seek,
diff --git a/c/blake3.c b/c/blake3.c
index c99d059..c2823d1 100644
--- a/c/blake3.c
+++ b/c/blake3.c
@@ -367,10 +367,11 @@ void blake3_hasher_init_keyed(blake3_hasher *self,
hasher_init_base(self, key_words, KEYED_HASH);
}
-void blake3_hasher_init_derive_key(blake3_hasher *self, const char *context) {
+void blake3_hasher_init_derive_key_len(blake3_hasher *self, const void *context,
+ size_t context_len) {
blake3_hasher context_hasher;
hasher_init_base(&context_hasher, IV, DERIVE_KEY_CONTEXT);
- blake3_hasher_update(&context_hasher, context, strlen(context));
+ blake3_hasher_update(&context_hasher, context, context_len);
uint8_t context_key[BLAKE3_KEY_LEN];
blake3_hasher_finalize(&context_hasher, context_key, BLAKE3_KEY_LEN);
uint32_t context_key_words[8];
@@ -378,6 +379,10 @@ void blake3_hasher_init_derive_key(blake3_hasher *self, const char *context) {
hasher_init_base(self, context_key_words, DERIVE_KEY_MATERIAL);
}
+void blake3_hasher_init_derive_key(blake3_hasher *self, const char *context) {
+ blake3_hasher_init_derive_key_len(self, context, strlen(context));
+}
+
// As described in hasher_push_cv() below, we do "lazy merging", delaying
// merges until right before the next CV is about to be added. This is
// different from the reference implementation. Another difference is that we
diff --git a/c/blake3.h b/c/blake3.h
index 5060e38..292d002 100644
--- a/c/blake3.h
+++ b/c/blake3.h
@@ -42,6 +42,8 @@ void blake3_hasher_init(blake3_hasher *self);
void blake3_hasher_init_keyed(blake3_hasher *self,
const uint8_t key[BLAKE3_KEY_LEN]);
void blake3_hasher_init_derive_key(blake3_hasher *self, const char *context);
+void blake3_hasher_init_derive_key_len(blake3_hasher *self, const void *context,
+ size_t context_len);
void blake3_hasher_update(blake3_hasher *self, const void *input,
size_t input_len);
void blake3_hasher_finalize(const blake3_hasher *self, uint8_t *out,