aboutsummaryrefslogtreecommitdiff
path: root/lib/blake2
diff options
context:
space:
mode:
Diffstat (limited to 'lib/blake2')
-rw-r--r--lib/blake2/blake2s.c37
-rw-r--r--lib/blake2/blake2s.h24
2 files changed, 33 insertions, 28 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;
}
diff --git a/lib/blake2/blake2s.h b/lib/blake2/blake2s.h
index 64b4156..ede170c 100644
--- a/lib/blake2/blake2s.h
+++ b/lib/blake2/blake2s.h
@@ -4,8 +4,8 @@
#include <stdint.h>
struct BLAKE2s_param {
- uint8_t outlen; // digest length
- uint8_t keylen; // key length
+ uint8_t outlen; /* digest length */
+ uint8_t keylen; /* key length */
uint8_t fanout;
uint8_t depth;
uint32_t leaf_length;
@@ -18,11 +18,11 @@ struct BLAKE2s_param {
};
struct BLAKE2s_ctx {
- uint8_t b[64]; // input buffer
- size_t c; // pointer for b[]
- uint32_t h[8]; // chained state vector h
- uint32_t t[2]; // total number of bytes
- struct BLAKE2s_param param; // parameter block
+ uint8_t b[64]; /* input buffer */
+ size_t c; /* pointer for b[] */
+ uint32_t h[8]; /* chained state vector h */
+ uint32_t t[2]; /* total number of bytes */
+ struct BLAKE2s_param param; /* parameter block */
};
/**
@@ -41,11 +41,11 @@ int BLAKE2s_init(struct BLAKE2s_ctx *ctx, uint8_t outlen, const void *key, uint8
void BLAKE2s_update(struct BLAKE2s_ctx *ctx, const void *d, size_t dd);
void BLAKE2s_final(struct BLAKE2s_ctx *ctx, void *out);
-// All-in-one convenience function.
-static inline int
-BLAKE2s(void *out, uint8_t outlen, // return buffer for digest
- const void *key, uint8_t keylen, // optional secret key
- const void *in, size_t inlen) // data to be hashed
+/* All-in-one convenience function. */
+static __inline__ int
+BLAKE2s(void *out, uint8_t outlen, /* return buffer for digest */
+ const void *key, uint8_t keylen, /* optional secret key */
+ const void *in, size_t inlen) /* data to be hashed */
{
struct BLAKE2s_ctx ctx;
if (BLAKE2s_init(&ctx, outlen, key, keylen)) return -1;