2 * Support for Intel AES-NI instructions. This file contains glue
3 * code, the real AES implementation is in intel-aes_asm.S.
5 * Copyright (C) 2008, Intel Corp.
6 * Author: Huang Ying <ying.huang@intel.com>
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.
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.
22 #include <linux/hardirq.h>
23 #include <linux/types.h>
24 #include <linux/crypto.h>
25 #include <linux/module.h>
26 #include <linux/err.h>
27 #include <crypto/algapi.h>
28 #include <crypto/aes.h>
29 #include <crypto/cryptd.h>
30 #include <crypto/ctr.h>
31 #include <crypto/b128ops.h>
32 #include <crypto/lrw.h>
33 #include <crypto/xts.h>
34 #include <asm/cpu_device_id.h>
36 #include <asm/crypto/aes.h>
37 #include <asm/crypto/ablk_helper.h>
38 #include <crypto/scatterwalk.h>
39 #include <crypto/internal/aead.h>
40 #include <linux/workqueue.h>
41 #include <linux/spinlock.h>
43 #if defined(CONFIG_CRYPTO_CTR) || defined(CONFIG_CRYPTO_CTR_MODULE)
47 #if defined(CONFIG_CRYPTO_PCBC) || defined(CONFIG_CRYPTO_PCBC_MODULE)
51 /* This data is stored at the end of the crypto_tfm struct.
52 * It's a type of per "session" data storage location.
53 * This needs to be 16 byte aligned.
55 struct aesni_rfc4106_gcm_ctx {
57 struct crypto_aes_ctx aes_key_expanded;
59 struct cryptd_aead *cryptd_tfm;
62 struct aesni_gcm_set_hash_subkey_result {
64 struct completion completion;
67 struct aesni_hash_subkey_req_data {
69 struct aesni_gcm_set_hash_subkey_result result;
70 struct scatterlist sg;
73 #define AESNI_ALIGN (16)
74 #define AES_BLOCK_MASK (~(AES_BLOCK_SIZE-1))
75 #define RFC4106_HASH_SUBKEY_SIZE 16
77 struct aesni_lrw_ctx {
78 struct lrw_table_ctx lrw_table;
79 u8 raw_aes_ctx[sizeof(struct crypto_aes_ctx) + AESNI_ALIGN - 1];
82 struct aesni_xts_ctx {
83 u8 raw_tweak_ctx[sizeof(struct crypto_aes_ctx) + AESNI_ALIGN - 1];
84 u8 raw_crypt_ctx[sizeof(struct crypto_aes_ctx) + AESNI_ALIGN - 1];
87 asmlinkage int aesni_set_key(struct crypto_aes_ctx *ctx, const u8 *in_key,
88 unsigned int key_len);
89 asmlinkage void aesni_enc(struct crypto_aes_ctx *ctx, u8 *out,
91 asmlinkage void aesni_dec(struct crypto_aes_ctx *ctx, u8 *out,
93 asmlinkage void aesni_ecb_enc(struct crypto_aes_ctx *ctx, u8 *out,
94 const u8 *in, unsigned int len);
95 asmlinkage void aesni_ecb_dec(struct crypto_aes_ctx *ctx, u8 *out,
96 const u8 *in, unsigned int len);
97 asmlinkage void aesni_cbc_enc(struct crypto_aes_ctx *ctx, u8 *out,
98 const u8 *in, unsigned int len, u8 *iv);
99 asmlinkage void aesni_cbc_dec(struct crypto_aes_ctx *ctx, u8 *out,
100 const u8 *in, unsigned int len, u8 *iv);
102 int crypto_fpu_init(void);
103 void crypto_fpu_exit(void);
106 asmlinkage void aesni_ctr_enc(struct crypto_aes_ctx *ctx, u8 *out,
107 const u8 *in, unsigned int len, u8 *iv);
109 /* asmlinkage void aesni_gcm_enc()
110 * void *ctx, AES Key schedule. Starts on a 16 byte boundary.
111 * u8 *out, Ciphertext output. Encrypt in-place is allowed.
112 * const u8 *in, Plaintext input
113 * unsigned long plaintext_len, Length of data in bytes for encryption.
114 * u8 *iv, Pre-counter block j0: 4 byte salt (from Security Association)
115 * concatenated with 8 byte Initialisation Vector (from IPSec ESP
116 * Payload) concatenated with 0x00000001. 16-byte aligned pointer.
117 * u8 *hash_subkey, the Hash sub key input. Data starts on a 16-byte boundary.
118 * const u8 *aad, Additional Authentication Data (AAD)
119 * unsigned long aad_len, Length of AAD in bytes. With RFC4106 this
120 * is going to be 8 or 12 bytes
121 * u8 *auth_tag, Authenticated Tag output.
122 * unsigned long auth_tag_len), Authenticated Tag Length in bytes.
123 * Valid values are 16 (most likely), 12 or 8.
125 asmlinkage void aesni_gcm_enc(void *ctx, u8 *out,
126 const u8 *in, unsigned long plaintext_len, u8 *iv,
127 u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
128 u8 *auth_tag, unsigned long auth_tag_len);
130 /* asmlinkage void aesni_gcm_dec()
131 * void *ctx, AES Key schedule. Starts on a 16 byte boundary.
132 * u8 *out, Plaintext output. Decrypt in-place is allowed.
133 * const u8 *in, Ciphertext input
134 * unsigned long ciphertext_len, Length of data in bytes for decryption.
135 * u8 *iv, Pre-counter block j0: 4 byte salt (from Security Association)
136 * concatenated with 8 byte Initialisation Vector (from IPSec ESP
137 * Payload) concatenated with 0x00000001. 16-byte aligned pointer.
138 * u8 *hash_subkey, the Hash sub key input. Data starts on a 16-byte boundary.
139 * const u8 *aad, Additional Authentication Data (AAD)
140 * unsigned long aad_len, Length of AAD in bytes. With RFC4106 this is going
141 * to be 8 or 12 bytes
142 * u8 *auth_tag, Authenticated Tag output.
143 * unsigned long auth_tag_len) Authenticated Tag Length in bytes.
144 * Valid values are 16 (most likely), 12 or 8.
146 asmlinkage void aesni_gcm_dec(void *ctx, u8 *out,
147 const u8 *in, unsigned long ciphertext_len, u8 *iv,
148 u8 *hash_subkey, const u8 *aad, unsigned long aad_len,
149 u8 *auth_tag, unsigned long auth_tag_len);
152 aesni_rfc4106_gcm_ctx *aesni_rfc4106_gcm_ctx_get(struct crypto_aead *tfm)
155 (struct aesni_rfc4106_gcm_ctx *)
157 crypto_tfm_ctx(crypto_aead_tfm(tfm)), AESNI_ALIGN);
161 static inline struct crypto_aes_ctx *aes_ctx(void *raw_ctx)
163 unsigned long addr = (unsigned long)raw_ctx;
164 unsigned long align = AESNI_ALIGN;
166 if (align <= crypto_tfm_ctx_alignment())
168 return (struct crypto_aes_ctx *)ALIGN(addr, align);
171 static int aes_set_key_common(struct crypto_tfm *tfm, void *raw_ctx,
172 const u8 *in_key, unsigned int key_len)
174 struct crypto_aes_ctx *ctx = aes_ctx(raw_ctx);
175 u32 *flags = &tfm->crt_flags;
178 if (key_len != AES_KEYSIZE_128 && key_len != AES_KEYSIZE_192 &&
179 key_len != AES_KEYSIZE_256) {
180 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
184 if (!irq_fpu_usable())
185 err = crypto_aes_expand_key(ctx, in_key, key_len);
188 err = aesni_set_key(ctx, in_key, key_len);
195 static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
196 unsigned int key_len)
198 return aes_set_key_common(tfm, crypto_tfm_ctx(tfm), in_key, key_len);
201 static void aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
203 struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
205 if (!irq_fpu_usable())
206 crypto_aes_encrypt_x86(ctx, dst, src);
209 aesni_enc(ctx, dst, src);
214 static void aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
216 struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
218 if (!irq_fpu_usable())
219 crypto_aes_decrypt_x86(ctx, dst, src);
222 aesni_dec(ctx, dst, src);
227 static void __aes_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
229 struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
231 aesni_enc(ctx, dst, src);
234 static void __aes_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
236 struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm));
238 aesni_dec(ctx, dst, src);
241 static int ecb_encrypt(struct blkcipher_desc *desc,
242 struct scatterlist *dst, struct scatterlist *src,
245 struct crypto_aes_ctx *ctx = aes_ctx(crypto_blkcipher_ctx(desc->tfm));
246 struct blkcipher_walk walk;
249 blkcipher_walk_init(&walk, dst, src, nbytes);
250 err = blkcipher_walk_virt(desc, &walk);
251 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
254 while ((nbytes = walk.nbytes)) {
255 aesni_ecb_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
256 nbytes & AES_BLOCK_MASK);
257 nbytes &= AES_BLOCK_SIZE - 1;
258 err = blkcipher_walk_done(desc, &walk, nbytes);
265 static int ecb_decrypt(struct blkcipher_desc *desc,
266 struct scatterlist *dst, struct scatterlist *src,
269 struct crypto_aes_ctx *ctx = aes_ctx(crypto_blkcipher_ctx(desc->tfm));
270 struct blkcipher_walk walk;
273 blkcipher_walk_init(&walk, dst, src, nbytes);
274 err = blkcipher_walk_virt(desc, &walk);
275 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
278 while ((nbytes = walk.nbytes)) {
279 aesni_ecb_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
280 nbytes & AES_BLOCK_MASK);
281 nbytes &= AES_BLOCK_SIZE - 1;
282 err = blkcipher_walk_done(desc, &walk, nbytes);
289 static int cbc_encrypt(struct blkcipher_desc *desc,
290 struct scatterlist *dst, struct scatterlist *src,
293 struct crypto_aes_ctx *ctx = aes_ctx(crypto_blkcipher_ctx(desc->tfm));
294 struct blkcipher_walk walk;
297 blkcipher_walk_init(&walk, dst, src, nbytes);
298 err = blkcipher_walk_virt(desc, &walk);
299 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
302 while ((nbytes = walk.nbytes)) {
303 aesni_cbc_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
304 nbytes & AES_BLOCK_MASK, walk.iv);
305 nbytes &= AES_BLOCK_SIZE - 1;
306 err = blkcipher_walk_done(desc, &walk, nbytes);
313 static int cbc_decrypt(struct blkcipher_desc *desc,
314 struct scatterlist *dst, struct scatterlist *src,
317 struct crypto_aes_ctx *ctx = aes_ctx(crypto_blkcipher_ctx(desc->tfm));
318 struct blkcipher_walk walk;
321 blkcipher_walk_init(&walk, dst, src, nbytes);
322 err = blkcipher_walk_virt(desc, &walk);
323 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
326 while ((nbytes = walk.nbytes)) {
327 aesni_cbc_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr,
328 nbytes & AES_BLOCK_MASK, walk.iv);
329 nbytes &= AES_BLOCK_SIZE - 1;
330 err = blkcipher_walk_done(desc, &walk, nbytes);
338 static void ctr_crypt_final(struct crypto_aes_ctx *ctx,
339 struct blkcipher_walk *walk)
341 u8 *ctrblk = walk->iv;
342 u8 keystream[AES_BLOCK_SIZE];
343 u8 *src = walk->src.virt.addr;
344 u8 *dst = walk->dst.virt.addr;
345 unsigned int nbytes = walk->nbytes;
347 aesni_enc(ctx, keystream, ctrblk);
348 crypto_xor(keystream, src, nbytes);
349 memcpy(dst, keystream, nbytes);
350 crypto_inc(ctrblk, AES_BLOCK_SIZE);
353 static int ctr_crypt(struct blkcipher_desc *desc,
354 struct scatterlist *dst, struct scatterlist *src,
357 struct crypto_aes_ctx *ctx = aes_ctx(crypto_blkcipher_ctx(desc->tfm));
358 struct blkcipher_walk walk;
361 blkcipher_walk_init(&walk, dst, src, nbytes);
362 err = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE);
363 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
366 while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
367 aesni_ctr_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
368 nbytes & AES_BLOCK_MASK, walk.iv);
369 nbytes &= AES_BLOCK_SIZE - 1;
370 err = blkcipher_walk_done(desc, &walk, nbytes);
373 ctr_crypt_final(ctx, &walk);
374 err = blkcipher_walk_done(desc, &walk, 0);
382 static int ablk_ecb_init(struct crypto_tfm *tfm)
384 return ablk_init_common(tfm, "__driver-ecb-aes-aesni");
387 static int ablk_cbc_init(struct crypto_tfm *tfm)
389 return ablk_init_common(tfm, "__driver-cbc-aes-aesni");
393 static int ablk_ctr_init(struct crypto_tfm *tfm)
395 return ablk_init_common(tfm, "__driver-ctr-aes-aesni");
399 static int ablk_rfc3686_ctr_init(struct crypto_tfm *tfm)
401 return ablk_init_common(tfm, "rfc3686(__driver-ctr-aes-aesni)");
407 static int ablk_pcbc_init(struct crypto_tfm *tfm)
409 return ablk_init_common(tfm, "fpu(pcbc(__driver-aes-aesni))");
413 static void lrw_xts_encrypt_callback(void *ctx, u8 *blks, unsigned int nbytes)
415 aesni_ecb_enc(ctx, blks, blks, nbytes);
418 static void lrw_xts_decrypt_callback(void *ctx, u8 *blks, unsigned int nbytes)
420 aesni_ecb_dec(ctx, blks, blks, nbytes);
423 static int lrw_aesni_setkey(struct crypto_tfm *tfm, const u8 *key,
426 struct aesni_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
429 err = aes_set_key_common(tfm, ctx->raw_aes_ctx, key,
430 keylen - AES_BLOCK_SIZE);
434 return lrw_init_table(&ctx->lrw_table, key + keylen - AES_BLOCK_SIZE);
437 static void lrw_aesni_exit_tfm(struct crypto_tfm *tfm)
439 struct aesni_lrw_ctx *ctx = crypto_tfm_ctx(tfm);
441 lrw_free_table(&ctx->lrw_table);
444 static int lrw_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
445 struct scatterlist *src, unsigned int nbytes)
447 struct aesni_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
449 struct lrw_crypt_req req = {
451 .tbuflen = sizeof(buf),
453 .table_ctx = &ctx->lrw_table,
454 .crypt_ctx = aes_ctx(ctx->raw_aes_ctx),
455 .crypt_fn = lrw_xts_encrypt_callback,
459 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
462 ret = lrw_crypt(desc, dst, src, nbytes, &req);
468 static int lrw_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
469 struct scatterlist *src, unsigned int nbytes)
471 struct aesni_lrw_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
473 struct lrw_crypt_req req = {
475 .tbuflen = sizeof(buf),
477 .table_ctx = &ctx->lrw_table,
478 .crypt_ctx = aes_ctx(ctx->raw_aes_ctx),
479 .crypt_fn = lrw_xts_decrypt_callback,
483 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
486 ret = lrw_crypt(desc, dst, src, nbytes, &req);
492 static int xts_aesni_setkey(struct crypto_tfm *tfm, const u8 *key,
495 struct aesni_xts_ctx *ctx = crypto_tfm_ctx(tfm);
496 u32 *flags = &tfm->crt_flags;
499 /* key consists of keys of equal size concatenated, therefore
500 * the length must be even
503 *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
507 /* first half of xts-key is for crypt */
508 err = aes_set_key_common(tfm, ctx->raw_crypt_ctx, key, keylen / 2);
512 /* second half of xts-key is for tweak */
513 return aes_set_key_common(tfm, ctx->raw_tweak_ctx, key + keylen / 2,
518 static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
519 struct scatterlist *src, unsigned int nbytes)
521 struct aesni_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
523 struct xts_crypt_req req = {
525 .tbuflen = sizeof(buf),
527 .tweak_ctx = aes_ctx(ctx->raw_tweak_ctx),
528 .tweak_fn = XTS_TWEAK_CAST(aesni_enc),
529 .crypt_ctx = aes_ctx(ctx->raw_crypt_ctx),
530 .crypt_fn = lrw_xts_encrypt_callback,
534 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
537 ret = xts_crypt(desc, dst, src, nbytes, &req);
543 static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
544 struct scatterlist *src, unsigned int nbytes)
546 struct aesni_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
548 struct xts_crypt_req req = {
550 .tbuflen = sizeof(buf),
552 .tweak_ctx = aes_ctx(ctx->raw_tweak_ctx),
553 .tweak_fn = XTS_TWEAK_CAST(aesni_enc),
554 .crypt_ctx = aes_ctx(ctx->raw_crypt_ctx),
555 .crypt_fn = lrw_xts_decrypt_callback,
559 desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
562 ret = xts_crypt(desc, dst, src, nbytes, &req);
569 static int rfc4106_init(struct crypto_tfm *tfm)
571 struct cryptd_aead *cryptd_tfm;
572 struct aesni_rfc4106_gcm_ctx *ctx = (struct aesni_rfc4106_gcm_ctx *)
573 PTR_ALIGN((u8 *)crypto_tfm_ctx(tfm), AESNI_ALIGN);
574 struct crypto_aead *cryptd_child;
575 struct aesni_rfc4106_gcm_ctx *child_ctx;
576 cryptd_tfm = cryptd_alloc_aead("__driver-gcm-aes-aesni", 0, 0);
577 if (IS_ERR(cryptd_tfm))
578 return PTR_ERR(cryptd_tfm);
580 cryptd_child = cryptd_aead_child(cryptd_tfm);
581 child_ctx = aesni_rfc4106_gcm_ctx_get(cryptd_child);
582 memcpy(child_ctx, ctx, sizeof(*ctx));
583 ctx->cryptd_tfm = cryptd_tfm;
584 tfm->crt_aead.reqsize = sizeof(struct aead_request)
585 + crypto_aead_reqsize(&cryptd_tfm->base);
589 static void rfc4106_exit(struct crypto_tfm *tfm)
591 struct aesni_rfc4106_gcm_ctx *ctx =
592 (struct aesni_rfc4106_gcm_ctx *)
593 PTR_ALIGN((u8 *)crypto_tfm_ctx(tfm), AESNI_ALIGN);
594 if (!IS_ERR(ctx->cryptd_tfm))
595 cryptd_free_aead(ctx->cryptd_tfm);
600 rfc4106_set_hash_subkey_done(struct crypto_async_request *req, int err)
602 struct aesni_gcm_set_hash_subkey_result *result = req->data;
604 if (err == -EINPROGRESS)
607 complete(&result->completion);
611 rfc4106_set_hash_subkey(u8 *hash_subkey, const u8 *key, unsigned int key_len)
613 struct crypto_ablkcipher *ctr_tfm;
614 struct ablkcipher_request *req;
616 struct aesni_hash_subkey_req_data *req_data;
618 ctr_tfm = crypto_alloc_ablkcipher("ctr(aes)", 0, 0);
620 return PTR_ERR(ctr_tfm);
622 crypto_ablkcipher_clear_flags(ctr_tfm, ~0);
624 ret = crypto_ablkcipher_setkey(ctr_tfm, key, key_len);
626 goto out_free_ablkcipher;
629 req = ablkcipher_request_alloc(ctr_tfm, GFP_KERNEL);
631 goto out_free_ablkcipher;
633 req_data = kmalloc(sizeof(*req_data), GFP_KERNEL);
635 goto out_free_request;
637 memset(req_data->iv, 0, sizeof(req_data->iv));
639 /* Clear the data in the hash sub key container to zero.*/
640 /* We want to cipher all zeros to create the hash sub key. */
641 memset(hash_subkey, 0, RFC4106_HASH_SUBKEY_SIZE);
643 init_completion(&req_data->result.completion);
644 sg_init_one(&req_data->sg, hash_subkey, RFC4106_HASH_SUBKEY_SIZE);
645 ablkcipher_request_set_tfm(req, ctr_tfm);
646 ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP |
647 CRYPTO_TFM_REQ_MAY_BACKLOG,
648 rfc4106_set_hash_subkey_done,
651 ablkcipher_request_set_crypt(req, &req_data->sg,
652 &req_data->sg, RFC4106_HASH_SUBKEY_SIZE, req_data->iv);
654 ret = crypto_ablkcipher_encrypt(req);
655 if (ret == -EINPROGRESS || ret == -EBUSY) {
656 ret = wait_for_completion_interruptible
657 (&req_data->result.completion);
659 ret = req_data->result.err;
663 ablkcipher_request_free(req);
665 crypto_free_ablkcipher(ctr_tfm);
669 static int rfc4106_set_key(struct crypto_aead *parent, const u8 *key,
670 unsigned int key_len)
673 struct crypto_tfm *tfm = crypto_aead_tfm(parent);
674 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(parent);
675 struct crypto_aead *cryptd_child = cryptd_aead_child(ctx->cryptd_tfm);
676 struct aesni_rfc4106_gcm_ctx *child_ctx =
677 aesni_rfc4106_gcm_ctx_get(cryptd_child);
678 u8 *new_key_align, *new_key_mem = NULL;
681 crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
684 /*Account for 4 byte nonce at the end.*/
686 if (key_len != AES_KEYSIZE_128) {
687 crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
691 memcpy(ctx->nonce, key + key_len, sizeof(ctx->nonce));
692 /*This must be on a 16 byte boundary!*/
693 if ((unsigned long)(&(ctx->aes_key_expanded.key_enc[0])) % AESNI_ALIGN)
696 if ((unsigned long)key % AESNI_ALIGN) {
697 /*key is not aligned: use an auxuliar aligned pointer*/
698 new_key_mem = kmalloc(key_len+AESNI_ALIGN, GFP_KERNEL);
702 new_key_align = PTR_ALIGN(new_key_mem, AESNI_ALIGN);
703 memcpy(new_key_align, key, key_len);
707 if (!irq_fpu_usable())
708 ret = crypto_aes_expand_key(&(ctx->aes_key_expanded),
712 ret = aesni_set_key(&(ctx->aes_key_expanded), key, key_len);
715 /*This must be on a 16 byte boundary!*/
716 if ((unsigned long)(&(ctx->hash_subkey[0])) % AESNI_ALIGN) {
720 ret = rfc4106_set_hash_subkey(ctx->hash_subkey, key, key_len);
721 memcpy(child_ctx, ctx, sizeof(*ctx));
727 /* This is the Integrity Check Value (aka the authentication tag length and can
728 * be 8, 12 or 16 bytes long. */
729 static int rfc4106_set_authsize(struct crypto_aead *parent,
730 unsigned int authsize)
732 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(parent);
733 struct crypto_aead *cryptd_child = cryptd_aead_child(ctx->cryptd_tfm);
743 crypto_aead_crt(parent)->authsize = authsize;
744 crypto_aead_crt(cryptd_child)->authsize = authsize;
748 static int rfc4106_encrypt(struct aead_request *req)
751 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
752 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
754 if (!irq_fpu_usable()) {
755 struct aead_request *cryptd_req =
756 (struct aead_request *) aead_request_ctx(req);
757 memcpy(cryptd_req, req, sizeof(*req));
758 aead_request_set_tfm(cryptd_req, &ctx->cryptd_tfm->base);
759 return crypto_aead_encrypt(cryptd_req);
761 struct crypto_aead *cryptd_child = cryptd_aead_child(ctx->cryptd_tfm);
763 ret = cryptd_child->base.crt_aead.encrypt(req);
769 static int rfc4106_decrypt(struct aead_request *req)
772 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
773 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
775 if (!irq_fpu_usable()) {
776 struct aead_request *cryptd_req =
777 (struct aead_request *) aead_request_ctx(req);
778 memcpy(cryptd_req, req, sizeof(*req));
779 aead_request_set_tfm(cryptd_req, &ctx->cryptd_tfm->base);
780 return crypto_aead_decrypt(cryptd_req);
782 struct crypto_aead *cryptd_child = cryptd_aead_child(ctx->cryptd_tfm);
784 ret = cryptd_child->base.crt_aead.decrypt(req);
790 static int __driver_rfc4106_encrypt(struct aead_request *req)
792 u8 one_entry_in_sg = 0;
793 u8 *src, *dst, *assoc;
794 __be32 counter = cpu_to_be32(1);
795 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
796 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
797 void *aes_ctx = &(ctx->aes_key_expanded);
798 unsigned long auth_tag_len = crypto_aead_authsize(tfm);
799 u8 iv_tab[16+AESNI_ALIGN];
800 u8* iv = (u8 *) PTR_ALIGN((u8 *)iv_tab, AESNI_ALIGN);
801 struct scatter_walk src_sg_walk;
802 struct scatter_walk assoc_sg_walk;
803 struct scatter_walk dst_sg_walk;
806 /* Assuming we are supporting rfc4106 64-bit extended */
807 /* sequence numbers We need to have the AAD length equal */
808 /* to 8 or 12 bytes */
809 if (unlikely(req->assoclen != 8 && req->assoclen != 12))
812 for (i = 0; i < 4; i++)
813 *(iv+i) = ctx->nonce[i];
814 for (i = 0; i < 8; i++)
815 *(iv+4+i) = req->iv[i];
816 *((__be32 *)(iv+12)) = counter;
818 if ((sg_is_last(req->src)) && (sg_is_last(req->assoc))) {
820 scatterwalk_start(&src_sg_walk, req->src);
821 scatterwalk_start(&assoc_sg_walk, req->assoc);
822 src = scatterwalk_map(&src_sg_walk);
823 assoc = scatterwalk_map(&assoc_sg_walk);
825 if (unlikely(req->src != req->dst)) {
826 scatterwalk_start(&dst_sg_walk, req->dst);
827 dst = scatterwalk_map(&dst_sg_walk);
831 /* Allocate memory for src, dst, assoc */
832 src = kmalloc(req->cryptlen + auth_tag_len + req->assoclen,
836 assoc = (src + req->cryptlen + auth_tag_len);
837 scatterwalk_map_and_copy(src, req->src, 0, req->cryptlen, 0);
838 scatterwalk_map_and_copy(assoc, req->assoc, 0,
843 aesni_gcm_enc(aes_ctx, dst, src, (unsigned long)req->cryptlen, iv,
844 ctx->hash_subkey, assoc, (unsigned long)req->assoclen, dst
845 + ((unsigned long)req->cryptlen), auth_tag_len);
847 /* The authTag (aka the Integrity Check Value) needs to be written
848 * back to the packet. */
849 if (one_entry_in_sg) {
850 if (unlikely(req->src != req->dst)) {
851 scatterwalk_unmap(dst);
852 scatterwalk_done(&dst_sg_walk, 0, 0);
854 scatterwalk_unmap(src);
855 scatterwalk_unmap(assoc);
856 scatterwalk_done(&src_sg_walk, 0, 0);
857 scatterwalk_done(&assoc_sg_walk, 0, 0);
859 scatterwalk_map_and_copy(dst, req->dst, 0,
860 req->cryptlen + auth_tag_len, 1);
866 static int __driver_rfc4106_decrypt(struct aead_request *req)
868 u8 one_entry_in_sg = 0;
869 u8 *src, *dst, *assoc;
870 unsigned long tempCipherLen = 0;
871 __be32 counter = cpu_to_be32(1);
873 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
874 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
875 void *aes_ctx = &(ctx->aes_key_expanded);
876 unsigned long auth_tag_len = crypto_aead_authsize(tfm);
877 u8 iv_and_authTag[32+AESNI_ALIGN];
878 u8 *iv = (u8 *) PTR_ALIGN((u8 *)iv_and_authTag, AESNI_ALIGN);
879 u8 *authTag = iv + 16;
880 struct scatter_walk src_sg_walk;
881 struct scatter_walk assoc_sg_walk;
882 struct scatter_walk dst_sg_walk;
885 if (unlikely((req->cryptlen < auth_tag_len) ||
886 (req->assoclen != 8 && req->assoclen != 12)))
888 /* Assuming we are supporting rfc4106 64-bit extended */
889 /* sequence numbers We need to have the AAD length */
890 /* equal to 8 or 12 bytes */
892 tempCipherLen = (unsigned long)(req->cryptlen - auth_tag_len);
894 for (i = 0; i < 4; i++)
895 *(iv+i) = ctx->nonce[i];
896 for (i = 0; i < 8; i++)
897 *(iv+4+i) = req->iv[i];
898 *((__be32 *)(iv+12)) = counter;
900 if ((sg_is_last(req->src)) && (sg_is_last(req->assoc))) {
902 scatterwalk_start(&src_sg_walk, req->src);
903 scatterwalk_start(&assoc_sg_walk, req->assoc);
904 src = scatterwalk_map(&src_sg_walk);
905 assoc = scatterwalk_map(&assoc_sg_walk);
907 if (unlikely(req->src != req->dst)) {
908 scatterwalk_start(&dst_sg_walk, req->dst);
909 dst = scatterwalk_map(&dst_sg_walk);
913 /* Allocate memory for src, dst, assoc */
914 src = kmalloc(req->cryptlen + req->assoclen, GFP_ATOMIC);
917 assoc = (src + req->cryptlen + auth_tag_len);
918 scatterwalk_map_and_copy(src, req->src, 0, req->cryptlen, 0);
919 scatterwalk_map_and_copy(assoc, req->assoc, 0,
924 aesni_gcm_dec(aes_ctx, dst, src, tempCipherLen, iv,
925 ctx->hash_subkey, assoc, (unsigned long)req->assoclen,
926 authTag, auth_tag_len);
928 /* Compare generated tag with passed in tag. */
929 retval = memcmp(src + tempCipherLen, authTag, auth_tag_len) ?
932 if (one_entry_in_sg) {
933 if (unlikely(req->src != req->dst)) {
934 scatterwalk_unmap(dst);
935 scatterwalk_done(&dst_sg_walk, 0, 0);
937 scatterwalk_unmap(src);
938 scatterwalk_unmap(assoc);
939 scatterwalk_done(&src_sg_walk, 0, 0);
940 scatterwalk_done(&assoc_sg_walk, 0, 0);
942 scatterwalk_map_and_copy(dst, req->dst, 0, req->cryptlen, 1);
949 static struct crypto_alg aesni_algs[] = { {
951 .cra_driver_name = "aes-aesni",
953 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
954 .cra_blocksize = AES_BLOCK_SIZE,
955 .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
958 .cra_module = THIS_MODULE,
961 .cia_min_keysize = AES_MIN_KEY_SIZE,
962 .cia_max_keysize = AES_MAX_KEY_SIZE,
963 .cia_setkey = aes_set_key,
964 .cia_encrypt = aes_encrypt,
965 .cia_decrypt = aes_decrypt
969 .cra_name = "__aes-aesni",
970 .cra_driver_name = "__driver-aes-aesni",
972 .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
973 .cra_blocksize = AES_BLOCK_SIZE,
974 .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
977 .cra_module = THIS_MODULE,
980 .cia_min_keysize = AES_MIN_KEY_SIZE,
981 .cia_max_keysize = AES_MAX_KEY_SIZE,
982 .cia_setkey = aes_set_key,
983 .cia_encrypt = __aes_encrypt,
984 .cia_decrypt = __aes_decrypt
988 .cra_name = "__ecb-aes-aesni",
989 .cra_driver_name = "__driver-ecb-aes-aesni",
991 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
992 .cra_blocksize = AES_BLOCK_SIZE,
993 .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
996 .cra_type = &crypto_blkcipher_type,
997 .cra_module = THIS_MODULE,
1000 .min_keysize = AES_MIN_KEY_SIZE,
1001 .max_keysize = AES_MAX_KEY_SIZE,
1002 .setkey = aes_set_key,
1003 .encrypt = ecb_encrypt,
1004 .decrypt = ecb_decrypt,
1008 .cra_name = "__cbc-aes-aesni",
1009 .cra_driver_name = "__driver-cbc-aes-aesni",
1011 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
1012 .cra_blocksize = AES_BLOCK_SIZE,
1013 .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
1016 .cra_type = &crypto_blkcipher_type,
1017 .cra_module = THIS_MODULE,
1020 .min_keysize = AES_MIN_KEY_SIZE,
1021 .max_keysize = AES_MAX_KEY_SIZE,
1022 .setkey = aes_set_key,
1023 .encrypt = cbc_encrypt,
1024 .decrypt = cbc_decrypt,
1028 .cra_name = "ecb(aes)",
1029 .cra_driver_name = "ecb-aes-aesni",
1030 .cra_priority = 400,
1031 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1032 .cra_blocksize = AES_BLOCK_SIZE,
1033 .cra_ctxsize = sizeof(struct async_helper_ctx),
1035 .cra_type = &crypto_ablkcipher_type,
1036 .cra_module = THIS_MODULE,
1037 .cra_init = ablk_ecb_init,
1038 .cra_exit = ablk_exit,
1041 .min_keysize = AES_MIN_KEY_SIZE,
1042 .max_keysize = AES_MAX_KEY_SIZE,
1043 .setkey = ablk_set_key,
1044 .encrypt = ablk_encrypt,
1045 .decrypt = ablk_decrypt,
1049 .cra_name = "cbc(aes)",
1050 .cra_driver_name = "cbc-aes-aesni",
1051 .cra_priority = 400,
1052 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1053 .cra_blocksize = AES_BLOCK_SIZE,
1054 .cra_ctxsize = sizeof(struct async_helper_ctx),
1056 .cra_type = &crypto_ablkcipher_type,
1057 .cra_module = THIS_MODULE,
1058 .cra_init = ablk_cbc_init,
1059 .cra_exit = ablk_exit,
1062 .min_keysize = AES_MIN_KEY_SIZE,
1063 .max_keysize = AES_MAX_KEY_SIZE,
1064 .ivsize = AES_BLOCK_SIZE,
1065 .setkey = ablk_set_key,
1066 .encrypt = ablk_encrypt,
1067 .decrypt = ablk_decrypt,
1070 #ifdef CONFIG_X86_64
1072 .cra_name = "__ctr-aes-aesni",
1073 .cra_driver_name = "__driver-ctr-aes-aesni",
1075 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
1077 .cra_ctxsize = sizeof(struct crypto_aes_ctx) +
1080 .cra_type = &crypto_blkcipher_type,
1081 .cra_module = THIS_MODULE,
1084 .min_keysize = AES_MIN_KEY_SIZE,
1085 .max_keysize = AES_MAX_KEY_SIZE,
1086 .ivsize = AES_BLOCK_SIZE,
1087 .setkey = aes_set_key,
1088 .encrypt = ctr_crypt,
1089 .decrypt = ctr_crypt,
1093 .cra_name = "ctr(aes)",
1094 .cra_driver_name = "ctr-aes-aesni",
1095 .cra_priority = 400,
1096 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1098 .cra_ctxsize = sizeof(struct async_helper_ctx),
1100 .cra_type = &crypto_ablkcipher_type,
1101 .cra_module = THIS_MODULE,
1102 .cra_init = ablk_ctr_init,
1103 .cra_exit = ablk_exit,
1106 .min_keysize = AES_MIN_KEY_SIZE,
1107 .max_keysize = AES_MAX_KEY_SIZE,
1108 .ivsize = AES_BLOCK_SIZE,
1109 .setkey = ablk_set_key,
1110 .encrypt = ablk_encrypt,
1111 .decrypt = ablk_encrypt,
1116 .cra_name = "__gcm-aes-aesni",
1117 .cra_driver_name = "__driver-gcm-aes-aesni",
1119 .cra_flags = CRYPTO_ALG_TYPE_AEAD,
1121 .cra_ctxsize = sizeof(struct aesni_rfc4106_gcm_ctx) +
1124 .cra_type = &crypto_aead_type,
1125 .cra_module = THIS_MODULE,
1128 .encrypt = __driver_rfc4106_encrypt,
1129 .decrypt = __driver_rfc4106_decrypt,
1133 .cra_name = "rfc4106(gcm(aes))",
1134 .cra_driver_name = "rfc4106-gcm-aesni",
1135 .cra_priority = 400,
1136 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1138 .cra_ctxsize = sizeof(struct aesni_rfc4106_gcm_ctx) +
1141 .cra_type = &crypto_nivaead_type,
1142 .cra_module = THIS_MODULE,
1143 .cra_init = rfc4106_init,
1144 .cra_exit = rfc4106_exit,
1147 .setkey = rfc4106_set_key,
1148 .setauthsize = rfc4106_set_authsize,
1149 .encrypt = rfc4106_encrypt,
1150 .decrypt = rfc4106_decrypt,
1158 .cra_name = "rfc3686(ctr(aes))",
1159 .cra_driver_name = "rfc3686-ctr-aes-aesni",
1160 .cra_priority = 400,
1161 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1163 .cra_ctxsize = sizeof(struct async_helper_ctx),
1165 .cra_type = &crypto_ablkcipher_type,
1166 .cra_module = THIS_MODULE,
1167 .cra_init = ablk_rfc3686_ctr_init,
1168 .cra_exit = ablk_exit,
1171 .min_keysize = AES_MIN_KEY_SIZE +
1172 CTR_RFC3686_NONCE_SIZE,
1173 .max_keysize = AES_MAX_KEY_SIZE +
1174 CTR_RFC3686_NONCE_SIZE,
1175 .ivsize = CTR_RFC3686_IV_SIZE,
1176 .setkey = ablk_set_key,
1177 .encrypt = ablk_encrypt,
1178 .decrypt = ablk_decrypt,
1186 .cra_name = "pcbc(aes)",
1187 .cra_driver_name = "pcbc-aes-aesni",
1188 .cra_priority = 400,
1189 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1190 .cra_blocksize = AES_BLOCK_SIZE,
1191 .cra_ctxsize = sizeof(struct async_helper_ctx),
1193 .cra_type = &crypto_ablkcipher_type,
1194 .cra_module = THIS_MODULE,
1195 .cra_init = ablk_pcbc_init,
1196 .cra_exit = ablk_exit,
1199 .min_keysize = AES_MIN_KEY_SIZE,
1200 .max_keysize = AES_MAX_KEY_SIZE,
1201 .ivsize = AES_BLOCK_SIZE,
1202 .setkey = ablk_set_key,
1203 .encrypt = ablk_encrypt,
1204 .decrypt = ablk_decrypt,
1209 .cra_name = "__lrw-aes-aesni",
1210 .cra_driver_name = "__driver-lrw-aes-aesni",
1212 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
1213 .cra_blocksize = AES_BLOCK_SIZE,
1214 .cra_ctxsize = sizeof(struct aesni_lrw_ctx),
1216 .cra_type = &crypto_blkcipher_type,
1217 .cra_module = THIS_MODULE,
1218 .cra_exit = lrw_aesni_exit_tfm,
1221 .min_keysize = AES_MIN_KEY_SIZE + AES_BLOCK_SIZE,
1222 .max_keysize = AES_MAX_KEY_SIZE + AES_BLOCK_SIZE,
1223 .ivsize = AES_BLOCK_SIZE,
1224 .setkey = lrw_aesni_setkey,
1225 .encrypt = lrw_encrypt,
1226 .decrypt = lrw_decrypt,
1230 .cra_name = "__xts-aes-aesni",
1231 .cra_driver_name = "__driver-xts-aes-aesni",
1233 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
1234 .cra_blocksize = AES_BLOCK_SIZE,
1235 .cra_ctxsize = sizeof(struct aesni_xts_ctx),
1237 .cra_type = &crypto_blkcipher_type,
1238 .cra_module = THIS_MODULE,
1241 .min_keysize = 2 * AES_MIN_KEY_SIZE,
1242 .max_keysize = 2 * AES_MAX_KEY_SIZE,
1243 .ivsize = AES_BLOCK_SIZE,
1244 .setkey = xts_aesni_setkey,
1245 .encrypt = xts_encrypt,
1246 .decrypt = xts_decrypt,
1250 .cra_name = "lrw(aes)",
1251 .cra_driver_name = "lrw-aes-aesni",
1252 .cra_priority = 400,
1253 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1254 .cra_blocksize = AES_BLOCK_SIZE,
1255 .cra_ctxsize = sizeof(struct async_helper_ctx),
1257 .cra_type = &crypto_ablkcipher_type,
1258 .cra_module = THIS_MODULE,
1259 .cra_init = ablk_init,
1260 .cra_exit = ablk_exit,
1263 .min_keysize = AES_MIN_KEY_SIZE + AES_BLOCK_SIZE,
1264 .max_keysize = AES_MAX_KEY_SIZE + AES_BLOCK_SIZE,
1265 .ivsize = AES_BLOCK_SIZE,
1266 .setkey = ablk_set_key,
1267 .encrypt = ablk_encrypt,
1268 .decrypt = ablk_decrypt,
1272 .cra_name = "xts(aes)",
1273 .cra_driver_name = "xts-aes-aesni",
1274 .cra_priority = 400,
1275 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1276 .cra_blocksize = AES_BLOCK_SIZE,
1277 .cra_ctxsize = sizeof(struct async_helper_ctx),
1279 .cra_type = &crypto_ablkcipher_type,
1280 .cra_module = THIS_MODULE,
1281 .cra_init = ablk_init,
1282 .cra_exit = ablk_exit,
1285 .min_keysize = 2 * AES_MIN_KEY_SIZE,
1286 .max_keysize = 2 * AES_MAX_KEY_SIZE,
1287 .ivsize = AES_BLOCK_SIZE,
1288 .setkey = ablk_set_key,
1289 .encrypt = ablk_encrypt,
1290 .decrypt = ablk_decrypt,
1296 static const struct x86_cpu_id aesni_cpu_id[] = {
1297 X86_FEATURE_MATCH(X86_FEATURE_AES),
1300 MODULE_DEVICE_TABLE(x86cpu, aesni_cpu_id);
1302 static int __init aesni_init(void)
1306 if (!x86_match_cpu(aesni_cpu_id))
1309 err = crypto_fpu_init();
1313 return crypto_register_algs(aesni_algs, ARRAY_SIZE(aesni_algs));
1316 static void __exit aesni_exit(void)
1318 crypto_unregister_algs(aesni_algs, ARRAY_SIZE(aesni_algs));
1323 module_init(aesni_init);
1324 module_exit(aesni_exit);
1326 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm, Intel AES-NI instructions optimized");
1327 MODULE_LICENSE("GPL");
1328 MODULE_ALIAS("aes");