]> git.cryptolib.org Git - avr-crypto-lib.git/commitdiff
+md5 (still buggy)
authorbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Wed, 2 Aug 2006 22:25:48 +0000 (22:25 +0000)
committerbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Wed, 2 Aug 2006 22:25:48 +0000 (22:25 +0000)
Makefile
md5.c
md5.h
md5_sbox.h [new file with mode: 0644]

index 89f89134bca8ff792554eca5f59379083b521c14..4e67726fcbda07907265ee19183057f2dbe36f22 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,7 @@
-PRG        = cast5
+PRG        = md5
 # cryptotest
-OBJ        = main-cast5-test.o debug.o uart.o serial-tools.o cast5.o
+OBJ        = main-md5-test.o debug.o uart.o serial-tools.o md5.o
+# main-cast5-test.o debug.o uart.o serial-tools.o cast5.o
 # main.o debug.o uart.o serial-tools.o sha256-asm.o xtea-asm.o arcfour-asm.o prng.o cast5.o
 MCU_TARGET     = atmega32
 OPTIMIZE       = -Os
diff --git a/md5.c b/md5.c
index d26c806e04d29573ee74e023b394cf65c33c1ca5..766fbb43fa229d982e06516e394dd6308b417794 100644 (file)
--- a/md5.c
+++ b/md5.c
@@ -8,8 +8,156 @@
  */
 
  #include "md5.h"
+ #include "md5_sbox.h"
+ #include "uart.h" 
  #include <stdint.h>
+ #include <string.h>
  
+void md5_init(md5_ctx_t *s){
+       s->counter = 0;
+       s->a[0] = 0x67452301;
+       s->a[1] = 0xefcdab89;
+       s->a[2] = 0x98badcfe;
+       s->a[3] = 0x10325476;
+}
  
\ No newline at end of file
+uint32_t md5_F(uint32_t x, uint32_t y, uint32_t z){
+       return ((x&y)|((~x)&z));
+}
+
+uint32_t md5_G(uint32_t x, uint32_t y, uint32_t z){
+       return ((x&z)|((~z)&y));
+}
+
+uint32_t md5_H(uint32_t x, uint32_t y, uint32_t z){
+       return (x^y^z);
+}
+
+uint32_t md5_I(uint32_t x, uint32_t y, uint32_t z){
+       return (y ^ (x | (~z)));
+}
+
+typedef uint32_t md5_func_t(uint32_t, uint32_t, uint32_t);
+
+#define ROTL32(x,n) ((x)<<(n) | (x)>>(32-(n)))  
+
+void md5_core(uint32_t* a, uint8_t as, void* block, uint8_t k, uint8_t s, uint8_t i, uint8_t fi){
+       uint32_t t;
+       md5_func_t* funcs[]={md5_F, md5_G, md5_H, md5_I};
+       char funcc[]={'*', '-', '+', '~'};
+       as &= 0x3;
+       /* a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
+       uart_putstr("\r\n DBG: md5_core [");
+       uart_putc(funcc[fi]);
+       uart_hexdump(&as, 1); uart_putc(' ');
+       uart_hexdump(&k, 1); uart_putc(' ');
+       uart_hexdump(&s, 1); uart_putc(' ');
+       uart_hexdump(&i, 1); uart_putc(']');
+       t = a[as] + funcs[fi](a[(as+1)&3], a[(as+2)&3], a[(as+3)&3]) + ((uint8_t*)block)[k] + md5_T[i] ;
+       a[as]=a[(as+1)&3] + ROTL32(t, s);
+}
+
+void md5_nextBlock(md5_ctx_t *state, void* block){
+       uint32_t        a[4];
+       uint8_t         m,n,i=0;
+       /* this requires other mixed sboxes */
+       uart_putstr("\r\n DBG: md5_nextBlock: block:\r\n");
+       uart_hexdump(block, 16);        uart_putstr("\r\n");
+       uart_hexdump(block+16, 16);     uart_putstr("\r\n");
+       uart_hexdump(block+32, 16);     uart_putstr("\r\n");
+       uart_hexdump(block+48, 16);     uart_putstr("\r\n");
+       
+       a[0]=state->a[0];
+       a[1]=state->a[1];
+       a[2]=state->a[2];
+       a[3]=state->a[3];
+       
+       /* round 1 */
+       uint8_t s1t[]={7,12,17,22};
+       for(m=0;m<4;++m){
+               for(n=0;n<4;++n){
+                       md5_core(a, 4-n, block, m*4+n, s1t[n],i++,0);
+/*                     t = a[(4-s)&3] + md5_F(a[(4-s-1)&3],a[(4-s-2)&3],a[(4-s-3)&3]) 
+                               + ((uint8_t*)block)[s+4*k] + md5_T[i++];
+                       a[(4-s)&3] = a[(4-s-1)&3] + ROTL32(t, s*5+7);
+*/             }
+       }
+       /* round 2 */
+       uint8_t s2t[]={5,9,14,20};
+       for(m=0;m<4;++m){
+               for(n=0;n<4;++n){
+                       md5_core(a, 4-n, block, (1+m*4+n*5)&0xf, s2t[n],i++,1);
+               }
+       }
+/*     for(k=0;k<4;++k){
+               for(s=0;s<4;++s){
+                       t = a[(4-s)&3] + md5_G(a[(4-s-1)&3],a[(4-s-2)&3],a[(4-s-3)&3])
+                               + ((uint8_t*)block)[((1+s*5) +(k*4))&0xf] + md5_T[i++];
+                       a[(4-s)&3] = a[(4-s-1)&3] + ROTL32(t, s2t[s]);
+               }
+       }
+*/
+       /* round 3 */
+       uint8_t s3t[]={4,11,16,23};
+       for(m=0;m<4;++m){
+               for(n=0;n<4;++n){
+                       md5_core(a, 4-n, block, (5-m*4+n*3)&0xf, s3t[n],i++,2);
+               }
+       }
+/*     for(k=0;k<4;++k){
+               for(s=0;s<4;++s){
+                       t = a[(4-s)&3] + md5_H(a[(4-s-1)&3],a[(4-s-2)&3],a[(4-s-3)&3])
+                               + ((uint8_t*)block)[((5+s*3)-(k*4))&0xf] + md5_T[i++];
+                       a[(4-s)&3] = a[(4-s-1)&3] + ROTL32(t, s3t[s]);
+               }
+       } */
+       /* round 4 */
+       uint8_t s4t[]={6,10,15,21};
+       for(m=0;m<4;++m){
+               for(n=0;n<4;++n){
+                       md5_core(a, 4-n, block, (0-m*4+n*7)&0xf, s4t[n],i++,3);
+               }
+       }
+/*     for(k=0;k<4;++k){
+               for(s=0;s<4;++s){
+                       t = a[(4-s)&3] + md5_I(a[(4-s-1)&3],a[(4-s-2)&3],a[(4-s-3)&3]) 
+                               + ((uint8_t*)block)[((s*7)-(k*4))&0xf] + md5_T[i++];
+                       a[(4-s)&3] = a[(4-s-1)&3] + ROTL32(t, s4t[s]);
+               }
+       } */
+       state->a[0] += a[0];
+       state->a[1] += a[1];
+       state->a[2] += a[2];
+       state->a[3] += a[3];
+       state->counter++;
+}
+
+void md5_lastBlock(md5_ctx_t *state, void* block, uint16_t length){
+       uint16_t l;
+       uint8_t b[64];
+       while (length >= 512){
+               md5_nextBlock(state, block);
+               length -= 512;
+               block += 512/8;
+       }
+       memset(b, 0, 64);
+       memcpy(b, block, length/8);
+       /* insert padding one */
+       l=length/8;
+       if(length%8){
+               uint8_t t;
+               t = ((uint8_t*)block)[l];
+               t |= (0x80>>(length%8));
+               b[l]=t;
+       }else{
+               b[l]=0x80;
+       }
+       /* insert length value */
+       if(l+sizeof(uint64_t) > 512/8){
+               md5_nextBlock(state, b);
+               state->counter--;
+               memset(b, 0, 64);
+       }
+       *((uint64_t*)&b[64-sizeof(uint64_t)]) = (state->counter * 512) + length;
+       md5_nextBlock(state, b);
+}
diff --git a/md5.h b/md5.h
index bd91f23080e6d7392f1fad64fc64328f001c1afa..24fb6d543a1df319526d06a54e5078e7b49b7aff 100644 (file)
--- a/md5.h
+++ b/md5.h
 #ifndef MD5_H_
 #define MD5_H_
 
+#include <stdint.h>
+
+typedef struct md5_ctx_st {
+       uint32_t a[4];
+       uint32_t counter;
+} md5_ctx_t;
+
+void md5_init(md5_ctx_t *s);
+void md5_nextBlock(md5_ctx_t *state, void* block);
+void md5_lastBlock(md5_ctx_t *state, void* block, uint16_t length);
+
 #endif /*MD5_H_*/
diff --git a/md5_sbox.h b/md5_sbox.h
new file mode 100644 (file)
index 0000000..6f5f7f2
--- /dev/null
@@ -0,0 +1,19 @@
+#ifndef MD5_SBOX_H_
+#define MD5_SBOX_H_
+
+uint32_t md5_T[]={
+       0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 
+       0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 
+       0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 
+       0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 
+       0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 
+       0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 
+       0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 
+       0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 
+       0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 
+       0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, 0xf4292244, 0x432aff97, 
+       0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 
+       0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 
+       0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391};
+
+#endif /*MD5_SBOX_H_*/