2017-12-12 23:52:28 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
2017-12-13 10:12:47 +00:00
|
|
|
#include <string.h>
|
2017-12-12 23:52:28 +00:00
|
|
|
|
|
|
|
#include "sha-256.h"
|
|
|
|
|
2017-12-15 20:34:20 +00:00
|
|
|
struct string_vector {
|
2017-12-13 10:12:47 +00:00
|
|
|
const char *input;
|
|
|
|
const char *output;
|
|
|
|
};
|
|
|
|
|
2017-12-15 20:34:20 +00:00
|
|
|
static const struct string_vector STRING_VECTORS[] = {
|
2017-12-13 10:12:47 +00:00
|
|
|
{
|
|
|
|
"",
|
|
|
|
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"abc",
|
|
|
|
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
|
|
|
|
"a8ae6e6ee929abea3afcfc5258c8ccd6f85273e0d4626d26c7279f3250f77c8e"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde",
|
|
|
|
"057ee79ece0b9a849552ab8d3c335fe9a5f1c46ef5f1d9b190c295728628299c"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0",
|
|
|
|
"2a6ad82f3620d3ebe9d678c812ae12312699d673240d5be8fac0910a70000d93"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
|
|
|
|
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
|
|
|
|
},
|
|
|
|
{
|
2017-12-14 19:11:09 +00:00
|
|
|
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"
|
|
|
|
"ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
|
2017-12-13 10:12:47 +00:00
|
|
|
"cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-15 20:34:20 +00:00
|
|
|
#define LARGE_MESSAGES 0
|
|
|
|
|
|
|
|
static uint8_t data1[] = { 0xbd };
|
|
|
|
static uint8_t data2[] = { 0xc9, 0x8c, 0x8e, 0x55 };
|
|
|
|
static uint8_t data7[1000];
|
|
|
|
static uint8_t data8[1000];
|
|
|
|
static uint8_t data9[1005];
|
|
|
|
#if LARGE_MESSAGES
|
|
|
|
static uint8_t data11[536870912];
|
|
|
|
static uint8_t data12[1090519040];
|
|
|
|
static uint8_t data13[1610612798];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void construct_binary_messages(void)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < sizeof data7; i++)
|
|
|
|
data7[i] = 0;
|
|
|
|
for (i = 0; i < sizeof data8; i++)
|
|
|
|
data8[i] = 0x41;
|
|
|
|
for (i = 0; i < sizeof data9; i++)
|
|
|
|
data9[i] = 0x55;
|
|
|
|
#if LARGE_MESSAGES
|
|
|
|
for (i = 0; i < sizeof data11; i++)
|
|
|
|
data11[i] = 0x5a;
|
|
|
|
for (i = 0; i < sizeof data12; i++)
|
|
|
|
data12[i] = 0;
|
|
|
|
for (i = 0; i < sizeof data13; i++)
|
|
|
|
data13[i] = 0x42;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
struct vector {
|
|
|
|
const uint8_t *input;
|
|
|
|
size_t input_len;
|
|
|
|
const char *output;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct vector VECTORS[] = {
|
|
|
|
{
|
|
|
|
data1,
|
|
|
|
sizeof data1,
|
|
|
|
"68325720aabd7c82f30f554b313d0570c95accbb7dc4b5aae11204c08ffe732b"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
data2,
|
|
|
|
sizeof data2,
|
|
|
|
"7abc22c0ae5af26ce93dbb94433a0e0b2e119d014f8e7f65bd56c61ccccd9504"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
data7,
|
|
|
|
55,
|
|
|
|
"02779466cdec163811d078815c633f21901413081449002f24aa3e80f0b88ef7"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
data7,
|
|
|
|
56,
|
|
|
|
"d4817aa5497628e7c77e6b606107042bbba3130888c5f47a375e6179be789fbb"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
data7,
|
|
|
|
57,
|
|
|
|
"65a16cb7861335d5ace3c60718b5052e44660726da4cd13bb745381b235a1785"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
data7,
|
|
|
|
64,
|
|
|
|
"f5a5fd42d16a20302798ef6ed309979b43003d2320d9f0e8ea9831a92759fb4b"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
data7,
|
|
|
|
sizeof data7,
|
|
|
|
"541b3e9daa09b20bf85fa273e5cbd3e80185aa4ec298e765db87742b70138a53"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
data8,
|
|
|
|
sizeof data8,
|
|
|
|
"c2e686823489ced2017f6059b8b239318b6364f6dcd835d0a519105a1eadd6e4"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
data9,
|
|
|
|
sizeof data9,
|
|
|
|
"f4d62ddec0f3dd90ea1380fa16a5ff8dc4c54b21740650f24afc4120903552b0"
|
|
|
|
}
|
|
|
|
#if LARGE_MESSAGES
|
|
|
|
,
|
|
|
|
{
|
|
|
|
data12,
|
|
|
|
1000000,
|
|
|
|
"d29751f2649b32ff572b5e0a9f541ea660a50f94ff0beedfb0b692b924cc8025"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
data11,
|
|
|
|
sizeof data11,
|
|
|
|
"15a1868c12cc53951e182344277447cd0979536badcc512ad24c67e9b2d4f3dd"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
data12,
|
|
|
|
sizeof data12,
|
|
|
|
"461c19a93bd4344f9215f5ec64357090342bc66b15a148317d276e31cbc20b53"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
data13,
|
|
|
|
sizeof data13,
|
|
|
|
"c23ce8a7895f4b21ec0daf37920ac0a262a220045a03eb2dfed48ef9b05aabea"
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2017-12-13 10:12:47 +00:00
|
|
|
static void hash_to_string(char string[65], const uint8_t hash[32])
|
|
|
|
{
|
2017-12-14 19:11:09 +00:00
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < 32; i++) {
|
2017-12-13 10:12:47 +00:00
|
|
|
string += sprintf(string, "%02x", hash[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-15 21:53:19 +00:00
|
|
|
static int string_test(const char input[], const char output[])
|
2017-12-12 23:52:28 +00:00
|
|
|
{
|
|
|
|
uint8_t hash[32];
|
2017-12-13 10:12:47 +00:00
|
|
|
char hash_string[65];
|
|
|
|
calc_sha_256(hash, input, strlen(input));
|
|
|
|
hash_to_string(hash_string, hash);
|
|
|
|
printf("input: %s\n", input);
|
|
|
|
printf("hash : %s\n", hash_string);
|
|
|
|
if (strcmp(output, hash_string)) {
|
|
|
|
printf("FAILURE!\n\n");
|
2017-12-15 21:53:19 +00:00
|
|
|
return 1;
|
2017-12-13 10:12:47 +00:00
|
|
|
} else {
|
|
|
|
printf("SUCCESS!\n\n");
|
2017-12-15 21:53:19 +00:00
|
|
|
return 0;
|
2017-12-13 10:12:47 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-15 20:34:20 +00:00
|
|
|
|
2017-12-15 21:53:19 +00:00
|
|
|
static int test(const uint8_t * input, size_t input_len, const char output[])
|
2017-12-15 20:34:20 +00:00
|
|
|
{
|
|
|
|
uint8_t hash[32];
|
|
|
|
char hash_string[65];
|
|
|
|
calc_sha_256(hash, input, input_len);
|
|
|
|
hash_to_string(hash_string, hash);
|
|
|
|
printf("input starts with 0x%02x, length %lu\n", *input, input_len);
|
|
|
|
printf("hash : %s\n", hash_string);
|
|
|
|
if (strcmp(output, hash_string)) {
|
|
|
|
printf("FAILURE!\n\n");
|
2017-12-15 21:53:19 +00:00
|
|
|
return 1;
|
2017-12-15 20:34:20 +00:00
|
|
|
} else {
|
|
|
|
printf("SUCCESS!\n\n");
|
2017-12-15 21:53:19 +00:00
|
|
|
return 0;
|
2017-12-15 20:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-13 10:12:47 +00:00
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2017-12-14 19:11:09 +00:00
|
|
|
size_t i;
|
2017-12-15 20:34:20 +00:00
|
|
|
for (i = 0; i < (sizeof STRING_VECTORS / sizeof (struct string_vector)); i++) {
|
|
|
|
const struct string_vector *vector = &STRING_VECTORS[i];
|
2017-12-15 21:53:19 +00:00
|
|
|
if (string_test(vector->input, vector->output))
|
|
|
|
return 1;
|
2017-12-15 20:34:20 +00:00
|
|
|
}
|
|
|
|
construct_binary_messages();
|
2017-12-14 19:11:09 +00:00
|
|
|
for (i = 0; i < (sizeof VECTORS / sizeof (struct vector)); i++) {
|
2017-12-13 10:12:47 +00:00
|
|
|
const struct vector *vector = &VECTORS[i];
|
2017-12-15 21:53:19 +00:00
|
|
|
if (test(vector->input, vector->input_len, vector->output))
|
|
|
|
return 1;
|
2017-12-12 23:52:28 +00:00
|
|
|
}
|
2017-12-13 10:12:47 +00:00
|
|
|
return 0;
|
2017-12-12 23:52:28 +00:00
|
|
|
}
|