19 #define ROTR64(x, y) (((x) >> (y)) ^ ((x) << (64 - (y)))) 24 #define B2B_GET64(p) \ 25 (((uint64_t)((uint8_t *)(p))[0]) ^ (((uint64_t)((uint8_t *)(p))[1]) << 8) \ 26 ^ (((uint64_t)((uint8_t *)(p))[2]) << 16) \ 27 ^ (((uint64_t)((uint8_t *)(p))[3]) << 24) \ 28 ^ (((uint64_t)((uint8_t *)(p))[4]) << 32) \ 29 ^ (((uint64_t)((uint8_t *)(p))[5]) << 40) \ 30 ^ (((uint64_t)((uint8_t *)(p))[6]) << 48) \ 31 ^ (((uint64_t)((uint8_t *)(p))[7]) << 56)) 35 #define B2B_G(a, b, c, d, x, y) \ 37 v[a] = v[a] + v[b] + (x); \ 38 v[d] = ROTR64(v[d] ^ v[a], 32); \ 40 v[b] = ROTR64(v[b] ^ v[c], 24); \ 41 v[a] = v[a] + v[b] + (y); \ 42 v[d] = ROTR64(v[d] ^ v[a], 16); \ 44 v[b] = ROTR64(v[b] ^ v[c], 63); \ 49 static const uint64_t
blake2b_iv[8] = {0x6A09E667F3BCC908,
61 const uint8_t sigma[12][16] = {
62 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
63 {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
64 {11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4},
65 {7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8},
66 {9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13},
67 {2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9},
68 {12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11},
69 {13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10},
70 {6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5},
71 {10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0},
72 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
73 {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3}};
78 for (i = 0; i < 8; i++) {
80 v[i + 8] = blake2b_iv[i];
89 for (i = 0; i < 16; i++) {
93 for (i = 0; i < 12; i++) {
94 B2B_G(0, 4, 8, 12, m[sigma[i][0]], m[sigma[i][1]]);
95 B2B_G(1, 5, 9, 13, m[sigma[i][2]], m[sigma[i][3]]);
96 B2B_G(2, 6, 10, 14, m[sigma[i][4]], m[sigma[i][5]]);
97 B2B_G(3, 7, 11, 15, m[sigma[i][6]], m[sigma[i][7]]);
98 B2B_G(0, 5, 10, 15, m[sigma[i][8]], m[sigma[i][9]]);
99 B2B_G(1, 6, 11, 12, m[sigma[i][10]], m[sigma[i][11]]);
100 B2B_G(2, 7, 8, 13, m[sigma[i][12]], m[sigma[i][13]]);
101 B2B_G(3, 4, 9, 14, m[sigma[i][14]], m[sigma[i][15]]);
104 for (i = 0; i < 8; ++i) {
105 ctx->
h[i] ^= v[i] ^ v[i + 8];
120 if (outlen == 0 || outlen > 64 || keylen > 64) {
124 for (i = 0; i < 8; i++) {
125 ctx->
h[i] = blake2b_iv[i];
127 ctx->
h[0] ^= 0x01010000 ^ (keylen << 8) ^ outlen;
134 for (i = keylen; i < 128; i++) {
153 for (i = 0; i < inlen; i++) {
156 if (ctx->
t[0] < ctx->
c) {
162 ctx->
b[ctx->
c++] = ((
const uint8_t *)in)[i];
173 if (ctx->
t[0] < ctx->
c) {
177 while (ctx->
c < 128) {
178 ctx->
b[ctx->
c++] = 0;
183 for (i = 0; i < ctx->
outlen; i++) {
184 ((uint8_t *)out)[i] = (ctx->
h[i >> 3] >> (8 * (i & 7))) & 0xFF;
int blake2b_init(blake2b_ctx *ctx, size_t outlen, const void *key, size_t keylen)
static void blake2b_compress(blake2b_ctx *ctx, int last)
int blake2b(void *out, size_t outlen, const void *key, size_t keylen, const void *in, size_t inlen)
#define B2B_G(a, b, c, d, x, y)
void blake2b_final(blake2b_ctx *ctx, void *out)
void blake2b_update(blake2b_ctx *ctx, const void *in, size_t inlen)
static const uint64_t blake2b_iv[8]