diff --git a/src/crypt/crypt_md5.c b/src/crypt/crypt_md5.c index 02f2244..684a9fe 100644 --- a/src/crypt/crypt_md5.c +++ b/src/crypt/crypt_md5.c @@ -22,11 +22,23 @@ static uint32_t rol(uint32_t n, int k) { return (n << k) | (n >> (32-k)); } #define G(x,y,z) (y ^ (z & (y ^ x))) #define H(x,y,z) (x ^ y ^ z) #define I(x,y,z) (y ^ (x | ~z)) -#define FF(a,b,c,d,w,s,t) a += F(b,c,d) + w + t; a = rol(a,s) + b -#define GG(a,b,c,d,w,s,t) a += G(b,c,d) + w + t; a = rol(a,s) + b -#define HH(a,b,c,d,w,s,t) a += H(b,c,d) + w + t; a = rol(a,s) + b -#define II(a,b,c,d,w,s,t) a += I(b,c,d) + w + t; a = rol(a,s) + b +#define FF(a,b,c,d,w,r,t) a += F(b,c,d) + w + t; a = rol(a,r) + b +#define GG(a,b,c,d,w,r,t) a += G(b,c,d) + w + t; a = rol(a,r) + b +#define HH(a,b,c,d,w,r,t) a += H(b,c,d) + w + t; a = rol(a,r) + b +#define II(a,b,c,d,w,r,t) a += I(b,c,d) + w + t; a = rol(a,r) + b +static const uint8_t idx[64] = { +0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15, +1,6,11,0,5,10,15,4,9,14,3,8,13,2,7,12, +5,8,11,14,1,4,7,10,13,0,3,6,9,12,15,2, +0,7,14,5,12,3,10,1,8,15,6,13,4,11,2,9 +}; +static const uint8_t rot[64] = { +7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22, +5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20, +4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23, +6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21 +}; static const uint32_t tab[64] = { 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, @@ -40,7 +52,7 @@ static const uint32_t tab[64] = { static void processblock(struct md5 *s, const uint8_t *buf) { - uint32_t i, W[16], a, b, c, d; + uint32_t i, W[16], a, b, c, d, x; for (i = 0; i < 16; i++) { W[i] = buf[4*i]; @@ -54,30 +66,21 @@ static void processblock(struct md5 *s, const uint8_t *buf) c = s->h[2]; d = s->h[3]; - i = 0; - while (i < 16) { - FF(a,b,c,d, W[i], 7, tab[i]); i++; - FF(d,a,b,c, W[i], 12, tab[i]); i++; - FF(c,d,a,b, W[i], 17, tab[i]); i++; - FF(b,c,d,a, W[i], 22, tab[i]); i++; + for (i = 0; i < 16; i++) { + FF(a,b,c,d,W[idx[i]],rot[i],tab[i]); + x = d; d = c; c = b; b = a; a = x; } - while (i < 32) { - GG(a,b,c,d, W[(5*i+1)%16], 5, tab[i]); i++; - GG(d,a,b,c, W[(5*i+1)%16], 9, tab[i]); i++; - GG(c,d,a,b, W[(5*i+1)%16], 14, tab[i]); i++; - GG(b,c,d,a, W[(5*i+1)%16], 20, tab[i]); i++; + for (; i < 32; i++) { + GG(a,b,c,d,W[idx[i]],rot[i],tab[i]); + x = d; d = c; c = b; b = a; a = x; } - while (i < 48) { - HH(a,b,c,d, W[(3*i+5)%16], 4, tab[i]); i++; - HH(d,a,b,c, W[(3*i+5)%16], 11, tab[i]); i++; - HH(c,d,a,b, W[(3*i+5)%16], 16, tab[i]); i++; - HH(b,c,d,a, W[(3*i+5)%16], 23, tab[i]); i++; + for (; i < 48; i++) { + HH(a,b,c,d,W[idx[i]],rot[i],tab[i]); + x = d; d = c; c = b; b = a; a = x; } - while (i < 64) { - II(a,b,c,d, W[7*i%16], 6, tab[i]); i++; - II(d,a,b,c, W[7*i%16], 10, tab[i]); i++; - II(c,d,a,b, W[7*i%16], 15, tab[i]); i++; - II(b,c,d,a, W[7*i%16], 21, tab[i]); i++; + for (; i < 64; i++) { + II(a,b,c,d,W[idx[i]],rot[i],tab[i]); + x = d; d = c; c = b; b = a; a = x; } s->h[0] += a;