add key/keylen parameters to blake2x_init

This commit is contained in:
Kelvin Sherlock 2017-04-10 18:29:51 -04:00
parent 0f97bb7415
commit 4ee9d767bb
3 changed files with 57 additions and 18 deletions

View File

@ -199,30 +199,51 @@ static int blake2b_init_param(hash_state *md, const unsigned char *P)
return CRYPT_OK;
}
int blake2b_init(hash_state *md, unsigned long outlen)
int blake2b_init(hash_state *md, unsigned long outlen, const unsigned char *key, unsigned long keylen)
{
unsigned char P[BLAKE2B_PARAM_SIZE];
int err;
LTC_ARGCHK(md != NULL);
if ((!outlen) || (outlen > BLAKE2B_OUTBYTES))
return CRYPT_INVALID_ARG;
if ((key && !keylen) || (keylen && !key) || (keylen > BLAKE2B_KEYBYTES))
return CRYPT_INVALID_ARG;
XMEMSET(P, 0, sizeof(P));
P[O_DIGEST_LENGTH] = (unsigned char)outlen;
P[O_KEY_LENGTH] = (unsigned char)keylen;
P[O_FANOUT] = 1;
P[O_DEPTH] = 1;
return blake2b_init_param(md, P);
err = blake2b_init_param(md, P);
if (err != CRYPT_OK) return err;
if (key) {
unsigned char block[BLAKE2B_BLOCKBYTES];
XMEMSET(block, 0, BLAKE2B_BLOCKBYTES);
XMEMCPY(block, key, keylen);
blake2b_process(md, block, BLAKE2B_BLOCKBYTES);
#ifdef LTC_CLEAN_STACK
zeromem(block, sizeof(block));
#endif
}
return CRYPT_OK;
}
int blake2b_160_init(hash_state *md) { return blake2b_init(md, 20); }
int blake2b_160_init(hash_state *md) { return blake2b_init(md, 20, NULL, 0); }
int blake2b_256_init(hash_state *md) { return blake2b_init(md, 32); }
int blake2b_256_init(hash_state *md) { return blake2b_init(md, 32, NULL, 0); }
int blake2b_384_init(hash_state *md) { return blake2b_init(md, 48); }
int blake2b_384_init(hash_state *md) { return blake2b_init(md, 48, NULL, 0); }
int blake2b_512_init(hash_state *md) { return blake2b_init(md, 64); }
int blake2b_512_init(hash_state *md) { return blake2b_init(md, 64, NULL, 0); }
#define G(r, i, a, b, c, d) \
do { \
@ -302,7 +323,7 @@ static int blake2b_compress(hash_state *md, const unsigned char *buf)
{
int err;
err = _blake2b_compress(md, buf);
burn_stack(sizeof(ulong64) * (32 + 1));
burn_stack(sizeof(ulong64) * 32 + sizeof(unsigned long));
return err;
}
#endif

View File

@ -193,32 +193,50 @@ static int blake2s_init_param(hash_state *md, const unsigned char *P)
return CRYPT_OK;
}
/* Sequential blake2s initialization */
int blake2s_init(hash_state *md, unsigned long outlen)
int blake2s_init(hash_state *md, unsigned long outlen, const unsigned char *key, unsigned long keylen)
{
unsigned char P[BLAKE2S_PARAM_SIZE];
int err;
LTC_ARGCHK(md != NULL);
if ((!outlen) || (outlen > BLAKE2S_OUTBYTES))
return CRYPT_INVALID_ARG;
if ((key && !keylen) || (keylen && !key) || (keylen > BLAKE2S_KEYBYTES))
return CRYPT_INVALID_ARG;
XMEMSET(P, 0, sizeof(P));
P[O_DIGEST_LENGTH] = (unsigned char)outlen;
P[O_KEY_LENGTH] = (unsigned char)keylen;
P[O_FANOUT] = 1;
P[O_DEPTH] = 1;
return blake2s_init_param(md, P);
err = blake2s_init_param(md, P);
if (err != CRYPT_OK) return err;
if (key) {
unsigned char block[BLAKE2S_BLOCKBYTES];
XMEMSET(block, 0, BLAKE2S_BLOCKBYTES);
XMEMCPY(block, key, keylen);
blake2s_process(md, block, BLAKE2S_BLOCKBYTES);
#ifdef LTC_CLEAN_STACK
zeromem(block, sizeof(block));
#endif
}
return CRYPT_OK;
}
int blake2s_128_init(hash_state *md) { return blake2s_init(md, 16); }
int blake2s_128_init(hash_state *md) { return blake2s_init(md, 16, NULL, 0); }
int blake2s_160_init(hash_state *md) { return blake2s_init(md, 20); }
int blake2s_160_init(hash_state *md) { return blake2s_init(md, 20, NULL, 0); }
int blake2s_224_init(hash_state *md) { return blake2s_init(md, 28); }
int blake2s_224_init(hash_state *md) { return blake2s_init(md, 28, NULL, 0); }
int blake2s_256_init(hash_state *md) { return blake2s_init(md, 32); }
int blake2s_256_init(hash_state *md) { return blake2s_init(md, 32, NULL, 0); }
#define G(r, i, a, b, c, d) \
do { \
@ -293,7 +311,7 @@ static int blake2s_compress(hash_state *md, const unsigned char *buf)
{
int err;
err = _blake2s_compress(md, buf);
burn_stack(sizeof(ulong32) * (32 + 2));
burn_stack(sizeof(ulong32) * (32) + sizeof(unsigned long));
return err;
}
#endif

View File

@ -362,7 +362,7 @@ extern const struct ltc_hash_descriptor blake2s_128_desc;
int blake2s_128_init(hash_state * md);
int blake2s_128_test(void);
int blake2s_init(hash_state * md, unsigned long outlen);
int blake2s_init(hash_state * md, unsigned long outlen, const unsigned char *key, unsigned long keylen);
int blake2s_process(hash_state * md, const unsigned char *in, unsigned long inlen);
int blake2s_done(hash_state * md, unsigned char *hash);
#endif
@ -384,7 +384,7 @@ extern const struct ltc_hash_descriptor blake2b_160_desc;
int blake2b_160_init(hash_state * md);
int blake2b_160_test(void);
int blake2b_init(hash_state * md, unsigned long outlen);
int blake2b_init(hash_state * md, unsigned long outlen, const unsigned char *key, unsigned long keylen);
int blake2b_process(hash_state * md, const unsigned char *in, unsigned long inlen);
int blake2b_done(hash_state * md, unsigned char *hash);
#endif