| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include "crypto/eap_v4_crypto.h" |
| 13 |
#include <stdint.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
static int g_failures = 0; |
| 18 |
|
| 19 |
static void from_hex(const char *hex, uint8_t *out, size_t out_len) |
| 20 |
{ |
| 21 |
for (size_t i = 0; i < out_len; i++) { |
| 22 |
unsigned v; |
| 23 |
sscanf(hex + 2 * i, "%2x", &v); |
| 24 |
out[i] = (uint8_t)v; |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
static void check(const char *name, const uint8_t *got, const uint8_t *want, size_t len) |
| 29 |
{ |
| 30 |
if (memcmp(got, want, len) != 0) { |
| 31 |
printf("FAIL: %s\n", name); |
| 32 |
g_failures++; |
| 33 |
} else { |
| 34 |
printf("ok: %s\n", name); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
static void test_hkdf_rfc5869_case1(void) |
| 41 |
{ |
| 42 |
uint8_t prk[32], info[10], okm[42], want[42]; |
| 43 |
from_hex("077709362c2e32df0ddc3f0dc47bba6390b6c73bb50f9c3122ec844ad7c2b3e5", prk, 32); |
| 44 |
from_hex("f0f1f2f3f4f5f6f7f8f9", info, 10); |
| 45 |
from_hex("3cb25f25faacd57a90434f64d0362f2a" |
| 46 |
"2d2d0a90cf1a5a4c5db02d56ecc4c5bf" |
| 47 |
"34007208d5b887185865", want, 42); |
| 48 |
int rc = _librist_crypto_hkdf_expand_sha256(prk, 32, info, 10, okm, 42); |
| 49 |
if (rc != 0) { printf("FAIL: hkdf rc=%d (backend?)\n", rc); g_failures++; return; } |
| 50 |
check("hkdf-expand rfc5869 case1", okm, want, 42); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
static void test_hkdf_direction_separation(void) |
| 57 |
{ |
| 58 |
uint8_t prk[32]; |
| 59 |
for (int i = 0; i < 32; i++) prk[i] = (uint8_t)(0x10 + i); |
| 60 |
const char *c2s = "RIST-EAP-v4 pass c2s"; |
| 61 |
const char *s2c = "RIST-EAP-v4 pass s2c"; |
| 62 |
uint8_t kc[32], ks[32], kc_again[32]; |
| 63 |
int rc = _librist_crypto_hkdf_expand_sha256(prk, 32, (const uint8_t *)c2s, strlen(c2s), kc, 32); |
| 64 |
rc |= _librist_crypto_hkdf_expand_sha256(prk, 32, (const uint8_t *)s2c, strlen(s2c), ks, 32); |
| 65 |
rc |= _librist_crypto_hkdf_expand_sha256(prk, 32, (const uint8_t *)c2s, strlen(c2s), kc_again, 32); |
| 66 |
if (rc != 0) { printf("FAIL: hkdf dir rc=%d (backend?)\n", rc); g_failures++; return; } |
| 67 |
if (memcmp(kc, ks, 32) == 0) { printf("FAIL: c2s and s2c keys are identical\n"); g_failures++; } |
| 68 |
else printf("ok: per-direction keys differ\n"); |
| 69 |
check("hkdf derivation is deterministic", kc, kc_again, 32); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
static void test_gcm_kat_case14(void) |
| 75 |
{ |
| 76 |
uint8_t key[32] = {0}, iv[12] = {0}, pt[16] = {0}; |
| 77 |
uint8_t ct[16], tag[16]; |
| 78 |
uint8_t want_ct[16], want_tag[16]; |
| 79 |
from_hex("cea7403d4d606b6e074ec5d3baf39d18", want_ct, 16); |
| 80 |
from_hex("d0d1c8a799996bf0265b98b5d48ab919", want_tag, 16); |
| 81 |
int rc = _librist_crypto_aes_gcm_encrypt(key, iv, 12, NULL, 0, pt, 16, ct, tag, 16); |
| 82 |
if (rc != 0) { printf("FAIL: gcm encrypt rc=%d (backend?)\n", rc); g_failures++; return; } |
| 83 |
check("gcm-256 KAT case14 ciphertext", ct, want_ct, 16); |
| 84 |
check("gcm-256 KAT case14 tag", tag, want_tag, 16); |
| 85 |
|
| 86 |
|
| 87 |
uint8_t out[16]; |
| 88 |
rc = _librist_crypto_aes_gcm_decrypt(key, iv, 12, NULL, 0, ct, 16, tag, 16, out); |
| 89 |
if (rc != 0) { printf("FAIL: gcm decrypt of KAT rc=%d\n", rc); g_failures++; } |
| 90 |
else check("gcm-256 KAT case14 decrypt", out, pt, 16); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
static void test_gcm_roundtrip_and_tamper(void) |
| 95 |
{ |
| 96 |
uint8_t key[32], iv[12]; |
| 97 |
for (int i = 0; i < 32; i++) key[i] = (uint8_t)(i + 1); |
| 98 |
for (int i = 0; i < 12; i++) iv[i] = (uint8_t)(0xa0 + i); |
| 99 |
const uint8_t pt[] = "correct horse battery staple"; |
| 100 |
const size_t pt_len = sizeof(pt) - 1; |
| 101 |
const uint8_t aad[] = {0x02, 0x07, 0x13, 0x10, 0x00}; |
| 102 |
uint8_t ct[64], tag[16], out[64]; |
| 103 |
|
| 104 |
int rc = _librist_crypto_aes_gcm_encrypt(key, iv, 12, aad, sizeof(aad), pt, pt_len, ct, tag, 16); |
| 105 |
if (rc != 0) { printf("FAIL: gcm rt encrypt rc=%d\n", rc); g_failures++; return; } |
| 106 |
|
| 107 |
rc = _librist_crypto_aes_gcm_decrypt(key, iv, 12, aad, sizeof(aad), ct, pt_len, tag, 16, out); |
| 108 |
if (rc != 0) { printf("FAIL: gcm rt decrypt rc=%d\n", rc); g_failures++; } |
| 109 |
else check("gcm round-trip plaintext", out, pt, pt_len); |
| 110 |
|
| 111 |
|
| 112 |
uint8_t bad_tag[16]; |
| 113 |
memcpy(bad_tag, tag, 16); |
| 114 |
bad_tag[0] ^= 0x01; |
| 115 |
memset(out, 0xcc, sizeof(out)); |
| 116 |
rc = _librist_crypto_aes_gcm_decrypt(key, iv, 12, aad, sizeof(aad), ct, pt_len, bad_tag, 16, out); |
| 117 |
if (rc == 0) { printf("FAIL: gcm accepted a tampered tag\n"); g_failures++; } |
| 118 |
else printf("ok: gcm rejects tampered tag\n"); |
| 119 |
|
| 120 |
|
| 121 |
uint8_t bad_aad[5]; |
| 122 |
memcpy(bad_aad, aad, 5); |
| 123 |
bad_aad[0] ^= 0x01; |
| 124 |
rc = _librist_crypto_aes_gcm_decrypt(key, iv, 12, bad_aad, sizeof(bad_aad), ct, pt_len, tag, 16, out); |
| 125 |
if (rc == 0) { printf("FAIL: gcm accepted tampered AAD\n"); g_failures++; } |
| 126 |
else printf("ok: gcm rejects tampered AAD\n"); |
| 127 |
} |
| 128 |
|
| 129 |
int main(void) |
| 130 |
{ |
| 131 |
test_hkdf_rfc5869_case1(); |
| 132 |
test_hkdf_direction_separation(); |
| 133 |
test_gcm_kat_case14(); |
| 134 |
test_gcm_roundtrip_and_tamper(); |
| 135 |
if (g_failures) { |
| 136 |
printf("%d FAILURE(S)\n", g_failures); |
| 137 |
return 1; |
| 138 |
} |
| 139 |
printf("all eap_v4_crypto KATs passed\n"); |
| 140 |
return 0; |
| 141 |
} |