]> git.cryptolib.org Git - avr-crypto-lib.git/blob - md5.c
new OMAC (CMAC) with Noekeon; + some bug fixes for the nessie test-env
[avr-crypto-lib.git] / md5.c
1 /* md5.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  * \file        md5.c
21  * \author      Daniel Otte
22  * \date        2006-07-31
23  * \license GPLv3 or later
24  * \brief   Implementation of the MD5 hash algorithm as described in RFC 1321
25  * 
26  */
27
28  #include "md5.h"
29  #include "md5_sbox.h"
30  #include "uart.h" 
31  #include <stdint.h>
32  #include <string.h>
33  
34  #undef DEBUG
35  
36 void md5_init(md5_ctx_t *s){
37         s->counter = 0;
38         s->a[0] = 0x67452301;
39         s->a[1] = 0xefcdab89;
40         s->a[2] = 0x98badcfe;
41         s->a[3] = 0x10325476;
42 }
43  
44 uint32_t md5_F(uint32_t x, uint32_t y, uint32_t z){
45         return ((x&y)|((~x)&z));
46 }
47
48 uint32_t md5_G(uint32_t x, uint32_t y, uint32_t z){
49         return ((x&z)|((~z)&y));
50 }
51
52 uint32_t md5_H(uint32_t x, uint32_t y, uint32_t z){
53         return (x^y^z);
54 }
55
56 uint32_t md5_I(uint32_t x, uint32_t y, uint32_t z){
57         return (y ^ (x | (~z)));
58 }
59
60 typedef uint32_t md5_func_t(uint32_t, uint32_t, uint32_t);
61
62 #define ROTL32(x,n) (((x)<<(n)) | ((x)>>(32-(n))))  
63
64 void md5_core(uint32_t* a, uint8_t as, void* block, uint8_t k, uint8_t s, uint8_t i, uint8_t fi){
65         uint32_t t;
66         md5_func_t* funcs[]={md5_F, md5_G, md5_H, md5_I};
67         as &= 0x3;
68         /* a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
69 #ifdef DEBUG
70         char funcc[]={'*', '-', '+', '~'};
71         uart_putstr("\r\n DBG: md5_core [");
72         uart_putc(funcc[fi]);
73         uart_hexdump(&as, 1); uart_putc(' ');
74         uart_hexdump(&k, 1); uart_putc(' ');
75         uart_hexdump(&s, 1); uart_putc(' ');
76         uart_hexdump(&i, 1); uart_putc(']');
77 #endif  
78         t = a[as] + funcs[fi](a[(as+1)&3], a[(as+2)&3], a[(as+3)&3]) + ((uint32_t*)block)[k] + md5_T[i] ;
79         a[as]=a[(as+1)&3] + ROTL32(t, s);
80 }
81
82 void md5_nextBlock(md5_ctx_t *state, void* block){
83         uint32_t        a[4];
84         uint8_t         m,n,i=0;
85         /* this requires other mixed sboxes */
86 #ifdef DEBUG
87         uart_putstr("\r\n DBG: md5_nextBlock: block:\r\n");
88         uart_hexdump(block, 16);        uart_putstr("\r\n");
89         uart_hexdump(block+16, 16);     uart_putstr("\r\n");
90         uart_hexdump(block+32, 16);     uart_putstr("\r\n");
91         uart_hexdump(block+48, 16);     uart_putstr("\r\n");
92 #endif  
93         
94         a[0]=state->a[0];
95         a[1]=state->a[1];
96         a[2]=state->a[2];
97         a[3]=state->a[3];
98         
99         /* round 1 */
100         uint8_t s1t[]={7,12,17,22};
101         for(m=0;m<4;++m){
102                 for(n=0;n<4;++n){
103                         md5_core(a, 4-n, block, m*4+n, s1t[n],i++,0);
104                 }
105         }
106         /* round 2 */
107         uint8_t s2t[]={5,9,14,20};
108         for(m=0;m<4;++m){
109                 for(n=0;n<4;++n){
110                         md5_core(a, 4-n, block, (1+m*4+n*5)&0xf, s2t[n],i++,1);
111                 }
112         }
113         /* round 3 */
114         uint8_t s3t[]={4,11,16,23};
115         for(m=0;m<4;++m){
116                 for(n=0;n<4;++n){
117                         md5_core(a, 4-n, block, (5-m*4+n*3)&0xf, s3t[n],i++,2);
118                 }
119         }
120         /* round 4 */
121         uint8_t s4t[]={6,10,15,21};
122         for(m=0;m<4;++m){
123                 for(n=0;n<4;++n){
124                         md5_core(a, 4-n, block, (0-m*4+n*7)&0xf, s4t[n],i++,3);
125                 }
126         }
127         state->a[0] += a[0];
128         state->a[1] += a[1];
129         state->a[2] += a[2];
130         state->a[3] += a[3];
131         state->counter++;
132 }
133
134 void md5_lastBlock(md5_ctx_t *state, void* block, uint16_t length_b){
135         uint16_t l;
136         uint8_t b[64];
137         while (length_b >= 512){
138                 md5_nextBlock(state, block);
139                 length_b -= 512;
140                 block = ((uint8_t*)block) + 512/8;
141         }
142         memset(b, 0, 64);
143         memcpy(b, block, length_b/8);
144         /* insert padding one */
145         l=length_b/8;
146         if(length_b%8){
147                 uint8_t t;
148                 t = ((uint8_t*)block)[l];
149                 t |= (0x80>>(length_b%8));
150                 b[l]=t;
151         }else{
152                 b[l]=0x80;
153         }
154         /* insert length value */
155         if(l+sizeof(uint64_t) >= 512/8){
156                 md5_nextBlock(state, b);
157                 state->counter--;
158                 memset(b, 0, 64);
159         }
160         *((uint64_t*)&b[64-sizeof(uint64_t)]) = (state->counter * 512) + length_b;
161         md5_nextBlock(state, b);
162 }