From 1ee55d2589fee6c40acb72a24c04528a8cc1772f Mon Sep 17 00:00:00 2001 From: bg Date: Mon, 12 Sep 2011 22:20:17 +0200 Subject: [PATCH] fixing hmac bugs and adding hfal-hmac, bcal-basic and scal-basic --- hfal/hfal-hmac.c | 59 ++++++++------ hfal/hfal-hmac.h | 4 +- keysize_descriptor.c | 161 -------------------------------------- keysize_descriptor.h | 61 --------------- mkfiles/002_bcal_basic.mk | 9 +++ mkfiles/002_hfal_hmac.mk | 9 +++ mkfiles/002_scal_basic.mk | 9 +++ mkfiles/zzz_lib.mk | 5 +- 8 files changed, 70 insertions(+), 247 deletions(-) delete mode 100644 keysize_descriptor.c delete mode 100644 keysize_descriptor.h create mode 100644 mkfiles/002_bcal_basic.mk create mode 100644 mkfiles/002_hfal_hmac.mk create mode 100644 mkfiles/002_scal_basic.mk diff --git a/hfal/hfal-hmac.c b/hfal/hfal-hmac.c index 0232cca..f36b58e 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,35 @@ uint8_t hfal_hmac_init(const hfdesc_t* hash_descriptor, } nextBlock(ctx->finctx, buffer); memset(buffer, 0, bs/8); + return 0; } - + +int 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){ + 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 +102,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){ diff --git a/hfal/hfal-hmac.h b/hfal/hfal-hmac.h index 7e7e456..63cb016 100644 --- a/hfal/hfal-hmac.h +++ b/hfal/hfal-hmac.h @@ -20,16 +20,16 @@ #ifndef HFAL_HMAC_H_ #define HFAL_HMAC_H_ -#include #include "hashfunction_descriptor.h" typedef struct { - hfdesc_t* desc; + const hfdesc_t* desc; void* ctx; void* finctx; } hfhmacgen_ctx_t; uint8_t hfal_hmac_init(const hfdesc_t* hash_descriptor, hfhmacgen_ctx_t* ctx, const void* key, uint16_t keylength_b); +int hfal_hmac_ctxcopy(hfhmacgen_ctx_t* dest, hfhmacgen_ctx_t* src); void hfal_hmac_nextBlock(hfhmacgen_ctx_t* ctx, const void* block); void hfal_hmac_lastBlock(hfhmacgen_ctx_t* ctx, const void* block, uint16_t length_b); void hfal_hmac_ctx2mac(void* dest, hfhmacgen_ctx_t* ctx); diff --git a/keysize_descriptor.c b/keysize_descriptor.c deleted file mode 100644 index 579ef56..0000000 --- a/keysize_descriptor.c +++ /dev/null @@ -1,161 +0,0 @@ -/* keysize_descriptor.c */ -/* - This file is part of the ARM-Crypto-Lib. - Copyright (C) 2009 Daniel Otte (daniel.otte@rub.de) - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ -/** - * \file keysize_descriptor.c - * \author Daniel Otte - * \email daniel.otte@rub.de - * \date 2009-01-07 - * \license GPLv3 or later - */ - -#include -#include -#include "keysize_descriptor.h" - -uint8_t is_valid_keysize_P(const void* ks_desc, uint16_t keysize){ - uint8_t type; - type = *((uint8_t*)ks_desc); - ks_desc = (uint8_t*)ks_desc + 1; - if(type==KS_TYPE_TERMINATOR) - return 0; - if(type==KS_TYPE_LIST){ - uint8_t items; - uint16_t item; - items = *((uint8_t*)ks_desc); - ks_desc = (uint8_t*)ks_desc + 1; - while(items--){ - item = *((uint16_t*)ks_desc); - ks_desc = (uint8_t*)ks_desc + 2; - if(item==keysize) - return 1; - } - ks_desc = (uint8_t*)ks_desc - 2; - } - if(type==KS_TYPE_RANGE){ - uint16_t max, min; - min = *((uint16_t*)ks_desc); - ks_desc = (uint8_t*)ks_desc + 2; - max = *((uint16_t*)ks_desc); - if(min<=keysize && keysize<=max) - return 1; - } - if(type==KS_TYPE_ARG_RANGE){ - uint16_t max, min, dist, offset; - min = *((uint16_t*)ks_desc); - ks_desc = (uint8_t*)ks_desc + 2; - max = *((uint16_t*)ks_desc); - ks_desc = (uint8_t*)ks_desc + 2; - dist = *((uint16_t*)ks_desc); - ks_desc = (uint8_t*)ks_desc + 2; - offset = *((uint16_t*)ks_desc); - if(min<=keysize && keysize<=max && (keysize%dist==offset)) - return 1; - } - if(type>KS_TYPE_ARG_RANGE){ - /* bad error, you may insert a big warning message here */ - return 0; - } - return is_valid_keysize_P((uint8_t*)ks_desc+1, keysize); /* search the next record */ -} - -uint16_t get_keysize(const void* ks_desc){ - uint8_t type; - uint16_t keysize; - type = *((uint8_t*)ks_desc); - if(type==KS_TYPE_LIST){ - ks_desc = (uint8_t*)ks_desc + 1; - } - ks_desc = (uint8_t*)ks_desc + 1; - keysize = *((uint8_t*)ks_desc); - return keysize; -} - -uint16_t get_keysizes(const void* ks_desc, uint16_t** list){ - uint8_t type; - uint16_t items; - uint8_t i; - type = *((uint8_t*)ks_desc); - ks_desc = (uint8_t*)ks_desc + 1; - if(type==KS_TYPE_LIST){ - items = *((uint8_t*)ks_desc); - ks_desc = (uint8_t*)ks_desc + 1; - if(!*list){ - *list = malloc(items*2); - if(!*list){ - return 0; - } - } - for(i=0; i. -*/ -/** - * \file keysize_descriptor.h - * \author Daniel Otte - * \email daniel.otte@rub.de - * \date 2009-01-07 - * \license GPLv3 or later - */ - -#ifndef KEYSIZE_DESCRIPTOR_H_ -#define KEYSIZE_DESCRIPTOR_H_ - -#include - -#define KS_TYPE_TERMINATOR 0x00 -#define KS_TYPE_LIST 0x01 -#define KS_TYPE_RANGE 0x02 -#define KS_TYPE_ARG_RANGE 0x03 - -#define KS_INT(a) ((a)&0xFF), ((a)>>8) - -typedef struct{ /* keysize is valid if listed in items */ - uint8_t n_items; /* number of items (value 0 is reserved) */ - uint16_t items[]; /* list of valid lengths */ -}keysize_desc_list_t; - -typedef struct{ /* keysize is valid if min<=keysize<=max */ - uint16_t min; - uint16_t max; -}keysize_desc_range_t; - -typedef struct{ /* keysize is valid if min<=keysize<=max and if keysize mod distance == offset */ - uint16_t min; - uint16_t max; - uint16_t distance; - uint16_t offset; -}keysize_desc_arg_range_t; - -uint8_t is_valid_keysize_P(const void* ks_desc, uint16_t keysize); -uint16_t get_keysize(const void* ks_desc); -uint16_t get_keysizes(const void* ks_desc, uint16_t** list); - - -#endif /* KEYSIZE_DESCRIPTOR_H_ */ diff --git a/mkfiles/002_bcal_basic.mk b/mkfiles/002_bcal_basic.mk new file mode 100644 index 0000000..4a0462a --- /dev/null +++ b/mkfiles/002_bcal_basic.mk @@ -0,0 +1,9 @@ +# Makefile for bcal-basic (library) +ALGO_NAME := BCAL_BASIC + +$(ALGO_NAME)_DIR := bcal/ +$(ALGO_NAME)_INCDIR := memxor/ +$(ALGO_NAME)_OBJ := bcal-basic.o keysize_descriptor.o +$(ALGO_NAME)_NESSIE_TEST := test nessie +$(ALGO_NAME)_PERFORMANCE_TEST := performance + diff --git a/mkfiles/002_hfal_hmac.mk b/mkfiles/002_hfal_hmac.mk new file mode 100644 index 0000000..a6afca7 --- /dev/null +++ b/mkfiles/002_hfal_hmac.mk @@ -0,0 +1,9 @@ +# Makefile for hfal-hmac (library) +ALGO_NAME := HFAL_HMAC + +$(ALGO_NAME)_DIR := hfal/ +$(ALGO_NAME)_INCDIR := memxor/ +$(ALGO_NAME)_OBJ := hfal-basic.o hfal-hmac.o +$(ALGO_NAME)_NESSIE_TEST := test nessie +$(ALGO_NAME)_PERFORMANCE_TEST := performance + diff --git a/mkfiles/002_scal_basic.mk b/mkfiles/002_scal_basic.mk new file mode 100644 index 0000000..6232a4b --- /dev/null +++ b/mkfiles/002_scal_basic.mk @@ -0,0 +1,9 @@ +# Makefile for scal-basic (library) +ALGO_NAME := SCAL_BASIC + +$(ALGO_NAME)_DIR := scal/ +$(ALGO_NAME)_INCDIR := memxor/ +$(ALGO_NAME)_OBJ := scal-basic.o +$(ALGO_NAME)_NESSIE_TEST := test nessie +$(ALGO_NAME)_PERFORMANCE_TEST := performance + diff --git a/mkfiles/zzz_lib.mk b/mkfiles/zzz_lib.mk index 04dcb0d..856a378 100644 --- a/mkfiles/zzz_lib.mk +++ b/mkfiles/zzz_lib.mk @@ -32,4 +32,7 @@ LIB_ALGOS:= \ SKEIN_C \ TDES \ TRIVIUM \ - XTEA_C + XTEA_C \ + HFAL_HMAC \ + BCAL_BASIC \ + SCAL_BASIC