Commit 13da9bbc authored by Davide Galassi's avatar Davide Galassi

Merge branch 'feat_hmac_test' into 'master'

HMAC SHA256 test

See merge request !3
parents 5b823c1b 4d986c0d
Pipeline #35921797 passed with stages
in 1 minute and 18 seconds
......@@ -79,7 +79,7 @@ LDFLAGS = $(lflags-y)
DATE := $(shell date +'%y%m%d')
.PHONY: all cry clean test coverage
.PHONY: all cry clean test testclean
all: cry
......
......@@ -8,23 +8,24 @@
#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;
size_t key_len;
size_t hash_len;
unsigned char key[CRY_HMAC_BLOCK_MAX];
size_t key_len;
};
typedef struct cry_hmac_ctx cry_hmac_ctx;
void cry_hmac_init(cry_hmac_ctx *ctx, void *hash_ctx,
cry_hash_itf *hash_itf, size_t hash_len,
const cry_hash_itf *hash_itf, size_t hash_len,
const unsigned char *key, size_t key_len);
int cry_hmac(cry_hmac_ctx *ctx, unsigned char *mac, const unsigned char *in,
size_t insize);
void cry_hmac_update(cry_hmac_ctx *ctx, unsigned char *in, size_t in_len);
void cry_hmac_digest(cry_hmac_ctx *ctx, unsigned char *mac);
#endif /* _CRY_HMAC_H_ */
#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,
cry_hash_itf *hash_itf, size_t hash_len,
const cry_hash_itf *hash_itf, size_t hash_len,
const unsigned char *key, size_t key_len)
{
int i;
unsigned char pad[BLOCK_SIZE];
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;
}
memset(pad, 0x36, BLOCK_SIZE);
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);
}
int cry_hmac(cry_hmac_ctx *ctx, unsigned char *mac, const unsigned char *in,
size_t insize)
void cry_hmac_update(cry_hmac_ctx *ctx, unsigned char *in, size_t in_len)
{
ctx->hash_itf->update(ctx->hash_ctx, in, in_len);
}
void cry_hmac_digest(cry_hmac_ctx *ctx, unsigned char *mac)
{
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;
const cry_hash_itf *hash_itf = 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);
}
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);
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);
return 0;
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);
}
......@@ -13,6 +13,7 @@ objs := \
memxor_test.o \
base64_test.o \
mpi_test.o \
hmac_test.o \
aes_test.o \
hill_test.o \
affine_test.o
......
......@@ -26,22 +26,6 @@ struct aes_param {
unsigned char dst[128];
};
#define EMPTY_STRING "NULL"
int raw_init(unsigned char *raw, unsigned int rawlen, const char *asc)
{
int len;
if (strcmp(asc, EMPTY_STRING) == 0)
return 0;
if (strcmp(asc, "-1") == 0)
return -1;
len = strlen(asc);
if (rawlen < (len >> 1))
len = rawlen << 1;
asc_to_raw(asc, len, raw);
return len >> 1;
}
static void param_init(struct aes_param *par, int argc, char *argv[])
{
......
......@@ -13,9 +13,6 @@ struct affine_param {
size_t dstlen;
};
int raw_init(unsigned char *raw, unsigned int rawlen, const char *asc);
static void param_init(struct affine_param *par, char *argv[])
{
memset(par, 0, sizeof(*par));
......
......@@ -14,8 +14,6 @@ struct hill_param {
size_t dstlen;
};
int raw_init(unsigned char *raw, unsigned int rawlen, const char *asc);
static void param_init(struct hill_param *par, char *argv[])
{
memset(par, 0, sizeof(*par));
......
This diff is collapsed.
#include "test.h"
#include <cry/hmac.h>
#include <cry/sha256.h>
struct hmac_param {
unsigned int keylen;
unsigned int msglen;
unsigned int maclen;
unsigned char key[256];
unsigned char msg[256];
unsigned char mac[64];
};
static void param_init(struct hmac_param *par, int argc, char *argv[])
{
memset(par, 0, sizeof(*par));
par->keylen = raw_init(par->key, sizeof(par->key), argv[0]);
par->msglen = raw_init(par->msg, sizeof(par->msg), argv[1]);
par->maclen = raw_init(par->mac, sizeof(par->mac), argv[2]);
}
static void sha256_clean(void *ctx)
{
/* Just to increase lib coverage */
}
static const struct cry_hash_itf sha256_itf = {
(cry_hash_init_f) cry_sha256_init,
(cry_hash_clean_f) sha256_clean,
(cry_hash_update_f) cry_sha256_update,
(cry_hash_digest_f) cry_sha256_digest,
};
static void hmac_digest(int argc, char *argv[])
{
cry_sha256_ctx sha256;
cry_hmac_ctx hmac;
struct hmac_param par;
unsigned char mac[64];
ASSERT(argc == 3);
param_init(&par, argc, argv);
cry_hmac_init(&hmac, &sha256, &sha256_itf, 32, par.key, par.keylen);
cry_hmac_update(&hmac, par.msg, par.msglen);
cry_hmac_digest(&hmac, mac);
ASSERT(memcmp(mac, par.mac, par.maclen) == 0);
}
static void dispatch(int argc, char *argv[])
{
char *test = *argv;
argv++;
argc--;
if (strcmp(test, "hmac_digest") == 0)
hmac_digest(argc, argv);
else
printf("Test '%s' not defined\n", test);
}
void hmac_test(void)
{
printf("* HMAC NIST CAVS 11.0\n");
func_test("hmac_test.data", dispatch);
printf("\n");
}
This diff is collapsed.
......@@ -4,6 +4,7 @@ void version_test(void);
void memxor_test(void);
void base64_test(void);
void mpi_test(void);
void hmac_test(void);
void aes_test(void);
void hill_test(void);
void affine_test(void);
......@@ -19,6 +20,7 @@ static const char *g_test_str[] = {
"memxor",
"base64",
"mpi",
"hmac",
"aes",
"hill",
"affine",
......@@ -32,6 +34,7 @@ static test_func_t g_test_func[] = {
memxor_test,
base64_test,
mpi_test,
hmac_test,
aes_test,
hill_test,
affine_test,
......@@ -178,6 +181,23 @@ void asc_to_raw(const char *asc, size_t size, unsigned char *raw)
}
#define EMPTY_STRING "NULL"
int raw_init(unsigned char *raw, unsigned int rawlen, const char *asc)
{
int len;
if (strcmp(asc, EMPTY_STRING) == 0)
return 0;
if (strcmp(asc, "-1") == 0)
return -1;
len = strlen(asc);
if (rawlen < (len >> 1))
len = rawlen << 1;
asc_to_raw(asc, len, raw);
return len >> 1;
}
static void help(const char *arg)
{
......
......@@ -23,6 +23,8 @@ void func_test(const char *datafile, dispatch_func_t dispatch);
void asc_to_raw(const char *asc, size_t size, unsigned char *raw);
int raw_init(unsigned char *raw, unsigned int rawlen, const char *asc);
#define ARLEN(ar) (sizeof(ar)/sizeof(ar[0]))
......
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