]> git.cryptolib.org Git - avr-crypto-lib.git/blob - hfal/hfal-hmac.c
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / hfal / hfal-hmac.c
1 /* hfal-hmac.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
5
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.
10
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.
15
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/>.
18 */
19
20 #include <avr/pgmspace.h>
21 #include "hashfunction_descriptor.h"
22 #include "hfal-basic.h"
23 #include <stdlib.h>
24
25 #define IPAD 0x36
26 #define OPAD 0x5C
27
28 uint8_t hfal_hmac_init(const hfdesc_t *hash_descriptor, 
29                        hfhmacgen_ctx_t *ctx, 
30                                            const void *key, uint16_t keylength_b){
31         uint16_t  bs = hfal_hash_getBlocksize();
32         uint8_t buffer[bs/8];
33         uint8_t i;
34         hf_init_fpt init;
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)
41                 return 3;
42         if(ctx->finctx==NULL){
43                 free(ctx->ctx)
44                 return 2;
45         }
46         if(ctx->ctx==NULL){
47                 free(ctx->finctx)
48                 return 1;
49         }               
50         if(keylength_b>bs){
51                 hfal_hash_mem(hash_descriptor, buffer, key, keylength_b);
52         } else {
53                 memcpy(buffer, key, (keylength_b+7)/8);
54         }
55         for(i=0; i<bs/8; ++i){
56                 buffer[i] ^= IPAD;
57         }
58         init = pgm_read_word(&(hash_descriptor->init));
59         nextBlock = pgm_read_word(&(hash_descriptor->nextBlock));
60         init(ctx->ctx);
61         init(ctx->finctx);
62         nextBlock(ctx->ctx, buffer);
63         for(i=0; i<bs/8; ++i){
64                 buffer[i] ^= IPAD^OPAD;
65         }
66         nextBlock(ctx->finctx, buffer);
67         memset(buffer, 0, bs/8);
68 }
69                                            
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);
74 }
75
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);
86 }
87
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);
92 }
93
94 void hfal_hmac_free(hfhmacgen_ctx_t *ctx){
95         hf_free_fpt free_fpt;
96         free_fpt = pgm_read_word(&(hash_descriptor->free));
97         if(free_fpt){
98                 free_fpt(ctx->ctx);
99                 free_fpt(ctx->finctx);
100         }
101         free(ctx->ctx)
102         free(ctx->finctx)
103 }
104
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){
106         hfhmacgen_ctx_t ctx;
107         uint16_t  bs = hfal_hash_getBlocksize();
108         hfal_hmac_init(hash_descriptor, &ctx, key, keylength_b);
109         while(length_b>bs){
110                 hfal_hmac_nextBlock(&ctx, msg);
111                 msg = msg + bs/8;
112                 length_b-=bs;
113         }
114         hfal_hmac_lastBlock(&ctx, msg, length_b);
115         hfal_hmac_ctx2mac(dest, &ctx);
116         hfal_free(&ctx);
117 }
118
119 uint16_t hfal_hmac_getBlocksize(const hfdesc_t *hash_descriptor){
120         return hfal_hash_getBlocksize(hash_descriptor);
121 }
122
123 uint16_t hfal_hmac_getMACsize(const hfdesc_t *hash_descriptor){
124         return hfal_hash_getHashsize(hash_descriptor);
125 }
126
127