| 1 | #include <botan/kdf.h> |
| 2 | #include <botan/build.h> |
| 3 | #include <assert.h> |
| 4 | #include <iostream> |
| 5 | #include <iomanip> |
| 6 | |
| 7 | using namespace std; |
| 8 | |
| 9 | /* |
| 10 | * Generate test vectors for SP 800-180 using the Botan library. |
| 11 | * |
| 12 | * Input key: 16 bytes |
| 13 | * PRF: HMAC-256, HMAC-384, HMAC-512 |
| 14 | * Label: empty, 1 byte, 2 bytes, 16 bytes |
| 15 | * Context: empty, 1 byte, 2 bytes, 16 bytes |
| 16 | */ |
| 17 | |
| 18 | void print_hex(const char *prefix, const uint8_t *s, size_t slen) |
| 19 | { |
| 20 | printf("%s", prefix); |
| 21 | for (unsigned i=0; i<slen; i++) { |
| 22 | printf("%02X", s[i]); |
| 23 | } |
| 24 | printf("\n"); |
| 25 | } |
| 26 | |
| 27 | void make_tv(Botan::KDF *kdf, const char *label, const char *context, unsigned olen, unsigned count) |
| 28 | { |
| 29 | uint8_t *out = new uint8_t[olen]; |
| 30 | uint8_t secret[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 31 | 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F }; |
| 32 | |
| 33 | kdf->kdf(out, olen, |
| 34 | secret, 16, |
| 35 | (uint8_t*)context, strlen(context), |
| 36 | (uint8_t*)label, strlen(label)); |
| 37 | |
| 38 | printf("COUNT = %d\n", count); |
| 39 | print_hex("KIN = ", secret, sizeof(secret)); |
| 40 | print_hex("LABEL = ", (const uint8_t*)label, strlen(label)); |
| 41 | print_hex("CONTEXT = ", (const uint8_t*)context, strlen(context)); |
| 42 | printf("RLEN = 32\n"); |
| 43 | print_hex("KOUT = ", out, olen); |
| 44 | |
| 45 | printf("\n"); |
| 46 | delete out; |
| 47 | } |
| 48 | |
| 49 | int main(void) |
| 50 | { |
| 51 | typedef struct PRF { |
| 52 | const char* botan; |
| 53 | const char* desc; |
| 54 | } PRF; |
| 55 | |
| 56 | PRF prf[] = { |
| 57 | { "SP800-108-Counter(HMAC(SHA-256))", "HMAC-SHA-256" }, |
| 58 | { "SP800-108-Counter(HMAC(SHA-384))", "HMAC-SHA-384" }, |
| 59 | { "SP800-108-Counter(HMAC(SHA-512))", "HMAC-SHA-512" }, |
| 60 | { "SP800-108-Counter(CMAC(AES-128))", "CMAC-AES-128" }, |
| 61 | { NULL, NULL } |
| 62 | }; |
| 63 | |
| 64 | const char *text[] = { |
| 65 | "", |
| 66 | "A", |
| 67 | "AB", |
| 68 | "ABCDEFGHILMNOPQR", |
| 69 | NULL |
| 70 | }; |
| 71 | |
| 72 | const unsigned olens[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 128, 129 }; |
| 73 | |
| 74 | unsigned count = 0; |
| 75 | |
| 76 | printf("#\n"); |
| 77 | printf("# Test vectors for NIST SP-800 180, KDF in Counter mode\n"); |
| 78 | printf("# Generated with Botan v%d.%d.%d\n", BOTAN_VERSION_MAJOR, BOTAN_VERSION_MINOR, BOTAN_VERSION_PATCH); |
| 79 | printf("#\n"); |
| 80 | |
| 81 | for (PRF *pprf=prf; pprf->botan; pprf++) { |
| 82 | Botan::KDF *kdf = Botan::get_kdf(pprf->botan); |
| 83 | |
| 84 | assert(kdf); |
| 85 | printf("\n[%s]\n\n", pprf->desc); |
| 86 | |
| 87 | for (const char **label=text; *label; label++) { |
| 88 | for (const char **context=text; *context; context++) { |
| 89 | for (unsigned i=0; i<sizeof(olens)/sizeof(olens[0]); i++) { |
| 90 | make_tv(kdf, *label, *context, olens[i], count); |
| 91 | count++; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | delete kdf; |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |