aes-gcm.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Galois/Counter Mode (GCM) and GMAC with AES
  3. *
  4. * Copyright (c) 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 inc32(u8 *block)
  14. {
  15. u32 val;
  16. val = WPA_GET_BE32(block + AES_BLOCK_SIZE - 4);
  17. val++;
  18. WPA_PUT_BE32(block + AES_BLOCK_SIZE - 4, val);
  19. }
  20. static void xor_block(u8 *dst, const u8 *src)
  21. {
  22. u32 *d = (u32 *) dst;
  23. u32 *s = (u32 *) src;
  24. *d++ ^= *s++;
  25. *d++ ^= *s++;
  26. *d++ ^= *s++;
  27. *d++ ^= *s++;
  28. }
  29. static void shift_right_block(u8 *v)
  30. {
  31. u32 val;
  32. val = WPA_GET_BE32(v + 12);
  33. val >>= 1;
  34. if (v[11] & 0x01)
  35. val |= 0x80000000;
  36. WPA_PUT_BE32(v + 12, val);
  37. val = WPA_GET_BE32(v + 8);
  38. val >>= 1;
  39. if (v[7] & 0x01)
  40. val |= 0x80000000;
  41. WPA_PUT_BE32(v + 8, val);
  42. val = WPA_GET_BE32(v + 4);
  43. val >>= 1;
  44. if (v[3] & 0x01)
  45. val |= 0x80000000;
  46. WPA_PUT_BE32(v + 4, val);
  47. val = WPA_GET_BE32(v);
  48. val >>= 1;
  49. WPA_PUT_BE32(v, val);
  50. }
  51. /* Multiplication in GF(2^128) */
  52. static void gf_mult(const u8 *x, const u8 *y, u8 *z)
  53. {
  54. u8 v[16];
  55. int i, j;
  56. os_memset(z, 0, 16); /* Z_0 = 0^128 */
  57. os_memcpy(v, y, 16); /* V_0 = Y */
  58. for (i = 0; i < 16; i++) {
  59. for (j = 0; j < 8; j++) {
  60. if (x[i] & BIT(7 - j)) {
  61. /* Z_(i + 1) = Z_i XOR V_i */
  62. xor_block(z, v);
  63. } else {
  64. /* Z_(i + 1) = Z_i */
  65. }
  66. if (v[15] & 0x01) {
  67. /* V_(i + 1) = (V_i >> 1) XOR R */
  68. shift_right_block(v);
  69. /* R = 11100001 || 0^120 */
  70. v[0] ^= 0xe1;
  71. } else {
  72. /* V_(i + 1) = V_i >> 1 */
  73. shift_right_block(v);
  74. }
  75. }
  76. }
  77. }
  78. static void ghash_start(u8 *y)
  79. {
  80. /* Y_0 = 0^128 */
  81. os_memset(y, 0, 16);
  82. }
  83. static void ghash(const u8 *h, const u8 *x, size_t xlen, u8 *y)
  84. {
  85. size_t m, i;
  86. const u8 *xpos = x;
  87. u8 tmp[16];
  88. m = xlen / 16;
  89. for (i = 0; i < m; i++) {
  90. /* Y_i = (Y^(i-1) XOR X_i) dot H */
  91. xor_block(y, xpos);
  92. xpos += 16;
  93. /* dot operation:
  94. * multiplication operation for binary Galois (finite) field of
  95. * 2^128 elements */
  96. gf_mult(y, h, tmp);
  97. os_memcpy(y, tmp, 16);
  98. }
  99. if (x + xlen > xpos) {
  100. /* Add zero padded last block */
  101. size_t last = x + xlen - xpos;
  102. os_memcpy(tmp, xpos, last);
  103. os_memset(tmp + last, 0, sizeof(tmp) - last);
  104. /* Y_i = (Y^(i-1) XOR X_i) dot H */
  105. xor_block(y, tmp);
  106. /* dot operation:
  107. * multiplication operation for binary Galois (finite) field of
  108. * 2^128 elements */
  109. gf_mult(y, h, tmp);
  110. os_memcpy(y, tmp, 16);
  111. }
  112. /* Return Y_m */
  113. }
  114. static void aes_gctr(void *aes, const u8 *icb, const u8 *x, size_t xlen, u8 *y)
  115. {
  116. size_t i, n, last;
  117. u8 cb[AES_BLOCK_SIZE], tmp[AES_BLOCK_SIZE];
  118. const u8 *xpos = x;
  119. u8 *ypos = y;
  120. if (xlen == 0)
  121. return;
  122. n = xlen / 16;
  123. os_memcpy(cb, icb, AES_BLOCK_SIZE);
  124. /* Full blocks */
  125. for (i = 0; i < n; i++) {
  126. aes_encrypt(aes, cb, ypos);
  127. xor_block(ypos, xpos);
  128. xpos += AES_BLOCK_SIZE;
  129. ypos += AES_BLOCK_SIZE;
  130. inc32(cb);
  131. }
  132. last = x + xlen - xpos;
  133. if (last) {
  134. /* Last, partial block */
  135. aes_encrypt(aes, cb, tmp);
  136. for (i = 0; i < last; i++)
  137. *ypos++ = *xpos++ ^ tmp[i];
  138. }
  139. }
  140. static void * aes_gcm_init_hash_subkey(const u8 *key, size_t key_len, u8 *H)
  141. {
  142. void *aes;
  143. aes = aes_encrypt_init(key, key_len);
  144. if (aes == NULL)
  145. return NULL;
  146. /* Generate hash subkey H = AES_K(0^128) */
  147. os_memset(H, 0, AES_BLOCK_SIZE);
  148. aes_encrypt(aes, H, H);
  149. wpa_hexdump_key(MSG_EXCESSIVE, "Hash subkey H for GHASH",
  150. H, AES_BLOCK_SIZE);
  151. return aes;
  152. }
  153. static void aes_gcm_prepare_j0(const u8 *iv, size_t iv_len, const u8 *H, u8 *J0)
  154. {
  155. u8 len_buf[16];
  156. if (iv_len == 12) {
  157. /* Prepare block J_0 = IV || 0^31 || 1 [len(IV) = 96] */
  158. os_memcpy(J0, iv, iv_len);
  159. os_memset(J0 + iv_len, 0, AES_BLOCK_SIZE - iv_len);
  160. J0[AES_BLOCK_SIZE - 1] = 0x01;
  161. } else {
  162. /*
  163. * s = 128 * ceil(len(IV)/128) - len(IV)
  164. * J_0 = GHASH_H(IV || 0^(s+64) || [len(IV)]_64)
  165. */
  166. ghash_start(J0);
  167. ghash(H, iv, iv_len, J0);
  168. WPA_PUT_BE64(len_buf, 0);
  169. WPA_PUT_BE64(len_buf + 8, iv_len * 8);
  170. ghash(H, len_buf, sizeof(len_buf), J0);
  171. }
  172. }
  173. static void aes_gcm_gctr(void *aes, const u8 *J0, const u8 *in, size_t len,
  174. u8 *out)
  175. {
  176. u8 J0inc[AES_BLOCK_SIZE];
  177. if (len == 0)
  178. return;
  179. os_memcpy(J0inc, J0, AES_BLOCK_SIZE);
  180. inc32(J0inc);
  181. aes_gctr(aes, J0inc, in, len, out);
  182. }
  183. static void aes_gcm_ghash(const u8 *H, const u8 *aad, size_t aad_len,
  184. const u8 *crypt, size_t crypt_len, u8 *S)
  185. {
  186. u8 len_buf[16];
  187. /*
  188. * u = 128 * ceil[len(C)/128] - len(C)
  189. * v = 128 * ceil[len(A)/128] - len(A)
  190. * S = GHASH_H(A || 0^v || C || 0^u || [len(A)]64 || [len(C)]64)
  191. * (i.e., zero padded to block size A || C and lengths of each in bits)
  192. */
  193. ghash_start(S);
  194. ghash(H, aad, aad_len, S);
  195. ghash(H, crypt, crypt_len, S);
  196. WPA_PUT_BE64(len_buf, aad_len * 8);
  197. WPA_PUT_BE64(len_buf + 8, crypt_len * 8);
  198. ghash(H, len_buf, sizeof(len_buf), S);
  199. wpa_hexdump_key(MSG_EXCESSIVE, "S = GHASH_H(...)", S, 16);
  200. }
  201. /**
  202. * aes_gcm_ae - GCM-AE_K(IV, P, A)
  203. */
  204. int aes_gcm_ae(const u8 *key, size_t key_len, const u8 *iv, size_t iv_len,
  205. const u8 *plain, size_t plain_len,
  206. const u8 *aad, size_t aad_len, u8 *crypt, u8 *tag)
  207. {
  208. u8 H[AES_BLOCK_SIZE];
  209. u8 J0[AES_BLOCK_SIZE];
  210. u8 S[16];
  211. void *aes;
  212. aes = aes_gcm_init_hash_subkey(key, key_len, H);
  213. if (aes == NULL)
  214. return -1;
  215. aes_gcm_prepare_j0(iv, iv_len, H, J0);
  216. /* C = GCTR_K(inc_32(J_0), P) */
  217. aes_gcm_gctr(aes, J0, plain, plain_len, crypt);
  218. aes_gcm_ghash(H, aad, aad_len, crypt, plain_len, S);
  219. /* T = MSB_t(GCTR_K(J_0, S)) */
  220. aes_gctr(aes, J0, S, sizeof(S), tag);
  221. /* Return (C, T) */
  222. aes_encrypt_deinit(aes);
  223. return 0;
  224. }
  225. /**
  226. * aes_gcm_ad - GCM-AD_K(IV, C, A, T)
  227. */
  228. int aes_gcm_ad(const u8 *key, size_t key_len, const u8 *iv, size_t iv_len,
  229. const u8 *crypt, size_t crypt_len,
  230. const u8 *aad, size_t aad_len, const u8 *tag, u8 *plain)
  231. {
  232. u8 H[AES_BLOCK_SIZE];
  233. u8 J0[AES_BLOCK_SIZE];
  234. u8 S[16], T[16];
  235. void *aes;
  236. aes = aes_gcm_init_hash_subkey(key, key_len, H);
  237. if (aes == NULL)
  238. return -1;
  239. aes_gcm_prepare_j0(iv, iv_len, H, J0);
  240. /* P = GCTR_K(inc_32(J_0), C) */
  241. aes_gcm_gctr(aes, J0, crypt, crypt_len, plain);
  242. aes_gcm_ghash(H, aad, aad_len, crypt, crypt_len, S);
  243. /* T' = MSB_t(GCTR_K(J_0, S)) */
  244. aes_gctr(aes, J0, S, sizeof(S), T);
  245. aes_encrypt_deinit(aes);
  246. if (os_memcmp_const(tag, T, 16) != 0) {
  247. wpa_printf(MSG_EXCESSIVE, "GCM: Tag mismatch");
  248. return -1;
  249. }
  250. return 0;
  251. }
  252. int aes_gmac(const u8 *key, size_t key_len, const u8 *iv, size_t iv_len,
  253. const u8 *aad, size_t aad_len, u8 *tag)
  254. {
  255. return aes_gcm_ae(key, key_len, iv, iv_len, NULL, 0, aad, aad_len, NULL,
  256. tag);
  257. }