]> git.cryptolib.org Git - avr-crypto-lib.git/blob - md5/md5.c
fixing style, typos and uart
[avr-crypto-lib.git] / md5 / md5.c
1 /* md5.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  * \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 "cli.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 static 
45 uint32_t md5_F(uint32_t x, uint32_t y, uint32_t z){
46         return ((x&y)|((~x)&z));
47 }
48
49 static
50 uint32_t md5_G(uint32_t x, uint32_t y, uint32_t z){
51         return ((x&z)|((~z)&y));
52 }
53
54 static
55 uint32_t md5_H(uint32_t x, uint32_t y, uint32_t z){
56         return (x^y^z);
57 }
58
59 static
60 uint32_t md5_I(uint32_t x, uint32_t y, uint32_t z){
61         return (y ^ (x | (~z)));
62 }
63
64 typedef uint32_t md5_func_t(uint32_t, uint32_t, uint32_t);
65
66 #define ROTL32(x,n) (((x)<<(n)) | ((x)>>(32-(n))))  
67
68 static
69 void md5_core(uint32_t *a, void *block, uint8_t as, uint8_t s, uint8_t i, uint8_t fi){
70         uint32_t t;
71         md5_func_t *funcs[]={md5_F, md5_G, md5_H, md5_I};
72         as &= 0x3;
73         /* a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
74 #ifdef DEBUG
75         char funcc[]={'*', '-', '+', '~'};
76         cli_putstr("\r\n DBG: md5_core [");
77         cli_putc(funcc[fi]);
78         cli_hexdump(&as, 1); cli_putc(' ');
79         cli_hexdump(&k, 1); cli_putc(' ');
80         cli_hexdump(&s, 1); cli_putc(' ');
81         cli_hexdump(&i, 1); cli_putc(']');
82 #endif  
83         t = a[as] + funcs[fi](a[(as+1)&3], a[(as+2)&3], a[(as+3)&3]) 
84             + *((uint32_t*)block) + pgm_read_dword(md5_T+i) ;
85         a[as]=a[(as+1)&3] + ROTL32(t, s);
86 }
87
88 void md5_nextBlock(md5_ctx_t *state, const void *block){
89         uint32_t        a[4];
90         uint8_t         m,n,i=0;
91         /* this requires other mixed sboxes */
92 #ifdef DEBUG
93         cli_putstr("\r\n DBG: md5_nextBlock: block:\r\n");
94         cli_hexdump(block, 16); cli_putstr("\r\n");
95         cli_hexdump(block+16, 16);      cli_putstr("\r\n");
96         cli_hexdump(block+32, 16);      cli_putstr("\r\n");
97         cli_hexdump(block+48, 16);      cli_putstr("\r\n");
98 #endif  
99         
100         a[0]=state->a[0];
101         a[1]=state->a[1];
102         a[2]=state->a[2];
103         a[3]=state->a[3];
104         
105         /* round 1 */
106         uint8_t s1t[]={7,12,17,22}; // 1,-1   1,4   2,-1   3,-2
107         for(m=0;m<4;++m){
108                 for(n=0;n<4;++n){
109                         md5_core(a, &(((uint32_t*)block)[m*4+n]), 4-n, s1t[n],i++,0);
110                 }
111         }
112         /* round 2 */
113         uint8_t s2t[]={5,9,14,20}; // 1,-3   1,1   2,-2   2,4
114         for(m=0;m<4;++m){
115                 for(n=0;n<4;++n){
116                         md5_core(a, &(((uint32_t*)block)[(1+m*4+n*5)&0xf]), 4-n, s2t[n],i++,1);
117                 }
118         }
119         /* round 3 */
120         uint8_t s3t[]={4,11,16,23}; // 0,4   1,3   2,0   3,-1
121         for(m=0;m<4;++m){
122                 for(n=0;n<4;++n){
123                         md5_core(a, &(((uint32_t*)block)[(5-m*4+n*3)&0xf]), 4-n, s3t[n],i++,2);
124                 }
125         }
126         /* round 4 */
127         uint8_t s4t[]={6,10,15,21}; // 1,-2   1,2   2,-1   3,-3
128         for(m=0;m<4;++m){
129                 for(n=0;n<4;++n){
130                         md5_core(a, &(((uint32_t*)block)[(0-m*4+n*7)&0xf]), 4-n, s4t[n],i++,3);
131                 }
132         }
133         state->a[0] += a[0];
134         state->a[1] += a[1];
135         state->a[2] += a[2];
136         state->a[3] += a[3];
137         state->counter++;
138 }
139
140 void md5_lastBlock(md5_ctx_t *state, const void *block, uint16_t length_b){
141         uint16_t l;
142         union {
143                 uint8_t   v8[64];
144                 uint64_t v64[ 8];
145         } buffer;
146         while (length_b >= 512){
147                 md5_nextBlock(state, block);
148                 length_b -= 512;
149                 block = ((uint8_t*)block) + 512/8;
150         }
151         memset(buffer.v8, 0, 64);
152         memcpy(buffer.v8, block, length_b/8);
153         /* insert padding one */
154         l=length_b/8;
155         if(length_b%8){
156                 uint8_t t;
157                 t = ((uint8_t*)block)[l];
158                 t |= (0x80>>(length_b%8));
159                 buffer.v8[l]=t;
160         }else{
161                 buffer.v8[l]=0x80;
162         }
163         /* insert length value */
164         if(l+sizeof(uint64_t) >= 512/8){
165                 md5_nextBlock(state, buffer.v8);
166                 state->counter--;
167                 memset(buffer.v8, 0, 64-8);
168         }
169         buffer.v64[7] = (state->counter * 512) + length_b;
170         md5_nextBlock(state, buffer.v8);
171 }
172
173 void md5_ctx2hash(md5_hash_t *dest, const md5_ctx_t *state){
174         memcpy(dest, state->a, MD5_HASH_BYTES);
175 }
176
177 void md5(md5_hash_t *dest, const void *msg, uint32_t length_b){
178         md5_ctx_t ctx;
179         md5_init(&ctx);
180         while(length_b>=MD5_BLOCK_BITS){
181                 md5_nextBlock(&ctx, msg);
182                 msg = (uint8_t*)msg + MD5_BLOCK_BYTES;
183                 length_b -= MD5_BLOCK_BITS;
184         }
185         md5_lastBlock(&ctx, msg, length_b);
186         md5_ctx2hash(dest, &ctx);
187 }
188