1
0
Fork 0
mirror of https://github.com/BLAKE3-team/BLAKE3 synced 2024-05-04 23:26:15 +02:00

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.
This commit is contained in:
Renzo Carbonara 2020-08-30 12:27:33 +03:00
parent c8a5b53e1d
commit 31e4080aa2
3 changed files with 21 additions and 2 deletions

View File

@ -116,6 +116,18 @@ and application-specific. A good default format for the context string
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,

View File

@ -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

View File

@ -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,