crypto: proc - identify internal ciphers
[deliverable/linux.git] / arch / x86 / crypto / aesni-intel_glue.c
CommitLineData
54b6a1bd
HY
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 *
0bd82f5f
TS
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 *
54b6a1bd
HY
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
22#include <linux/hardirq.h>
23#include <linux/types.h>
24#include <linux/crypto.h>
7c52d551 25#include <linux/module.h>
54b6a1bd
HY
26#include <linux/err.h>
27#include <crypto/algapi.h>
28#include <crypto/aes.h>
29#include <crypto/cryptd.h>
12387a46 30#include <crypto/ctr.h>
023af608
JK
31#include <crypto/b128ops.h>
32#include <crypto/lrw.h>
33#include <crypto/xts.h>
3bd391f0 34#include <asm/cpu_device_id.h>
54b6a1bd 35#include <asm/i387.h>
70ef2601 36#include <asm/crypto/aes.h>
801201aa 37#include <crypto/ablk_helper.h>
0bd82f5f
TS
38#include <crypto/scatterwalk.h>
39#include <crypto/internal/aead.h>
40#include <linux/workqueue.h>
41#include <linux/spinlock.h>
c456a9cd
JK
42#ifdef CONFIG_X86_64
43#include <asm/crypto/glue_helper.h>
44#endif
54b6a1bd 45
e31ac32d 46
0bd82f5f
TS
47/* This data is stored at the end of the crypto_tfm struct.
48 * It's a type of per "session" data storage location.
49 * This needs to be 16 byte aligned.
50 */
51struct aesni_rfc4106_gcm_ctx {
52 u8 hash_subkey[16];
53 struct crypto_aes_ctx aes_key_expanded;
54 u8 nonce[4];
55 struct cryptd_aead *cryptd_tfm;
56};
57
58struct aesni_gcm_set_hash_subkey_result {
59 int err;
60 struct completion completion;
61};
62
63struct aesni_hash_subkey_req_data {
64 u8 iv[16];
65 struct aesni_gcm_set_hash_subkey_result result;
66 struct scatterlist sg;
67};
68
69#define AESNI_ALIGN (16)
54b6a1bd 70#define AES_BLOCK_MASK (~(AES_BLOCK_SIZE-1))
0bd82f5f 71#define RFC4106_HASH_SUBKEY_SIZE 16
54b6a1bd 72
023af608
JK
73struct aesni_lrw_ctx {
74 struct lrw_table_ctx lrw_table;
75 u8 raw_aes_ctx[sizeof(struct crypto_aes_ctx) + AESNI_ALIGN - 1];
76};
77
78struct aesni_xts_ctx {
79 u8 raw_tweak_ctx[sizeof(struct crypto_aes_ctx) + AESNI_ALIGN - 1];
80 u8 raw_crypt_ctx[sizeof(struct crypto_aes_ctx) + AESNI_ALIGN - 1];
81};
82
54b6a1bd
HY
83asmlinkage int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key,
84 unsigned int key_len);
85asmlinkage void aesni_enc(struct crypto_aes_ctx *ctx, u8 *out,
86 const u8 *in);
87asmlinkage void aesni_dec(struct crypto_aes_ctx *ctx, u8 *out,
88 const u8 *in);
89asmlinkage void aesni_ecb_enc(struct crypto_aes_ctx *ctx, u8 *out,
90 const u8 *in, unsigned int len);
91asmlinkage void aesni_ecb_dec(struct crypto_aes_ctx *ctx, u8 *out,
92 const u8 *in, unsigned int len);
93asmlinkage void aesni_cbc_enc(struct crypto_aes_ctx *ctx, u8 *out,
94 const u8 *in, unsigned int len, u8 *iv);
95asmlinkage void aesni_cbc_dec(struct crypto_aes_ctx *ctx, u8 *out,
96 const u8 *in, unsigned int len, u8 *iv);
9bed4aca
RD
97
98int crypto_fpu_init(void);
99void crypto_fpu_exit(void);
100
d764593a
TC
101#define AVX_GEN2_OPTSIZE 640
102#define AVX_GEN4_OPTSIZE 4096
103
0d258efb 104#ifdef CONFIG_X86_64
22cddcc7 105
106static void (*aesni_ctr_enc_tfm)(struct crypto_aes_ctx *ctx, u8 *out,
107 const u8 *in, unsigned int len, u8 *iv);
12387a46
HY
108asmlinkage void aesni_ctr_enc(struct crypto_aes_ctx *ctx, u8 *out,
109 const u8 *in, unsigned int len, u8 *iv);
54b6a1bd 110
c456a9cd
JK
111asmlinkage void aesni_xts_crypt8(struct crypto_aes_ctx *ctx, u8 *out,
112 const u8 *in, bool enc, u8 *iv);
113
0bd82f5f
TS
114/* asmlinkage void aesni_gcm_enc()
115 * void *ctx, AES Key schedule. Starts on a 16 byte boundary.
116 * u8 *out, Ciphertext output. Encrypt in-place is allowed.
117 * const u8 *in, Plaintext input
118 * unsigned long plaintext_len, Length of data in bytes for encryption.
119 * u8 *iv, Pre-counter block j0: 4 byte salt (from Security Association)
120 * concatenated with 8 byte Initialisation Vector (from IPSec ESP
121 * Payload) concatenated with 0x00000001. 16-byte aligned pointer.
122 * u8 *hash_subkey, the Hash sub key input. Data starts on a 16-byte boundary.
123 * const u8 *aad, Additional Authentication Data (AAD)
124 * unsigned long aad_len, Length of AAD in bytes. With RFC4106 this
125 * is going to be 8 or 12 bytes
126 * u8 *auth_tag, Authenticated Tag output.
127 * unsigned long auth_tag_len), Authenticated Tag Length in bytes.
128 * Valid values are 16 (most likely), 12 or 8.
129 */
130asmlinkage void aesni_gcm_enc(void *ctx, u8 *out,
131 const u8 *in, unsigned long plaintext_len, u8 *iv,
132 u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
133 u8 *auth_tag, unsigned long auth_tag_len);
134
135/* asmlinkage void aesni_gcm_dec()
136 * void *ctx, AES Key schedule. Starts on a 16 byte boundary.
137 * u8 *out, Plaintext output. Decrypt in-place is allowed.
138 * const u8 *in, Ciphertext input
139 * unsigned long ciphertext_len, Length of data in bytes for decryption.
140 * u8 *iv, Pre-counter block j0: 4 byte salt (from Security Association)
141 * concatenated with 8 byte Initialisation Vector (from IPSec ESP
142 * Payload) concatenated with 0x00000001. 16-byte aligned pointer.
143 * u8 *hash_subkey, the Hash sub key input. Data starts on a 16-byte boundary.
144 * const u8 *aad, Additional Authentication Data (AAD)
145 * unsigned long aad_len, Length of AAD in bytes. With RFC4106 this is going
146 * to be 8 or 12 bytes
147 * u8 *auth_tag, Authenticated Tag output.
148 * unsigned long auth_tag_len) Authenticated Tag Length in bytes.
149 * Valid values are 16 (most likely), 12 or 8.
150 */
151asmlinkage void aesni_gcm_dec(void *ctx, u8 *out,
152 const u8 *in, unsigned long ciphertext_len, u8 *iv,
153 u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
154 u8 *auth_tag, unsigned long auth_tag_len);
155
d764593a
TC
156
157#ifdef CONFIG_AS_AVX
22cddcc7 158asmlinkage void aes_ctr_enc_128_avx_by8(const u8 *in, u8 *iv,
159 void *keys, u8 *out, unsigned int num_bytes);
160asmlinkage void aes_ctr_enc_192_avx_by8(const u8 *in, u8 *iv,
161 void *keys, u8 *out, unsigned int num_bytes);
162asmlinkage void aes_ctr_enc_256_avx_by8(const u8 *in, u8 *iv,
163 void *keys, u8 *out, unsigned int num_bytes);
d764593a
TC
164/*
165 * asmlinkage void aesni_gcm_precomp_avx_gen2()
166 * gcm_data *my_ctx_data, context data
167 * u8 *hash_subkey, the Hash sub key input. Data starts on a 16-byte boundary.
168 */
169asmlinkage void aesni_gcm_precomp_avx_gen2(void *my_ctx_data, u8 *hash_subkey);
170
171asmlinkage void aesni_gcm_enc_avx_gen2(void *ctx, u8 *out,
172 const u8 *in, unsigned long plaintext_len, u8 *iv,
173 const u8 *aad, unsigned long aad_len,
174 u8 *auth_tag, unsigned long auth_tag_len);
175
176asmlinkage void aesni_gcm_dec_avx_gen2(void *ctx, u8 *out,
177 const u8 *in, unsigned long ciphertext_len, u8 *iv,
178 const u8 *aad, unsigned long aad_len,
179 u8 *auth_tag, unsigned long auth_tag_len);
180
181static void aesni_gcm_enc_avx(void *ctx, u8 *out,
182 const u8 *in, unsigned long plaintext_len, u8 *iv,
183 u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
184 u8 *auth_tag, unsigned long auth_tag_len)
185{
e31ac32d
TM
186 struct crypto_aes_ctx *aes_ctx = (struct crypto_aes_ctx*)ctx;
187 if ((plaintext_len < AVX_GEN2_OPTSIZE) || (aes_ctx-> key_length != AES_KEYSIZE_128)){
d764593a
TC
188 aesni_gcm_enc(ctx, out, in, plaintext_len, iv, hash_subkey, aad,
189 aad_len, auth_tag, auth_tag_len);
190 } else {
191 aesni_gcm_precomp_avx_gen2(ctx, hash_subkey);
192 aesni_gcm_enc_avx_gen2(ctx, out, in, plaintext_len, iv, aad,
193 aad_len, auth_tag, auth_tag_len);
194 }
195}
196
197static void aesni_gcm_dec_avx(void *ctx, u8 *out,
198 const u8 *in, unsigned long ciphertext_len, u8 *iv,
199 u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
200 u8 *auth_tag, unsigned long auth_tag_len)
201{
e31ac32d
TM
202 struct crypto_aes_ctx *aes_ctx = (struct crypto_aes_ctx*)ctx;
203 if ((ciphertext_len < AVX_GEN2_OPTSIZE) || (aes_ctx-> key_length != AES_KEYSIZE_128)) {
d764593a
TC
204 aesni_gcm_dec(ctx, out, in, ciphertext_len, iv, hash_subkey, aad,
205 aad_len, auth_tag, auth_tag_len);
206 } else {
207 aesni_gcm_precomp_avx_gen2(ctx, hash_subkey);
208 aesni_gcm_dec_avx_gen2(ctx, out, in, ciphertext_len, iv, aad,
209 aad_len, auth_tag, auth_tag_len);
210 }
211}
212#endif
213
214#ifdef CONFIG_AS_AVX2
215/*
216 * asmlinkage void aesni_gcm_precomp_avx_gen4()
217 * gcm_data *my_ctx_data, context data
218 * u8 *hash_subkey, the Hash sub key input. Data starts on a 16-byte boundary.
219 */
220asmlinkage void aesni_gcm_precomp_avx_gen4(void *my_ctx_data, u8 *hash_subkey);
221
222asmlinkage void aesni_gcm_enc_avx_gen4(void *ctx, u8 *out,
223 const u8 *in, unsigned long plaintext_len, u8 *iv,
224 const u8 *aad, unsigned long aad_len,
225 u8 *auth_tag, unsigned long auth_tag_len);
226
227asmlinkage void aesni_gcm_dec_avx_gen4(void *ctx, u8 *out,
228 const u8 *in, unsigned long ciphertext_len, u8 *iv,
229 const u8 *aad, unsigned long aad_len,
230 u8 *auth_tag, unsigned long auth_tag_len);
231
232static void aesni_gcm_enc_avx2(void *ctx, u8 *out,
233 const u8 *in, unsigned long plaintext_len, u8 *iv,
234 u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
235 u8 *auth_tag, unsigned long auth_tag_len)
236{
e31ac32d
TM
237 struct crypto_aes_ctx *aes_ctx = (struct crypto_aes_ctx*)ctx;
238 if ((plaintext_len < AVX_GEN2_OPTSIZE) || (aes_ctx-> key_length != AES_KEYSIZE_128)) {
d764593a
TC
239 aesni_gcm_enc(ctx, out, in, plaintext_len, iv, hash_subkey, aad,
240 aad_len, auth_tag, auth_tag_len);
241 } else if (plaintext_len < AVX_GEN4_OPTSIZE) {
242 aesni_gcm_precomp_avx_gen2(ctx, hash_subkey);
243 aesni_gcm_enc_avx_gen2(ctx, out, in, plaintext_len, iv, aad,
244 aad_len, auth_tag, auth_tag_len);
245 } else {
246 aesni_gcm_precomp_avx_gen4(ctx, hash_subkey);
247 aesni_gcm_enc_avx_gen4(ctx, out, in, plaintext_len, iv, aad,
248 aad_len, auth_tag, auth_tag_len);
249 }
250}
251
252static void aesni_gcm_dec_avx2(void *ctx, u8 *out,
253 const u8 *in, unsigned long ciphertext_len, u8 *iv,
254 u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
255 u8 *auth_tag, unsigned long auth_tag_len)
256{
e31ac32d
TM
257 struct crypto_aes_ctx *aes_ctx = (struct crypto_aes_ctx*)ctx;
258 if ((ciphertext_len < AVX_GEN2_OPTSIZE) || (aes_ctx-> key_length != AES_KEYSIZE_128)) {
d764593a
TC
259 aesni_gcm_dec(ctx, out, in, ciphertext_len, iv, hash_subkey,
260 aad, aad_len, auth_tag, auth_tag_len);
261 } else if (ciphertext_len < AVX_GEN4_OPTSIZE) {
262 aesni_gcm_precomp_avx_gen2(ctx, hash_subkey);
263 aesni_gcm_dec_avx_gen2(ctx, out, in, ciphertext_len, iv, aad,
264 aad_len, auth_tag, auth_tag_len);
265 } else {
266 aesni_gcm_precomp_avx_gen4(ctx, hash_subkey);
267 aesni_gcm_dec_avx_gen4(ctx, out, in, ciphertext_len, iv, aad,
268 aad_len, auth_tag, auth_tag_len);
269 }
270}
271#endif
272
273static void (*aesni_gcm_enc_tfm)(void *ctx, u8 *out,
274 const u8 *in, unsigned long plaintext_len, u8 *iv,
275 u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
276 u8 *auth_tag, unsigned long auth_tag_len);
277
278static void (*aesni_gcm_dec_tfm)(void *ctx, u8 *out,
279 const u8 *in, unsigned long ciphertext_len, u8 *iv,
280 u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
281 u8 *auth_tag, unsigned long auth_tag_len);
282
0bd82f5f
TS
283static inline struct
284aesni_rfc4106_gcm_ctx *aesni_rfc4106_gcm_ctx_get(struct crypto_aead *tfm)
285{
286 return
287 (struct aesni_rfc4106_gcm_ctx *)
288 PTR_ALIGN((u8 *)
289 crypto_tfm_ctx(crypto_aead_tfm(tfm)), AESNI_ALIGN);
290}
559ad0ff 291#endif
0bd82f5f 292
54b6a1bd
HY
293static inline struct crypto_aes_ctx *aes_ctx(void *raw_ctx)
294{
295 unsigned long addr = (unsigned long)raw_ctx;
296 unsigned long align = AESNI_ALIGN;
297
298 if (align <= crypto_tfm_ctx_alignment())
299 align = 1;
300 return (struct crypto_aes_ctx *)ALIGN(addr, align);
301}
302
303static int aes_set_key_common(struct crypto_tfm *tfm, void *raw_ctx,
304 const u8 *in_key, unsigned int key_len)
305{
306 struct crypto_aes_ctx *ctx = aes_ctx(raw_ctx);
307 u32 *flags = &tfm->crt_flags;
308 int err;
309
310 if (key_len != AES_KEYSIZE_128 && key_len != AES_KEYSIZE_192 &&
311 key_len != AES_KEYSIZE_256) {
312 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
313 return -EINVAL;
314 }
315
13b79b97 316 if (!irq_fpu_usable())
54b6a1bd
HY
317 err = crypto_aes_expand_key(ctx, in_key, key_len);
318 else {
319 kernel_fpu_begin();
320 err = aesni_set_key(ctx, in_key, key_len);
321 kernel_fpu_end();
322 }
323
324 return err;
325}
326
327static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
328 unsigned int key_len)
329{
330 return aes_set_key_common(tfm, crypto_tfm_ctx(tfm), in_key, key_len);
331}
332
333static void aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
334{
335 struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
336
13b79b97 337 if (!irq_fpu_usable())
54b6a1bd
HY
338 crypto_aes_encrypt_x86(ctx, dst, src);
339 else {
340 kernel_fpu_begin();
341 aesni_enc(ctx, dst, src);
342 kernel_fpu_end();
343 }
344}
345
346static void aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
347{
348 struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
349
13b79b97 350 if (!irq_fpu_usable())
54b6a1bd
HY
351 crypto_aes_decrypt_x86(ctx, dst, src);
352 else {
353 kernel_fpu_begin();
354 aesni_dec(ctx, dst, src);
355 kernel_fpu_end();
356 }
357}
358
2cf4ac8b
HY
359static 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
363 aesni_enc(ctx, dst, src);
364}
365
366static void __aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
367{
368 struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
369
370 aesni_dec(ctx, dst, src);
371}
372
54b6a1bd
HY
373static int ecb_encrypt(struct blkcipher_desc *desc,
374 struct scatterlist *dst, struct scatterlist *src,
375 unsigned int nbytes)
376{
377 struct crypto_aes_ctx *ctx = aes_ctx(crypto_blkcipher_ctx(desc->tfm));
378 struct blkcipher_walk walk;
379 int err;
380
381 blkcipher_walk_init(&walk, dst, src, nbytes);
382 err = blkcipher_walk_virt(desc, &walk);
9251b64f 383 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
54b6a1bd
HY
384
385 kernel_fpu_begin();
386 while ((nbytes = walk.nbytes)) {
387 aesni_ecb_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
388 nbytes & AES_BLOCK_MASK);
389 nbytes &= AES_BLOCK_SIZE - 1;
390 err = blkcipher_walk_done(desc, &walk, nbytes);
391 }
392 kernel_fpu_end();
393
394 return err;
395}
396
397static int ecb_decrypt(struct blkcipher_desc *desc,
398 struct scatterlist *dst, struct scatterlist *src,
399 unsigned int nbytes)
400{
401 struct crypto_aes_ctx *ctx = aes_ctx(crypto_blkcipher_ctx(desc->tfm));
402 struct blkcipher_walk walk;
403 int err;
404
405 blkcipher_walk_init(&walk, dst, src, nbytes);
406 err = blkcipher_walk_virt(desc, &walk);
9251b64f 407 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
54b6a1bd
HY
408
409 kernel_fpu_begin();
410 while ((nbytes = walk.nbytes)) {
411 aesni_ecb_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
412 nbytes & AES_BLOCK_MASK);
413 nbytes &= AES_BLOCK_SIZE - 1;
414 err = blkcipher_walk_done(desc, &walk, nbytes);
415 }
416 kernel_fpu_end();
417
418 return err;
419}
420
54b6a1bd
HY
421static int cbc_encrypt(struct blkcipher_desc *desc,
422 struct scatterlist *dst, struct scatterlist *src,
423 unsigned int nbytes)
424{
425 struct crypto_aes_ctx *ctx = aes_ctx(crypto_blkcipher_ctx(desc->tfm));
426 struct blkcipher_walk walk;
427 int err;
428
429 blkcipher_walk_init(&walk, dst, src, nbytes);
430 err = blkcipher_walk_virt(desc, &walk);
9251b64f 431 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
54b6a1bd
HY
432
433 kernel_fpu_begin();
434 while ((nbytes = walk.nbytes)) {
435 aesni_cbc_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
436 nbytes & AES_BLOCK_MASK, walk.iv);
437 nbytes &= AES_BLOCK_SIZE - 1;
438 err = blkcipher_walk_done(desc, &walk, nbytes);
439 }
440 kernel_fpu_end();
441
442 return err;
443}
444
445static int cbc_decrypt(struct blkcipher_desc *desc,
446 struct scatterlist *dst, struct scatterlist *src,
447 unsigned int nbytes)
448{
449 struct crypto_aes_ctx *ctx = aes_ctx(crypto_blkcipher_ctx(desc->tfm));
450 struct blkcipher_walk walk;
451 int err;
452
453 blkcipher_walk_init(&walk, dst, src, nbytes);
454 err = blkcipher_walk_virt(desc, &walk);
9251b64f 455 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
54b6a1bd
HY
456
457 kernel_fpu_begin();
458 while ((nbytes = walk.nbytes)) {
459 aesni_cbc_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
460 nbytes & AES_BLOCK_MASK, walk.iv);
461 nbytes &= AES_BLOCK_SIZE - 1;
462 err = blkcipher_walk_done(desc, &walk, nbytes);
463 }
464 kernel_fpu_end();
465
466 return err;
467}
468
0d258efb 469#ifdef CONFIG_X86_64
12387a46
HY
470static void ctr_crypt_final(struct crypto_aes_ctx *ctx,
471 struct blkcipher_walk *walk)
472{
473 u8 *ctrblk = walk->iv;
474 u8 keystream[AES_BLOCK_SIZE];
475 u8 *src = walk->src.virt.addr;
476 u8 *dst = walk->dst.virt.addr;
477 unsigned int nbytes = walk->nbytes;
478
479 aesni_enc(ctx, keystream, ctrblk);
480 crypto_xor(keystream, src, nbytes);
481 memcpy(dst, keystream, nbytes);
482 crypto_inc(ctrblk, AES_BLOCK_SIZE);
483}
484
5cfed7b3 485#ifdef CONFIG_AS_AVX
22cddcc7 486static void aesni_ctr_enc_avx_tfm(struct crypto_aes_ctx *ctx, u8 *out,
487 const u8 *in, unsigned int len, u8 *iv)
488{
489 /*
490 * based on key length, override with the by8 version
491 * of ctr mode encryption/decryption for improved performance
492 * aes_set_key_common() ensures that key length is one of
493 * {128,192,256}
494 */
495 if (ctx->key_length == AES_KEYSIZE_128)
496 aes_ctr_enc_128_avx_by8(in, iv, (void *)ctx, out, len);
497 else if (ctx->key_length == AES_KEYSIZE_192)
498 aes_ctr_enc_192_avx_by8(in, iv, (void *)ctx, out, len);
499 else
500 aes_ctr_enc_256_avx_by8(in, iv, (void *)ctx, out, len);
501}
502#endif
503
12387a46
HY
504static int ctr_crypt(struct blkcipher_desc *desc,
505 struct scatterlist *dst, struct scatterlist *src,
506 unsigned int nbytes)
507{
508 struct crypto_aes_ctx *ctx = aes_ctx(crypto_blkcipher_ctx(desc->tfm));
509 struct blkcipher_walk walk;
510 int err;
511
512 blkcipher_walk_init(&walk, dst, src, nbytes);
513 err = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE);
514 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
515
516 kernel_fpu_begin();
517 while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
22cddcc7 518 aesni_ctr_enc_tfm(ctx, walk.dst.virt.addr, walk.src.virt.addr,
e31ac32d 519 nbytes & AES_BLOCK_MASK, walk.iv);
12387a46
HY
520 nbytes &= AES_BLOCK_SIZE - 1;
521 err = blkcipher_walk_done(desc, &walk, nbytes);
522 }
523 if (walk.nbytes) {
524 ctr_crypt_final(ctx, &walk);
525 err = blkcipher_walk_done(desc, &walk, 0);
526 }
527 kernel_fpu_end();
528
529 return err;
530}
0d258efb 531#endif
12387a46 532
54b6a1bd
HY
533static int ablk_ecb_init(struct crypto_tfm *tfm)
534{
ef45b834 535 return ablk_init_common(tfm, "__driver-ecb-aes-aesni");
54b6a1bd
HY
536}
537
54b6a1bd
HY
538static int ablk_cbc_init(struct crypto_tfm *tfm)
539{
ef45b834 540 return ablk_init_common(tfm, "__driver-cbc-aes-aesni");
54b6a1bd
HY
541}
542
0d258efb 543#ifdef CONFIG_X86_64
2cf4ac8b
HY
544static int ablk_ctr_init(struct crypto_tfm *tfm)
545{
ef45b834 546 return ablk_init_common(tfm, "__driver-ctr-aes-aesni");
2cf4ac8b
HY
547}
548
0d258efb 549#endif
2cf4ac8b 550
304576a7 551#if IS_ENABLED(CONFIG_CRYPTO_PCBC)
2cf4ac8b
HY
552static int ablk_pcbc_init(struct crypto_tfm *tfm)
553{
ef45b834 554 return ablk_init_common(tfm, "fpu(pcbc(__driver-aes-aesni))");
2cf4ac8b 555}
2cf4ac8b
HY
556#endif
557
023af608 558static void lrw_xts_encrypt_callback(void *ctx, u8 *blks, unsigned int nbytes)
2cf4ac8b 559{
023af608
JK
560 aesni_ecb_enc(ctx, blks, blks, nbytes);
561}
562
563static void lrw_xts_decrypt_callback(void *ctx, u8 *blks, unsigned int nbytes)
564{
565 aesni_ecb_dec(ctx, blks, blks, nbytes);
566}
567
568static int lrw_aesni_setkey(struct crypto_tfm *tfm, const u8 *key,
569 unsigned int keylen)
570{
571 struct aesni_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
572 int err;
573
574 err = aes_set_key_common(tfm, ctx->raw_aes_ctx, key,
575 keylen - AES_BLOCK_SIZE);
576 if (err)
577 return err;
578
579 return lrw_init_table(&ctx->lrw_table, key + keylen - AES_BLOCK_SIZE);
580}
581
582static void lrw_aesni_exit_tfm(struct crypto_tfm *tfm)
583{
584 struct aesni_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
585
586 lrw_free_table(&ctx->lrw_table);
587}
588
589static int lrw_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
590 struct scatterlist *src, unsigned int nbytes)
591{
592 struct aesni_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
593 be128 buf[8];
594 struct lrw_crypt_req req = {
595 .tbuf = buf,
596 .tbuflen = sizeof(buf),
597
598 .table_ctx = &ctx->lrw_table,
599 .crypt_ctx = aes_ctx(ctx->raw_aes_ctx),
600 .crypt_fn = lrw_xts_encrypt_callback,
601 };
602 int ret;
603
604 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
605
606 kernel_fpu_begin();
607 ret = lrw_crypt(desc, dst, src, nbytes, &req);
608 kernel_fpu_end();
609
610 return ret;
611}
612
613static int lrw_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
614 struct scatterlist *src, unsigned int nbytes)
615{
616 struct aesni_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
617 be128 buf[8];
618 struct lrw_crypt_req req = {
619 .tbuf = buf,
620 .tbuflen = sizeof(buf),
621
622 .table_ctx = &ctx->lrw_table,
623 .crypt_ctx = aes_ctx(ctx->raw_aes_ctx),
624 .crypt_fn = lrw_xts_decrypt_callback,
625 };
626 int ret;
627
628 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
629
630 kernel_fpu_begin();
631 ret = lrw_crypt(desc, dst, src, nbytes, &req);
632 kernel_fpu_end();
633
634 return ret;
635}
636
637static int xts_aesni_setkey(struct crypto_tfm *tfm, const u8 *key,
638 unsigned int keylen)
639{
640 struct aesni_xts_ctx *ctx = crypto_tfm_ctx(tfm);
641 u32 *flags = &tfm->crt_flags;
642 int err;
643
644 /* key consists of keys of equal size concatenated, therefore
645 * the length must be even
646 */
647 if (keylen % 2) {
648 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
649 return -EINVAL;
650 }
651
652 /* first half of xts-key is for crypt */
653 err = aes_set_key_common(tfm, ctx->raw_crypt_ctx, key, keylen / 2);
654 if (err)
655 return err;
656
657 /* second half of xts-key is for tweak */
658 return aes_set_key_common(tfm, ctx->raw_tweak_ctx, key + keylen / 2,
659 keylen / 2);
660}
661
662
32bec973
JK
663static void aesni_xts_tweak(void *ctx, u8 *out, const u8 *in)
664{
665 aesni_enc(ctx, out, in);
666}
667
c456a9cd
JK
668#ifdef CONFIG_X86_64
669
670static void aesni_xts_enc(void *ctx, u128 *dst, const u128 *src, le128 *iv)
671{
672 glue_xts_crypt_128bit_one(ctx, dst, src, iv, GLUE_FUNC_CAST(aesni_enc));
673}
674
675static void aesni_xts_dec(void *ctx, u128 *dst, const u128 *src, le128 *iv)
676{
677 glue_xts_crypt_128bit_one(ctx, dst, src, iv, GLUE_FUNC_CAST(aesni_dec));
678}
679
680static void aesni_xts_enc8(void *ctx, u128 *dst, const u128 *src, le128 *iv)
681{
682 aesni_xts_crypt8(ctx, (u8 *)dst, (const u8 *)src, true, (u8 *)iv);
683}
684
685static void aesni_xts_dec8(void *ctx, u128 *dst, const u128 *src, le128 *iv)
686{
687 aesni_xts_crypt8(ctx, (u8 *)dst, (const u8 *)src, false, (u8 *)iv);
688}
689
690static const struct common_glue_ctx aesni_enc_xts = {
691 .num_funcs = 2,
692 .fpu_blocks_limit = 1,
693
694 .funcs = { {
695 .num_blocks = 8,
696 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(aesni_xts_enc8) }
697 }, {
698 .num_blocks = 1,
699 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(aesni_xts_enc) }
700 } }
701};
702
703static const struct common_glue_ctx aesni_dec_xts = {
704 .num_funcs = 2,
705 .fpu_blocks_limit = 1,
706
707 .funcs = { {
708 .num_blocks = 8,
709 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(aesni_xts_dec8) }
710 }, {
711 .num_blocks = 1,
712 .fn_u = { .xts = GLUE_XTS_FUNC_CAST(aesni_xts_dec) }
713 } }
714};
715
716static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
717 struct scatterlist *src, unsigned int nbytes)
718{
719 struct aesni_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
720
721 return glue_xts_crypt_128bit(&aesni_enc_xts, desc, dst, src, nbytes,
722 XTS_TWEAK_CAST(aesni_xts_tweak),
723 aes_ctx(ctx->raw_tweak_ctx),
724 aes_ctx(ctx->raw_crypt_ctx));
725}
726
727static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
728 struct scatterlist *src, unsigned int nbytes)
729{
730 struct aesni_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
731
732 return glue_xts_crypt_128bit(&aesni_dec_xts, desc, dst, src, nbytes,
733 XTS_TWEAK_CAST(aesni_xts_tweak),
734 aes_ctx(ctx->raw_tweak_ctx),
735 aes_ctx(ctx->raw_crypt_ctx));
736}
737
738#else
739
023af608
JK
740static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
741 struct scatterlist *src, unsigned int nbytes)
742{
743 struct aesni_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
744 be128 buf[8];
745 struct xts_crypt_req req = {
746 .tbuf = buf,
747 .tbuflen = sizeof(buf),
748
749 .tweak_ctx = aes_ctx(ctx->raw_tweak_ctx),
32bec973 750 .tweak_fn = aesni_xts_tweak,
023af608
JK
751 .crypt_ctx = aes_ctx(ctx->raw_crypt_ctx),
752 .crypt_fn = lrw_xts_encrypt_callback,
753 };
754 int ret;
755
756 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
757
758 kernel_fpu_begin();
759 ret = xts_crypt(desc, dst, src, nbytes, &req);
760 kernel_fpu_end();
761
762 return ret;
763}
764
765static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
766 struct scatterlist *src, unsigned int nbytes)
767{
768 struct aesni_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
769 be128 buf[8];
770 struct xts_crypt_req req = {
771 .tbuf = buf,
772 .tbuflen = sizeof(buf),
773
774 .tweak_ctx = aes_ctx(ctx->raw_tweak_ctx),
32bec973 775 .tweak_fn = aesni_xts_tweak,
023af608
JK
776 .crypt_ctx = aes_ctx(ctx->raw_crypt_ctx),
777 .crypt_fn = lrw_xts_decrypt_callback,
778 };
779 int ret;
780
781 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
782
783 kernel_fpu_begin();
784 ret = xts_crypt(desc, dst, src, nbytes, &req);
785 kernel_fpu_end();
786
787 return ret;
2cf4ac8b 788}
2cf4ac8b 789
c456a9cd
JK
790#endif
791
559ad0ff 792#ifdef CONFIG_X86_64
0bd82f5f
TS
793static int rfc4106_init(struct crypto_tfm *tfm)
794{
795 struct cryptd_aead *cryptd_tfm;
796 struct aesni_rfc4106_gcm_ctx *ctx = (struct aesni_rfc4106_gcm_ctx *)
797 PTR_ALIGN((u8 *)crypto_tfm_ctx(tfm), AESNI_ALIGN);
60af520c
TS
798 struct crypto_aead *cryptd_child;
799 struct aesni_rfc4106_gcm_ctx *child_ctx;
0bd82f5f
TS
800 cryptd_tfm = cryptd_alloc_aead("__driver-gcm-aes-aesni", 0, 0);
801 if (IS_ERR(cryptd_tfm))
802 return PTR_ERR(cryptd_tfm);
60af520c
TS
803
804 cryptd_child = cryptd_aead_child(cryptd_tfm);
805 child_ctx = aesni_rfc4106_gcm_ctx_get(cryptd_child);
806 memcpy(child_ctx, ctx, sizeof(*ctx));
0bd82f5f
TS
807 ctx->cryptd_tfm = cryptd_tfm;
808 tfm->crt_aead.reqsize = sizeof(struct aead_request)
809 + crypto_aead_reqsize(&cryptd_tfm->base);
810 return 0;
811}
812
813static void rfc4106_exit(struct crypto_tfm *tfm)
814{
815 struct aesni_rfc4106_gcm_ctx *ctx =
816 (struct aesni_rfc4106_gcm_ctx *)
817 PTR_ALIGN((u8 *)crypto_tfm_ctx(tfm), AESNI_ALIGN);
818 if (!IS_ERR(ctx->cryptd_tfm))
819 cryptd_free_aead(ctx->cryptd_tfm);
820 return;
821}
822
823static void
824rfc4106_set_hash_subkey_done(struct crypto_async_request *req, int err)
825{
826 struct aesni_gcm_set_hash_subkey_result *result = req->data;
827
828 if (err == -EINPROGRESS)
829 return;
830 result->err = err;
831 complete(&result->completion);
832}
833
834static int
835rfc4106_set_hash_subkey(u8 *hash_subkey, const u8 *key, unsigned int key_len)
836{
837 struct crypto_ablkcipher *ctr_tfm;
838 struct ablkcipher_request *req;
839 int ret = -EINVAL;
840 struct aesni_hash_subkey_req_data *req_data;
841
842 ctr_tfm = crypto_alloc_ablkcipher("ctr(aes)", 0, 0);
843 if (IS_ERR(ctr_tfm))
844 return PTR_ERR(ctr_tfm);
845
846 crypto_ablkcipher_clear_flags(ctr_tfm, ~0);
847
848 ret = crypto_ablkcipher_setkey(ctr_tfm, key, key_len);
7efd95f6 849 if (ret)
fc9044e2 850 goto out_free_ablkcipher;
0bd82f5f 851
fc9044e2 852 ret = -ENOMEM;
0bd82f5f 853 req = ablkcipher_request_alloc(ctr_tfm, GFP_KERNEL);
fc9044e2 854 if (!req)
7efd95f6 855 goto out_free_ablkcipher;
0bd82f5f
TS
856
857 req_data = kmalloc(sizeof(*req_data), GFP_KERNEL);
fc9044e2 858 if (!req_data)
7efd95f6 859 goto out_free_request;
fc9044e2 860
0bd82f5f
TS
861 memset(req_data->iv, 0, sizeof(req_data->iv));
862
863 /* Clear the data in the hash sub key container to zero.*/
864 /* We want to cipher all zeros to create the hash sub key. */
865 memset(hash_subkey, 0, RFC4106_HASH_SUBKEY_SIZE);
866
867 init_completion(&req_data->result.completion);
868 sg_init_one(&req_data->sg, hash_subkey, RFC4106_HASH_SUBKEY_SIZE);
869 ablkcipher_request_set_tfm(req, ctr_tfm);
870 ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP |
871 CRYPTO_TFM_REQ_MAY_BACKLOG,
872 rfc4106_set_hash_subkey_done,
873 &req_data->result);
874
875 ablkcipher_request_set_crypt(req, &req_data->sg,
876 &req_data->sg, RFC4106_HASH_SUBKEY_SIZE, req_data->iv);
877
878 ret = crypto_ablkcipher_encrypt(req);
879 if (ret == -EINPROGRESS || ret == -EBUSY) {
880 ret = wait_for_completion_interruptible
881 (&req_data->result.completion);
882 if (!ret)
883 ret = req_data->result.err;
884 }
fc9044e2 885 kfree(req_data);
7efd95f6 886out_free_request:
0bd82f5f 887 ablkcipher_request_free(req);
7efd95f6 888out_free_ablkcipher:
0bd82f5f
TS
889 crypto_free_ablkcipher(ctr_tfm);
890 return ret;
891}
892
81e397d9
TS
893static int common_rfc4106_set_key(struct crypto_aead *aead, const u8 *key,
894 unsigned int key_len)
0bd82f5f
TS
895{
896 int ret = 0;
81e397d9
TS
897 struct crypto_tfm *tfm = crypto_aead_tfm(aead);
898 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(aead);
bf084d8f 899 u8 *new_key_align, *new_key_mem = NULL;
0bd82f5f
TS
900
901 if (key_len < 4) {
902 crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
903 return -EINVAL;
904 }
905 /*Account for 4 byte nonce at the end.*/
906 key_len -= 4;
e31ac32d
TM
907 if (key_len != AES_KEYSIZE_128 && key_len != AES_KEYSIZE_192 &&
908 key_len != AES_KEYSIZE_256) {
0bd82f5f
TS
909 crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
910 return -EINVAL;
911 }
912
913 memcpy(ctx->nonce, key + key_len, sizeof(ctx->nonce));
914 /*This must be on a 16 byte boundary!*/
915 if ((unsigned long)(&(ctx->aes_key_expanded.key_enc[0])) % AESNI_ALIGN)
916 return -EINVAL;
917
918 if ((unsigned long)key % AESNI_ALIGN) {
919 /*key is not aligned: use an auxuliar aligned pointer*/
920 new_key_mem = kmalloc(key_len+AESNI_ALIGN, GFP_KERNEL);
921 if (!new_key_mem)
922 return -ENOMEM;
923
bf084d8f
MB
924 new_key_align = PTR_ALIGN(new_key_mem, AESNI_ALIGN);
925 memcpy(new_key_align, key, key_len);
926 key = new_key_align;
0bd82f5f
TS
927 }
928
929 if (!irq_fpu_usable())
930 ret = crypto_aes_expand_key(&(ctx->aes_key_expanded),
931 key, key_len);
932 else {
933 kernel_fpu_begin();
934 ret = aesni_set_key(&(ctx->aes_key_expanded), key, key_len);
935 kernel_fpu_end();
936 }
937 /*This must be on a 16 byte boundary!*/
938 if ((unsigned long)(&(ctx->hash_subkey[0])) % AESNI_ALIGN) {
939 ret = -EINVAL;
940 goto exit;
941 }
942 ret = rfc4106_set_hash_subkey(ctx->hash_subkey, key, key_len);
943exit:
944 kfree(new_key_mem);
945 return ret;
946}
947
81e397d9
TS
948static int rfc4106_set_key(struct crypto_aead *parent, const u8 *key,
949 unsigned int key_len)
0bd82f5f
TS
950{
951 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(parent);
81e397d9
TS
952 struct crypto_aead *child = cryptd_aead_child(ctx->cryptd_tfm);
953 struct aesni_rfc4106_gcm_ctx *c_ctx = aesni_rfc4106_gcm_ctx_get(child);
954 struct cryptd_aead *cryptd_tfm = ctx->cryptd_tfm;
955 int ret;
0bd82f5f 956
81e397d9
TS
957 ret = crypto_aead_setkey(child, key, key_len);
958 if (!ret) {
959 memcpy(ctx, c_ctx, sizeof(*ctx));
960 ctx->cryptd_tfm = cryptd_tfm;
961 }
962 return ret;
963}
964
965static int common_rfc4106_set_authsize(struct crypto_aead *aead,
966 unsigned int authsize)
967{
0bd82f5f
TS
968 switch (authsize) {
969 case 8:
970 case 12:
971 case 16:
972 break;
973 default:
974 return -EINVAL;
975 }
81e397d9 976 crypto_aead_crt(aead)->authsize = authsize;
0bd82f5f
TS
977 return 0;
978}
979
81e397d9
TS
980/* This is the Integrity Check Value (aka the authentication tag length and can
981 * be 8, 12 or 16 bytes long. */
982static int rfc4106_set_authsize(struct crypto_aead *parent,
983 unsigned int authsize)
0bd82f5f 984{
81e397d9
TS
985 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(parent);
986 struct crypto_aead *child = cryptd_aead_child(ctx->cryptd_tfm);
0bd82f5f 987 int ret;
0bd82f5f 988
81e397d9
TS
989 ret = crypto_aead_setauthsize(child, authsize);
990 if (!ret)
991 crypto_aead_crt(parent)->authsize = authsize;
992 return ret;
0bd82f5f
TS
993}
994
0bd82f5f
TS
995static int __driver_rfc4106_encrypt(struct aead_request *req)
996{
997 u8 one_entry_in_sg = 0;
998 u8 *src, *dst, *assoc;
999 __be32 counter = cpu_to_be32(1);
1000 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1001 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
e31ac32d 1002 u32 key_len = ctx->aes_key_expanded.key_length;
0bd82f5f
TS
1003 void *aes_ctx = &(ctx->aes_key_expanded);
1004 unsigned long auth_tag_len = crypto_aead_authsize(tfm);
1005 u8 iv_tab[16+AESNI_ALIGN];
1006 u8* iv = (u8 *) PTR_ALIGN((u8 *)iv_tab, AESNI_ALIGN);
1007 struct scatter_walk src_sg_walk;
1008 struct scatter_walk assoc_sg_walk;
1009 struct scatter_walk dst_sg_walk;
1010 unsigned int i;
1011
1012 /* Assuming we are supporting rfc4106 64-bit extended */
1013 /* sequence numbers We need to have the AAD length equal */
1014 /* to 8 or 12 bytes */
1015 if (unlikely(req->assoclen != 8 && req->assoclen != 12))
1016 return -EINVAL;
e31ac32d
TM
1017 if (unlikely(auth_tag_len != 8 && auth_tag_len != 12 && auth_tag_len != 16))
1018 return -EINVAL;
1019 if (unlikely(key_len != AES_KEYSIZE_128 &&
1020 key_len != AES_KEYSIZE_192 &&
1021 key_len != AES_KEYSIZE_256))
1022 return -EINVAL;
1023
0bd82f5f
TS
1024 /* IV below built */
1025 for (i = 0; i < 4; i++)
1026 *(iv+i) = ctx->nonce[i];
1027 for (i = 0; i < 8; i++)
1028 *(iv+4+i) = req->iv[i];
1029 *((__be32 *)(iv+12)) = counter;
1030
1031 if ((sg_is_last(req->src)) && (sg_is_last(req->assoc))) {
1032 one_entry_in_sg = 1;
1033 scatterwalk_start(&src_sg_walk, req->src);
1034 scatterwalk_start(&assoc_sg_walk, req->assoc);
8fd75e12
CW
1035 src = scatterwalk_map(&src_sg_walk);
1036 assoc = scatterwalk_map(&assoc_sg_walk);
0bd82f5f
TS
1037 dst = src;
1038 if (unlikely(req->src != req->dst)) {
1039 scatterwalk_start(&dst_sg_walk, req->dst);
8fd75e12 1040 dst = scatterwalk_map(&dst_sg_walk);
0bd82f5f
TS
1041 }
1042
1043 } else {
1044 /* Allocate memory for src, dst, assoc */
1045 src = kmalloc(req->cryptlen + auth_tag_len + req->assoclen,
1046 GFP_ATOMIC);
1047 if (unlikely(!src))
1048 return -ENOMEM;
1049 assoc = (src + req->cryptlen + auth_tag_len);
1050 scatterwalk_map_and_copy(src, req->src, 0, req->cryptlen, 0);
1051 scatterwalk_map_and_copy(assoc, req->assoc, 0,
1052 req->assoclen, 0);
1053 dst = src;
1054 }
1055
d764593a 1056 aesni_gcm_enc_tfm(aes_ctx, dst, src, (unsigned long)req->cryptlen, iv,
0bd82f5f
TS
1057 ctx->hash_subkey, assoc, (unsigned long)req->assoclen, dst
1058 + ((unsigned long)req->cryptlen), auth_tag_len);
1059
1060 /* The authTag (aka the Integrity Check Value) needs to be written
1061 * back to the packet. */
1062 if (one_entry_in_sg) {
1063 if (unlikely(req->src != req->dst)) {
8fd75e12 1064 scatterwalk_unmap(dst);
0bd82f5f
TS
1065 scatterwalk_done(&dst_sg_walk, 0, 0);
1066 }
8fd75e12
CW
1067 scatterwalk_unmap(src);
1068 scatterwalk_unmap(assoc);
0bd82f5f
TS
1069 scatterwalk_done(&src_sg_walk, 0, 0);
1070 scatterwalk_done(&assoc_sg_walk, 0, 0);
1071 } else {
1072 scatterwalk_map_and_copy(dst, req->dst, 0,
1073 req->cryptlen + auth_tag_len, 1);
1074 kfree(src);
1075 }
1076 return 0;
1077}
1078
1079static int __driver_rfc4106_decrypt(struct aead_request *req)
1080{
1081 u8 one_entry_in_sg = 0;
1082 u8 *src, *dst, *assoc;
1083 unsigned long tempCipherLen = 0;
1084 __be32 counter = cpu_to_be32(1);
1085 int retval = 0;
1086 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1087 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
e31ac32d 1088 u32 key_len = ctx->aes_key_expanded.key_length;
0bd82f5f
TS
1089 void *aes_ctx = &(ctx->aes_key_expanded);
1090 unsigned long auth_tag_len = crypto_aead_authsize(tfm);
1091 u8 iv_and_authTag[32+AESNI_ALIGN];
1092 u8 *iv = (u8 *) PTR_ALIGN((u8 *)iv_and_authTag, AESNI_ALIGN);
1093 u8 *authTag = iv + 16;
1094 struct scatter_walk src_sg_walk;
1095 struct scatter_walk assoc_sg_walk;
1096 struct scatter_walk dst_sg_walk;
1097 unsigned int i;
1098
1099 if (unlikely((req->cryptlen < auth_tag_len) ||
1100 (req->assoclen != 8 && req->assoclen != 12)))
1101 return -EINVAL;
e31ac32d
TM
1102 if (unlikely(auth_tag_len != 8 && auth_tag_len != 12 && auth_tag_len != 16))
1103 return -EINVAL;
1104 if (unlikely(key_len != AES_KEYSIZE_128 &&
1105 key_len != AES_KEYSIZE_192 &&
1106 key_len != AES_KEYSIZE_256))
1107 return -EINVAL;
1108
0bd82f5f
TS
1109 /* Assuming we are supporting rfc4106 64-bit extended */
1110 /* sequence numbers We need to have the AAD length */
1111 /* equal to 8 or 12 bytes */
1112
1113 tempCipherLen = (unsigned long)(req->cryptlen - auth_tag_len);
1114 /* IV below built */
1115 for (i = 0; i < 4; i++)
1116 *(iv+i) = ctx->nonce[i];
1117 for (i = 0; i < 8; i++)
1118 *(iv+4+i) = req->iv[i];
1119 *((__be32 *)(iv+12)) = counter;
1120
1121 if ((sg_is_last(req->src)) && (sg_is_last(req->assoc))) {
1122 one_entry_in_sg = 1;
1123 scatterwalk_start(&src_sg_walk, req->src);
1124 scatterwalk_start(&assoc_sg_walk, req->assoc);
8fd75e12
CW
1125 src = scatterwalk_map(&src_sg_walk);
1126 assoc = scatterwalk_map(&assoc_sg_walk);
0bd82f5f
TS
1127 dst = src;
1128 if (unlikely(req->src != req->dst)) {
1129 scatterwalk_start(&dst_sg_walk, req->dst);
8fd75e12 1130 dst = scatterwalk_map(&dst_sg_walk);
0bd82f5f
TS
1131 }
1132
1133 } else {
1134 /* Allocate memory for src, dst, assoc */
1135 src = kmalloc(req->cryptlen + req->assoclen, GFP_ATOMIC);
1136 if (!src)
1137 return -ENOMEM;
1138 assoc = (src + req->cryptlen + auth_tag_len);
1139 scatterwalk_map_and_copy(src, req->src, 0, req->cryptlen, 0);
1140 scatterwalk_map_and_copy(assoc, req->assoc, 0,
1141 req->assoclen, 0);
1142 dst = src;
1143 }
1144
d764593a 1145 aesni_gcm_dec_tfm(aes_ctx, dst, src, tempCipherLen, iv,
0bd82f5f
TS
1146 ctx->hash_subkey, assoc, (unsigned long)req->assoclen,
1147 authTag, auth_tag_len);
1148
1149 /* Compare generated tag with passed in tag. */
fed28611 1150 retval = crypto_memneq(src + tempCipherLen, authTag, auth_tag_len) ?
0bd82f5f
TS
1151 -EBADMSG : 0;
1152
1153 if (one_entry_in_sg) {
1154 if (unlikely(req->src != req->dst)) {
8fd75e12 1155 scatterwalk_unmap(dst);
0bd82f5f
TS
1156 scatterwalk_done(&dst_sg_walk, 0, 0);
1157 }
8fd75e12
CW
1158 scatterwalk_unmap(src);
1159 scatterwalk_unmap(assoc);
0bd82f5f
TS
1160 scatterwalk_done(&src_sg_walk, 0, 0);
1161 scatterwalk_done(&assoc_sg_walk, 0, 0);
1162 } else {
1163 scatterwalk_map_and_copy(dst, req->dst, 0, req->cryptlen, 1);
1164 kfree(src);
1165 }
1166 return retval;
1167}
81e397d9
TS
1168
1169static int rfc4106_encrypt(struct aead_request *req)
1170{
1171 int ret;
1172 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1173 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
1174
1175 if (!irq_fpu_usable()) {
1176 struct aead_request *cryptd_req =
1177 (struct aead_request *) aead_request_ctx(req);
1178
1179 memcpy(cryptd_req, req, sizeof(*req));
1180 aead_request_set_tfm(cryptd_req, &ctx->cryptd_tfm->base);
1181 ret = crypto_aead_encrypt(cryptd_req);
1182 } else {
1183 kernel_fpu_begin();
1184 ret = __driver_rfc4106_encrypt(req);
1185 kernel_fpu_end();
1186 }
1187 return ret;
1188}
1189
1190static int rfc4106_decrypt(struct aead_request *req)
1191{
1192 int ret;
1193 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1194 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
1195
1196 if (!irq_fpu_usable()) {
1197 struct aead_request *cryptd_req =
1198 (struct aead_request *) aead_request_ctx(req);
1199
1200 memcpy(cryptd_req, req, sizeof(*req));
1201 aead_request_set_tfm(cryptd_req, &ctx->cryptd_tfm->base);
1202 ret = crypto_aead_decrypt(cryptd_req);
1203 } else {
1204 kernel_fpu_begin();
1205 ret = __driver_rfc4106_decrypt(req);
1206 kernel_fpu_end();
1207 }
1208 return ret;
1209}
1210
1211static int helper_rfc4106_encrypt(struct aead_request *req)
1212{
1213 int ret;
1214
1215 if (unlikely(!irq_fpu_usable())) {
1216 WARN_ONCE(1, "__gcm-aes-aesni alg used in invalid context");
1217 ret = -EINVAL;
1218 } else {
1219 kernel_fpu_begin();
1220 ret = __driver_rfc4106_encrypt(req);
1221 kernel_fpu_end();
1222 }
1223 return ret;
1224}
1225
1226static int helper_rfc4106_decrypt(struct aead_request *req)
1227{
1228 int ret;
1229
1230 if (unlikely(!irq_fpu_usable())) {
1231 WARN_ONCE(1, "__gcm-aes-aesni alg used in invalid context");
1232 ret = -EINVAL;
1233 } else {
1234 kernel_fpu_begin();
1235 ret = __driver_rfc4106_decrypt(req);
1236 kernel_fpu_end();
1237 }
1238 return ret;
1239}
fa46ccb8 1240#endif
0bd82f5f 1241
fa46ccb8
JK
1242static struct crypto_alg aesni_algs[] = { {
1243 .cra_name = "aes",
1244 .cra_driver_name = "aes-aesni",
1245 .cra_priority = 300,
1246 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
1247 .cra_blocksize = AES_BLOCK_SIZE,
1248 .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
1249 AESNI_ALIGN - 1,
1250 .cra_alignmask = 0,
1251 .cra_module = THIS_MODULE,
1252 .cra_u = {
1253 .cipher = {
1254 .cia_min_keysize = AES_MIN_KEY_SIZE,
1255 .cia_max_keysize = AES_MAX_KEY_SIZE,
1256 .cia_setkey = aes_set_key,
1257 .cia_encrypt = aes_encrypt,
1258 .cia_decrypt = aes_decrypt
1259 }
1260 }
1261}, {
1262 .cra_name = "__aes-aesni",
1263 .cra_driver_name = "__driver-aes-aesni",
1264 .cra_priority = 0,
1265 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
1266 .cra_blocksize = AES_BLOCK_SIZE,
1267 .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
1268 AESNI_ALIGN - 1,
1269 .cra_alignmask = 0,
1270 .cra_module = THIS_MODULE,
1271 .cra_u = {
1272 .cipher = {
1273 .cia_min_keysize = AES_MIN_KEY_SIZE,
1274 .cia_max_keysize = AES_MAX_KEY_SIZE,
1275 .cia_setkey = aes_set_key,
1276 .cia_encrypt = __aes_encrypt,
1277 .cia_decrypt = __aes_decrypt
1278 }
1279 }
1280}, {
1281 .cra_name = "__ecb-aes-aesni",
1282 .cra_driver_name = "__driver-ecb-aes-aesni",
1283 .cra_priority = 0,
1284 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
1285 .cra_blocksize = AES_BLOCK_SIZE,
1286 .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
1287 AESNI_ALIGN - 1,
1288 .cra_alignmask = 0,
1289 .cra_type = &crypto_blkcipher_type,
1290 .cra_module = THIS_MODULE,
1291 .cra_u = {
1292 .blkcipher = {
1293 .min_keysize = AES_MIN_KEY_SIZE,
1294 .max_keysize = AES_MAX_KEY_SIZE,
1295 .setkey = aes_set_key,
1296 .encrypt = ecb_encrypt,
1297 .decrypt = ecb_decrypt,
1298 },
1299 },
1300}, {
1301 .cra_name = "__cbc-aes-aesni",
1302 .cra_driver_name = "__driver-cbc-aes-aesni",
1303 .cra_priority = 0,
1304 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
1305 .cra_blocksize = AES_BLOCK_SIZE,
1306 .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
1307 AESNI_ALIGN - 1,
1308 .cra_alignmask = 0,
1309 .cra_type = &crypto_blkcipher_type,
1310 .cra_module = THIS_MODULE,
1311 .cra_u = {
1312 .blkcipher = {
1313 .min_keysize = AES_MIN_KEY_SIZE,
1314 .max_keysize = AES_MAX_KEY_SIZE,
1315 .setkey = aes_set_key,
1316 .encrypt = cbc_encrypt,
1317 .decrypt = cbc_decrypt,
1318 },
1319 },
1320}, {
1321 .cra_name = "ecb(aes)",
1322 .cra_driver_name = "ecb-aes-aesni",
1323 .cra_priority = 400,
1324 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1325 .cra_blocksize = AES_BLOCK_SIZE,
a9629d71 1326 .cra_ctxsize = sizeof(struct async_helper_ctx),
fa46ccb8
JK
1327 .cra_alignmask = 0,
1328 .cra_type = &crypto_ablkcipher_type,
1329 .cra_module = THIS_MODULE,
1330 .cra_init = ablk_ecb_init,
1331 .cra_exit = ablk_exit,
1332 .cra_u = {
1333 .ablkcipher = {
1334 .min_keysize = AES_MIN_KEY_SIZE,
1335 .max_keysize = AES_MAX_KEY_SIZE,
1336 .setkey = ablk_set_key,
1337 .encrypt = ablk_encrypt,
1338 .decrypt = ablk_decrypt,
1339 },
1340 },
1341}, {
1342 .cra_name = "cbc(aes)",
1343 .cra_driver_name = "cbc-aes-aesni",
1344 .cra_priority = 400,
1345 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1346 .cra_blocksize = AES_BLOCK_SIZE,
a9629d71 1347 .cra_ctxsize = sizeof(struct async_helper_ctx),
fa46ccb8
JK
1348 .cra_alignmask = 0,
1349 .cra_type = &crypto_ablkcipher_type,
1350 .cra_module = THIS_MODULE,
1351 .cra_init = ablk_cbc_init,
1352 .cra_exit = ablk_exit,
1353 .cra_u = {
1354 .ablkcipher = {
1355 .min_keysize = AES_MIN_KEY_SIZE,
1356 .max_keysize = AES_MAX_KEY_SIZE,
1357 .ivsize = AES_BLOCK_SIZE,
1358 .setkey = ablk_set_key,
1359 .encrypt = ablk_encrypt,
1360 .decrypt = ablk_decrypt,
1361 },
1362 },
1363#ifdef CONFIG_X86_64
1364}, {
1365 .cra_name = "__ctr-aes-aesni",
1366 .cra_driver_name = "__driver-ctr-aes-aesni",
1367 .cra_priority = 0,
1368 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
1369 .cra_blocksize = 1,
1370 .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
1371 AESNI_ALIGN - 1,
1372 .cra_alignmask = 0,
1373 .cra_type = &crypto_blkcipher_type,
1374 .cra_module = THIS_MODULE,
1375 .cra_u = {
1376 .blkcipher = {
1377 .min_keysize = AES_MIN_KEY_SIZE,
1378 .max_keysize = AES_MAX_KEY_SIZE,
1379 .ivsize = AES_BLOCK_SIZE,
1380 .setkey = aes_set_key,
1381 .encrypt = ctr_crypt,
1382 .decrypt = ctr_crypt,
1383 },
1384 },
1385}, {
1386 .cra_name = "ctr(aes)",
1387 .cra_driver_name = "ctr-aes-aesni",
1388 .cra_priority = 400,
1389 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1390 .cra_blocksize = 1,
a9629d71 1391 .cra_ctxsize = sizeof(struct async_helper_ctx),
fa46ccb8
JK
1392 .cra_alignmask = 0,
1393 .cra_type = &crypto_ablkcipher_type,
1394 .cra_module = THIS_MODULE,
1395 .cra_init = ablk_ctr_init,
1396 .cra_exit = ablk_exit,
1397 .cra_u = {
1398 .ablkcipher = {
1399 .min_keysize = AES_MIN_KEY_SIZE,
1400 .max_keysize = AES_MAX_KEY_SIZE,
1401 .ivsize = AES_BLOCK_SIZE,
1402 .setkey = ablk_set_key,
1403 .encrypt = ablk_encrypt,
1404 .decrypt = ablk_encrypt,
1405 .geniv = "chainiv",
1406 },
1407 },
1408}, {
1409 .cra_name = "__gcm-aes-aesni",
1410 .cra_driver_name = "__driver-gcm-aes-aesni",
0bd82f5f
TS
1411 .cra_priority = 0,
1412 .cra_flags = CRYPTO_ALG_TYPE_AEAD,
1413 .cra_blocksize = 1,
fa46ccb8
JK
1414 .cra_ctxsize = sizeof(struct aesni_rfc4106_gcm_ctx) +
1415 AESNI_ALIGN,
0bd82f5f
TS
1416 .cra_alignmask = 0,
1417 .cra_type = &crypto_aead_type,
1418 .cra_module = THIS_MODULE,
0bd82f5f
TS
1419 .cra_u = {
1420 .aead = {
81e397d9
TS
1421 .setkey = common_rfc4106_set_key,
1422 .setauthsize = common_rfc4106_set_authsize,
1423 .encrypt = helper_rfc4106_encrypt,
1424 .decrypt = helper_rfc4106_decrypt,
1425 .ivsize = 8,
1426 .maxauthsize = 16,
0bd82f5f
TS
1427 },
1428 },
fa46ccb8
JK
1429}, {
1430 .cra_name = "rfc4106(gcm(aes))",
1431 .cra_driver_name = "rfc4106-gcm-aesni",
1432 .cra_priority = 400,
1433 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1434 .cra_blocksize = 1,
1435 .cra_ctxsize = sizeof(struct aesni_rfc4106_gcm_ctx) +
1436 AESNI_ALIGN,
1437 .cra_alignmask = 0,
1438 .cra_type = &crypto_nivaead_type,
1439 .cra_module = THIS_MODULE,
1440 .cra_init = rfc4106_init,
1441 .cra_exit = rfc4106_exit,
1442 .cra_u = {
1443 .aead = {
1444 .setkey = rfc4106_set_key,
1445 .setauthsize = rfc4106_set_authsize,
1446 .encrypt = rfc4106_encrypt,
1447 .decrypt = rfc4106_decrypt,
1448 .geniv = "seqiv",
1449 .ivsize = 8,
1450 .maxauthsize = 16,
1451 },
1452 },
fa46ccb8 1453#endif
304576a7 1454#if IS_ENABLED(CONFIG_CRYPTO_PCBC)
fa46ccb8 1455}, {
023af608
JK
1456 .cra_name = "pcbc(aes)",
1457 .cra_driver_name = "pcbc-aes-aesni",
fa46ccb8
JK
1458 .cra_priority = 400,
1459 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1460 .cra_blocksize = AES_BLOCK_SIZE,
a9629d71 1461 .cra_ctxsize = sizeof(struct async_helper_ctx),
fa46ccb8
JK
1462 .cra_alignmask = 0,
1463 .cra_type = &crypto_ablkcipher_type,
1464 .cra_module = THIS_MODULE,
023af608 1465 .cra_init = ablk_pcbc_init,
fa46ccb8
JK
1466 .cra_exit = ablk_exit,
1467 .cra_u = {
1468 .ablkcipher = {
023af608
JK
1469 .min_keysize = AES_MIN_KEY_SIZE,
1470 .max_keysize = AES_MAX_KEY_SIZE,
fa46ccb8
JK
1471 .ivsize = AES_BLOCK_SIZE,
1472 .setkey = ablk_set_key,
1473 .encrypt = ablk_encrypt,
1474 .decrypt = ablk_decrypt,
1475 },
1476 },
1477#endif
fa46ccb8 1478}, {
023af608
JK
1479 .cra_name = "__lrw-aes-aesni",
1480 .cra_driver_name = "__driver-lrw-aes-aesni",
1481 .cra_priority = 0,
1482 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
1483 .cra_blocksize = AES_BLOCK_SIZE,
1484 .cra_ctxsize = sizeof(struct aesni_lrw_ctx),
1485 .cra_alignmask = 0,
1486 .cra_type = &crypto_blkcipher_type,
1487 .cra_module = THIS_MODULE,
1488 .cra_exit = lrw_aesni_exit_tfm,
1489 .cra_u = {
1490 .blkcipher = {
1491 .min_keysize = AES_MIN_KEY_SIZE + AES_BLOCK_SIZE,
1492 .max_keysize = AES_MAX_KEY_SIZE + AES_BLOCK_SIZE,
1493 .ivsize = AES_BLOCK_SIZE,
1494 .setkey = lrw_aesni_setkey,
1495 .encrypt = lrw_encrypt,
1496 .decrypt = lrw_decrypt,
1497 },
1498 },
1499}, {
1500 .cra_name = "__xts-aes-aesni",
1501 .cra_driver_name = "__driver-xts-aes-aesni",
1502 .cra_priority = 0,
1503 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
1504 .cra_blocksize = AES_BLOCK_SIZE,
1505 .cra_ctxsize = sizeof(struct aesni_xts_ctx),
1506 .cra_alignmask = 0,
1507 .cra_type = &crypto_blkcipher_type,
1508 .cra_module = THIS_MODULE,
1509 .cra_u = {
1510 .blkcipher = {
1511 .min_keysize = 2 * AES_MIN_KEY_SIZE,
1512 .max_keysize = 2 * AES_MAX_KEY_SIZE,
1513 .ivsize = AES_BLOCK_SIZE,
1514 .setkey = xts_aesni_setkey,
1515 .encrypt = xts_encrypt,
1516 .decrypt = xts_decrypt,
1517 },
1518 },
1519}, {
1520 .cra_name = "lrw(aes)",
1521 .cra_driver_name = "lrw-aes-aesni",
fa46ccb8
JK
1522 .cra_priority = 400,
1523 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1524 .cra_blocksize = AES_BLOCK_SIZE,
a9629d71 1525 .cra_ctxsize = sizeof(struct async_helper_ctx),
fa46ccb8
JK
1526 .cra_alignmask = 0,
1527 .cra_type = &crypto_ablkcipher_type,
1528 .cra_module = THIS_MODULE,
023af608 1529 .cra_init = ablk_init,
fa46ccb8
JK
1530 .cra_exit = ablk_exit,
1531 .cra_u = {
1532 .ablkcipher = {
023af608
JK
1533 .min_keysize = AES_MIN_KEY_SIZE + AES_BLOCK_SIZE,
1534 .max_keysize = AES_MAX_KEY_SIZE + AES_BLOCK_SIZE,
fa46ccb8
JK
1535 .ivsize = AES_BLOCK_SIZE,
1536 .setkey = ablk_set_key,
1537 .encrypt = ablk_encrypt,
1538 .decrypt = ablk_decrypt,
1539 },
1540 },
fa46ccb8
JK
1541}, {
1542 .cra_name = "xts(aes)",
1543 .cra_driver_name = "xts-aes-aesni",
1544 .cra_priority = 400,
1545 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1546 .cra_blocksize = AES_BLOCK_SIZE,
a9629d71 1547 .cra_ctxsize = sizeof(struct async_helper_ctx),
fa46ccb8
JK
1548 .cra_alignmask = 0,
1549 .cra_type = &crypto_ablkcipher_type,
1550 .cra_module = THIS_MODULE,
023af608 1551 .cra_init = ablk_init,
fa46ccb8
JK
1552 .cra_exit = ablk_exit,
1553 .cra_u = {
1554 .ablkcipher = {
1555 .min_keysize = 2 * AES_MIN_KEY_SIZE,
1556 .max_keysize = 2 * AES_MAX_KEY_SIZE,
1557 .ivsize = AES_BLOCK_SIZE,
1558 .setkey = ablk_set_key,
1559 .encrypt = ablk_encrypt,
1560 .decrypt = ablk_decrypt,
1561 },
1562 },
fa46ccb8 1563} };
0bd82f5f 1564
3bd391f0
AK
1565
1566static const struct x86_cpu_id aesni_cpu_id[] = {
1567 X86_FEATURE_MATCH(X86_FEATURE_AES),
1568 {}
1569};
1570MODULE_DEVICE_TABLE(x86cpu, aesni_cpu_id);
1571
54b6a1bd
HY
1572static int __init aesni_init(void)
1573{
7af6c245 1574 int err;
54b6a1bd 1575
3bd391f0 1576 if (!x86_match_cpu(aesni_cpu_id))
54b6a1bd 1577 return -ENODEV;
8610d7bf 1578#ifdef CONFIG_X86_64
d764593a
TC
1579#ifdef CONFIG_AS_AVX2
1580 if (boot_cpu_has(X86_FEATURE_AVX2)) {
1581 pr_info("AVX2 version of gcm_enc/dec engaged.\n");
1582 aesni_gcm_enc_tfm = aesni_gcm_enc_avx2;
1583 aesni_gcm_dec_tfm = aesni_gcm_dec_avx2;
1584 } else
1585#endif
1586#ifdef CONFIG_AS_AVX
1587 if (boot_cpu_has(X86_FEATURE_AVX)) {
1588 pr_info("AVX version of gcm_enc/dec engaged.\n");
1589 aesni_gcm_enc_tfm = aesni_gcm_enc_avx;
1590 aesni_gcm_dec_tfm = aesni_gcm_dec_avx;
1591 } else
1592#endif
1593 {
1594 pr_info("SSE version of gcm_enc/dec engaged.\n");
1595 aesni_gcm_enc_tfm = aesni_gcm_enc;
1596 aesni_gcm_dec_tfm = aesni_gcm_dec;
1597 }
22cddcc7 1598 aesni_ctr_enc_tfm = aesni_ctr_enc;
5cfed7b3 1599#ifdef CONFIG_AS_AVX
22cddcc7 1600 if (cpu_has_avx) {
1601 /* optimize performance of ctr mode encryption transform */
1602 aesni_ctr_enc_tfm = aesni_ctr_enc_avx_tfm;
1603 pr_info("AES CTR mode by8 optimization enabled\n");
1604 }
1605#endif
8610d7bf 1606#endif
0bd82f5f 1607
fa46ccb8
JK
1608 err = crypto_fpu_init();
1609 if (err)
1610 return err;
54b6a1bd 1611
fa46ccb8 1612 return crypto_register_algs(aesni_algs, ARRAY_SIZE(aesni_algs));
54b6a1bd
HY
1613}
1614
1615static void __exit aesni_exit(void)
1616{
fa46ccb8 1617 crypto_unregister_algs(aesni_algs, ARRAY_SIZE(aesni_algs));
b23b6451
AL
1618
1619 crypto_fpu_exit();
54b6a1bd
HY
1620}
1621
1622module_init(aesni_init);
1623module_exit(aesni_exit);
1624
1625MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm, Intel AES-NI instructions optimized");
1626MODULE_LICENSE("GPL");
5d26a105 1627MODULE_ALIAS_CRYPTO("aes");
This page took 0.297941 seconds and 5 git commands to generate.