26 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
27 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
28 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
29 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
32static const uint8_t blake2b_sigma[12][16] =
34 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
35 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
36 { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } ,
37 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } ,
38 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } ,
39 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } ,
40 { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } ,
41 { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } ,
42 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } ,
43 { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } ,
44 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
45 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
59 if(
S->last_node) blake2b_set_lastnode(S);
66 S->t[1] += (
S->t[0] < inc);
73 for(i = 0; i < 8; ++i)
S->h[i] = blake2b_IV[i];
84 for(i = 0; i < 8; ++i)
85 S->h[i] ^= load64(p +
sizeof(S->h[i]) * i);
87 S->outlen = P->digest_length;
96 P->digest_length = (
uint8_t)outlen;
100 store32(&P->leaf_length, 0);
101 store32(&P->node_offset, 0);
102 store32(&P->xof_length, 0);
105 memset(P->reserved, 0,
sizeof(P->reserved));
106 memset(P->salt, 0,
sizeof(P->salt));
107 memset(P->personal, 0,
sizeof(P->personal));
119 P->digest_length = (
uint8_t)outlen;
120 P->key_length = (
uint8_t)keylen;
123 store32(&P->leaf_length, 0);
124 store32(&P->node_offset, 0);
125 store32(&P->xof_length, 0);
128 memset(P->reserved, 0,
sizeof(P->reserved));
129 memset(P->salt, 0,
sizeof(P->salt));
130 memset(P->personal, 0,
sizeof(P->personal));
137 memcpy(block, key, keylen);
144#define G(r,i,a,b,c,d) \
146 a = a + b + m[blake2b_sigma[r][2*i+0]]; \
147 d = rotr64(d ^ a, 32); \
149 b = rotr64(b ^ c, 24); \
150 a = a + b + m[blake2b_sigma[r][2*i+1]]; \
151 d = rotr64(d ^ a, 16); \
153 b = rotr64(b ^ c, 63); \
158 G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
159 G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
160 G(r,2,v[ 2],v[ 6],v[10],v[14]); \
161 G(r,3,v[ 3],v[ 7],v[11],v[15]); \
162 G(r,4,v[ 0],v[ 5],v[10],v[15]); \
163 G(r,5,v[ 1],v[ 6],v[11],v[12]); \
164 G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
165 G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
173 for(i = 0; i < 16; ++i) {
174 m[i] = load64(block + i *
sizeof(m[i]));
177 for(i = 0; i < 8; ++i) {
181 v[8] = blake2b_IV[0];
182 v[9] = blake2b_IV[1];
183 v[10] = blake2b_IV[2];
184 v[11] = blake2b_IV[3];
185 v[12] = blake2b_IV[4] ^ S->t[0];
186 v[13] = blake2b_IV[5] ^ S->t[1];
187 v[14] = blake2b_IV[6] ^ S->f[0];
188 v[15] = blake2b_IV[7] ^ S->f[1];
203 for(i = 0; i < 8; ++i) {
204 S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
212 const unsigned char* in = (
const unsigned char*)pin;
214 size_t left = S->buflen;
218 memcpy(S->buf + left, in, fill);
220 blake2b_compress(S, S->buf);
221 in += fill; inlen -= fill;
224 blake2b_compress(S, in);
229 memcpy(S->buf + S->buflen, in, inlen);
239 if(out == NULL || outlen < S->outlen)
242 if(blake2b_is_lastblock(S))
245 blake2b_increment_counter(S, S->buflen);
246 blake2b_set_lastblock(S);
248 blake2b_compress(S, S->buf);
250 for(i = 0; i < 8; ++i)
251 store64(buffer +
sizeof(S->h[i]) * i, S->h[i]);
253 memcpy(out, buffer, S->outlen);
254 secure_zero_memory(buffer,
sizeof(buffer));
259int blake2b(
void* out,
size_t outlen,
const void* in,
size_t inlen,
const void* key,
size_t keylen) {
263 if(NULL == in && inlen > 0)
return -1;
265 if(NULL == out)
return -1;
267 if(NULL == key && keylen > 0)
return -1;
284int blake2(
void* out,
size_t outlen,
const void* in,
size_t inlen,
const void* key,
size_t keylen) {
285 return blake2b(out, outlen, in, inlen, key, keylen);
int blake2b_init_key(blake2b_state *S, size_t outlen, const void *key, size_t keylen)
int blake2b(void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen)
int blake2b_update(blake2b_state *S, const void *pin, size_t inlen)
int blake2b_init(blake2b_state *S, size_t outlen)
int blake2(void *out, size_t outlen, const void *in, size_t inlen, const void *key, size_t keylen)
int blake2b_final(blake2b_state *S, void *out, size_t outlen)
int blake2b_init_param(blake2b_state *S, const blake2b_param *P)
struct blake2b_param__ blake2b_param