]> git.cryptolib.org Git - avr-crypto-lib.git/blob - hmac-sha256.c
schon wieder was falsch gemacht ...
[avr-crypto-lib.git] / hmac-sha256.c
1 /**
2  * 
3  * implementation of HMAC as described in RFC2104
4  * Author:      Daniel Otte
5  * 
6  * License:     GPL
7  **/
8
9 /* 
10  * hmac = hash ( k^opad , hash( k^ipad  , msg))
11  */
12
13 #include <stdint.h>
14 #include <string.h>
15 #include "config.h"
16 #include "sha256.h"
17
18 #define IPAD 0x36
19 #define OPAD 0x5C
20
21 typedef sha256_ctx_t hmac_sha256_ctx_t;
22
23 #ifndef HMAC_SHORTONLY
24
25 void hmac_sha256_init(hmac_sha256_ctx_t *s, void* key, uint16_t kl){
26         uint8_t buffer[SHA256_BLOCK_BITS/8];
27         uint8_t i;
28         
29         if (kl > SHA256_BLOCK_BITS){
30                 sha256((void*)buffer, key, kl);
31         } else {
32                 memcpy(buffer, key, (kl+7)/8);
33         }
34         
35         for (i=0; i<SHA256_BLOCK_BITS/8; ++i){
36                 buffer[i] ^= IPAD;
37         }
38         
39         sha256_init(s);
40         sha256_nextBlock(s, buffer);
41 #if defined SECURE_WIPE_BUFFER
42         memset(buffer, 0, SHA256_BLOCK_BITS/8);
43 #endif
44 }
45
46 void hmac_sha256_final(hmac_sha256_ctx_t *s, void* key, uint16_t kl){
47         uint8_t buffer[SHA256_BLOCK_BITS/8];
48         uint8_t i;
49         sha256_ctx_t a;
50         
51         if (kl > SHA256_BLOCK_BITS){
52                 sha256((void*)buffer, key, kl);
53         } else {
54                 memcpy(buffer, key, (kl+7)/8);
55         }
56         
57         for (i=0; i<SHA256_BLOCK_BITS/8; ++i){
58                 buffer[i] ^= OPAD;
59         }
60         
61         sha256_init(&a);
62         sha256_nextBlock(&a, buffer); /* hash key ^ opad */
63         sha256_ctx2hash((void*)buffer, s);  /* copy hash(key ^ ipad, msg) to buffer */
64         sha256_lastBlock(s, buffer, SHA256_HASH_BITS);
65 #if defined SECURE_WIPE_BUFFER
66         memset(buffer, 0, SHA256_BLOCK_BITS/8);
67         memset(a.h, 0, 8*4);
68 #endif  
69 }
70
71 #endif
72
73 /*
74 void hmac_sha256_nextBlock()
75 void hmac_sha256_lastBlock()
76 */
77
78 /*
79  * keylength in bits!
80  * message length in bits!
81  */
82 void hmac_sha256(void* dest, void* key, uint16_t kl, void* msg, uint64_t ml){ /* a one-shot*/
83         sha256_ctx_t s;
84         uint8_t i;
85         uint8_t buffer[SHA256_BLOCK_BITS/8];
86         
87         memset(buffer, 0, SHA256_BLOCK_BITS/8);
88         
89         /* if key is larger than a block we have to hash it*/
90         if (kl > SHA256_BLOCK_BITS){
91                 sha256((void*)buffer, key, kl);
92         } else {
93                 memcpy(buffer, key, (kl+7)/8);
94         }
95         
96         for (i=0; i<SHA256_BLOCK_BITS/8; ++i){
97                 buffer[i] ^= IPAD;
98         }
99         sha256_init(&s);
100         sha256_nextBlock(&s, buffer);
101         while (ml >= SHA256_BLOCK_BITS){
102                 sha256_nextBlock(&s, msg);
103                 msg = (uint8_t*)msg + SHA256_BLOCK_BITS/8;
104                 ml -=  SHA256_BLOCK_BITS;
105         }
106         sha256_lastBlock(&s, msg, ml);
107         /* since buffer still contains key xor ipad we can do ... */
108         for (i=0; i<SHA256_BLOCK_BITS/8; ++i){
109                 buffer[i] ^= IPAD ^ OPAD;
110         }
111         sha256_ctx2hash(dest, &s); /* save inner hash temporary to dest */
112         sha256_init(&s);
113         sha256_nextBlock(&s, buffer);
114         sha256_lastBlock(&s, dest, SHA256_HASH_BITS);
115         sha256_ctx2hash(dest, &s);
116 }