23 #define ROTR32(x, y) (((x) >> (y)) ^ ((x) << (32 - (y)))) 37 #define B2S_GET32(p) \ 38 (((uint32_t)((uint8_t *)(p))[0]) ^ (((uint32_t)((uint8_t *)(p))[1]) << 8) \ 39 ^ (((uint32_t)((uint8_t *)(p))[2]) << 16) \ 40 ^ (((uint32_t)((uint8_t *)(p))[3]) << 24)) 44 #define B2S_G(a, b, c, d, x, y) \ 46 v[a] = v[a] + v[b] + (x); \ 47 v[d] = ROTR32(v[d] ^ v[a], 16); \ 49 v[b] = ROTR32(v[b] ^ v[c], 12); \ 50 v[a] = v[a] + v[b] + (y); \ 51 v[d] = ROTR32(v[d] ^ v[a], 8); \ 53 v[b] = ROTR32(v[b] ^ v[c], 7); \ 70 const uint8_t sigma[10][16] = {
71 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
72 {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
73 {11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4},
74 {7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8},
75 {9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13},
76 {2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9},
77 {12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11},
78 {13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10},
79 {6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5},
80 {10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0}};
85 for (i = 0; i < 8; i++) {
87 v[i + 8] = blake2s_iv[i];
96 for (i = 0; i < 16; i++) {
100 for (i = 0; i < 10; i++) {
101 B2S_G(0, 4, 8, 12, m[sigma[i][0]], m[sigma[i][1]]);
102 B2S_G(1, 5, 9, 13, m[sigma[i][2]], m[sigma[i][3]]);
103 B2S_G(2, 6, 10, 14, m[sigma[i][4]], m[sigma[i][5]]);
104 B2S_G(3, 7, 11, 15, m[sigma[i][6]], m[sigma[i][7]]);
105 B2S_G(0, 5, 10, 15, m[sigma[i][8]], m[sigma[i][9]]);
106 B2S_G(1, 6, 11, 12, m[sigma[i][10]], m[sigma[i][11]]);
107 B2S_G(2, 7, 8, 13, m[sigma[i][12]], m[sigma[i][13]]);
108 B2S_G(3, 4, 9, 14, m[sigma[i][14]], m[sigma[i][15]]);
111 for (i = 0; i < 8; ++i) {
112 ctx->
h[i] ^= v[i] ^ v[i + 8];
122 for (i = 0; i < inlen; i++) {
125 if (ctx->
t[0] < ctx->
c) {
131 ctx->
b[ctx->
c++] = ((
const uint8_t *)in)[i];
144 if (ctx->
t[0] < ctx->
c) {
148 while (ctx->
c < 64) {
149 ctx->
b[ctx->
c++] = 0;
154 for (i = 0; i < ctx->
outlen; i++) {
155 ((uint8_t *)out)[i] = (uint8_t)((ctx->
h[i >> 2] >> (8 * (i & 3))) & 0xFF);
167 if (outlen == 0 || outlen > 32 || keylen > 32) {
171 for (i = 0; i < 8; i++) {
172 ctx->
h[i] = blake2s_iv[i];
175 ctx->
h[0] ^= 0x01010000 ^ (keylen << 8) ^ outlen;
182 for (i = keylen; i < 64; i++) {
static const uint32_t blake2s_iv[8]
void blake2s_256(void *out, const void *in, size_t inlen)
One-shot convenience function to calculate blake2s_256 hash.
#define B2S_G(a, b, c, d, x, y)
int blake2s(void *out, size_t outlen, const void *key, size_t keylen, const void *in, size_t inlen)
All in one blake2s hashing function.
unsigned char opaque[128]
static void blake2s_compress(blake2s_ctx_full *ctx, int last)
void blake2s_final(blake2s_ctx *ctx_opaque, void *out)
Finalize hash calculation.
int blake2s_init(blake2s_ctx *ctx_opaque, size_t outlen, const void *key, size_t keylen)
Generic blake2s init function.
void blake2s_256_init(blake2s_ctx *ctx_opaque)
Initialize hash context.
void blake2s_update(blake2s_ctx *ctx_opaque, const void *in, size_t inlen)
Update context with incoming bytes.