ccmp_test.c 3.3 KB

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