aesni-intel_glue.c 43 KB

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