| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- /* Host vectors for the software CCMP helpers (ccmp.c): FIPS-197 AES-128,
- * RFC 3610 CCM packet vector #1 and the 802.11 CCMP MPDU vector, in both
- * directions (RX decrypt, TX encrypt for the RTL8821AU). */
- #include <stdio.h>
- #include <string.h>
- #include "ccmp.c"
- static void hex(const unsigned char *p, int n, char *out)
- {
- int i;
- for (i = 0; i < n; i++)
- sprintf(out + 2 * i, "%02x", p[i]);
- }
- static void unhex(const char *s, unsigned char *out, int n)
- {
- int i;
- for (i = 0; i < n; i++) {
- unsigned int v;
- sscanf(s + 2 * i, "%2x", &v);
- out[i] = (unsigned char)v;
- }
- }
- int main(void)
- {
- unsigned char key[16], pt[16], ct[16], ccmout[23];
- char h[64];
- int failed = 0;
- /* FIPS-197 AES-128 */
- unhex("000102030405060708090a0b0c0d0e0f", key, 16);
- unhex("00112233445566778899aabbccddeeff", pt, 16);
- {
- unsigned char rk[176];
- aes_setkey(rk, key);
- aes_encrypt(rk, pt, ct);
- }
- hex(ct, 16, h);
- if (strcmp(h, "69c4e0d86a7b0430d8cdb78070b4c55a")) {
- printf("FAIL aes-128 %s\n", h);
- failed = 1;
- } else {
- printf("ok aes-128\n");
- }
- /* RFC 3610 packet vector #1 (L=2, 13-byte nonce, M=8) */
- {
- const char *k = "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf";
- const char *n = "00000003020100a0a1a2a3a4a5";
- const char *a = "0001020304050607";
- const char *p = "08090a0b0c0d0e0f101112131415161718191a1b1c1d1e";
- const char *c = "588c979a61c663d2f066d0c2c0f989806d5f6b61dac384";
- const char *t = "17e8d12cfdf926e0";
- unsigned char kk[16], nn[13], aa[8], pp[23], cc[23], tt[8];
- unhex(k, kk, 16);
- unhex(n, nn, 13);
- unhex(a, aa, 8);
- unhex(p, pp, 23);
- unhex(c, cc, 23);
- unhex(t, tt, 8);
- memcpy(ccmout, cc, 23);
- if (ccm_decrypt(kk, nn, aa, 8, ccmout, 23, tt) < 0 ||
- memcmp(ccmout, pp, 23)) {
- printf("FAIL ccm\n");
- failed = 1;
- } else {
- printf("ok ccm\n");
- }
- memcpy(ccmout, cc, 23);
- tt[0] ^= 1;
- if (ccm_decrypt(kk, nn, aa, 8, ccmout, 23, tt) >= 0) {
- printf("FAIL ccm bad tag accepted\n");
- failed = 1;
- } else {
- printf("ok ccm bad tag\n");
- }
- }
- /* 802.11 CCMP test vector (P802.11i/D7.0 I.7.4, MPDU 1). The header
- * has Retry set and a nonzero sequence number, exercising the AAD
- * masking and the PN byte order. */
- {
- static const unsigned char enc[] = {
- 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 unsigned char tk[16] = {
- 0xc9, 0x7c, 0x1f, 0x67, 0xce, 0x37, 0x11, 0x85,
- 0x51, 0x4a, 0x8a, 0x19, 0xf2, 0xbd, 0xd5, 0x2f,
- };
- static const unsigned char plain[20] = {
- 0xf8, 0xba, 0x1a, 0x55, 0xd0, 0x2f, 0x85, 0xae,
- 0x96, 0x7b, 0xb6, 0x2f, 0xb6, 0xcd, 0xa8, 0xeb,
- 0x7e, 0x78, 0xa0, 0x50,
- };
- unsigned char frame[sizeof(enc)];
- memcpy(frame, enc, sizeof(frame));
- if (ccmp_rx_decrypt(frame, sizeof(frame), 24, tk) < 0 ||
- memcmp(frame + 32, plain, sizeof(plain))) {
- printf("FAIL ccmp rx vector\n");
- failed = 1;
- } else {
- printf("ok ccmp rx vector\n");
- }
- memcpy(frame, enc, sizeof(frame));
- frame[sizeof(frame) - 1] ^= 1;
- if (ccmp_rx_decrypt(frame, sizeof(frame), 24, tk) >= 0) {
- printf("FAIL ccmp rx bad tag accepted\n");
- failed = 1;
- } else {
- printf("ok ccmp rx bad tag\n");
- }
- /* TX: header + CCMP header + plaintext must encrypt to the
- * same MPDU, MIC included. */
- memcpy(frame, enc, 32);
- memcpy(frame + 32, plain, sizeof(plain));
- if (ccmp_tx_encrypt(frame, 32 + sizeof(plain), 24, tk) < 0 ||
- memcmp(frame, enc, sizeof(enc))) {
- printf("FAIL ccmp tx vector\n");
- failed = 1;
- } else {
- printf("ok ccmp tx vector\n");
- }
- }
- /* RFC 3610 vector #1 through the encrypt direction. */
- {
- unsigned char kk[16], nn[13], aa[8], buf[31], want[31];
- unhex("c0c1c2c3c4c5c6c7c8c9cacbcccdcecf", kk, 16);
- unhex("00000003020100a0a1a2a3a4a5", nn, 13);
- unhex("0001020304050607", aa, 8);
- unhex("08090a0b0c0d0e0f101112131415161718191a1b1c1d1e", buf, 23);
- unhex("588c979a61c663d2f066d0c2c0f989806d5f6b61dac384"
- "17e8d12cfdf926e0", want, 31);
- ccm_encrypt(kk, nn, aa, 8, buf, 23);
- if (memcmp(buf, want, 31)) {
- printf("FAIL ccm encrypt\n");
- failed = 1;
- } else {
- printf("ok ccm encrypt\n");
- }
- }
- return failed;
- }
|