X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=hfal%2Fhfal-hmac.c;h=f27105592998d7b24d3933b26b7334538c9842aa;hb=7390f9235d6bc08b7fe34a5f43a04bd3b58f6ea6;hp=0232cca9d11bea0139a3507d6d78002280cb970c;hpb=d70d1d77bab1a5f5278227d674bc59da0378fe15;p=arm-crypto-lib.git diff --git a/hfal/hfal-hmac.c b/hfal/hfal-hmac.c index 0232cca..f271055 100644 --- a/hfal/hfal-hmac.c +++ b/hfal/hfal-hmac.c @@ -17,10 +17,11 @@ along with this program. If not, see . */ -#include #include "hashfunction_descriptor.h" #include "hfal-basic.h" +#include "hfal-hmac.h" #include +#include #define IPAD 0x36 #define OPAD 0x5C @@ -28,23 +29,23 @@ uint8_t hfal_hmac_init(const hfdesc_t* hash_descriptor, hfhmacgen_ctx_t* ctx, const void* key, uint16_t keylength_b){ - uint16_t bs = hfal_hash_getBlocksize(); + uint16_t bs = hfal_hash_getBlocksize(hash_descriptor); uint8_t buffer[bs/8]; uint8_t i; hf_init_fpt init; hf_nextBlock_fpt nextBlock; memset(buffer, 0, bs/8); ctx->desc = hash_descriptor; - ctx->ctx = malloc(pgm_read_word(&(hash_descriptor->ctxsize_B))); - ctx->finctx = malloc(pgm_read_word(&(hash_descriptor->ctxsize_B))); + ctx->ctx = malloc(hash_descriptor->ctxsize_B); + ctx->finctx = malloc(hash_descriptor->ctxsize_B); if(ctx->ctx==NULL && ctx->finctx==NULL) return 3; if(ctx->finctx==NULL){ - free(ctx->ctx) + free(ctx->ctx); return 2; } if(ctx->ctx==NULL){ - free(ctx->finctx) + free(ctx->finctx); return 1; } if(keylength_b>bs){ @@ -55,8 +56,8 @@ uint8_t hfal_hmac_init(const hfdesc_t* hash_descriptor, for(i=0; iinit)); - nextBlock = pgm_read_word(&(hash_descriptor->nextBlock)); + init = hash_descriptor->init; + nextBlock = hash_descriptor->nextBlock; init(ctx->ctx); init(ctx->finctx); nextBlock(ctx->ctx, buffer); @@ -65,21 +66,36 @@ uint8_t hfal_hmac_init(const hfdesc_t* hash_descriptor, } nextBlock(ctx->finctx, buffer); memset(buffer, 0, bs/8); + return 0; } - + +uint8_t hfal_hmac_ctxcopy(hfhmacgen_ctx_t* dest, hfhmacgen_ctx_t* src){ + dest->desc = src->desc; + dest->ctx = malloc(dest->desc->ctxsize_B); + if(dest->ctx == NULL){ + return 1; + } + memcpy(dest->ctx, src->ctx, dest->desc->ctxsize_B); + dest->finctx = malloc(dest->desc->ctxsize_B); + if(dest->finctx == NULL){ + free(dest->ctx); + return 1; + } + memcpy(dest->finctx, src->finctx, dest->desc->ctxsize_B); + return 0; +} + void hfal_hmac_nextBlock(hfhmacgen_ctx_t* ctx, const void* block){ - hf_nextBlock_fpt nextBlock; - nextBlock = pgm_read_word(&(hash_descriptor->nextBlock)); - nextBlock(ctx->ctx, block); + ctx->desc->nextBlock(ctx->ctx, block); } void hfal_hmac_lastBlock(hfhmacgen_ctx_t* ctx, const void* block, uint16_t length_b){ hf_lastBlock_fpt lastBlock; hf_ctx2hash_fpt ctx2hash; - uint16_t hs = pgm_read_word(&(hash_descriptor->hashsize_b)); + uint16_t hs = ctx->desc->hashsize_b; uint8_t buffer[(hs+7)/8]; - lastBlock = pgm_read_word(&(hash_descriptor->lastBlock)); - ctx2hash = pgm_read_word(&(hash_descriptor->ctx2hash)); + lastBlock = ctx->desc->lastBlock; + ctx2hash = ctx->desc->ctx2hash; lastBlock(ctx->ctx, block, length_b); ctx2hash(buffer, ctx->ctx); lastBlock(ctx->finctx, buffer, hs); @@ -87,33 +103,33 @@ void hfal_hmac_lastBlock(hfhmacgen_ctx_t* ctx, const void* block, uint16_t lengt void hfal_hmac_ctx2mac(void* dest, hfhmacgen_ctx_t* ctx){ hf_ctx2hash_fpt ctx2hash; - ctx2hash = pgm_read_word(&(hash_descriptor->ctx2hash)); + ctx2hash = ctx->desc->ctx2hash; ctx2hash(dest, ctx->finctx); } void hfal_hmac_free(hfhmacgen_ctx_t* ctx){ hf_free_fpt free_fpt; - free_fpt = pgm_read_word(&(hash_descriptor->free)); + free_fpt = ctx->desc->free; if(free_fpt){ free_fpt(ctx->ctx); free_fpt(ctx->finctx); } - free(ctx->ctx) - free(ctx->finctx) + free(ctx->ctx); + free(ctx->finctx); } void hfal_hmac_mem(const hfdesc_t* hash_descriptor, const void* key, uint16_t keylength_b, void* dest, const void* msg, uint32_t length_b){ hfhmacgen_ctx_t ctx; - uint16_t bs = hfal_hash_getBlocksize(); + uint16_t bs = hfal_hash_getBlocksize(hash_descriptor); hfal_hmac_init(hash_descriptor, &ctx, key, keylength_b); while(length_b>bs){ hfal_hmac_nextBlock(&ctx, msg); - msg = msg + bs/8; + msg = (uint8_t*)msg + bs/8; length_b-=bs; } hfal_hmac_lastBlock(&ctx, msg, length_b); hfal_hmac_ctx2mac(dest, &ctx); - hfal_free(&ctx); + hfal_hmac_free(&ctx); } uint16_t hfal_hmac_getBlocksize(const hfdesc_t* hash_descriptor){