1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Support for Intel AES-NI instructions. This file contains glue
4 * code, the real AES implementation is in intel-aes_asm.S.
6 * Copyright (C) 2008, Intel Corp.
7 * Author: Huang Ying <ying.huang@intel.com>
9 * Added RFC4106 AES-GCM support for 128-bit keys under the AEAD
10 * interface for 64-bit kernels.
11 * Authors: Adrian Hoban <adrian.hoban@intel.com>
12 * Gabriele Paoloni <gabriele.paoloni@intel.com>
13 * Tadeusz Struk (tadeusz.struk@intel.com)
14 * Aidan O'Mahony (aidan.o.mahony@intel.com)
15 * Copyright (c) 2010, Intel Corporation.
18 #include <linux/hardirq.h>
19 #include <linux/types.h>
20 #include <linux/module.h>
21 #include <linux/err.h>
22 #include <crypto/algapi.h>
23 #include <crypto/aes.h>
24 #include <crypto/ctr.h>
25 #include <crypto/b128ops.h>
26 #include <crypto/gcm.h>
27 #include <crypto/xts.h>
28 #include <asm/cpu_device_id.h>
30 #include <crypto/scatterwalk.h>
31 #include <crypto/internal/aead.h>
32 #include <crypto/internal/simd.h>
33 #include <crypto/internal/skcipher.h>
34 #include <linux/workqueue.h>
35 #include <linux/spinlock.h>
37 #include <asm/crypto/glue_helper.h>
41 #define AESNI_ALIGN 16
42 #define AESNI_ALIGN_ATTR __attribute__ ((__aligned__(AESNI_ALIGN)))
43 #define AES_BLOCK_MASK (~(AES_BLOCK_SIZE - 1))
44 #define RFC4106_HASH_SUBKEY_SIZE 16
45 #define AESNI_ALIGN_EXTRA ((AESNI_ALIGN - 1) & ~(CRYPTO_MINALIGN - 1))
46 #define CRYPTO_AES_CTX_SIZE (sizeof(struct crypto_aes_ctx) + AESNI_ALIGN_EXTRA)
47 #define XTS_AES_CTX_SIZE (sizeof(struct aesni_xts_ctx) + AESNI_ALIGN_EXTRA)
49 /* This data is stored at the end of the crypto_tfm struct.
50 * It's a type of per "session" data storage location.
51 * This needs to be 16 byte aligned.
53 struct aesni_rfc4106_gcm_ctx {
54 u8 hash_subkey[16] AESNI_ALIGN_ATTR;
55 struct crypto_aes_ctx aes_key_expanded AESNI_ALIGN_ATTR;
59 struct generic_gcmaes_ctx {
60 u8 hash_subkey[16] AESNI_ALIGN_ATTR;
61 struct crypto_aes_ctx aes_key_expanded AESNI_ALIGN_ATTR;
64 struct aesni_xts_ctx {
65 u8 raw_tweak_ctx[sizeof(struct crypto_aes_ctx)] AESNI_ALIGN_ATTR;
66 u8 raw_crypt_ctx[sizeof(struct crypto_aes_ctx)] AESNI_ALIGN_ATTR;
69 #define GCM_BLOCK_LEN 16
71 struct gcm_context_data {
72 /* init, update and finalize context data */
73 u8 aad_hash[GCM_BLOCK_LEN];
76 u8 partial_block_enc_key[GCM_BLOCK_LEN];
77 u8 orig_IV[GCM_BLOCK_LEN];
78 u8 current_counter[GCM_BLOCK_LEN];
79 u64 partial_block_len;
81 u8 hash_keys[GCM_BLOCK_LEN * 16];
84 asmlinkage int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key,
85 unsigned int key_len);
86 asmlinkage void aesni_enc(const void *ctx, u8 *out, const u8 *in);
87 asmlinkage void aesni_dec(const void *ctx, u8 *out, const u8 *in);
88 asmlinkage void aesni_ecb_enc(struct crypto_aes_ctx *ctx, u8 *out,
89 const u8 *in, unsigned int len);
90 asmlinkage void aesni_ecb_dec(struct crypto_aes_ctx *ctx, u8 *out,
91 const u8 *in, unsigned int len);
92 asmlinkage void aesni_cbc_enc(struct crypto_aes_ctx *ctx, u8 *out,
93 const u8 *in, unsigned int len, u8 *iv);
94 asmlinkage void aesni_cbc_dec(struct crypto_aes_ctx *ctx, u8 *out,
95 const u8 *in, unsigned int len, u8 *iv);
96 asmlinkage void aesni_cts_cbc_enc(struct crypto_aes_ctx *ctx, u8 *out,
97 const u8 *in, unsigned int len, u8 *iv);
98 asmlinkage void aesni_cts_cbc_dec(struct crypto_aes_ctx *ctx, u8 *out,
99 const u8 *in, unsigned int len, u8 *iv);
101 #define AVX_GEN2_OPTSIZE 640
102 #define AVX_GEN4_OPTSIZE 4096
106 static void (*aesni_ctr_enc_tfm)(struct crypto_aes_ctx *ctx, u8 *out,
107 const u8 *in, unsigned int len, u8 *iv);
108 asmlinkage void aesni_ctr_enc(struct crypto_aes_ctx *ctx, u8 *out,
109 const u8 *in, unsigned int len, u8 *iv);
111 asmlinkage void aesni_xts_crypt8(const struct crypto_aes_ctx *ctx, u8 *out,
112 const u8 *in, bool enc, le128 *iv);
114 /* asmlinkage void aesni_gcm_enc()
115 * void *ctx, AES Key schedule. Starts on a 16 byte boundary.
116 * struct gcm_context_data. May be uninitialized.
117 * u8 *out, Ciphertext output. Encrypt in-place is allowed.
118 * const u8 *in, Plaintext input
119 * unsigned long plaintext_len, Length of data in bytes for encryption.
120 * u8 *iv, Pre-counter block j0: 12 byte IV concatenated with 0x00000001.
121 * 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.
125 * u8 *auth_tag, Authenticated Tag output.
126 * unsigned long auth_tag_len), Authenticated Tag Length in bytes.
127 * Valid values are 16 (most likely), 12 or 8.
129 asmlinkage void aesni_gcm_enc(void *ctx,
130 struct gcm_context_data *gdata, 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);
135 /* asmlinkage void aesni_gcm_dec()
136 * void *ctx, AES Key schedule. Starts on a 16 byte boundary.
137 * struct gcm_context_data. May be uninitialized.
138 * u8 *out, Plaintext output. Decrypt in-place is allowed.
139 * const u8 *in, Ciphertext input
140 * unsigned long ciphertext_len, Length of data in bytes for decryption.
141 * u8 *iv, Pre-counter block j0: 12 byte IV concatenated with 0x00000001.
142 * 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.
151 asmlinkage void aesni_gcm_dec(void *ctx,
152 struct gcm_context_data *gdata, u8 *out,
153 const u8 *in, unsigned long ciphertext_len, u8 *iv,
154 u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
155 u8 *auth_tag, unsigned long auth_tag_len);
157 /* Scatter / Gather routines, with args similar to above */
158 asmlinkage void aesni_gcm_init(void *ctx,
159 struct gcm_context_data *gdata,
161 u8 *hash_subkey, const u8 *aad,
162 unsigned long aad_len);
163 asmlinkage void aesni_gcm_enc_update(void *ctx,
164 struct gcm_context_data *gdata, u8 *out,
165 const u8 *in, unsigned long plaintext_len);
166 asmlinkage void aesni_gcm_dec_update(void *ctx,
167 struct gcm_context_data *gdata, u8 *out,
169 unsigned long ciphertext_len);
170 asmlinkage void aesni_gcm_finalize(void *ctx,
171 struct gcm_context_data *gdata,
172 u8 *auth_tag, unsigned long auth_tag_len);
174 static const struct aesni_gcm_tfm_s {
175 void (*init)(void *ctx, struct gcm_context_data *gdata, u8 *iv,
176 u8 *hash_subkey, const u8 *aad, unsigned long aad_len);
177 void (*enc_update)(void *ctx, struct gcm_context_data *gdata, u8 *out,
178 const u8 *in, unsigned long plaintext_len);
179 void (*dec_update)(void *ctx, struct gcm_context_data *gdata, u8 *out,
180 const u8 *in, unsigned long ciphertext_len);
181 void (*finalize)(void *ctx, struct gcm_context_data *gdata,
182 u8 *auth_tag, unsigned long auth_tag_len);
185 static const struct aesni_gcm_tfm_s aesni_gcm_tfm_sse = {
186 .init = &aesni_gcm_init,
187 .enc_update = &aesni_gcm_enc_update,
188 .dec_update = &aesni_gcm_dec_update,
189 .finalize = &aesni_gcm_finalize,
192 asmlinkage void aes_ctr_enc_128_avx_by8(const u8 *in, u8 *iv,
193 void *keys, u8 *out, unsigned int num_bytes);
194 asmlinkage void aes_ctr_enc_192_avx_by8(const u8 *in, u8 *iv,
195 void *keys, u8 *out, unsigned int num_bytes);
196 asmlinkage void aes_ctr_enc_256_avx_by8(const u8 *in, u8 *iv,
197 void *keys, u8 *out, unsigned int num_bytes);
199 * asmlinkage void aesni_gcm_init_avx_gen2()
200 * gcm_data *my_ctx_data, context data
201 * u8 *hash_subkey, the Hash sub key input. Data starts on a 16-byte boundary.
203 asmlinkage void aesni_gcm_init_avx_gen2(void *my_ctx_data,
204 struct gcm_context_data *gdata,
208 unsigned long aad_len);
210 asmlinkage void aesni_gcm_enc_update_avx_gen2(void *ctx,
211 struct gcm_context_data *gdata, u8 *out,
212 const u8 *in, unsigned long plaintext_len);
213 asmlinkage void aesni_gcm_dec_update_avx_gen2(void *ctx,
214 struct gcm_context_data *gdata, u8 *out,
216 unsigned long ciphertext_len);
217 asmlinkage void aesni_gcm_finalize_avx_gen2(void *ctx,
218 struct gcm_context_data *gdata,
219 u8 *auth_tag, unsigned long auth_tag_len);
221 asmlinkage void aesni_gcm_enc_avx_gen2(void *ctx,
222 struct gcm_context_data *gdata, 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);
227 asmlinkage void aesni_gcm_dec_avx_gen2(void *ctx,
228 struct gcm_context_data *gdata, u8 *out,
229 const u8 *in, unsigned long ciphertext_len, u8 *iv,
230 const u8 *aad, unsigned long aad_len,
231 u8 *auth_tag, unsigned long auth_tag_len);
233 static const struct aesni_gcm_tfm_s aesni_gcm_tfm_avx_gen2 = {
234 .init = &aesni_gcm_init_avx_gen2,
235 .enc_update = &aesni_gcm_enc_update_avx_gen2,
236 .dec_update = &aesni_gcm_dec_update_avx_gen2,
237 .finalize = &aesni_gcm_finalize_avx_gen2,
241 * asmlinkage void aesni_gcm_init_avx_gen4()
242 * gcm_data *my_ctx_data, context data
243 * u8 *hash_subkey, the Hash sub key input. Data starts on a 16-byte boundary.
245 asmlinkage void aesni_gcm_init_avx_gen4(void *my_ctx_data,
246 struct gcm_context_data *gdata,
250 unsigned long aad_len);
252 asmlinkage void aesni_gcm_enc_update_avx_gen4(void *ctx,
253 struct gcm_context_data *gdata, u8 *out,
254 const u8 *in, unsigned long plaintext_len);
255 asmlinkage void aesni_gcm_dec_update_avx_gen4(void *ctx,
256 struct gcm_context_data *gdata, u8 *out,
258 unsigned long ciphertext_len);
259 asmlinkage void aesni_gcm_finalize_avx_gen4(void *ctx,
260 struct gcm_context_data *gdata,
261 u8 *auth_tag, unsigned long auth_tag_len);
263 asmlinkage void aesni_gcm_enc_avx_gen4(void *ctx,
264 struct gcm_context_data *gdata, u8 *out,
265 const u8 *in, unsigned long plaintext_len, u8 *iv,
266 const u8 *aad, unsigned long aad_len,
267 u8 *auth_tag, unsigned long auth_tag_len);
269 asmlinkage void aesni_gcm_dec_avx_gen4(void *ctx,
270 struct gcm_context_data *gdata, u8 *out,
271 const u8 *in, unsigned long ciphertext_len, u8 *iv,
272 const u8 *aad, unsigned long aad_len,
273 u8 *auth_tag, unsigned long auth_tag_len);
275 static const struct aesni_gcm_tfm_s aesni_gcm_tfm_avx_gen4 = {
276 .init = &aesni_gcm_init_avx_gen4,
277 .enc_update = &aesni_gcm_enc_update_avx_gen4,
278 .dec_update = &aesni_gcm_dec_update_avx_gen4,
279 .finalize = &aesni_gcm_finalize_avx_gen4,
283 aesni_rfc4106_gcm_ctx *aesni_rfc4106_gcm_ctx_get(struct crypto_aead *tfm)
285 unsigned long align = AESNI_ALIGN;
287 if (align <= crypto_tfm_ctx_alignment())
289 return PTR_ALIGN(crypto_aead_ctx(tfm), align);
293 generic_gcmaes_ctx *generic_gcmaes_ctx_get(struct crypto_aead *tfm)
295 unsigned long align = AESNI_ALIGN;
297 if (align <= crypto_tfm_ctx_alignment())
299 return PTR_ALIGN(crypto_aead_ctx(tfm), align);
303 static inline struct crypto_aes_ctx *aes_ctx(void *raw_ctx)
305 unsigned long addr = (unsigned long)raw_ctx;
306 unsigned long align = AESNI_ALIGN;
308 if (align <= crypto_tfm_ctx_alignment())
310 return (struct crypto_aes_ctx *)ALIGN(addr, align);
313 static int aes_set_key_common(struct crypto_tfm *tfm, void *raw_ctx,
314 const u8 *in_key, unsigned int key_len)
316 struct crypto_aes_ctx *ctx = aes_ctx(raw_ctx);
319 if (key_len != AES_KEYSIZE_128 && key_len != AES_KEYSIZE_192 &&
320 key_len != AES_KEYSIZE_256)
323 if (!crypto_simd_usable())
324 err = aes_expandkey(ctx, in_key, key_len);
327 err = aesni_set_key(ctx, in_key, key_len);
334 static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
335 unsigned int key_len)
337 return aes_set_key_common(tfm, crypto_tfm_ctx(tfm), in_key, key_len);
340 static void aesni_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
342 struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
344 if (!crypto_simd_usable()) {
345 aes_encrypt(ctx, dst, src);
348 aesni_enc(ctx, dst, src);
353 static void aesni_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
355 struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
357 if (!crypto_simd_usable()) {
358 aes_decrypt(ctx, dst, src);
361 aesni_dec(ctx, dst, src);
366 static int aesni_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
369 return aes_set_key_common(crypto_skcipher_tfm(tfm),
370 crypto_skcipher_ctx(tfm), key, len);
373 static int ecb_encrypt(struct skcipher_request *req)
375 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
376 struct crypto_aes_ctx *ctx = aes_ctx(crypto_skcipher_ctx(tfm));
377 struct skcipher_walk walk;
381 err = skcipher_walk_virt(&walk, req, true);
384 while ((nbytes = walk.nbytes)) {
385 aesni_ecb_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
386 nbytes & AES_BLOCK_MASK);
387 nbytes &= AES_BLOCK_SIZE - 1;
388 err = skcipher_walk_done(&walk, nbytes);
395 static int ecb_decrypt(struct skcipher_request *req)
397 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
398 struct crypto_aes_ctx *ctx = aes_ctx(crypto_skcipher_ctx(tfm));
399 struct skcipher_walk walk;
403 err = skcipher_walk_virt(&walk, req, true);
406 while ((nbytes = walk.nbytes)) {
407 aesni_ecb_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
408 nbytes & AES_BLOCK_MASK);
409 nbytes &= AES_BLOCK_SIZE - 1;
410 err = skcipher_walk_done(&walk, nbytes);
417 static int cbc_encrypt(struct skcipher_request *req)
419 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
420 struct crypto_aes_ctx *ctx = aes_ctx(crypto_skcipher_ctx(tfm));
421 struct skcipher_walk walk;
425 err = skcipher_walk_virt(&walk, req, true);
428 while ((nbytes = walk.nbytes)) {
429 aesni_cbc_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
430 nbytes & AES_BLOCK_MASK, walk.iv);
431 nbytes &= AES_BLOCK_SIZE - 1;
432 err = skcipher_walk_done(&walk, nbytes);
439 static int cbc_decrypt(struct skcipher_request *req)
441 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
442 struct crypto_aes_ctx *ctx = aes_ctx(crypto_skcipher_ctx(tfm));
443 struct skcipher_walk walk;
447 err = skcipher_walk_virt(&walk, req, true);
450 while ((nbytes = walk.nbytes)) {
451 aesni_cbc_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
452 nbytes & AES_BLOCK_MASK, walk.iv);
453 nbytes &= AES_BLOCK_SIZE - 1;
454 err = skcipher_walk_done(&walk, nbytes);
461 static int cts_cbc_encrypt(struct skcipher_request *req)
463 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
464 struct crypto_aes_ctx *ctx = aes_ctx(crypto_skcipher_ctx(tfm));
465 int cbc_blocks = DIV_ROUND_UP(req->cryptlen, AES_BLOCK_SIZE) - 2;
466 struct scatterlist *src = req->src, *dst = req->dst;
467 struct scatterlist sg_src[2], sg_dst[2];
468 struct skcipher_request subreq;
469 struct skcipher_walk walk;
472 skcipher_request_set_tfm(&subreq, tfm);
473 skcipher_request_set_callback(&subreq, skcipher_request_flags(req),
476 if (req->cryptlen <= AES_BLOCK_SIZE) {
477 if (req->cryptlen < AES_BLOCK_SIZE)
482 if (cbc_blocks > 0) {
483 skcipher_request_set_crypt(&subreq, req->src, req->dst,
484 cbc_blocks * AES_BLOCK_SIZE,
487 err = cbc_encrypt(&subreq);
491 if (req->cryptlen == AES_BLOCK_SIZE)
494 dst = src = scatterwalk_ffwd(sg_src, req->src, subreq.cryptlen);
495 if (req->dst != req->src)
496 dst = scatterwalk_ffwd(sg_dst, req->dst,
500 /* handle ciphertext stealing */
501 skcipher_request_set_crypt(&subreq, src, dst,
502 req->cryptlen - cbc_blocks * AES_BLOCK_SIZE,
505 err = skcipher_walk_virt(&walk, &subreq, false);
510 aesni_cts_cbc_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
511 walk.nbytes, walk.iv);
514 return skcipher_walk_done(&walk, 0);
517 static int cts_cbc_decrypt(struct skcipher_request *req)
519 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
520 struct crypto_aes_ctx *ctx = aes_ctx(crypto_skcipher_ctx(tfm));
521 int cbc_blocks = DIV_ROUND_UP(req->cryptlen, AES_BLOCK_SIZE) - 2;
522 struct scatterlist *src = req->src, *dst = req->dst;
523 struct scatterlist sg_src[2], sg_dst[2];
524 struct skcipher_request subreq;
525 struct skcipher_walk walk;
528 skcipher_request_set_tfm(&subreq, tfm);
529 skcipher_request_set_callback(&subreq, skcipher_request_flags(req),
532 if (req->cryptlen <= AES_BLOCK_SIZE) {
533 if (req->cryptlen < AES_BLOCK_SIZE)
538 if (cbc_blocks > 0) {
539 skcipher_request_set_crypt(&subreq, req->src, req->dst,
540 cbc_blocks * AES_BLOCK_SIZE,
543 err = cbc_decrypt(&subreq);
547 if (req->cryptlen == AES_BLOCK_SIZE)
550 dst = src = scatterwalk_ffwd(sg_src, req->src, subreq.cryptlen);
551 if (req->dst != req->src)
552 dst = scatterwalk_ffwd(sg_dst, req->dst,
556 /* handle ciphertext stealing */
557 skcipher_request_set_crypt(&subreq, src, dst,
558 req->cryptlen - cbc_blocks * AES_BLOCK_SIZE,
561 err = skcipher_walk_virt(&walk, &subreq, false);
566 aesni_cts_cbc_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
567 walk.nbytes, walk.iv);
570 return skcipher_walk_done(&walk, 0);
574 static void ctr_crypt_final(struct crypto_aes_ctx *ctx,
575 struct skcipher_walk *walk)
577 u8 *ctrblk = walk->iv;
578 u8 keystream[AES_BLOCK_SIZE];
579 u8 *src = walk->src.virt.addr;
580 u8 *dst = walk->dst.virt.addr;
581 unsigned int nbytes = walk->nbytes;
583 aesni_enc(ctx, keystream, ctrblk);
584 crypto_xor_cpy(dst, keystream, src, nbytes);
586 crypto_inc(ctrblk, AES_BLOCK_SIZE);
589 static void aesni_ctr_enc_avx_tfm(struct crypto_aes_ctx *ctx, u8 *out,
590 const u8 *in, unsigned int len, u8 *iv)
593 * based on key length, override with the by8 version
594 * of ctr mode encryption/decryption for improved performance
595 * aes_set_key_common() ensures that key length is one of
598 if (ctx->key_length == AES_KEYSIZE_128)
599 aes_ctr_enc_128_avx_by8(in, iv, (void *)ctx, out, len);
600 else if (ctx->key_length == AES_KEYSIZE_192)
601 aes_ctr_enc_192_avx_by8(in, iv, (void *)ctx, out, len);
603 aes_ctr_enc_256_avx_by8(in, iv, (void *)ctx, out, len);
606 static int ctr_crypt(struct skcipher_request *req)
608 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
609 struct crypto_aes_ctx *ctx = aes_ctx(crypto_skcipher_ctx(tfm));
610 struct skcipher_walk walk;
614 err = skcipher_walk_virt(&walk, req, true);
617 while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
618 aesni_ctr_enc_tfm(ctx, walk.dst.virt.addr, walk.src.virt.addr,
619 nbytes & AES_BLOCK_MASK, walk.iv);
620 nbytes &= AES_BLOCK_SIZE - 1;
621 err = skcipher_walk_done(&walk, nbytes);
624 ctr_crypt_final(ctx, &walk);
625 err = skcipher_walk_done(&walk, 0);
632 static int xts_aesni_setkey(struct crypto_skcipher *tfm, const u8 *key,
635 struct aesni_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
638 err = xts_verify_key(tfm, key, keylen);
644 /* first half of xts-key is for crypt */
645 err = aes_set_key_common(crypto_skcipher_tfm(tfm), ctx->raw_crypt_ctx,
650 /* second half of xts-key is for tweak */
651 return aes_set_key_common(crypto_skcipher_tfm(tfm), ctx->raw_tweak_ctx,
652 key + keylen, keylen);
656 static void aesni_xts_enc(const void *ctx, u8 *dst, const u8 *src, le128 *iv)
658 glue_xts_crypt_128bit_one(ctx, dst, src, iv, aesni_enc);
661 static void aesni_xts_dec(const void *ctx, u8 *dst, const u8 *src, le128 *iv)
663 glue_xts_crypt_128bit_one(ctx, dst, src, iv, aesni_dec);
666 static void aesni_xts_enc8(const void *ctx, u8 *dst, const u8 *src, le128 *iv)
668 aesni_xts_crypt8(ctx, dst, src, true, iv);
671 static void aesni_xts_dec8(const void *ctx, u8 *dst, const u8 *src, le128 *iv)
673 aesni_xts_crypt8(ctx, dst, src, false, iv);
676 static const struct common_glue_ctx aesni_enc_xts = {
678 .fpu_blocks_limit = 1,
682 .fn_u = { .xts = aesni_xts_enc8 }
685 .fn_u = { .xts = aesni_xts_enc }
689 static const struct common_glue_ctx aesni_dec_xts = {
691 .fpu_blocks_limit = 1,
695 .fn_u = { .xts = aesni_xts_dec8 }
698 .fn_u = { .xts = aesni_xts_dec }
702 static int xts_encrypt(struct skcipher_request *req)
704 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
705 struct aesni_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
707 return glue_xts_req_128bit(&aesni_enc_xts, req, aesni_enc,
708 aes_ctx(ctx->raw_tweak_ctx),
709 aes_ctx(ctx->raw_crypt_ctx),
713 static int xts_decrypt(struct skcipher_request *req)
715 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
716 struct aesni_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
718 return glue_xts_req_128bit(&aesni_dec_xts, req, aesni_enc,
719 aes_ctx(ctx->raw_tweak_ctx),
720 aes_ctx(ctx->raw_crypt_ctx),
725 rfc4106_set_hash_subkey(u8 *hash_subkey, const u8 *key, unsigned int key_len)
727 struct crypto_aes_ctx ctx;
730 ret = aes_expandkey(&ctx, key, key_len);
734 /* Clear the data in the hash sub key container to zero.*/
735 /* We want to cipher all zeros to create the hash sub key. */
736 memset(hash_subkey, 0, RFC4106_HASH_SUBKEY_SIZE);
738 aes_encrypt(&ctx, hash_subkey, hash_subkey);
740 memzero_explicit(&ctx, sizeof(ctx));
744 static int common_rfc4106_set_key(struct crypto_aead *aead, const u8 *key,
745 unsigned int key_len)
747 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(aead);
752 /*Account for 4 byte nonce at the end.*/
755 memcpy(ctx->nonce, key + key_len, sizeof(ctx->nonce));
757 return aes_set_key_common(crypto_aead_tfm(aead),
758 &ctx->aes_key_expanded, key, key_len) ?:
759 rfc4106_set_hash_subkey(ctx->hash_subkey, key, key_len);
762 /* This is the Integrity Check Value (aka the authentication tag) length and can
763 * be 8, 12 or 16 bytes long. */
764 static int common_rfc4106_set_authsize(struct crypto_aead *aead,
765 unsigned int authsize)
779 static int generic_gcmaes_set_authsize(struct crypto_aead *tfm,
780 unsigned int authsize)
798 static int gcmaes_crypt_by_sg(bool enc, struct aead_request *req,
799 unsigned int assoclen, u8 *hash_subkey,
800 u8 *iv, void *aes_ctx)
802 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
803 unsigned long auth_tag_len = crypto_aead_authsize(tfm);
804 const struct aesni_gcm_tfm_s *gcm_tfm = aesni_gcm_tfm;
805 struct gcm_context_data data AESNI_ALIGN_ATTR;
806 struct scatter_walk dst_sg_walk = {};
807 unsigned long left = req->cryptlen;
808 unsigned long len, srclen, dstlen;
809 struct scatter_walk assoc_sg_walk;
810 struct scatter_walk src_sg_walk;
811 struct scatterlist src_start[2];
812 struct scatterlist dst_start[2];
813 struct scatterlist *src_sg;
814 struct scatterlist *dst_sg;
815 u8 *src, *dst, *assoc;
820 left -= auth_tag_len;
822 if (left < AVX_GEN4_OPTSIZE && gcm_tfm == &aesni_gcm_tfm_avx_gen4)
823 gcm_tfm = &aesni_gcm_tfm_avx_gen2;
824 if (left < AVX_GEN2_OPTSIZE && gcm_tfm == &aesni_gcm_tfm_avx_gen2)
825 gcm_tfm = &aesni_gcm_tfm_sse;
827 /* Linearize assoc, if not already linear */
828 if (req->src->length >= assoclen && req->src->length &&
829 (!PageHighMem(sg_page(req->src)) ||
830 req->src->offset + req->src->length <= PAGE_SIZE)) {
831 scatterwalk_start(&assoc_sg_walk, req->src);
832 assoc = scatterwalk_map(&assoc_sg_walk);
834 /* assoc can be any length, so must be on heap */
835 assocmem = kmalloc(assoclen, GFP_ATOMIC);
836 if (unlikely(!assocmem))
840 scatterwalk_map_and_copy(assoc, req->src, 0, assoclen, 0);
844 src_sg = scatterwalk_ffwd(src_start, req->src, req->assoclen);
845 scatterwalk_start(&src_sg_walk, src_sg);
846 if (req->src != req->dst) {
847 dst_sg = scatterwalk_ffwd(dst_start, req->dst,
849 scatterwalk_start(&dst_sg_walk, dst_sg);
854 gcm_tfm->init(aes_ctx, &data, iv,
855 hash_subkey, assoc, assoclen);
856 if (req->src != req->dst) {
858 src = scatterwalk_map(&src_sg_walk);
859 dst = scatterwalk_map(&dst_sg_walk);
860 srclen = scatterwalk_clamp(&src_sg_walk, left);
861 dstlen = scatterwalk_clamp(&dst_sg_walk, left);
862 len = min(srclen, dstlen);
865 gcm_tfm->enc_update(aes_ctx, &data,
868 gcm_tfm->dec_update(aes_ctx, &data,
873 scatterwalk_unmap(src);
874 scatterwalk_unmap(dst);
875 scatterwalk_advance(&src_sg_walk, len);
876 scatterwalk_advance(&dst_sg_walk, len);
877 scatterwalk_done(&src_sg_walk, 0, left);
878 scatterwalk_done(&dst_sg_walk, 1, left);
882 dst = src = scatterwalk_map(&src_sg_walk);
883 len = scatterwalk_clamp(&src_sg_walk, left);
886 gcm_tfm->enc_update(aes_ctx, &data,
889 gcm_tfm->dec_update(aes_ctx, &data,
893 scatterwalk_unmap(src);
894 scatterwalk_advance(&src_sg_walk, len);
895 scatterwalk_done(&src_sg_walk, 1, left);
898 gcm_tfm->finalize(aes_ctx, &data, authTag, auth_tag_len);
902 scatterwalk_unmap(assoc);
909 /* Copy out original authTag */
910 scatterwalk_map_and_copy(authTagMsg, req->src,
911 req->assoclen + req->cryptlen -
915 /* Compare generated tag with passed in tag. */
916 return crypto_memneq(authTagMsg, authTag, auth_tag_len) ?
920 /* Copy in the authTag */
921 scatterwalk_map_and_copy(authTag, req->dst,
922 req->assoclen + req->cryptlen,
928 static int gcmaes_encrypt(struct aead_request *req, unsigned int assoclen,
929 u8 *hash_subkey, u8 *iv, void *aes_ctx)
931 return gcmaes_crypt_by_sg(true, req, assoclen, hash_subkey, iv,
935 static int gcmaes_decrypt(struct aead_request *req, unsigned int assoclen,
936 u8 *hash_subkey, u8 *iv, void *aes_ctx)
938 return gcmaes_crypt_by_sg(false, req, assoclen, hash_subkey, iv,
942 static int helper_rfc4106_encrypt(struct aead_request *req)
944 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
945 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
946 void *aes_ctx = &(ctx->aes_key_expanded);
947 u8 iv[16] __attribute__ ((__aligned__(AESNI_ALIGN)));
949 __be32 counter = cpu_to_be32(1);
951 /* Assuming we are supporting rfc4106 64-bit extended */
952 /* sequence numbers We need to have the AAD length equal */
953 /* to 16 or 20 bytes */
954 if (unlikely(req->assoclen != 16 && req->assoclen != 20))
958 for (i = 0; i < 4; i++)
959 *(iv+i) = ctx->nonce[i];
960 for (i = 0; i < 8; i++)
961 *(iv+4+i) = req->iv[i];
962 *((__be32 *)(iv+12)) = counter;
964 return gcmaes_encrypt(req, req->assoclen - 8, ctx->hash_subkey, iv,
968 static int helper_rfc4106_decrypt(struct aead_request *req)
970 __be32 counter = cpu_to_be32(1);
971 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
972 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
973 void *aes_ctx = &(ctx->aes_key_expanded);
974 u8 iv[16] __attribute__ ((__aligned__(AESNI_ALIGN)));
977 if (unlikely(req->assoclen != 16 && req->assoclen != 20))
980 /* Assuming we are supporting rfc4106 64-bit extended */
981 /* sequence numbers We need to have the AAD length */
982 /* equal to 16 or 20 bytes */
985 for (i = 0; i < 4; i++)
986 *(iv+i) = ctx->nonce[i];
987 for (i = 0; i < 8; i++)
988 *(iv+4+i) = req->iv[i];
989 *((__be32 *)(iv+12)) = counter;
991 return gcmaes_decrypt(req, req->assoclen - 8, ctx->hash_subkey, iv,
996 static struct crypto_alg aesni_cipher_alg = {
998 .cra_driver_name = "aes-aesni",
1000 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
1001 .cra_blocksize = AES_BLOCK_SIZE,
1002 .cra_ctxsize = CRYPTO_AES_CTX_SIZE,
1003 .cra_module = THIS_MODULE,
1006 .cia_min_keysize = AES_MIN_KEY_SIZE,
1007 .cia_max_keysize = AES_MAX_KEY_SIZE,
1008 .cia_setkey = aes_set_key,
1009 .cia_encrypt = aesni_encrypt,
1010 .cia_decrypt = aesni_decrypt
1015 static struct skcipher_alg aesni_skciphers[] = {
1018 .cra_name = "__ecb(aes)",
1019 .cra_driver_name = "__ecb-aes-aesni",
1020 .cra_priority = 400,
1021 .cra_flags = CRYPTO_ALG_INTERNAL,
1022 .cra_blocksize = AES_BLOCK_SIZE,
1023 .cra_ctxsize = CRYPTO_AES_CTX_SIZE,
1024 .cra_module = THIS_MODULE,
1026 .min_keysize = AES_MIN_KEY_SIZE,
1027 .max_keysize = AES_MAX_KEY_SIZE,
1028 .setkey = aesni_skcipher_setkey,
1029 .encrypt = ecb_encrypt,
1030 .decrypt = ecb_decrypt,
1033 .cra_name = "__cbc(aes)",
1034 .cra_driver_name = "__cbc-aes-aesni",
1035 .cra_priority = 400,
1036 .cra_flags = CRYPTO_ALG_INTERNAL,
1037 .cra_blocksize = AES_BLOCK_SIZE,
1038 .cra_ctxsize = CRYPTO_AES_CTX_SIZE,
1039 .cra_module = THIS_MODULE,
1041 .min_keysize = AES_MIN_KEY_SIZE,
1042 .max_keysize = AES_MAX_KEY_SIZE,
1043 .ivsize = AES_BLOCK_SIZE,
1044 .setkey = aesni_skcipher_setkey,
1045 .encrypt = cbc_encrypt,
1046 .decrypt = cbc_decrypt,
1049 .cra_name = "__cts(cbc(aes))",
1050 .cra_driver_name = "__cts-cbc-aes-aesni",
1051 .cra_priority = 400,
1052 .cra_flags = CRYPTO_ALG_INTERNAL,
1053 .cra_blocksize = AES_BLOCK_SIZE,
1054 .cra_ctxsize = CRYPTO_AES_CTX_SIZE,
1055 .cra_module = THIS_MODULE,
1057 .min_keysize = AES_MIN_KEY_SIZE,
1058 .max_keysize = AES_MAX_KEY_SIZE,
1059 .ivsize = AES_BLOCK_SIZE,
1060 .walksize = 2 * AES_BLOCK_SIZE,
1061 .setkey = aesni_skcipher_setkey,
1062 .encrypt = cts_cbc_encrypt,
1063 .decrypt = cts_cbc_decrypt,
1064 #ifdef CONFIG_X86_64
1067 .cra_name = "__ctr(aes)",
1068 .cra_driver_name = "__ctr-aes-aesni",
1069 .cra_priority = 400,
1070 .cra_flags = CRYPTO_ALG_INTERNAL,
1072 .cra_ctxsize = CRYPTO_AES_CTX_SIZE,
1073 .cra_module = THIS_MODULE,
1075 .min_keysize = AES_MIN_KEY_SIZE,
1076 .max_keysize = AES_MAX_KEY_SIZE,
1077 .ivsize = AES_BLOCK_SIZE,
1078 .chunksize = AES_BLOCK_SIZE,
1079 .setkey = aesni_skcipher_setkey,
1080 .encrypt = ctr_crypt,
1081 .decrypt = ctr_crypt,
1084 .cra_name = "__xts(aes)",
1085 .cra_driver_name = "__xts-aes-aesni",
1086 .cra_priority = 401,
1087 .cra_flags = CRYPTO_ALG_INTERNAL,
1088 .cra_blocksize = AES_BLOCK_SIZE,
1089 .cra_ctxsize = XTS_AES_CTX_SIZE,
1090 .cra_module = THIS_MODULE,
1092 .min_keysize = 2 * AES_MIN_KEY_SIZE,
1093 .max_keysize = 2 * AES_MAX_KEY_SIZE,
1094 .ivsize = AES_BLOCK_SIZE,
1095 .setkey = xts_aesni_setkey,
1096 .encrypt = xts_encrypt,
1097 .decrypt = xts_decrypt,
1103 struct simd_skcipher_alg *aesni_simd_skciphers[ARRAY_SIZE(aesni_skciphers)];
1105 #ifdef CONFIG_X86_64
1106 static int generic_gcmaes_set_key(struct crypto_aead *aead, const u8 *key,
1107 unsigned int key_len)
1109 struct generic_gcmaes_ctx *ctx = generic_gcmaes_ctx_get(aead);
1111 return aes_set_key_common(crypto_aead_tfm(aead),
1112 &ctx->aes_key_expanded, key, key_len) ?:
1113 rfc4106_set_hash_subkey(ctx->hash_subkey, key, key_len);
1116 static int generic_gcmaes_encrypt(struct aead_request *req)
1118 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1119 struct generic_gcmaes_ctx *ctx = generic_gcmaes_ctx_get(tfm);
1120 void *aes_ctx = &(ctx->aes_key_expanded);
1121 u8 iv[16] __attribute__ ((__aligned__(AESNI_ALIGN)));
1122 __be32 counter = cpu_to_be32(1);
1124 memcpy(iv, req->iv, 12);
1125 *((__be32 *)(iv+12)) = counter;
1127 return gcmaes_encrypt(req, req->assoclen, ctx->hash_subkey, iv,
1131 static int generic_gcmaes_decrypt(struct aead_request *req)
1133 __be32 counter = cpu_to_be32(1);
1134 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1135 struct generic_gcmaes_ctx *ctx = generic_gcmaes_ctx_get(tfm);
1136 void *aes_ctx = &(ctx->aes_key_expanded);
1137 u8 iv[16] __attribute__ ((__aligned__(AESNI_ALIGN)));
1139 memcpy(iv, req->iv, 12);
1140 *((__be32 *)(iv+12)) = counter;
1142 return gcmaes_decrypt(req, req->assoclen, ctx->hash_subkey, iv,
1146 static struct aead_alg aesni_aeads[] = { {
1147 .setkey = common_rfc4106_set_key,
1148 .setauthsize = common_rfc4106_set_authsize,
1149 .encrypt = helper_rfc4106_encrypt,
1150 .decrypt = helper_rfc4106_decrypt,
1151 .ivsize = GCM_RFC4106_IV_SIZE,
1154 .cra_name = "__rfc4106(gcm(aes))",
1155 .cra_driver_name = "__rfc4106-gcm-aesni",
1156 .cra_priority = 400,
1157 .cra_flags = CRYPTO_ALG_INTERNAL,
1159 .cra_ctxsize = sizeof(struct aesni_rfc4106_gcm_ctx),
1160 .cra_alignmask = AESNI_ALIGN - 1,
1161 .cra_module = THIS_MODULE,
1164 .setkey = generic_gcmaes_set_key,
1165 .setauthsize = generic_gcmaes_set_authsize,
1166 .encrypt = generic_gcmaes_encrypt,
1167 .decrypt = generic_gcmaes_decrypt,
1168 .ivsize = GCM_AES_IV_SIZE,
1171 .cra_name = "__gcm(aes)",
1172 .cra_driver_name = "__generic-gcm-aesni",
1173 .cra_priority = 400,
1174 .cra_flags = CRYPTO_ALG_INTERNAL,
1176 .cra_ctxsize = sizeof(struct generic_gcmaes_ctx),
1177 .cra_alignmask = AESNI_ALIGN - 1,
1178 .cra_module = THIS_MODULE,
1182 static struct aead_alg aesni_aeads[0];
1185 static struct simd_aead_alg *aesni_simd_aeads[ARRAY_SIZE(aesni_aeads)];
1187 static const struct x86_cpu_id aesni_cpu_id[] = {
1188 X86_MATCH_FEATURE(X86_FEATURE_AES, NULL),
1191 MODULE_DEVICE_TABLE(x86cpu, aesni_cpu_id);
1193 static int __init aesni_init(void)
1197 if (!x86_match_cpu(aesni_cpu_id))
1199 #ifdef CONFIG_X86_64
1200 if (boot_cpu_has(X86_FEATURE_AVX2)) {
1201 pr_info("AVX2 version of gcm_enc/dec engaged.\n");
1202 aesni_gcm_tfm = &aesni_gcm_tfm_avx_gen4;
1204 if (boot_cpu_has(X86_FEATURE_AVX)) {
1205 pr_info("AVX version of gcm_enc/dec engaged.\n");
1206 aesni_gcm_tfm = &aesni_gcm_tfm_avx_gen2;
1208 pr_info("SSE version of gcm_enc/dec engaged.\n");
1209 aesni_gcm_tfm = &aesni_gcm_tfm_sse;
1211 aesni_ctr_enc_tfm = aesni_ctr_enc;
1212 if (boot_cpu_has(X86_FEATURE_AVX)) {
1213 /* optimize performance of ctr mode encryption transform */
1214 aesni_ctr_enc_tfm = aesni_ctr_enc_avx_tfm;
1215 pr_info("AES CTR mode by8 optimization enabled\n");
1219 err = crypto_register_alg(&aesni_cipher_alg);
1223 err = simd_register_skciphers_compat(aesni_skciphers,
1224 ARRAY_SIZE(aesni_skciphers),
1225 aesni_simd_skciphers);
1227 goto unregister_cipher;
1229 err = simd_register_aeads_compat(aesni_aeads, ARRAY_SIZE(aesni_aeads),
1232 goto unregister_skciphers;
1236 unregister_skciphers:
1237 simd_unregister_skciphers(aesni_skciphers, ARRAY_SIZE(aesni_skciphers),
1238 aesni_simd_skciphers);
1240 crypto_unregister_alg(&aesni_cipher_alg);
1244 static void __exit aesni_exit(void)
1246 simd_unregister_aeads(aesni_aeads, ARRAY_SIZE(aesni_aeads),
1248 simd_unregister_skciphers(aesni_skciphers, ARRAY_SIZE(aesni_skciphers),
1249 aesni_simd_skciphers);
1250 crypto_unregister_alg(&aesni_cipher_alg);
1253 late_initcall(aesni_init);
1254 module_exit(aesni_exit);
1256 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm, Intel AES-NI instructions optimized");
1257 MODULE_LICENSE("GPL");
1258 MODULE_ALIAS_CRYPTO("aes");