3 This file is part of the Crypto-avr-lib/microcrypt-lib.
4 Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
27 void md5_core(uint32_t* a, void* block, uint8_t as, uint8_t s, uint8_t i, uint8_t fi);
30 #define ROTL32(x,n) (((x)<<(n)) | ((x)>>(32-(n))))
33 void md5_core(uint32_t* a, void* block, uint8_t as, uint8_t s, uint8_t i, uint8_t fi){
35 md5_func_t* funcs[]={md5_F, md5_G, md5_H, md5_I};
37 // * a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). * /
39 char funcc[]={'*', '-', '+', '~'};
40 uart_putstr("\r\n DBG: md5_core [");
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(']');
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);
53 void md5_nextBlock(md5_ctx_t *state, void* block){
56 /* this requires other mixed sboxes */
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");
71 uint8_t s1t[]={7,12,17,22}; // 1,-1 1,4 2,-1 3,-2
74 md5_core(a, &(((uint32_t*)block)[m*4+n]), 4-n, s1t[n],i++,0);
78 uint8_t s2t[]={5,9,14,20}; // 1,-3 1,1 2,-2 2,4
81 md5_core(a, &(((uint32_t*)block)[(1+m*4+n*5)&0xf]), 4-n, s2t[n],i++,1);
85 uint8_t s3t[]={4,11,16,23}; // 0,4 1,3 2,0 3,-1
88 md5_core(a, &(((uint32_t*)block)[(5-m*4+n*3)&0xf]), 4-n, s3t[n],i++,2);
92 uint8_t s4t[]={6,10,15,21}; // 1,-2 1,2 2,-1 3,-3
95 md5_core(a, &(((uint32_t*)block)[(0-m*4+n*7)&0xf]), 4-n, s4t[n],i++,3);
105 void md5_lastBlock(md5_ctx_t *state, void* block, uint16_t length_b){
108 while (length_b >= 512){
109 md5_nextBlock(state, block);
111 block = ((uint8_t*)block) + 512/8;
114 memcpy(b, block, length_b/8);
115 /* insert padding one */
119 t = ((uint8_t*)block)[l];
120 t |= (0x80>>(length_b%8));
125 /* insert length value */
126 if(l+sizeof(uint64_t) >= 512/8){
127 md5_nextBlock(state, b);
131 *((uint64_t*)&b[64-sizeof(uint64_t)]) = (state->counter * 512) + length_b;
132 md5_nextBlock(state, b);