aesni-intel_glue.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  1. /*
  2. * Support for Intel AES-NI instructions. This file contains glue
  3. * code, the real AES implementation is in intel-aes_asm.S.
  4. *
  5. * Copyright (C) 2008, Intel Corp.
  6. * Author: Huang Ying <ying.huang@intel.com>
  7. *
  8. * Added RFC4106 AES-GCM support for 128-bit keys under the AEAD
  9. * interface for 64-bit kernels.
  10. * Authors: Adrian Hoban <adrian.hoban@intel.com>
  11. * Gabriele Paoloni <gabriele.paoloni@intel.com>
  12. * Tadeusz Struk (tadeusz.struk@intel.com)
  13. * Aidan O'Mahony (aidan.o.mahony@intel.com)
  14. * Copyright (c) 2010, Intel Corporation.
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. */
  21. #include <linux/hardirq.h>
  22. #include <linux/types.h>
  23. #include <linux/crypto.h>
  24. #include <linux/module.h>
  25. #include <linux/err.h>
  26. #include <crypto/algapi.h>
  27. #include <crypto/aes.h>
  28. #include <crypto/cryptd.h>
  29. #include <crypto/ctr.h>
  30. #include <crypto/b128ops.h>
  31. #include <crypto/lrw.h>
  32. #include <crypto/xts.h>
  33. #include <asm/cpu_device_id.h>
  34. #include <asm/fpu/api.h>
  35. #include <asm/crypto/aes.h>
  36. #include <crypto/ablk_helper.h>
  37. #include <crypto/scatterwalk.h>
  38. #include <crypto/internal/aead.h>
  39. #include <linux/workqueue.h>
  40. #include <linux/spinlock.h>
  41. #ifdef CONFIG_X86_64
  42. #include <asm/crypto/glue_helper.h>
  43. #endif
  44. #define AESNI_ALIGN 16
  45. #define AES_BLOCK_MASK (~(AES_BLOCK_SIZE - 1))
  46. #define RFC4106_HASH_SUBKEY_SIZE 16
  47. /* This data is stored at the end of the crypto_tfm struct.
  48. * It's a type of per "session" data storage location.
  49. * This needs to be 16 byte aligned.
  50. */
  51. struct aesni_rfc4106_gcm_ctx {
  52. u8 hash_subkey[16] __attribute__ ((__aligned__(AESNI_ALIGN)));
  53. struct crypto_aes_ctx aes_key_expanded
  54. __attribute__ ((__aligned__(AESNI_ALIGN)));
  55. u8 nonce[4];
  56. };
  57. struct aesni_gcm_set_hash_subkey_result {
  58. int err;
  59. struct completion completion;
  60. };
  61. struct aesni_hash_subkey_req_data {
  62. u8 iv[16];
  63. struct aesni_gcm_set_hash_subkey_result result;
  64. struct scatterlist sg;
  65. };
  66. struct aesni_lrw_ctx {
  67. struct lrw_table_ctx lrw_table;
  68. u8 raw_aes_ctx[sizeof(struct crypto_aes_ctx) + AESNI_ALIGN - 1];
  69. };
  70. struct aesni_xts_ctx {
  71. u8 raw_tweak_ctx[sizeof(struct crypto_aes_ctx) + AESNI_ALIGN - 1];
  72. u8 raw_crypt_ctx[sizeof(struct crypto_aes_ctx) + AESNI_ALIGN - 1];
  73. };
  74. asmlinkage int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key,
  75. unsigned int key_len);
  76. asmlinkage void aesni_enc(struct crypto_aes_ctx *ctx, u8 *out,
  77. const u8 *in);
  78. asmlinkage void aesni_dec(struct crypto_aes_ctx *ctx, u8 *out,
  79. const u8 *in);
  80. asmlinkage void aesni_ecb_enc(struct crypto_aes_ctx *ctx, u8 *out,
  81. const u8 *in, unsigned int len);
  82. asmlinkage void aesni_ecb_dec(struct crypto_aes_ctx *ctx, u8 *out,
  83. const u8 *in, unsigned int len);
  84. asmlinkage void aesni_cbc_enc(struct crypto_aes_ctx *ctx, u8 *out,
  85. const u8 *in, unsigned int len, u8 *iv);
  86. asmlinkage void aesni_cbc_dec(struct crypto_aes_ctx *ctx, u8 *out,
  87. const u8 *in, unsigned int len, u8 *iv);
  88. int crypto_fpu_init(void);
  89. void crypto_fpu_exit(void);
  90. #define AVX_GEN2_OPTSIZE 640
  91. #define AVX_GEN4_OPTSIZE 4096
  92. #ifdef CONFIG_X86_64
  93. static void (*aesni_ctr_enc_tfm)(struct crypto_aes_ctx *ctx, u8 *out,
  94. const u8 *in, unsigned int len, u8 *iv);
  95. asmlinkage void aesni_ctr_enc(struct crypto_aes_ctx *ctx, u8 *out,
  96. const u8 *in, unsigned int len, u8 *iv);
  97. asmlinkage void aesni_xts_crypt8(struct crypto_aes_ctx *ctx, u8 *out,
  98. const u8 *in, bool enc, u8 *iv);
  99. /* asmlinkage void aesni_gcm_enc()
  100. * void *ctx, AES Key schedule. Starts on a 16 byte boundary.
  101. * u8 *out, Ciphertext output. Encrypt in-place is allowed.
  102. * const u8 *in, Plaintext input
  103. * unsigned long plaintext_len, Length of data in bytes for encryption.
  104. * u8 *iv, Pre-counter block j0: 4 byte salt (from Security Association)
  105. * concatenated with 8 byte Initialisation Vector (from IPSec ESP
  106. * Payload) concatenated with 0x00000001. 16-byte aligned pointer.
  107. * u8 *hash_subkey, the Hash sub key input. Data starts on a 16-byte boundary.
  108. * const u8 *aad, Additional Authentication Data (AAD)
  109. * unsigned long aad_len, Length of AAD in bytes. With RFC4106 this
  110. * is going to be 8 or 12 bytes
  111. * u8 *auth_tag, Authenticated Tag output.
  112. * unsigned long auth_tag_len), Authenticated Tag Length in bytes.
  113. * Valid values are 16 (most likely), 12 or 8.
  114. */
  115. asmlinkage void aesni_gcm_enc(void *ctx, u8 *out,
  116. const u8 *in, unsigned long plaintext_len, u8 *iv,
  117. u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
  118. u8 *auth_tag, unsigned long auth_tag_len);
  119. /* asmlinkage void aesni_gcm_dec()
  120. * void *ctx, AES Key schedule. Starts on a 16 byte boundary.
  121. * u8 *out, Plaintext output. Decrypt in-place is allowed.
  122. * const u8 *in, Ciphertext input
  123. * unsigned long ciphertext_len, Length of data in bytes for decryption.
  124. * u8 *iv, Pre-counter block j0: 4 byte salt (from Security Association)
  125. * concatenated with 8 byte Initialisation Vector (from IPSec ESP
  126. * Payload) concatenated with 0x00000001. 16-byte aligned pointer.
  127. * u8 *hash_subkey, the Hash sub key input. Data starts on a 16-byte boundary.
  128. * const u8 *aad, Additional Authentication Data (AAD)
  129. * unsigned long aad_len, Length of AAD in bytes. With RFC4106 this is going
  130. * to be 8 or 12 bytes
  131. * u8 *auth_tag, Authenticated Tag output.
  132. * unsigned long auth_tag_len) Authenticated Tag Length in bytes.
  133. * Valid values are 16 (most likely), 12 or 8.
  134. */
  135. asmlinkage void aesni_gcm_dec(void *ctx, u8 *out,
  136. const u8 *in, unsigned long ciphertext_len, u8 *iv,
  137. u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
  138. u8 *auth_tag, unsigned long auth_tag_len);
  139. #ifdef CONFIG_AS_AVX
  140. asmlinkage void aes_ctr_enc_128_avx_by8(const u8 *in, u8 *iv,
  141. void *keys, u8 *out, unsigned int num_bytes);
  142. asmlinkage void aes_ctr_enc_192_avx_by8(const u8 *in, u8 *iv,
  143. void *keys, u8 *out, unsigned int num_bytes);
  144. asmlinkage void aes_ctr_enc_256_avx_by8(const u8 *in, u8 *iv,
  145. void *keys, u8 *out, unsigned int num_bytes);
  146. /*
  147. * asmlinkage void aesni_gcm_precomp_avx_gen2()
  148. * gcm_data *my_ctx_data, context data
  149. * u8 *hash_subkey, the Hash sub key input. Data starts on a 16-byte boundary.
  150. */
  151. asmlinkage void aesni_gcm_precomp_avx_gen2(void *my_ctx_data, u8 *hash_subkey);
  152. asmlinkage void aesni_gcm_enc_avx_gen2(void *ctx, u8 *out,
  153. const u8 *in, unsigned long plaintext_len, u8 *iv,
  154. const u8 *aad, unsigned long aad_len,
  155. u8 *auth_tag, unsigned long auth_tag_len);
  156. asmlinkage void aesni_gcm_dec_avx_gen2(void *ctx, u8 *out,
  157. const u8 *in, unsigned long ciphertext_len, u8 *iv,
  158. const u8 *aad, unsigned long aad_len,
  159. u8 *auth_tag, unsigned long auth_tag_len);
  160. static void aesni_gcm_enc_avx(void *ctx, u8 *out,
  161. const u8 *in, unsigned long plaintext_len, u8 *iv,
  162. u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
  163. u8 *auth_tag, unsigned long auth_tag_len)
  164. {
  165. struct crypto_aes_ctx *aes_ctx = (struct crypto_aes_ctx*)ctx;
  166. if ((plaintext_len < AVX_GEN2_OPTSIZE) || (aes_ctx-> key_length != AES_KEYSIZE_128)){
  167. aesni_gcm_enc(ctx, out, in, plaintext_len, iv, hash_subkey, aad,
  168. aad_len, auth_tag, auth_tag_len);
  169. } else {
  170. aesni_gcm_precomp_avx_gen2(ctx, hash_subkey);
  171. aesni_gcm_enc_avx_gen2(ctx, out, in, plaintext_len, iv, aad,
  172. aad_len, auth_tag, auth_tag_len);
  173. }
  174. }
  175. static void aesni_gcm_dec_avx(void *ctx, u8 *out,
  176. const u8 *in, unsigned long ciphertext_len, u8 *iv,
  177. u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
  178. u8 *auth_tag, unsigned long auth_tag_len)
  179. {
  180. struct crypto_aes_ctx *aes_ctx = (struct crypto_aes_ctx*)ctx;
  181. if ((ciphertext_len < AVX_GEN2_OPTSIZE) || (aes_ctx-> key_length != AES_KEYSIZE_128)) {
  182. aesni_gcm_dec(ctx, out, in, ciphertext_len, iv, hash_subkey, aad,
  183. aad_len, auth_tag, auth_tag_len);
  184. } else {
  185. aesni_gcm_precomp_avx_gen2(ctx, hash_subkey);
  186. aesni_gcm_dec_avx_gen2(ctx, out, in, ciphertext_len, iv, aad,
  187. aad_len, auth_tag, auth_tag_len);
  188. }
  189. }
  190. #endif
  191. #ifdef CONFIG_AS_AVX2
  192. /*
  193. * asmlinkage void aesni_gcm_precomp_avx_gen4()
  194. * gcm_data *my_ctx_data, context data
  195. * u8 *hash_subkey, the Hash sub key input. Data starts on a 16-byte boundary.
  196. */
  197. asmlinkage void aesni_gcm_precomp_avx_gen4(void *my_ctx_data, u8 *hash_subkey);
  198. asmlinkage void aesni_gcm_enc_avx_gen4(void *ctx, u8 *out,
  199. const u8 *in, unsigned long plaintext_len, u8 *iv,
  200. const u8 *aad, unsigned long aad_len,
  201. u8 *auth_tag, unsigned long auth_tag_len);
  202. asmlinkage void aesni_gcm_dec_avx_gen4(void *ctx, u8 *out,
  203. const u8 *in, unsigned long ciphertext_len, u8 *iv,
  204. const u8 *aad, unsigned long aad_len,
  205. u8 *auth_tag, unsigned long auth_tag_len);
  206. static void aesni_gcm_enc_avx2(void *ctx, u8 *out,
  207. const u8 *in, unsigned long plaintext_len, u8 *iv,
  208. u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
  209. u8 *auth_tag, unsigned long auth_tag_len)
  210. {
  211. struct crypto_aes_ctx *aes_ctx = (struct crypto_aes_ctx*)ctx;
  212. if ((plaintext_len < AVX_GEN2_OPTSIZE) || (aes_ctx-> key_length != AES_KEYSIZE_128)) {
  213. aesni_gcm_enc(ctx, out, in, plaintext_len, iv, hash_subkey, aad,
  214. aad_len, auth_tag, auth_tag_len);
  215. } else if (plaintext_len < AVX_GEN4_OPTSIZE) {
  216. aesni_gcm_precomp_avx_gen2(ctx, hash_subkey);
  217. aesni_gcm_enc_avx_gen2(ctx, out, in, plaintext_len, iv, aad,
  218. aad_len, auth_tag, auth_tag_len);
  219. } else {
  220. aesni_gcm_precomp_avx_gen4(ctx, hash_subkey);
  221. aesni_gcm_enc_avx_gen4(ctx, out, in, plaintext_len, iv, aad,
  222. aad_len, auth_tag, auth_tag_len);
  223. }
  224. }
  225. static void aesni_gcm_dec_avx2(void *ctx, u8 *out,
  226. const u8 *in, unsigned long ciphertext_len, u8 *iv,
  227. u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
  228. u8 *auth_tag, unsigned long auth_tag_len)
  229. {
  230. struct crypto_aes_ctx *aes_ctx = (struct crypto_aes_ctx*)ctx;
  231. if ((ciphertext_len < AVX_GEN2_OPTSIZE) || (aes_ctx-> key_length != AES_KEYSIZE_128)) {
  232. aesni_gcm_dec(ctx, out, in, ciphertext_len, iv, hash_subkey,
  233. aad, aad_len, auth_tag, auth_tag_len);
  234. } else if (ciphertext_len < AVX_GEN4_OPTSIZE) {
  235. aesni_gcm_precomp_avx_gen2(ctx, hash_subkey);
  236. aesni_gcm_dec_avx_gen2(ctx, out, in, ciphertext_len, iv, aad,
  237. aad_len, auth_tag, auth_tag_len);
  238. } else {
  239. aesni_gcm_precomp_avx_gen4(ctx, hash_subkey);
  240. aesni_gcm_dec_avx_gen4(ctx, out, in, ciphertext_len, iv, aad,
  241. aad_len, auth_tag, auth_tag_len);
  242. }
  243. }
  244. #endif
  245. static void (*aesni_gcm_enc_tfm)(void *ctx, u8 *out,
  246. const u8 *in, unsigned long plaintext_len, u8 *iv,
  247. u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
  248. u8 *auth_tag, unsigned long auth_tag_len);
  249. static void (*aesni_gcm_dec_tfm)(void *ctx, u8 *out,
  250. const u8 *in, unsigned long ciphertext_len, u8 *iv,
  251. u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
  252. u8 *auth_tag, unsigned long auth_tag_len);
  253. static inline struct
  254. aesni_rfc4106_gcm_ctx *aesni_rfc4106_gcm_ctx_get(struct crypto_aead *tfm)
  255. {
  256. unsigned long align = AESNI_ALIGN;
  257. if (align <= crypto_tfm_ctx_alignment())
  258. align = 1;
  259. return PTR_ALIGN(crypto_aead_ctx(tfm), align);
  260. }
  261. #endif
  262. static inline struct crypto_aes_ctx *aes_ctx(void *raw_ctx)
  263. {
  264. unsigned long addr = (unsigned long)raw_ctx;
  265. unsigned long align = AESNI_ALIGN;
  266. if (align <= crypto_tfm_ctx_alignment())
  267. align = 1;
  268. return (struct crypto_aes_ctx *)ALIGN(addr, align);
  269. }
  270. static int aes_set_key_common(struct crypto_tfm *tfm, void *raw_ctx,
  271. const u8 *in_key, unsigned int key_len)
  272. {
  273. struct crypto_aes_ctx *ctx = aes_ctx(raw_ctx);
  274. u32 *flags = &tfm->crt_flags;
  275. int err;
  276. if (key_len != AES_KEYSIZE_128 && key_len != AES_KEYSIZE_192 &&
  277. key_len != AES_KEYSIZE_256) {
  278. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  279. return -EINVAL;
  280. }
  281. if (!irq_fpu_usable())
  282. err = crypto_aes_expand_key(ctx, in_key, key_len);
  283. else {
  284. kernel_fpu_begin();
  285. err = aesni_set_key(ctx, in_key, key_len);
  286. kernel_fpu_end();
  287. }
  288. return err;
  289. }
  290. static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  291. unsigned int key_len)
  292. {
  293. return aes_set_key_common(tfm, crypto_tfm_ctx(tfm), in_key, key_len);
  294. }
  295. static void aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  296. {
  297. struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
  298. if (!irq_fpu_usable())
  299. crypto_aes_encrypt_x86(ctx, dst, src);
  300. else {
  301. kernel_fpu_begin();
  302. aesni_enc(ctx, dst, src);
  303. kernel_fpu_end();
  304. }
  305. }
  306. static void aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  307. {
  308. struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
  309. if (!irq_fpu_usable())
  310. crypto_aes_decrypt_x86(ctx, dst, src);
  311. else {
  312. kernel_fpu_begin();
  313. aesni_dec(ctx, dst, src);
  314. kernel_fpu_end();
  315. }
  316. }
  317. static void __aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  318. {
  319. struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
  320. aesni_enc(ctx, dst, src);
  321. }
  322. static void __aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  323. {
  324. struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
  325. aesni_dec(ctx, dst, src);
  326. }
  327. static int ecb_encrypt(struct blkcipher_desc *desc,
  328. struct scatterlist *dst, struct scatterlist *src,
  329. unsigned int nbytes)
  330. {
  331. struct crypto_aes_ctx *ctx = aes_ctx(crypto_blkcipher_ctx(desc->tfm));
  332. struct blkcipher_walk walk;
  333. int err;
  334. blkcipher_walk_init(&walk, dst, src, nbytes);
  335. err = blkcipher_walk_virt(desc, &walk);
  336. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  337. kernel_fpu_begin();
  338. while ((nbytes = walk.nbytes)) {
  339. aesni_ecb_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  340. nbytes & AES_BLOCK_MASK);
  341. nbytes &= AES_BLOCK_SIZE - 1;
  342. err = blkcipher_walk_done(desc, &walk, nbytes);
  343. }
  344. kernel_fpu_end();
  345. return err;
  346. }
  347. static int ecb_decrypt(struct blkcipher_desc *desc,
  348. struct scatterlist *dst, struct scatterlist *src,
  349. unsigned int nbytes)
  350. {
  351. struct crypto_aes_ctx *ctx = aes_ctx(crypto_blkcipher_ctx(desc->tfm));
  352. struct blkcipher_walk walk;
  353. int err;
  354. blkcipher_walk_init(&walk, dst, src, nbytes);
  355. err = blkcipher_walk_virt(desc, &walk);
  356. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  357. kernel_fpu_begin();
  358. while ((nbytes = walk.nbytes)) {
  359. aesni_ecb_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  360. nbytes & AES_BLOCK_MASK);
  361. nbytes &= AES_BLOCK_SIZE - 1;
  362. err = blkcipher_walk_done(desc, &walk, nbytes);
  363. }
  364. kernel_fpu_end();
  365. return err;
  366. }
  367. static int cbc_encrypt(struct blkcipher_desc *desc,
  368. struct scatterlist *dst, struct scatterlist *src,
  369. unsigned int nbytes)
  370. {
  371. struct crypto_aes_ctx *ctx = aes_ctx(crypto_blkcipher_ctx(desc->tfm));
  372. struct blkcipher_walk walk;
  373. int err;
  374. blkcipher_walk_init(&walk, dst, src, nbytes);
  375. err = blkcipher_walk_virt(desc, &walk);
  376. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  377. kernel_fpu_begin();
  378. while ((nbytes = walk.nbytes)) {
  379. aesni_cbc_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  380. nbytes & AES_BLOCK_MASK, walk.iv);
  381. nbytes &= AES_BLOCK_SIZE - 1;
  382. err = blkcipher_walk_done(desc, &walk, nbytes);
  383. }
  384. kernel_fpu_end();
  385. return err;
  386. }
  387. static int cbc_decrypt(struct blkcipher_desc *desc,
  388. struct scatterlist *dst, struct scatterlist *src,
  389. unsigned int nbytes)
  390. {
  391. struct crypto_aes_ctx *ctx = aes_ctx(crypto_blkcipher_ctx(desc->tfm));
  392. struct blkcipher_walk walk;
  393. int err;
  394. blkcipher_walk_init(&walk, dst, src, nbytes);
  395. err = blkcipher_walk_virt(desc, &walk);
  396. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  397. kernel_fpu_begin();
  398. while ((nbytes = walk.nbytes)) {
  399. aesni_cbc_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  400. nbytes & AES_BLOCK_MASK, walk.iv);
  401. nbytes &= AES_BLOCK_SIZE - 1;
  402. err = blkcipher_walk_done(desc, &walk, nbytes);
  403. }
  404. kernel_fpu_end();
  405. return err;
  406. }
  407. #ifdef CONFIG_X86_64
  408. static void ctr_crypt_final(struct crypto_aes_ctx *ctx,
  409. struct blkcipher_walk *walk)
  410. {
  411. u8 *ctrblk = walk->iv;
  412. u8 keystream[AES_BLOCK_SIZE];
  413. u8 *src = walk->src.virt.addr;
  414. u8 *dst = walk->dst.virt.addr;
  415. unsigned int nbytes = walk->nbytes;
  416. aesni_enc(ctx, keystream, ctrblk);
  417. crypto_xor(keystream, src, nbytes);
  418. memcpy(dst, keystream, nbytes);
  419. crypto_inc(ctrblk, AES_BLOCK_SIZE);
  420. }
  421. #ifdef CONFIG_AS_AVX
  422. static void aesni_ctr_enc_avx_tfm(struct crypto_aes_ctx *ctx, u8 *out,
  423. const u8 *in, unsigned int len, u8 *iv)
  424. {
  425. /*
  426. * based on key length, override with the by8 version
  427. * of ctr mode encryption/decryption for improved performance
  428. * aes_set_key_common() ensures that key length is one of
  429. * {128,192,256}
  430. */
  431. if (ctx->key_length == AES_KEYSIZE_128)
  432. aes_ctr_enc_128_avx_by8(in, iv, (void *)ctx, out, len);
  433. else if (ctx->key_length == AES_KEYSIZE_192)
  434. aes_ctr_enc_192_avx_by8(in, iv, (void *)ctx, out, len);
  435. else
  436. aes_ctr_enc_256_avx_by8(in, iv, (void *)ctx, out, len);
  437. }
  438. #endif
  439. static int ctr_crypt(struct blkcipher_desc *desc,
  440. struct scatterlist *dst, struct scatterlist *src,
  441. unsigned int nbytes)
  442. {
  443. struct crypto_aes_ctx *ctx = aes_ctx(crypto_blkcipher_ctx(desc->tfm));
  444. struct blkcipher_walk walk;
  445. int err;
  446. blkcipher_walk_init(&walk, dst, src, nbytes);
  447. err = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE);
  448. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  449. kernel_fpu_begin();
  450. while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
  451. aesni_ctr_enc_tfm(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  452. nbytes & AES_BLOCK_MASK, walk.iv);
  453. nbytes &= AES_BLOCK_SIZE - 1;
  454. err = blkcipher_walk_done(desc, &walk, nbytes);
  455. }
  456. if (walk.nbytes) {
  457. ctr_crypt_final(ctx, &walk);
  458. err = blkcipher_walk_done(desc, &walk, 0);
  459. }
  460. kernel_fpu_end();
  461. return err;
  462. }
  463. #endif
  464. static int ablk_ecb_init(struct crypto_tfm *tfm)
  465. {
  466. return ablk_init_common(tfm, "__driver-ecb-aes-aesni");
  467. }
  468. static int ablk_cbc_init(struct crypto_tfm *tfm)
  469. {
  470. return ablk_init_common(tfm, "__driver-cbc-aes-aesni");
  471. }
  472. #ifdef CONFIG_X86_64
  473. static int ablk_ctr_init(struct crypto_tfm *tfm)
  474. {
  475. return ablk_init_common(tfm, "__driver-ctr-aes-aesni");
  476. }
  477. #endif
  478. #if IS_ENABLED(CONFIG_CRYPTO_PCBC)
  479. static int ablk_pcbc_init(struct crypto_tfm *tfm)
  480. {
  481. return ablk_init_common(tfm, "fpu(pcbc(__driver-aes-aesni))");
  482. }
  483. #endif
  484. static void lrw_xts_encrypt_callback(void *ctx, u8 *blks, unsigned int nbytes)
  485. {
  486. aesni_ecb_enc(ctx, blks, blks, nbytes);
  487. }
  488. static void lrw_xts_decrypt_callback(void *ctx, u8 *blks, unsigned int nbytes)
  489. {
  490. aesni_ecb_dec(ctx, blks, blks, nbytes);
  491. }
  492. static int lrw_aesni_setkey(struct crypto_tfm *tfm, const u8 *key,
  493. unsigned int keylen)
  494. {
  495. struct aesni_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
  496. int err;
  497. err = aes_set_key_common(tfm, ctx->raw_aes_ctx, key,
  498. keylen - AES_BLOCK_SIZE);
  499. if (err)
  500. return err;
  501. return lrw_init_table(&ctx->lrw_table, key + keylen - AES_BLOCK_SIZE);
  502. }
  503. static void lrw_aesni_exit_tfm(struct crypto_tfm *tfm)
  504. {
  505. struct aesni_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
  506. lrw_free_table(&ctx->lrw_table);
  507. }
  508. static int lrw_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  509. struct scatterlist *src, unsigned int nbytes)
  510. {
  511. struct aesni_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  512. be128 buf[8];
  513. struct lrw_crypt_req req = {
  514. .tbuf = buf,
  515. .tbuflen = sizeof(buf),
  516. .table_ctx = &ctx->lrw_table,
  517. .crypt_ctx = aes_ctx(ctx->raw_aes_ctx),
  518. .crypt_fn = lrw_xts_encrypt_callback,
  519. };
  520. int ret;
  521. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  522. kernel_fpu_begin();
  523. ret = lrw_crypt(desc, dst, src, nbytes, &req);
  524. kernel_fpu_end();
  525. return ret;
  526. }
  527. static int lrw_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  528. struct scatterlist *src, unsigned int nbytes)
  529. {
  530. struct aesni_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  531. be128 buf[8];
  532. struct lrw_crypt_req req = {
  533. .tbuf = buf,
  534. .tbuflen = sizeof(buf),
  535. .table_ctx = &ctx->lrw_table,
  536. .crypt_ctx = aes_ctx(ctx->raw_aes_ctx),
  537. .crypt_fn = lrw_xts_decrypt_callback,
  538. };
  539. int ret;
  540. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  541. kernel_fpu_begin();
  542. ret = lrw_crypt(desc, dst, src, nbytes, &req);
  543. kernel_fpu_end();
  544. return ret;
  545. }
  546. static int xts_aesni_setkey(struct crypto_tfm *tfm, const u8 *key,
  547. unsigned int keylen)
  548. {
  549. struct aesni_xts_ctx *ctx = crypto_tfm_ctx(tfm);
  550. u32 *flags = &tfm->crt_flags;
  551. int err;
  552. /* key consists of keys of equal size concatenated, therefore
  553. * the length must be even
  554. */
  555. if (keylen % 2) {
  556. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  557. return -EINVAL;
  558. }
  559. /* first half of xts-key is for crypt */
  560. err = aes_set_key_common(tfm, ctx->raw_crypt_ctx, key, keylen / 2);
  561. if (err)
  562. return err;
  563. /* second half of xts-key is for tweak */
  564. return aes_set_key_common(tfm, ctx->raw_tweak_ctx, key + keylen / 2,
  565. keylen / 2);
  566. }
  567. static void aesni_xts_tweak(void *ctx, u8 *out, const u8 *in)
  568. {
  569. aesni_enc(ctx, out, in);
  570. }
  571. #ifdef CONFIG_X86_64
  572. static void aesni_xts_enc(void *ctx, u128 *dst, const u128 *src, le128 *iv)
  573. {
  574. glue_xts_crypt_128bit_one(ctx, dst, src, iv, GLUE_FUNC_CAST(aesni_enc));
  575. }
  576. static void aesni_xts_dec(void *ctx, u128 *dst, const u128 *src, le128 *iv)
  577. {
  578. glue_xts_crypt_128bit_one(ctx, dst, src, iv, GLUE_FUNC_CAST(aesni_dec));
  579. }
  580. static void aesni_xts_enc8(void *ctx, u128 *dst, const u128 *src, le128 *iv)
  581. {
  582. aesni_xts_crypt8(ctx, (u8 *)dst, (const u8 *)src, true, (u8 *)iv);
  583. }
  584. static void aesni_xts_dec8(void *ctx, u128 *dst, const u128 *src, le128 *iv)
  585. {
  586. aesni_xts_crypt8(ctx, (u8 *)dst, (const u8 *)src, false, (u8 *)iv);
  587. }
  588. static const struct common_glue_ctx aesni_enc_xts = {
  589. .num_funcs = 2,
  590. .fpu_blocks_limit = 1,
  591. .funcs = { {
  592. .num_blocks = 8,
  593. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(aesni_xts_enc8) }
  594. }, {
  595. .num_blocks = 1,
  596. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(aesni_xts_enc) }
  597. } }
  598. };
  599. static const struct common_glue_ctx aesni_dec_xts = {
  600. .num_funcs = 2,
  601. .fpu_blocks_limit = 1,
  602. .funcs = { {
  603. .num_blocks = 8,
  604. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(aesni_xts_dec8) }
  605. }, {
  606. .num_blocks = 1,
  607. .fn_u = { .xts = GLUE_XTS_FUNC_CAST(aesni_xts_dec) }
  608. } }
  609. };
  610. static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  611. struct scatterlist *src, unsigned int nbytes)
  612. {
  613. struct aesni_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  614. return glue_xts_crypt_128bit(&aesni_enc_xts, desc, dst, src, nbytes,
  615. XTS_TWEAK_CAST(aesni_xts_tweak),
  616. aes_ctx(ctx->raw_tweak_ctx),
  617. aes_ctx(ctx->raw_crypt_ctx));
  618. }
  619. static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  620. struct scatterlist *src, unsigned int nbytes)
  621. {
  622. struct aesni_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  623. return glue_xts_crypt_128bit(&aesni_dec_xts, desc, dst, src, nbytes,
  624. XTS_TWEAK_CAST(aesni_xts_tweak),
  625. aes_ctx(ctx->raw_tweak_ctx),
  626. aes_ctx(ctx->raw_crypt_ctx));
  627. }
  628. #else
  629. static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  630. struct scatterlist *src, unsigned int nbytes)
  631. {
  632. struct aesni_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  633. be128 buf[8];
  634. struct xts_crypt_req req = {
  635. .tbuf = buf,
  636. .tbuflen = sizeof(buf),
  637. .tweak_ctx = aes_ctx(ctx->raw_tweak_ctx),
  638. .tweak_fn = aesni_xts_tweak,
  639. .crypt_ctx = aes_ctx(ctx->raw_crypt_ctx),
  640. .crypt_fn = lrw_xts_encrypt_callback,
  641. };
  642. int ret;
  643. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  644. kernel_fpu_begin();
  645. ret = xts_crypt(desc, dst, src, nbytes, &req);
  646. kernel_fpu_end();
  647. return ret;
  648. }
  649. static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  650. struct scatterlist *src, unsigned int nbytes)
  651. {
  652. struct aesni_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  653. be128 buf[8];
  654. struct xts_crypt_req req = {
  655. .tbuf = buf,
  656. .tbuflen = sizeof(buf),
  657. .tweak_ctx = aes_ctx(ctx->raw_tweak_ctx),
  658. .tweak_fn = aesni_xts_tweak,
  659. .crypt_ctx = aes_ctx(ctx->raw_crypt_ctx),
  660. .crypt_fn = lrw_xts_decrypt_callback,
  661. };
  662. int ret;
  663. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  664. kernel_fpu_begin();
  665. ret = xts_crypt(desc, dst, src, nbytes, &req);
  666. kernel_fpu_end();
  667. return ret;
  668. }
  669. #endif
  670. #ifdef CONFIG_X86_64
  671. static int rfc4106_init(struct crypto_aead *aead)
  672. {
  673. struct cryptd_aead *cryptd_tfm;
  674. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  675. cryptd_tfm = cryptd_alloc_aead("__driver-gcm-aes-aesni",
  676. CRYPTO_ALG_INTERNAL,
  677. CRYPTO_ALG_INTERNAL);
  678. if (IS_ERR(cryptd_tfm))
  679. return PTR_ERR(cryptd_tfm);
  680. *ctx = cryptd_tfm;
  681. crypto_aead_set_reqsize(aead, crypto_aead_reqsize(&cryptd_tfm->base));
  682. return 0;
  683. }
  684. static void rfc4106_exit(struct crypto_aead *aead)
  685. {
  686. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  687. cryptd_free_aead(*ctx);
  688. }
  689. static void
  690. rfc4106_set_hash_subkey_done(struct crypto_async_request *req, int err)
  691. {
  692. struct aesni_gcm_set_hash_subkey_result *result = req->data;
  693. if (err == -EINPROGRESS)
  694. return;
  695. result->err = err;
  696. complete(&result->completion);
  697. }
  698. static int
  699. rfc4106_set_hash_subkey(u8 *hash_subkey, const u8 *key, unsigned int key_len)
  700. {
  701. struct crypto_ablkcipher *ctr_tfm;
  702. struct ablkcipher_request *req;
  703. int ret = -EINVAL;
  704. struct aesni_hash_subkey_req_data *req_data;
  705. ctr_tfm = crypto_alloc_ablkcipher("ctr(aes)", 0, 0);
  706. if (IS_ERR(ctr_tfm))
  707. return PTR_ERR(ctr_tfm);
  708. ret = crypto_ablkcipher_setkey(ctr_tfm, key, key_len);
  709. if (ret)
  710. goto out_free_ablkcipher;
  711. ret = -ENOMEM;
  712. req = ablkcipher_request_alloc(ctr_tfm, GFP_KERNEL);
  713. if (!req)
  714. goto out_free_ablkcipher;
  715. req_data = kmalloc(sizeof(*req_data), GFP_KERNEL);
  716. if (!req_data)
  717. goto out_free_request;
  718. memset(req_data->iv, 0, sizeof(req_data->iv));
  719. /* Clear the data in the hash sub key container to zero.*/
  720. /* We want to cipher all zeros to create the hash sub key. */
  721. memset(hash_subkey, 0, RFC4106_HASH_SUBKEY_SIZE);
  722. init_completion(&req_data->result.completion);
  723. sg_init_one(&req_data->sg, hash_subkey, RFC4106_HASH_SUBKEY_SIZE);
  724. ablkcipher_request_set_tfm(req, ctr_tfm);
  725. ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP |
  726. CRYPTO_TFM_REQ_MAY_BACKLOG,
  727. rfc4106_set_hash_subkey_done,
  728. &req_data->result);
  729. ablkcipher_request_set_crypt(req, &req_data->sg,
  730. &req_data->sg, RFC4106_HASH_SUBKEY_SIZE, req_data->iv);
  731. ret = crypto_ablkcipher_encrypt(req);
  732. if (ret == -EINPROGRESS || ret == -EBUSY) {
  733. ret = wait_for_completion_interruptible
  734. (&req_data->result.completion);
  735. if (!ret)
  736. ret = req_data->result.err;
  737. }
  738. kfree(req_data);
  739. out_free_request:
  740. ablkcipher_request_free(req);
  741. out_free_ablkcipher:
  742. crypto_free_ablkcipher(ctr_tfm);
  743. return ret;
  744. }
  745. static int common_rfc4106_set_key(struct crypto_aead *aead, const u8 *key,
  746. unsigned int key_len)
  747. {
  748. struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(aead);
  749. if (key_len < 4) {
  750. crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  751. return -EINVAL;
  752. }
  753. /*Account for 4 byte nonce at the end.*/
  754. key_len -= 4;
  755. memcpy(ctx->nonce, key + key_len, sizeof(ctx->nonce));
  756. return aes_set_key_common(crypto_aead_tfm(aead),
  757. &ctx->aes_key_expanded, key, key_len) ?:
  758. rfc4106_set_hash_subkey(ctx->hash_subkey, key, key_len);
  759. }
  760. static int rfc4106_set_key(struct crypto_aead *parent, const u8 *key,
  761. unsigned int key_len)
  762. {
  763. struct cryptd_aead **ctx = crypto_aead_ctx(parent);
  764. struct cryptd_aead *cryptd_tfm = *ctx;
  765. return crypto_aead_setkey(&cryptd_tfm->base, key, key_len);
  766. }
  767. static int common_rfc4106_set_authsize(struct crypto_aead *aead,
  768. unsigned int authsize)
  769. {
  770. switch (authsize) {
  771. case 8:
  772. case 12:
  773. case 16:
  774. break;
  775. default:
  776. return -EINVAL;
  777. }
  778. return 0;
  779. }
  780. /* This is the Integrity Check Value (aka the authentication tag length and can
  781. * be 8, 12 or 16 bytes long. */
  782. static int rfc4106_set_authsize(struct crypto_aead *parent,
  783. unsigned int authsize)
  784. {
  785. struct cryptd_aead **ctx = crypto_aead_ctx(parent);
  786. struct cryptd_aead *cryptd_tfm = *ctx;
  787. return crypto_aead_setauthsize(&cryptd_tfm->base, authsize);
  788. }
  789. static int helper_rfc4106_encrypt(struct aead_request *req)
  790. {
  791. u8 one_entry_in_sg = 0;
  792. u8 *src, *dst, *assoc;
  793. __be32 counter = cpu_to_be32(1);
  794. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  795. struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
  796. void *aes_ctx = &(ctx->aes_key_expanded);
  797. unsigned long auth_tag_len = crypto_aead_authsize(tfm);
  798. u8 iv[16] __attribute__ ((__aligned__(AESNI_ALIGN)));
  799. struct scatter_walk src_sg_walk;
  800. struct scatter_walk dst_sg_walk;
  801. unsigned int i;
  802. /* Assuming we are supporting rfc4106 64-bit extended */
  803. /* sequence numbers We need to have the AAD length equal */
  804. /* to 16 or 20 bytes */
  805. if (unlikely(req->assoclen != 16 && req->assoclen != 20))
  806. return -EINVAL;
  807. /* IV below built */
  808. for (i = 0; i < 4; i++)
  809. *(iv+i) = ctx->nonce[i];
  810. for (i = 0; i < 8; i++)
  811. *(iv+4+i) = req->iv[i];
  812. *((__be32 *)(iv+12)) = counter;
  813. if (sg_is_last(req->src) &&
  814. req->src->offset + req->src->length <= PAGE_SIZE &&
  815. sg_is_last(req->dst) && req->dst->length &&
  816. req->dst->offset + req->dst->length <= PAGE_SIZE) {
  817. one_entry_in_sg = 1;
  818. scatterwalk_start(&src_sg_walk, req->src);
  819. assoc = scatterwalk_map(&src_sg_walk);
  820. src = assoc + req->assoclen;
  821. dst = src;
  822. if (unlikely(req->src != req->dst)) {
  823. scatterwalk_start(&dst_sg_walk, req->dst);
  824. dst = scatterwalk_map(&dst_sg_walk) + req->assoclen;
  825. }
  826. } else {
  827. /* Allocate memory for src, dst, assoc */
  828. assoc = kmalloc(req->cryptlen + auth_tag_len + req->assoclen,
  829. GFP_ATOMIC);
  830. if (unlikely(!assoc))
  831. return -ENOMEM;
  832. scatterwalk_map_and_copy(assoc, req->src, 0,
  833. req->assoclen + req->cryptlen, 0);
  834. src = assoc + req->assoclen;
  835. dst = src;
  836. }
  837. kernel_fpu_begin();
  838. aesni_gcm_enc_tfm(aes_ctx, dst, src, req->cryptlen, iv,
  839. ctx->hash_subkey, assoc, req->assoclen - 8,
  840. dst + req->cryptlen, auth_tag_len);
  841. kernel_fpu_end();
  842. /* The authTag (aka the Integrity Check Value) needs to be written
  843. * back to the packet. */
  844. if (one_entry_in_sg) {
  845. if (unlikely(req->src != req->dst)) {
  846. scatterwalk_unmap(dst - req->assoclen);
  847. scatterwalk_advance(&dst_sg_walk, req->dst->length);
  848. scatterwalk_done(&dst_sg_walk, 1, 0);
  849. }
  850. scatterwalk_unmap(assoc);
  851. scatterwalk_advance(&src_sg_walk, req->src->length);
  852. scatterwalk_done(&src_sg_walk, req->src == req->dst, 0);
  853. } else {
  854. scatterwalk_map_and_copy(dst, req->dst, req->assoclen,
  855. req->cryptlen + auth_tag_len, 1);
  856. kfree(assoc);
  857. }
  858. return 0;
  859. }
  860. static int helper_rfc4106_decrypt(struct aead_request *req)
  861. {
  862. u8 one_entry_in_sg = 0;
  863. u8 *src, *dst, *assoc;
  864. unsigned long tempCipherLen = 0;
  865. __be32 counter = cpu_to_be32(1);
  866. int retval = 0;
  867. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  868. struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
  869. void *aes_ctx = &(ctx->aes_key_expanded);
  870. unsigned long auth_tag_len = crypto_aead_authsize(tfm);
  871. u8 iv[16] __attribute__ ((__aligned__(AESNI_ALIGN)));
  872. u8 authTag[16];
  873. struct scatter_walk src_sg_walk;
  874. struct scatter_walk dst_sg_walk;
  875. unsigned int i;
  876. if (unlikely(req->assoclen != 16 && req->assoclen != 20))
  877. return -EINVAL;
  878. /* Assuming we are supporting rfc4106 64-bit extended */
  879. /* sequence numbers We need to have the AAD length */
  880. /* equal to 16 or 20 bytes */
  881. tempCipherLen = (unsigned long)(req->cryptlen - auth_tag_len);
  882. /* IV below built */
  883. for (i = 0; i < 4; i++)
  884. *(iv+i) = ctx->nonce[i];
  885. for (i = 0; i < 8; i++)
  886. *(iv+4+i) = req->iv[i];
  887. *((__be32 *)(iv+12)) = counter;
  888. if (sg_is_last(req->src) &&
  889. req->src->offset + req->src->length <= PAGE_SIZE &&
  890. sg_is_last(req->dst) &&
  891. req->dst->offset + req->dst->length <= PAGE_SIZE) {
  892. one_entry_in_sg = 1;
  893. scatterwalk_start(&src_sg_walk, req->src);
  894. assoc = scatterwalk_map(&src_sg_walk);
  895. src = assoc + req->assoclen;
  896. dst = src;
  897. if (unlikely(req->src != req->dst)) {
  898. scatterwalk_start(&dst_sg_walk, req->dst);
  899. dst = scatterwalk_map(&dst_sg_walk) + req->assoclen;
  900. }
  901. } else {
  902. /* Allocate memory for src, dst, assoc */
  903. assoc = kmalloc(req->cryptlen + req->assoclen, GFP_ATOMIC);
  904. if (!assoc)
  905. return -ENOMEM;
  906. scatterwalk_map_and_copy(assoc, req->src, 0,
  907. req->assoclen + req->cryptlen, 0);
  908. src = assoc + req->assoclen;
  909. dst = src;
  910. }
  911. kernel_fpu_begin();
  912. aesni_gcm_dec_tfm(aes_ctx, dst, src, tempCipherLen, iv,
  913. ctx->hash_subkey, assoc, req->assoclen - 8,
  914. authTag, auth_tag_len);
  915. kernel_fpu_end();
  916. /* Compare generated tag with passed in tag. */
  917. retval = crypto_memneq(src + tempCipherLen, authTag, auth_tag_len) ?
  918. -EBADMSG : 0;
  919. if (one_entry_in_sg) {
  920. if (unlikely(req->src != req->dst)) {
  921. scatterwalk_unmap(dst - req->assoclen);
  922. scatterwalk_advance(&dst_sg_walk, req->dst->length);
  923. scatterwalk_done(&dst_sg_walk, 1, 0);
  924. }
  925. scatterwalk_unmap(assoc);
  926. scatterwalk_advance(&src_sg_walk, req->src->length);
  927. scatterwalk_done(&src_sg_walk, req->src == req->dst, 0);
  928. } else {
  929. scatterwalk_map_and_copy(dst, req->dst, req->assoclen,
  930. tempCipherLen, 1);
  931. kfree(assoc);
  932. }
  933. return retval;
  934. }
  935. static int rfc4106_encrypt(struct aead_request *req)
  936. {
  937. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  938. struct cryptd_aead **ctx = crypto_aead_ctx(tfm);
  939. struct cryptd_aead *cryptd_tfm = *ctx;
  940. aead_request_set_tfm(req, irq_fpu_usable() ?
  941. cryptd_aead_child(cryptd_tfm) :
  942. &cryptd_tfm->base);
  943. return crypto_aead_encrypt(req);
  944. }
  945. static int rfc4106_decrypt(struct aead_request *req)
  946. {
  947. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  948. struct cryptd_aead **ctx = crypto_aead_ctx(tfm);
  949. struct cryptd_aead *cryptd_tfm = *ctx;
  950. aead_request_set_tfm(req, irq_fpu_usable() ?
  951. cryptd_aead_child(cryptd_tfm) :
  952. &cryptd_tfm->base);
  953. return crypto_aead_decrypt(req);
  954. }
  955. #endif
  956. static struct crypto_alg aesni_algs[] = { {
  957. .cra_name = "aes",
  958. .cra_driver_name = "aes-aesni",
  959. .cra_priority = 300,
  960. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  961. .cra_blocksize = AES_BLOCK_SIZE,
  962. .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
  963. AESNI_ALIGN - 1,
  964. .cra_alignmask = 0,
  965. .cra_module = THIS_MODULE,
  966. .cra_u = {
  967. .cipher = {
  968. .cia_min_keysize = AES_MIN_KEY_SIZE,
  969. .cia_max_keysize = AES_MAX_KEY_SIZE,
  970. .cia_setkey = aes_set_key,
  971. .cia_encrypt = aes_encrypt,
  972. .cia_decrypt = aes_decrypt
  973. }
  974. }
  975. }, {
  976. .cra_name = "__aes-aesni",
  977. .cra_driver_name = "__driver-aes-aesni",
  978. .cra_priority = 0,
  979. .cra_flags = CRYPTO_ALG_TYPE_CIPHER | CRYPTO_ALG_INTERNAL,
  980. .cra_blocksize = AES_BLOCK_SIZE,
  981. .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
  982. AESNI_ALIGN - 1,
  983. .cra_alignmask = 0,
  984. .cra_module = THIS_MODULE,
  985. .cra_u = {
  986. .cipher = {
  987. .cia_min_keysize = AES_MIN_KEY_SIZE,
  988. .cia_max_keysize = AES_MAX_KEY_SIZE,
  989. .cia_setkey = aes_set_key,
  990. .cia_encrypt = __aes_encrypt,
  991. .cia_decrypt = __aes_decrypt
  992. }
  993. }
  994. }, {
  995. .cra_name = "__ecb-aes-aesni",
  996. .cra_driver_name = "__driver-ecb-aes-aesni",
  997. .cra_priority = 0,
  998. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  999. CRYPTO_ALG_INTERNAL,
  1000. .cra_blocksize = AES_BLOCK_SIZE,
  1001. .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
  1002. AESNI_ALIGN - 1,
  1003. .cra_alignmask = 0,
  1004. .cra_type = &crypto_blkcipher_type,
  1005. .cra_module = THIS_MODULE,
  1006. .cra_u = {
  1007. .blkcipher = {
  1008. .min_keysize = AES_MIN_KEY_SIZE,
  1009. .max_keysize = AES_MAX_KEY_SIZE,
  1010. .setkey = aes_set_key,
  1011. .encrypt = ecb_encrypt,
  1012. .decrypt = ecb_decrypt,
  1013. },
  1014. },
  1015. }, {
  1016. .cra_name = "__cbc-aes-aesni",
  1017. .cra_driver_name = "__driver-cbc-aes-aesni",
  1018. .cra_priority = 0,
  1019. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  1020. CRYPTO_ALG_INTERNAL,
  1021. .cra_blocksize = AES_BLOCK_SIZE,
  1022. .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
  1023. AESNI_ALIGN - 1,
  1024. .cra_alignmask = 0,
  1025. .cra_type = &crypto_blkcipher_type,
  1026. .cra_module = THIS_MODULE,
  1027. .cra_u = {
  1028. .blkcipher = {
  1029. .min_keysize = AES_MIN_KEY_SIZE,
  1030. .max_keysize = AES_MAX_KEY_SIZE,
  1031. .setkey = aes_set_key,
  1032. .encrypt = cbc_encrypt,
  1033. .decrypt = cbc_decrypt,
  1034. },
  1035. },
  1036. }, {
  1037. .cra_name = "ecb(aes)",
  1038. .cra_driver_name = "ecb-aes-aesni",
  1039. .cra_priority = 400,
  1040. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1041. .cra_blocksize = AES_BLOCK_SIZE,
  1042. .cra_ctxsize = sizeof(struct async_helper_ctx),
  1043. .cra_alignmask = 0,
  1044. .cra_type = &crypto_ablkcipher_type,
  1045. .cra_module = THIS_MODULE,
  1046. .cra_init = ablk_ecb_init,
  1047. .cra_exit = ablk_exit,
  1048. .cra_u = {
  1049. .ablkcipher = {
  1050. .min_keysize = AES_MIN_KEY_SIZE,
  1051. .max_keysize = AES_MAX_KEY_SIZE,
  1052. .setkey = ablk_set_key,
  1053. .encrypt = ablk_encrypt,
  1054. .decrypt = ablk_decrypt,
  1055. },
  1056. },
  1057. }, {
  1058. .cra_name = "cbc(aes)",
  1059. .cra_driver_name = "cbc-aes-aesni",
  1060. .cra_priority = 400,
  1061. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1062. .cra_blocksize = AES_BLOCK_SIZE,
  1063. .cra_ctxsize = sizeof(struct async_helper_ctx),
  1064. .cra_alignmask = 0,
  1065. .cra_type = &crypto_ablkcipher_type,
  1066. .cra_module = THIS_MODULE,
  1067. .cra_init = ablk_cbc_init,
  1068. .cra_exit = ablk_exit,
  1069. .cra_u = {
  1070. .ablkcipher = {
  1071. .min_keysize = AES_MIN_KEY_SIZE,
  1072. .max_keysize = AES_MAX_KEY_SIZE,
  1073. .ivsize = AES_BLOCK_SIZE,
  1074. .setkey = ablk_set_key,
  1075. .encrypt = ablk_encrypt,
  1076. .decrypt = ablk_decrypt,
  1077. },
  1078. },
  1079. #ifdef CONFIG_X86_64
  1080. }, {
  1081. .cra_name = "__ctr-aes-aesni",
  1082. .cra_driver_name = "__driver-ctr-aes-aesni",
  1083. .cra_priority = 0,
  1084. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  1085. CRYPTO_ALG_INTERNAL,
  1086. .cra_blocksize = 1,
  1087. .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
  1088. AESNI_ALIGN - 1,
  1089. .cra_alignmask = 0,
  1090. .cra_type = &crypto_blkcipher_type,
  1091. .cra_module = THIS_MODULE,
  1092. .cra_u = {
  1093. .blkcipher = {
  1094. .min_keysize = AES_MIN_KEY_SIZE,
  1095. .max_keysize = AES_MAX_KEY_SIZE,
  1096. .ivsize = AES_BLOCK_SIZE,
  1097. .setkey = aes_set_key,
  1098. .encrypt = ctr_crypt,
  1099. .decrypt = ctr_crypt,
  1100. },
  1101. },
  1102. }, {
  1103. .cra_name = "ctr(aes)",
  1104. .cra_driver_name = "ctr-aes-aesni",
  1105. .cra_priority = 400,
  1106. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1107. .cra_blocksize = 1,
  1108. .cra_ctxsize = sizeof(struct async_helper_ctx),
  1109. .cra_alignmask = 0,
  1110. .cra_type = &crypto_ablkcipher_type,
  1111. .cra_module = THIS_MODULE,
  1112. .cra_init = ablk_ctr_init,
  1113. .cra_exit = ablk_exit,
  1114. .cra_u = {
  1115. .ablkcipher = {
  1116. .min_keysize = AES_MIN_KEY_SIZE,
  1117. .max_keysize = AES_MAX_KEY_SIZE,
  1118. .ivsize = AES_BLOCK_SIZE,
  1119. .setkey = ablk_set_key,
  1120. .encrypt = ablk_encrypt,
  1121. .decrypt = ablk_encrypt,
  1122. .geniv = "chainiv",
  1123. },
  1124. },
  1125. #endif
  1126. #if IS_ENABLED(CONFIG_CRYPTO_PCBC)
  1127. }, {
  1128. .cra_name = "pcbc(aes)",
  1129. .cra_driver_name = "pcbc-aes-aesni",
  1130. .cra_priority = 400,
  1131. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1132. .cra_blocksize = AES_BLOCK_SIZE,
  1133. .cra_ctxsize = sizeof(struct async_helper_ctx),
  1134. .cra_alignmask = 0,
  1135. .cra_type = &crypto_ablkcipher_type,
  1136. .cra_module = THIS_MODULE,
  1137. .cra_init = ablk_pcbc_init,
  1138. .cra_exit = ablk_exit,
  1139. .cra_u = {
  1140. .ablkcipher = {
  1141. .min_keysize = AES_MIN_KEY_SIZE,
  1142. .max_keysize = AES_MAX_KEY_SIZE,
  1143. .ivsize = AES_BLOCK_SIZE,
  1144. .setkey = ablk_set_key,
  1145. .encrypt = ablk_encrypt,
  1146. .decrypt = ablk_decrypt,
  1147. },
  1148. },
  1149. #endif
  1150. }, {
  1151. .cra_name = "__lrw-aes-aesni",
  1152. .cra_driver_name = "__driver-lrw-aes-aesni",
  1153. .cra_priority = 0,
  1154. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  1155. CRYPTO_ALG_INTERNAL,
  1156. .cra_blocksize = AES_BLOCK_SIZE,
  1157. .cra_ctxsize = sizeof(struct aesni_lrw_ctx),
  1158. .cra_alignmask = 0,
  1159. .cra_type = &crypto_blkcipher_type,
  1160. .cra_module = THIS_MODULE,
  1161. .cra_exit = lrw_aesni_exit_tfm,
  1162. .cra_u = {
  1163. .blkcipher = {
  1164. .min_keysize = AES_MIN_KEY_SIZE + AES_BLOCK_SIZE,
  1165. .max_keysize = AES_MAX_KEY_SIZE + AES_BLOCK_SIZE,
  1166. .ivsize = AES_BLOCK_SIZE,
  1167. .setkey = lrw_aesni_setkey,
  1168. .encrypt = lrw_encrypt,
  1169. .decrypt = lrw_decrypt,
  1170. },
  1171. },
  1172. }, {
  1173. .cra_name = "__xts-aes-aesni",
  1174. .cra_driver_name = "__driver-xts-aes-aesni",
  1175. .cra_priority = 0,
  1176. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  1177. CRYPTO_ALG_INTERNAL,
  1178. .cra_blocksize = AES_BLOCK_SIZE,
  1179. .cra_ctxsize = sizeof(struct aesni_xts_ctx),
  1180. .cra_alignmask = 0,
  1181. .cra_type = &crypto_blkcipher_type,
  1182. .cra_module = THIS_MODULE,
  1183. .cra_u = {
  1184. .blkcipher = {
  1185. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  1186. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  1187. .ivsize = AES_BLOCK_SIZE,
  1188. .setkey = xts_aesni_setkey,
  1189. .encrypt = xts_encrypt,
  1190. .decrypt = xts_decrypt,
  1191. },
  1192. },
  1193. }, {
  1194. .cra_name = "lrw(aes)",
  1195. .cra_driver_name = "lrw-aes-aesni",
  1196. .cra_priority = 400,
  1197. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1198. .cra_blocksize = AES_BLOCK_SIZE,
  1199. .cra_ctxsize = sizeof(struct async_helper_ctx),
  1200. .cra_alignmask = 0,
  1201. .cra_type = &crypto_ablkcipher_type,
  1202. .cra_module = THIS_MODULE,
  1203. .cra_init = ablk_init,
  1204. .cra_exit = ablk_exit,
  1205. .cra_u = {
  1206. .ablkcipher = {
  1207. .min_keysize = AES_MIN_KEY_SIZE + AES_BLOCK_SIZE,
  1208. .max_keysize = AES_MAX_KEY_SIZE + AES_BLOCK_SIZE,
  1209. .ivsize = AES_BLOCK_SIZE,
  1210. .setkey = ablk_set_key,
  1211. .encrypt = ablk_encrypt,
  1212. .decrypt = ablk_decrypt,
  1213. },
  1214. },
  1215. }, {
  1216. .cra_name = "xts(aes)",
  1217. .cra_driver_name = "xts-aes-aesni",
  1218. .cra_priority = 400,
  1219. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1220. .cra_blocksize = AES_BLOCK_SIZE,
  1221. .cra_ctxsize = sizeof(struct async_helper_ctx),
  1222. .cra_alignmask = 0,
  1223. .cra_type = &crypto_ablkcipher_type,
  1224. .cra_module = THIS_MODULE,
  1225. .cra_init = ablk_init,
  1226. .cra_exit = ablk_exit,
  1227. .cra_u = {
  1228. .ablkcipher = {
  1229. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  1230. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  1231. .ivsize = AES_BLOCK_SIZE,
  1232. .setkey = ablk_set_key,
  1233. .encrypt = ablk_encrypt,
  1234. .decrypt = ablk_decrypt,
  1235. },
  1236. },
  1237. } };
  1238. #ifdef CONFIG_X86_64
  1239. static struct aead_alg aesni_aead_algs[] = { {
  1240. .setkey = common_rfc4106_set_key,
  1241. .setauthsize = common_rfc4106_set_authsize,
  1242. .encrypt = helper_rfc4106_encrypt,
  1243. .decrypt = helper_rfc4106_decrypt,
  1244. .ivsize = 8,
  1245. .maxauthsize = 16,
  1246. .base = {
  1247. .cra_name = "__gcm-aes-aesni",
  1248. .cra_driver_name = "__driver-gcm-aes-aesni",
  1249. .cra_flags = CRYPTO_ALG_INTERNAL,
  1250. .cra_blocksize = 1,
  1251. .cra_ctxsize = sizeof(struct aesni_rfc4106_gcm_ctx),
  1252. .cra_alignmask = AESNI_ALIGN - 1,
  1253. .cra_module = THIS_MODULE,
  1254. },
  1255. }, {
  1256. .init = rfc4106_init,
  1257. .exit = rfc4106_exit,
  1258. .setkey = rfc4106_set_key,
  1259. .setauthsize = rfc4106_set_authsize,
  1260. .encrypt = rfc4106_encrypt,
  1261. .decrypt = rfc4106_decrypt,
  1262. .ivsize = 8,
  1263. .maxauthsize = 16,
  1264. .base = {
  1265. .cra_name = "rfc4106(gcm(aes))",
  1266. .cra_driver_name = "rfc4106-gcm-aesni",
  1267. .cra_priority = 400,
  1268. .cra_flags = CRYPTO_ALG_ASYNC,
  1269. .cra_blocksize = 1,
  1270. .cra_ctxsize = sizeof(struct cryptd_aead *),
  1271. .cra_module = THIS_MODULE,
  1272. },
  1273. } };
  1274. #else
  1275. static struct aead_alg aesni_aead_algs[0];
  1276. #endif
  1277. static const struct x86_cpu_id aesni_cpu_id[] = {
  1278. X86_FEATURE_MATCH(X86_FEATURE_AES),
  1279. {}
  1280. };
  1281. MODULE_DEVICE_TABLE(x86cpu, aesni_cpu_id);
  1282. static int __init aesni_init(void)
  1283. {
  1284. int err;
  1285. if (!x86_match_cpu(aesni_cpu_id))
  1286. return -ENODEV;
  1287. #ifdef CONFIG_X86_64
  1288. #ifdef CONFIG_AS_AVX2
  1289. if (boot_cpu_has(X86_FEATURE_AVX2)) {
  1290. pr_info("AVX2 version of gcm_enc/dec engaged.\n");
  1291. aesni_gcm_enc_tfm = aesni_gcm_enc_avx2;
  1292. aesni_gcm_dec_tfm = aesni_gcm_dec_avx2;
  1293. } else
  1294. #endif
  1295. #ifdef CONFIG_AS_AVX
  1296. if (boot_cpu_has(X86_FEATURE_AVX)) {
  1297. pr_info("AVX version of gcm_enc/dec engaged.\n");
  1298. aesni_gcm_enc_tfm = aesni_gcm_enc_avx;
  1299. aesni_gcm_dec_tfm = aesni_gcm_dec_avx;
  1300. } else
  1301. #endif
  1302. {
  1303. pr_info("SSE version of gcm_enc/dec engaged.\n");
  1304. aesni_gcm_enc_tfm = aesni_gcm_enc;
  1305. aesni_gcm_dec_tfm = aesni_gcm_dec;
  1306. }
  1307. aesni_ctr_enc_tfm = aesni_ctr_enc;
  1308. #ifdef CONFIG_AS_AVX
  1309. if (cpu_has_avx) {
  1310. /* optimize performance of ctr mode encryption transform */
  1311. aesni_ctr_enc_tfm = aesni_ctr_enc_avx_tfm;
  1312. pr_info("AES CTR mode by8 optimization enabled\n");
  1313. }
  1314. #endif
  1315. #endif
  1316. err = crypto_fpu_init();
  1317. if (err)
  1318. return err;
  1319. err = crypto_register_algs(aesni_algs, ARRAY_SIZE(aesni_algs));
  1320. if (err)
  1321. goto fpu_exit;
  1322. err = crypto_register_aeads(aesni_aead_algs,
  1323. ARRAY_SIZE(aesni_aead_algs));
  1324. if (err)
  1325. goto unregister_algs;
  1326. return err;
  1327. unregister_algs:
  1328. crypto_unregister_algs(aesni_algs, ARRAY_SIZE(aesni_algs));
  1329. fpu_exit:
  1330. crypto_fpu_exit();
  1331. return err;
  1332. }
  1333. static void __exit aesni_exit(void)
  1334. {
  1335. crypto_unregister_aeads(aesni_aead_algs, ARRAY_SIZE(aesni_aead_algs));
  1336. crypto_unregister_algs(aesni_algs, ARRAY_SIZE(aesni_algs));
  1337. crypto_fpu_exit();
  1338. }
  1339. late_initcall(aesni_init);
  1340. module_exit(aesni_exit);
  1341. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm, Intel AES-NI instructions optimized");
  1342. MODULE_LICENSE("GPL");
  1343. MODULE_ALIAS_CRYPTO("aes");