|
| 1 | +/* |
| 2 | + This is a test vector suite for LekKit' SHA256 library |
| 3 | + (https://github.com/LekKit/sha256) |
| 4 | + |
| 5 | + Used sources: |
| 6 | + https://www.di-mgt.com.au/sha_testvectors.html |
| 7 | + https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA2_Additional.pdf |
| 8 | +*/ |
| 9 | + |
| 10 | +#include "sha256.h" |
| 11 | +#include <stdio.h> |
| 12 | +#include <stdlib.h> |
| 13 | + |
| 14 | +void test_str(const char* in, const char* out) { |
| 15 | + static size_t test_count = 0; |
| 16 | + |
| 17 | + char buffer[65] = {0}; |
| 18 | + sha256_easy_hash_hex(in, strlen(in), buffer); |
| 19 | + if (strcmp(out, buffer) != 0) { |
| 20 | + printf("String test #%i failed!!!\nsha256(\"%s\") = \"%s\"\nExpected value: \"%s\"\n\n", test_count, in, buffer, out); |
| 21 | + printf("Please report this issue to https://github.com/LekKit/sha256\n"); |
| 22 | + exit(0); |
| 23 | + } else { |
| 24 | + printf("String test #%i passed\nsha256(\"%s\") = \"%s\"\n\n", test_count, in, buffer); |
| 25 | + } |
| 26 | + |
| 27 | + test_count++; |
| 28 | +} |
| 29 | + |
| 30 | +void test_bytes(char byte, size_t size, const char* out) { |
| 31 | + static size_t test_count = 0; |
| 32 | + |
| 33 | + char buffer[1024]; |
| 34 | + struct sha256_buff buff; |
| 35 | + size_t tmp_size = size; |
| 36 | + memset(buffer, byte, 1024); |
| 37 | + sha256_init(&buff); |
| 38 | + |
| 39 | + while (tmp_size >= 1024) { |
| 40 | + sha256_update(&buff, buffer, 1024); |
| 41 | + tmp_size -= 1024; |
| 42 | + } |
| 43 | + sha256_update(&buff, buffer, size % 1024); |
| 44 | + sha256_finalize(&buff); |
| 45 | + |
| 46 | + sha256_read_hex(&buff, buffer); |
| 47 | + buffer[64] = 0; |
| 48 | + |
| 49 | + if (strcmp(out, buffer) != 0) { |
| 50 | + printf("Byte test #%i failed!!!\nsha256(0x%X * %u) = \"%s\"\nExpected value: \"%s\"\n\n", test_count, byte, size, buffer, out); |
| 51 | + printf("Please report this issue to https://github.com/LekKit/sha256\n"); |
| 52 | + exit(0); |
| 53 | + } else { |
| 54 | + printf("Byte test #%i passed\nsha256(0x%X * %u) = \"%s\"\n\n", test_count, byte, size, buffer); |
| 55 | + } |
| 56 | + |
| 57 | + test_count++; |
| 58 | +} |
| 59 | + |
| 60 | +int main() { |
| 61 | + test_str("", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); |
| 62 | + test_str("abc", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); |
| 63 | + test_str("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"); |
| 64 | + test_str("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1"); |
| 65 | + test_str("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno", "2ff100b36c386c65a1afc462ad53e25479bec9498ed00aa5a04de584bc25301b"); |
| 66 | + test_str("LekKit", "500f94082c97ab2c1188e53d9f467a9e73bfb366e7309adbfac098f0a46d7711"); |
| 67 | + test_str("\xBD", "68325720aabd7c82f30f554b313d0570c95accbb7dc4b5aae11204c08ffe732b"); |
| 68 | + test_str("\xC9\x8C\x8E\x55", "7abc22c0ae5af26ce93dbb94433a0e0b2e119d014f8e7f65bd56c61ccccd9504"); |
| 69 | + |
| 70 | + test_bytes(0x00, 55, "02779466cdec163811d078815c633f21901413081449002f24aa3e80f0b88ef7"); |
| 71 | + test_bytes(0x00, 56, "d4817aa5497628e7c77e6b606107042bbba3130888c5f47a375e6179be789fbb"); |
| 72 | + test_bytes(0x00, 57, "65a16cb7861335d5ace3c60718b5052e44660726da4cd13bb745381b235a1785"); |
| 73 | + test_bytes(0x00, 64, "f5a5fd42d16a20302798ef6ed309979b43003d2320d9f0e8ea9831a92759fb4b"); |
| 74 | + test_bytes(0x00, 1000, "541b3e9daa09b20bf85fa273e5cbd3e80185aa4ec298e765db87742b70138a53"); |
| 75 | + test_bytes(0x41, 1000, "c2e686823489ced2017f6059b8b239318b6364f6dcd835d0a519105a1eadd6e4"); |
| 76 | + test_bytes(0x55, 1005, "f4d62ddec0f3dd90ea1380fa16a5ff8dc4c54b21740650f24afc4120903552b0"); |
| 77 | + test_bytes(0x00, 1000000, "d29751f2649b32ff572b5e0a9f541ea660a50f94ff0beedfb0b692b924cc8025"); |
| 78 | + test_bytes(0x61, 1000000, "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"); |
| 79 | + test_bytes(0x5A, 536870912, "15a1868c12cc53951e182344277447cd0979536badcc512ad24c67e9b2d4f3dd"); |
| 80 | + test_bytes(0x00, 1090519040, "461c19a93bd4344f9215f5ec64357090342bc66b15a148317d276e31cbc20b53"); |
| 81 | + test_bytes(0x42, 1610612798, "c23ce8a7895f4b21ec0daf37920ac0a262a220045a03eb2dfed48ef9b05aabea"); |
| 82 | + |
| 83 | + printf("All tests passed! Fine then ;)\n"); |
| 84 | + |
| 85 | + return 0; |
| 86 | +} |
0 commit comments