aesni-intel_glue.c 38 KB

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