Tests at compile time instead of runtime
This commit is contained in:
parent
b21a7b5529
commit
da12ae4e94
26
sha-256.c
26
sha-256.c
|
@ -93,30 +93,31 @@ static int calc_chunk(uint8_t chunk[CHUNK_SIZE], struct buffer_state * state)
|
||||||
memset(chunk, 0x00, left);
|
memset(chunk, 0x00, left);
|
||||||
chunk += left;
|
chunk += left;
|
||||||
|
|
||||||
if (sizeof len > 4) {
|
#if SIZE_MAX > UINT32_MAX
|
||||||
chunk[0] = (uint8_t) (len >> 56);
|
chunk[0] = (uint8_t) (len >> 56);
|
||||||
chunk[1] = (uint8_t) (len >> 48);
|
chunk[1] = (uint8_t) (len >> 48);
|
||||||
chunk[2] = (uint8_t) (len >> 40);
|
chunk[2] = (uint8_t) (len >> 40);
|
||||||
chunk[3] = (uint8_t) (len >> 32);
|
chunk[3] = (uint8_t) (len >> 32);
|
||||||
} else {
|
#else
|
||||||
chunk[0] = 0;
|
chunk[0] = 0;
|
||||||
chunk[1] = 0;
|
chunk[1] = 0;
|
||||||
chunk[2] = 0;
|
chunk[2] = 0;
|
||||||
chunk[3] = 0;
|
chunk[3] = 0;
|
||||||
}
|
#endif
|
||||||
if (sizeof len > 2) {
|
|
||||||
|
#if SIZE_MAX > UINT16_MAX
|
||||||
chunk[4] = (uint8_t) (len >> 24);
|
chunk[4] = (uint8_t) (len >> 24);
|
||||||
chunk[5] = (uint8_t) (len >> 16);
|
chunk[5] = (uint8_t) (len >> 16);
|
||||||
} else {
|
#else
|
||||||
chunk[4] = 0;
|
chunk[4] = 0;
|
||||||
chunk[5] = 0;
|
chunk[5] = 0;
|
||||||
}
|
#endif
|
||||||
|
|
||||||
if (sizeof len > 1) {
|
#if SIZE_MAX > UINT8_MAX
|
||||||
chunk[6] = (uint8_t) (len >> 8);
|
chunk[6] = (uint8_t) (len >> 8);
|
||||||
} else {
|
#else
|
||||||
chunk[6] = 0;
|
chunk[6] = 0;
|
||||||
}
|
#endif
|
||||||
|
|
||||||
chunk[7] = (uint8_t) len;
|
chunk[7] = (uint8_t) len;
|
||||||
|
|
||||||
|
@ -181,16 +182,17 @@ 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 (int i = 0; i < 16; i++) {
|
for (i = 0; i < 16; i++) {
|
||||||
w[i] = (uint32_t) p[0] << 24 | (uint32_t) p[1] << 16 |
|
w[i] = (uint32_t) p[0] << 24 | (uint32_t) p[1] << 16 |
|
||||||
(uint32_t) p[2] << 8 | (uint32_t) p[3];
|
(uint32_t) p[2] << 8 | (uint32_t) p[3];
|
||||||
p += 4;
|
p += 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array: */
|
/* Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array: */
|
||||||
for (int i = 16; i < 64; i++) {
|
for (i = 16; i < 64; i++) {
|
||||||
const uint32_t s0 = right_rot(w[i - 15], 7) ^ right_rot(w[i - 15], 18) ^ (w[i - 15] >> 3);
|
const uint32_t s0 = right_rot(w[i - 15], 7) ^ right_rot(w[i - 15], 18) ^ (w[i - 15] >> 3);
|
||||||
const uint32_t s1 = right_rot(w[i - 2], 17) ^ right_rot(w[i - 2], 19) ^ (w[i - 2] >> 10);
|
const uint32_t s1 = right_rot(w[i - 2], 17) ^ right_rot(w[i - 2], 19) ^ (w[i - 2] >> 10);
|
||||||
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
|
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
|
||||||
|
@ -207,7 +209,7 @@ void calc_sha_256(uint8_t hash[32], const void * input, size_t len)
|
||||||
h = h7;
|
h = h7;
|
||||||
|
|
||||||
/* Compression function main loop: */
|
/* Compression function main loop: */
|
||||||
for (int 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(e, 6) ^ right_rot(e, 11) ^ right_rot(e, 25);
|
||||||
const uint32_t ch = (e & f) ^ (~e & g);
|
const uint32_t ch = (e & f) ^ (~e & g);
|
||||||
const uint32_t temp1 = h + s1 + ch + k[i] + w[i];
|
const uint32_t temp1 = h + s1 + ch + k[i] + w[i];
|
||||||
|
|
34
test.c
34
test.c
|
@ -35,15 +35,40 @@ static const struct vector VECTORS[] = {
|
||||||
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
|
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoi"
|
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"
|
||||||
"jklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
|
"ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
|
||||||
"cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1"
|
"cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"
|
||||||
|
"ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstuabcdefghbcdefghi"
|
||||||
|
"cdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopq"
|
||||||
|
"klmnopqrlmnopqrsmnopqrstnopqrstuabcdefghbcdefghicdefghijdefghijk"
|
||||||
|
"efghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrs"
|
||||||
|
"mnopqrstnopqrstuabcdefghbcdefghicdefghijdefghijkefghijklfghijklm"
|
||||||
|
"ghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
|
||||||
|
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"
|
||||||
|
"ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstuabcdefghbcdefghi"
|
||||||
|
"cdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopq"
|
||||||
|
"klmnopqrlmnopqrsmnopqrstnopqrstuabcdefghbcdefghicdefghijdefghijk"
|
||||||
|
"efghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrs"
|
||||||
|
"mnopqrstnopqrstuabcdefghbcdefghicdefghijdefghijkefghijklfghijklm"
|
||||||
|
"ghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
|
||||||
|
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"
|
||||||
|
"ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstuabcdefghbcdefghi"
|
||||||
|
"cdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopq"
|
||||||
|
"klmnopqrlmnopqrsmnopqrstnopqrstuabcdefghbcdefghicdefghijdefghijk"
|
||||||
|
"efghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrs"
|
||||||
|
"mnopqrstnopqrstuabcdefghbcdefghicdefghijdefghijkefghijklfghijklm"
|
||||||
|
"ghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
|
||||||
|
"c7273f978615fc09e6a567bb28586fa30b76a2c915ffcdc728d3414fceb63493"
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
static void hash_to_string(char string[65], const uint8_t hash[32])
|
static void hash_to_string(char string[65], const uint8_t hash[32])
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < 32; i++) {
|
size_t i;
|
||||||
|
for (i = 0; i < 32; i++) {
|
||||||
string += sprintf(string, "%02x", hash[i]);
|
string += sprintf(string, "%02x", hash[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,7 +91,8 @@ static void test(const char input[], const char output[])
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < (sizeof VECTORS / sizeof (struct vector)); i++) {
|
size_t i;
|
||||||
|
for (i = 0; i < (sizeof VECTORS / sizeof (struct vector)); i++) {
|
||||||
const struct vector *vector = &VECTORS[i];
|
const struct vector *vector = &VECTORS[i];
|
||||||
test(vector->input, vector->output);
|
test(vector->input, vector->output);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue