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>
38 #define AESNI_ALIGN 16
39 #define AESNI_ALIGN_ATTR __attribute__ ((__aligned__(AESNI_ALIGN)))
40 #define AES_BLOCK_MASK (~(AES_BLOCK_SIZE - 1))
41 #define RFC4106_HASH_SUBKEY_SIZE 16
42 #define AESNI_ALIGN_EXTRA ((AESNI_ALIGN - 1) & ~(CRYPTO_MINALIGN - 1))
43 #define CRYPTO_AES_CTX_SIZE (sizeof(struct crypto_aes_ctx) + AESNI_ALIGN_EXTRA)
44 #define XTS_AES_CTX_SIZE (sizeof(struct aesni_xts_ctx) + AESNI_ALIGN_EXTRA)
46 /* This data is stored at the end of the crypto_tfm struct.
47 * It's a type of per "session" data storage location.
48 * This needs to be 16 byte aligned.
50 struct aesni_rfc4106_gcm_ctx {
51 u8 hash_subkey[16] AESNI_ALIGN_ATTR;
52 struct crypto_aes_ctx aes_key_expanded AESNI_ALIGN_ATTR;
56 struct generic_gcmaes_ctx {
57 u8 hash_subkey[16] AESNI_ALIGN_ATTR;
58 struct crypto_aes_ctx aes_key_expanded AESNI_ALIGN_ATTR;
61 struct aesni_xts_ctx {
62 u8 raw_tweak_ctx[sizeof(struct crypto_aes_ctx)] AESNI_ALIGN_ATTR;
63 u8 raw_crypt_ctx[sizeof(struct crypto_aes_ctx)] AESNI_ALIGN_ATTR;
66 #define GCM_BLOCK_LEN 16
68 struct gcm_context_data {
69 /* init, update and finalize context data */
70 u8 aad_hash[GCM_BLOCK_LEN];
73 u8 partial_block_enc_key[GCM_BLOCK_LEN];
74 u8 orig_IV[GCM_BLOCK_LEN];
75 u8 current_counter[GCM_BLOCK_LEN];
76 u64 partial_block_len;
78 u8 hash_keys[GCM_BLOCK_LEN * 16];
81 asmlinkage int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key,
82 unsigned int key_len);
83 asmlinkage void aesni_enc(const void *ctx, u8 *out, const u8 *in);
84 asmlinkage void aesni_dec(const void *ctx, u8 *out, const u8 *in);
85 asmlinkage void aesni_ecb_enc(struct crypto_aes_ctx *ctx, u8 *out,
86 const u8 *in, unsigned int len);
87 asmlinkage void aesni_ecb_dec(struct crypto_aes_ctx *ctx, u8 *out,
88 const u8 *in, unsigned int len);
89 asmlinkage void aesni_cbc_enc(struct crypto_aes_ctx *ctx, u8 *out,
90 const u8 *in, unsigned int len, u8 *iv);
91 asmlinkage void aesni_cbc_dec(struct crypto_aes_ctx *ctx, u8 *out,
92 const u8 *in, unsigned int len, u8 *iv);
93 asmlinkage void aesni_cts_cbc_enc(struct crypto_aes_ctx *ctx, u8 *out,
94 const u8 *in, unsigned int len, u8 *iv);
95 asmlinkage void aesni_cts_cbc_dec(struct crypto_aes_ctx *ctx, u8 *out,
96 const u8 *in, unsigned int len, u8 *iv);
98 #define AVX_GEN2_OPTSIZE 640
99 #define AVX_GEN4_OPTSIZE 4096
101 asmlinkage void aesni_xts_encrypt(const struct crypto_aes_ctx *ctx, u8 *out,
102 const u8 *in, unsigned int len, u8 *iv);
104 asmlinkage void aesni_xts_decrypt(const struct crypto_aes_ctx *ctx, u8 *out,
105 const u8 *in, unsigned int len, u8 *iv);
109 static void (*aesni_ctr_enc_tfm)(struct crypto_aes_ctx *ctx, u8 *out,
110 const u8 *in, unsigned int len, u8 *iv);
111 asmlinkage void aesni_ctr_enc(struct crypto_aes_ctx *ctx, u8 *out,
112 const u8 *in, unsigned int len, u8 *iv);
114 /* Scatter / Gather routines, with args similar to above */
115 asmlinkage void aesni_gcm_init(void *ctx,
116 struct gcm_context_data *gdata,
118 u8 *hash_subkey, const u8 *aad,
119 unsigned long aad_len);
120 asmlinkage void aesni_gcm_enc_update(void *ctx,
121 struct gcm_context_data *gdata, u8 *out,
122 const u8 *in, unsigned long plaintext_len);
123 asmlinkage void aesni_gcm_dec_update(void *ctx,
124 struct gcm_context_data *gdata, u8 *out,
126 unsigned long ciphertext_len);
127 asmlinkage void aesni_gcm_finalize(void *ctx,
128 struct gcm_context_data *gdata,
129 u8 *auth_tag, unsigned long auth_tag_len);
131 static const struct aesni_gcm_tfm_s {
132 void (*init)(void *ctx, struct gcm_context_data *gdata, u8 *iv,
133 u8 *hash_subkey, const u8 *aad, unsigned long aad_len);
134 void (*enc_update)(void *ctx, struct gcm_context_data *gdata, u8 *out,
135 const u8 *in, unsigned long plaintext_len);
136 void (*dec_update)(void *ctx, struct gcm_context_data *gdata, u8 *out,
137 const u8 *in, unsigned long ciphertext_len);
138 void (*finalize)(void *ctx, struct gcm_context_data *gdata,
139 u8 *auth_tag, unsigned long auth_tag_len);
142 static const struct aesni_gcm_tfm_s aesni_gcm_tfm_sse = {
143 .init = &aesni_gcm_init,
144 .enc_update = &aesni_gcm_enc_update,
145 .dec_update = &aesni_gcm_dec_update,
146 .finalize = &aesni_gcm_finalize,
149 asmlinkage void aes_ctr_enc_128_avx_by8(const u8 *in, u8 *iv,
150 void *keys, u8 *out, unsigned int num_bytes);
151 asmlinkage void aes_ctr_enc_192_avx_by8(const u8 *in, u8 *iv,
152 void *keys, u8 *out, unsigned int num_bytes);
153 asmlinkage void aes_ctr_enc_256_avx_by8(const u8 *in, u8 *iv,
154 void *keys, u8 *out, unsigned int num_bytes);
156 * asmlinkage void aesni_gcm_init_avx_gen2()
157 * gcm_data *my_ctx_data, context data
158 * u8 *hash_subkey, the Hash sub key input. Data starts on a 16-byte boundary.
160 asmlinkage void aesni_gcm_init_avx_gen2(void *my_ctx_data,
161 struct gcm_context_data *gdata,
165 unsigned long aad_len);
167 asmlinkage void aesni_gcm_enc_update_avx_gen2(void *ctx,
168 struct gcm_context_data *gdata, u8 *out,
169 const u8 *in, unsigned long plaintext_len);
170 asmlinkage void aesni_gcm_dec_update_avx_gen2(void *ctx,
171 struct gcm_context_data *gdata, u8 *out,
173 unsigned long ciphertext_len);
174 asmlinkage void aesni_gcm_finalize_avx_gen2(void *ctx,
175 struct gcm_context_data *gdata,
176 u8 *auth_tag, unsigned long auth_tag_len);
178 static const struct aesni_gcm_tfm_s aesni_gcm_tfm_avx_gen2 = {
179 .init = &aesni_gcm_init_avx_gen2,
180 .enc_update = &aesni_gcm_enc_update_avx_gen2,
181 .dec_update = &aesni_gcm_dec_update_avx_gen2,
182 .finalize = &aesni_gcm_finalize_avx_gen2,
186 * asmlinkage void aesni_gcm_init_avx_gen4()
187 * gcm_data *my_ctx_data, context data
188 * u8 *hash_subkey, the Hash sub key input. Data starts on a 16-byte boundary.
190 asmlinkage void aesni_gcm_init_avx_gen4(void *my_ctx_data,
191 struct gcm_context_data *gdata,
195 unsigned long aad_len);
197 asmlinkage void aesni_gcm_enc_update_avx_gen4(void *ctx,
198 struct gcm_context_data *gdata, u8 *out,
199 const u8 *in, unsigned long plaintext_len);
200 asmlinkage void aesni_gcm_dec_update_avx_gen4(void *ctx,
201 struct gcm_context_data *gdata, u8 *out,
203 unsigned long ciphertext_len);
204 asmlinkage void aesni_gcm_finalize_avx_gen4(void *ctx,
205 struct gcm_context_data *gdata,
206 u8 *auth_tag, unsigned long auth_tag_len);
208 static const struct aesni_gcm_tfm_s aesni_gcm_tfm_avx_gen4 = {
209 .init = &aesni_gcm_init_avx_gen4,
210 .enc_update = &aesni_gcm_enc_update_avx_gen4,
211 .dec_update = &aesni_gcm_dec_update_avx_gen4,
212 .finalize = &aesni_gcm_finalize_avx_gen4,
216 aesni_rfc4106_gcm_ctx *aesni_rfc4106_gcm_ctx_get(struct crypto_aead *tfm)
218 unsigned long align = AESNI_ALIGN;
220 if (align <= crypto_tfm_ctx_alignment())
222 return PTR_ALIGN(crypto_aead_ctx(tfm), align);
226 generic_gcmaes_ctx *generic_gcmaes_ctx_get(struct crypto_aead *tfm)
228 unsigned long align = AESNI_ALIGN;
230 if (align <= crypto_tfm_ctx_alignment())
232 return PTR_ALIGN(crypto_aead_ctx(tfm), align);
236 static inline struct crypto_aes_ctx *aes_ctx(void *raw_ctx)
238 unsigned long addr = (unsigned long)raw_ctx;
239 unsigned long align = AESNI_ALIGN;
241 if (align <= crypto_tfm_ctx_alignment())
243 return (struct crypto_aes_ctx *)ALIGN(addr, align);
246 static int aes_set_key_common(struct crypto_tfm *tfm, void *raw_ctx,
247 const u8 *in_key, unsigned int key_len)
249 struct crypto_aes_ctx *ctx = aes_ctx(raw_ctx);
252 if (key_len != AES_KEYSIZE_128 && key_len != AES_KEYSIZE_192 &&
253 key_len != AES_KEYSIZE_256)
256 if (!crypto_simd_usable())
257 err = aes_expandkey(ctx, in_key, key_len);
260 err = aesni_set_key(ctx, in_key, key_len);
267 static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
268 unsigned int key_len)
270 return aes_set_key_common(tfm, crypto_tfm_ctx(tfm), in_key, key_len);
273 static void aesni_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
275 struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
277 if (!crypto_simd_usable()) {
278 aes_encrypt(ctx, dst, src);
281 aesni_enc(ctx, dst, src);
286 static void aesni_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
288 struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
290 if (!crypto_simd_usable()) {
291 aes_decrypt(ctx, dst, src);
294 aesni_dec(ctx, dst, src);
299 static int aesni_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
302 return aes_set_key_common(crypto_skcipher_tfm(tfm),
303 crypto_skcipher_ctx(tfm), key, len);
306 static int ecb_encrypt(struct skcipher_request *req)
308 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
309 struct crypto_aes_ctx *ctx = aes_ctx(crypto_skcipher_ctx(tfm));
310 struct skcipher_walk walk;
314 err = skcipher_walk_virt(&walk, req, true);
317 while ((nbytes = walk.nbytes)) {
318 aesni_ecb_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
319 nbytes & AES_BLOCK_MASK);
320 nbytes &= AES_BLOCK_SIZE - 1;
321 err = skcipher_walk_done(&walk, nbytes);
328 static int ecb_decrypt(struct skcipher_request *req)
330 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
331 struct crypto_aes_ctx *ctx = aes_ctx(crypto_skcipher_ctx(tfm));
332 struct skcipher_walk walk;
336 err = skcipher_walk_virt(&walk, req, true);
339 while ((nbytes = walk.nbytes)) {
340 aesni_ecb_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
341 nbytes & AES_BLOCK_MASK);
342 nbytes &= AES_BLOCK_SIZE - 1;
343 err = skcipher_walk_done(&walk, nbytes);
350 static int cbc_encrypt(struct skcipher_request *req)
352 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
353 struct crypto_aes_ctx *ctx = aes_ctx(crypto_skcipher_ctx(tfm));
354 struct skcipher_walk walk;
358 err = skcipher_walk_virt(&walk, req, true);
361 while ((nbytes = walk.nbytes)) {
362 aesni_cbc_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
363 nbytes & AES_BLOCK_MASK, walk.iv);
364 nbytes &= AES_BLOCK_SIZE - 1;
365 err = skcipher_walk_done(&walk, nbytes);
372 static int cbc_decrypt(struct skcipher_request *req)
374 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
375 struct crypto_aes_ctx *ctx = aes_ctx(crypto_skcipher_ctx(tfm));
376 struct skcipher_walk walk;
380 err = skcipher_walk_virt(&walk, req, true);
383 while ((nbytes = walk.nbytes)) {
384 aesni_cbc_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
385 nbytes & AES_BLOCK_MASK, walk.iv);
386 nbytes &= AES_BLOCK_SIZE - 1;
387 err = skcipher_walk_done(&walk, nbytes);
394 static int cts_cbc_encrypt(struct skcipher_request *req)
396 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
397 struct crypto_aes_ctx *ctx = aes_ctx(crypto_skcipher_ctx(tfm));
398 int cbc_blocks = DIV_ROUND_UP(req->cryptlen, AES_BLOCK_SIZE) - 2;
399 struct scatterlist *src = req->src, *dst = req->dst;
400 struct scatterlist sg_src[2], sg_dst[2];
401 struct skcipher_request subreq;
402 struct skcipher_walk walk;
405 skcipher_request_set_tfm(&subreq, tfm);
406 skcipher_request_set_callback(&subreq, skcipher_request_flags(req),
409 if (req->cryptlen <= AES_BLOCK_SIZE) {
410 if (req->cryptlen < AES_BLOCK_SIZE)
415 if (cbc_blocks > 0) {
416 skcipher_request_set_crypt(&subreq, req->src, req->dst,
417 cbc_blocks * AES_BLOCK_SIZE,
420 err = cbc_encrypt(&subreq);
424 if (req->cryptlen == AES_BLOCK_SIZE)
427 dst = src = scatterwalk_ffwd(sg_src, req->src, subreq.cryptlen);
428 if (req->dst != req->src)
429 dst = scatterwalk_ffwd(sg_dst, req->dst,
433 /* handle ciphertext stealing */
434 skcipher_request_set_crypt(&subreq, src, dst,
435 req->cryptlen - cbc_blocks * AES_BLOCK_SIZE,
438 err = skcipher_walk_virt(&walk, &subreq, false);
443 aesni_cts_cbc_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
444 walk.nbytes, walk.iv);
447 return skcipher_walk_done(&walk, 0);
450 static int cts_cbc_decrypt(struct skcipher_request *req)
452 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
453 struct crypto_aes_ctx *ctx = aes_ctx(crypto_skcipher_ctx(tfm));
454 int cbc_blocks = DIV_ROUND_UP(req->cryptlen, AES_BLOCK_SIZE) - 2;
455 struct scatterlist *src = req->src, *dst = req->dst;
456 struct scatterlist sg_src[2], sg_dst[2];
457 struct skcipher_request subreq;
458 struct skcipher_walk walk;
461 skcipher_request_set_tfm(&subreq, tfm);
462 skcipher_request_set_callback(&subreq, skcipher_request_flags(req),
465 if (req->cryptlen <= AES_BLOCK_SIZE) {
466 if (req->cryptlen < AES_BLOCK_SIZE)
471 if (cbc_blocks > 0) {
472 skcipher_request_set_crypt(&subreq, req->src, req->dst,
473 cbc_blocks * AES_BLOCK_SIZE,
476 err = cbc_decrypt(&subreq);
480 if (req->cryptlen == AES_BLOCK_SIZE)
483 dst = src = scatterwalk_ffwd(sg_src, req->src, subreq.cryptlen);
484 if (req->dst != req->src)
485 dst = scatterwalk_ffwd(sg_dst, req->dst,
489 /* handle ciphertext stealing */
490 skcipher_request_set_crypt(&subreq, src, dst,
491 req->cryptlen - cbc_blocks * AES_BLOCK_SIZE,
494 err = skcipher_walk_virt(&walk, &subreq, false);
499 aesni_cts_cbc_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
500 walk.nbytes, walk.iv);
503 return skcipher_walk_done(&walk, 0);
507 static void ctr_crypt_final(struct crypto_aes_ctx *ctx,
508 struct skcipher_walk *walk)
510 u8 *ctrblk = walk->iv;
511 u8 keystream[AES_BLOCK_SIZE];
512 u8 *src = walk->src.virt.addr;
513 u8 *dst = walk->dst.virt.addr;
514 unsigned int nbytes = walk->nbytes;
516 aesni_enc(ctx, keystream, ctrblk);
517 crypto_xor_cpy(dst, keystream, src, nbytes);
519 crypto_inc(ctrblk, AES_BLOCK_SIZE);
522 static void aesni_ctr_enc_avx_tfm(struct crypto_aes_ctx *ctx, u8 *out,
523 const u8 *in, unsigned int len, u8 *iv)
526 * based on key length, override with the by8 version
527 * of ctr mode encryption/decryption for improved performance
528 * aes_set_key_common() ensures that key length is one of
531 if (ctx->key_length == AES_KEYSIZE_128)
532 aes_ctr_enc_128_avx_by8(in, iv, (void *)ctx, out, len);
533 else if (ctx->key_length == AES_KEYSIZE_192)
534 aes_ctr_enc_192_avx_by8(in, iv, (void *)ctx, out, len);
536 aes_ctr_enc_256_avx_by8(in, iv, (void *)ctx, out, len);
539 static int ctr_crypt(struct skcipher_request *req)
541 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
542 struct crypto_aes_ctx *ctx = aes_ctx(crypto_skcipher_ctx(tfm));
543 struct skcipher_walk walk;
547 err = skcipher_walk_virt(&walk, req, true);
550 while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
551 aesni_ctr_enc_tfm(ctx, walk.dst.virt.addr, walk.src.virt.addr,
552 nbytes & AES_BLOCK_MASK, walk.iv);
553 nbytes &= AES_BLOCK_SIZE - 1;
554 err = skcipher_walk_done(&walk, nbytes);
557 ctr_crypt_final(ctx, &walk);
558 err = skcipher_walk_done(&walk, 0);
566 rfc4106_set_hash_subkey(u8 *hash_subkey, const u8 *key, unsigned int key_len)
568 struct crypto_aes_ctx ctx;
571 ret = aes_expandkey(&ctx, key, key_len);
575 /* Clear the data in the hash sub key container to zero.*/
576 /* We want to cipher all zeros to create the hash sub key. */
577 memset(hash_subkey, 0, RFC4106_HASH_SUBKEY_SIZE);
579 aes_encrypt(&ctx, hash_subkey, hash_subkey);
581 memzero_explicit(&ctx, sizeof(ctx));
585 static int common_rfc4106_set_key(struct crypto_aead *aead, const u8 *key,
586 unsigned int key_len)
588 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(aead);
593 /*Account for 4 byte nonce at the end.*/
596 memcpy(ctx->nonce, key + key_len, sizeof(ctx->nonce));
598 return aes_set_key_common(crypto_aead_tfm(aead),
599 &ctx->aes_key_expanded, key, key_len) ?:
600 rfc4106_set_hash_subkey(ctx->hash_subkey, key, key_len);
603 /* This is the Integrity Check Value (aka the authentication tag) length and can
604 * be 8, 12 or 16 bytes long. */
605 static int common_rfc4106_set_authsize(struct crypto_aead *aead,
606 unsigned int authsize)
620 static int generic_gcmaes_set_authsize(struct crypto_aead *tfm,
621 unsigned int authsize)
639 static int gcmaes_crypt_by_sg(bool enc, struct aead_request *req,
640 unsigned int assoclen, u8 *hash_subkey,
641 u8 *iv, void *aes_ctx)
643 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
644 unsigned long auth_tag_len = crypto_aead_authsize(tfm);
645 const struct aesni_gcm_tfm_s *gcm_tfm = aesni_gcm_tfm;
646 u8 databuf[sizeof(struct gcm_context_data) + (AESNI_ALIGN - 8)] __aligned(8);
647 struct gcm_context_data *data = PTR_ALIGN((void *)databuf, AESNI_ALIGN);
648 struct scatter_walk dst_sg_walk = {};
649 unsigned long left = req->cryptlen;
650 unsigned long len, srclen, dstlen;
651 struct scatter_walk assoc_sg_walk;
652 struct scatter_walk src_sg_walk;
653 struct scatterlist src_start[2];
654 struct scatterlist dst_start[2];
655 struct scatterlist *src_sg;
656 struct scatterlist *dst_sg;
657 u8 *src, *dst, *assoc;
662 left -= auth_tag_len;
664 if (left < AVX_GEN4_OPTSIZE && gcm_tfm == &aesni_gcm_tfm_avx_gen4)
665 gcm_tfm = &aesni_gcm_tfm_avx_gen2;
666 if (left < AVX_GEN2_OPTSIZE && gcm_tfm == &aesni_gcm_tfm_avx_gen2)
667 gcm_tfm = &aesni_gcm_tfm_sse;
669 /* Linearize assoc, if not already linear */
670 if (req->src->length >= assoclen && req->src->length) {
671 scatterwalk_start(&assoc_sg_walk, req->src);
672 assoc = scatterwalk_map(&assoc_sg_walk);
674 gfp_t flags = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
675 GFP_KERNEL : GFP_ATOMIC;
677 /* assoc can be any length, so must be on heap */
678 assocmem = kmalloc(assoclen, flags);
679 if (unlikely(!assocmem))
683 scatterwalk_map_and_copy(assoc, req->src, 0, assoclen, 0);
687 src_sg = scatterwalk_ffwd(src_start, req->src, req->assoclen);
688 scatterwalk_start(&src_sg_walk, src_sg);
689 if (req->src != req->dst) {
690 dst_sg = scatterwalk_ffwd(dst_start, req->dst,
692 scatterwalk_start(&dst_sg_walk, dst_sg);
697 gcm_tfm->init(aes_ctx, data, iv, hash_subkey, assoc, assoclen);
698 if (req->src != req->dst) {
700 src = scatterwalk_map(&src_sg_walk);
701 dst = scatterwalk_map(&dst_sg_walk);
702 srclen = scatterwalk_clamp(&src_sg_walk, left);
703 dstlen = scatterwalk_clamp(&dst_sg_walk, left);
704 len = min(srclen, dstlen);
707 gcm_tfm->enc_update(aes_ctx, data,
710 gcm_tfm->dec_update(aes_ctx, data,
715 scatterwalk_unmap(src);
716 scatterwalk_unmap(dst);
717 scatterwalk_advance(&src_sg_walk, len);
718 scatterwalk_advance(&dst_sg_walk, len);
719 scatterwalk_done(&src_sg_walk, 0, left);
720 scatterwalk_done(&dst_sg_walk, 1, left);
724 dst = src = scatterwalk_map(&src_sg_walk);
725 len = scatterwalk_clamp(&src_sg_walk, left);
728 gcm_tfm->enc_update(aes_ctx, data,
731 gcm_tfm->dec_update(aes_ctx, data,
735 scatterwalk_unmap(src);
736 scatterwalk_advance(&src_sg_walk, len);
737 scatterwalk_done(&src_sg_walk, 1, left);
740 gcm_tfm->finalize(aes_ctx, data, authTag, auth_tag_len);
744 scatterwalk_unmap(assoc);
751 /* Copy out original authTag */
752 scatterwalk_map_and_copy(authTagMsg, req->src,
753 req->assoclen + req->cryptlen -
757 /* Compare generated tag with passed in tag. */
758 return crypto_memneq(authTagMsg, authTag, auth_tag_len) ?
762 /* Copy in the authTag */
763 scatterwalk_map_and_copy(authTag, req->dst,
764 req->assoclen + req->cryptlen,
770 static int gcmaes_encrypt(struct aead_request *req, unsigned int assoclen,
771 u8 *hash_subkey, u8 *iv, void *aes_ctx)
773 return gcmaes_crypt_by_sg(true, req, assoclen, hash_subkey, iv,
777 static int gcmaes_decrypt(struct aead_request *req, unsigned int assoclen,
778 u8 *hash_subkey, u8 *iv, void *aes_ctx)
780 return gcmaes_crypt_by_sg(false, req, assoclen, hash_subkey, iv,
784 static int helper_rfc4106_encrypt(struct aead_request *req)
786 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
787 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
788 void *aes_ctx = &(ctx->aes_key_expanded);
789 u8 ivbuf[16 + (AESNI_ALIGN - 8)] __aligned(8);
790 u8 *iv = PTR_ALIGN(&ivbuf[0], AESNI_ALIGN);
792 __be32 counter = cpu_to_be32(1);
794 /* Assuming we are supporting rfc4106 64-bit extended */
795 /* sequence numbers We need to have the AAD length equal */
796 /* to 16 or 20 bytes */
797 if (unlikely(req->assoclen != 16 && req->assoclen != 20))
801 for (i = 0; i < 4; i++)
802 *(iv+i) = ctx->nonce[i];
803 for (i = 0; i < 8; i++)
804 *(iv+4+i) = req->iv[i];
805 *((__be32 *)(iv+12)) = counter;
807 return gcmaes_encrypt(req, req->assoclen - 8, ctx->hash_subkey, iv,
811 static int helper_rfc4106_decrypt(struct aead_request *req)
813 __be32 counter = cpu_to_be32(1);
814 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
815 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
816 void *aes_ctx = &(ctx->aes_key_expanded);
817 u8 ivbuf[16 + (AESNI_ALIGN - 8)] __aligned(8);
818 u8 *iv = PTR_ALIGN(&ivbuf[0], AESNI_ALIGN);
821 if (unlikely(req->assoclen != 16 && req->assoclen != 20))
824 /* Assuming we are supporting rfc4106 64-bit extended */
825 /* sequence numbers We need to have the AAD length */
826 /* equal to 16 or 20 bytes */
829 for (i = 0; i < 4; i++)
830 *(iv+i) = ctx->nonce[i];
831 for (i = 0; i < 8; i++)
832 *(iv+4+i) = req->iv[i];
833 *((__be32 *)(iv+12)) = counter;
835 return gcmaes_decrypt(req, req->assoclen - 8, ctx->hash_subkey, iv,
840 static int xts_aesni_setkey(struct crypto_skcipher *tfm, const u8 *key,
843 struct aesni_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
846 err = xts_verify_key(tfm, key, keylen);
852 /* first half of xts-key is for crypt */
853 err = aes_set_key_common(crypto_skcipher_tfm(tfm), ctx->raw_crypt_ctx,
858 /* second half of xts-key is for tweak */
859 return aes_set_key_common(crypto_skcipher_tfm(tfm), ctx->raw_tweak_ctx,
860 key + keylen, keylen);
863 static int xts_crypt(struct skcipher_request *req, bool encrypt)
865 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
866 struct aesni_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
867 int tail = req->cryptlen % AES_BLOCK_SIZE;
868 struct skcipher_request subreq;
869 struct skcipher_walk walk;
872 if (req->cryptlen < AES_BLOCK_SIZE)
875 err = skcipher_walk_virt(&walk, req, false);
877 if (unlikely(tail > 0 && walk.nbytes < walk.total)) {
878 int blocks = DIV_ROUND_UP(req->cryptlen, AES_BLOCK_SIZE) - 2;
880 skcipher_walk_abort(&walk);
882 skcipher_request_set_tfm(&subreq, tfm);
883 skcipher_request_set_callback(&subreq,
884 skcipher_request_flags(req),
886 skcipher_request_set_crypt(&subreq, req->src, req->dst,
887 blocks * AES_BLOCK_SIZE, req->iv);
889 err = skcipher_walk_virt(&walk, req, false);
896 /* calculate first value of T */
897 aesni_enc(aes_ctx(ctx->raw_tweak_ctx), walk.iv, walk.iv);
899 while (walk.nbytes > 0) {
900 int nbytes = walk.nbytes;
902 if (nbytes < walk.total)
903 nbytes &= ~(AES_BLOCK_SIZE - 1);
906 aesni_xts_encrypt(aes_ctx(ctx->raw_crypt_ctx),
907 walk.dst.virt.addr, walk.src.virt.addr,
910 aesni_xts_decrypt(aes_ctx(ctx->raw_crypt_ctx),
911 walk.dst.virt.addr, walk.src.virt.addr,
915 err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
921 if (unlikely(tail > 0 && !err)) {
922 struct scatterlist sg_src[2], sg_dst[2];
923 struct scatterlist *src, *dst;
925 dst = src = scatterwalk_ffwd(sg_src, req->src, req->cryptlen);
926 if (req->dst != req->src)
927 dst = scatterwalk_ffwd(sg_dst, req->dst, req->cryptlen);
929 skcipher_request_set_crypt(req, src, dst, AES_BLOCK_SIZE + tail,
932 err = skcipher_walk_virt(&walk, &subreq, false);
938 aesni_xts_encrypt(aes_ctx(ctx->raw_crypt_ctx),
939 walk.dst.virt.addr, walk.src.virt.addr,
940 walk.nbytes, walk.iv);
942 aesni_xts_decrypt(aes_ctx(ctx->raw_crypt_ctx),
943 walk.dst.virt.addr, walk.src.virt.addr,
944 walk.nbytes, walk.iv);
947 err = skcipher_walk_done(&walk, 0);
952 static int xts_encrypt(struct skcipher_request *req)
954 return xts_crypt(req, true);
957 static int xts_decrypt(struct skcipher_request *req)
959 return xts_crypt(req, false);
962 static struct crypto_alg aesni_cipher_alg = {
964 .cra_driver_name = "aes-aesni",
966 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
967 .cra_blocksize = AES_BLOCK_SIZE,
968 .cra_ctxsize = CRYPTO_AES_CTX_SIZE,
969 .cra_module = THIS_MODULE,
972 .cia_min_keysize = AES_MIN_KEY_SIZE,
973 .cia_max_keysize = AES_MAX_KEY_SIZE,
974 .cia_setkey = aes_set_key,
975 .cia_encrypt = aesni_encrypt,
976 .cia_decrypt = aesni_decrypt
981 static struct skcipher_alg aesni_skciphers[] = {
984 .cra_name = "__ecb(aes)",
985 .cra_driver_name = "__ecb-aes-aesni",
987 .cra_flags = CRYPTO_ALG_INTERNAL,
988 .cra_blocksize = AES_BLOCK_SIZE,
989 .cra_ctxsize = CRYPTO_AES_CTX_SIZE,
990 .cra_module = THIS_MODULE,
992 .min_keysize = AES_MIN_KEY_SIZE,
993 .max_keysize = AES_MAX_KEY_SIZE,
994 .setkey = aesni_skcipher_setkey,
995 .encrypt = ecb_encrypt,
996 .decrypt = ecb_decrypt,
999 .cra_name = "__cbc(aes)",
1000 .cra_driver_name = "__cbc-aes-aesni",
1001 .cra_priority = 400,
1002 .cra_flags = CRYPTO_ALG_INTERNAL,
1003 .cra_blocksize = AES_BLOCK_SIZE,
1004 .cra_ctxsize = CRYPTO_AES_CTX_SIZE,
1005 .cra_module = THIS_MODULE,
1007 .min_keysize = AES_MIN_KEY_SIZE,
1008 .max_keysize = AES_MAX_KEY_SIZE,
1009 .ivsize = AES_BLOCK_SIZE,
1010 .setkey = aesni_skcipher_setkey,
1011 .encrypt = cbc_encrypt,
1012 .decrypt = cbc_decrypt,
1015 .cra_name = "__cts(cbc(aes))",
1016 .cra_driver_name = "__cts-cbc-aes-aesni",
1017 .cra_priority = 400,
1018 .cra_flags = CRYPTO_ALG_INTERNAL,
1019 .cra_blocksize = AES_BLOCK_SIZE,
1020 .cra_ctxsize = CRYPTO_AES_CTX_SIZE,
1021 .cra_module = THIS_MODULE,
1023 .min_keysize = AES_MIN_KEY_SIZE,
1024 .max_keysize = AES_MAX_KEY_SIZE,
1025 .ivsize = AES_BLOCK_SIZE,
1026 .walksize = 2 * AES_BLOCK_SIZE,
1027 .setkey = aesni_skcipher_setkey,
1028 .encrypt = cts_cbc_encrypt,
1029 .decrypt = cts_cbc_decrypt,
1030 #ifdef CONFIG_X86_64
1033 .cra_name = "__ctr(aes)",
1034 .cra_driver_name = "__ctr-aes-aesni",
1035 .cra_priority = 400,
1036 .cra_flags = CRYPTO_ALG_INTERNAL,
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 .chunksize = AES_BLOCK_SIZE,
1045 .setkey = aesni_skcipher_setkey,
1046 .encrypt = ctr_crypt,
1047 .decrypt = ctr_crypt,
1051 .cra_name = "__xts(aes)",
1052 .cra_driver_name = "__xts-aes-aesni",
1053 .cra_priority = 401,
1054 .cra_flags = CRYPTO_ALG_INTERNAL,
1055 .cra_blocksize = AES_BLOCK_SIZE,
1056 .cra_ctxsize = XTS_AES_CTX_SIZE,
1057 .cra_module = THIS_MODULE,
1059 .min_keysize = 2 * AES_MIN_KEY_SIZE,
1060 .max_keysize = 2 * AES_MAX_KEY_SIZE,
1061 .ivsize = AES_BLOCK_SIZE,
1062 .walksize = 2 * AES_BLOCK_SIZE,
1063 .setkey = xts_aesni_setkey,
1064 .encrypt = xts_encrypt,
1065 .decrypt = xts_decrypt,
1070 struct simd_skcipher_alg *aesni_simd_skciphers[ARRAY_SIZE(aesni_skciphers)];
1072 #ifdef CONFIG_X86_64
1073 static int generic_gcmaes_set_key(struct crypto_aead *aead, const u8 *key,
1074 unsigned int key_len)
1076 struct generic_gcmaes_ctx *ctx = generic_gcmaes_ctx_get(aead);
1078 return aes_set_key_common(crypto_aead_tfm(aead),
1079 &ctx->aes_key_expanded, key, key_len) ?:
1080 rfc4106_set_hash_subkey(ctx->hash_subkey, key, key_len);
1083 static int generic_gcmaes_encrypt(struct aead_request *req)
1085 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1086 struct generic_gcmaes_ctx *ctx = generic_gcmaes_ctx_get(tfm);
1087 void *aes_ctx = &(ctx->aes_key_expanded);
1088 u8 ivbuf[16 + (AESNI_ALIGN - 8)] __aligned(8);
1089 u8 *iv = PTR_ALIGN(&ivbuf[0], AESNI_ALIGN);
1090 __be32 counter = cpu_to_be32(1);
1092 memcpy(iv, req->iv, 12);
1093 *((__be32 *)(iv+12)) = counter;
1095 return gcmaes_encrypt(req, req->assoclen, ctx->hash_subkey, iv,
1099 static int generic_gcmaes_decrypt(struct aead_request *req)
1101 __be32 counter = cpu_to_be32(1);
1102 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
1103 struct generic_gcmaes_ctx *ctx = generic_gcmaes_ctx_get(tfm);
1104 void *aes_ctx = &(ctx->aes_key_expanded);
1105 u8 ivbuf[16 + (AESNI_ALIGN - 8)] __aligned(8);
1106 u8 *iv = PTR_ALIGN(&ivbuf[0], AESNI_ALIGN);
1108 memcpy(iv, req->iv, 12);
1109 *((__be32 *)(iv+12)) = counter;
1111 return gcmaes_decrypt(req, req->assoclen, ctx->hash_subkey, iv,
1115 static struct aead_alg aesni_aeads[] = { {
1116 .setkey = common_rfc4106_set_key,
1117 .setauthsize = common_rfc4106_set_authsize,
1118 .encrypt = helper_rfc4106_encrypt,
1119 .decrypt = helper_rfc4106_decrypt,
1120 .ivsize = GCM_RFC4106_IV_SIZE,
1123 .cra_name = "__rfc4106(gcm(aes))",
1124 .cra_driver_name = "__rfc4106-gcm-aesni",
1125 .cra_priority = 400,
1126 .cra_flags = CRYPTO_ALG_INTERNAL,
1128 .cra_ctxsize = sizeof(struct aesni_rfc4106_gcm_ctx),
1129 .cra_alignmask = AESNI_ALIGN - 1,
1130 .cra_module = THIS_MODULE,
1133 .setkey = generic_gcmaes_set_key,
1134 .setauthsize = generic_gcmaes_set_authsize,
1135 .encrypt = generic_gcmaes_encrypt,
1136 .decrypt = generic_gcmaes_decrypt,
1137 .ivsize = GCM_AES_IV_SIZE,
1140 .cra_name = "__gcm(aes)",
1141 .cra_driver_name = "__generic-gcm-aesni",
1142 .cra_priority = 400,
1143 .cra_flags = CRYPTO_ALG_INTERNAL,
1145 .cra_ctxsize = sizeof(struct generic_gcmaes_ctx),
1146 .cra_alignmask = AESNI_ALIGN - 1,
1147 .cra_module = THIS_MODULE,
1151 static struct aead_alg aesni_aeads[0];
1154 static struct simd_aead_alg *aesni_simd_aeads[ARRAY_SIZE(aesni_aeads)];
1156 static const struct x86_cpu_id aesni_cpu_id[] = {
1157 X86_MATCH_FEATURE(X86_FEATURE_AES, NULL),
1160 MODULE_DEVICE_TABLE(x86cpu, aesni_cpu_id);
1162 static int __init aesni_init(void)
1166 if (!x86_match_cpu(aesni_cpu_id))
1168 #ifdef CONFIG_X86_64
1169 if (boot_cpu_has(X86_FEATURE_AVX2)) {
1170 pr_info("AVX2 version of gcm_enc/dec engaged.\n");
1171 aesni_gcm_tfm = &aesni_gcm_tfm_avx_gen4;
1173 if (boot_cpu_has(X86_FEATURE_AVX)) {
1174 pr_info("AVX version of gcm_enc/dec engaged.\n");
1175 aesni_gcm_tfm = &aesni_gcm_tfm_avx_gen2;
1177 pr_info("SSE version of gcm_enc/dec engaged.\n");
1178 aesni_gcm_tfm = &aesni_gcm_tfm_sse;
1180 aesni_ctr_enc_tfm = aesni_ctr_enc;
1181 if (boot_cpu_has(X86_FEATURE_AVX)) {
1182 /* optimize performance of ctr mode encryption transform */
1183 aesni_ctr_enc_tfm = aesni_ctr_enc_avx_tfm;
1184 pr_info("AES CTR mode by8 optimization enabled\n");
1188 err = crypto_register_alg(&aesni_cipher_alg);
1192 err = simd_register_skciphers_compat(aesni_skciphers,
1193 ARRAY_SIZE(aesni_skciphers),
1194 aesni_simd_skciphers);
1196 goto unregister_cipher;
1198 err = simd_register_aeads_compat(aesni_aeads, ARRAY_SIZE(aesni_aeads),
1201 goto unregister_skciphers;
1205 unregister_skciphers:
1206 simd_unregister_skciphers(aesni_skciphers, ARRAY_SIZE(aesni_skciphers),
1207 aesni_simd_skciphers);
1209 crypto_unregister_alg(&aesni_cipher_alg);
1213 static void __exit aesni_exit(void)
1215 simd_unregister_aeads(aesni_aeads, ARRAY_SIZE(aesni_aeads),
1217 simd_unregister_skciphers(aesni_skciphers, ARRAY_SIZE(aesni_skciphers),
1218 aesni_simd_skciphers);
1219 crypto_unregister_alg(&aesni_cipher_alg);
1222 late_initcall(aesni_init);
1223 module_exit(aesni_exit);
1225 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm, Intel AES-NI instructions optimized");
1226 MODULE_LICENSE("GPL");
1227 MODULE_ALIAS_CRYPTO("aes");