diff options
Diffstat (limited to 'process.c')
-rw-r--r-- | process.c | 44 |
1 files changed, 23 insertions, 21 deletions
@@ -2,33 +2,34 @@ #include "common.h" -static inline uint32_t -rorl(uint32_t n, int k) +static inline uint_least32_t +rorl(uint_least32_t n, int k) { - return (n << k) | (n >> (32 - k)); + return TRUNC32((n << k) | (n >> (32 - k))); } void libsha1_process(struct libsha1_state *restrict state, const unsigned char *restrict chunk) { -#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 += rorl(a, 5) + F0(b, c, d) + state->w[i] + (uint32_t)0x5A827999UL, b = rorl(b, 30)) -#define G1(a, b, c, d, e, i) (e += rorl(a, 5) + F1(b, c, d) + state->w[i] + (uint32_t)0x6ED9EBA1UL, b = rorl(b, 30)) -#define G2(a, b, c, d, e, i) (e += rorl(a, 5) + F2(b, c, d) + state->w[i] + (uint32_t)0x8F1BBCDCUL, b = rorl(b, 30)) -#define G3(a, b, c, d, e, i) (e += rorl(a, 5) + F3(b, c, d) + state->w[i] + (uint32_t)0xCA62C1D6UL, b = rorl(b, 30)) +#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 G_(A, B, C, D, E, I, F, X) (E = TRUNC32(E + rorl(A, 5) + F(B, C, D) + state->w[I] + (uint_least32_t)X##UL), B = rorl(B, 30)) +#define G0(A, B, C, D, E, I) G_(A, B, C, D, E, I, F0, 0x5A827999) +#define G1(A, B, C, D, E, I) G_(A, B, C, D, E, I, F1, 0x6ED9EBA1) +#define G2(A, B, C, D, E, I) G_(A, B, C, D, E, I, F2, 0x8F1BBCDC) +#define G3(A, B, C, D, E, I) G_(A, B, C, D, E, I, F3, 0xCA62C1D6) - uint32_t a, b, c, d, e; + uint_least32_t a, b, c, d, e; int i; for (i = 0; i < 16; i++) { - state->w[i] = (uint32_t)chunk[4 * i + 0] << 24; - state->w[i] |= (uint32_t)chunk[4 * i + 1] << 16; - state->w[i] |= (uint32_t)chunk[4 * i + 2] << 8; - state->w[i] |= (uint32_t)chunk[4 * i + 3]; + state->w[i] = (uint_least32_t)chunk[4 * i + 0] << 24; + state->w[i] |= (uint_least32_t)chunk[4 * i + 1] << 16; + state->w[i] |= (uint_least32_t)chunk[4 * i + 2] << 8; + state->w[i] |= (uint_least32_t)chunk[4 * i + 3]; } if (state->algorithm == LIBSHA1_1) { for (; i < 80; i++) @@ -70,16 +71,17 @@ libsha1_process(struct libsha1_state *restrict state, const unsigned char *restr G3(c, d, e, a, b, i++); G3(b, c, d, e, a, i++); } - state->h[0] += a; - state->h[1] += b; - state->h[2] += c; - state->h[3] += d; - state->h[4] += e; + state->h[0] = TRUNC32(state->h[0] + a); + state->h[1] = TRUNC32(state->h[1] + b); + state->h[2] = TRUNC32(state->h[2] + c); + state->h[3] = TRUNC32(state->h[3] + d); + state->h[4] = TRUNC32(state->h[4] + e); #undef F0 #undef F1 #undef F2 #undef F3 +#undef G_ #undef G0 #undef G1 #undef G2 |