aes-ccm.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Counter with CBC-MAC (CCM) with AES
  3. *
  4. * Copyright (c) 2010-2012, Jouni Malinen <j@w1.fi>
  5. *
  6. * This software may be distributed under the terms of the BSD license.
  7. * See README for more details.
  8. */
  9. #include "includes.h"
  10. #include "common.h"
  11. #include "aes.h"
  12. #include "aes_wrap.h"
  13. static void xor_aes_block(u8 *dst, const u8 *src)
  14. {
  15. u32 *d = (u32 *) dst;
  16. u32 *s = (u32 *) src;
  17. *d++ ^= *s++;
  18. *d++ ^= *s++;
  19. *d++ ^= *s++;
  20. *d++ ^= *s++;
  21. }
  22. static void aes_ccm_auth_start(void *aes, size_t M, size_t L, const u8 *nonce,
  23. const u8 *aad, size_t aad_len, size_t plain_len,
  24. u8 *x)
  25. {
  26. u8 aad_buf[2 * AES_BLOCK_SIZE];
  27. u8 b[AES_BLOCK_SIZE];
  28. /* Authentication */
  29. /* B_0: Flags | Nonce N | l(m) */
  30. b[0] = aad_len ? 0x40 : 0 /* Adata */;
  31. b[0] |= (((M - 2) / 2) /* M' */ << 3);
  32. b[0] |= (L - 1) /* L' */;
  33. os_memcpy(&b[1], nonce, 15 - L);
  34. WPA_PUT_BE16(&b[AES_BLOCK_SIZE - L], plain_len);
  35. wpa_hexdump_key(MSG_EXCESSIVE, "CCM B_0", b, AES_BLOCK_SIZE);
  36. aes_encrypt(aes, b, x); /* X_1 = E(K, B_0) */
  37. if (!aad_len)
  38. return;
  39. WPA_PUT_BE16(aad_buf, aad_len);
  40. os_memcpy(aad_buf + 2, aad, aad_len);
  41. os_memset(aad_buf + 2 + aad_len, 0, sizeof(aad_buf) - 2 - aad_len);
  42. xor_aes_block(aad_buf, x);
  43. aes_encrypt(aes, aad_buf, x); /* X_2 = E(K, X_1 XOR B_1) */
  44. if (aad_len > AES_BLOCK_SIZE - 2) {
  45. xor_aes_block(&aad_buf[AES_BLOCK_SIZE], x);
  46. /* X_3 = E(K, X_2 XOR B_2) */
  47. aes_encrypt(aes, &aad_buf[AES_BLOCK_SIZE], x);
  48. }
  49. }
  50. static void aes_ccm_auth(void *aes, const u8 *data, size_t len, u8 *x)
  51. {
  52. size_t last = len % AES_BLOCK_SIZE;
  53. size_t i;
  54. for (i = 0; i < len / AES_BLOCK_SIZE; i++) {
  55. /* X_i+1 = E(K, X_i XOR B_i) */
  56. xor_aes_block(x, data);
  57. data += AES_BLOCK_SIZE;
  58. aes_encrypt(aes, x, x);
  59. }
  60. if (last) {
  61. /* XOR zero-padded last block */
  62. for (i = 0; i < last; i++)
  63. x[i] ^= *data++;
  64. aes_encrypt(aes, x, x);
  65. }
  66. }
  67. static void aes_ccm_encr_start(size_t L, const u8 *nonce, u8 *a)
  68. {
  69. /* A_i = Flags | Nonce N | Counter i */
  70. a[0] = L - 1; /* Flags = L' */
  71. os_memcpy(&a[1], nonce, 15 - L);
  72. }
  73. static void aes_ccm_encr(void *aes, size_t L, const u8 *in, size_t len, u8 *out,
  74. u8 *a)
  75. {
  76. size_t last = len % AES_BLOCK_SIZE;
  77. size_t i;
  78. /* crypt = msg XOR (S_1 | S_2 | ... | S_n) */
  79. for (i = 1; i <= len / AES_BLOCK_SIZE; i++) {
  80. WPA_PUT_BE16(&a[AES_BLOCK_SIZE - 2], i);
  81. /* S_i = E(K, A_i) */
  82. aes_encrypt(aes, a, out);
  83. xor_aes_block(out, in);
  84. out += AES_BLOCK_SIZE;
  85. in += AES_BLOCK_SIZE;
  86. }
  87. if (last) {
  88. WPA_PUT_BE16(&a[AES_BLOCK_SIZE - 2], i);
  89. aes_encrypt(aes, a, out);
  90. /* XOR zero-padded last block */
  91. for (i = 0; i < last; i++)
  92. *out++ ^= *in++;
  93. }
  94. }
  95. static void aes_ccm_encr_auth(void *aes, size_t M, u8 *x, u8 *a, u8 *auth)
  96. {
  97. size_t i;
  98. u8 tmp[AES_BLOCK_SIZE];
  99. wpa_hexdump_key(MSG_EXCESSIVE, "CCM T", x, M);
  100. /* U = T XOR S_0; S_0 = E(K, A_0) */
  101. WPA_PUT_BE16(&a[AES_BLOCK_SIZE - 2], 0);
  102. aes_encrypt(aes, a, tmp);
  103. for (i = 0; i < M; i++)
  104. auth[i] = x[i] ^ tmp[i];
  105. wpa_hexdump_key(MSG_EXCESSIVE, "CCM U", auth, M);
  106. }
  107. static void aes_ccm_decr_auth(void *aes, size_t M, u8 *a, const u8 *auth, u8 *t)
  108. {
  109. size_t i;
  110. u8 tmp[AES_BLOCK_SIZE];
  111. wpa_hexdump_key(MSG_EXCESSIVE, "CCM U", auth, M);
  112. /* U = T XOR S_0; S_0 = E(K, A_0) */
  113. WPA_PUT_BE16(&a[AES_BLOCK_SIZE - 2], 0);
  114. aes_encrypt(aes, a, tmp);
  115. for (i = 0; i < M; i++)
  116. t[i] = auth[i] ^ tmp[i];
  117. wpa_hexdump_key(MSG_EXCESSIVE, "CCM T", t, M);
  118. }
  119. /* AES-CCM with fixed L=2 and aad_len <= 30 assumption */
  120. int aes_ccm_ae(const u8 *key, size_t key_len, const u8 *nonce,
  121. size_t M, const u8 *plain, size_t plain_len,
  122. const u8 *aad, size_t aad_len, u8 *crypt, u8 *auth)
  123. {
  124. const size_t L = 2;
  125. void *aes;
  126. u8 x[AES_BLOCK_SIZE], a[AES_BLOCK_SIZE];
  127. if (aad_len > 30 || M > AES_BLOCK_SIZE)
  128. return -1;
  129. aes = aes_encrypt_init(key, key_len);
  130. if (aes == NULL)
  131. return -1;
  132. aes_ccm_auth_start(aes, M, L, nonce, aad, aad_len, plain_len, x);
  133. aes_ccm_auth(aes, plain, plain_len, x);
  134. /* Encryption */
  135. aes_ccm_encr_start(L, nonce, a);
  136. aes_ccm_encr(aes, L, plain, plain_len, crypt, a);
  137. aes_ccm_encr_auth(aes, M, x, a, auth);
  138. aes_encrypt_deinit(aes);
  139. return 0;
  140. }
  141. /* AES-CCM with fixed L=2 and aad_len <= 30 assumption */
  142. int aes_ccm_ad(const u8 *key, size_t key_len, const u8 *nonce,
  143. size_t M, const u8 *crypt, size_t crypt_len,
  144. const u8 *aad, size_t aad_len, const u8 *auth, u8 *plain)
  145. {
  146. const size_t L = 2;
  147. void *aes;
  148. u8 x[AES_BLOCK_SIZE], a[AES_BLOCK_SIZE];
  149. u8 t[AES_BLOCK_SIZE];
  150. if (aad_len > 30 || M > AES_BLOCK_SIZE)
  151. return -1;
  152. aes = aes_encrypt_init(key, key_len);
  153. if (aes == NULL)
  154. return -1;
  155. /* Decryption */
  156. aes_ccm_encr_start(L, nonce, a);
  157. aes_ccm_decr_auth(aes, M, a, auth, t);
  158. /* plaintext = msg XOR (S_1 | S_2 | ... | S_n) */
  159. aes_ccm_encr(aes, L, crypt, crypt_len, plain, a);
  160. aes_ccm_auth_start(aes, M, L, nonce, aad, aad_len, crypt_len, x);
  161. aes_ccm_auth(aes, plain, crypt_len, x);
  162. aes_encrypt_deinit(aes);
  163. if (os_memcmp(x, t, M) != 0) {
  164. wpa_printf(MSG_EXCESSIVE, "CCM: Auth mismatch");
  165. return -1;
  166. }
  167. return 0;
  168. }