aesni-intel_glue.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  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/i387.h>
  35. #include <asm/crypto/aes.h>
  36. #include <asm/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. #if defined(CONFIG_CRYPTO_CTR) || defined(CONFIG_CRYPTO_CTR_MODULE)
  42. #define HAS_CTR
  43. #endif
  44. #if defined(CONFIG_CRYPTO_PCBC) || defined(CONFIG_CRYPTO_PCBC_MODULE)
  45. #define HAS_PCBC
  46. #endif
  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];
  53. struct crypto_aes_ctx aes_key_expanded;
  54. u8 nonce[4];
  55. struct cryptd_aead *cryptd_tfm;
  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. #define AESNI_ALIGN (16)
  67. #define AES_BLOCK_MASK (~(AES_BLOCK_SIZE-1))
  68. #define RFC4106_HASH_SUBKEY_SIZE 16
  69. struct aesni_lrw_ctx {
  70. struct lrw_table_ctx lrw_table;
  71. u8 raw_aes_ctx[sizeof(struct crypto_aes_ctx) + AESNI_ALIGN - 1];
  72. };
  73. struct aesni_xts_ctx {
  74. u8 raw_tweak_ctx[sizeof(struct crypto_aes_ctx) + AESNI_ALIGN - 1];
  75. u8 raw_crypt_ctx[sizeof(struct crypto_aes_ctx) + AESNI_ALIGN - 1];
  76. };
  77. asmlinkage int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key,
  78. unsigned int key_len);
  79. asmlinkage void aesni_enc(struct crypto_aes_ctx *ctx, u8 *out,
  80. const u8 *in);
  81. asmlinkage void aesni_dec(struct crypto_aes_ctx *ctx, u8 *out,
  82. const u8 *in);
  83. asmlinkage void aesni_ecb_enc(struct crypto_aes_ctx *ctx, u8 *out,
  84. const u8 *in, unsigned int len);
  85. asmlinkage void aesni_ecb_dec(struct crypto_aes_ctx *ctx, u8 *out,
  86. const u8 *in, unsigned int len);
  87. asmlinkage void aesni_cbc_enc(struct crypto_aes_ctx *ctx, u8 *out,
  88. const u8 *in, unsigned int len, u8 *iv);
  89. asmlinkage void aesni_cbc_dec(struct crypto_aes_ctx *ctx, u8 *out,
  90. const u8 *in, unsigned int len, u8 *iv);
  91. int crypto_fpu_init(void);
  92. void crypto_fpu_exit(void);
  93. #ifdef CONFIG_X86_64
  94. asmlinkage void aesni_ctr_enc(struct crypto_aes_ctx *ctx, u8 *out,
  95. const u8 *in, unsigned int len, u8 *iv);
  96. /* asmlinkage void aesni_gcm_enc()
  97. * void *ctx, AES Key schedule. Starts on a 16 byte boundary.
  98. * u8 *out, Ciphertext output. Encrypt in-place is allowed.
  99. * const u8 *in, Plaintext input
  100. * unsigned long plaintext_len, Length of data in bytes for encryption.
  101. * u8 *iv, Pre-counter block j0: 4 byte salt (from Security Association)
  102. * concatenated with 8 byte Initialisation Vector (from IPSec ESP
  103. * Payload) concatenated with 0x00000001. 16-byte aligned pointer.
  104. * u8 *hash_subkey, the Hash sub key input. Data starts on a 16-byte boundary.
  105. * const u8 *aad, Additional Authentication Data (AAD)
  106. * unsigned long aad_len, Length of AAD in bytes. With RFC4106 this
  107. * is going to be 8 or 12 bytes
  108. * u8 *auth_tag, Authenticated Tag output.
  109. * unsigned long auth_tag_len), Authenticated Tag Length in bytes.
  110. * Valid values are 16 (most likely), 12 or 8.
  111. */
  112. asmlinkage void aesni_gcm_enc(void *ctx, u8 *out,
  113. const u8 *in, unsigned long plaintext_len, u8 *iv,
  114. u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
  115. u8 *auth_tag, unsigned long auth_tag_len);
  116. /* asmlinkage void aesni_gcm_dec()
  117. * void *ctx, AES Key schedule. Starts on a 16 byte boundary.
  118. * u8 *out, Plaintext output. Decrypt in-place is allowed.
  119. * const u8 *in, Ciphertext input
  120. * unsigned long ciphertext_len, Length of data in bytes for decryption.
  121. * u8 *iv, Pre-counter block j0: 4 byte salt (from Security Association)
  122. * concatenated with 8 byte Initialisation Vector (from IPSec ESP
  123. * Payload) concatenated with 0x00000001. 16-byte aligned pointer.
  124. * u8 *hash_subkey, the Hash sub key input. Data starts on a 16-byte boundary.
  125. * const u8 *aad, Additional Authentication Data (AAD)
  126. * unsigned long aad_len, Length of AAD in bytes. With RFC4106 this is going
  127. * to be 8 or 12 bytes
  128. * u8 *auth_tag, Authenticated Tag output.
  129. * unsigned long auth_tag_len) Authenticated Tag Length in bytes.
  130. * Valid values are 16 (most likely), 12 or 8.
  131. */
  132. asmlinkage void aesni_gcm_dec(void *ctx, u8 *out,
  133. const u8 *in, unsigned long ciphertext_len, u8 *iv,
  134. u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
  135. u8 *auth_tag, unsigned long auth_tag_len);
  136. static inline struct
  137. aesni_rfc4106_gcm_ctx *aesni_rfc4106_gcm_ctx_get(struct crypto_aead *tfm)
  138. {
  139. return
  140. (struct aesni_rfc4106_gcm_ctx *)
  141. PTR_ALIGN((u8 *)
  142. crypto_tfm_ctx(crypto_aead_tfm(tfm)), AESNI_ALIGN);
  143. }
  144. #endif
  145. static inline struct crypto_aes_ctx *aes_ctx(void *raw_ctx)
  146. {
  147. unsigned long addr = (unsigned long)raw_ctx;
  148. unsigned long align = AESNI_ALIGN;
  149. if (align <= crypto_tfm_ctx_alignment())
  150. align = 1;
  151. return (struct crypto_aes_ctx *)ALIGN(addr, align);
  152. }
  153. static int aes_set_key_common(struct crypto_tfm *tfm, void *raw_ctx,
  154. const u8 *in_key, unsigned int key_len)
  155. {
  156. struct crypto_aes_ctx *ctx = aes_ctx(raw_ctx);
  157. u32 *flags = &tfm->crt_flags;
  158. int err;
  159. if (key_len != AES_KEYSIZE_128 && key_len != AES_KEYSIZE_192 &&
  160. key_len != AES_KEYSIZE_256) {
  161. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  162. return -EINVAL;
  163. }
  164. if (!irq_fpu_usable())
  165. err = crypto_aes_expand_key(ctx, in_key, key_len);
  166. else {
  167. kernel_fpu_begin();
  168. err = aesni_set_key(ctx, in_key, key_len);
  169. kernel_fpu_end();
  170. }
  171. return err;
  172. }
  173. static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
  174. unsigned int key_len)
  175. {
  176. return aes_set_key_common(tfm, crypto_tfm_ctx(tfm), in_key, key_len);
  177. }
  178. static void aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  179. {
  180. struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
  181. if (!irq_fpu_usable())
  182. crypto_aes_encrypt_x86(ctx, dst, src);
  183. else {
  184. kernel_fpu_begin();
  185. aesni_enc(ctx, dst, src);
  186. kernel_fpu_end();
  187. }
  188. }
  189. static void aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  190. {
  191. struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
  192. if (!irq_fpu_usable())
  193. crypto_aes_decrypt_x86(ctx, dst, src);
  194. else {
  195. kernel_fpu_begin();
  196. aesni_dec(ctx, dst, src);
  197. kernel_fpu_end();
  198. }
  199. }
  200. static void __aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  201. {
  202. struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
  203. aesni_enc(ctx, dst, src);
  204. }
  205. static void __aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  206. {
  207. struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
  208. aesni_dec(ctx, dst, src);
  209. }
  210. static int ecb_encrypt(struct blkcipher_desc *desc,
  211. struct scatterlist *dst, struct scatterlist *src,
  212. unsigned int nbytes)
  213. {
  214. struct crypto_aes_ctx *ctx = aes_ctx(crypto_blkcipher_ctx(desc->tfm));
  215. struct blkcipher_walk walk;
  216. int err;
  217. blkcipher_walk_init(&walk, dst, src, nbytes);
  218. err = blkcipher_walk_virt(desc, &walk);
  219. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  220. kernel_fpu_begin();
  221. while ((nbytes = walk.nbytes)) {
  222. aesni_ecb_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  223. nbytes & AES_BLOCK_MASK);
  224. nbytes &= AES_BLOCK_SIZE - 1;
  225. err = blkcipher_walk_done(desc, &walk, nbytes);
  226. }
  227. kernel_fpu_end();
  228. return err;
  229. }
  230. static int ecb_decrypt(struct blkcipher_desc *desc,
  231. struct scatterlist *dst, struct scatterlist *src,
  232. unsigned int nbytes)
  233. {
  234. struct crypto_aes_ctx *ctx = aes_ctx(crypto_blkcipher_ctx(desc->tfm));
  235. struct blkcipher_walk walk;
  236. int err;
  237. blkcipher_walk_init(&walk, dst, src, nbytes);
  238. err = blkcipher_walk_virt(desc, &walk);
  239. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  240. kernel_fpu_begin();
  241. while ((nbytes = walk.nbytes)) {
  242. aesni_ecb_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  243. nbytes & AES_BLOCK_MASK);
  244. nbytes &= AES_BLOCK_SIZE - 1;
  245. err = blkcipher_walk_done(desc, &walk, nbytes);
  246. }
  247. kernel_fpu_end();
  248. return err;
  249. }
  250. static int cbc_encrypt(struct blkcipher_desc *desc,
  251. struct scatterlist *dst, struct scatterlist *src,
  252. unsigned int nbytes)
  253. {
  254. struct crypto_aes_ctx *ctx = aes_ctx(crypto_blkcipher_ctx(desc->tfm));
  255. struct blkcipher_walk walk;
  256. int err;
  257. blkcipher_walk_init(&walk, dst, src, nbytes);
  258. err = blkcipher_walk_virt(desc, &walk);
  259. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  260. kernel_fpu_begin();
  261. while ((nbytes = walk.nbytes)) {
  262. aesni_cbc_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  263. nbytes & AES_BLOCK_MASK, walk.iv);
  264. nbytes &= AES_BLOCK_SIZE - 1;
  265. err = blkcipher_walk_done(desc, &walk, nbytes);
  266. }
  267. kernel_fpu_end();
  268. return err;
  269. }
  270. static int cbc_decrypt(struct blkcipher_desc *desc,
  271. struct scatterlist *dst, struct scatterlist *src,
  272. unsigned int nbytes)
  273. {
  274. struct crypto_aes_ctx *ctx = aes_ctx(crypto_blkcipher_ctx(desc->tfm));
  275. struct blkcipher_walk walk;
  276. int err;
  277. blkcipher_walk_init(&walk, dst, src, nbytes);
  278. err = blkcipher_walk_virt(desc, &walk);
  279. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  280. kernel_fpu_begin();
  281. while ((nbytes = walk.nbytes)) {
  282. aesni_cbc_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  283. nbytes & AES_BLOCK_MASK, walk.iv);
  284. nbytes &= AES_BLOCK_SIZE - 1;
  285. err = blkcipher_walk_done(desc, &walk, nbytes);
  286. }
  287. kernel_fpu_end();
  288. return err;
  289. }
  290. #ifdef CONFIG_X86_64
  291. static void ctr_crypt_final(struct crypto_aes_ctx *ctx,
  292. struct blkcipher_walk *walk)
  293. {
  294. u8 *ctrblk = walk->iv;
  295. u8 keystream[AES_BLOCK_SIZE];
  296. u8 *src = walk->src.virt.addr;
  297. u8 *dst = walk->dst.virt.addr;
  298. unsigned int nbytes = walk->nbytes;
  299. aesni_enc(ctx, keystream, ctrblk);
  300. crypto_xor(keystream, src, nbytes);
  301. memcpy(dst, keystream, nbytes);
  302. crypto_inc(ctrblk, AES_BLOCK_SIZE);
  303. }
  304. static int ctr_crypt(struct blkcipher_desc *desc,
  305. struct scatterlist *dst, struct scatterlist *src,
  306. unsigned int nbytes)
  307. {
  308. struct crypto_aes_ctx *ctx = aes_ctx(crypto_blkcipher_ctx(desc->tfm));
  309. struct blkcipher_walk walk;
  310. int err;
  311. blkcipher_walk_init(&walk, dst, src, nbytes);
  312. err = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE);
  313. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  314. kernel_fpu_begin();
  315. while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
  316. aesni_ctr_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
  317. nbytes & AES_BLOCK_MASK, walk.iv);
  318. nbytes &= AES_BLOCK_SIZE - 1;
  319. err = blkcipher_walk_done(desc, &walk, nbytes);
  320. }
  321. if (walk.nbytes) {
  322. ctr_crypt_final(ctx, &walk);
  323. err = blkcipher_walk_done(desc, &walk, 0);
  324. }
  325. kernel_fpu_end();
  326. return err;
  327. }
  328. #endif
  329. static int ablk_ecb_init(struct crypto_tfm *tfm)
  330. {
  331. return ablk_init_common(tfm, "__driver-ecb-aes-aesni");
  332. }
  333. static int ablk_cbc_init(struct crypto_tfm *tfm)
  334. {
  335. return ablk_init_common(tfm, "__driver-cbc-aes-aesni");
  336. }
  337. #ifdef CONFIG_X86_64
  338. static int ablk_ctr_init(struct crypto_tfm *tfm)
  339. {
  340. return ablk_init_common(tfm, "__driver-ctr-aes-aesni");
  341. }
  342. #ifdef HAS_CTR
  343. static int ablk_rfc3686_ctr_init(struct crypto_tfm *tfm)
  344. {
  345. return ablk_init_common(tfm, "rfc3686(__driver-ctr-aes-aesni)");
  346. }
  347. #endif
  348. #endif
  349. #ifdef HAS_PCBC
  350. static int ablk_pcbc_init(struct crypto_tfm *tfm)
  351. {
  352. return ablk_init_common(tfm, "fpu(pcbc(__driver-aes-aesni))");
  353. }
  354. #endif
  355. static void lrw_xts_encrypt_callback(void *ctx, u8 *blks, unsigned int nbytes)
  356. {
  357. aesni_ecb_enc(ctx, blks, blks, nbytes);
  358. }
  359. static void lrw_xts_decrypt_callback(void *ctx, u8 *blks, unsigned int nbytes)
  360. {
  361. aesni_ecb_dec(ctx, blks, blks, nbytes);
  362. }
  363. static int lrw_aesni_setkey(struct crypto_tfm *tfm, const u8 *key,
  364. unsigned int keylen)
  365. {
  366. struct aesni_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
  367. int err;
  368. err = aes_set_key_common(tfm, ctx->raw_aes_ctx, key,
  369. keylen - AES_BLOCK_SIZE);
  370. if (err)
  371. return err;
  372. return lrw_init_table(&ctx->lrw_table, key + keylen - AES_BLOCK_SIZE);
  373. }
  374. static void lrw_aesni_exit_tfm(struct crypto_tfm *tfm)
  375. {
  376. struct aesni_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
  377. lrw_free_table(&ctx->lrw_table);
  378. }
  379. static int lrw_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  380. struct scatterlist *src, unsigned int nbytes)
  381. {
  382. struct aesni_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  383. be128 buf[8];
  384. struct lrw_crypt_req req = {
  385. .tbuf = buf,
  386. .tbuflen = sizeof(buf),
  387. .table_ctx = &ctx->lrw_table,
  388. .crypt_ctx = aes_ctx(ctx->raw_aes_ctx),
  389. .crypt_fn = lrw_xts_encrypt_callback,
  390. };
  391. int ret;
  392. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  393. kernel_fpu_begin();
  394. ret = lrw_crypt(desc, dst, src, nbytes, &req);
  395. kernel_fpu_end();
  396. return ret;
  397. }
  398. static int lrw_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  399. struct scatterlist *src, unsigned int nbytes)
  400. {
  401. struct aesni_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  402. be128 buf[8];
  403. struct lrw_crypt_req req = {
  404. .tbuf = buf,
  405. .tbuflen = sizeof(buf),
  406. .table_ctx = &ctx->lrw_table,
  407. .crypt_ctx = aes_ctx(ctx->raw_aes_ctx),
  408. .crypt_fn = lrw_xts_decrypt_callback,
  409. };
  410. int ret;
  411. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  412. kernel_fpu_begin();
  413. ret = lrw_crypt(desc, dst, src, nbytes, &req);
  414. kernel_fpu_end();
  415. return ret;
  416. }
  417. static int xts_aesni_setkey(struct crypto_tfm *tfm, const u8 *key,
  418. unsigned int keylen)
  419. {
  420. struct aesni_xts_ctx *ctx = crypto_tfm_ctx(tfm);
  421. u32 *flags = &tfm->crt_flags;
  422. int err;
  423. /* key consists of keys of equal size concatenated, therefore
  424. * the length must be even
  425. */
  426. if (keylen % 2) {
  427. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  428. return -EINVAL;
  429. }
  430. /* first half of xts-key is for crypt */
  431. err = aes_set_key_common(tfm, ctx->raw_crypt_ctx, key, keylen / 2);
  432. if (err)
  433. return err;
  434. /* second half of xts-key is for tweak */
  435. return aes_set_key_common(tfm, ctx->raw_tweak_ctx, key + keylen / 2,
  436. keylen / 2);
  437. }
  438. static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  439. struct scatterlist *src, unsigned int nbytes)
  440. {
  441. struct aesni_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  442. be128 buf[8];
  443. struct xts_crypt_req req = {
  444. .tbuf = buf,
  445. .tbuflen = sizeof(buf),
  446. .tweak_ctx = aes_ctx(ctx->raw_tweak_ctx),
  447. .tweak_fn = XTS_TWEAK_CAST(aesni_enc),
  448. .crypt_ctx = aes_ctx(ctx->raw_crypt_ctx),
  449. .crypt_fn = lrw_xts_encrypt_callback,
  450. };
  451. int ret;
  452. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  453. kernel_fpu_begin();
  454. ret = xts_crypt(desc, dst, src, nbytes, &req);
  455. kernel_fpu_end();
  456. return ret;
  457. }
  458. static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  459. struct scatterlist *src, unsigned int nbytes)
  460. {
  461. struct aesni_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  462. be128 buf[8];
  463. struct xts_crypt_req req = {
  464. .tbuf = buf,
  465. .tbuflen = sizeof(buf),
  466. .tweak_ctx = aes_ctx(ctx->raw_tweak_ctx),
  467. .tweak_fn = XTS_TWEAK_CAST(aesni_enc),
  468. .crypt_ctx = aes_ctx(ctx->raw_crypt_ctx),
  469. .crypt_fn = lrw_xts_decrypt_callback,
  470. };
  471. int ret;
  472. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  473. kernel_fpu_begin();
  474. ret = xts_crypt(desc, dst, src, nbytes, &req);
  475. kernel_fpu_end();
  476. return ret;
  477. }
  478. #ifdef CONFIG_X86_64
  479. static int rfc4106_init(struct crypto_tfm *tfm)
  480. {
  481. struct cryptd_aead *cryptd_tfm;
  482. struct aesni_rfc4106_gcm_ctx *ctx = (struct aesni_rfc4106_gcm_ctx *)
  483. PTR_ALIGN((u8 *)crypto_tfm_ctx(tfm), AESNI_ALIGN);
  484. struct crypto_aead *cryptd_child;
  485. struct aesni_rfc4106_gcm_ctx *child_ctx;
  486. cryptd_tfm = cryptd_alloc_aead("__driver-gcm-aes-aesni", 0, 0);
  487. if (IS_ERR(cryptd_tfm))
  488. return PTR_ERR(cryptd_tfm);
  489. cryptd_child = cryptd_aead_child(cryptd_tfm);
  490. child_ctx = aesni_rfc4106_gcm_ctx_get(cryptd_child);
  491. memcpy(child_ctx, ctx, sizeof(*ctx));
  492. ctx->cryptd_tfm = cryptd_tfm;
  493. tfm->crt_aead.reqsize = sizeof(struct aead_request)
  494. + crypto_aead_reqsize(&cryptd_tfm->base);
  495. return 0;
  496. }
  497. static void rfc4106_exit(struct crypto_tfm *tfm)
  498. {
  499. struct aesni_rfc4106_gcm_ctx *ctx =
  500. (struct aesni_rfc4106_gcm_ctx *)
  501. PTR_ALIGN((u8 *)crypto_tfm_ctx(tfm), AESNI_ALIGN);
  502. if (!IS_ERR(ctx->cryptd_tfm))
  503. cryptd_free_aead(ctx->cryptd_tfm);
  504. return;
  505. }
  506. static void
  507. rfc4106_set_hash_subkey_done(struct crypto_async_request *req, int err)
  508. {
  509. struct aesni_gcm_set_hash_subkey_result *result = req->data;
  510. if (err == -EINPROGRESS)
  511. return;
  512. result->err = err;
  513. complete(&result->completion);
  514. }
  515. static int
  516. rfc4106_set_hash_subkey(u8 *hash_subkey, const u8 *key, unsigned int key_len)
  517. {
  518. struct crypto_ablkcipher *ctr_tfm;
  519. struct ablkcipher_request *req;
  520. int ret = -EINVAL;
  521. struct aesni_hash_subkey_req_data *req_data;
  522. ctr_tfm = crypto_alloc_ablkcipher("ctr(aes)", 0, 0);
  523. if (IS_ERR(ctr_tfm))
  524. return PTR_ERR(ctr_tfm);
  525. crypto_ablkcipher_clear_flags(ctr_tfm, ~0);
  526. ret = crypto_ablkcipher_setkey(ctr_tfm, key, key_len);
  527. if (ret)
  528. goto out_free_ablkcipher;
  529. ret = -ENOMEM;
  530. req = ablkcipher_request_alloc(ctr_tfm, GFP_KERNEL);
  531. if (!req)
  532. goto out_free_ablkcipher;
  533. req_data = kmalloc(sizeof(*req_data), GFP_KERNEL);
  534. if (!req_data)
  535. goto out_free_request;
  536. memset(req_data->iv, 0, sizeof(req_data->iv));
  537. /* Clear the data in the hash sub key container to zero.*/
  538. /* We want to cipher all zeros to create the hash sub key. */
  539. memset(hash_subkey, 0, RFC4106_HASH_SUBKEY_SIZE);
  540. init_completion(&req_data->result.completion);
  541. sg_init_one(&req_data->sg, hash_subkey, RFC4106_HASH_SUBKEY_SIZE);
  542. ablkcipher_request_set_tfm(req, ctr_tfm);
  543. ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP |
  544. CRYPTO_TFM_REQ_MAY_BACKLOG,
  545. rfc4106_set_hash_subkey_done,
  546. &req_data->result);
  547. ablkcipher_request_set_crypt(req, &req_data->sg,
  548. &req_data->sg, RFC4106_HASH_SUBKEY_SIZE, req_data->iv);
  549. ret = crypto_ablkcipher_encrypt(req);
  550. if (ret == -EINPROGRESS || ret == -EBUSY) {
  551. ret = wait_for_completion_interruptible
  552. (&req_data->result.completion);
  553. if (!ret)
  554. ret = req_data->result.err;
  555. }
  556. kfree(req_data);
  557. out_free_request:
  558. ablkcipher_request_free(req);
  559. out_free_ablkcipher:
  560. crypto_free_ablkcipher(ctr_tfm);
  561. return ret;
  562. }
  563. static int rfc4106_set_key(struct crypto_aead *parent, const u8 *key,
  564. unsigned int key_len)
  565. {
  566. int ret = 0;
  567. struct crypto_tfm *tfm = crypto_aead_tfm(parent);
  568. struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(parent);
  569. struct crypto_aead *cryptd_child = cryptd_aead_child(ctx->cryptd_tfm);
  570. struct aesni_rfc4106_gcm_ctx *child_ctx =
  571. aesni_rfc4106_gcm_ctx_get(cryptd_child);
  572. u8 *new_key_align, *new_key_mem = NULL;
  573. if (key_len < 4) {
  574. crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  575. return -EINVAL;
  576. }
  577. /*Account for 4 byte nonce at the end.*/
  578. key_len -= 4;
  579. if (key_len != AES_KEYSIZE_128) {
  580. crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  581. return -EINVAL;
  582. }
  583. memcpy(ctx->nonce, key + key_len, sizeof(ctx->nonce));
  584. /*This must be on a 16 byte boundary!*/
  585. if ((unsigned long)(&(ctx->aes_key_expanded.key_enc[0])) % AESNI_ALIGN)
  586. return -EINVAL;
  587. if ((unsigned long)key % AESNI_ALIGN) {
  588. /*key is not aligned: use an auxuliar aligned pointer*/
  589. new_key_mem = kmalloc(key_len+AESNI_ALIGN, GFP_KERNEL);
  590. if (!new_key_mem)
  591. return -ENOMEM;
  592. new_key_align = PTR_ALIGN(new_key_mem, AESNI_ALIGN);
  593. memcpy(new_key_align, key, key_len);
  594. key = new_key_align;
  595. }
  596. if (!irq_fpu_usable())
  597. ret = crypto_aes_expand_key(&(ctx->aes_key_expanded),
  598. key, key_len);
  599. else {
  600. kernel_fpu_begin();
  601. ret = aesni_set_key(&(ctx->aes_key_expanded), key, key_len);
  602. kernel_fpu_end();
  603. }
  604. /*This must be on a 16 byte boundary!*/
  605. if ((unsigned long)(&(ctx->hash_subkey[0])) % AESNI_ALIGN) {
  606. ret = -EINVAL;
  607. goto exit;
  608. }
  609. ret = rfc4106_set_hash_subkey(ctx->hash_subkey, key, key_len);
  610. memcpy(child_ctx, ctx, sizeof(*ctx));
  611. exit:
  612. kfree(new_key_mem);
  613. return ret;
  614. }
  615. /* This is the Integrity Check Value (aka the authentication tag length and can
  616. * be 8, 12 or 16 bytes long. */
  617. static int rfc4106_set_authsize(struct crypto_aead *parent,
  618. unsigned int authsize)
  619. {
  620. struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(parent);
  621. struct crypto_aead *cryptd_child = cryptd_aead_child(ctx->cryptd_tfm);
  622. switch (authsize) {
  623. case 8:
  624. case 12:
  625. case 16:
  626. break;
  627. default:
  628. return -EINVAL;
  629. }
  630. crypto_aead_crt(parent)->authsize = authsize;
  631. crypto_aead_crt(cryptd_child)->authsize = authsize;
  632. return 0;
  633. }
  634. static int rfc4106_encrypt(struct aead_request *req)
  635. {
  636. int ret;
  637. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  638. struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
  639. if (!irq_fpu_usable()) {
  640. struct aead_request *cryptd_req =
  641. (struct aead_request *) aead_request_ctx(req);
  642. memcpy(cryptd_req, req, sizeof(*req));
  643. aead_request_set_tfm(cryptd_req, &ctx->cryptd_tfm->base);
  644. return crypto_aead_encrypt(cryptd_req);
  645. } else {
  646. struct crypto_aead *cryptd_child = cryptd_aead_child(ctx->cryptd_tfm);
  647. kernel_fpu_begin();
  648. ret = cryptd_child->base.crt_aead.encrypt(req);
  649. kernel_fpu_end();
  650. return ret;
  651. }
  652. }
  653. static int rfc4106_decrypt(struct aead_request *req)
  654. {
  655. int ret;
  656. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  657. struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
  658. if (!irq_fpu_usable()) {
  659. struct aead_request *cryptd_req =
  660. (struct aead_request *) aead_request_ctx(req);
  661. memcpy(cryptd_req, req, sizeof(*req));
  662. aead_request_set_tfm(cryptd_req, &ctx->cryptd_tfm->base);
  663. return crypto_aead_decrypt(cryptd_req);
  664. } else {
  665. struct crypto_aead *cryptd_child = cryptd_aead_child(ctx->cryptd_tfm);
  666. kernel_fpu_begin();
  667. ret = cryptd_child->base.crt_aead.decrypt(req);
  668. kernel_fpu_end();
  669. return ret;
  670. }
  671. }
  672. static int __driver_rfc4106_encrypt(struct aead_request *req)
  673. {
  674. u8 one_entry_in_sg = 0;
  675. u8 *src, *dst, *assoc;
  676. __be32 counter = cpu_to_be32(1);
  677. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  678. struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
  679. void *aes_ctx = &(ctx->aes_key_expanded);
  680. unsigned long auth_tag_len = crypto_aead_authsize(tfm);
  681. u8 iv_tab[16+AESNI_ALIGN];
  682. u8* iv = (u8 *) PTR_ALIGN((u8 *)iv_tab, AESNI_ALIGN);
  683. struct scatter_walk src_sg_walk;
  684. struct scatter_walk assoc_sg_walk;
  685. struct scatter_walk dst_sg_walk;
  686. unsigned int i;
  687. /* Assuming we are supporting rfc4106 64-bit extended */
  688. /* sequence numbers We need to have the AAD length equal */
  689. /* to 8 or 12 bytes */
  690. if (unlikely(req->assoclen != 8 && req->assoclen != 12))
  691. return -EINVAL;
  692. /* IV below built */
  693. for (i = 0; i < 4; i++)
  694. *(iv+i) = ctx->nonce[i];
  695. for (i = 0; i < 8; i++)
  696. *(iv+4+i) = req->iv[i];
  697. *((__be32 *)(iv+12)) = counter;
  698. if ((sg_is_last(req->src)) && (sg_is_last(req->assoc))) {
  699. one_entry_in_sg = 1;
  700. scatterwalk_start(&src_sg_walk, req->src);
  701. scatterwalk_start(&assoc_sg_walk, req->assoc);
  702. src = scatterwalk_map(&src_sg_walk);
  703. assoc = scatterwalk_map(&assoc_sg_walk);
  704. dst = src;
  705. if (unlikely(req->src != req->dst)) {
  706. scatterwalk_start(&dst_sg_walk, req->dst);
  707. dst = scatterwalk_map(&dst_sg_walk);
  708. }
  709. } else {
  710. /* Allocate memory for src, dst, assoc */
  711. src = kmalloc(req->cryptlen + auth_tag_len + req->assoclen,
  712. GFP_ATOMIC);
  713. if (unlikely(!src))
  714. return -ENOMEM;
  715. assoc = (src + req->cryptlen + auth_tag_len);
  716. scatterwalk_map_and_copy(src, req->src, 0, req->cryptlen, 0);
  717. scatterwalk_map_and_copy(assoc, req->assoc, 0,
  718. req->assoclen, 0);
  719. dst = src;
  720. }
  721. aesni_gcm_enc(aes_ctx, dst, src, (unsigned long)req->cryptlen, iv,
  722. ctx->hash_subkey, assoc, (unsigned long)req->assoclen, dst
  723. + ((unsigned long)req->cryptlen), auth_tag_len);
  724. /* The authTag (aka the Integrity Check Value) needs to be written
  725. * back to the packet. */
  726. if (one_entry_in_sg) {
  727. if (unlikely(req->src != req->dst)) {
  728. scatterwalk_unmap(dst);
  729. scatterwalk_done(&dst_sg_walk, 0, 0);
  730. }
  731. scatterwalk_unmap(src);
  732. scatterwalk_unmap(assoc);
  733. scatterwalk_done(&src_sg_walk, 0, 0);
  734. scatterwalk_done(&assoc_sg_walk, 0, 0);
  735. } else {
  736. scatterwalk_map_and_copy(dst, req->dst, 0,
  737. req->cryptlen + auth_tag_len, 1);
  738. kfree(src);
  739. }
  740. return 0;
  741. }
  742. static int __driver_rfc4106_decrypt(struct aead_request *req)
  743. {
  744. u8 one_entry_in_sg = 0;
  745. u8 *src, *dst, *assoc;
  746. unsigned long tempCipherLen = 0;
  747. __be32 counter = cpu_to_be32(1);
  748. int retval = 0;
  749. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  750. struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
  751. void *aes_ctx = &(ctx->aes_key_expanded);
  752. unsigned long auth_tag_len = crypto_aead_authsize(tfm);
  753. u8 iv_and_authTag[32+AESNI_ALIGN];
  754. u8 *iv = (u8 *) PTR_ALIGN((u8 *)iv_and_authTag, AESNI_ALIGN);
  755. u8 *authTag = iv + 16;
  756. struct scatter_walk src_sg_walk;
  757. struct scatter_walk assoc_sg_walk;
  758. struct scatter_walk dst_sg_walk;
  759. unsigned int i;
  760. if (unlikely((req->cryptlen < auth_tag_len) ||
  761. (req->assoclen != 8 && req->assoclen != 12)))
  762. return -EINVAL;
  763. /* Assuming we are supporting rfc4106 64-bit extended */
  764. /* sequence numbers We need to have the AAD length */
  765. /* equal to 8 or 12 bytes */
  766. tempCipherLen = (unsigned long)(req->cryptlen - auth_tag_len);
  767. /* IV below built */
  768. for (i = 0; i < 4; i++)
  769. *(iv+i) = ctx->nonce[i];
  770. for (i = 0; i < 8; i++)
  771. *(iv+4+i) = req->iv[i];
  772. *((__be32 *)(iv+12)) = counter;
  773. if ((sg_is_last(req->src)) && (sg_is_last(req->assoc))) {
  774. one_entry_in_sg = 1;
  775. scatterwalk_start(&src_sg_walk, req->src);
  776. scatterwalk_start(&assoc_sg_walk, req->assoc);
  777. src = scatterwalk_map(&src_sg_walk);
  778. assoc = scatterwalk_map(&assoc_sg_walk);
  779. dst = src;
  780. if (unlikely(req->src != req->dst)) {
  781. scatterwalk_start(&dst_sg_walk, req->dst);
  782. dst = scatterwalk_map(&dst_sg_walk);
  783. }
  784. } else {
  785. /* Allocate memory for src, dst, assoc */
  786. src = kmalloc(req->cryptlen + req->assoclen, GFP_ATOMIC);
  787. if (!src)
  788. return -ENOMEM;
  789. assoc = (src + req->cryptlen + auth_tag_len);
  790. scatterwalk_map_and_copy(src, req->src, 0, req->cryptlen, 0);
  791. scatterwalk_map_and_copy(assoc, req->assoc, 0,
  792. req->assoclen, 0);
  793. dst = src;
  794. }
  795. aesni_gcm_dec(aes_ctx, dst, src, tempCipherLen, iv,
  796. ctx->hash_subkey, assoc, (unsigned long)req->assoclen,
  797. authTag, auth_tag_len);
  798. /* Compare generated tag with passed in tag. */
  799. retval = memcmp(src + tempCipherLen, authTag, auth_tag_len) ?
  800. -EBADMSG : 0;
  801. if (one_entry_in_sg) {
  802. if (unlikely(req->src != req->dst)) {
  803. scatterwalk_unmap(dst);
  804. scatterwalk_done(&dst_sg_walk, 0, 0);
  805. }
  806. scatterwalk_unmap(src);
  807. scatterwalk_unmap(assoc);
  808. scatterwalk_done(&src_sg_walk, 0, 0);
  809. scatterwalk_done(&assoc_sg_walk, 0, 0);
  810. } else {
  811. scatterwalk_map_and_copy(dst, req->dst, 0, req->cryptlen, 1);
  812. kfree(src);
  813. }
  814. return retval;
  815. }
  816. #endif
  817. static struct crypto_alg aesni_algs[] = { {
  818. .cra_name = "aes",
  819. .cra_driver_name = "aes-aesni",
  820. .cra_priority = 300,
  821. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  822. .cra_blocksize = AES_BLOCK_SIZE,
  823. .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
  824. AESNI_ALIGN - 1,
  825. .cra_alignmask = 0,
  826. .cra_module = THIS_MODULE,
  827. .cra_u = {
  828. .cipher = {
  829. .cia_min_keysize = AES_MIN_KEY_SIZE,
  830. .cia_max_keysize = AES_MAX_KEY_SIZE,
  831. .cia_setkey = aes_set_key,
  832. .cia_encrypt = aes_encrypt,
  833. .cia_decrypt = aes_decrypt
  834. }
  835. }
  836. }, {
  837. .cra_name = "__aes-aesni",
  838. .cra_driver_name = "__driver-aes-aesni",
  839. .cra_priority = 0,
  840. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  841. .cra_blocksize = AES_BLOCK_SIZE,
  842. .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
  843. AESNI_ALIGN - 1,
  844. .cra_alignmask = 0,
  845. .cra_module = THIS_MODULE,
  846. .cra_u = {
  847. .cipher = {
  848. .cia_min_keysize = AES_MIN_KEY_SIZE,
  849. .cia_max_keysize = AES_MAX_KEY_SIZE,
  850. .cia_setkey = aes_set_key,
  851. .cia_encrypt = __aes_encrypt,
  852. .cia_decrypt = __aes_decrypt
  853. }
  854. }
  855. }, {
  856. .cra_name = "__ecb-aes-aesni",
  857. .cra_driver_name = "__driver-ecb-aes-aesni",
  858. .cra_priority = 0,
  859. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  860. .cra_blocksize = AES_BLOCK_SIZE,
  861. .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
  862. AESNI_ALIGN - 1,
  863. .cra_alignmask = 0,
  864. .cra_type = &crypto_blkcipher_type,
  865. .cra_module = THIS_MODULE,
  866. .cra_u = {
  867. .blkcipher = {
  868. .min_keysize = AES_MIN_KEY_SIZE,
  869. .max_keysize = AES_MAX_KEY_SIZE,
  870. .setkey = aes_set_key,
  871. .encrypt = ecb_encrypt,
  872. .decrypt = ecb_decrypt,
  873. },
  874. },
  875. }, {
  876. .cra_name = "__cbc-aes-aesni",
  877. .cra_driver_name = "__driver-cbc-aes-aesni",
  878. .cra_priority = 0,
  879. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  880. .cra_blocksize = AES_BLOCK_SIZE,
  881. .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
  882. AESNI_ALIGN - 1,
  883. .cra_alignmask = 0,
  884. .cra_type = &crypto_blkcipher_type,
  885. .cra_module = THIS_MODULE,
  886. .cra_u = {
  887. .blkcipher = {
  888. .min_keysize = AES_MIN_KEY_SIZE,
  889. .max_keysize = AES_MAX_KEY_SIZE,
  890. .setkey = aes_set_key,
  891. .encrypt = cbc_encrypt,
  892. .decrypt = cbc_decrypt,
  893. },
  894. },
  895. }, {
  896. .cra_name = "ecb(aes)",
  897. .cra_driver_name = "ecb-aes-aesni",
  898. .cra_priority = 400,
  899. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  900. .cra_blocksize = AES_BLOCK_SIZE,
  901. .cra_ctxsize = sizeof(struct async_helper_ctx),
  902. .cra_alignmask = 0,
  903. .cra_type = &crypto_ablkcipher_type,
  904. .cra_module = THIS_MODULE,
  905. .cra_init = ablk_ecb_init,
  906. .cra_exit = ablk_exit,
  907. .cra_u = {
  908. .ablkcipher = {
  909. .min_keysize = AES_MIN_KEY_SIZE,
  910. .max_keysize = AES_MAX_KEY_SIZE,
  911. .setkey = ablk_set_key,
  912. .encrypt = ablk_encrypt,
  913. .decrypt = ablk_decrypt,
  914. },
  915. },
  916. }, {
  917. .cra_name = "cbc(aes)",
  918. .cra_driver_name = "cbc-aes-aesni",
  919. .cra_priority = 400,
  920. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  921. .cra_blocksize = AES_BLOCK_SIZE,
  922. .cra_ctxsize = sizeof(struct async_helper_ctx),
  923. .cra_alignmask = 0,
  924. .cra_type = &crypto_ablkcipher_type,
  925. .cra_module = THIS_MODULE,
  926. .cra_init = ablk_cbc_init,
  927. .cra_exit = ablk_exit,
  928. .cra_u = {
  929. .ablkcipher = {
  930. .min_keysize = AES_MIN_KEY_SIZE,
  931. .max_keysize = AES_MAX_KEY_SIZE,
  932. .ivsize = AES_BLOCK_SIZE,
  933. .setkey = ablk_set_key,
  934. .encrypt = ablk_encrypt,
  935. .decrypt = ablk_decrypt,
  936. },
  937. },
  938. #ifdef CONFIG_X86_64
  939. }, {
  940. .cra_name = "__ctr-aes-aesni",
  941. .cra_driver_name = "__driver-ctr-aes-aesni",
  942. .cra_priority = 0,
  943. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  944. .cra_blocksize = 1,
  945. .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
  946. AESNI_ALIGN - 1,
  947. .cra_alignmask = 0,
  948. .cra_type = &crypto_blkcipher_type,
  949. .cra_module = THIS_MODULE,
  950. .cra_u = {
  951. .blkcipher = {
  952. .min_keysize = AES_MIN_KEY_SIZE,
  953. .max_keysize = AES_MAX_KEY_SIZE,
  954. .ivsize = AES_BLOCK_SIZE,
  955. .setkey = aes_set_key,
  956. .encrypt = ctr_crypt,
  957. .decrypt = ctr_crypt,
  958. },
  959. },
  960. }, {
  961. .cra_name = "ctr(aes)",
  962. .cra_driver_name = "ctr-aes-aesni",
  963. .cra_priority = 400,
  964. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  965. .cra_blocksize = 1,
  966. .cra_ctxsize = sizeof(struct async_helper_ctx),
  967. .cra_alignmask = 0,
  968. .cra_type = &crypto_ablkcipher_type,
  969. .cra_module = THIS_MODULE,
  970. .cra_init = ablk_ctr_init,
  971. .cra_exit = ablk_exit,
  972. .cra_u = {
  973. .ablkcipher = {
  974. .min_keysize = AES_MIN_KEY_SIZE,
  975. .max_keysize = AES_MAX_KEY_SIZE,
  976. .ivsize = AES_BLOCK_SIZE,
  977. .setkey = ablk_set_key,
  978. .encrypt = ablk_encrypt,
  979. .decrypt = ablk_encrypt,
  980. .geniv = "chainiv",
  981. },
  982. },
  983. }, {
  984. .cra_name = "__gcm-aes-aesni",
  985. .cra_driver_name = "__driver-gcm-aes-aesni",
  986. .cra_priority = 0,
  987. .cra_flags = CRYPTO_ALG_TYPE_AEAD,
  988. .cra_blocksize = 1,
  989. .cra_ctxsize = sizeof(struct aesni_rfc4106_gcm_ctx) +
  990. AESNI_ALIGN,
  991. .cra_alignmask = 0,
  992. .cra_type = &crypto_aead_type,
  993. .cra_module = THIS_MODULE,
  994. .cra_u = {
  995. .aead = {
  996. .encrypt = __driver_rfc4106_encrypt,
  997. .decrypt = __driver_rfc4106_decrypt,
  998. },
  999. },
  1000. }, {
  1001. .cra_name = "rfc4106(gcm(aes))",
  1002. .cra_driver_name = "rfc4106-gcm-aesni",
  1003. .cra_priority = 400,
  1004. .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
  1005. .cra_blocksize = 1,
  1006. .cra_ctxsize = sizeof(struct aesni_rfc4106_gcm_ctx) +
  1007. AESNI_ALIGN,
  1008. .cra_alignmask = 0,
  1009. .cra_type = &crypto_nivaead_type,
  1010. .cra_module = THIS_MODULE,
  1011. .cra_init = rfc4106_init,
  1012. .cra_exit = rfc4106_exit,
  1013. .cra_u = {
  1014. .aead = {
  1015. .setkey = rfc4106_set_key,
  1016. .setauthsize = rfc4106_set_authsize,
  1017. .encrypt = rfc4106_encrypt,
  1018. .decrypt = rfc4106_decrypt,
  1019. .geniv = "seqiv",
  1020. .ivsize = 8,
  1021. .maxauthsize = 16,
  1022. },
  1023. },
  1024. #ifdef HAS_CTR
  1025. }, {
  1026. .cra_name = "rfc3686(ctr(aes))",
  1027. .cra_driver_name = "rfc3686-ctr-aes-aesni",
  1028. .cra_priority = 400,
  1029. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1030. .cra_blocksize = 1,
  1031. .cra_ctxsize = sizeof(struct async_helper_ctx),
  1032. .cra_alignmask = 0,
  1033. .cra_type = &crypto_ablkcipher_type,
  1034. .cra_module = THIS_MODULE,
  1035. .cra_init = ablk_rfc3686_ctr_init,
  1036. .cra_exit = ablk_exit,
  1037. .cra_u = {
  1038. .ablkcipher = {
  1039. .min_keysize = AES_MIN_KEY_SIZE +
  1040. CTR_RFC3686_NONCE_SIZE,
  1041. .max_keysize = AES_MAX_KEY_SIZE +
  1042. CTR_RFC3686_NONCE_SIZE,
  1043. .ivsize = CTR_RFC3686_IV_SIZE,
  1044. .setkey = ablk_set_key,
  1045. .encrypt = ablk_encrypt,
  1046. .decrypt = ablk_decrypt,
  1047. .geniv = "seqiv",
  1048. },
  1049. },
  1050. #endif
  1051. #endif
  1052. #ifdef HAS_PCBC
  1053. }, {
  1054. .cra_name = "pcbc(aes)",
  1055. .cra_driver_name = "pcbc-aes-aesni",
  1056. .cra_priority = 400,
  1057. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1058. .cra_blocksize = AES_BLOCK_SIZE,
  1059. .cra_ctxsize = sizeof(struct async_helper_ctx),
  1060. .cra_alignmask = 0,
  1061. .cra_type = &crypto_ablkcipher_type,
  1062. .cra_module = THIS_MODULE,
  1063. .cra_init = ablk_pcbc_init,
  1064. .cra_exit = ablk_exit,
  1065. .cra_u = {
  1066. .ablkcipher = {
  1067. .min_keysize = AES_MIN_KEY_SIZE,
  1068. .max_keysize = AES_MAX_KEY_SIZE,
  1069. .ivsize = AES_BLOCK_SIZE,
  1070. .setkey = ablk_set_key,
  1071. .encrypt = ablk_encrypt,
  1072. .decrypt = ablk_decrypt,
  1073. },
  1074. },
  1075. #endif
  1076. }, {
  1077. .cra_name = "__lrw-aes-aesni",
  1078. .cra_driver_name = "__driver-lrw-aes-aesni",
  1079. .cra_priority = 0,
  1080. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  1081. .cra_blocksize = AES_BLOCK_SIZE,
  1082. .cra_ctxsize = sizeof(struct aesni_lrw_ctx),
  1083. .cra_alignmask = 0,
  1084. .cra_type = &crypto_blkcipher_type,
  1085. .cra_module = THIS_MODULE,
  1086. .cra_exit = lrw_aesni_exit_tfm,
  1087. .cra_u = {
  1088. .blkcipher = {
  1089. .min_keysize = AES_MIN_KEY_SIZE + AES_BLOCK_SIZE,
  1090. .max_keysize = AES_MAX_KEY_SIZE + AES_BLOCK_SIZE,
  1091. .ivsize = AES_BLOCK_SIZE,
  1092. .setkey = lrw_aesni_setkey,
  1093. .encrypt = lrw_encrypt,
  1094. .decrypt = lrw_decrypt,
  1095. },
  1096. },
  1097. }, {
  1098. .cra_name = "__xts-aes-aesni",
  1099. .cra_driver_name = "__driver-xts-aes-aesni",
  1100. .cra_priority = 0,
  1101. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  1102. .cra_blocksize = AES_BLOCK_SIZE,
  1103. .cra_ctxsize = sizeof(struct aesni_xts_ctx),
  1104. .cra_alignmask = 0,
  1105. .cra_type = &crypto_blkcipher_type,
  1106. .cra_module = THIS_MODULE,
  1107. .cra_u = {
  1108. .blkcipher = {
  1109. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  1110. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  1111. .ivsize = AES_BLOCK_SIZE,
  1112. .setkey = xts_aesni_setkey,
  1113. .encrypt = xts_encrypt,
  1114. .decrypt = xts_decrypt,
  1115. },
  1116. },
  1117. }, {
  1118. .cra_name = "lrw(aes)",
  1119. .cra_driver_name = "lrw-aes-aesni",
  1120. .cra_priority = 400,
  1121. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1122. .cra_blocksize = AES_BLOCK_SIZE,
  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_init,
  1128. .cra_exit = ablk_exit,
  1129. .cra_u = {
  1130. .ablkcipher = {
  1131. .min_keysize = AES_MIN_KEY_SIZE + AES_BLOCK_SIZE,
  1132. .max_keysize = AES_MAX_KEY_SIZE + AES_BLOCK_SIZE,
  1133. .ivsize = AES_BLOCK_SIZE,
  1134. .setkey = ablk_set_key,
  1135. .encrypt = ablk_encrypt,
  1136. .decrypt = ablk_decrypt,
  1137. },
  1138. },
  1139. }, {
  1140. .cra_name = "xts(aes)",
  1141. .cra_driver_name = "xts-aes-aesni",
  1142. .cra_priority = 400,
  1143. .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
  1144. .cra_blocksize = AES_BLOCK_SIZE,
  1145. .cra_ctxsize = sizeof(struct async_helper_ctx),
  1146. .cra_alignmask = 0,
  1147. .cra_type = &crypto_ablkcipher_type,
  1148. .cra_module = THIS_MODULE,
  1149. .cra_init = ablk_init,
  1150. .cra_exit = ablk_exit,
  1151. .cra_u = {
  1152. .ablkcipher = {
  1153. .min_keysize = 2 * AES_MIN_KEY_SIZE,
  1154. .max_keysize = 2 * AES_MAX_KEY_SIZE,
  1155. .ivsize = AES_BLOCK_SIZE,
  1156. .setkey = ablk_set_key,
  1157. .encrypt = ablk_encrypt,
  1158. .decrypt = ablk_decrypt,
  1159. },
  1160. },
  1161. } };
  1162. static const struct x86_cpu_id aesni_cpu_id[] = {
  1163. X86_FEATURE_MATCH(X86_FEATURE_AES),
  1164. {}
  1165. };
  1166. MODULE_DEVICE_TABLE(x86cpu, aesni_cpu_id);
  1167. static int __init aesni_init(void)
  1168. {
  1169. int err;
  1170. if (!x86_match_cpu(aesni_cpu_id))
  1171. return -ENODEV;
  1172. err = crypto_fpu_init();
  1173. if (err)
  1174. return err;
  1175. return crypto_register_algs(aesni_algs, ARRAY_SIZE(aesni_algs));
  1176. }
  1177. static void __exit aesni_exit(void)
  1178. {
  1179. crypto_unregister_algs(aesni_algs, ARRAY_SIZE(aesni_algs));
  1180. crypto_fpu_exit();
  1181. }
  1182. module_init(aesni_init);
  1183. module_exit(aesni_exit);
  1184. MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm, Intel AES-NI instructions optimized");
  1185. MODULE_LICENSE("GPL");
  1186. MODULE_ALIAS("aes");