From 5e748bbc8c0ec4a467870fe2606bc94d8d651b53 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sun, 21 Apr 2013 14:17:25 +0200 Subject: [PATCH] Add a streaming interface to crypto_generichash --- .../crypto_generichash/blake2/ref/blake2.h | 6 +++ .../blake2/ref/blake2s-ref.c | 1 + .../blake2/ref/generichash_blake2b.c | 41 ++++++++++++++++++- .../crypto_generichash/crypto_generichash.c | 22 +++++++++- .../include/sodium/crypto_generichash.h | 14 ++++++- .../sodium/crypto_generichash_blake2b.h | 40 ++++++++++++++++-- 6 files changed, 116 insertions(+), 8 deletions(-) diff --git a/src/libsodium/crypto_generichash/blake2/ref/blake2.h b/src/libsodium/crypto_generichash/blake2/ref/blake2.h index f8aba833..6e865f4d 100644 --- a/src/libsodium/crypto_generichash/blake2/ref/blake2.h +++ b/src/libsodium/crypto_generichash/blake2/ref/blake2.h @@ -17,6 +17,8 @@ #include #include +#include "crypto_generichash_blake2b.h" + #if defined(_MSC_VER) #define ALIGN(x) __declspec(align(x)) #else @@ -86,6 +88,9 @@ extern "C" { uint8_t personal[BLAKE2B_PERSONALBYTES]; // 64 } blake2b_param; +#ifndef DEFINE_BLAKE2B_STATE +typedef crypto_generichash_blake2b_state blake2b_state; +#else ALIGN( 64 ) typedef struct __blake2b_state { uint64_t h[8]; @@ -95,6 +100,7 @@ extern "C" { size_t buflen; uint8_t last_node; } blake2b_state; +#endif typedef struct __blake2sp_state { diff --git a/src/libsodium/crypto_generichash/blake2/ref/blake2s-ref.c b/src/libsodium/crypto_generichash/blake2/ref/blake2s-ref.c index ba392c63..d7f74a6d 100644 --- a/src/libsodium/crypto_generichash/blake2/ref/blake2s-ref.c +++ b/src/libsodium/crypto_generichash/blake2/ref/blake2s-ref.c @@ -15,6 +15,7 @@ #include #include +#include "crypto_generichash_blake2b.h" #include "blake2.h" #include "blake2-impl.h" diff --git a/src/libsodium/crypto_generichash/blake2/ref/generichash_blake2b.c b/src/libsodium/crypto_generichash/blake2/ref/generichash_blake2b.c index 897dbb50..bd2c795f 100644 --- a/src/libsodium/crypto_generichash/blake2/ref/generichash_blake2b.c +++ b/src/libsodium/crypto_generichash/blake2/ref/generichash_blake2b.c @@ -20,5 +20,44 @@ crypto_generichash_blake2b(unsigned char *out, const unsigned char *in, assert(keylen <= UINT8_MAX); return blake2b((uint8_t *) out, in, key, - (uint8_t) outlen, inlen, (uint8_t) keylen); + (uint8_t) outlen, (uint64_t) inlen, (uint8_t) keylen); +} + +int +crypto_generichash_blake2b_init(crypto_generichash_blake2b_state *state, + const unsigned char *key, + const size_t keylen, + const size_t outlen) +{ + if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES || + keylen > BLAKE2B_KEYBYTES) { + return -1; + } + assert(outlen <= UINT8_MAX); + assert(keylen <= UINT8_MAX); + if (blake2b_init(state, (uint8_t) outlen) != 0) { + return -1; + } + if (key != NULL && keylen > 0U && + blake2b_init_key(state, (uint8_t) outlen, key, keylen) != 0) { + return -1; + } + return 0; +} + +int +crypto_generichash_blake2b_update(crypto_generichash_blake2b_state *state, + const unsigned char *in, + unsigned long long inlen) +{ + return blake2b_update(state, (const uint8_t *) in, (uint64_t) inlen); +} + +int +crypto_generichash_blake2b_final(crypto_generichash_blake2b_state *state, + unsigned char *out, + const size_t outlen) +{ + assert(outlen <= UINT8_MAX); + return blake2b_final(state, (uint8_t *) out, (uint8_t) outlen); } diff --git a/src/libsodium/crypto_generichash/crypto_generichash.c b/src/libsodium/crypto_generichash/crypto_generichash.c index 3bc10031..b4ba0671 100644 --- a/src/libsodium/crypto_generichash/crypto_generichash.c +++ b/src/libsodium/crypto_generichash/crypto_generichash.c @@ -9,7 +9,25 @@ crypto_generichash(unsigned char *out, const unsigned char *in, return crypto_generichash_blake2b(out, in, key, outlen, inlen, keylen); } +int +crypto_generichash_init(crypto_generichash_blake2b_state *state, + const unsigned char *key, + const size_t keylen, const size_t outlen) +{ + return crypto_generichash_blake2b_init(state, key, keylen, outlen); +} +int +crypto_generichash_update(crypto_generichash_blake2b_state *state, + const unsigned char *in, + unsigned long long inlen) +{ + return crypto_generichash_blake2b_update(state, in, inlen); +} - - +int +crypto_generichash_final(crypto_generichash_blake2b_state *state, + unsigned char *out, const size_t outlen) +{ + return crypto_generichash_blake2b_final(state, out, outlen); +} diff --git a/src/libsodium/include/sodium/crypto_generichash.h b/src/libsodium/include/sodium/crypto_generichash.h index 2e451634..77212338 100644 --- a/src/libsodium/include/sodium/crypto_generichash.h +++ b/src/libsodium/include/sodium/crypto_generichash.h @@ -9,10 +9,22 @@ #define crypto_generichash_KEYBYTES_MAX crypto_generichash_blake2b_KEYBYTES_MAX #define crypto_generichash_PRIMITIVE "blake2b" +typedef crypto_generichash_blake2b_state crypto_generichash_state; + int crypto_generichash(unsigned char *out, const unsigned char *in, const unsigned char *key, size_t outlen, unsigned long long inlen, size_t keylen); -#endif +int crypto_generichash_init(crypto_generichash_blake2b_state *state, + const unsigned char *key, + const size_t keylen, const size_t outlen); +int crypto_generichash_update(crypto_generichash_blake2b_state *state, + const unsigned char *in, + unsigned long long inlen); + +int crypto_generichash_final(crypto_generichash_blake2b_state *state, + unsigned char *out, const size_t outlen); + +#endif diff --git a/src/libsodium/include/sodium/crypto_generichash_blake2b.h b/src/libsodium/include/sodium/crypto_generichash_blake2b.h index 60914662..1dba21fe 100644 --- a/src/libsodium/include/sodium/crypto_generichash_blake2b.h +++ b/src/libsodium/include/sodium/crypto_generichash_blake2b.h @@ -1,12 +1,31 @@ #ifndef crypto_generichash_blake2b_H #define crypto_generichash_blake2b_H +#include #include -#define crypto_generichash_blake2b_BYTES_MIN 1U -#define crypto_generichash_blake2b_BYTES_MAX 64U -#define crypto_generichash_blake2b_KEYBYTES_MIN 0U -#define crypto_generichash_blake2b_KEYBYTES_MAX 64U +#define crypto_generichash_blake2b_BYTES_MIN 1U +#define crypto_generichash_blake2b_BYTES_MAX 64U +#define crypto_generichash_blake2b_KEYBYTES_MIN 0U +#define crypto_generichash_blake2b_KEYBYTES_MAX 64U +#define crypto_generichash_blake2b_BLOCKBYTES 128U + +#if defined(_MSC_VER) +# define CRYPTO_ALIGN(x) __declspec(align(x)) +#else +# define CRYPTO_ALIGN(x) __attribute__((aligned(x))) +#endif + +#pragma pack(push, 1) +CRYPTO_ALIGN(64) typedef struct { + uint64_t h[8]; + uint64_t t[2]; + uint64_t f[2]; + uint8_t buf[2 * crypto_generichash_blake2b_BLOCKBYTES]; + size_t buflen; + uint8_t last_node; +} crypto_generichash_blake2b_state; +#pragma pack(pop) #ifdef __cplusplus extern "C" { @@ -18,6 +37,19 @@ int crypto_generichash_blake2b_ref(unsigned char *out, unsigned long long inlen, size_t keylen); +int crypto_generichash_blake2b_init(crypto_generichash_blake2b_state *state, + const unsigned char *key, + const size_t keylen, + const size_t outlen); + +int crypto_generichash_blake2b_update(crypto_generichash_blake2b_state *state, + const unsigned char *in, + unsigned long long inlen); + +int crypto_generichash_blake2b_final(crypto_generichash_blake2b_state *state, + unsigned char *out, + const size_t outlen); + #ifdef __cplusplus } #endif