aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2022-07-07 14:32:58 +0200
committerMattias Andrée <maandree@kth.se>2022-07-07 14:32:58 +0200
commit811a82296969a037d1e580d65bb5a73241ffae1f (patch)
tree404759c5e429d450c91f110497758424fb4eaf1d
parentDon't marshal w (diff)
downloadlibsha1-811a82296969a037d1e580d65bb5a73241ffae1f.tar.gz
libsha1-811a82296969a037d1e580d65bb5a73241ffae1f.tar.bz2
libsha1-811a82296969a037d1e580d65bb5a73241ffae1f.tar.xz
Use uint_least32_t instead of uint32_t
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r--common.h10
-rw-r--r--init.c2
-rw-r--r--libsha1.h4
-rw-r--r--process.c44
4 files changed, 36 insertions, 24 deletions
diff --git a/common.h b/common.h
index 864082c..2c8250c 100644
--- a/common.h
+++ b/common.h
@@ -4,6 +4,7 @@
#include <sys/stat.h>
#include <alloca.h>
#include <errno.h>
+#include <inttypes.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
@@ -16,6 +17,15 @@
/**
+ * Truncate an unsigned integer to an unsigned 32-bit integer
+ *
+ * @param X:uint_least32_t The value to truncate
+ * @return :uint_least32_t The 32 lowest bits in `X`
+ */
+#define TRUNC32(X) ((X) & (uint_least32_t)0xFFFFFFFFUL)
+
+
+/**
* Process a chunk using SHA-1 or SHA-0
*
* @param state The hashing state
diff --git a/init.c b/init.c
index 4d319f7..8cdaea5 100644
--- a/init.c
+++ b/init.c
@@ -5,7 +5,7 @@
/**
* Initial state for SHA-1 and SHA-0
*/
-static const uint32_t H[] = {
+static const uint_least32_t H[] = {
0x67452301UL, 0xEFCDAB89UL, 0x98BADCFEUL, 0x10325476UL, 0xC3D2E1F0UL
};
diff --git a/libsha1.h b/libsha1.h
index 9865b3b..c841e85 100644
--- a/libsha1.h
+++ b/libsha1.h
@@ -44,12 +44,12 @@ struct libsha1_state {
*
* Does not need to be marshalled
*/
- uint32_t w[80];
+ uint_least32_t w[80];
/**
* Hashing values
*/
- uint32_t h[5];
+ uint_least32_t h[5];
/**
* Space for chunks to process
diff --git a/process.c b/process.c
index fb7df21..d9273f8 100644
--- a/process.c
+++ b/process.c
@@ -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