Commit b48d3ade authored by Davide Galassi's avatar Davide Galassi

HMAC initialization refactory

parent ff00a1bf
Pipeline #35781835 passed with stages
in 1 minute and 21 seconds
......@@ -8,13 +8,13 @@
#ifndef _CRY_HMAC_H_
#define _CRY_HMAC_H_
#define CRY_HMAC_HASH_BLOCK_MAX 64
#define CRY_HMAC_BLOCK_MAX 64
struct cry_hmac_ctx {
void *hash_ctx;
const cry_hash_itf *hash_itf;
size_t hash_len;
const unsigned char *key;
unsigned char key[CRY_HMAC_BLOCK_MAX];
size_t key_len;
};
......
#include "cry/hmac.h"
#include <string.h>
#define BLOCK_SIZE 64
#define BLOCK_SIZE CRY_HMAC_BLOCK_MAX
void cry_hmac_init(cry_hmac_ctx *ctx, void *hash_ctx,
const cry_hash_itf *hash_itf, size_t hash_len,
......@@ -10,54 +10,49 @@ void cry_hmac_init(cry_hmac_ctx *ctx, void *hash_ctx,
ctx->hash_ctx = hash_ctx;
ctx->hash_itf = hash_itf;
ctx->hash_len = hash_len;
ctx->key = key;
ctx->key_len = key_len;
if (key_len <= BLOCK_SIZE) {
memcpy(ctx->key, key, key_len);
ctx->key_len = key_len;
} else {
if (hash_itf->init)
hash_itf->init(ctx->hash_ctx);
hash_itf->update(ctx->hash_ctx, key, key_len);
hash_itf->digest(ctx->hash_ctx, ctx->key);
if (hash_itf->clean)
hash_itf->clean(ctx->hash_ctx);
ctx->key_len = ctx->hash_len;
}
}
int cry_hmac_digest(cry_hmac_ctx *ctx, unsigned char *mac,
const unsigned char *in, size_t insize)
{
int i;
const unsigned char *key;
size_t key_len;
unsigned char hkey[BLOCK_SIZE];
unsigned char pad[BLOCK_SIZE];
const cry_hash_itf *hash = ctx->hash_itf;
if (ctx->key_len <= BLOCK_SIZE) {
key = ctx->key;
key_len = ctx->key_len;
} else {
key = hkey;
key_len = ctx->hash_len;
if (hash->init)
hash->init(ctx->hash_ctx);
hash->update(ctx->hash_ctx, ctx->key, ctx->key_len);
hash->digest(ctx->hash_ctx, hkey);
if (hash->clean)
hash->clean(ctx->hash_ctx);
}
const cry_hash_itf *hash_itf = ctx->hash_itf;
memset(pad, 0x36, BLOCK_SIZE);
for (i = 0; i < key_len; i++)
pad[i] ^= key[i];
if (hash->init)
hash->init(ctx->hash_ctx);
hash->update(ctx->hash_ctx, pad, BLOCK_SIZE);
hash->update(ctx->hash_ctx, in, insize);
hash->digest(ctx->hash_ctx, mac);
if (hash->clean)
hash->clean(ctx->hash_ctx);
for (i = 0; i < ctx->key_len; i++)
pad[i] ^= ctx->key[i];
if (hash_itf->init)
hash_itf->init(ctx->hash_ctx);
hash_itf->update(ctx->hash_ctx, pad, BLOCK_SIZE);
hash_itf->update(ctx->hash_ctx, in, insize);
hash_itf->digest(ctx->hash_ctx, mac);
if (hash_itf->clean)
hash_itf->clean(ctx->hash_ctx);
memset(pad, 0x5C, BLOCK_SIZE);
for (i = 0; i < key_len; i++)
pad[i] ^= key[i];
if (hash->init)
hash->init(ctx->hash_ctx);
hash->update(ctx->hash_ctx, pad, BLOCK_SIZE);
hash->update(ctx->hash_ctx, mac, ctx->hash_len);
hash->digest(ctx->hash_ctx, mac);
if (hash->clean)
hash->clean(ctx->hash_ctx);
for (i = 0; i < ctx->key_len; i++)
pad[i] ^= ctx->key[i];
if (hash_itf->init)
hash_itf->init(ctx->hash_ctx);
hash_itf->update(ctx->hash_ctx, pad, BLOCK_SIZE);
hash_itf->update(ctx->hash_ctx, mac, ctx->hash_len);
hash_itf->digest(ctx->hash_ctx, mac);
if (hash_itf->clean)
hash_itf->clean(ctx->hash_ctx);
return 0;
}
......@@ -6,7 +6,7 @@ struct hmac_param {
unsigned int keylen;
unsigned int msglen;
unsigned int maclen;
unsigned char key[128];
unsigned char key[256];
unsigned char msg[256];
unsigned char mac[64];
};
......@@ -64,4 +64,5 @@ void hmac_test(void)
{
printf("* HMAC NIST CAVS 11.0\n");
func_test("hmac_test.data", dispatch);
printf("\n");
}
......@@ -51,3 +51,9 @@ hmac_digest
0cb35a02ddc8c7fb7c93aeab77b9318118b0fd449524209d879a1cd69d5439e192741f9c5c64a353a774e28681c58ced576783ba20bea51ed82ae50e30e6a147843130900dac
21063443bf02ffe9f813dc6688920d036041a2a3a63a9956fc254a2c05ae03472537ef3489c93c7c68517c7588094c5e033434ab4b0ecf9e6c032c17911f73adcac6ccfd0ca57c427ae85127e2ad41d98bb94e5f2e6aad2e42ed26f87cb1bec6971c9446517c0966b6402321a06834997f3ab66756377a2f064d0277cf4e2bb9
5c258ba6241f65c2ee5356bb47332236baea227857e29506165861a4c7379c51
HMAC-SHA256 #166 (klen=70)
hmac_digest
0cb35a02ddc8c7fb7c93aeab77b9318118b0fd449524209d879a1cd69d5439e192741f9c5c64a353a774e28681c58ced576783ba20bea51ed82ae50e30e6a147843130900dac
21063443bf02ffe9f813dc6688920d036041a2a3a63a9956fc254a2c05ae03472537ef3489c93c7c68517c7588094c5e033434ab4b0ecf9e6c032c17911f73adcac6ccfd0ca57c427ae85127e2ad41d98bb94e5f2e6aad2e42ed26f87cb1bec6971c9446517c0966b6402321a06834997f3ab66756377a2f064d0277cf4e2bb9
5c258ba6241f65c2ee5356bb47332236baea227857e29506165861a4c7379c51
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment