]> git.cryptolib.org Git - avr-crypto-lib.git/blobdiff - md5.c
renaming to AVR-Crypto-Lib
[avr-crypto-lib.git] / md5.c
diff --git a/md5.c b/md5.c
index 88f4f6871e7e140ad23ef0edf380f3ec33a6871b..1ecf02aa9e3a8d6046a464cca94038a4371bcfc4 100644 (file)
--- a/md5.c
+++ b/md5.c
@@ -1,6 +1,6 @@
 /* md5.c */
 /*
-    This file is part of the Crypto-avr-lib/microcrypt-lib.
+    This file is part of the This file is part of the AVR-Crypto-Lib.
     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
 
     This program is free software: you can redistribute it and/or modify
@@ -40,19 +40,23 @@ void md5_init(md5_ctx_t *s){
        s->a[2] = 0x98badcfe;
        s->a[3] = 0x10325476;
 }
+
+static 
 uint32_t md5_F(uint32_t x, uint32_t y, uint32_t z){
        return ((x&y)|((~x)&z));
 }
 
+static
 uint32_t md5_G(uint32_t x, uint32_t y, uint32_t z){
        return ((x&z)|((~z)&y));
 }
 
+static
 uint32_t md5_H(uint32_t x, uint32_t y, uint32_t z){
        return (x^y^z);
 }
 
+static
 uint32_t md5_I(uint32_t x, uint32_t y, uint32_t z){
        return (y ^ (x | (~z)));
 }
@@ -61,7 +65,8 @@ 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){
+static
+void md5_core(uint32_t* a, void* block, uint8_t as, uint8_t s, uint8_t i, uint8_t fi){
        uint32_t t;
        md5_func_t* funcs[]={md5_F, md5_G, md5_H, md5_I};
        as &= 0x3;
@@ -75,11 +80,11 @@ void md5_core(uint32_t* a, uint8_t as, void* block, uint8_t k, uint8_t s, uint8_
        uart_hexdump(&s, 1); uart_putc(' ');
        uart_hexdump(&i, 1); uart_putc(']');
 #endif 
-       t = a[as] + funcs[fi](a[(as+1)&3], a[(as+2)&3], a[(as+3)&3]) + ((uint32_t*)block)[k] + md5_T[i] ;
+       t = a[as] + funcs[fi](a[(as+1)&3], a[(as+2)&3], a[(as+3)&3]) + *((uint32_t*)block) + md5_T[i] ;
        a[as]=a[(as+1)&3] + ROTL32(t, s);
 }
 
-void md5_nextBlock(md5_ctx_t *state, void* block){
+void md5_nextBlock(md5_ctx_t *state, const void* block){
        uint32_t        a[4];
        uint8_t         m,n,i=0;
        /* this requires other mixed sboxes */
@@ -97,31 +102,31 @@ void md5_nextBlock(md5_ctx_t *state, void* block){
        a[3]=state->a[3];
        
        /* round 1 */
-       uint8_t s1t[]={7,12,17,22};
+       uint8_t s1t[]={7,12,17,22}; // 1,-1   1,4   2,-1   3,-2
        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);
+                       md5_core(a, &(((uint32_t*)block)[m*4+n]), 4-n, s1t[n],i++,0);
                }
        }
        /* round 2 */
-       uint8_t s2t[]={5,9,14,20};
+       uint8_t s2t[]={5,9,14,20}; // 1,-3   1,1   2,-2   2,4
        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);
+                       md5_core(a, &(((uint32_t*)block)[(1+m*4+n*5)&0xf]), 4-n, s2t[n],i++,1);
                }
        }
        /* round 3 */
-       uint8_t s3t[]={4,11,16,23};
+       uint8_t s3t[]={4,11,16,23}; // 0,4   1,3   2,0   3,-1
        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);
+                       md5_core(a, &(((uint32_t*)block)[(5-m*4+n*3)&0xf]), 4-n, s3t[n],i++,2);
                }
        }
        /* round 4 */
-       uint8_t s4t[]={6,10,15,21};
+       uint8_t s4t[]={6,10,15,21}; // 1,-2   1,2   2,-1   3,-3
        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);
+                       md5_core(a, &(((uint32_t*)block)[(0-m*4+n*7)&0xf]), 4-n, s4t[n],i++,3);
                }
        }
        state->a[0] += a[0];
@@ -131,7 +136,7 @@ void md5_nextBlock(md5_ctx_t *state, void* block){
        state->counter++;
 }
 
-void md5_lastBlock(md5_ctx_t *state, void* block, uint16_t length_b){
+void md5_lastBlock(md5_ctx_t *state, const void* block, uint16_t length_b){
        uint16_t l;
        uint8_t b[64];
        while (length_b >= 512){
@@ -155,8 +160,25 @@ void md5_lastBlock(md5_ctx_t *state, void* block, uint16_t length_b){
        if(l+sizeof(uint64_t) >= 512/8){
                md5_nextBlock(state, b);
                state->counter--;
-               memset(b, 0, 64);
+               memset(b, 0, 64-8);
        }
        *((uint64_t*)&b[64-sizeof(uint64_t)]) = (state->counter * 512) + length_b;
        md5_nextBlock(state, b);
 }
+
+void md5_ctx2hash(md5_hash_t* dest, const md5_ctx_t* state){
+       memcpy(dest, state->a, MD5_HASH_BYTES);
+}
+
+void md5(md5_hash_t* dest, const void* msg, uint32_t length_b){
+       md5_ctx_t ctx;
+       md5_init(&ctx);
+       while(length_b>=MD5_BLOCK_BITS){
+               md5_nextBlock(&ctx, msg);
+               msg = (uint8_t*)msg + MD5_BLOCK_BYTES;
+               length_b -= MD5_BLOCK_BITS;
+       }
+       md5_lastBlock(&ctx, msg, length_b);
+       md5_ctx2hash(dest, &ctx);
+}
+