]> git.cryptolib.org Git - avr-crypto-lib.git/blob - hmac-sha256.c
insereated GPLv3 stub
[avr-crypto-lib.git] / hmac-sha256.c
1 /* hmac-sha256.c */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
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  * 
21  * implementation of HMAC as described in RFC2104
22  * Author:      Daniel Otte
23  * 
24  * License:     GPL
25  **/
26
27 /* 
28  * hmac = hash ( k^opad , hash( k^ipad  , msg))
29  */
30
31 #include <stdint.h>
32 #include <string.h>
33 #include "config.h"
34 #include "sha256.h"
35
36 #define IPAD 0x36
37 #define OPAD 0x5C
38
39 typedef sha256_ctx_t hmac_sha256_ctx_t;
40
41 #ifndef HMAC_SHORTONLY
42
43 void hmac_sha256_init(hmac_sha256_ctx_t *s, void* key, uint16_t kl){
44         uint8_t buffer[SHA256_BLOCK_BITS/8];
45         uint8_t i;
46         
47         memset(buffer, 0, SHA256_BLOCK_BITS/8);
48         if (kl > SHA256_BLOCK_BITS){
49                 sha256((void*)buffer, key, kl);
50         } else {
51                 memcpy(buffer, key, (kl+7)/8);
52         }
53         
54         for (i=0; i<SHA256_BLOCK_BITS/8; ++i){
55                 buffer[i] ^= IPAD;
56         }
57         
58         sha256_init(s);
59         sha256_nextBlock(s, buffer);
60 #if defined SECURE_WIPE_BUFFER
61         memset(buffer, 0, SHA256_BLOCK_BITS/8);
62 #endif
63 }
64
65 void hmac_sha256_final(hmac_sha256_ctx_t *s, void* key, uint16_t kl){
66         uint8_t buffer[SHA256_BLOCK_BITS/8];
67         uint8_t i;
68         sha256_ctx_t a;
69         
70         memset(buffer, 0, SHA256_BLOCK_BITS/8);
71         if (kl > SHA256_BLOCK_BITS){
72                 sha256((void*)buffer, key, kl);
73         } else {
74                 memcpy(buffer, key, (kl+7)/8);
75         }
76         
77         for (i=0; i<SHA256_BLOCK_BITS/8; ++i){
78                 buffer[i] ^= OPAD;
79         }
80         
81         sha256_init(&a);
82         sha256_nextBlock(&a, buffer); /* hash key ^ opad */
83         sha256_ctx2hash((void*)buffer, s);  /* copy hash(key ^ ipad, msg) to buffer */
84         sha256_lastBlock(&a, buffer, SHA256_HASH_BITS);
85         memcpy(s, &a, sizeof(sha256_ctx_t));
86 #if defined SECURE_WIPE_BUFFER
87         memset(buffer, 0, SHA256_BLOCK_BITS/8);
88         memset(a.h, 0, 8*4);
89 #endif  
90 }
91
92 #endif
93
94 /*
95 void hmac_sha256_nextBlock()
96 void hmac_sha256_lastBlock()
97 */
98
99 /*
100  * keylength in bits!
101  * message length in bits!
102  */
103 void hmac_sha256(void* dest, void* key, uint16_t kl, void* msg, uint64_t ml){ /* a one-shot*/
104         sha256_ctx_t s;
105         uint8_t i;
106         uint8_t buffer[SHA256_BLOCK_BITS/8];
107         
108         memset(buffer, 0, SHA256_BLOCK_BITS/8);
109         
110         /* if key is larger than a block we have to hash it*/
111         if (kl > SHA256_BLOCK_BITS){
112                 sha256((void*)buffer, key, kl);
113         } else {
114                 memcpy(buffer, key, (kl+7)/8);
115         }
116         
117         for (i=0; i<SHA256_BLOCK_BITS/8; ++i){
118                 buffer[i] ^= IPAD;
119         }
120         sha256_init(&s);
121         sha256_nextBlock(&s, buffer);
122         while (ml >= SHA256_BLOCK_BITS){
123                 sha256_nextBlock(&s, msg);
124                 msg = (uint8_t*)msg + SHA256_BLOCK_BITS/8;
125                 ml -=  SHA256_BLOCK_BITS;
126         }
127         sha256_lastBlock(&s, msg, ml);
128         /* since buffer still contains key xor ipad we can do ... */
129         for (i=0; i<SHA256_BLOCK_BITS/8; ++i){
130                 buffer[i] ^= IPAD ^ OPAD;
131         }
132         sha256_ctx2hash(dest, &s); /* save inner hash temporary to dest */
133         sha256_init(&s);
134         sha256_nextBlock(&s, buffer);
135         sha256_lastBlock(&s, dest, SHA256_HASH_BITS);
136         sha256_ctx2hash(dest, &s);
137 }