blob: 31c34ee131f34505872a018b75a8489f1ac9c854 [file] [log] [blame]
Huang Ying54b6a1b2009-01-18 16:28:34 +11001/*
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 *
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04008 * 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 *
Huang Ying54b6a1b2009-01-18 16:28:34 +110016 * 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
22#include <linux/hardirq.h>
23#include <linux/types.h>
Paul Gortmaker7c52d552011-05-27 12:33:10 -040024#include <linux/module.h>
Huang Ying54b6a1b2009-01-18 16:28:34 +110025#include <linux/err.h>
26#include <crypto/algapi.h>
27#include <crypto/aes.h>
28#include <crypto/cryptd.h>
Huang Ying12387a42010-03-10 18:28:55 +080029#include <crypto/ctr.h>
Jussi Kivilinna023af602012-07-22 18:18:37 +030030#include <crypto/b128ops.h>
Jussi Kivilinna023af602012-07-22 18:18:37 +030031#include <crypto/xts.h>
Andi Kleen3bd391f2012-01-26 00:09:06 +010032#include <asm/cpu_device_id.h>
Ingo Molnardf6b35f2015-04-24 02:46:00 +020033#include <asm/fpu/api.h>
Jussi Kivilinna70ef2602012-06-18 14:07:50 +030034#include <asm/crypto/aes.h>
Tadeusz Struk0bd82f52010-11-04 15:00:45 -040035#include <crypto/scatterwalk.h>
36#include <crypto/internal/aead.h>
Herbert Xu85671862016-11-22 20:08:33 +080037#include <crypto/internal/simd.h>
38#include <crypto/internal/skcipher.h>
Tadeusz Struk0bd82f52010-11-04 15:00:45 -040039#include <linux/workqueue.h>
40#include <linux/spinlock.h>
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +030041#ifdef CONFIG_X86_64
42#include <asm/crypto/glue_helper.h>
43#endif
Huang Ying54b6a1b2009-01-18 16:28:34 +110044
Timothy McCaffreye31ac322015-01-13 13:16:43 -050045
Herbert Xub7c89d92015-06-01 15:53:06 +080046#define AESNI_ALIGN 16
Herbert Xu85671862016-11-22 20:08:33 +080047#define AESNI_ALIGN_ATTR __attribute__ ((__aligned__(AESNI_ALIGN)))
Herbert Xub7c89d92015-06-01 15:53:06 +080048#define AES_BLOCK_MASK (~(AES_BLOCK_SIZE - 1))
49#define RFC4106_HASH_SUBKEY_SIZE 16
Herbert Xu85671862016-11-22 20:08:33 +080050#define AESNI_ALIGN_EXTRA ((AESNI_ALIGN - 1) & ~(CRYPTO_MINALIGN - 1))
51#define CRYPTO_AES_CTX_SIZE (sizeof(struct crypto_aes_ctx) + AESNI_ALIGN_EXTRA)
52#define XTS_AES_CTX_SIZE (sizeof(struct aesni_xts_ctx) + AESNI_ALIGN_EXTRA)
Herbert Xub7c89d92015-06-01 15:53:06 +080053
Tadeusz Struk0bd82f52010-11-04 15:00:45 -040054/* This data is stored at the end of the crypto_tfm struct.
55 * It's a type of per "session" data storage location.
56 * This needs to be 16 byte aligned.
57 */
58struct aesni_rfc4106_gcm_ctx {
Herbert Xu85671862016-11-22 20:08:33 +080059 u8 hash_subkey[16] AESNI_ALIGN_ATTR;
60 struct crypto_aes_ctx aes_key_expanded AESNI_ALIGN_ATTR;
Tadeusz Struk0bd82f52010-11-04 15:00:45 -040061 u8 nonce[4];
Tadeusz Struk0bd82f52010-11-04 15:00:45 -040062};
63
Jussi Kivilinna023af602012-07-22 18:18:37 +030064struct aesni_xts_ctx {
Herbert Xu85671862016-11-22 20:08:33 +080065 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;
Jussi Kivilinna023af602012-07-22 18:18:37 +030067};
68
Huang Ying54b6a1b2009-01-18 16:28:34 +110069asmlinkage int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key,
70 unsigned int key_len);
71asmlinkage void aesni_enc(struct crypto_aes_ctx *ctx, u8 *out,
72 const u8 *in);
73asmlinkage void aesni_dec(struct crypto_aes_ctx *ctx, u8 *out,
74 const u8 *in);
75asmlinkage void aesni_ecb_enc(struct crypto_aes_ctx *ctx, u8 *out,
76 const u8 *in, unsigned int len);
77asmlinkage void aesni_ecb_dec(struct crypto_aes_ctx *ctx, u8 *out,
78 const u8 *in, unsigned int len);
79asmlinkage void aesni_cbc_enc(struct crypto_aes_ctx *ctx, u8 *out,
80 const u8 *in, unsigned int len, u8 *iv);
81asmlinkage void aesni_cbc_dec(struct crypto_aes_ctx *ctx, u8 *out,
82 const u8 *in, unsigned int len, u8 *iv);
Randy Dunlap9bed4ac2011-05-18 09:03:34 +100083
84int crypto_fpu_init(void);
85void crypto_fpu_exit(void);
86
Tim Chend7645932013-12-11 14:28:41 -080087#define AVX_GEN2_OPTSIZE 640
88#define AVX_GEN4_OPTSIZE 4096
89
Mathias Krause0d258ef2010-11-27 16:34:46 +080090#ifdef CONFIG_X86_64
chandramouli narayanan22cddcc2014-06-10 09:22:47 -070091
92static void (*aesni_ctr_enc_tfm)(struct crypto_aes_ctx *ctx, u8 *out,
93 const u8 *in, unsigned int len, u8 *iv);
Huang Ying12387a42010-03-10 18:28:55 +080094asmlinkage void aesni_ctr_enc(struct crypto_aes_ctx *ctx, u8 *out,
95 const u8 *in, unsigned int len, u8 *iv);
Huang Ying54b6a1b2009-01-18 16:28:34 +110096
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +030097asmlinkage void aesni_xts_crypt8(struct crypto_aes_ctx *ctx, u8 *out,
98 const u8 *in, bool enc, u8 *iv);
99
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400100/* asmlinkage void aesni_gcm_enc()
101 * void *ctx, AES Key schedule. Starts on a 16 byte boundary.
102 * u8 *out, Ciphertext output. Encrypt in-place is allowed.
103 * const u8 *in, Plaintext input
104 * unsigned long plaintext_len, Length of data in bytes for encryption.
105 * u8 *iv, Pre-counter block j0: 4 byte salt (from Security Association)
106 * concatenated with 8 byte Initialisation Vector (from IPSec ESP
107 * Payload) concatenated with 0x00000001. 16-byte aligned pointer.
108 * u8 *hash_subkey, the Hash sub key input. Data starts on a 16-byte boundary.
109 * const u8 *aad, Additional Authentication Data (AAD)
110 * unsigned long aad_len, Length of AAD in bytes. With RFC4106 this
111 * is going to be 8 or 12 bytes
112 * u8 *auth_tag, Authenticated Tag output.
113 * unsigned long auth_tag_len), Authenticated Tag Length in bytes.
114 * Valid values are 16 (most likely), 12 or 8.
115 */
116asmlinkage void aesni_gcm_enc(void *ctx, u8 *out,
117 const u8 *in, unsigned long plaintext_len, u8 *iv,
118 u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
119 u8 *auth_tag, unsigned long auth_tag_len);
120
121/* asmlinkage void aesni_gcm_dec()
122 * void *ctx, AES Key schedule. Starts on a 16 byte boundary.
123 * u8 *out, Plaintext output. Decrypt in-place is allowed.
124 * const u8 *in, Ciphertext input
125 * unsigned long ciphertext_len, Length of data in bytes for decryption.
126 * u8 *iv, Pre-counter block j0: 4 byte salt (from Security Association)
127 * concatenated with 8 byte Initialisation Vector (from IPSec ESP
128 * Payload) concatenated with 0x00000001. 16-byte aligned pointer.
129 * u8 *hash_subkey, the Hash sub key input. Data starts on a 16-byte boundary.
130 * const u8 *aad, Additional Authentication Data (AAD)
131 * unsigned long aad_len, Length of AAD in bytes. With RFC4106 this is going
132 * to be 8 or 12 bytes
133 * u8 *auth_tag, Authenticated Tag output.
134 * unsigned long auth_tag_len) Authenticated Tag Length in bytes.
135 * Valid values are 16 (most likely), 12 or 8.
136 */
137asmlinkage void aesni_gcm_dec(void *ctx, u8 *out,
138 const u8 *in, unsigned long ciphertext_len, u8 *iv,
139 u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
140 u8 *auth_tag, unsigned long auth_tag_len);
141
Tim Chend7645932013-12-11 14:28:41 -0800142
143#ifdef CONFIG_AS_AVX
chandramouli narayanan22cddcc2014-06-10 09:22:47 -0700144asmlinkage void aes_ctr_enc_128_avx_by8(const u8 *in, u8 *iv,
145 void *keys, u8 *out, unsigned int num_bytes);
146asmlinkage void aes_ctr_enc_192_avx_by8(const u8 *in, u8 *iv,
147 void *keys, u8 *out, unsigned int num_bytes);
148asmlinkage void aes_ctr_enc_256_avx_by8(const u8 *in, u8 *iv,
149 void *keys, u8 *out, unsigned int num_bytes);
Tim Chend7645932013-12-11 14:28:41 -0800150/*
151 * asmlinkage void aesni_gcm_precomp_avx_gen2()
152 * gcm_data *my_ctx_data, context data
153 * u8 *hash_subkey, the Hash sub key input. Data starts on a 16-byte boundary.
154 */
155asmlinkage void aesni_gcm_precomp_avx_gen2(void *my_ctx_data, u8 *hash_subkey);
156
157asmlinkage void aesni_gcm_enc_avx_gen2(void *ctx, u8 *out,
158 const u8 *in, unsigned long plaintext_len, u8 *iv,
159 const u8 *aad, unsigned long aad_len,
160 u8 *auth_tag, unsigned long auth_tag_len);
161
162asmlinkage void aesni_gcm_dec_avx_gen2(void *ctx, u8 *out,
163 const u8 *in, unsigned long ciphertext_len, u8 *iv,
164 const u8 *aad, unsigned long aad_len,
165 u8 *auth_tag, unsigned long auth_tag_len);
166
167static void aesni_gcm_enc_avx(void *ctx, u8 *out,
168 const u8 *in, unsigned long plaintext_len, u8 *iv,
169 u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
170 u8 *auth_tag, unsigned long auth_tag_len)
171{
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500172 struct crypto_aes_ctx *aes_ctx = (struct crypto_aes_ctx*)ctx;
173 if ((plaintext_len < AVX_GEN2_OPTSIZE) || (aes_ctx-> key_length != AES_KEYSIZE_128)){
Tim Chend7645932013-12-11 14:28:41 -0800174 aesni_gcm_enc(ctx, out, in, plaintext_len, iv, hash_subkey, aad,
175 aad_len, auth_tag, auth_tag_len);
176 } else {
177 aesni_gcm_precomp_avx_gen2(ctx, hash_subkey);
178 aesni_gcm_enc_avx_gen2(ctx, out, in, plaintext_len, iv, aad,
179 aad_len, auth_tag, auth_tag_len);
180 }
181}
182
183static void aesni_gcm_dec_avx(void *ctx, u8 *out,
184 const u8 *in, unsigned long ciphertext_len, u8 *iv,
185 u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
186 u8 *auth_tag, unsigned long auth_tag_len)
187{
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500188 struct crypto_aes_ctx *aes_ctx = (struct crypto_aes_ctx*)ctx;
189 if ((ciphertext_len < AVX_GEN2_OPTSIZE) || (aes_ctx-> key_length != AES_KEYSIZE_128)) {
Tim Chend7645932013-12-11 14:28:41 -0800190 aesni_gcm_dec(ctx, out, in, ciphertext_len, iv, hash_subkey, aad,
191 aad_len, auth_tag, auth_tag_len);
192 } else {
193 aesni_gcm_precomp_avx_gen2(ctx, hash_subkey);
194 aesni_gcm_dec_avx_gen2(ctx, out, in, ciphertext_len, iv, aad,
195 aad_len, auth_tag, auth_tag_len);
196 }
197}
198#endif
199
200#ifdef CONFIG_AS_AVX2
201/*
202 * asmlinkage void aesni_gcm_precomp_avx_gen4()
203 * gcm_data *my_ctx_data, context data
204 * u8 *hash_subkey, the Hash sub key input. Data starts on a 16-byte boundary.
205 */
206asmlinkage void aesni_gcm_precomp_avx_gen4(void *my_ctx_data, u8 *hash_subkey);
207
208asmlinkage void aesni_gcm_enc_avx_gen4(void *ctx, u8 *out,
209 const u8 *in, unsigned long plaintext_len, u8 *iv,
210 const u8 *aad, unsigned long aad_len,
211 u8 *auth_tag, unsigned long auth_tag_len);
212
213asmlinkage void aesni_gcm_dec_avx_gen4(void *ctx, u8 *out,
214 const u8 *in, unsigned long ciphertext_len, u8 *iv,
215 const u8 *aad, unsigned long aad_len,
216 u8 *auth_tag, unsigned long auth_tag_len);
217
218static void aesni_gcm_enc_avx2(void *ctx, u8 *out,
219 const u8 *in, unsigned long plaintext_len, u8 *iv,
220 u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
221 u8 *auth_tag, unsigned long auth_tag_len)
222{
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500223 struct crypto_aes_ctx *aes_ctx = (struct crypto_aes_ctx*)ctx;
224 if ((plaintext_len < AVX_GEN2_OPTSIZE) || (aes_ctx-> key_length != AES_KEYSIZE_128)) {
Tim Chend7645932013-12-11 14:28:41 -0800225 aesni_gcm_enc(ctx, out, in, plaintext_len, iv, hash_subkey, aad,
226 aad_len, auth_tag, auth_tag_len);
227 } else if (plaintext_len < AVX_GEN4_OPTSIZE) {
228 aesni_gcm_precomp_avx_gen2(ctx, hash_subkey);
229 aesni_gcm_enc_avx_gen2(ctx, out, in, plaintext_len, iv, aad,
230 aad_len, auth_tag, auth_tag_len);
231 } else {
232 aesni_gcm_precomp_avx_gen4(ctx, hash_subkey);
233 aesni_gcm_enc_avx_gen4(ctx, out, in, plaintext_len, iv, aad,
234 aad_len, auth_tag, auth_tag_len);
235 }
236}
237
238static void aesni_gcm_dec_avx2(void *ctx, u8 *out,
239 const u8 *in, unsigned long ciphertext_len, u8 *iv,
240 u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
241 u8 *auth_tag, unsigned long auth_tag_len)
242{
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500243 struct crypto_aes_ctx *aes_ctx = (struct crypto_aes_ctx*)ctx;
244 if ((ciphertext_len < AVX_GEN2_OPTSIZE) || (aes_ctx-> key_length != AES_KEYSIZE_128)) {
Tim Chend7645932013-12-11 14:28:41 -0800245 aesni_gcm_dec(ctx, out, in, ciphertext_len, iv, hash_subkey,
246 aad, aad_len, auth_tag, auth_tag_len);
247 } else if (ciphertext_len < AVX_GEN4_OPTSIZE) {
248 aesni_gcm_precomp_avx_gen2(ctx, hash_subkey);
249 aesni_gcm_dec_avx_gen2(ctx, out, in, ciphertext_len, iv, aad,
250 aad_len, auth_tag, auth_tag_len);
251 } else {
252 aesni_gcm_precomp_avx_gen4(ctx, hash_subkey);
253 aesni_gcm_dec_avx_gen4(ctx, out, in, ciphertext_len, iv, aad,
254 aad_len, auth_tag, auth_tag_len);
255 }
256}
257#endif
258
259static void (*aesni_gcm_enc_tfm)(void *ctx, u8 *out,
260 const u8 *in, unsigned long plaintext_len, u8 *iv,
261 u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
262 u8 *auth_tag, unsigned long auth_tag_len);
263
264static void (*aesni_gcm_dec_tfm)(void *ctx, u8 *out,
265 const u8 *in, unsigned long ciphertext_len, u8 *iv,
266 u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
267 u8 *auth_tag, unsigned long auth_tag_len);
268
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400269static inline struct
270aesni_rfc4106_gcm_ctx *aesni_rfc4106_gcm_ctx_get(struct crypto_aead *tfm)
271{
Herbert Xub7c89d92015-06-01 15:53:06 +0800272 unsigned long align = AESNI_ALIGN;
273
274 if (align <= crypto_tfm_ctx_alignment())
275 align = 1;
276 return PTR_ALIGN(crypto_aead_ctx(tfm), align);
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400277}
Mathias Krause559ad0f2010-11-29 08:35:39 +0800278#endif
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400279
Huang Ying54b6a1b2009-01-18 16:28:34 +1100280static inline struct crypto_aes_ctx *aes_ctx(void *raw_ctx)
281{
282 unsigned long addr = (unsigned long)raw_ctx;
283 unsigned long align = AESNI_ALIGN;
284
285 if (align <= crypto_tfm_ctx_alignment())
286 align = 1;
287 return (struct crypto_aes_ctx *)ALIGN(addr, align);
288}
289
290static int aes_set_key_common(struct crypto_tfm *tfm, void *raw_ctx,
291 const u8 *in_key, unsigned int key_len)
292{
293 struct crypto_aes_ctx *ctx = aes_ctx(raw_ctx);
294 u32 *flags = &tfm->crt_flags;
295 int err;
296
297 if (key_len != AES_KEYSIZE_128 && key_len != AES_KEYSIZE_192 &&
298 key_len != AES_KEYSIZE_256) {
299 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
300 return -EINVAL;
301 }
302
Huang Ying13b79b972009-10-20 16:20:47 +0900303 if (!irq_fpu_usable())
Huang Ying54b6a1b2009-01-18 16:28:34 +1100304 err = crypto_aes_expand_key(ctx, in_key, key_len);
305 else {
306 kernel_fpu_begin();
307 err = aesni_set_key(ctx, in_key, key_len);
308 kernel_fpu_end();
309 }
310
311 return err;
312}
313
314static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
315 unsigned int key_len)
316{
317 return aes_set_key_common(tfm, crypto_tfm_ctx(tfm), in_key, key_len);
318}
319
320static void aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
321{
322 struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
323
Huang Ying13b79b972009-10-20 16:20:47 +0900324 if (!irq_fpu_usable())
Huang Ying54b6a1b2009-01-18 16:28:34 +1100325 crypto_aes_encrypt_x86(ctx, dst, src);
326 else {
327 kernel_fpu_begin();
328 aesni_enc(ctx, dst, src);
329 kernel_fpu_end();
330 }
331}
332
333static void aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
334{
335 struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
336
Huang Ying13b79b972009-10-20 16:20:47 +0900337 if (!irq_fpu_usable())
Huang Ying54b6a1b2009-01-18 16:28:34 +1100338 crypto_aes_decrypt_x86(ctx, dst, src);
339 else {
340 kernel_fpu_begin();
341 aesni_dec(ctx, dst, src);
342 kernel_fpu_end();
343 }
344}
345
Huang Ying2cf4ac82009-03-29 15:41:20 +0800346static void __aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
347{
348 struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
349
350 aesni_enc(ctx, dst, src);
351}
352
353static void __aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
354{
355 struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
356
357 aesni_dec(ctx, dst, src);
358}
359
Herbert Xu85671862016-11-22 20:08:33 +0800360static int aesni_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
361 unsigned int len)
Huang Ying54b6a1b2009-01-18 16:28:34 +1100362{
Herbert Xu85671862016-11-22 20:08:33 +0800363 return aes_set_key_common(crypto_skcipher_tfm(tfm),
364 crypto_skcipher_ctx(tfm), key, len);
365}
366
367static int ecb_encrypt(struct skcipher_request *req)
368{
369 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
370 struct crypto_aes_ctx *ctx = aes_ctx(crypto_skcipher_ctx(tfm));
371 struct skcipher_walk walk;
372 unsigned int nbytes;
Huang Ying54b6a1b2009-01-18 16:28:34 +1100373 int err;
374
Herbert Xu85671862016-11-22 20:08:33 +0800375 err = skcipher_walk_virt(&walk, req, true);
Huang Ying54b6a1b2009-01-18 16:28:34 +1100376
377 kernel_fpu_begin();
378 while ((nbytes = walk.nbytes)) {
379 aesni_ecb_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
380 nbytes & AES_BLOCK_MASK);
381 nbytes &= AES_BLOCK_SIZE - 1;
Herbert Xu85671862016-11-22 20:08:33 +0800382 err = skcipher_walk_done(&walk, nbytes);
Huang Ying54b6a1b2009-01-18 16:28:34 +1100383 }
384 kernel_fpu_end();
385
386 return err;
387}
388
Herbert Xu85671862016-11-22 20:08:33 +0800389static int ecb_decrypt(struct skcipher_request *req)
Huang Ying54b6a1b2009-01-18 16:28:34 +1100390{
Herbert Xu85671862016-11-22 20:08:33 +0800391 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
392 struct crypto_aes_ctx *ctx = aes_ctx(crypto_skcipher_ctx(tfm));
393 struct skcipher_walk walk;
394 unsigned int nbytes;
Huang Ying54b6a1b2009-01-18 16:28:34 +1100395 int err;
396
Herbert Xu85671862016-11-22 20:08:33 +0800397 err = skcipher_walk_virt(&walk, req, true);
Huang Ying54b6a1b2009-01-18 16:28:34 +1100398
399 kernel_fpu_begin();
400 while ((nbytes = walk.nbytes)) {
401 aesni_ecb_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
402 nbytes & AES_BLOCK_MASK);
403 nbytes &= AES_BLOCK_SIZE - 1;
Herbert Xu85671862016-11-22 20:08:33 +0800404 err = skcipher_walk_done(&walk, nbytes);
Huang Ying54b6a1b2009-01-18 16:28:34 +1100405 }
406 kernel_fpu_end();
407
408 return err;
409}
410
Herbert Xu85671862016-11-22 20:08:33 +0800411static int cbc_encrypt(struct skcipher_request *req)
Huang Ying54b6a1b2009-01-18 16:28:34 +1100412{
Herbert Xu85671862016-11-22 20:08:33 +0800413 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;
Huang Ying54b6a1b2009-01-18 16:28:34 +1100417 int err;
418
Herbert Xu85671862016-11-22 20:08:33 +0800419 err = skcipher_walk_virt(&walk, req, true);
Huang Ying54b6a1b2009-01-18 16:28:34 +1100420
421 kernel_fpu_begin();
422 while ((nbytes = walk.nbytes)) {
423 aesni_cbc_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
424 nbytes & AES_BLOCK_MASK, walk.iv);
425 nbytes &= AES_BLOCK_SIZE - 1;
Herbert Xu85671862016-11-22 20:08:33 +0800426 err = skcipher_walk_done(&walk, nbytes);
Huang Ying54b6a1b2009-01-18 16:28:34 +1100427 }
428 kernel_fpu_end();
429
430 return err;
431}
432
Herbert Xu85671862016-11-22 20:08:33 +0800433static int cbc_decrypt(struct skcipher_request *req)
Huang Ying54b6a1b2009-01-18 16:28:34 +1100434{
Herbert Xu85671862016-11-22 20:08:33 +0800435 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
436 struct crypto_aes_ctx *ctx = aes_ctx(crypto_skcipher_ctx(tfm));
437 struct skcipher_walk walk;
438 unsigned int nbytes;
Huang Ying54b6a1b2009-01-18 16:28:34 +1100439 int err;
440
Herbert Xu85671862016-11-22 20:08:33 +0800441 err = skcipher_walk_virt(&walk, req, true);
Huang Ying54b6a1b2009-01-18 16:28:34 +1100442
443 kernel_fpu_begin();
444 while ((nbytes = walk.nbytes)) {
445 aesni_cbc_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
446 nbytes & AES_BLOCK_MASK, walk.iv);
447 nbytes &= AES_BLOCK_SIZE - 1;
Herbert Xu85671862016-11-22 20:08:33 +0800448 err = skcipher_walk_done(&walk, nbytes);
Huang Ying54b6a1b2009-01-18 16:28:34 +1100449 }
450 kernel_fpu_end();
451
452 return err;
453}
454
Mathias Krause0d258ef2010-11-27 16:34:46 +0800455#ifdef CONFIG_X86_64
Huang Ying12387a42010-03-10 18:28:55 +0800456static void ctr_crypt_final(struct crypto_aes_ctx *ctx,
Herbert Xu85671862016-11-22 20:08:33 +0800457 struct skcipher_walk *walk)
Huang Ying12387a42010-03-10 18:28:55 +0800458{
459 u8 *ctrblk = walk->iv;
460 u8 keystream[AES_BLOCK_SIZE];
461 u8 *src = walk->src.virt.addr;
462 u8 *dst = walk->dst.virt.addr;
463 unsigned int nbytes = walk->nbytes;
464
465 aesni_enc(ctx, keystream, ctrblk);
466 crypto_xor(keystream, src, nbytes);
467 memcpy(dst, keystream, nbytes);
468 crypto_inc(ctrblk, AES_BLOCK_SIZE);
469}
470
Mathias Krause5cfed7b2014-09-28 22:24:01 +0200471#ifdef CONFIG_AS_AVX
chandramouli narayanan22cddcc2014-06-10 09:22:47 -0700472static void aesni_ctr_enc_avx_tfm(struct crypto_aes_ctx *ctx, u8 *out,
473 const u8 *in, unsigned int len, u8 *iv)
474{
475 /*
476 * based on key length, override with the by8 version
477 * of ctr mode encryption/decryption for improved performance
478 * aes_set_key_common() ensures that key length is one of
479 * {128,192,256}
480 */
481 if (ctx->key_length == AES_KEYSIZE_128)
482 aes_ctr_enc_128_avx_by8(in, iv, (void *)ctx, out, len);
483 else if (ctx->key_length == AES_KEYSIZE_192)
484 aes_ctr_enc_192_avx_by8(in, iv, (void *)ctx, out, len);
485 else
486 aes_ctr_enc_256_avx_by8(in, iv, (void *)ctx, out, len);
487}
488#endif
489
Herbert Xu85671862016-11-22 20:08:33 +0800490static int ctr_crypt(struct skcipher_request *req)
Huang Ying12387a42010-03-10 18:28:55 +0800491{
Herbert Xu85671862016-11-22 20:08:33 +0800492 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
493 struct crypto_aes_ctx *ctx = aes_ctx(crypto_skcipher_ctx(tfm));
494 struct skcipher_walk walk;
495 unsigned int nbytes;
Huang Ying12387a42010-03-10 18:28:55 +0800496 int err;
497
Herbert Xu85671862016-11-22 20:08:33 +0800498 err = skcipher_walk_virt(&walk, req, true);
Huang Ying12387a42010-03-10 18:28:55 +0800499
500 kernel_fpu_begin();
501 while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
chandramouli narayanan22cddcc2014-06-10 09:22:47 -0700502 aesni_ctr_enc_tfm(ctx, walk.dst.virt.addr, walk.src.virt.addr,
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500503 nbytes & AES_BLOCK_MASK, walk.iv);
Huang Ying12387a42010-03-10 18:28:55 +0800504 nbytes &= AES_BLOCK_SIZE - 1;
Herbert Xu85671862016-11-22 20:08:33 +0800505 err = skcipher_walk_done(&walk, nbytes);
Huang Ying12387a42010-03-10 18:28:55 +0800506 }
507 if (walk.nbytes) {
508 ctr_crypt_final(ctx, &walk);
Herbert Xu85671862016-11-22 20:08:33 +0800509 err = skcipher_walk_done(&walk, 0);
Huang Ying12387a42010-03-10 18:28:55 +0800510 }
511 kernel_fpu_end();
512
513 return err;
514}
Huang Ying12387a42010-03-10 18:28:55 +0800515
Herbert Xu85671862016-11-22 20:08:33 +0800516static int xts_aesni_setkey(struct crypto_skcipher *tfm, const u8 *key,
Jussi Kivilinna023af602012-07-22 18:18:37 +0300517 unsigned int keylen)
518{
Herbert Xu85671862016-11-22 20:08:33 +0800519 struct aesni_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
Jussi Kivilinna023af602012-07-22 18:18:37 +0300520 int err;
521
Herbert Xu85671862016-11-22 20:08:33 +0800522 err = xts_verify_key(tfm, key, keylen);
Jussi Kivilinna023af602012-07-22 18:18:37 +0300523 if (err)
524 return err;
525
Herbert Xu85671862016-11-22 20:08:33 +0800526 keylen /= 2;
Jussi Kivilinna023af602012-07-22 18:18:37 +0300527
528 /* first half of xts-key is for crypt */
Herbert Xu85671862016-11-22 20:08:33 +0800529 err = aes_set_key_common(crypto_skcipher_tfm(tfm), ctx->raw_crypt_ctx,
530 key, keylen);
Jussi Kivilinna023af602012-07-22 18:18:37 +0300531 if (err)
532 return err;
533
534 /* second half of xts-key is for tweak */
Herbert Xu85671862016-11-22 20:08:33 +0800535 return aes_set_key_common(crypto_skcipher_tfm(tfm), ctx->raw_tweak_ctx,
536 key + keylen, keylen);
Jussi Kivilinna023af602012-07-22 18:18:37 +0300537}
538
539
Jussi Kivilinna32bec972012-10-18 23:24:57 +0300540static void aesni_xts_tweak(void *ctx, u8 *out, const u8 *in)
541{
542 aesni_enc(ctx, out, in);
543}
544
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +0300545static void aesni_xts_enc(void *ctx, u128 *dst, const u128 *src, le128 *iv)
546{
547 glue_xts_crypt_128bit_one(ctx, dst, src, iv, GLUE_FUNC_CAST(aesni_enc));
548}
549
550static void aesni_xts_dec(void *ctx, u128 *dst, const u128 *src, le128 *iv)
551{
552 glue_xts_crypt_128bit_one(ctx, dst, src, iv, GLUE_FUNC_CAST(aesni_dec));
553}
554
555static void aesni_xts_enc8(void *ctx, u128 *dst, const u128 *src, le128 *iv)
556{
557 aesni_xts_crypt8(ctx, (u8 *)dst, (const u8 *)src, true, (u8 *)iv);
558}
559
560static void aesni_xts_dec8(void *ctx, u128 *dst, const u128 *src, le128 *iv)
561{
562 aesni_xts_crypt8(ctx, (u8 *)dst, (const u8 *)src, false, (u8 *)iv);
563}
564
565static const struct common_glue_ctx aesni_enc_xts = {
566 .num_funcs = 2,
567 .fpu_blocks_limit = 1,
568
569 .funcs = { {
570 .num_blocks = 8,
571 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(aesni_xts_enc8) }
572 }, {
573 .num_blocks = 1,
574 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(aesni_xts_enc) }
575 } }
576};
577
578static const struct common_glue_ctx aesni_dec_xts = {
579 .num_funcs = 2,
580 .fpu_blocks_limit = 1,
581
582 .funcs = { {
583 .num_blocks = 8,
584 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(aesni_xts_dec8) }
585 }, {
586 .num_blocks = 1,
587 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(aesni_xts_dec) }
588 } }
589};
590
Herbert Xu85671862016-11-22 20:08:33 +0800591static int xts_encrypt(struct skcipher_request *req)
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +0300592{
Herbert Xu85671862016-11-22 20:08:33 +0800593 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
594 struct aesni_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +0300595
Herbert Xu85671862016-11-22 20:08:33 +0800596 return glue_xts_req_128bit(&aesni_enc_xts, req,
597 XTS_TWEAK_CAST(aesni_xts_tweak),
598 aes_ctx(ctx->raw_tweak_ctx),
599 aes_ctx(ctx->raw_crypt_ctx));
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +0300600}
601
Herbert Xu85671862016-11-22 20:08:33 +0800602static int xts_decrypt(struct skcipher_request *req)
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +0300603{
Herbert Xu85671862016-11-22 20:08:33 +0800604 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
605 struct aesni_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +0300606
Herbert Xu85671862016-11-22 20:08:33 +0800607 return glue_xts_req_128bit(&aesni_dec_xts, req,
608 XTS_TWEAK_CAST(aesni_xts_tweak),
609 aes_ctx(ctx->raw_tweak_ctx),
610 aes_ctx(ctx->raw_crypt_ctx));
Jussi Kivilinnac456a9c2013-04-08 21:51:16 +0300611}
612
Herbert Xuaf05b3002015-05-28 22:08:03 +0800613static int rfc4106_init(struct crypto_aead *aead)
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400614{
615 struct cryptd_aead *cryptd_tfm;
Herbert Xuaf05b3002015-05-28 22:08:03 +0800616 struct cryptd_aead **ctx = crypto_aead_ctx(aead);
617
Stephan Muellereabdc322015-03-30 21:58:17 +0200618 cryptd_tfm = cryptd_alloc_aead("__driver-gcm-aes-aesni",
619 CRYPTO_ALG_INTERNAL,
620 CRYPTO_ALG_INTERNAL);
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400621 if (IS_ERR(cryptd_tfm))
622 return PTR_ERR(cryptd_tfm);
Tadeusz Struk60af5202011-03-13 16:56:17 +0800623
Herbert Xuaf05b3002015-05-28 22:08:03 +0800624 *ctx = cryptd_tfm;
Herbert Xue9b8d2c2015-07-09 07:17:28 +0800625 crypto_aead_set_reqsize(aead, crypto_aead_reqsize(&cryptd_tfm->base));
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400626 return 0;
627}
628
Herbert Xuaf05b3002015-05-28 22:08:03 +0800629static void rfc4106_exit(struct crypto_aead *aead)
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400630{
Herbert Xuaf05b3002015-05-28 22:08:03 +0800631 struct cryptd_aead **ctx = crypto_aead_ctx(aead);
632
633 cryptd_free_aead(*ctx);
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400634}
635
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400636static int
637rfc4106_set_hash_subkey(u8 *hash_subkey, const u8 *key, unsigned int key_len)
638{
Herbert Xu02fa4722016-06-29 18:03:59 +0800639 struct crypto_cipher *tfm;
640 int ret;
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400641
Herbert Xu02fa4722016-06-29 18:03:59 +0800642 tfm = crypto_alloc_cipher("aes", 0, 0);
643 if (IS_ERR(tfm))
644 return PTR_ERR(tfm);
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400645
Herbert Xu02fa4722016-06-29 18:03:59 +0800646 ret = crypto_cipher_setkey(tfm, key, key_len);
Jesper Juhl7efd95f62011-01-23 18:56:36 +1100647 if (ret)
Herbert Xu02fa4722016-06-29 18:03:59 +0800648 goto out_free_cipher;
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400649
650 /* Clear the data in the hash sub key container to zero.*/
651 /* We want to cipher all zeros to create the hash sub key. */
652 memset(hash_subkey, 0, RFC4106_HASH_SUBKEY_SIZE);
653
Herbert Xu02fa4722016-06-29 18:03:59 +0800654 crypto_cipher_encrypt_one(tfm, hash_subkey, hash_subkey);
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400655
Herbert Xu02fa4722016-06-29 18:03:59 +0800656out_free_cipher:
657 crypto_free_cipher(tfm);
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400658 return ret;
659}
660
Tadeusz Struk81e397d2015-02-06 10:25:20 -0800661static int common_rfc4106_set_key(struct crypto_aead *aead, const u8 *key,
662 unsigned int key_len)
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400663{
Tadeusz Struk81e397d2015-02-06 10:25:20 -0800664 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(aead);
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400665
666 if (key_len < 4) {
Herbert Xub7c89d92015-06-01 15:53:06 +0800667 crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400668 return -EINVAL;
669 }
670 /*Account for 4 byte nonce at the end.*/
671 key_len -= 4;
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400672
673 memcpy(ctx->nonce, key + key_len, sizeof(ctx->nonce));
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400674
Herbert Xub7c89d92015-06-01 15:53:06 +0800675 return aes_set_key_common(crypto_aead_tfm(aead),
676 &ctx->aes_key_expanded, key, key_len) ?:
677 rfc4106_set_hash_subkey(ctx->hash_subkey, key, key_len);
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400678}
679
Tadeusz Struk81e397d2015-02-06 10:25:20 -0800680static int rfc4106_set_key(struct crypto_aead *parent, const u8 *key,
681 unsigned int key_len)
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400682{
Herbert Xuaf05b3002015-05-28 22:08:03 +0800683 struct cryptd_aead **ctx = crypto_aead_ctx(parent);
684 struct cryptd_aead *cryptd_tfm = *ctx;
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400685
Herbert Xuaf05b3002015-05-28 22:08:03 +0800686 return crypto_aead_setkey(&cryptd_tfm->base, key, key_len);
Tadeusz Struk81e397d2015-02-06 10:25:20 -0800687}
688
689static int common_rfc4106_set_authsize(struct crypto_aead *aead,
690 unsigned int authsize)
691{
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400692 switch (authsize) {
693 case 8:
694 case 12:
695 case 16:
696 break;
697 default:
698 return -EINVAL;
699 }
Herbert Xub7c89d92015-06-01 15:53:06 +0800700
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400701 return 0;
702}
703
Tadeusz Struk81e397d2015-02-06 10:25:20 -0800704/* This is the Integrity Check Value (aka the authentication tag length and can
705 * be 8, 12 or 16 bytes long. */
706static int rfc4106_set_authsize(struct crypto_aead *parent,
707 unsigned int authsize)
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400708{
Herbert Xuaf05b3002015-05-28 22:08:03 +0800709 struct cryptd_aead **ctx = crypto_aead_ctx(parent);
710 struct cryptd_aead *cryptd_tfm = *ctx;
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400711
Herbert Xuaf05b3002015-05-28 22:08:03 +0800712 return crypto_aead_setauthsize(&cryptd_tfm->base, authsize);
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400713}
714
Herbert Xub7c89d92015-06-01 15:53:06 +0800715static int helper_rfc4106_encrypt(struct aead_request *req)
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400716{
717 u8 one_entry_in_sg = 0;
718 u8 *src, *dst, *assoc;
719 __be32 counter = cpu_to_be32(1);
720 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
721 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
722 void *aes_ctx = &(ctx->aes_key_expanded);
723 unsigned long auth_tag_len = crypto_aead_authsize(tfm);
Herbert Xub7c89d92015-06-01 15:53:06 +0800724 u8 iv[16] __attribute__ ((__aligned__(AESNI_ALIGN)));
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400725 struct scatter_walk src_sg_walk;
Arnd Bergmannbeae2c92016-11-10 17:44:51 +0100726 struct scatter_walk dst_sg_walk = {};
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400727 unsigned int i;
728
729 /* Assuming we are supporting rfc4106 64-bit extended */
730 /* sequence numbers We need to have the AAD length equal */
Herbert Xue9b8d2c2015-07-09 07:17:28 +0800731 /* to 16 or 20 bytes */
732 if (unlikely(req->assoclen != 16 && req->assoclen != 20))
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400733 return -EINVAL;
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500734
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400735 /* IV below built */
736 for (i = 0; i < 4; i++)
737 *(iv+i) = ctx->nonce[i];
738 for (i = 0; i < 8; i++)
739 *(iv+4+i) = req->iv[i];
740 *((__be32 *)(iv+12)) = counter;
741
Herbert Xub7c89d92015-06-01 15:53:06 +0800742 if (sg_is_last(req->src) &&
743 req->src->offset + req->src->length <= PAGE_SIZE &&
744 sg_is_last(req->dst) &&
745 req->dst->offset + req->dst->length <= PAGE_SIZE) {
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400746 one_entry_in_sg = 1;
747 scatterwalk_start(&src_sg_walk, req->src);
Herbert Xub7c89d92015-06-01 15:53:06 +0800748 assoc = scatterwalk_map(&src_sg_walk);
749 src = assoc + req->assoclen;
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400750 dst = src;
751 if (unlikely(req->src != req->dst)) {
752 scatterwalk_start(&dst_sg_walk, req->dst);
Herbert Xub7c89d92015-06-01 15:53:06 +0800753 dst = scatterwalk_map(&dst_sg_walk) + req->assoclen;
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400754 }
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400755 } else {
756 /* Allocate memory for src, dst, assoc */
Herbert Xub7c89d92015-06-01 15:53:06 +0800757 assoc = kmalloc(req->cryptlen + auth_tag_len + req->assoclen,
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400758 GFP_ATOMIC);
Herbert Xub7c89d92015-06-01 15:53:06 +0800759 if (unlikely(!assoc))
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400760 return -ENOMEM;
Herbert Xub7c89d92015-06-01 15:53:06 +0800761 scatterwalk_map_and_copy(assoc, req->src, 0,
762 req->assoclen + req->cryptlen, 0);
763 src = assoc + req->assoclen;
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400764 dst = src;
765 }
766
Herbert Xub7c89d92015-06-01 15:53:06 +0800767 kernel_fpu_begin();
Herbert Xue9b8d2c2015-07-09 07:17:28 +0800768 aesni_gcm_enc_tfm(aes_ctx, dst, src, req->cryptlen, iv,
769 ctx->hash_subkey, assoc, req->assoclen - 8,
770 dst + req->cryptlen, auth_tag_len);
Herbert Xub7c89d92015-06-01 15:53:06 +0800771 kernel_fpu_end();
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400772
773 /* The authTag (aka the Integrity Check Value) needs to be written
774 * back to the packet. */
775 if (one_entry_in_sg) {
776 if (unlikely(req->src != req->dst)) {
Herbert Xub7c89d92015-06-01 15:53:06 +0800777 scatterwalk_unmap(dst - req->assoclen);
778 scatterwalk_advance(&dst_sg_walk, req->dst->length);
779 scatterwalk_done(&dst_sg_walk, 1, 0);
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400780 }
Cong Wang8fd75e12011-11-25 23:14:17 +0800781 scatterwalk_unmap(assoc);
Herbert Xub7c89d92015-06-01 15:53:06 +0800782 scatterwalk_advance(&src_sg_walk, req->src->length);
783 scatterwalk_done(&src_sg_walk, req->src == req->dst, 0);
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400784 } else {
Herbert Xub7c89d92015-06-01 15:53:06 +0800785 scatterwalk_map_and_copy(dst, req->dst, req->assoclen,
786 req->cryptlen + auth_tag_len, 1);
787 kfree(assoc);
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400788 }
789 return 0;
790}
791
Herbert Xub7c89d92015-06-01 15:53:06 +0800792static int helper_rfc4106_decrypt(struct aead_request *req)
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400793{
794 u8 one_entry_in_sg = 0;
795 u8 *src, *dst, *assoc;
796 unsigned long tempCipherLen = 0;
797 __be32 counter = cpu_to_be32(1);
798 int retval = 0;
799 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
800 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
801 void *aes_ctx = &(ctx->aes_key_expanded);
802 unsigned long auth_tag_len = crypto_aead_authsize(tfm);
Herbert Xub7c89d92015-06-01 15:53:06 +0800803 u8 iv[16] __attribute__ ((__aligned__(AESNI_ALIGN)));
804 u8 authTag[16];
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400805 struct scatter_walk src_sg_walk;
Arnd Bergmannbeae2c92016-11-10 17:44:51 +0100806 struct scatter_walk dst_sg_walk = {};
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400807 unsigned int i;
808
Herbert Xue9b8d2c2015-07-09 07:17:28 +0800809 if (unlikely(req->assoclen != 16 && req->assoclen != 20))
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400810 return -EINVAL;
Timothy McCaffreye31ac322015-01-13 13:16:43 -0500811
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400812 /* Assuming we are supporting rfc4106 64-bit extended */
813 /* sequence numbers We need to have the AAD length */
Herbert Xue9b8d2c2015-07-09 07:17:28 +0800814 /* equal to 16 or 20 bytes */
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400815
816 tempCipherLen = (unsigned long)(req->cryptlen - auth_tag_len);
817 /* IV below built */
818 for (i = 0; i < 4; i++)
819 *(iv+i) = ctx->nonce[i];
820 for (i = 0; i < 8; i++)
821 *(iv+4+i) = req->iv[i];
822 *((__be32 *)(iv+12)) = counter;
823
Herbert Xub7c89d92015-06-01 15:53:06 +0800824 if (sg_is_last(req->src) &&
825 req->src->offset + req->src->length <= PAGE_SIZE &&
826 sg_is_last(req->dst) &&
827 req->dst->offset + req->dst->length <= PAGE_SIZE) {
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400828 one_entry_in_sg = 1;
829 scatterwalk_start(&src_sg_walk, req->src);
Herbert Xub7c89d92015-06-01 15:53:06 +0800830 assoc = scatterwalk_map(&src_sg_walk);
831 src = assoc + req->assoclen;
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400832 dst = src;
833 if (unlikely(req->src != req->dst)) {
834 scatterwalk_start(&dst_sg_walk, req->dst);
Herbert Xub7c89d92015-06-01 15:53:06 +0800835 dst = scatterwalk_map(&dst_sg_walk) + req->assoclen;
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400836 }
837
838 } else {
839 /* Allocate memory for src, dst, assoc */
Herbert Xub7c89d92015-06-01 15:53:06 +0800840 assoc = kmalloc(req->cryptlen + req->assoclen, GFP_ATOMIC);
841 if (!assoc)
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400842 return -ENOMEM;
Herbert Xub7c89d92015-06-01 15:53:06 +0800843 scatterwalk_map_and_copy(assoc, req->src, 0,
844 req->assoclen + req->cryptlen, 0);
845 src = assoc + req->assoclen;
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400846 dst = src;
847 }
848
Herbert Xub7c89d92015-06-01 15:53:06 +0800849 kernel_fpu_begin();
Tim Chend7645932013-12-11 14:28:41 -0800850 aesni_gcm_dec_tfm(aes_ctx, dst, src, tempCipherLen, iv,
Herbert Xue9b8d2c2015-07-09 07:17:28 +0800851 ctx->hash_subkey, assoc, req->assoclen - 8,
852 authTag, auth_tag_len);
Herbert Xub7c89d92015-06-01 15:53:06 +0800853 kernel_fpu_end();
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400854
855 /* Compare generated tag with passed in tag. */
Daniel Borkmannfed28612013-12-11 11:28:59 +0100856 retval = crypto_memneq(src + tempCipherLen, authTag, auth_tag_len) ?
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400857 -EBADMSG : 0;
858
859 if (one_entry_in_sg) {
860 if (unlikely(req->src != req->dst)) {
Herbert Xub7c89d92015-06-01 15:53:06 +0800861 scatterwalk_unmap(dst - req->assoclen);
862 scatterwalk_advance(&dst_sg_walk, req->dst->length);
863 scatterwalk_done(&dst_sg_walk, 1, 0);
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400864 }
Cong Wang8fd75e12011-11-25 23:14:17 +0800865 scatterwalk_unmap(assoc);
Herbert Xub7c89d92015-06-01 15:53:06 +0800866 scatterwalk_advance(&src_sg_walk, req->src->length);
867 scatterwalk_done(&src_sg_walk, req->src == req->dst, 0);
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400868 } else {
Herbert Xub7c89d92015-06-01 15:53:06 +0800869 scatterwalk_map_and_copy(dst, req->dst, req->assoclen,
870 tempCipherLen, 1);
871 kfree(assoc);
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400872 }
873 return retval;
874}
Tadeusz Struk81e397d2015-02-06 10:25:20 -0800875
876static int rfc4106_encrypt(struct aead_request *req)
877{
Tadeusz Struk81e397d2015-02-06 10:25:20 -0800878 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
Herbert Xuaf05b3002015-05-28 22:08:03 +0800879 struct cryptd_aead **ctx = crypto_aead_ctx(tfm);
880 struct cryptd_aead *cryptd_tfm = *ctx;
Tadeusz Struk81e397d2015-02-06 10:25:20 -0800881
Herbert Xu38b2f682016-06-21 16:55:14 +0800882 tfm = &cryptd_tfm->base;
883 if (irq_fpu_usable() && (!in_atomic() ||
884 !cryptd_aead_queued(cryptd_tfm)))
885 tfm = cryptd_aead_child(cryptd_tfm);
886
887 aead_request_set_tfm(req, tfm);
Tadeusz Struk81e397d2015-02-06 10:25:20 -0800888
Herbert Xue9b8d2c2015-07-09 07:17:28 +0800889 return crypto_aead_encrypt(req);
Tadeusz Struk81e397d2015-02-06 10:25:20 -0800890}
891
892static int rfc4106_decrypt(struct aead_request *req)
893{
Tadeusz Struk81e397d2015-02-06 10:25:20 -0800894 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
Herbert Xuaf05b3002015-05-28 22:08:03 +0800895 struct cryptd_aead **ctx = crypto_aead_ctx(tfm);
896 struct cryptd_aead *cryptd_tfm = *ctx;
Tadeusz Struk81e397d2015-02-06 10:25:20 -0800897
Herbert Xu38b2f682016-06-21 16:55:14 +0800898 tfm = &cryptd_tfm->base;
899 if (irq_fpu_usable() && (!in_atomic() ||
900 !cryptd_aead_queued(cryptd_tfm)))
901 tfm = cryptd_aead_child(cryptd_tfm);
902
903 aead_request_set_tfm(req, tfm);
Tadeusz Struk81e397d2015-02-06 10:25:20 -0800904
Herbert Xue9b8d2c2015-07-09 07:17:28 +0800905 return crypto_aead_decrypt(req);
Tadeusz Struk81e397d2015-02-06 10:25:20 -0800906}
Jussi Kivilinnafa46ccb2012-05-11 16:00:48 +0300907#endif
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400908
Jussi Kivilinnafa46ccb2012-05-11 16:00:48 +0300909static struct crypto_alg aesni_algs[] = { {
910 .cra_name = "aes",
911 .cra_driver_name = "aes-aesni",
912 .cra_priority = 300,
913 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
914 .cra_blocksize = AES_BLOCK_SIZE,
Herbert Xu85671862016-11-22 20:08:33 +0800915 .cra_ctxsize = CRYPTO_AES_CTX_SIZE,
Jussi Kivilinnafa46ccb2012-05-11 16:00:48 +0300916 .cra_module = THIS_MODULE,
917 .cra_u = {
918 .cipher = {
919 .cia_min_keysize = AES_MIN_KEY_SIZE,
920 .cia_max_keysize = AES_MAX_KEY_SIZE,
921 .cia_setkey = aes_set_key,
922 .cia_encrypt = aes_encrypt,
923 .cia_decrypt = aes_decrypt
924 }
925 }
926}, {
Herbert Xu85671862016-11-22 20:08:33 +0800927 .cra_name = "__aes",
928 .cra_driver_name = "__aes-aesni",
929 .cra_priority = 300,
Stephan Muellereabdc322015-03-30 21:58:17 +0200930 .cra_flags = CRYPTO_ALG_TYPE_CIPHER | CRYPTO_ALG_INTERNAL,
Jussi Kivilinnafa46ccb2012-05-11 16:00:48 +0300931 .cra_blocksize = AES_BLOCK_SIZE,
Herbert Xu85671862016-11-22 20:08:33 +0800932 .cra_ctxsize = CRYPTO_AES_CTX_SIZE,
Jussi Kivilinnafa46ccb2012-05-11 16:00:48 +0300933 .cra_module = THIS_MODULE,
934 .cra_u = {
935 .cipher = {
936 .cia_min_keysize = AES_MIN_KEY_SIZE,
937 .cia_max_keysize = AES_MAX_KEY_SIZE,
938 .cia_setkey = aes_set_key,
939 .cia_encrypt = __aes_encrypt,
940 .cia_decrypt = __aes_decrypt
941 }
942 }
Jussi Kivilinnafa46ccb2012-05-11 16:00:48 +0300943} };
Tadeusz Struk0bd82f52010-11-04 15:00:45 -0400944
Herbert Xu85671862016-11-22 20:08:33 +0800945static struct skcipher_alg aesni_skciphers[] = {
946 {
947 .base = {
948 .cra_name = "__ecb(aes)",
949 .cra_driver_name = "__ecb-aes-aesni",
950 .cra_priority = 400,
951 .cra_flags = CRYPTO_ALG_INTERNAL,
952 .cra_blocksize = AES_BLOCK_SIZE,
953 .cra_ctxsize = CRYPTO_AES_CTX_SIZE,
954 .cra_module = THIS_MODULE,
955 },
956 .min_keysize = AES_MIN_KEY_SIZE,
957 .max_keysize = AES_MAX_KEY_SIZE,
958 .setkey = aesni_skcipher_setkey,
959 .encrypt = ecb_encrypt,
960 .decrypt = ecb_decrypt,
961 }, {
962 .base = {
963 .cra_name = "__cbc(aes)",
964 .cra_driver_name = "__cbc-aes-aesni",
965 .cra_priority = 400,
966 .cra_flags = CRYPTO_ALG_INTERNAL,
967 .cra_blocksize = AES_BLOCK_SIZE,
968 .cra_ctxsize = CRYPTO_AES_CTX_SIZE,
969 .cra_module = THIS_MODULE,
970 },
971 .min_keysize = AES_MIN_KEY_SIZE,
972 .max_keysize = AES_MAX_KEY_SIZE,
973 .ivsize = AES_BLOCK_SIZE,
974 .setkey = aesni_skcipher_setkey,
975 .encrypt = cbc_encrypt,
976 .decrypt = cbc_decrypt,
977#ifdef CONFIG_X86_64
978 }, {
979 .base = {
980 .cra_name = "__ctr(aes)",
981 .cra_driver_name = "__ctr-aes-aesni",
982 .cra_priority = 400,
983 .cra_flags = CRYPTO_ALG_INTERNAL,
984 .cra_blocksize = 1,
985 .cra_ctxsize = CRYPTO_AES_CTX_SIZE,
986 .cra_module = THIS_MODULE,
987 },
988 .min_keysize = AES_MIN_KEY_SIZE,
989 .max_keysize = AES_MAX_KEY_SIZE,
990 .ivsize = AES_BLOCK_SIZE,
991 .chunksize = AES_BLOCK_SIZE,
992 .setkey = aesni_skcipher_setkey,
993 .encrypt = ctr_crypt,
994 .decrypt = ctr_crypt,
995 }, {
996 .base = {
997 .cra_name = "__xts(aes)",
998 .cra_driver_name = "__xts-aes-aesni",
999 .cra_priority = 401,
1000 .cra_flags = CRYPTO_ALG_INTERNAL,
1001 .cra_blocksize = AES_BLOCK_SIZE,
1002 .cra_ctxsize = XTS_AES_CTX_SIZE,
1003 .cra_module = THIS_MODULE,
1004 },
1005 .min_keysize = 2 * AES_MIN_KEY_SIZE,
1006 .max_keysize = 2 * AES_MAX_KEY_SIZE,
1007 .ivsize = AES_BLOCK_SIZE,
1008 .setkey = xts_aesni_setkey,
1009 .encrypt = xts_encrypt,
1010 .decrypt = xts_decrypt,
1011#endif
1012 }
1013};
1014
1015struct simd_skcipher_alg *aesni_simd_skciphers[ARRAY_SIZE(aesni_skciphers)];
1016
1017struct {
1018 const char *algname;
1019 const char *drvname;
1020 const char *basename;
1021 struct simd_skcipher_alg *simd;
1022} aesni_simd_skciphers2[] = {
1023#if IS_ENABLED(CONFIG_CRYPTO_PCBC)
1024 {
1025 .algname = "pcbc(aes)",
1026 .drvname = "pcbc-aes-aesni",
1027 .basename = "fpu(pcbc(__aes-aesni))",
1028 },
1029#endif
1030};
1031
Herbert Xuaf05b3002015-05-28 22:08:03 +08001032#ifdef CONFIG_X86_64
1033static struct aead_alg aesni_aead_algs[] = { {
Herbert Xub7c89d92015-06-01 15:53:06 +08001034 .setkey = common_rfc4106_set_key,
1035 .setauthsize = common_rfc4106_set_authsize,
1036 .encrypt = helper_rfc4106_encrypt,
1037 .decrypt = helper_rfc4106_decrypt,
1038 .ivsize = 8,
1039 .maxauthsize = 16,
1040 .base = {
1041 .cra_name = "__gcm-aes-aesni",
1042 .cra_driver_name = "__driver-gcm-aes-aesni",
1043 .cra_flags = CRYPTO_ALG_INTERNAL,
1044 .cra_blocksize = 1,
1045 .cra_ctxsize = sizeof(struct aesni_rfc4106_gcm_ctx),
1046 .cra_alignmask = AESNI_ALIGN - 1,
1047 .cra_module = THIS_MODULE,
1048 },
1049}, {
Herbert Xuaf05b3002015-05-28 22:08:03 +08001050 .init = rfc4106_init,
1051 .exit = rfc4106_exit,
1052 .setkey = rfc4106_set_key,
1053 .setauthsize = rfc4106_set_authsize,
1054 .encrypt = rfc4106_encrypt,
1055 .decrypt = rfc4106_decrypt,
1056 .ivsize = 8,
1057 .maxauthsize = 16,
1058 .base = {
1059 .cra_name = "rfc4106(gcm(aes))",
1060 .cra_driver_name = "rfc4106-gcm-aesni",
1061 .cra_priority = 400,
Herbert Xu5e4b8c12015-08-13 17:29:06 +08001062 .cra_flags = CRYPTO_ALG_ASYNC,
Herbert Xuaf05b3002015-05-28 22:08:03 +08001063 .cra_blocksize = 1,
1064 .cra_ctxsize = sizeof(struct cryptd_aead *),
1065 .cra_module = THIS_MODULE,
1066 },
1067} };
1068#else
1069static struct aead_alg aesni_aead_algs[0];
1070#endif
1071
Andi Kleen3bd391f2012-01-26 00:09:06 +01001072
1073static const struct x86_cpu_id aesni_cpu_id[] = {
1074 X86_FEATURE_MATCH(X86_FEATURE_AES),
1075 {}
1076};
1077MODULE_DEVICE_TABLE(x86cpu, aesni_cpu_id);
1078
Herbert Xu85671862016-11-22 20:08:33 +08001079static void aesni_free_simds(void)
1080{
1081 int i;
1082
1083 for (i = 0; i < ARRAY_SIZE(aesni_simd_skciphers) &&
1084 aesni_simd_skciphers[i]; i++)
1085 simd_skcipher_free(aesni_simd_skciphers[i]);
1086
1087 for (i = 0; i < ARRAY_SIZE(aesni_simd_skciphers2) &&
1088 aesni_simd_skciphers2[i].simd; i++)
1089 simd_skcipher_free(aesni_simd_skciphers2[i].simd);
1090}
1091
Huang Ying54b6a1b2009-01-18 16:28:34 +11001092static int __init aesni_init(void)
1093{
Herbert Xu85671862016-11-22 20:08:33 +08001094 struct simd_skcipher_alg *simd;
1095 const char *basename;
1096 const char *algname;
1097 const char *drvname;
Jussi Kivilinna7af6c2452012-07-11 14:20:51 +03001098 int err;
Herbert Xu85671862016-11-22 20:08:33 +08001099 int i;
Huang Ying54b6a1b2009-01-18 16:28:34 +11001100
Andi Kleen3bd391f2012-01-26 00:09:06 +01001101 if (!x86_match_cpu(aesni_cpu_id))
Huang Ying54b6a1b2009-01-18 16:28:34 +11001102 return -ENODEV;
Andy Shevchenko8610d7b2013-12-30 15:52:24 +02001103#ifdef CONFIG_X86_64
Tim Chend7645932013-12-11 14:28:41 -08001104#ifdef CONFIG_AS_AVX2
1105 if (boot_cpu_has(X86_FEATURE_AVX2)) {
1106 pr_info("AVX2 version of gcm_enc/dec engaged.\n");
1107 aesni_gcm_enc_tfm = aesni_gcm_enc_avx2;
1108 aesni_gcm_dec_tfm = aesni_gcm_dec_avx2;
1109 } else
1110#endif
1111#ifdef CONFIG_AS_AVX
1112 if (boot_cpu_has(X86_FEATURE_AVX)) {
1113 pr_info("AVX version of gcm_enc/dec engaged.\n");
1114 aesni_gcm_enc_tfm = aesni_gcm_enc_avx;
1115 aesni_gcm_dec_tfm = aesni_gcm_dec_avx;
1116 } else
1117#endif
1118 {
1119 pr_info("SSE version of gcm_enc/dec engaged.\n");
1120 aesni_gcm_enc_tfm = aesni_gcm_enc;
1121 aesni_gcm_dec_tfm = aesni_gcm_dec;
1122 }
chandramouli narayanan22cddcc2014-06-10 09:22:47 -07001123 aesni_ctr_enc_tfm = aesni_ctr_enc;
Mathias Krause5cfed7b2014-09-28 22:24:01 +02001124#ifdef CONFIG_AS_AVX
Borislav Petkovda154e82016-04-04 22:24:56 +02001125 if (boot_cpu_has(X86_FEATURE_AVX)) {
chandramouli narayanan22cddcc2014-06-10 09:22:47 -07001126 /* optimize performance of ctr mode encryption transform */
1127 aesni_ctr_enc_tfm = aesni_ctr_enc_avx_tfm;
1128 pr_info("AES CTR mode by8 optimization enabled\n");
1129 }
1130#endif
Andy Shevchenko8610d7b2013-12-30 15:52:24 +02001131#endif
Tadeusz Struk0bd82f52010-11-04 15:00:45 -04001132
Jussi Kivilinnafa46ccb2012-05-11 16:00:48 +03001133 err = crypto_fpu_init();
1134 if (err)
1135 return err;
Huang Ying54b6a1b2009-01-18 16:28:34 +11001136
Herbert Xuaf05b3002015-05-28 22:08:03 +08001137 err = crypto_register_algs(aesni_algs, ARRAY_SIZE(aesni_algs));
1138 if (err)
1139 goto fpu_exit;
1140
Herbert Xu85671862016-11-22 20:08:33 +08001141 err = crypto_register_skciphers(aesni_skciphers,
1142 ARRAY_SIZE(aesni_skciphers));
Herbert Xuaf05b3002015-05-28 22:08:03 +08001143 if (err)
1144 goto unregister_algs;
1145
Herbert Xu85671862016-11-22 20:08:33 +08001146 err = crypto_register_aeads(aesni_aead_algs,
1147 ARRAY_SIZE(aesni_aead_algs));
1148 if (err)
1149 goto unregister_skciphers;
Herbert Xuaf05b3002015-05-28 22:08:03 +08001150
Herbert Xu85671862016-11-22 20:08:33 +08001151 for (i = 0; i < ARRAY_SIZE(aesni_skciphers); i++) {
1152 algname = aesni_skciphers[i].base.cra_name + 2;
1153 drvname = aesni_skciphers[i].base.cra_driver_name + 2;
1154 basename = aesni_skciphers[i].base.cra_driver_name;
1155 simd = simd_skcipher_create_compat(algname, drvname, basename);
1156 err = PTR_ERR(simd);
1157 if (IS_ERR(simd))
1158 goto unregister_simds;
1159
1160 aesni_simd_skciphers[i] = simd;
1161 }
1162
1163 for (i = 0; i < ARRAY_SIZE(aesni_simd_skciphers2); i++) {
1164 algname = aesni_simd_skciphers2[i].algname;
1165 drvname = aesni_simd_skciphers2[i].drvname;
1166 basename = aesni_simd_skciphers2[i].basename;
1167 simd = simd_skcipher_create_compat(algname, drvname, basename);
1168 err = PTR_ERR(simd);
1169 if (IS_ERR(simd))
1170 goto unregister_simds;
1171
1172 aesni_simd_skciphers2[i].simd = simd;
1173 }
1174
1175 return 0;
1176
1177unregister_simds:
1178 aesni_free_simds();
1179 crypto_unregister_aeads(aesni_aead_algs, ARRAY_SIZE(aesni_aead_algs));
1180unregister_skciphers:
1181 crypto_unregister_skciphers(aesni_skciphers,
1182 ARRAY_SIZE(aesni_skciphers));
Herbert Xuaf05b3002015-05-28 22:08:03 +08001183unregister_algs:
1184 crypto_unregister_algs(aesni_algs, ARRAY_SIZE(aesni_algs));
1185fpu_exit:
1186 crypto_fpu_exit();
1187 return err;
Huang Ying54b6a1b2009-01-18 16:28:34 +11001188}
1189
1190static void __exit aesni_exit(void)
1191{
Herbert Xu85671862016-11-22 20:08:33 +08001192 aesni_free_simds();
Herbert Xuaf05b3002015-05-28 22:08:03 +08001193 crypto_unregister_aeads(aesni_aead_algs, ARRAY_SIZE(aesni_aead_algs));
Herbert Xu85671862016-11-22 20:08:33 +08001194 crypto_unregister_skciphers(aesni_skciphers,
1195 ARRAY_SIZE(aesni_skciphers));
Jussi Kivilinnafa46ccb2012-05-11 16:00:48 +03001196 crypto_unregister_algs(aesni_algs, ARRAY_SIZE(aesni_algs));
Andy Lutomirskib23b6452011-05-16 15:12:47 +10001197
1198 crypto_fpu_exit();
Huang Ying54b6a1b2009-01-18 16:28:34 +11001199}
1200
Tadeusz Struk0fbafd02015-06-27 15:56:38 +09001201late_initcall(aesni_init);
Huang Ying54b6a1b2009-01-18 16:28:34 +11001202module_exit(aesni_exit);
1203
1204MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm, Intel AES-NI instructions optimized");
1205MODULE_LICENSE("GPL");
Kees Cook5d26a102014-11-20 17:05:53 -08001206MODULE_ALIAS_CRYPTO("aes");