| 1 | /* tests/sha256_vectors.c - known-answer tests for src/sha256.c. |
| 2 | * |
| 3 | * The plugin signs webhook bodies with HMAC-SHA256, so "our HMAC matches |
| 4 | * Python's hmac" is not enough on its own: both could agree on a wrong digest. |
| 5 | * This file pins the published vectors: |
| 6 | * |
| 7 | * FIPS 180-4 / NIST CAVP SHA-256("abc") |
| 8 | * FIPS 180-4 SHA-256("") (empty message) |
| 9 | * FIPS 180-4 SHA-256(448-bit message) (two-block padding edge) |
| 10 | * FIPS 180-4 SHA-256(896-bit message) (padding-length edge) |
| 11 | * RFC 4231 case 1 HMAC-SHA256("key", "The quick brown fox...") |
| 12 | * RFC 4231 case 2 HMAC-SHA256("Jefe", "what do ya want for nothing?") |
| 13 | * RFC 4231 case 3 HMAC-SHA256(0xaa x 20, 0xdd x 50) |
| 14 | * RFC 4231 case 6 HMAC-SHA256(key > blocksize, long message) |
| 15 | * |
| 16 | * Exit 0 when every vector matches, 1 otherwise. |
| 17 | * |
| 18 | * Build: cc -O2 -std=c99 -I src tests/sha256_vectors.c src/sha256.c -o /tmp/v |
| 19 | */ |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include "sha256.h" |
| 24 | |
| 25 | static int fails; |
| 26 | |
| 27 | static void hex_of_sha(const void *msg, size_t len, char *out) |
| 28 | { |
| 29 | sx_sha256_hex(msg, len, out); |
| 30 | } |
| 31 | |
| 32 | static void want_sha(const char *label, const void *msg, size_t len, const char *expect) |
| 33 | { |
| 34 | char got[65]; |
| 35 | hex_of_sha(msg, len, got); |
| 36 | if (strcmp(got, expect) == 0) { |
| 37 | printf("ok %s\n", label); |
| 38 | } else { |
| 39 | printf("FAIL %s\n want %s\n got %s\n", label, expect, got); |
| 40 | fails++; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | static void want_hmac(const char *label, const void *key, size_t klen, |
| 45 | const void *msg, size_t mlen, const char *expect) |
| 46 | { |
| 47 | char got[65]; |
| 48 | sx_hmac_sha256_hex(key, klen, msg, mlen, got); |
| 49 | if (strcmp(got, expect) == 0) { |
| 50 | printf("ok %s\n", label); |
| 51 | } else { |
| 52 | printf("FAIL %s\n want %s\n got %s\n", label, expect, got); |
| 53 | fails++; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | int main(void) |
| 58 | { |
| 59 | static const char abc[] = "abc"; |
| 60 | static const char *vec448 = |
| 61 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; |
| 62 | static const char *vec896 = |
| 63 | "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" |
| 64 | "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"; |
| 65 | static const char rfc4231_key1[] = "key"; |
| 66 | static const char rfc4231_msg1[] = "The quick brown fox jumps over the lazy dog"; |
| 67 | static const char rfc4231_key2[] = "Jefe"; |
| 68 | static const char rfc4231_msg2[] = "what do ya want for nothing?"; |
| 69 | |
| 70 | want_sha("SHA-256(\"abc\")", |
| 71 | abc, 3, |
| 72 | "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); |
| 73 | want_sha("SHA-256(\"\")", |
| 74 | "", 0, |
| 75 | "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); |
| 76 | want_sha("SHA-256(448-bit message)", |
| 77 | vec448, strlen(vec448), |
| 78 | "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"); |
| 79 | want_sha("SHA-256(896-bit message)", |
| 80 | vec896, strlen(vec896), |
| 81 | "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1"); |
| 82 | |
| 83 | want_hmac("RFC 4231 case 1", |
| 84 | rfc4231_key1, strlen(rfc4231_key1), |
| 85 | rfc4231_msg1, strlen(rfc4231_msg1), |
| 86 | "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"); |
| 87 | want_hmac("RFC 4231 case 2", |
| 88 | rfc4231_key2, strlen(rfc4231_key2), |
| 89 | rfc4231_msg2, strlen(rfc4231_msg2), |
| 90 | "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843"); |
| 91 | { |
| 92 | unsigned char k[20], m[50]; |
| 93 | memset(k, 0xaa, sizeof k); |
| 94 | memset(m, 0xdd, sizeof m); |
| 95 | want_hmac("RFC 4231 case 3", |
| 96 | k, sizeof k, m, sizeof m, |
| 97 | "773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe"); |
| 98 | } |
| 99 | { |
| 100 | unsigned char k[131]; |
| 101 | const char *m = |
| 102 | "Test Using Larger Than Block-Size Key - Hash Key First"; |
| 103 | memset(k, 0xaa, sizeof k); |
| 104 | want_hmac("RFC 4231 case 6", |
| 105 | k, sizeof k, m, strlen(m), |
| 106 | "60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54"); |
| 107 | } |
| 108 | /* A key longer than the block is hashed first; 64 bytes exactly is the |
| 109 | * boundary case a naive implementation gets wrong. */ |
| 110 | { |
| 111 | unsigned char k64[64]; |
| 112 | char hex_a[65], hex_b[65]; |
| 113 | memset(k64, 0x0b, sizeof k64); |
| 114 | sx_hmac_sha256_hex(k64, sizeof k64, abc, 3, hex_a); |
| 115 | sx_hmac_sha256_hex(k64, sizeof k64, abc, 3, hex_b); |
| 116 | if (strcmp(hex_a, hex_b) == 0) |
| 117 | printf("ok HMAC with a 64-byte key is deterministic\n"); |
| 118 | else { |
| 119 | printf("FAIL HMAC with a 64-byte key is not deterministic\n"); |
| 120 | fails++; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (fails) { |
| 125 | printf("\n%d known-answer test(s) failed\n", fails); |
| 126 | return 1; |
| 127 | } |
| 128 | printf("\nall SHA-256 / HMAC-SHA256 known-answer tests passed\n"); |
| 129 | return 0; |
| 130 | } |