diff options
Diffstat (limited to 'lib/blake2/blake2s.c')
-rw-r--r-- | lib/blake2/blake2s.c | 37 |
1 files changed, 21 insertions, 16 deletions
diff --git a/lib/blake2/blake2s.c b/lib/blake2/blake2s.c index b924a1e..9206c89 100644 --- a/lib/blake2/blake2s.c +++ b/lib/blake2/blake2s.c @@ -34,14 +34,16 @@ G(uint32_t v[16], unsigned a, unsigned b, unsigned c, unsigned d, uint32_t x, ui void F(struct BLAKE2s_ctx *ctx, uint32_t m[16], uint32_t f) { - // Initialize local work vector v + unsigned i; + + /* Initialize local work vector v */ uint32_t v[16] = {ctx->h[0], ctx->h[1], ctx->h[2], ctx->h[3], ctx->h[4], ctx->h[5], ctx->h[6], ctx->h[7], IV[0], IV[1], IV[2], IV[3], IV[4] ^ ctx->t[0], IV[5] ^ ctx->t[1], IV[6], IV[7]}; - if (f) v[14] = ~v[14]; // if last block flag, invert all bits + if (f) v[14] = ~v[14]; /* if last block flag, invert all bits */ - // cryptographic mixing - for (unsigned i = 0; i < 10; ++i) { + /* cryptographic mixing */ + for (i = 0; i < 10; ++i) { G(v, 0, 4, 8, 12, m[SIGMA[i][0]], m[SIGMA[i][1]]); G(v, 1, 5, 9, 13, m[SIGMA[i][2]], m[SIGMA[i][3]]); G(v, 2, 6, 10, 14, m[SIGMA[i][4]], m[SIGMA[i][5]]); @@ -53,8 +55,8 @@ F(struct BLAKE2s_ctx *ctx, uint32_t m[16], uint32_t f) G(v, 3, 4, 9, 14, m[SIGMA[i][14]], m[SIGMA[i][15]]); } - // xor the two halves - for (unsigned i = 0; i < 8; ++i) ctx->h[i] ^= (v[i] ^ v[i + 8]); + /* xor the two halves */ + for (i = 0; i < 8; ++i) ctx->h[i] ^= (v[i] ^ v[i + 8]); } int @@ -70,14 +72,14 @@ BLAKE2s_init(struct BLAKE2s_ctx *ctx, uint8_t outlen, const void *key, uint8_t k ctx->param.fanout = 1; ctx->param.depth = 1; - // copy IV into state vector h + /* copy IV into state vector h */ memcpy(ctx->h, IV, 32); - // copy param block 0 onto h[0] + /* copy param block 0 onto h[0] */ ctx->h[0] ^= (ctx->param.depth << 24) ^ (ctx->param.fanout << 16) ^ (keylen << 8) ^ outlen; if (keylen > 0) { BLAKE2s_update(ctx, key, keylen); - ctx->c = 64; // at the end + ctx->c = 64; /* at the end */ } return 0; @@ -88,15 +90,16 @@ BLAKE2s_init(struct BLAKE2s_ctx *ctx, uint8_t outlen, const void *key, uint8_t k void BLAKE2s_update(struct BLAKE2s_ctx *ctx, const void *d, size_t dd) { - for (unsigned i = 0; i < dd;) { + unsigned i, j; + for (i = 0; i < dd;) { - if (ctx->c == 64) { // if block is full, consume block + if (ctx->c == 64) { /* if block is full, consume block */ ctx->t[0] += ctx->c; if (ctx->t[0] < ctx->c) ctx->t[1] += 1; - ctx->c = 0; // reset counter + ctx->c = 0; /* reset counter */ uint32_t *m = (uint32_t *)ctx->b; - for (unsigned j = 0; j < 16; ++j) m[j] = htole32(m[j]); + for (j = 0; j < 16; ++j) m[j] = htole32(m[j]); F(ctx, m, 0); } @@ -110,14 +113,16 @@ BLAKE2s_update(struct BLAKE2s_ctx *ctx, const void *d, size_t dd) void BLAKE2s_final(struct BLAKE2s_ctx *ctx, void *out) { + unsigned i; + ctx->t[0] += ctx->c; if (ctx->t[0] < ctx->c) ctx->t[1] += 1; - for (; ctx->c < 64; ++(ctx->c)) ctx->b[ctx->c] = 0; // fill up block with zeroes + for (; ctx->c < 64; ++(ctx->c)) ctx->b[ctx->c] = 0; /* fill up block with zeroes */ uint32_t *m = (uint32_t *)ctx->b; - for (unsigned i = 0; i < 16; ++i) m[i] = htole32(m[i]); + for (i = 0; i < 16; ++i) m[i] = htole32(m[i]); F(ctx, m, 1); - for (unsigned i = 0; i < ctx->param.outlen; ++i) ((uint8_t *)out)[i] = (ctx->h[i >> 2] >> (8 * (i & 3))) & 0xff; + for (i = 0; i < ctx->param.outlen; ++i) ((uint8_t *)out)[i] = (ctx->h[i >> 2] >> (8 * (i & 3))) & 0xff; } |