X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/puzzles/blobdiff_plain/2ef96bd6ebb0e89bc054d2edb1ef280c97faa955..b498c53924c571d997f97c7ff3dd81c3e66894a1:/random.c diff --git a/random.c b/random.c index dad2c0e..d70dd00 100644 --- a/random.c +++ b/random.c @@ -15,15 +15,6 @@ #include "puzzles.h" -typedef unsigned long uint32; - -typedef struct { - uint32 h[5]; - unsigned char block[64]; - int blkused; - uint32 lenhi, lenlo; -} SHA_State; - /* ---------------------------------------------------------------------- * Core SHA algorithm: processes 16-word blocks into a message digest. */ @@ -108,14 +99,14 @@ static void SHATransform(uint32 * digest, uint32 * block) * the end, and pass those blocks to the core SHA algorithm. */ -static void SHA_Init(SHA_State * s) +void SHA_Init(SHA_State * s) { SHA_Core_Init(s->h); s->blkused = 0; s->lenhi = s->lenlo = 0; } -static void SHA_Bytes(SHA_State * s, void *p, int len) +void SHA_Bytes(SHA_State * s, void *p, int len) { unsigned char *q = (unsigned char *) p; uint32 wordblock[16]; @@ -158,7 +149,7 @@ static void SHA_Bytes(SHA_State * s, void *p, int len) } } -static void SHA_Final(SHA_State * s, unsigned char *output) +void SHA_Final(SHA_State * s, unsigned char *output) { int i; int pad; @@ -177,26 +168,26 @@ static void SHA_Final(SHA_State * s, unsigned char *output) c[0] = 0x80; SHA_Bytes(s, &c, pad); - c[0] = (lenhi >> 24) & 0xFF; - c[1] = (lenhi >> 16) & 0xFF; - c[2] = (lenhi >> 8) & 0xFF; - c[3] = (lenhi >> 0) & 0xFF; - c[4] = (lenlo >> 24) & 0xFF; - c[5] = (lenlo >> 16) & 0xFF; - c[6] = (lenlo >> 8) & 0xFF; - c[7] = (lenlo >> 0) & 0xFF; + c[0] = (unsigned char)((lenhi >> 24) & 0xFF); + c[1] = (unsigned char)((lenhi >> 16) & 0xFF); + c[2] = (unsigned char)((lenhi >> 8) & 0xFF); + c[3] = (unsigned char)((lenhi >> 0) & 0xFF); + c[4] = (unsigned char)((lenlo >> 24) & 0xFF); + c[5] = (unsigned char)((lenlo >> 16) & 0xFF); + c[6] = (unsigned char)((lenlo >> 8) & 0xFF); + c[7] = (unsigned char)((lenlo >> 0) & 0xFF); SHA_Bytes(s, &c, 8); for (i = 0; i < 5; i++) { - output[i * 4] = (s->h[i] >> 24) & 0xFF; - output[i * 4 + 1] = (s->h[i] >> 16) & 0xFF; - output[i * 4 + 2] = (s->h[i] >> 8) & 0xFF; - output[i * 4 + 3] = (s->h[i]) & 0xFF; + output[i * 4] = (unsigned char)((s->h[i] >> 24) & 0xFF); + output[i * 4 + 1] = (unsigned char)((s->h[i] >> 16) & 0xFF); + output[i * 4 + 2] = (unsigned char)((s->h[i] >> 8) & 0xFF); + output[i * 4 + 3] = (unsigned char)((s->h[i]) & 0xFF); } } -static void SHA_Simple(void *p, int len, unsigned char *output) +void SHA_Simple(void *p, int len, unsigned char *output) { SHA_State s; @@ -231,7 +222,7 @@ random_state *random_init(char *seed, int len) unsigned long random_bits(random_state *state, int bits) { - int ret = 0; + unsigned long ret = 0; int n; for (n = 0; n < bits; n += 8) { @@ -251,7 +242,13 @@ unsigned long random_bits(random_state *state, int bits) ret = (ret << 8) | state->databuf[state->pos++]; } - ret &= (1 << bits) - 1; + /* + * `(1 << bits) - 1' is not good enough, since if bits==32 on a + * 32-bit machine, behaviour is undefined and Intel has a nasty + * habit of shifting left by zero instead. We'll shift by + * bits-1 and then separately shift by one. + */ + ret &= (1 << (bits-1)) * 2 - 1; return ret; }