Tests at compile time instead of runtime

This commit is contained in:
Alain Mosnier 2017-12-14 20:11:09 +01:00
parent b21a7b5529
commit da12ae4e94
2 changed files with 58 additions and 30 deletions

View File

@ -93,30 +93,31 @@ static int calc_chunk(uint8_t chunk[CHUNK_SIZE], struct buffer_state * state)
memset(chunk, 0x00, left);
chunk += left;
if (sizeof len > 4) {
#if SIZE_MAX > UINT32_MAX
chunk[0] = (uint8_t) (len >> 56);
chunk[1] = (uint8_t) (len >> 48);
chunk[2] = (uint8_t) (len >> 40);
chunk[3] = (uint8_t) (len >> 32);
} else {
#else
chunk[0] = 0;
chunk[1] = 0;
chunk[2] = 0;
chunk[3] = 0;
}
if (sizeof len > 2) {
#endif
#if SIZE_MAX > UINT16_MAX
chunk[4] = (uint8_t) (len >> 24);
chunk[5] = (uint8_t) (len >> 16);
} else {
#else
chunk[4] = 0;
chunk[5] = 0;
}
#endif
if (sizeof len > 1) {
#if SIZE_MAX > UINT8_MAX
chunk[6] = (uint8_t) (len >> 8);
} else {
#else
chunk[6] = 0;
}
#endif
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];
const uint8_t *p = chunk;
int i;
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 |
(uint32_t) p[2] << 8 | (uint32_t) p[3];
p += 4;
}
/* 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 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;
@ -207,7 +209,7 @@ void calc_sha_256(uint8_t hash[32], const void * input, size_t len)
h = h7;
/* 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 ch = (e & f) ^ (~e & g);
const uint32_t temp1 = h + s1 + ch + k[i] + w[i];

34
test.c
View File

@ -35,15 +35,40 @@ static const struct vector VECTORS[] = {
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
},
{
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoi"
"jklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"
"ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
"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])
{
for (size_t i = 0; i < 32; i++) {
size_t i;
for (i = 0; i < 32; i++) {
string += sprintf(string, "%02x", hash[i]);
}
}
@ -66,7 +91,8 @@ static void test(const char input[], const char output[])
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];
test(vector->input, vector->output);
}