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