Embedded systems coursework 2.
Fork of Crypto_light by
Diff: MD5.cpp
- Revision:
- 3:85c6ee25cf3e
- Parent:
- 1:14a7cea431aa
- Child:
- 4:0da19393bd57
--- a/MD5.cpp Mon Sep 09 16:16:24 2013 +0000 +++ b/MD5.cpp Wed Sep 11 17:22:40 2013 +0000 @@ -1,72 +1,31 @@ #include "MD5.h" #include <string.h> -static const uint32_t 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 -}; - static const uint32_t A = 0x67452301; static const uint32_t B = 0xefcdab89; static const uint32_t C = 0x98badcfe; static const uint32_t D = 0x10325476; -static uint32_t F(uint32_t x, uint32_t y, uint32_t z) -{ - return (x & y) | ((~x) & z); -} - -static uint32_t G(uint32_t x, uint32_t y, uint32_t z) -{ - return (x & z) | (y & (~z)); -} -static uint32_t H(uint32_t x, uint32_t y, uint32_t z) -{ - return x ^ y ^ z; -} +#define F(X,Y,Z) (((X) & (Y)) | ((~(X)) & (Z))) +#define G(X,Y,Z) (((X) & (Z)) | ((Y) & (~(Z)))) +#define H(X,Y,Z) ((X) ^ (Y) ^ (Z)) +#define I(X,Y,Z) ((Y) ^ ((X) | (~(Z)))) -static uint32_t I(uint32_t x, uint32_t y, uint32_t z) -{ - return y ^ (x | (~z)); -} +#define ROTL(W,N) (((W) << N) | ((W) >> (32-N))) -static uint32_t rotLeft(uint32_t w, uint8_t s) -{ - return (w << s) | (w >> (32-s)); -} +#define ROUND1(a,b,c,d,x,s,t) \ + a = ROTL(a + F(b,c,d) + x + t,s) + b; + +#define ROUND2(a,b,c,d,x,s,t) \ + a = ROTL(a + G(b,c,d) + x + t,s) + b; -#define ROUND1(a,b,c,d,k,s,i) \ - a += F(b,c,d) + x[k] + T[i-1]; \ - a = rotLeft(a,s);\ - a += b; -#define ROUND2(a,b,c,d,k,s,i) \ - a += G(b,c,d) + x[k] + T[i-1]; \ - a = rotLeft(a,s);\ - a += b; -#define ROUND3(a,b,c,d,k,s,i) \ - a += H(b,c,d) + x[k] + T[i-1]; \ - a = rotLeft(a,s);\ - a += b; -#define ROUND4(a,b,c,d,k,s,i) \ - a += I(b,c,d) + x[k] + T[i-1]; \ - a = rotLeft(a,s);\ - a += b; +#define ROUND3(a,b,c,d,x,s,t) \ + a = ROTL(a + H(b,c,d) + x + t,s) + b; + +#define ROUND4(a,b,c,d,x,s,t) \ + a = ROTL(a + I(b,c,d) + x + t,s) + b; + MD5::MD5(): @@ -118,17 +77,22 @@ padding = 56 - (totalBufferLength % 64); else padding = 56 + (64 - (totalBufferLength % 64)); - uint8_t val = 0x80; - add(&val, 1); - val = 0; - for(int i = 0; i < padding-1; ++i) - add(&val,1); - totalBufferLength -= padding; - uint64_t lengthBit = totalBufferLength * 8; + buffer[bufferLength++] = 0x80; + padding--; + if(padding+bufferLength == 56) + memset(&buffer[bufferLength], 0, padding); + else + { + memset(&buffer[bufferLength], 0, 64-bufferLength); + computeRounds(&a, &b, &c, &d, buffer); + memset(buffer, 0, bufferLength); + } + uint64_t lengthBit = totalBufferLength << 3; uint32_t lengthBitLow = lengthBit; uint32_t lengthBitHigh = lengthBit >> 32; - add((uint8_t*)&lengthBitLow,4); - add((uint8_t*)&lengthBitHigh,4); + memcpy(&buffer[56], &lengthBitLow, 4); + memcpy(&buffer[60], &lengthBitHigh, 4); + computeRounds(&a, &b, &c, &d, buffer); memcpy(digest, &a, 4); memcpy(&digest[4], &b, 4); @@ -149,47 +113,44 @@ uint32_t a = *a2, b = *b2, c = *c2, d = *d2; uint32_t tmpA = a, tmpB = b, tmpC = c, tmpD = d; - uint32_t x[16]; - for(int j = 0; j < 16; ++j) - memcpy(&x[j], &buffer[j*4], 4); - + uint32_t *x = (uint32_t*)buffer; + // Round 1 - ROUND1(a,b,c,d,0,7,1); ROUND1(d,a,b,c,1,12,2); ROUND1(c,d,a,b,2,17,3); ROUND1(b,c,d,a,3,22,4); - ROUND1(a,b,c,d,4,7,5); ROUND1(d,a,b,c,5,12,6); ROUND1(c,d,a,b,6,17,7); ROUND1(b,c,d,a,7,22,8); - ROUND1(a,b,c,d,8,7,9); ROUND1(d,a,b,c,9,12,10); ROUND1(c,d,a,b,10,17,11); ROUND1(b,c,d,a,11,22,12); - ROUND1(a,b,c,d,12,7,13); ROUND1(d,a,b,c,13,12,14); ROUND1(c,d,a,b,14,17,15); ROUND1(b,c,d,a,15,22,16); - + ROUND1(a,b,c,d,x[0],7,0xd76aa478); ROUND1(d,a,b,c,x[1],12,0xe8c7b756); ROUND1(c,d,a,b,x[2],17,0x242070db); ROUND1(b,c,d,a,x[3],22,0xc1bdceee); + ROUND1(a,b,c,d,x[4],7,0xf57c0faf); ROUND1(d,a,b,c,x[5],12,0x4787c62a); ROUND1(c,d,a,b,x[6],17,0xa8304613); ROUND1(b,c,d,a,x[7],22,0xfd469501); + ROUND1(a,b,c,d,x[8],7,0x698098d8); ROUND1(d,a,b,c,x[9],12,0x8b44f7af); ROUND1(c,d,a,b,x[10],17,0xffff5bb1); ROUND1(b,c,d,a,x[11],22,0x895cd7be); + ROUND1(a,b,c,d,x[12],7,0x6b901122); ROUND1(d,a,b,c,x[13],12,0xfd987193); ROUND1(c,d,a,b,x[14],17,0xa679438e); ROUND1(b,c,d,a,x[15],22,0x49b40821); + + // Round 2 - ROUND2(a,b,c,d,1,5,17); ROUND2(d,a,b,c,6,9,18); ROUND2(c,d,a,b,11,14,19); ROUND2(b,c,d,a,0,20,20); - ROUND2(a,b,c,d,5,5,21); ROUND2(d,a,b,c,10,9,22); ROUND2(c,d,a,b,15,14,23); ROUND2(b,c,d,a,4,20,24); - ROUND2(a,b,c,d,9,5,25); ROUND2(d,a,b,c,14,9,26); ROUND2(c,d,a,b,3,14,27); ROUND2(b,c,d,a,8,20,28); - ROUND2(a,b,c,d,13,5,29); ROUND2(d,a,b,c,2,9,30); ROUND2(c,d,a,b,7,14,31); ROUND2(b,c,d,a,12,20,32); + ROUND2(a,b,c,d,x[1],5,0xf61e2562); ROUND2(d,a,b,c,x[6],9,0xc040b340); ROUND2(c,d,a,b,x[11],14,0x265e5a51); ROUND2(b,c,d,a,x[0],20,0xe9b6c7aa); + ROUND2(a,b,c,d,x[5],5,0xd62f105d); ROUND2(d,a,b,c,x[10],9,0x02441453); ROUND2(c,d,a,b,x[15],14,0xd8a1e681); ROUND2(b,c,d,a,x[4],20,0xe7d3fbc8); + ROUND2(a,b,c,d,x[9],5,0x21e1cde6); ROUND2(d,a,b,c,x[14],9,0xc33707d6); ROUND2(c,d,a,b,x[3],14,0xf4d50d87); ROUND2(b,c,d,a,x[8],20,0x455a14ed); + ROUND2(a,b,c,d,x[13],5,0xa9e3e905); ROUND2(d,a,b,c,x[2],9,0xfcefa3f8); ROUND2(c,d,a,b,x[7],14,0x676f02d9); ROUND2(b,c,d,a,x[12],20,0x8d2a4c8a); + // Round 3 - ROUND3(a,b,c,d,5,4,33); ROUND3(d,a,b,c,8,11,34); ROUND3(c,d,a,b,11,16,35); ROUND3(b,c,d,a,14,23,36); - ROUND3(a,b,c,d,1,4,37); ROUND3(d,a,b,c,4,11,38); ROUND3(c,d,a,b,7,16,39); ROUND3(b,c,d,a,10,23,40); - ROUND3(a,b,c,d,13,4,41); ROUND3(d,a,b,c,0,11,42); ROUND3(c,d,a,b,3,16,43); ROUND3(b,c,d,a,6,23,44); - ROUND3(a,b,c,d,9,4,45); ROUND3(d,a,b,c,12,11,46); ROUND3(c,d,a,b,15,16,47); ROUND3(b,c,d,a,2,23,48); - + ROUND3(a,b,c,d,x[5],4,0xfffa3942); ROUND3(d,a,b,c,x[8],11,0x8771f681); ROUND3(c,d,a,b,x[11],16,0x6d9d6122); ROUND3(b,c,d,a,x[14],23,0xfde5380c); + ROUND3(a,b,c,d,x[1],4,0xa4beea44); ROUND3(d,a,b,c,x[4],11,0x4bdecfa9); ROUND3(c,d,a,b,x[7],16,0xf6bb4b60); ROUND3(b,c,d,a,x[10],23,0xbebfbc70); + ROUND3(a,b,c,d,x[13],4,0x289b7ec6); ROUND3(d,a,b,c,x[0],11,0xeaa127fa); ROUND3(c,d,a,b,x[3],16,0xd4ef3085); ROUND3(b,c,d,a,x[6],23,0x04881d05); + ROUND3(a,b,c,d,x[9],4,0xd9d4d039); ROUND3(d,a,b,c,x[12],11,0xe6db99e5); ROUND3(c,d,a,b,x[15],16,0x1fa27cf8); ROUND3(b,c,d,a,x[2],23,0xc4ac5665); + + // Round 4 - ROUND4(a,b,c,d,0,6,49); ROUND4(d,a,b,c,7,10,50); ROUND4(c,d,a,b,14,15,51); ROUND4(b,c,d,a,5,21,52); - ROUND4(a,b,c,d,12,6,53); ROUND4(d,a,b,c,3,10,54); ROUND4(c,d,a,b,10,15,55); ROUND4(b,c,d,a,1,21,56); - ROUND4(a,b,c,d,8,6,57); ROUND4(d,a,b,c,15,10,58); ROUND4(c,d,a,b,6,15,59); ROUND4(b,c,d,a,13,21,60); - ROUND4(a,b,c,d,4,6,61); ROUND4(d,a,b,c,11,10,62); ROUND4(c,d,a,b,2,15,63); ROUND4(b,c,d,a,9,21,64); - - a += tmpA; - b += tmpB; - c += tmpC; - d += tmpD; - - *a2 = a; - *b2 = b; - *c2 = c; - *d2 = d; + ROUND4(a,b,c,d,x[0],6,0xf4292244); ROUND4(d,a,b,c,x[7],10,0x432aff97); ROUND4(c,d,a,b,x[14],15,0xab9423a7); ROUND4(b,c,d,a,x[5],21,0xfc93a039); + ROUND4(a,b,c,d,x[12],6,0x655b59c3); ROUND4(d,a,b,c,x[3],10,0x8f0ccc92); ROUND4(c,d,a,b,x[10],15,0xffeff47d); ROUND4(b,c,d,a,x[1],21,0x85845dd1); + ROUND4(a,b,c,d,x[8],6,0x6fa87e4f); ROUND4(d,a,b,c,x[15],10,0xfe2ce6e0); ROUND4(c,d,a,b,x[6],15,0xa3014314); ROUND4(b,c,d,a,x[13],21,0x4e0811a1); + ROUND4(a,b,c,d,x[4],6,0xf7537e82); ROUND4(d,a,b,c,x[11],10,0xbd3af235); ROUND4(c,d,a,b,x[2],15,0x2ad7d2bb); ROUND4(b,c,d,a,x[9],21,0xeb86d391); + + *a2 = a + tmpA; + *b2 = b + tmpB; + *c2 = c + tmpC; + *d2 = d + tmpD; } void MD5::computeDigest(uint8_t *digest, uint8_t *msg, uint32_t length) { + uint64_t lengthBit = length << 3; uint16_t padding; if(length % 64 < 56) padding = 56 - (length % 64); @@ -197,29 +158,25 @@ padding = 56 + (64 - (length % 64)); uint32_t a = A, b = B, c = C, d = D; - - uint32_t offset = 0; - while(length - offset >= 64) + while(length >= 64) { - computeRounds(&a, &b, &c, &d, &msg[offset]); - offset += 64; + computeRounds(&a, &b, &c, &d, msg); + msg += 64; + length -= 64; } uint8_t buffer[64]; - memcpy(buffer, &msg[offset], length-offset); - uint8_t bufferLength = length - offset; - buffer[bufferLength++] = 0x80; + memcpy(buffer, msg, length); + buffer[length++] = 0x80; padding--; - while(padding > 0) + if(padding+length == 56) + memset(&buffer[length], 0, padding); + else { - if(bufferLength == 64) - { - computeRounds(&a, &b, &c, &d, buffer); - bufferLength = 0; - } - buffer[bufferLength++] = 0; - padding--; + memset(&buffer[length], 0, 64-length); + computeRounds(&a, &b, &c, &d, msg); + memset(buffer, 0, length); } - uint64_t lengthBit = length * 8; + uint32_t lengthBitLow = lengthBit; uint32_t lengthBitHigh = lengthBit >> 32; memcpy(&buffer[56], &lengthBitLow, 4);