atlas
aien-dev/atlas
Brought by drakestapleton
atlas / test_sha256_kat.c
| 1 | #include <stdio.h> |
| 2 | #include <stdint.h> |
| 3 | #include <string.h> |
| 4 | |
| 5 | void sha256_compute(const uint8_t *data, uint64_t len, uint8_t scratch_buf[128], uint32_t out_digest[8]); |
| 6 | |
| 7 | int test_vector(const char *name, const uint8_t *data, uint64_t len, const char *expected_hex) { |
| 8 | uint8_t scratch[128]; |
| 9 | uint32_t digest[8]; |
| 10 | sha256_compute(data, len, scratch, digest); |
| 11 | |
| 12 | char computed_hex[65]; |
| 13 | for (int i = 0; i < 8; i++) { |
| 14 | sprintf(computed_hex + i * 8, "%08x", digest[i]); |
| 15 | } |
| 16 | computed_hex[64] = '\0'; |
| 17 | |
| 18 | int match = (strcmp(computed_hex, expected_hex) == 0); |
| 19 | printf(" [%s] %s\n", match ? "PASS" : "FAIL", name); |
| 20 | printf(" Computed: %s\n", computed_hex); |
| 21 | printf(" Expected: %s\n", expected_hex); |
| 22 | return match; |
| 23 | } |
| 24 | |
| 25 | int main() { |
| 26 | printf("======================================================================\n"); |
| 27 | printf("SHA-256 KNOWN-ANSWER TEST (KAT) RUNNER — NIST FIPS 180-4 VECTORS\n"); |
| 28 | printf("======================================================================\n"); |
| 29 | |
| 30 | int all_passed = 1; |
| 31 | |
| 32 | // Vector 1: Empty string "" |
| 33 | all_passed &= test_vector( |
| 34 | "NIST Vector 1 (Empty String)", |
| 35 | (const uint8_t *)"", 0, |
| 36 | "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" |
| 37 | ); |
| 38 | |
| 39 | // Vector 2: "abc" |
| 40 | all_passed &= test_vector( |
| 41 | "NIST Vector 2 (\"abc\")", |
| 42 | (const uint8_t *)"abc", 3, |
| 43 | "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" |
| 44 | ); |
| 45 | |
| 46 | // Vector 3: 56-byte multi-block boundary |
| 47 | all_passed &= test_vector( |
| 48 | "NIST Vector 3 (56-byte Boundary: \"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\")", |
| 49 | (const uint8_t *)"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56, |
| 50 | "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" |
| 51 | ); |
| 52 | |
| 53 | // Vector 4: 112 bytes (two full blocks) |
| 54 | const char *v4_str = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopqabcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; |
| 55 | all_passed &= test_vector( |
| 56 | "NIST Vector 4 (112-byte Multi-Block)", |
| 57 | (const uint8_t *)v4_str, strlen(v4_str), |
| 58 | "59f109d9533b2b70e7c3b814a2bd218f78ea5d3714455bc67987cf0d664399cf" |
| 59 | ); |
| 60 | |
| 61 | // Vector 5: 256-byte Physics-0 payload |
| 62 | uint8_t payload[256]; |
| 63 | FILE *f = fopen("physics-0.bin", "rb"); |
| 64 | if (f) { |
| 65 | size_t n = fread(payload, 1, 256, f); |
| 66 | (void)n; |
| 67 | fclose(f); |
| 68 | all_passed &= test_vector( |
| 69 | "PHYSICS-0 Canonical Staging Payload (256 bytes)", |
| 70 | payload, 256, |
| 71 | "e1d89bb1e0854ebaccd2be5c756c8a2ff8c5ecc8c0f90b4abd461fb7bf98374c" |
| 72 | ); |
| 73 | } else { |
| 74 | printf(" [WARN] physics-0.bin not found on disk\n"); |
| 75 | } |
| 76 | |
| 77 | printf("----------------------------------------------------------------------\n"); |
| 78 | printf("KAT RESULT: %s\n", all_passed ? "ALL NIST VECTORS PASSED" : "FAILURES DETECTED"); |
| 79 | printf("======================================================================\n"); |
| 80 | return all_passed ? 0 : 1; |
| 81 | } |