ccmp_test.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Host vectors for the software CCMP helpers (ccmp.c): FIPS-197 AES-128,
  2. * RFC 3610 CCM packet vector #1 and the 802.11 CCMP MPDU vector, in both
  3. * directions (RX decrypt, TX encrypt for the RTL8821AU). */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "ccmp.c"
  7. static void hex(const unsigned char *p, int n, char *out)
  8. {
  9. int i;
  10. for (i = 0; i < n; i++)
  11. sprintf(out + 2 * i, "%02x", p[i]);
  12. }
  13. static void unhex(const char *s, unsigned char *out, int n)
  14. {
  15. int i;
  16. for (i = 0; i < n; i++) {
  17. unsigned int v;
  18. sscanf(s + 2 * i, "%2x", &v);
  19. out[i] = (unsigned char)v;
  20. }
  21. }
  22. int main(void)
  23. {
  24. unsigned char key[16], pt[16], ct[16], ccmout[23];
  25. char h[64];
  26. int failed = 0;
  27. /* FIPS-197 AES-128 */
  28. unhex("000102030405060708090a0b0c0d0e0f", key, 16);
  29. unhex("00112233445566778899aabbccddeeff", pt, 16);
  30. {
  31. unsigned char rk[176];
  32. aes_setkey(rk, key);
  33. aes_encrypt(rk, pt, ct);
  34. }
  35. hex(ct, 16, h);
  36. if (strcmp(h, "69c4e0d86a7b0430d8cdb78070b4c55a")) {
  37. printf("FAIL aes-128 %s\n", h);
  38. failed = 1;
  39. } else {
  40. printf("ok aes-128\n");
  41. }
  42. /* RFC 3610 packet vector #1 (L=2, 13-byte nonce, M=8) */
  43. {
  44. const char *k = "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf";
  45. const char *n = "00000003020100a0a1a2a3a4a5";
  46. const char *a = "0001020304050607";
  47. const char *p = "08090a0b0c0d0e0f101112131415161718191a1b1c1d1e";
  48. const char *c = "588c979a61c663d2f066d0c2c0f989806d5f6b61dac384";
  49. const char *t = "17e8d12cfdf926e0";
  50. unsigned char kk[16], nn[13], aa[8], pp[23], cc[23], tt[8];
  51. unhex(k, kk, 16);
  52. unhex(n, nn, 13);
  53. unhex(a, aa, 8);
  54. unhex(p, pp, 23);
  55. unhex(c, cc, 23);
  56. unhex(t, tt, 8);
  57. memcpy(ccmout, cc, 23);
  58. if (ccm_decrypt(kk, nn, aa, 8, ccmout, 23, tt) < 0 ||
  59. memcmp(ccmout, pp, 23)) {
  60. printf("FAIL ccm\n");
  61. failed = 1;
  62. } else {
  63. printf("ok ccm\n");
  64. }
  65. memcpy(ccmout, cc, 23);
  66. tt[0] ^= 1;
  67. if (ccm_decrypt(kk, nn, aa, 8, ccmout, 23, tt) >= 0) {
  68. printf("FAIL ccm bad tag accepted\n");
  69. failed = 1;
  70. } else {
  71. printf("ok ccm bad tag\n");
  72. }
  73. }
  74. /* 802.11 CCMP test vector (P802.11i/D7.0 I.7.4, MPDU 1). The header
  75. * has Retry set and a nonzero sequence number, exercising the AAD
  76. * masking and the PN byte order. */
  77. {
  78. static const unsigned char enc[] = {
  79. 0x08, 0x48, 0xc3, 0x2c, 0x0f, 0xd2, 0xe1, 0x28,
  80. 0xa5, 0x7c, 0x50, 0x30, 0xf1, 0x84, 0x44, 0x08,
  81. 0xab, 0xae, 0xa5, 0xb8, 0xfc, 0xba, 0x80, 0x33,
  82. 0x0c, 0xe7, 0x00, 0x20, 0x76, 0x97, 0x03, 0xb5,
  83. 0xf3, 0xd0, 0xa2, 0xfe, 0x9a, 0x3d, 0xbf, 0x23,
  84. 0x42, 0xa6, 0x43, 0xe4, 0x32, 0x46, 0xe8, 0x0c,
  85. 0x3c, 0x04, 0xd0, 0x19, 0x78, 0x45, 0xce, 0x0b,
  86. 0x16, 0xf9, 0x76, 0x23,
  87. };
  88. static const unsigned char tk[16] = {
  89. 0xc9, 0x7c, 0x1f, 0x67, 0xce, 0x37, 0x11, 0x85,
  90. 0x51, 0x4a, 0x8a, 0x19, 0xf2, 0xbd, 0xd5, 0x2f,
  91. };
  92. static const unsigned char plain[20] = {
  93. 0xf8, 0xba, 0x1a, 0x55, 0xd0, 0x2f, 0x85, 0xae,
  94. 0x96, 0x7b, 0xb6, 0x2f, 0xb6, 0xcd, 0xa8, 0xeb,
  95. 0x7e, 0x78, 0xa0, 0x50,
  96. };
  97. unsigned char frame[sizeof(enc)];
  98. memcpy(frame, enc, sizeof(frame));
  99. if (ccmp_rx_decrypt(frame, sizeof(frame), 24, tk) < 0 ||
  100. memcmp(frame + 32, plain, sizeof(plain))) {
  101. printf("FAIL ccmp rx vector\n");
  102. failed = 1;
  103. } else {
  104. printf("ok ccmp rx vector\n");
  105. }
  106. memcpy(frame, enc, sizeof(frame));
  107. frame[sizeof(frame) - 1] ^= 1;
  108. if (ccmp_rx_decrypt(frame, sizeof(frame), 24, tk) >= 0) {
  109. printf("FAIL ccmp rx bad tag accepted\n");
  110. failed = 1;
  111. } else {
  112. printf("ok ccmp rx bad tag\n");
  113. }
  114. /* TX: header + CCMP header + plaintext must encrypt to the
  115. * same MPDU, MIC included. */
  116. memcpy(frame, enc, 32);
  117. memcpy(frame + 32, plain, sizeof(plain));
  118. if (ccmp_tx_encrypt(frame, 32 + sizeof(plain), 24, tk) < 0 ||
  119. memcmp(frame, enc, sizeof(enc))) {
  120. printf("FAIL ccmp tx vector\n");
  121. failed = 1;
  122. } else {
  123. printf("ok ccmp tx vector\n");
  124. }
  125. }
  126. /* RFC 3610 vector #1 through the encrypt direction. */
  127. {
  128. unsigned char kk[16], nn[13], aa[8], buf[31], want[31];
  129. unhex("c0c1c2c3c4c5c6c7c8c9cacbcccdcecf", kk, 16);
  130. unhex("00000003020100a0a1a2a3a4a5", nn, 13);
  131. unhex("0001020304050607", aa, 8);
  132. unhex("08090a0b0c0d0e0f101112131415161718191a1b1c1d1e", buf, 23);
  133. unhex("588c979a61c663d2f066d0c2c0f989806d5f6b61dac384"
  134. "17e8d12cfdf926e0", want, 31);
  135. ccm_encrypt(kk, nn, aa, 8, buf, 23);
  136. if (memcmp(buf, want, 31)) {
  137. printf("FAIL ccm encrypt\n");
  138. failed = 1;
  139. } else {
  140. printf("ok ccm encrypt\n");
  141. }
  142. }
  143. return failed;
  144. }