libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / libsodium / src / libsodium / crypto_generichash / blake2 / ref / generichash_blake2b.c
blob8329ba38a12cfaad959416badcaa3fdd8fa58857
2 #include <assert.h>
3 #include <limits.h>
4 #include <stdint.h>
6 #include "api.h"
7 #include "blake2.h"
9 int
10 crypto_generichash_blake2b(unsigned char *out, size_t outlen,
11 const unsigned char *in, unsigned long long inlen,
12 const unsigned char *key, size_t keylen)
14 if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES ||
15 keylen > BLAKE2B_KEYBYTES || inlen > UINT64_MAX) {
16 return -1;
18 assert(outlen <= UINT8_MAX);
19 assert(keylen <= UINT8_MAX);
21 return blake2b((uint8_t *) out, in, key,
22 (uint8_t) outlen, (uint64_t) inlen, (uint8_t) keylen);
25 int
26 crypto_generichash_blake2b_init(crypto_generichash_blake2b_state *state,
27 const unsigned char *key,
28 const size_t keylen, const size_t outlen)
30 if (outlen <= 0U || outlen > BLAKE2B_OUTBYTES ||
31 keylen > BLAKE2B_KEYBYTES) {
32 return -1;
34 assert(outlen <= UINT8_MAX);
35 assert(keylen <= UINT8_MAX);
36 if (blake2b_init(state, (uint8_t) outlen) != 0) {
37 return -1;
39 if (key != NULL && keylen > 0U &&
40 blake2b_init_key(state, (uint8_t) outlen, key, keylen) != 0) {
41 return -1;
43 return 0;
46 int
47 crypto_generichash_blake2b_update(crypto_generichash_blake2b_state *state,
48 const unsigned char *in,
49 unsigned long long inlen)
51 return blake2b_update(state, (const uint8_t *) in, (uint64_t) inlen);
54 int
55 crypto_generichash_blake2b_final(crypto_generichash_blake2b_state *state,
56 unsigned char *out,
57 const size_t outlen)
59 assert(outlen <= UINT8_MAX);
60 return blake2b_final(state, (uint8_t *) out, (uint8_t) outlen);