#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/module.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_media.h>
#include <net80211/ieee80211_var.h>
static const u_int8_t test1_key[] = {
0xc9, 0x7c, 0x1f, 0x67, 0xce, 0x37, 0x11, 0x85, 0x51, 0x4a, 0x8a,
0x19, 0xf2, 0xbd, 0xd5, 0x2f
};
static const u_int8_t test1_plaintext[] = {
0x08, 0x48, 0xc3, 0x2c, 0x0f, 0xd2, 0xe1, 0x28,
0xa5, 0x7c, 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08,
0xab, 0xae, 0xa5, 0xb8, 0xfc, 0xba, 0x80, 0x33,
0xf8, 0xba, 0x1a, 0x55, 0xd0, 0x2f, 0x85, 0xae,
0x96, 0x7b, 0xb6, 0x2f, 0xb6, 0xcd, 0xa8, 0xeb,
0x7e, 0x78, 0xa0, 0x50,
};
static const u_int8_t test1_encrypted[] = {
0x08, 0x48, 0xc3, 0x2c, 0x0f, 0xd2, 0xe1, 0x28,
0xa5, 0x7c, 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08,
0xab, 0xae, 0xa5, 0xb8, 0xfc, 0xba, 0x80, 0x33,
0x0c, 0xe7, 0x00, 0x20, 0x76, 0x97, 0x03, 0xb5,
0xf3, 0xd0, 0xa2, 0xfe, 0x9a, 0x3d, 0xbf, 0x23,
0x42, 0xa6, 0x43, 0xe4, 0x32, 0x46, 0xe8, 0x0c,
0x3c, 0x04, 0xd0, 0x19, 0x78, 0x45, 0xce, 0x0b,
0x16, 0xf9, 0x76, 0x23,
};
static const u_int8_t test2_key[] = {
0x8f, 0x7a, 0x05, 0x3f, 0xa5, 0x77, 0xa5, 0x59, 0x75, 0x29, 0x27,
0x20, 0x97, 0xa6, 0x03, 0xd5
};
static const u_int8_t test2_plaintext[] = {
0x38, 0xc0, 0x6a, 0x51, 0xea, 0x10, 0x0c, 0x84, 0x68, 0x50, 0xee,
0xc1, 0x76, 0x2c, 0x88, 0xde, 0xaf, 0x2e, 0xe9, 0xf4, 0x6a, 0x07,
0xe0, 0xcc,
0x83, 0xa0, 0x63, 0x4b, 0x5e, 0xd7, 0x62, 0x7e, 0xb9, 0xdf, 0x22,
0x5e, 0x05, 0x74, 0x03, 0x42, 0xde, 0x19, 0x41, 0x17
};
static const u_int8_t test2_encrypted[] = {
0x38, 0xc0, 0x6a, 0x51, 0xea, 0x10, 0x0c, 0x84, 0x68, 0x50, 0xee,
0xc1, 0x76, 0x2c, 0x88, 0xde, 0xaf, 0x2e, 0xe9, 0xf4, 0x6a, 0x07,
0xe0, 0xcc, 0xea, 0x97, 0x00, 0xa0, 0xba, 0xcb, 0xf3, 0x31, 0x81,
0x4b, 0x69, 0x65, 0xd0, 0x5b, 0xf2, 0xb2, 0xed, 0x38, 0xd4, 0xbe,
0xb0, 0x69, 0xfe, 0x82, 0x71, 0x4a, 0x61, 0x0b, 0x54, 0x2f, 0xbf,
0x8d, 0xa0, 0x6a, 0xa4, 0xae,
};
static const u_int8_t test3_key[] = {
0x40, 0xcf, 0xb7, 0xa6, 0x2e, 0x88, 0x01, 0x3b, 0xd6, 0xd3,
0xaf, 0xfc, 0xc1, 0x91, 0x04, 0x1e
};
static const u_int8_t test3_plaintext[] = {
0xb8, 0xc8, 0xdc, 0x61, 0xd9, 0x57, 0x7d, 0xf7, 0x63, 0xc8,
0xb6, 0xa8, 0x8a, 0xdf, 0x36, 0x91, 0xdc, 0x4a, 0x8b, 0xca,
0x94, 0xdd, 0x60, 0x82, 0x20, 0x85,
0x2c, 0x1b, 0xd0, 0x36, 0x83, 0x1c, 0x95, 0x49, 0x6c, 0x5f,
0x4d, 0xbf, 0x3d, 0x55, 0x9e, 0x72, 0xde, 0x80, 0x2a, 0x18
};
static const u_int8_t test3_encrypted[] = {
0xb8, 0xc8, 0xdc, 0x61, 0xd9, 0x57, 0x7d, 0xf7, 0x63, 0xc8,
0xb6, 0xa8, 0x8a, 0xdf, 0x36, 0x91, 0xdc, 0x4a, 0x8b, 0xca,
0x94, 0xdd, 0x60, 0x82, 0x20, 0x85, 0xae, 0xa5, 0x00, 0xa0,
0xf3, 0xa0, 0xdc, 0x2f, 0x89, 0xd8, 0x58, 0x03, 0x40, 0xb6,
0x26, 0xa0, 0xb6, 0xd4, 0xd0, 0x13, 0xbf, 0x18, 0xf2, 0x91,
0xb8, 0x96, 0x46, 0xc8, 0xfd, 0x1f, 0x1f, 0x61, 0xa9, 0xfb,
0x4b, 0xb3,
};
static const u_int8_t test4_key[] = {
0x8c, 0x89, 0xa2, 0xeb, 0xc9, 0x6c, 0x76, 0x02,
0x70, 0x7f, 0xcf, 0x24, 0xb3, 0x2d, 0x38, 0x33,
};
static const u_int8_t test4_plaintext[] = {
0xa8, 0xca, 0x3a, 0x11, 0x71, 0x2a, 0x9d, 0xdf, 0x11, 0xdb,
0x8e, 0xf8, 0x22, 0x73, 0x47, 0x01, 0x59, 0x14, 0x0d, 0xd6,
0x46, 0xa2, 0xc0, 0x2f, 0x67, 0xa5,
0x4f, 0xad, 0x2b, 0x1c, 0x29, 0x0f, 0xa5, 0xeb, 0xd8, 0x72,
0xfb, 0xc3, 0xf3, 0xa0, 0x74, 0x89, 0x8f, 0x8b, 0x2f, 0xbb,
};
static const u_int8_t test4_encrypted[] = {
0xa8, 0xca, 0x3a, 0x11, 0x71, 0x2a, 0x9d, 0xdf, 0x11, 0xdb,
0x8e, 0xf8, 0x22, 0x73, 0x47, 0x01, 0x59, 0x14, 0x0d, 0xd6,
0x46, 0xa2, 0xc0, 0x2f, 0x67, 0xa5, 0xe3, 0x0f, 0x00, 0x20,
0x5a, 0xa5, 0x70, 0xf6, 0x9d, 0x59, 0xb1, 0x5f, 0x37, 0x14,
0x48, 0xc2, 0x30, 0xf4, 0xd7, 0x39, 0x05, 0x2e, 0x13, 0xab,
0x3b, 0x1a, 0x7b, 0x10, 0x31, 0xfc, 0x88, 0x00, 0x4f, 0x35,
0xee, 0x3d,
};
static const u_int8_t test5_key[] = {
0xa5, 0x74, 0xd5, 0x14, 0x3b, 0xb2, 0x5e, 0xfd,
0xde, 0xff, 0x30, 0x12, 0x2f, 0xdf, 0xd0, 0x66,
};
static const u_int8_t test5_plaintext[] = {
0x88, 0xda, 0x18, 0x41, 0x45, 0xde, 0xc6, 0x9a, 0x74, 0x80,
0xf3, 0x51, 0x94, 0x6b, 0xc9, 0x6b, 0xe2, 0x76, 0xfb, 0xe6,
0xc1, 0x27, 0x80, 0xf2, 0x4b, 0x19,
0x28, 0x96, 0x9b, 0x95, 0x4f, 0x26, 0x3a, 0x80, 0x18, 0xa9,
0xef, 0x70, 0xa8, 0xb0, 0x51, 0x46, 0x24, 0x81, 0x92, 0x2e,
};
static const u_int8_t test5_encrypted[] = {
0x88, 0xda, 0x18, 0x41, 0x45, 0xde, 0xc6, 0x9a, 0x74, 0x80,
0xf3, 0x51, 0x94, 0x6b, 0xc9, 0x6b, 0xe2, 0x76, 0xfb, 0xe6,
0xc1, 0x27, 0x80, 0xf2, 0x4b, 0x19, 0x37, 0x0e, 0x00, 0xa0,
0x3c, 0xe0, 0xff, 0xa7, 0xeb, 0x4a, 0xe4, 0x95, 0x6a, 0x80,
0x1d, 0xa9, 0x62, 0x4b, 0x7e, 0x0c, 0x18, 0xb2, 0x3e, 0x61,
0x5e, 0xc0, 0x3a, 0xf6, 0xce, 0x0c, 0x3b, 0xe1, 0x97, 0xd3,
0x05, 0xeb,
};
static const u_int8_t test6_key[] = {
0xf7, 0x1e, 0xea, 0x4e, 0x1f, 0x58, 0x80, 0x4b,
0x97, 0x17, 0x23, 0x0a, 0xd0, 0x61, 0x46, 0x41,
};
static const u_int8_t test6_plaintext[] = {
0x88, 0x52, 0xe1, 0x1f, 0x5a, 0xf2, 0x84, 0x30, 0xfd, 0xab,
0xbf, 0xf9, 0x43, 0xb9, 0xf9, 0xa6, 0xab, 0x1d, 0x98, 0xc7,
0xfe, 0x73, 0x50, 0x71, 0x3d, 0x6a,
0xab, 0xfd, 0xa2, 0x2d, 0x3a, 0x0b, 0xfc, 0x9c, 0xc1, 0xfc,
0x07, 0x93, 0x63, 0xc2, 0xfc, 0xa1, 0x43, 0xe6, 0xeb, 0x1d,
};
static const u_int8_t test6_encrypted[] = {
0x88, 0x52, 0xe1, 0x1f, 0x5a, 0xf2, 0x84, 0x30, 0xfd, 0xab,
0xbf, 0xf9, 0x43, 0xb9, 0xf9, 0xa6, 0xab, 0x1d, 0x98, 0xc7,
0xfe, 0x73, 0x50, 0x71, 0x3d, 0x6a, 0x89, 0x89, 0x00, 0x60,
0xa4, 0xec, 0x81, 0x6b, 0x9a, 0x70, 0x9b, 0x60, 0xa3, 0x9d,
0x40, 0xb1, 0xdf, 0xb6, 0x12, 0xe1, 0x8b, 0x5f, 0x11, 0x4b,
0xad, 0xb6, 0xcc, 0x86, 0x30, 0x9a, 0x8d, 0x5c, 0x46, 0x6b,
0xbb, 0x71,
};
static const u_int8_t test7_key[] = {
0x1b, 0xdb, 0x34, 0x98, 0x0e, 0x03, 0x81, 0x24,
0xa1, 0xdb, 0x1a, 0x89, 0x2b, 0xec, 0x36, 0x6a,
};
static const u_int8_t test7_plaintext[] = {
0x18, 0x79, 0x81, 0x46, 0x9b, 0x50, 0xf4, 0xfd, 0x56, 0xf6,
0xef, 0xec, 0x95, 0x20, 0x16, 0x91, 0x83, 0x57, 0x0c, 0x4c,
0xcd, 0xee, 0x20, 0xa0,
0x98, 0xbe, 0xca, 0x86, 0xf4, 0xb3, 0x8d, 0xa2, 0x0c, 0xfd,
0xf2, 0x47, 0x24, 0xc5, 0x8e, 0xb8, 0x35, 0x66, 0x53, 0x39,
};
static const u_int8_t test7_encrypted[] = {
0x18, 0x79, 0x81, 0x46, 0x9b, 0x50, 0xf4, 0xfd, 0x56, 0xf6,
0xef, 0xec, 0x95, 0x20, 0x16, 0x91, 0x83, 0x57, 0x0c, 0x4c,
0xcd, 0xee, 0x20, 0xa0, 0x23, 0xe7, 0x00, 0xe0, 0x73, 0x40,
0xec, 0x5e, 0x12, 0xc5, 0x37, 0xeb, 0xf3, 0xab, 0x58, 0x4e,
0xf1, 0xfe, 0xf9, 0xa1, 0xf3, 0x54, 0x7a, 0x8c, 0x13, 0xb3,
0x22, 0x5a, 0x2d, 0x09, 0x57, 0xec, 0xfa, 0xbe, 0x95, 0xb9,
};
static const u_int8_t test8_key[] = {
0x6e, 0xac, 0x1b, 0xf5, 0x4b, 0xd5, 0x4e, 0xdb,
0x23, 0x21, 0x75, 0x43, 0x03, 0x02, 0x4c, 0x71,
};
static const u_int8_t test8_plaintext[] = {
0xb8, 0xd9, 0x4c, 0x72, 0x55, 0x2d, 0x5f, 0x72, 0xbb, 0x70,
0xca, 0x3f, 0x3a, 0xae, 0x60, 0xc4, 0x8b, 0xa9, 0xb5, 0xf8,
0x2c, 0x2f, 0x50, 0xeb, 0x2a, 0x55,
0x57, 0xcb, 0x5c, 0x0e, 0x5f, 0xcd, 0x88, 0x5e, 0x9a, 0x42,
0x39, 0xe9, 0xb9, 0xca, 0xd6, 0x0d, 0x64, 0x37, 0x59, 0x79,
};
static const u_int8_t test8_encrypted[] = {
0xb8, 0xd9, 0x4c, 0x72, 0x55, 0x2d, 0x5f, 0x72, 0xbb, 0x70,
0xca, 0x3f, 0x3a, 0xae, 0x60, 0xc4, 0x8b, 0xa9, 0xb5, 0xf8,
0x2c, 0x2f, 0x50, 0xeb, 0x2a, 0x55, 0xdd, 0xcc, 0x00, 0xa0,
0x6e, 0x99, 0xfd, 0xce, 0x4b, 0xf2, 0x81, 0xef, 0x8e, 0xc7,
0x73, 0x9f, 0x91, 0x59, 0x1b, 0x97, 0xa8, 0x7d, 0xc1, 0x4b,
0x3f, 0xa1, 0x74, 0x62, 0x6d, 0xba, 0x8e, 0xf7, 0xf0, 0x80,
0x87, 0xdd,
};
#define TEST(n,name,cipher,keyix,pn) { \
name, IEEE80211_CIPHER_##cipher,keyix, pn##LL, \
test##n##_key, sizeof(test##n##_key), \
test##n##_plaintext, sizeof(test##n##_plaintext), \
test##n##_encrypted, sizeof(test##n##_encrypted) \
}
struct ciphertest {
const char *name;
int cipher;
int keyix;
u_int64_t pn;
const u_int8_t *key;
size_t key_len;
const u_int8_t *plaintext;
size_t plaintext_len;
const u_int8_t *encrypted;
size_t encrypted_len;
} ccmptests[] = {
TEST(1, "CCMP test mpdu 1", AES_CCM, 0, 199027030681356),
TEST(2, "CCMP test mpdu 2", AES_CCM, 2, 54923164817386),
TEST(3, "CCMP test mpdu 3", AES_CCM, 2, 52624639632814),
TEST(4, "CCMP test mpdu 4", AES_CCM, 0, 270963670912995),
TEST(5, "CCMP test mpdu 5", AES_CCM, 2, 184717420531255),
TEST(6, "CCMP test mpdu 6", AES_CCM, 1, 118205765159305),
TEST(7, "CCMP test mpdu 7", AES_CCM, 3, 104368786630435),
TEST(8, "CCMP test mpdu 8", AES_CCM, 2, 227588596223197),
};
static void
dumpdata(const char *tag, const void *p, size_t len)
{
int i;
printf("%s: 0x%p len %u", tag, p, len);
for (i = 0; i < len; i++) {
if ((i % 16) == 0)
printf("\n%03d:", i);
printf(" %02x", ((const u_int8_t *)p)[i]);
}
printf("\n");
}
static void
cmpfail(const void *gen, size_t genlen, const void *ref, size_t reflen)
{
int i;
for (i = 0; i < genlen; i++)
if (((const u_int8_t *)gen)[i] != ((const u_int8_t *)ref)[i]) {
printf("first difference at byte %u\n", i);
break;
}
dumpdata("Generated", gen, genlen);
dumpdata("Reference", ref, reflen);
}
static void
printtest(const struct ciphertest *t)
{
printf("keyix %u pn %llu key_len %u plaintext_len %u\n"
, t->keyix
, t->pn
, t->key_len
, t->plaintext_len
);
}
static int
runtest(struct ieee80211vap *vap, struct ciphertest *t)
{
struct ieee80211_key *key = &vap->iv_nw_keys[t->keyix];
struct mbuf *m = NULL;
const struct ieee80211_cipher *cip;
int hdrlen;
printf("%s: ", t->name);
memset(key, 0, sizeof(*key));
key->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
key->wk_cipher = &ieee80211_cipher_none;
if (!ieee80211_crypto_newkey(vap, t->cipher,
IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, key)) {
printf("FAIL: ieee80211_crypto_newkey failed\n");
goto bad;
}
memcpy(key->wk_key, t->key, t->key_len);
key->wk_keylen = t->key_len;
memset(key->wk_keyrsc, 0, sizeof(key->wk_keyrsc));
key->wk_keytsc = t->pn-1;
if (!ieee80211_crypto_setkey(vap, key)) {
printf("FAIL: ieee80211_crypto_setkey failed\n");
goto bad;
}
cip = key->wk_cipher;
m = m_getcl(M_NOWAIT, MT_HEADER, M_PKTHDR);
m->m_data += cip->ic_header;
memcpy(mtod(m, void *), t->plaintext, t->plaintext_len);
m->m_len = t->plaintext_len;
m->m_pkthdr.len = m->m_len;
hdrlen = ieee80211_anyhdrsize(mtod(m, void *));
if (!cip->ic_encap(key, m)) {
printtest(t);
printf("FAIL: ccmp encap failed\n");
goto bad;
}
if (m->m_pkthdr.len != t->encrypted_len) {
printf("FAIL: encap data length mismatch\n");
printtest(t);
cmpfail(mtod(m, const void *), m->m_pkthdr.len,
t->encrypted, t->encrypted_len);
goto bad;
} else if (memcmp(mtod(m, const void *), t->encrypted, t->encrypted_len)) {
printf("FAIL: encrypt data does not compare\n");
printtest(t);
cmpfail(mtod(m, const void *), m->m_pkthdr.len,
t->encrypted, t->encrypted_len);
dumpdata("Plaintext", t->plaintext, t->plaintext_len);
goto bad;
}
if (!cip->ic_decap(key, m, hdrlen)) {
printf("FAIL: ccmp decap failed\n");
printtest(t);
cmpfail(mtod(m, const void *), m->m_len,
t->plaintext, t->plaintext_len);
goto bad;
}
if (m->m_pkthdr.len != t->plaintext_len) {
printf("FAIL: decap botch; length mismatch\n");
printtest(t);
cmpfail(mtod(m, const void *), m->m_pkthdr.len,
t->plaintext, t->plaintext_len);
goto bad;
} else if (memcmp(mtod(m, const void *), t->plaintext, t->plaintext_len)) {
printf("FAIL: decap botch; data does not compare\n");
printtest(t);
cmpfail(mtod(m, const void *), m->m_pkthdr.len,
t->plaintext, t->plaintext_len);
goto bad;
}
m_freem(m);
ieee80211_crypto_delkey(vap, key);
printf("PASS\n");
return 1;
bad:
if (m != NULL)
m_freem(m);
ieee80211_crypto_delkey(vap, key);
return 0;
}
static int tests = -1;
static int debug = 0;
static int
init_crypto_ccmp_test(void)
{
struct ieee80211com ic;
struct ieee80211vap vap;
struct ifnet ifp;
int i, pass, total;
memset(&ic, 0, sizeof(ic));
memset(&vap, 0, sizeof(vap));
memset(&ifp, 0, sizeof(ifp));
ieee80211_crypto_attach(&ic);
strncpy(ifp.if_xname, "test_ccmp", sizeof(ifp.if_xname));
vap.iv_ic = ⁣
vap.iv_ifp = &ifp;
if (debug)
vap.iv_debug = IEEE80211_MSG_CRYPTO;
ieee80211_crypto_vattach(&vap);
pass = 0;
total = 0;
for (i = 0; i < nitems(ccmptests); i++)
if (tests & (1<<i)) {
total++;
pass += runtest(&vap, &ccmptests[i]);
}
printf("%u of %u 802.11i AES-CCMP test vectors passed\n", pass, total);
ieee80211_crypto_vdetach(&vap);
ieee80211_crypto_detach(&ic);
return (pass == total ? 0 : -1);
}
static int
test_ccmp_modevent(module_t mod, int type, void *unused)
{
switch (type) {
case MOD_LOAD:
(void) init_crypto_ccmp_test();
return 0;
case MOD_UNLOAD:
return 0;
}
return EINVAL;
}
static moduledata_t test_ccmp_mod = {
"test_ccmp",
test_ccmp_modevent,
0
};
DECLARE_MODULE(test_ccmp, test_ccmp_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
MODULE_VERSION(test_ccmp, 1);
MODULE_DEPEND(test_ccmp, wlan, 1, 1, 1);