3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2009 Daniel Otte (daniel.otte@rub.de)
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <avr/pgmspace.h>
21 #include "hashfunction_descriptor.h"
22 #include "hfal-basic.h"
28 uint8_t hfal_hmac_init(const hfdesc_t* hash_descriptor,
30 const void* key, uint16_t keylength_b){
31 uint16_t bs = hfal_hash_getBlocksize();
35 hf_nextBlock_fpt nextBlock;
36 memset(buffer, 0, bs/8);
37 ctx->desc = hash_descriptor;
38 ctx->ctx = malloc(pgm_read_word(&(hash_descriptor->ctxsize_B)));
39 ctx->finctx = malloc(pgm_read_word(&(hash_descriptor->ctxsize_B)));
40 if(ctx->ctx==NULL && ctx->finctx==NULL)
42 if(ctx->finctx==NULL){
51 hfal_hash_mem(hash_descriptor, buffer, key, keylength_b);
53 memcpy(buffer, key, (keylength_b+7)/8);
55 for(i=0; i<bs/8; ++i){
58 init = pgm_read_word(&(hash_descriptor->init));
59 nextBlock = pgm_read_word(&(hash_descriptor->nextBlock));
62 nextBlock(ctx->ctx, buffer);
63 for(i=0; i<bs/8; ++i){
64 buffer[i] ^= IPAD^OPAD;
66 nextBlock(ctx->finctx, buffer);
67 memset(buffer, 0, bs/8);
70 void hfal_hmac_nextBlock(hfhmacgen_ctx_t* ctx, const void* block){
71 hf_nextBlock_fpt nextBlock;
72 nextBlock = pgm_read_word(&(hash_descriptor->nextBlock));
73 nextBlock(ctx->ctx, block);
76 void hfal_hmac_lastBlock(hfhmacgen_ctx_t* ctx, const void* block, uint16_t length_b){
77 hf_lastBlock_fpt lastBlock;
78 hf_ctx2hash_fpt ctx2hash;
79 uint16_t hs = pgm_read_word(&(hash_descriptor->hashsize_b));
80 uint8_t buffer[(hs+7)/8];
81 lastBlock = pgm_read_word(&(hash_descriptor->lastBlock));
82 ctx2hash = pgm_read_word(&(hash_descriptor->ctx2hash));
83 lastBlock(ctx->ctx, block, length_b);
84 ctx2hash(buffer, ctx->ctx);
85 lastBlock(ctx->finctx, buffer, hs);
88 void hfal_hmac_ctx2mac(void* dest, hfhmacgen_ctx_t* ctx){
89 hf_ctx2hash_fpt ctx2hash;
90 ctx2hash = pgm_read_word(&(hash_descriptor->ctx2hash));
91 ctx2hash(dest, ctx->finctx);
94 void hfal_hmac_free(hfhmacgen_ctx_t* ctx){
96 free_fpt = pgm_read_word(&(hash_descriptor->free));
99 free_fpt(ctx->finctx);
105 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){
107 uint16_t bs = hfal_hash_getBlocksize();
108 hfal_hmac_init(hash_descriptor, &ctx, key, keylength_b);
110 hfal_hmac_nextBlock(&ctx, msg);
114 hfal_hmac_lastBlock(&ctx, msg, length_b);
115 hfal_hmac_ctx2mac(dest, &ctx);
119 uint16_t hfal_hmac_getBlocksize(const hfdesc_t* hash_descriptor){
120 return hfal_hash_getBlocksize(hash_descriptor);
123 uint16_t hfal_hmac_getMACsize(const hfdesc_t* hash_descriptor){
124 return hfal_hash_getHashsize(hash_descriptor);