Made changes according to review comments from StackExchange CODE REVIEW
This commit is contained in:
parent
d7214be727
commit
cba84edcb5
154
sha-256.c
154
sha-256.c
|
@ -6,6 +6,10 @@
|
||||||
#define CHUNK_SIZE 64
|
#define CHUNK_SIZE 64
|
||||||
#define TOTAL_LEN_LEN 8
|
#define TOTAL_LEN_LEN 8
|
||||||
|
|
||||||
|
/*
|
||||||
|
* ABOUT bool: this file does not use bool in order to be as pre-C99 compatible as possible.
|
||||||
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Comments from pseudo-code at https://en.wikipedia.org/wiki/SHA-2 are reproduced here.
|
* Comments from pseudo-code at https://en.wikipedia.org/wiki/SHA-2 are reproduced here.
|
||||||
* When useful for clarification, portions of the pseudo-code are reproduced here too.
|
* When useful for clarification, portions of the pseudo-code are reproduced here too.
|
||||||
|
@ -30,8 +34,8 @@ struct buffer_state {
|
||||||
const uint8_t * p;
|
const uint8_t * p;
|
||||||
size_t len;
|
size_t len;
|
||||||
size_t total_len;
|
size_t total_len;
|
||||||
int single_one_delivered;
|
int single_one_delivered; // bool
|
||||||
int total_len_delivered;
|
int total_len_delivered; // bool
|
||||||
};
|
};
|
||||||
|
|
||||||
static inline uint32_t right_rot(uint32_t value, unsigned int count)
|
static inline uint32_t right_rot(uint32_t value, unsigned int count)
|
||||||
|
@ -52,6 +56,7 @@ static void init_buf_state(struct buffer_state * state, const void * input, size
|
||||||
state->total_len_delivered = 0;
|
state->total_len_delivered = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Return value: bool */
|
||||||
static int calc_chunk(uint8_t chunk[CHUNK_SIZE], struct buffer_state * state)
|
static int calc_chunk(uint8_t chunk[CHUNK_SIZE], struct buffer_state * state)
|
||||||
{
|
{
|
||||||
size_t space_in_chunk;
|
size_t space_in_chunk;
|
||||||
|
@ -65,7 +70,6 @@ static int calc_chunk(uint8_t chunk[CHUNK_SIZE], struct buffer_state * state)
|
||||||
state->p += CHUNK_SIZE;
|
state->p += CHUNK_SIZE;
|
||||||
state->len -= CHUNK_SIZE;
|
state->len -= CHUNK_SIZE;
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(chunk, state->p, state->len);
|
memcpy(chunk, state->p, state->len);
|
||||||
|
@ -89,38 +93,18 @@ static int calc_chunk(uint8_t chunk[CHUNK_SIZE], struct buffer_state * state)
|
||||||
*/
|
*/
|
||||||
if (space_in_chunk >= TOTAL_LEN_LEN) {
|
if (space_in_chunk >= TOTAL_LEN_LEN) {
|
||||||
const size_t left = space_in_chunk - TOTAL_LEN_LEN;
|
const size_t left = space_in_chunk - TOTAL_LEN_LEN;
|
||||||
const size_t len = state->total_len * 8;
|
size_t len = state->total_len;
|
||||||
|
int i;
|
||||||
memset(chunk, 0x00, left);
|
memset(chunk, 0x00, left);
|
||||||
chunk += left;
|
chunk += left;
|
||||||
|
|
||||||
#if SIZE_MAX > UINT32_MAX
|
/* Storing of len * 8 as a big endian 64-bit without overflow. */
|
||||||
chunk[0] = (uint8_t) (len >> 56);
|
chunk[7] = (uint8_t) (len << 3);
|
||||||
chunk[1] = (uint8_t) (len >> 48);
|
len >>= 5;
|
||||||
chunk[2] = (uint8_t) (len >> 40);
|
for (i = 6; i >= 0; i--) {
|
||||||
chunk[3] = (uint8_t) (len >> 32);
|
chunk[i] = (uint8_t) len;
|
||||||
#else
|
len >>= 8;
|
||||||
chunk[0] = 0;
|
}
|
||||||
chunk[1] = 0;
|
|
||||||
chunk[2] = 0;
|
|
||||||
chunk[3] = 0;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if SIZE_MAX > UINT16_MAX
|
|
||||||
chunk[4] = (uint8_t) (len >> 24);
|
|
||||||
chunk[5] = (uint8_t) (len >> 16);
|
|
||||||
#else
|
|
||||||
chunk[4] = 0;
|
|
||||||
chunk[5] = 0;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if SIZE_MAX > UINT8_MAX
|
|
||||||
chunk[6] = (uint8_t) (len >> 8);
|
|
||||||
#else
|
|
||||||
chunk[6] = 0;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
chunk[7] = (uint8_t) len;
|
|
||||||
|
|
||||||
state->total_len_delivered = 1;
|
state->total_len_delivered = 1;
|
||||||
} else {
|
} else {
|
||||||
memset(chunk, 0x00, space_in_chunk);
|
memset(chunk, 0x00, space_in_chunk);
|
||||||
|
@ -131,18 +115,16 @@ static int calc_chunk(uint8_t chunk[CHUNK_SIZE], struct buffer_state * state)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Limitations:
|
* Limitations:
|
||||||
* - len must be small enough for (8 * len) to fit in len. Otherwise, the results are unpredictable.
|
|
||||||
* - sizeof size_t is assumed to be either 8, 16, 32 or 64. Otherwise, the results are unpredictable.
|
|
||||||
* - Since input is a pointer in RAM, the data to hash should be in RAM, which could be a problem
|
* - Since input is a pointer in RAM, the data to hash should be in RAM, which could be a problem
|
||||||
* for large data sizes.
|
* for large data sizes.
|
||||||
* - SHA algorithms theoretically operate on bit strings. However, this implementation has no support
|
* - SHA algorithms theoretically operate on bit strings. However, this implementation has no support
|
||||||
* for bit string lengths that are not multiples of eight, and it really operates on arrays of bytes.
|
* for bit string lengths that are not multiples of eight, and it really operates on arrays of bytes.
|
||||||
* the len parameter is a number of bytes.
|
* In particular, the len parameter is a number of bytes.
|
||||||
*/
|
*/
|
||||||
void calc_sha_256(uint8_t hash[32], const void * input, size_t len)
|
void calc_sha_256(uint8_t hash[32], const void * input, size_t len)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
* Note 1: All variables are 32 bit unsigned integers and addition is calculated modulo 232
|
* Note 1: All integers (expect indexes) are 32-bit unsigned integers and addition is calculated modulo 2^32.
|
||||||
* Note 2: For each round, there is one round constant k[i] and one entry in the message schedule array w[i], 0 = i = 63
|
* Note 2: For each round, there is one round constant k[i] and one entry in the message schedule array w[i], 0 = i = 63
|
||||||
* Note 3: The compression function uses 8 working variables, a through h
|
* Note 3: The compression function uses 8 working variables, a through h
|
||||||
* Note 4: Big-endian convention is used when expressing the constants in this pseudocode,
|
* Note 4: Big-endian convention is used when expressing the constants in this pseudocode,
|
||||||
|
@ -154,18 +136,10 @@ void calc_sha_256(uint8_t hash[32], const void * input, size_t len)
|
||||||
* Initialize hash values:
|
* Initialize hash values:
|
||||||
* (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
|
* (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
|
||||||
*/
|
*/
|
||||||
|
uint32_t h[] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
|
||||||
uint32_t h0 = 0x6a09e667;
|
int i, j;
|
||||||
uint32_t h1 = 0xbb67ae85;
|
|
||||||
uint32_t h2 = 0x3c6ef372;
|
|
||||||
uint32_t h3 = 0xa54ff53a;
|
|
||||||
uint32_t h4 = 0x510e527f;
|
|
||||||
uint32_t h5 = 0x9b05688c;
|
|
||||||
uint32_t h6 = 0x1f83d9ab;
|
|
||||||
uint32_t h7 = 0x5be0cd19;
|
|
||||||
|
|
||||||
/* 512-bit chunks is what we will operate on. */
|
/* 512-bit chunks is what we will operate on. */
|
||||||
|
|
||||||
uint8_t chunk[64];
|
uint8_t chunk[64];
|
||||||
|
|
||||||
struct buffer_state state;
|
struct buffer_state state;
|
||||||
|
@ -173,7 +147,7 @@ void calc_sha_256(uint8_t hash[32], const void * input, size_t len)
|
||||||
init_buf_state(&state, input, len);
|
init_buf_state(&state, input, len);
|
||||||
|
|
||||||
while (calc_chunk(chunk, &state)) {
|
while (calc_chunk(chunk, &state)) {
|
||||||
uint32_t a, b, c, d, e, f, g, h;
|
uint32_t ah[8];
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* create a 64-entry message schedule array w[0..63] of 32-bit words
|
* create a 64-entry message schedule array w[0..63] of 32-bit words
|
||||||
|
@ -182,7 +156,6 @@ void calc_sha_256(uint8_t hash[32], const void * input, size_t len)
|
||||||
*/
|
*/
|
||||||
uint32_t w[64];
|
uint32_t w[64];
|
||||||
const uint8_t *p = chunk;
|
const uint8_t *p = chunk;
|
||||||
int i;
|
|
||||||
|
|
||||||
memset(w, 0x00, sizeof w);
|
memset(w, 0x00, sizeof w);
|
||||||
for (i = 0; i < 16; i++) {
|
for (i = 0; i < 16; i++) {
|
||||||
|
@ -199,76 +172,39 @@ void calc_sha_256(uint8_t hash[32], const void * input, size_t len)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initialize working variables to current hash value: */
|
/* Initialize working variables to current hash value: */
|
||||||
a = h0;
|
for (i = 0; i < 8; i++)
|
||||||
b = h1;
|
ah[i] = h[i];
|
||||||
c = h2;
|
|
||||||
d = h3;
|
|
||||||
e = h4;
|
|
||||||
f = h5;
|
|
||||||
g = h6;
|
|
||||||
h = h7;
|
|
||||||
|
|
||||||
/* Compression function main loop: */
|
/* Compression function main loop: */
|
||||||
for (i = 0; i < 64; i++) {
|
for (i = 0; i < 64; i++) {
|
||||||
const uint32_t s1 = right_rot(e, 6) ^ right_rot(e, 11) ^ right_rot(e, 25);
|
const uint32_t s1 = right_rot(ah[4], 6) ^ right_rot(ah[4], 11) ^ right_rot(ah[4], 25);
|
||||||
const uint32_t ch = (e & f) ^ (~e & g);
|
const uint32_t ch = (ah[4] & ah[5]) ^ (~ah[4] & ah[6]);
|
||||||
const uint32_t temp1 = h + s1 + ch + k[i] + w[i];
|
const uint32_t temp1 = ah[7] + s1 + ch + k[i] + w[i];
|
||||||
const uint32_t s0 = right_rot(a, 2) ^ right_rot(a, 13) ^ right_rot(a, 22);
|
const uint32_t s0 = right_rot(ah[0], 2) ^ right_rot(ah[0], 13) ^ right_rot(ah[0], 22);
|
||||||
const uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
|
const uint32_t maj = (ah[0] & ah[1]) ^ (ah[0] & ah[2]) ^ (ah[1] & ah[2]);
|
||||||
const uint32_t temp2 = s0 + maj;
|
const uint32_t temp2 = s0 + maj;
|
||||||
|
|
||||||
h = g;
|
ah[7] = ah[6];
|
||||||
g = f;
|
ah[6] = ah[5];
|
||||||
f = e;
|
ah[5] = ah[4];
|
||||||
e = d + temp1;
|
ah[4] = ah[3] + temp1;
|
||||||
d = c;
|
ah[3] = ah[2];
|
||||||
c = b;
|
ah[2] = ah[1];
|
||||||
b = a;
|
ah[1] = ah[0];
|
||||||
a = temp1 + temp2;
|
ah[0] = temp1 + temp2;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add the compressed chunk to the current hash value: */
|
/* Add the compressed chunk to the current hash value: */
|
||||||
h0 = h0 + a;
|
for (i = 0; i < 8; i++)
|
||||||
h1 = h1 + b;
|
h[i] += ah[i];
|
||||||
h2 = h2 + c;
|
|
||||||
h3 = h3 + d;
|
|
||||||
h4 = h4 + e;
|
|
||||||
h5 = h5 + f;
|
|
||||||
h6 = h6 + g;
|
|
||||||
h7 = h7 + h;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Produce the final hash value (big-endian): */
|
/* Produce the final hash value (big-endian): */
|
||||||
hash[0] = (uint8_t) (h0 >> 24);
|
for (i = 0, j = 0; i < 8; i++)
|
||||||
hash[1] = (uint8_t) (h0 >> 16);
|
{
|
||||||
hash[2] = (uint8_t) (h0 >> 8);
|
hash[j++] = (uint8_t) (h[i] >> 24);
|
||||||
hash[3] = (uint8_t) h0;
|
hash[j++] = (uint8_t) (h[i] >> 16);
|
||||||
hash[4] = (uint8_t) (h1 >> 24);
|
hash[j++] = (uint8_t) (h[i] >> 8);
|
||||||
hash[5] = (uint8_t) (h1 >> 16);
|
hash[j++] = (uint8_t) h[i];
|
||||||
hash[6] = (uint8_t) (h1 >> 8);
|
}
|
||||||
hash[7] = (uint8_t) h1;
|
|
||||||
hash[8] = (uint8_t) (h2 >> 24);
|
|
||||||
hash[9] = (uint8_t) (h2 >> 16);
|
|
||||||
hash[10] = (uint8_t) (h2 >> 8);
|
|
||||||
hash[11] = (uint8_t) h2;
|
|
||||||
hash[12] = (uint8_t) (h3 >> 24);
|
|
||||||
hash[13] = (uint8_t) (h3 >> 16);
|
|
||||||
hash[14] = (uint8_t) (h3 >> 8);
|
|
||||||
hash[15] = (uint8_t) h3;
|
|
||||||
hash[16] = (uint8_t) (h4 >> 24);
|
|
||||||
hash[17] = (uint8_t) (h4 >> 16);
|
|
||||||
hash[18] = (uint8_t) (h4 >> 8);
|
|
||||||
hash[19] = (uint8_t) h4;
|
|
||||||
hash[20] = (uint8_t) (h5 >> 24);
|
|
||||||
hash[21] = (uint8_t) (h5 >> 16);
|
|
||||||
hash[22] = (uint8_t) (h5 >> 8);
|
|
||||||
hash[23] = (uint8_t) h5;
|
|
||||||
hash[24] = (uint8_t) (h6 >> 24);
|
|
||||||
hash[25] = (uint8_t) (h6 >> 16);
|
|
||||||
hash[26] = (uint8_t) (h6 >> 8);
|
|
||||||
hash[27] = (uint8_t) h6;
|
|
||||||
hash[28] = (uint8_t) (h7 >> 24);
|
|
||||||
hash[29] = (uint8_t) (h7 >> 16);
|
|
||||||
hash[30] = (uint8_t) (h7 >> 8);
|
|
||||||
hash[31] = (uint8_t) h7;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue