Add a streaming interface to crypto_generichash
This commit is contained in:
parent
76cb00d56f
commit
5e748bbc8c
@ -17,6 +17,8 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#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
|
||||
{
|
||||
|
@ -15,6 +15,7 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "crypto_generichash_blake2b.h"
|
||||
#include "blake2.h"
|
||||
#include "blake2-impl.h"
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -1,12 +1,31 @@
|
||||
#ifndef crypto_generichash_blake2b_H
|
||||
#define crypto_generichash_blake2b_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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
|
||||
|
Loading…
Reference in New Issue
Block a user