aesni-intel_glue.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  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(
  682. aead,
  683. sizeof(struct aead_request) +
  684. crypto_aead_reqsize(&cryptd_tfm->base));
  685. return 0;
  686. }
  687. static void rfc4106_exit(struct crypto_aead *aead)
  688. {
  689. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  690. cryptd_free_aead(*ctx);
  691. }
  692. static void
  693. rfc4106_set_hash_subkey_done(struct crypto_async_request *req, int err)
  694. {
  695. struct aesni_gcm_set_hash_subkey_result *result = req->data;
  696. if (err == -EINPROGRESS)
  697. return;
  698. result->err = err;
  699. complete(&result->completion);
  700. }
  701. static int
  702. rfc4106_set_hash_subkey(u8 *hash_subkey, const u8 *key, unsigned int key_len)
  703. {
  704. struct crypto_ablkcipher *ctr_tfm;
  705. struct ablkcipher_request *req;
  706. int ret = -EINVAL;
  707. struct aesni_hash_subkey_req_data *req_data;
  708. ctr_tfm = crypto_alloc_ablkcipher("ctr(aes)", 0, 0);
  709. if (IS_ERR(ctr_tfm))
  710. return PTR_ERR(ctr_tfm);
  711. ret = crypto_ablkcipher_setkey(ctr_tfm, key, key_len);
  712. if (ret)
  713. goto out_free_ablkcipher;
  714. ret = -ENOMEM;
  715. req = ablkcipher_request_alloc(ctr_tfm, GFP_KERNEL);
  716. if (!req)
  717. goto out_free_ablkcipher;
  718. req_data = kmalloc(sizeof(*req_data), GFP_KERNEL);
  719. if (!req_data)
  720. goto out_free_request;
  721. memset(req_data->iv, 0, sizeof(req_data->iv));
  722. /* Clear the data in the hash sub key container to zero.*/
  723. /* We want to cipher all zeros to create the hash sub key. */
  724. memset(hash_subkey, 0, RFC4106_HASH_SUBKEY_SIZE);
  725. init_completion(&req_data->result.completion);
  726. sg_init_one(&req_data->sg, hash_subkey, RFC4106_HASH_SUBKEY_SIZE);
  727. ablkcipher_request_set_tfm(req, ctr_tfm);
  728. ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP |
  729. CRYPTO_TFM_REQ_MAY_BACKLOG,
  730. rfc4106_set_hash_subkey_done,
  731. &req_data->result);
  732. ablkcipher_request_set_crypt(req, &req_data->sg,
  733. &req_data->sg, RFC4106_HASH_SUBKEY_SIZE, req_data->iv);
  734. ret = crypto_ablkcipher_encrypt(req);
  735. if (ret == -EINPROGRESS || ret == -EBUSY) {
  736. ret = wait_for_completion_interruptible
  737. (&req_data->result.completion);
  738. if (!ret)
  739. ret = req_data->result.err;
  740. }
  741. kfree(req_data);
  742. out_free_request:
  743. ablkcipher_request_free(req);
  744. out_free_ablkcipher:
  745. crypto_free_ablkcipher(ctr_tfm);
  746. return ret;
  747. }
  748. static int common_rfc4106_set_key(struct crypto_aead *aead, const u8 *key,
  749. unsigned int key_len)
  750. {
  751. struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(aead);
  752. if (key_len < 4) {
  753. crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  754. return -EINVAL;
  755. }
  756. /*Account for 4 byte nonce at the end.*/
  757. key_len -= 4;
  758. memcpy(ctx->nonce, key + key_len, sizeof(ctx->nonce));
  759. return aes_set_key_common(crypto_aead_tfm(aead),
  760. &ctx->aes_key_expanded, key, key_len) ?:
  761. rfc4106_set_hash_subkey(ctx->hash_subkey, key, key_len);
  762. }
  763. static int rfc4106_set_key(struct crypto_aead *parent, const u8 *key,
  764. unsigned int key_len)
  765. {
  766. struct cryptd_aead **ctx = crypto_aead_ctx(parent);
  767. struct cryptd_aead *cryptd_tfm = *ctx;
  768. return crypto_aead_setkey(&cryptd_tfm->base, key, key_len);
  769. }
  770. static int common_rfc4106_set_authsize(struct crypto_aead *aead,
  771. unsigned int authsize)
  772. {
  773. switch (authsize) {
  774. case 8:
  775. case 12:
  776. case 16:
  777. break;
  778. default:
  779. return -EINVAL;
  780. }
  781. return 0;
  782. }
  783. /* This is the Integrity Check Value (aka the authentication tag length and can
  784. * be 8, 12 or 16 bytes long. */
  785. static int rfc4106_set_authsize(struct crypto_aead *parent,
  786. unsigned int authsize)
  787. {
  788. struct cryptd_aead **ctx = crypto_aead_ctx(parent);
  789. struct cryptd_aead *cryptd_tfm = *ctx;
  790. return crypto_aead_setauthsize(&cryptd_tfm->base, authsize);
  791. }
  792. static int helper_rfc4106_encrypt(struct aead_request *req)
  793. {
  794. u8 one_entry_in_sg = 0;
  795. u8 *src, *dst, *assoc;
  796. __be32 counter = cpu_to_be32(1);
  797. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  798. struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
  799. void *aes_ctx = &(ctx->aes_key_expanded);
  800. unsigned long auth_tag_len = crypto_aead_authsize(tfm);
  801. u8 iv[16] __attribute__ ((__aligned__(AESNI_ALIGN)));
  802. struct scatter_walk src_sg_walk;
  803. struct scatter_walk dst_sg_walk;
  804. unsigned int i;
  805. /* Assuming we are supporting rfc4106 64-bit extended */
  806. /* sequence numbers We need to have the AAD length equal */
  807. /* to 8 or 12 bytes */
  808. if (unlikely(req->assoclen != 8 && req->assoclen != 12))
  809. return -EINVAL;
  810. /* IV below built */
  811. for (i = 0; i < 4; i++)
  812. *(iv+i) = ctx->nonce[i];
  813. for (i = 0; i < 8; i++)
  814. *(iv+4+i) = req->iv[i];
  815. *((__be32 *)(iv+12)) = counter;
  816. if (sg_is_last(req->src) &&
  817. req->src->offset + req->src->length <= PAGE_SIZE &&
  818. sg_is_last(req->dst) &&
  819. req->dst->offset + req->dst->length <= PAGE_SIZE) {
  820. one_entry_in_sg = 1;
  821. scatterwalk_start(&src_sg_walk, req->src);
  822. assoc = scatterwalk_map(&src_sg_walk);
  823. src = assoc + req->assoclen;
  824. dst = src;
  825. if (unlikely(req->src != req->dst)) {
  826. scatterwalk_start(&dst_sg_walk, req->dst);
  827. dst = scatterwalk_map(&dst_sg_walk) + req->assoclen;
  828. }
  829. } else {
  830. /* Allocate memory for src, dst, assoc */
  831. assoc = kmalloc(req->cryptlen + auth_tag_len + req->assoclen,
  832. GFP_ATOMIC);
  833. if (unlikely(!assoc))
  834. return -ENOMEM;
  835. scatterwalk_map_and_copy(assoc, req->src, 0,
  836. req->assoclen + req->cryptlen, 0);
  837. src = assoc + req->assoclen;
  838. dst = src;
  839. }
  840. kernel_fpu_begin();
  841. aesni_gcm_enc_tfm(aes_ctx, dst, src, (unsigned long)req->cryptlen, iv,
  842. ctx->hash_subkey, assoc, (unsigned long)req->assoclen, dst
  843. + ((unsigned long)req->cryptlen), auth_tag_len);
  844. kernel_fpu_end();
  845. /* The authTag (aka the Integrity Check Value) needs to be written
  846. * back to the packet. */
  847. if (one_entry_in_sg) {
  848. if (unlikely(req->src != req->dst)) {
  849. scatterwalk_unmap(dst - req->assoclen);
  850. scatterwalk_advance(&dst_sg_walk, req->dst->length);
  851. scatterwalk_done(&dst_sg_walk, 1, 0);
  852. }
  853. scatterwalk_unmap(assoc);
  854. scatterwalk_advance(&src_sg_walk, req->src->length);
  855. scatterwalk_done(&src_sg_walk, req->src == req->dst, 0);
  856. } else {
  857. scatterwalk_map_and_copy(dst, req->dst, req->assoclen,
  858. req->cryptlen + auth_tag_len, 1);
  859. kfree(assoc);
  860. }
  861. return 0;
  862. }
  863. static int helper_rfc4106_decrypt(struct aead_request *req)
  864. {
  865. u8 one_entry_in_sg = 0;
  866. u8 *src, *dst, *assoc;
  867. unsigned long tempCipherLen = 0;
  868. __be32 counter = cpu_to_be32(1);
  869. int retval = 0;
  870. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  871. struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
  872. void *aes_ctx = &(ctx->aes_key_expanded);
  873. unsigned long auth_tag_len = crypto_aead_authsize(tfm);
  874. u8 iv[16] __attribute__ ((__aligned__(AESNI_ALIGN)));
  875. u8 authTag[16];
  876. struct scatter_walk src_sg_walk;
  877. struct scatter_walk dst_sg_walk;
  878. unsigned int i;
  879. if (unlikely(req->assoclen != 8 && req->assoclen != 12))
  880. return -EINVAL;
  881. /* Assuming we are supporting rfc4106 64-bit extended */
  882. /* sequence numbers We need to have the AAD length */
  883. /* equal to 8 or 12 bytes */
  884. tempCipherLen = (unsigned long)(req->cryptlen - auth_tag_len);
  885. /* IV below built */
  886. for (i = 0; i < 4; i++)
  887. *(iv+i) = ctx->nonce[i];
  888. for (i = 0; i < 8; i++)
  889. *(iv+4+i) = req->iv[i];
  890. *((__be32 *)(iv+12)) = counter;
  891. if (sg_is_last(req->src) &&
  892. req->src->offset + req->src->length <= PAGE_SIZE &&
  893. sg_is_last(req->dst) &&
  894. req->dst->offset + req->dst->length <= PAGE_SIZE) {
  895. one_entry_in_sg = 1;
  896. scatterwalk_start(&src_sg_walk, req->src);
  897. assoc = scatterwalk_map(&src_sg_walk);
  898. src = assoc + req->assoclen;
  899. dst = src;
  900. if (unlikely(req->src != req->dst)) {
  901. scatterwalk_start(&dst_sg_walk, req->dst);
  902. dst = scatterwalk_map(&dst_sg_walk) + req->assoclen;
  903. }
  904. } else {
  905. /* Allocate memory for src, dst, assoc */
  906. assoc = kmalloc(req->cryptlen + req->assoclen, GFP_ATOMIC);
  907. if (!assoc)
  908. return -ENOMEM;
  909. scatterwalk_map_and_copy(assoc, req->src, 0,
  910. req->assoclen + req->cryptlen, 0);
  911. src = assoc + req->assoclen;
  912. dst = src;
  913. }
  914. kernel_fpu_begin();
  915. aesni_gcm_dec_tfm(aes_ctx, dst, src, tempCipherLen, iv,
  916. ctx->hash_subkey, assoc, (unsigned long)req->assoclen,
  917. authTag, auth_tag_len);
  918. kernel_fpu_end();
  919. /* Compare generated tag with passed in tag. */
  920. retval = crypto_memneq(src + tempCipherLen, authTag, auth_tag_len) ?
  921. -EBADMSG : 0;
  922. if (one_entry_in_sg) {
  923. if (unlikely(req->src != req->dst)) {
  924. scatterwalk_unmap(dst - req->assoclen);
  925. scatterwalk_advance(&dst_sg_walk, req->dst->length);
  926. scatterwalk_done(&dst_sg_walk, 1, 0);
  927. }
  928. scatterwalk_unmap(assoc);
  929. scatterwalk_advance(&src_sg_walk, req->src->length);
  930. scatterwalk_done(&src_sg_walk, req->src == req->dst, 0);
  931. } else {
  932. scatterwalk_map_and_copy(dst, req->dst, req->assoclen,
  933. tempCipherLen, 1);
  934. kfree(assoc);
  935. }
  936. return retval;
  937. }
  938. static int rfc4106_encrypt(struct aead_request *req)
  939. {
  940. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  941. struct cryptd_aead **ctx = crypto_aead_ctx(tfm);
  942. struct cryptd_aead *cryptd_tfm = *ctx;
  943. struct aead_request *subreq = aead_request_ctx(req);
  944. aead_request_set_tfm(subreq, irq_fpu_usable() ?
  945. cryptd_aead_child(cryptd_tfm) :
  946. &cryptd_tfm->base);
  947. aead_request_set_callback(subreq, req->base.flags,
  948. req->base.complete, req->base.data);
  949. aead_request_set_crypt(subreq, req->src, req->dst,
  950. req->cryptlen, req->iv);
  951. aead_request_set_ad(subreq, req->assoclen);
  952. return crypto_aead_encrypt(subreq);
  953. }
  954. static int rfc4106_decrypt(struct aead_request *req)
  955. {
  956. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  957. struct cryptd_aead **ctx = crypto_aead_ctx(tfm);
  958. struct cryptd_aead *cryptd_tfm = *ctx;
  959. struct aead_request *subreq = aead_request_ctx(req);
  960. aead_request_set_tfm(subreq, irq_fpu_usable() ?
  961. cryptd_aead_child(cryptd_tfm) :
  962. &cryptd_tfm->base);
  963. aead_request_set_callback(subreq, req->base.flags,
  964. req->base.complete, req->base.data);
  965. aead_request_set_crypt(subreq, req->src, req->dst,
  966. req->cryptlen, req->iv);
  967. aead_request_set_ad(subreq, req->assoclen);
  968. return crypto_aead_decrypt(subreq);
  969. }
  970. #endif
  971. static struct crypto_alg aesni_algs[] = { {
  972. .cra_name = "aes",
  973. .cra_driver_name = "aes-aesni",
  974. .cra_priority = 300,
  975. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  976. .cra_blocksize = AES_BLOCK_SIZE,
  977. .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
  978. AESNI_ALIGN - 1,
  979. .cra_alignmask = 0,
  980. .cra_module = THIS_MODULE,
  981. .cra_u = {
  982. .cipher = {
  983. .cia_min_keysize = AES_MIN_KEY_SIZE,
  984. .cia_max_keysize = AES_MAX_KEY_SIZE,
  985. .cia_setkey = aes_set_key,
  986. .cia_encrypt = aes_encrypt,
  987. .cia_decrypt = aes_decrypt
  988. }
  989. }
  990. }, {
  991. .cra_name = "__aes-aesni",
  992. .cra_driver_name = "__driver-aes-aesni",
  993. .cra_priority = 0,
  994. .cra_flags = CRYPTO_ALG_TYPE_CIPHER | CRYPTO_ALG_INTERNAL,
  995. .cra_blocksize = AES_BLOCK_SIZE,
  996. .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
  997. AESNI_ALIGN - 1,
  998. .cra_alignmask = 0,
  999. .cra_module = THIS_MODULE,
  1000. .cra_u = {
  1001. .cipher = {
  1002. .cia_min_keysize = AES_MIN_KEY_SIZE,
  1003. .cia_max_keysize = AES_MAX_KEY_SIZE,
  1004. .cia_setkey = aes_set_key,
  1005. .cia_encrypt = __aes_encrypt,
  1006. .cia_decrypt = __aes_decrypt
  1007. }
  1008. }
  1009. }, {
  1010. .cra_name = "__ecb-aes-aesni",
  1011. .cra_driver_name = "__driver-ecb-aes-aesni",
  1012. .cra_priority = 0,
  1013. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  1014. CRYPTO_ALG_INTERNAL,
  1015. .cra_blocksize = AES_BLOCK_SIZE,
  1016. .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
  1017. AESNI_ALIGN - 1,
  1018. .cra_alignmask = 0,
  1019. .cra_type = &crypto_blkcipher_type,
  1020. .cra_module = THIS_MODULE,
  1021. .cra_u = {
  1022. .blkcipher = {
  1023. .min_keysize = AES_MIN_KEY_SIZE,
  1024. .max_keysize = AES_MAX_KEY_SIZE,
  1025. .setkey = aes_set_key,
  1026. .encrypt = ecb_encrypt,
  1027. .decrypt = ecb_decrypt,
  1028. },
  1029. },
  1030. }, {
  1031. .cra_name = "__cbc-aes-aesni",
  1032. .cra_driver_name = "__driver-cbc-aes-aesni",
  1033. .cra_priority = 0,
  1034. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  1035. CRYPTO_ALG_INTERNAL,
  1036. .cra_blocksize = AES_BLOCK_SIZE,
  1037. .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
  1038. AESNI_ALIGN - 1,
  1039. .cra_alignmask = 0,
  1040. .cra_type = &crypto_blkcipher_type,
  1041. .cra_module = THIS_MODULE,
  1042. .cra_u = {
  1043. .blkcipher = {
  1044. .min_keysize = AES_MIN_KEY_SIZE,
  1045. .max_keysize = AES_MAX_KEY_SIZE,
  1046. .setkey = aes_set_key,
  1047. .encrypt = cbc_encrypt,
  1048. .decrypt = cbc_decrypt,
  1049. },
  1050. },
  1051. }, {
  1052. .cra_name = "ecb(aes)",
  1053. .cra_driver_name = "ecb-aes-aesni",
  1054. .cra_priority = 400,
  1055. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1056. .cra_blocksize = AES_BLOCK_SIZE,
  1057. .cra_ctxsize = sizeof(struct async_helper_ctx),
  1058. .cra_alignmask = 0,
  1059. .cra_type = &crypto_ablkcipher_type,
  1060. .cra_module = THIS_MODULE,
  1061. .cra_init = ablk_ecb_init,
  1062. .cra_exit = ablk_exit,
  1063. .cra_u = {
  1064. .ablkcipher = {
  1065. .min_keysize = AES_MIN_KEY_SIZE,
  1066. .max_keysize = AES_MAX_KEY_SIZE,
  1067. .setkey = ablk_set_key,
  1068. .encrypt = ablk_encrypt,
  1069. .decrypt = ablk_decrypt,
  1070. },
  1071. },
  1072. }, {
  1073. .cra_name = "cbc(aes)",
  1074. .cra_driver_name = "cbc-aes-aesni",
  1075. .cra_priority = 400,
  1076. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1077. .cra_blocksize = AES_BLOCK_SIZE,
  1078. .cra_ctxsize = sizeof(struct async_helper_ctx),
  1079. .cra_alignmask = 0,
  1080. .cra_type = &crypto_ablkcipher_type,
  1081. .cra_module = THIS_MODULE,
  1082. .cra_init = ablk_cbc_init,
  1083. .cra_exit = ablk_exit,
  1084. .cra_u = {
  1085. .ablkcipher = {
  1086. .min_keysize = AES_MIN_KEY_SIZE,
  1087. .max_keysize = AES_MAX_KEY_SIZE,
  1088. .ivsize = AES_BLOCK_SIZE,
  1089. .setkey = ablk_set_key,
  1090. .encrypt = ablk_encrypt,
  1091. .decrypt = ablk_decrypt,
  1092. },
  1093. },
  1094. #ifdef CONFIG_X86_64
  1095. }, {
  1096. .cra_name = "__ctr-aes-aesni",
  1097. .cra_driver_name = "__driver-ctr-aes-aesni",
  1098. .cra_priority = 0,
  1099. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  1100. CRYPTO_ALG_INTERNAL,
  1101. .cra_blocksize = 1,
  1102. .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
  1103. AESNI_ALIGN - 1,
  1104. .cra_alignmask = 0,
  1105. .cra_type = &crypto_blkcipher_type,
  1106. .cra_module = THIS_MODULE,
  1107. .cra_u = {
  1108. .blkcipher = {
  1109. .min_keysize = AES_MIN_KEY_SIZE,
  1110. .max_keysize = AES_MAX_KEY_SIZE,
  1111. .ivsize = AES_BLOCK_SIZE,
  1112. .setkey = aes_set_key,
  1113. .encrypt = ctr_crypt,
  1114. .decrypt = ctr_crypt,
  1115. },
  1116. },
  1117. }, {
  1118. .cra_name = "ctr(aes)",
  1119. .cra_driver_name = "ctr-aes-aesni",
  1120. .cra_priority = 400,
  1121. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1122. .cra_blocksize = 1,
  1123. .cra_ctxsize = sizeof(struct async_helper_ctx),
  1124. .cra_alignmask = 0,
  1125. .cra_type = &crypto_ablkcipher_type,
  1126. .cra_module = THIS_MODULE,
  1127. .cra_init = ablk_ctr_init,
  1128. .cra_exit = ablk_exit,
  1129. .cra_u = {
  1130. .ablkcipher = {
  1131. .min_keysize = AES_MIN_KEY_SIZE,
  1132. .max_keysize = AES_MAX_KEY_SIZE,
  1133. .ivsize = AES_BLOCK_SIZE,
  1134. .setkey = ablk_set_key,
  1135. .encrypt = ablk_encrypt,
  1136. .decrypt = ablk_encrypt,
  1137. .geniv = "chainiv",
  1138. },
  1139. },
  1140. #endif
  1141. #if IS_ENABLED(CONFIG_CRYPTO_PCBC)
  1142. }, {
  1143. .cra_name = "pcbc(aes)",
  1144. .cra_driver_name = "pcbc-aes-aesni",
  1145. .cra_priority = 400,
  1146. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1147. .cra_blocksize = AES_BLOCK_SIZE,
  1148. .cra_ctxsize = sizeof(struct async_helper_ctx),
  1149. .cra_alignmask = 0,
  1150. .cra_type = &crypto_ablkcipher_type,
  1151. .cra_module = THIS_MODULE,
  1152. .cra_init = ablk_pcbc_init,
  1153. .cra_exit = ablk_exit,
  1154. .cra_u = {
  1155. .ablkcipher = {
  1156. .min_keysize = AES_MIN_KEY_SIZE,
  1157. .max_keysize = AES_MAX_KEY_SIZE,
  1158. .ivsize = AES_BLOCK_SIZE,
  1159. .setkey = ablk_set_key,
  1160. .encrypt = ablk_encrypt,
  1161. .decrypt = ablk_decrypt,
  1162. },
  1163. },
  1164. #endif
  1165. }, {
  1166. .cra_name = "__lrw-aes-aesni",
  1167. .cra_driver_name = "__driver-lrw-aes-aesni",
  1168. .cra_priority = 0,
  1169. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  1170. CRYPTO_ALG_INTERNAL,
  1171. .cra_blocksize = AES_BLOCK_SIZE,
  1172. .cra_ctxsize = sizeof(struct aesni_lrw_ctx),
  1173. .cra_alignmask = 0,
  1174. .cra_type = &crypto_blkcipher_type,
  1175. .cra_module = THIS_MODULE,
  1176. .cra_exit = lrw_aesni_exit_tfm,
  1177. .cra_u = {
  1178. .blkcipher = {
  1179. .min_keysize = AES_MIN_KEY_SIZE + AES_BLOCK_SIZE,
  1180. .max_keysize = AES_MAX_KEY_SIZE + AES_BLOCK_SIZE,
  1181. .ivsize = AES_BLOCK_SIZE,
  1182. .setkey = lrw_aesni_setkey,
  1183. .encrypt = lrw_encrypt,
  1184. .decrypt = lrw_decrypt,
  1185. },
  1186. },
  1187. }, {
  1188. .cra_name = "__xts-aes-aesni",
  1189. .cra_driver_name = "__driver-xts-aes-aesni",
  1190. .cra_priority = 0,
  1191. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER |
  1192. CRYPTO_ALG_INTERNAL,
  1193. .cra_blocksize = AES_BLOCK_SIZE,
  1194. .cra_ctxsize = sizeof(struct aesni_xts_ctx),
  1195. .cra_alignmask = 0,
  1196. .cra_type = &crypto_blkcipher_type,
  1197. .cra_module = THIS_MODULE,
  1198. .cra_u = {
  1199. .blkcipher = {
  1200. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  1201. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  1202. .ivsize = AES_BLOCK_SIZE,
  1203. .setkey = xts_aesni_setkey,
  1204. .encrypt = xts_encrypt,
  1205. .decrypt = xts_decrypt,
  1206. },
  1207. },
  1208. }, {
  1209. .cra_name = "lrw(aes)",
  1210. .cra_driver_name = "lrw-aes-aesni",
  1211. .cra_priority = 400,
  1212. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1213. .cra_blocksize = AES_BLOCK_SIZE,
  1214. .cra_ctxsize = sizeof(struct async_helper_ctx),
  1215. .cra_alignmask = 0,
  1216. .cra_type = &crypto_ablkcipher_type,
  1217. .cra_module = THIS_MODULE,
  1218. .cra_init = ablk_init,
  1219. .cra_exit = ablk_exit,
  1220. .cra_u = {
  1221. .ablkcipher = {
  1222. .min_keysize = AES_MIN_KEY_SIZE + AES_BLOCK_SIZE,
  1223. .max_keysize = AES_MAX_KEY_SIZE + AES_BLOCK_SIZE,
  1224. .ivsize = AES_BLOCK_SIZE,
  1225. .setkey = ablk_set_key,
  1226. .encrypt = ablk_encrypt,
  1227. .decrypt = ablk_decrypt,
  1228. },
  1229. },
  1230. }, {
  1231. .cra_name = "xts(aes)",
  1232. .cra_driver_name = "xts-aes-aesni",
  1233. .cra_priority = 400,
  1234. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1235. .cra_blocksize = AES_BLOCK_SIZE,
  1236. .cra_ctxsize = sizeof(struct async_helper_ctx),
  1237. .cra_alignmask = 0,
  1238. .cra_type = &crypto_ablkcipher_type,
  1239. .cra_module = THIS_MODULE,
  1240. .cra_init = ablk_init,
  1241. .cra_exit = ablk_exit,
  1242. .cra_u = {
  1243. .ablkcipher = {
  1244. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  1245. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  1246. .ivsize = AES_BLOCK_SIZE,
  1247. .setkey = ablk_set_key,
  1248. .encrypt = ablk_encrypt,
  1249. .decrypt = ablk_decrypt,
  1250. },
  1251. },
  1252. } };
  1253. #ifdef CONFIG_X86_64
  1254. static struct aead_alg aesni_aead_algs[] = { {
  1255. .setkey = common_rfc4106_set_key,
  1256. .setauthsize = common_rfc4106_set_authsize,
  1257. .encrypt = helper_rfc4106_encrypt,
  1258. .decrypt = helper_rfc4106_decrypt,
  1259. .ivsize = 8,
  1260. .maxauthsize = 16,
  1261. .base = {
  1262. .cra_name = "__gcm-aes-aesni",
  1263. .cra_driver_name = "__driver-gcm-aes-aesni",
  1264. .cra_flags = CRYPTO_ALG_INTERNAL,
  1265. .cra_blocksize = 1,
  1266. .cra_ctxsize = sizeof(struct aesni_rfc4106_gcm_ctx),
  1267. .cra_alignmask = AESNI_ALIGN - 1,
  1268. .cra_module = THIS_MODULE,
  1269. },
  1270. }, {
  1271. .init = rfc4106_init,
  1272. .exit = rfc4106_exit,
  1273. .setkey = rfc4106_set_key,
  1274. .setauthsize = rfc4106_set_authsize,
  1275. .encrypt = rfc4106_encrypt,
  1276. .decrypt = rfc4106_decrypt,
  1277. .ivsize = 8,
  1278. .maxauthsize = 16,
  1279. .base = {
  1280. .cra_name = "rfc4106(gcm(aes))",
  1281. .cra_driver_name = "rfc4106-gcm-aesni",
  1282. .cra_priority = 400,
  1283. .cra_flags = CRYPTO_ALG_ASYNC,
  1284. .cra_blocksize = 1,
  1285. .cra_ctxsize = sizeof(struct cryptd_aead *),
  1286. .cra_module = THIS_MODULE,
  1287. },
  1288. } };
  1289. #else
  1290. static struct aead_alg aesni_aead_algs[0];
  1291. #endif
  1292. static const struct x86_cpu_id aesni_cpu_id[] = {
  1293. X86_FEATURE_MATCH(X86_FEATURE_AES),
  1294. {}
  1295. };
  1296. MODULE_DEVICE_TABLE(x86cpu, aesni_cpu_id);
  1297. static int __init aesni_init(void)
  1298. {
  1299. int err;
  1300. if (!x86_match_cpu(aesni_cpu_id))
  1301. return -ENODEV;
  1302. #ifdef CONFIG_X86_64
  1303. #ifdef CONFIG_AS_AVX2
  1304. if (boot_cpu_has(X86_FEATURE_AVX2)) {
  1305. pr_info("AVX2 version of gcm_enc/dec engaged.\n");
  1306. aesni_gcm_enc_tfm = aesni_gcm_enc_avx2;
  1307. aesni_gcm_dec_tfm = aesni_gcm_dec_avx2;
  1308. } else
  1309. #endif
  1310. #ifdef CONFIG_AS_AVX
  1311. if (boot_cpu_has(X86_FEATURE_AVX)) {
  1312. pr_info("AVX version of gcm_enc/dec engaged.\n");
  1313. aesni_gcm_enc_tfm = aesni_gcm_enc_avx;
  1314. aesni_gcm_dec_tfm = aesni_gcm_dec_avx;
  1315. } else
  1316. #endif
  1317. {
  1318. pr_info("SSE version of gcm_enc/dec engaged.\n");
  1319. aesni_gcm_enc_tfm = aesni_gcm_enc;
  1320. aesni_gcm_dec_tfm = aesni_gcm_dec;
  1321. }
  1322. aesni_ctr_enc_tfm = aesni_ctr_enc;
  1323. #ifdef CONFIG_AS_AVX
  1324. if (cpu_has_avx) {
  1325. /* optimize performance of ctr mode encryption transform */
  1326. aesni_ctr_enc_tfm = aesni_ctr_enc_avx_tfm;
  1327. pr_info("AES CTR mode by8 optimization enabled\n");
  1328. }
  1329. #endif
  1330. #endif
  1331. err = crypto_fpu_init();
  1332. if (err)
  1333. return err;
  1334. err = crypto_register_algs(aesni_algs, ARRAY_SIZE(aesni_algs));
  1335. if (err)
  1336. goto fpu_exit;
  1337. err = crypto_register_aeads(aesni_aead_algs,
  1338. ARRAY_SIZE(aesni_aead_algs));
  1339. if (err)
  1340. goto unregister_algs;
  1341. return err;
  1342. unregister_algs:
  1343. crypto_unregister_algs(aesni_algs, ARRAY_SIZE(aesni_algs));
  1344. fpu_exit:
  1345. crypto_fpu_exit();
  1346. return err;
  1347. }
  1348. static void __exit aesni_exit(void)
  1349. {
  1350. crypto_unregister_aeads(aesni_aead_algs, ARRAY_SIZE(aesni_aead_algs));
  1351. crypto_unregister_algs(aesni_algs, ARRAY_SIZE(aesni_algs));
  1352. crypto_fpu_exit();
  1353. }
  1354. late_initcall(aesni_init);
  1355. module_exit(aesni_exit);
  1356. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm, Intel AES-NI instructions optimized");
  1357. MODULE_LICENSE("GPL");
  1358. MODULE_ALIAS_CRYPTO("aes");