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