[RFC 54/56] NAN: Add crypto API tests

Andrei Otcheretianski andrei.otcheretianski at intel.com
Sun Dec 7 03:19:03 PST 2025


From: Ilan Peer <ilan.peer at intel.com>

Based on the test vectors provided in WiFi Aware specification v4.0.

Signed-off-by: Ilan Peer <ilan.peer at intel.com>
---
 src/nan/nan_module_tests.c | 237 +++++++++++++++++++++++++++++++++++++
 1 file changed, 237 insertions(+)

diff --git a/src/nan/nan_module_tests.c b/src/nan/nan_module_tests.c
index 6ebf3c792d..dedec5b612 100644
--- a/src/nan/nan_module_tests.c
+++ b/src/nan/nan_module_tests.c
@@ -7,6 +7,9 @@
  */
 
 #include "nan_module_tests.h"
+#include "crypto/sha256.h"
+#include "crypto/sha384.h"
+#include "crypto/crypto.h"
 
 #define NAN_TEST_MAX_NAF_LEN     1024
 #define NAN_TEST_MAX_PEERS       20
@@ -1158,7 +1161,241 @@ static int nan_test_run(void)
 }
 
 
+static const u8 nan_key[32] = {
+	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+};
+
+static u8 nan_addrs[][ETH_ALEN] = {
+	{
+		0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
+	},
+	{
+		0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
+	},
+};
+
+static u8 nan_nonce[][WPA_NONCE_LEN] = {
+	{
+		0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
+		0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
+		0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
+		0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
+	},
+	{
+		0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
+		0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
+		0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
+		0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
+	},
+};
+
+static struct nan_ptk nan_ptk_128 = {
+	.kck =	{
+		0x67, 0x40, 0x47, 0x6f, 0x9c, 0xb4, 0xcd, 0xee,
+		0xfb, 0xc7, 0xad, 0x25, 0x78, 0xd5, 0x5e, 0xb2,
+	},
+	.kek = {
+		0xe5, 0xc7, 0x55, 0xbb, 0x4e, 0x4f, 0xd7, 0xbb,
+		0xdb, 0x19, 0xa5, 0xb3, 0x6c, 0xcc, 0x77, 0xe3,
+	},
+	.tk = {
+		0xe2, 0x7f, 0xfc, 0x7b, 0xa2, 0xd6, 0xb2, 0x44,
+		0x63, 0x4a, 0x0c, 0xf2, 0x09, 0xf8, 0x06, 0x9a,
+	},
+	.kck_len = 16,
+	.kek_len = 16,
+	.tk_len = 16,
+};
+
+static struct nan_ptk nan_ptk_256 = {
+	.kck =	{
+		0x72, 0x70, 0xd5, 0xdc, 0x05, 0x44, 0x8a, 0xb4,
+		0x1a, 0x73, 0xe5, 0x51, 0x56, 0xa0, 0x78, 0x3a,
+		0x33, 0x74, 0x62, 0x4e, 0x81, 0xe0, 0x39, 0xf8,
+	},
+	.kek = {
+		0x55, 0x3e, 0x2c, 0x8a, 0x0b, 0xab, 0xfc, 0xe1,
+		0xf9, 0x45, 0xfb, 0xa7, 0xa6, 0xc9, 0x0c, 0x77,
+		0x76, 0x64, 0x6b, 0xaa, 0xc0, 0x59, 0x7c, 0xd6,
+		0xa4, 0x99, 0x31, 0xf2, 0x6e, 0xbf, 0x0e, 0x49,
+	},
+	.tk = {
+		0x44, 0x61, 0x12, 0xba, 0x4c, 0x25, 0x8b, 0xe5,
+		0x29, 0x33, 0x8f, 0x77, 0x45, 0x55, 0x13, 0x87,
+		0x69, 0x61, 0xf4, 0x33, 0xad, 0x87, 0x47, 0xbb,
+		0xa1, 0xd6, 0x5b, 0x4d, 0xaf, 0x0a, 0x37, 0x5a
+	},
+	.kck_len = 24,
+	.kek_len = 32,
+	.tk_len = 32,
+};
+
+static const u8 nan_service_id[NAN_SERVICE_ID_LEN] = {
+	0x48, 0xda, 0x63, 0xdc, 0xde, 0x19,
+};
+
+static u8 nan_pmkid_128[PMKID_LEN] = {
+	0xcf, 0x4f, 0x64, 0x44, 0xd8, 0xe2, 0xc4, 0xef,
+	0xa4, 0xf3, 0xa6, 0x51, 0xf3, 0x0f, 0x63, 0x4f,
+};
+
+static u8 nan_pmkid_256[PMKID_LEN] = {
+	0x08, 0x11, 0x23, 0xc8, 0x57, 0x52, 0x6a, 0x09,
+	0x6d, 0x46, 0x48, 0x2b, 0xa8, 0x02, 0xfc, 0x28,
+};
+
+static u8 nan_crypto_sha256_digest[SHA256_MAC_LEN] = {
+	0xde, 0x7a, 0xa9, 0x34, 0x0d, 0x1d, 0xbb, 0x73,
+	0x4d, 0x4d, 0x5a, 0xcb, 0x6b, 0xe4, 0xed, 0xc9,
+	0x0a, 0x16, 0x37, 0xaa, 0x1e, 0x5a, 0x34, 0x2e,
+	0x58, 0xdd, 0x0b, 0x30, 0x4b, 0xa4, 0x8c, 0x32,
+};
+
+static u8 nan_crypto_sha384_digest[SHA384_MAC_LEN] = {
+	0xb1, 0xf9, 0x75, 0xbd, 0x80, 0x84, 0x21, 0x35,
+	0xc2, 0x38, 0xf7, 0x0d, 0x03, 0x32, 0x38, 0xe7,
+	0xbb, 0x55, 0x39, 0x21, 0xf5, 0xb3, 0xbd, 0x5c,
+	0x93, 0x78, 0x52, 0x61, 0x6f, 0x83, 0x58, 0x2d,
+	0xdc, 0x0d, 0xe2, 0x25, 0x5e, 0x72, 0x6c, 0x95,
+	0xd0, 0xcb, 0x09, 0xef, 0x2a, 0x8f, 0x08, 0x17,
+};
+
+static u8 sha_data[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
+
+static u8 sha256_digest[] = {
+	0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
+	0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
+	0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
+	0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1,
+};
+
+static u8 sha384_digest[] = {
+	0x33, 0x91, 0xfd, 0xdd, 0xfc, 0x8d, 0xc7, 0x39,
+	0x37, 0x07, 0xa6, 0x5b, 0x1b, 0x47, 0x09, 0x39,
+	0x7c, 0xf8, 0xb1, 0xd1, 0x62, 0xaf, 0x05, 0xab,
+	0xfe, 0x8f, 0x45, 0x0d, 0xe5, 0xf3, 0x6b, 0xc6,
+	0xb0, 0x45, 0x5a, 0x85, 0x20, 0xbc, 0x4e, 0x6f,
+	0x5f, 0xe9, 0x5b, 0x1f, 0xe3, 0xc8, 0x45, 0x2b,
+};
+
+#define NAN_CRYPTO_FAIL(_ret)                                       \
+	if (_ret) {                                                 \
+		wpa_printf(MSG_ERROR,                               \
+			   "NAN Crypto test fail: %s:%d",           \
+			   __func__, __LINE__);                     \
+		return -1;                                          \
+	}
+
+
+static int nan_test_crypto_key_mic(void)
+{
+	u8 digest[SHA384_MAC_LEN];
+	u8 *data = nan_nonce[0];
+	size_t data_len = sizeof(nan_nonce[0]);
+
+	NAN_CRYPTO_FAIL(nan_crypto_key_mic(data, data_len, (u8 *)nan_key,
+					   sizeof(nan_key), NAN_CS_SK_CCM_128,
+					   digest));
+
+	NAN_CRYPTO_FAIL(os_memcmp(digest, nan_crypto_sha256_digest,
+				  NAN_KEY_MIC_LEN));
+
+	os_memset(digest, 0, sizeof(digest));
+
+	NAN_CRYPTO_FAIL(nan_crypto_key_mic(data, data_len, (u8 *)nan_key,
+					   sizeof(nan_key), NAN_CS_SK_GCM_256,
+					   digest));
+	NAN_CRYPTO_FAIL(os_memcmp(digest, nan_crypto_sha384_digest,
+				  NAN_KEY_MIC_24_LEN));
+
+	return 0;
+}
+
+
+static int nan_test_crypto_pmkid(void)
+{
+	u8 pmkid[PMKID_LEN];
+
+	NAN_CRYPTO_FAIL(nan_crypto_calc_pmkid(nan_key, nan_addrs[0],
+					      nan_addrs[1], nan_service_id,
+					      NAN_CS_SK_CCM_128, pmkid));
+
+	NAN_CRYPTO_FAIL(os_memcmp(pmkid, nan_pmkid_128, sizeof(pmkid)));
+
+	NAN_CRYPTO_FAIL(nan_crypto_calc_pmkid(nan_key, nan_addrs[0],
+					      nan_addrs[1], nan_service_id,
+					      NAN_CS_SK_GCM_256, pmkid));
+
+	NAN_CRYPTO_FAIL(os_memcmp(pmkid, nan_pmkid_256, sizeof(pmkid)));
+
+	return 0;
+}
+
+
+static int nan_test_ptk(void)
+{
+	struct nan_ptk tptk;
+
+	os_memset(&tptk, 0, sizeof(tptk));
+
+	NAN_CRYPTO_FAIL(nan_crypto_pmk_to_ptk(nan_key,
+					      nan_addrs[0], nan_addrs[1],
+					      nan_nonce[0], nan_nonce[1],
+					      &tptk, NAN_CS_SK_CCM_128));
+
+	NAN_CRYPTO_FAIL(os_memcmp(&tptk, &nan_ptk_128, sizeof(tptk)));
+
+	os_memset(&tptk, 0, sizeof(tptk));
+
+	NAN_CRYPTO_FAIL(nan_crypto_pmk_to_ptk(nan_key,
+					      nan_addrs[0], nan_addrs[1],
+					      nan_nonce[0], nan_nonce[1],
+					      &tptk, NAN_CS_SK_GCM_256));
+
+	NAN_CRYPTO_FAIL(os_memcmp(&tptk, &nan_ptk_256, sizeof(tptk)));
+
+	return 0;
+}
+
+
+static int nan_test_crypto_auth_token(void)
+{
+	u8 digest[SHA384_MAC_LEN];
+	u8 *data = sha_data;
+	size_t len = os_strlen((const char *)data);
+
+	NAN_CRYPTO_FAIL(nan_crypto_calc_auth_token(NAN_CS_SK_CCM_128, data,
+						   len, digest));
+	NAN_CRYPTO_FAIL(os_memcmp(digest, sha256_digest, NAN_AUTH_TOKEN_LEN));
+
+	os_memset(digest, 0, sizeof(digest));
+
+	NAN_CRYPTO_FAIL(nan_crypto_calc_auth_token(NAN_CS_SK_GCM_256, data, len,
+						   digest));
+	NAN_CRYPTO_FAIL(os_memcmp(digest, sha384_digest, NAN_AUTH_TOKEN_LEN));
+
+	return 0;
+}
+
+
+int nan_test_crypto(void)
+{
+	NAN_CRYPTO_FAIL(nan_test_crypto_key_mic());
+	NAN_CRYPTO_FAIL(nan_test_crypto_pmkid());
+	NAN_CRYPTO_FAIL(nan_test_ptk());
+	NAN_CRYPTO_FAIL(nan_test_crypto_auth_token());
+
+	return 0;
+}
+
+
 int nan_module_tests(void)
 {
+	if (nan_test_crypto())
+		return -1;
+
 	return nan_test_run();
 }
-- 
2.49.0




More information about the Hostap mailing list