From 30f3baa340417b9a53bf702224917c6649daa446 Mon Sep 17 00:00:00 2001 From: Jens Nyberg Date: Mon, 15 Jul 2013 01:45:29 +0200 Subject: [PATCH] More consistancy between md5 and sha1 Signed-off-by: Jens Nyberg --- util/md5.c | 6 +++++- util/sha1.c | 59 +++++++++++++++++++++++++++++++++-------------------------- 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/util/md5.c b/util/md5.c index 0d8353d..733cc06 100644 --- a/util/md5.c +++ b/util/md5.c @@ -3,7 +3,6 @@ #include #include "../md5.h" -static uint32_t rol(uint32_t n, int k) { return (n << k) | (n >> (32-k)); } #define F(x,y,z) (z ^ (x & (y ^ z))) #define G(x,y,z) (y ^ (z & (y ^ x))) #define H(x,y,z) (x ^ y ^ z) @@ -24,6 +23,11 @@ static const uint32_t tab[64] = { 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 }; +static uint32_t rol(uint32_t n, int k) +{ + return (n << k) | (n >> (32-k)); +} + static void processblock(struct md5 *s, const uint8_t *buf) { uint32_t i, W[16], a, b, c, d; diff --git a/util/sha1.c b/util/sha1.c index 9910d78..c1d2e4f 100644 --- a/util/sha1.c +++ b/util/sha1.c @@ -3,15 +3,19 @@ #include #include "../sha1.h" -static uint32_t rol(uint32_t n, int k) { return (n << k) | (n >> (32-k)); } #define F0(b,c,d) (d ^ (b & (c ^ d))) #define F1(b,c,d) (b ^ c ^ d) #define F2(b,c,d) ((b & c) | (d & (b | c))) #define F3(b,c,d) (b ^ c ^ d) -#define G0(a,b,c,d,e,i) e += rol(a,5)+F0(b,c,d)+W[i]+0x5A827999; b = rol(b,30) -#define G1(a,b,c,d,e,i) e += rol(a,5)+F1(b,c,d)+W[i]+0x6ED9EBA1; b = rol(b,30) -#define G2(a,b,c,d,e,i) e += rol(a,5)+F2(b,c,d)+W[i]+0x8F1BBCDC; b = rol(b,30) -#define G3(a,b,c,d,e,i) e += rol(a,5)+F3(b,c,d)+W[i]+0xCA62C1D6; b = rol(b,30) +#define G0(a,b,c,d,e,w) e += rol(a,5)+F0(b,c,d)+w+0x5A827999; b = rol(b,30) +#define G1(a,b,c,d,e,w) e += rol(a,5)+F1(b,c,d)+w+0x6ED9EBA1; b = rol(b,30) +#define G2(a,b,c,d,e,w) e += rol(a,5)+F2(b,c,d)+w+0x8F1BBCDC; b = rol(b,30) +#define G3(a,b,c,d,e,w) e += rol(a,5)+F3(b,c,d)+w+0xCA62C1D6; b = rol(b,30) + +static uint32_t rol(uint32_t n, int k) +{ + return (n << k) | (n >> (32-k)); +} static void processblock(struct sha1 *s, const uint8_t *buf) { @@ -31,33 +35,36 @@ static void processblock(struct sha1 *s, const uint8_t *buf) c = s->h[2]; d = s->h[3]; e = s->h[4]; - for (i = 0; i < 20; ) { - G0(a,b,c,d,e,i++); - G0(e,a,b,c,d,i++); - G0(d,e,a,b,c,i++); - G0(c,d,e,a,b,i++); - G0(b,c,d,e,a,i++); + + i = 0; + + while (i < 20) { + G0(a,b,c,d,e,W[i]); i++; + G0(e,a,b,c,d,W[i]); i++; + G0(d,e,a,b,c,W[i]); i++; + G0(c,d,e,a,b,W[i]); i++; + G0(b,c,d,e,a,W[i]); i++; } while (i < 40) { - G1(a,b,c,d,e,i++); - G1(e,a,b,c,d,i++); - G1(d,e,a,b,c,i++); - G1(c,d,e,a,b,i++); - G1(b,c,d,e,a,i++); + G1(a,b,c,d,e,W[i]); i++; + G1(e,a,b,c,d,W[i]); i++; + G1(d,e,a,b,c,W[i]); i++; + G1(c,d,e,a,b,W[i]); i++; + G1(b,c,d,e,a,W[i]); i++; } while (i < 60) { - G2(a,b,c,d,e,i++); - G2(e,a,b,c,d,i++); - G2(d,e,a,b,c,i++); - G2(c,d,e,a,b,i++); - G2(b,c,d,e,a,i++); + G2(a,b,c,d,e,W[i]); i++; + G2(e,a,b,c,d,W[i]); i++; + G2(d,e,a,b,c,W[i]); i++; + G2(c,d,e,a,b,W[i]); i++; + G2(b,c,d,e,a,W[i]); i++; } while (i < 80) { - G3(a,b,c,d,e,i++); - G3(e,a,b,c,d,i++); - G3(d,e,a,b,c,i++); - G3(c,d,e,a,b,i++); - G3(b,c,d,e,a,i++); + G3(a,b,c,d,e,W[i]); i++; + G3(e,a,b,c,d,W[i]); i++; + G3(d,e,a,b,c,W[i]); i++; + G3(c,d,e,a,b,W[i]); i++; + G3(b,c,d,e,a,W[i]); i++; } s->h[0] += a; s->h[1] += b; -- 1.8.3.2