]> git.cryptolib.org Git - labortage2013badge.git/blob - firmware/hmac-sha1.c
seems to be working ok
[labortage2013badge.git] / firmware / hmac-sha1.c
1 /* hmac-sha1.c */
2 /*
3     This file is part of the AVR-Crypto-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  * email:       daniel.otte@rub.de
24  * License:     GPLv3 or later
25  **/
26
27 /* 
28  * hmac = hash ( k^opad , hash( k^ipad  , msg))
29  */
30
31 #include <stdint.h>
32 #include <string.h>
33 #include "sha1.h"
34 #include "hmac-sha1.h"
35
36 #define IPAD 0x36
37 #define OPAD 0x5C
38
39
40 #ifndef HMAC_SHORTONLY
41
42 void hmac_sha1_init(hmac_sha1_ctx_t *s, const void *key, uint16_t keylength_b){
43         uint8_t buffer[SHA1_BLOCK_BYTES];
44         uint8_t i;
45         
46         memset(buffer, 0, SHA1_BLOCK_BYTES);
47         if (keylength_b > SHA1_BLOCK_BITS){
48                 sha1((void*)buffer, key, keylength_b);
49         } else {
50                 memcpy(buffer, key, (keylength_b+7)/8);
51         }
52         
53         for (i=0; i<SHA1_BLOCK_BYTES; ++i){
54                 buffer[i] ^= IPAD;
55         }
56         sha1_init(&(s->a));
57         sha1_nextBlock(&(s->a), buffer);
58         
59         for (i=0; i<SHA1_BLOCK_BYTES; ++i){
60                 buffer[i] ^= IPAD^OPAD;
61         }
62         sha1_init(&(s->b));
63         sha1_nextBlock(&(s->b), buffer);
64         
65         
66 #if defined SECURE_WIPE_BUFFER
67         memset(buffer, 0, SHA1_BLOCK_BYTES);
68 #endif
69 }
70
71 void hmac_sha1_nextBlock(hmac_sha1_ctx_t *s, const void *block){
72         sha1_nextBlock(&(s->a), block);
73 }
74 void hmac_sha1_lastBlock(hmac_sha1_ctx_t *s, const void *block, uint16_t length_b){
75         while(length_b>=SHA1_BLOCK_BITS){
76                 sha1_nextBlock(&s->a, block);
77                 block = (uint8_t*)block + SHA1_BLOCK_BYTES;
78                 length_b -= SHA1_BLOCK_BITS;
79         }
80         sha1_lastBlock(&s->a, block, length_b);
81 }
82
83 void hmac_sha1_final(void *dest, hmac_sha1_ctx_t *s){
84         sha1_ctx2hash(dest, &s->a);
85         sha1_lastBlock(&s->b, dest, SHA1_HASH_BITS);
86         sha1_ctx2hash(dest, &(s->b));
87 }
88
89 #endif
90
91 /*
92  * key length in bits!
93  * message length in bits!
94  */
95 void hmac_sha1(void *dest, const void *key, uint16_t keylength_b, const void *msg, uint32_t msglength_b){ /* a one-shot */
96         sha1_ctx_t s;
97         uint8_t i;
98         uint8_t buffer[SHA1_BLOCK_BYTES];
99         
100         memset(buffer, 0, SHA1_BLOCK_BYTES);
101         
102         /* if key is larger than a block we have to hash it*/
103         if (keylength_b > SHA1_BLOCK_BITS){
104                 sha1((void*)buffer, key, keylength_b);
105         } else {
106                 memcpy(buffer, key, (keylength_b+7)/8);
107         }
108         
109         for (i=0; i<SHA1_BLOCK_BYTES; ++i){
110                 buffer[i] ^= IPAD;
111         }
112         sha1_init(&s);
113         sha1_nextBlock(&s, buffer);
114         while (msglength_b >= SHA1_BLOCK_BITS){
115                 sha1_nextBlock(&s, msg);
116                 msg = (uint8_t*)msg + SHA1_BLOCK_BYTES;
117                 msglength_b -=  SHA1_BLOCK_BITS;
118         }
119         sha1_lastBlock(&s, msg, msglength_b);
120         /* since buffer still contains key xor ipad we can do ... */
121         for (i=0; i<SHA1_BLOCK_BYTES; ++i){
122                 buffer[i] ^= IPAD ^ OPAD;
123         }
124         sha1_ctx2hash(dest, &s); /* save inner hash temporary to dest */
125         sha1_init(&s);
126         sha1_nextBlock(&s, buffer);
127         sha1_lastBlock(&s, dest, SHA1_HASH_BITS);
128         sha1_ctx2hash(dest, &s);
129 }
130