Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
sha256.c: simplify byte reverse ordering and explicitly cast from ULL to unsigned char 1. Currently implemented reverse ordering looks surprisingly complex, but we can safely remove the subtraction part if we pass the current index to the state array and increase the loop 2x times more (4 * 2). It's the same as the previous implementation, except for the loop and index covers here, so the extra calculation (i * 8) in the loop, is no longer needed.
rilysh committed 2 years ago
commit 4d261cc900fc2ccbf37237003f104dbde7607f43
parent 2c762da
1 file changed +15 -20
modified libpkg/sha256.c
@@ -18,7 +18,6 @@
#include "sha256.h"

/****************************** MACROS ******************************/
-
#define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))
#define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))

#define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
@@ -113,7 +112,7 @@ void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len)

void sha256_final(SHA256_CTX *ctx, BYTE hash[])
{
-
	WORD i;
+
	WORD i, j;

	i = ctx->datalen;

@@ -133,26 +132,22 @@ void sha256_final(SHA256_CTX *ctx, BYTE hash[])

	// Append to the padding the total message's length in bits and transform.
	ctx->bitlen += ctx->datalen * 8;
-
	ctx->data[63] = ctx->bitlen;
-
	ctx->data[62] = ctx->bitlen >> 8;
-
	ctx->data[61] = ctx->bitlen >> 16;
-
	ctx->data[60] = ctx->bitlen >> 24;
-
	ctx->data[59] = ctx->bitlen >> 32;
-
	ctx->data[58] = ctx->bitlen >> 40;
-
	ctx->data[57] = ctx->bitlen >> 48;
-
	ctx->data[56] = ctx->bitlen >> 56;
+
	ctx->data[63] = (BYTE)ctx->bitlen;
+
	ctx->data[62] = (BYTE)(ctx->bitlen >> 8);
+
	ctx->data[61] = (BYTE)(ctx->bitlen >> 16);
+
	ctx->data[60] = (BYTE)(ctx->bitlen >> 24);
+
	ctx->data[59] = (BYTE)(ctx->bitlen >> 32);
+
	ctx->data[58] = (BYTE)(ctx->bitlen >> 40);
+
	ctx->data[57] = (BYTE)(ctx->bitlen >> 48);
+
	ctx->data[56] = (BYTE)(ctx->bitlen >> 56);
	sha256_transform(ctx, ctx->data);

	// Since this implementation uses little endian byte ordering and SHA uses big endian,
	// reverse all the bytes when copying the final state to the output hash.
-
	for (i = 0; i < 4; ++i) {
-
		hash[i]      = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
-
		hash[i + 4]  = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
-
		hash[i + 8]  = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
-
		hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
-
		hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
-
		hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;
-
		hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;
-
		hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;
-
	}
+
	for (i = j = 0; i < 8; ++i, ++j) {
+
		hash[j++] = (ctx->state[i] >> 24) & 0xff;
+
	        hash[j++] = (ctx->state[i] >> 16) & 0xff;
+
	        hash[j++] = (ctx->state[i] >> 8) & 0xff;
+
	        hash[j]   = ctx->state[i] & 0xff;
+
        }
}