]> git.cryptolib.org Git - arm-crypto-lib.git/commitdiff
fixing hmac bugs and adding hfal-hmac, bcal-basic and scal-basic
authorbg <daniel.otte@rub.de>
Mon, 12 Sep 2011 20:20:17 +0000 (22:20 +0200)
committerbg <daniel.otte@rub.de>
Mon, 12 Sep 2011 20:20:17 +0000 (22:20 +0200)
hfal/hfal-hmac.c
hfal/hfal-hmac.h
keysize_descriptor.c [deleted file]
keysize_descriptor.h [deleted file]
mkfiles/002_bcal_basic.mk [new file with mode: 0644]
mkfiles/002_hfal_hmac.mk [new file with mode: 0644]
mkfiles/002_scal_basic.mk [new file with mode: 0644]
mkfiles/zzz_lib.mk

index 0232cca9d11bea0139a3507d6d78002280cb970c..f36b58e7683b75a7fba50ee5e5bf3ebc87f4b70d 100644 (file)
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include <avr/pgmspace.h>
 #include "hashfunction_descriptor.h"
 #include "hfal-basic.h"
+#include "hfal-hmac.h"
 #include <stdlib.h>
+#include <string.h>
 
 #define IPAD 0x36
 #define OPAD 0x5C
 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; i<bs/8; ++i){
                buffer[i] ^= IPAD;
        }
-       init = pgm_read_word(&(hash_descriptor->init));
-       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){
index 7e7e45604d45036389922435bf5cdcd2f2d06750..63cb016eb0c4944f994e0cd50c29d5750df82879 100644 (file)
 #ifndef HFAL_HMAC_H_
 #define HFAL_HMAC_H_
 
-#include <avr/pgmspace.h>
 #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 (file)
index 579ef56..0000000
+++ /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 <http://www.gnu.org/licenses/>.
-*/
-/**
- * \file    keysize_descriptor.c 
- * \author  Daniel Otte
- * \email   daniel.otte@rub.de
- * \date    2009-01-07
- * \license GPLv3 or later
- */
-
-#include <stdint.h>
-#include <stdlib.h>
-#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<items; ++i){
-                       ((uint16_t*)(*list))[i] = *((uint16_t*)ks_desc);
-                       ks_desc = (uint8_t*)ks_desc + 2;
-               }
-               return items;
-       }
-       if(type==KS_TYPE_ARG_RANGE){
-               uint16_t min, max, distance, 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;
-               distance = *((uint16_t*)ks_desc);
-               ks_desc = (uint8_t*)ks_desc + 2;
-               offset = *((uint16_t*)ks_desc);
-               items = (max-min)/distance+1;
-               if(min%distance!=offset){
-                       --items;
-                       min += (distance-(min%distance-offset))%distance;
-               }
-               if(!*list){
-                       *list = malloc(items*2);
-                       if(!*list){
-                               return 0;
-                       }
-               }
-               i=0;
-               while(min<max){
-                       ((uint16_t*)*list)[i++] = min;
-                       min += distance;
-               }
-               return i;
-       }
-       if(type==KS_TYPE_RANGE){
-               uint16_t min, max, distance=8, offset=0;
-               min = *((uint16_t*)ks_desc);
-               ks_desc = (uint8_t*)ks_desc + 2;
-               max = *((uint16_t*)ks_desc);
-               items = (max-min)/distance+1;
-               if(min%distance!=offset){
-                       --items;
-                       min += (distance-(min%distance-offset))%distance;
-               }
-               if(!*list){
-                       *list = malloc(items*2);
-                       if(!*list){
-                               return 0;
-                       }
-               }
-               i=0;
-               while(min<max){
-                       ((uint16_t*)*list)[i++] = min;
-                       min += distance;
-               }
-               return i;
-       }
-       return 0;
-}
diff --git a/keysize_descriptor.h b/keysize_descriptor.h
deleted file mode 100644 (file)
index 2858e90..0000000
+++ /dev/null
@@ -1,61 +0,0 @@
-/* keysize_descriptor.h */
-/*
-    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 <http://www.gnu.org/licenses/>.
-*/
-/**
- * \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 <stdint.h>
-
-#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 (file)
index 0000000..4a0462a
--- /dev/null
@@ -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 (file)
index 0000000..a6afca7
--- /dev/null
@@ -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 (file)
index 0000000..6232a4b
--- /dev/null
@@ -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
+
index 04dcb0d097a6d9ee8e2625d5ba6a75eec1b7f01e..856a378dcbe2a504a0cda1340b431e4e2558545d 100644 (file)
@@ -32,4 +32,7 @@ LIB_ALGOS:= \
     SKEIN_C \
     TDES \
     TRIVIUM \
-    XTEA_C 
+    XTEA_C \
+    HFAL_HMAC \
+    BCAL_BASIC \
+    SCAL_BASIC