3 This file is part of the ARM-Crypto-Lib.
4 Copyright (C) 2006-2010 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 "hashfunction_descriptor.h"
21 #include "hfal-basic.h"
22 #include "hfal-hmac.h"
29 uint8_t hfal_hmac_init(const hfdesc_t* hash_descriptor,
31 const void* key, uint16_t keylength_b){
32 uint16_t bs = hfal_hash_getBlocksize(hash_descriptor);
36 hf_nextBlock_fpt nextBlock;
37 memset(buffer, 0, bs/8);
38 ctx->desc = hash_descriptor;
39 ctx->ctx = malloc(hash_descriptor->ctxsize_B);
40 ctx->finctx = malloc(hash_descriptor->ctxsize_B);
41 if(ctx->ctx==NULL && ctx->finctx==NULL)
43 if(ctx->finctx==NULL){
52 hfal_hash_mem(hash_descriptor, buffer, key, keylength_b);
54 memcpy(buffer, key, (keylength_b+7)/8);
56 for(i=0; i<bs/8; ++i){
59 init = hash_descriptor->init;
60 nextBlock = hash_descriptor->nextBlock;
63 nextBlock(ctx->ctx, buffer);
64 for(i=0; i<bs/8; ++i){
65 buffer[i] ^= IPAD^OPAD;
67 nextBlock(ctx->finctx, buffer);
68 memset(buffer, 0, bs/8);
72 uint8_t hfal_hmac_ctxcopy(hfhmacgen_ctx_t* dest, hfhmacgen_ctx_t* src){
73 dest->desc = src->desc;
74 dest->ctx = malloc(dest->desc->ctxsize_B);
75 if(dest->ctx == NULL){
78 memcpy(dest->ctx, src->ctx, dest->desc->ctxsize_B);
79 dest->finctx = malloc(dest->desc->ctxsize_B);
80 if(dest->finctx == NULL){
83 memcpy(dest->finctx, src->finctx, dest->desc->ctxsize_B);
87 void hfal_hmac_nextBlock(hfhmacgen_ctx_t* ctx, const void* block){
88 ctx->desc->nextBlock(ctx->ctx, block);
91 void hfal_hmac_lastBlock(hfhmacgen_ctx_t* ctx, const void* block, uint16_t length_b){
92 hf_lastBlock_fpt lastBlock;
93 hf_ctx2hash_fpt ctx2hash;
94 uint16_t hs = ctx->desc->hashsize_b;
95 uint8_t buffer[(hs+7)/8];
96 lastBlock = ctx->desc->lastBlock;
97 ctx2hash = ctx->desc->ctx2hash;
98 lastBlock(ctx->ctx, block, length_b);
99 ctx2hash(buffer, ctx->ctx);
100 lastBlock(ctx->finctx, buffer, hs);
103 void hfal_hmac_ctx2mac(void* dest, hfhmacgen_ctx_t* ctx){
104 hf_ctx2hash_fpt ctx2hash;
105 ctx2hash = ctx->desc->ctx2hash;
106 ctx2hash(dest, ctx->finctx);
109 void hfal_hmac_free(hfhmacgen_ctx_t* ctx){
110 hf_free_fpt free_fpt;
111 free_fpt = ctx->desc->free;
114 free_fpt(ctx->finctx);
120 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){
122 uint16_t bs = hfal_hash_getBlocksize(hash_descriptor);
123 hfal_hmac_init(hash_descriptor, &ctx, key, keylength_b);
125 hfal_hmac_nextBlock(&ctx, msg);
126 msg = (uint8_t*)msg + bs/8;
129 hfal_hmac_lastBlock(&ctx, msg, length_b);
130 hfal_hmac_ctx2mac(dest, &ctx);
131 hfal_hmac_free(&ctx);
134 uint16_t hfal_hmac_getBlocksize(const hfdesc_t* hash_descriptor){
135 return hfal_hash_getBlocksize(hash_descriptor);
138 uint16_t hfal_hmac_getMACsize(const hfdesc_t* hash_descriptor){
139 return hfal_hash_getHashsize(hash_descriptor);