23 #include <linux/types.h>
25 #include <linux/module.h>
28 #include <crypto/aes.h>
43 #if defined(CONFIG_CRYPTO_CTR) || defined(CONFIG_CRYPTO_CTR_MODULE)
47 #if defined(CONFIG_CRYPTO_PCBC) || defined(CONFIG_CRYPTO_PCBC_MODULE)
73 #define AESNI_ALIGN (16)
74 #define AES_BLOCK_MASK (~(AES_BLOCK_SIZE-1))
75 #define RFC4106_HASH_SUBKEY_SIZE 16
94 const u8 *
in,
unsigned int len);
96 const u8 *
in,
unsigned int len);
98 const u8 *
in,
unsigned int len,
u8 *
iv);
100 const u8 *
in,
unsigned int len,
u8 *
iv);
107 const u8 *
in,
unsigned int len,
u8 *
iv);
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);
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);
157 crypto_tfm_ctx(crypto_aead_tfm(tfm)),
AESNI_ALIGN);
163 unsigned long addr = (
unsigned long)raw_ctx;
166 if (align <= crypto_tfm_ctx_alignment())
171 static int aes_set_key_common(
struct crypto_tfm *tfm,
void *raw_ctx,
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);
214 static void aes_decrypt(
struct crypto_tfm *tfm,
u8 *dst,
const u8 *src)
227 static void __aes_encrypt(
struct crypto_tfm *tfm,
u8 *dst,
const u8 *src)
234 static void __aes_decrypt(
struct crypto_tfm *tfm,
u8 *dst,
const u8 *src)
249 blkcipher_walk_init(&walk, dst, src, nbytes);
254 while ((nbytes = walk.nbytes)) {
273 blkcipher_walk_init(&walk, dst, src, nbytes);
278 while ((nbytes = walk.nbytes)) {
297 blkcipher_walk_init(&walk, dst, src, nbytes);
302 while ((nbytes = walk.nbytes)) {
321 blkcipher_walk_init(&walk, dst, src, nbytes);
326 while ((nbytes = walk.nbytes)) {
341 u8 *ctrblk = walk->
iv;
345 unsigned int nbytes = walk->
nbytes;
349 memcpy(dst, keystream, nbytes);
361 blkcipher_walk_init(&walk, dst, src, nbytes);
367 aesni_ctr_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr,
373 ctr_crypt_final(ctx, &walk);
382 static int ablk_ecb_init(
struct crypto_tfm *tfm)
387 static int ablk_cbc_init(
struct crypto_tfm *tfm)
393 static int ablk_ctr_init(
struct crypto_tfm *tfm)
399 static int ablk_rfc3686_ctr_init(
struct crypto_tfm *tfm)
407 static int ablk_pcbc_init(
struct crypto_tfm *tfm)
413 static void lrw_xts_encrypt_callback(
void *ctx,
u8 *blks,
unsigned int nbytes)
418 static void lrw_xts_decrypt_callback(
void *ctx,
u8 *blks,
unsigned int nbytes)
429 err = aes_set_key_common(tfm, ctx->
raw_aes_ctx, key,
437 static void lrw_aesni_exit_tfm(
struct crypto_tfm *tfm)
451 .tbuflen =
sizeof(
buf),
455 .crypt_fn = lrw_xts_encrypt_callback,
462 ret =
lrw_crypt(desc, dst, src, nbytes, &req);
475 .tbuflen =
sizeof(
buf),
479 .crypt_fn = lrw_xts_decrypt_callback,
486 ret =
lrw_crypt(desc, dst, src, nbytes, &req);
492 static int xts_aesni_setkey(
struct crypto_tfm *tfm,
const u8 *key,
508 err = aes_set_key_common(tfm, ctx->
raw_crypt_ctx, key, keylen / 2);
513 return aes_set_key_common(tfm, ctx->
raw_tweak_ctx, key + keylen / 2,
518 static void aesni_xts_tweak(
void *ctx,
u8 *
out,
const u8 *
in)
530 .tbuflen =
sizeof(
buf),
533 .tweak_fn = aesni_xts_tweak,
535 .crypt_fn = lrw_xts_encrypt_callback,
542 ret =
xts_crypt(desc, dst, src, nbytes, &req);
555 .tbuflen =
sizeof(
buf),
558 .tweak_fn = aesni_xts_tweak,
560 .crypt_fn = lrw_xts_decrypt_callback,
567 ret =
xts_crypt(desc, dst, src, nbytes, &req);
574 static int rfc4106_init(
struct crypto_tfm *tfm)
582 if (IS_ERR(cryptd_tfm))
583 return PTR_ERR(cryptd_tfm);
586 child_ctx = aesni_rfc4106_gcm_ctx_get(cryptd_child);
587 memcpy(child_ctx, ctx,
sizeof(*ctx));
590 + crypto_aead_reqsize(&cryptd_tfm->
base);
594 static void rfc4106_exit(
struct crypto_tfm *tfm)
616 rfc4106_set_hash_subkey(
u8 *hash_subkey,
const u8 *key,
unsigned int key_len)
625 return PTR_ERR(ctr_tfm);
627 crypto_ablkcipher_clear_flags(ctr_tfm, ~0);
629 ret = crypto_ablkcipher_setkey(ctr_tfm, key, key_len);
631 goto out_free_ablkcipher;
634 req = ablkcipher_request_alloc(ctr_tfm,
GFP_KERNEL);
636 goto out_free_ablkcipher;
640 goto out_free_request;
642 memset(req_data->
iv, 0,
sizeof(req_data->
iv));
648 init_completion(&req_data->
result.completion);
650 ablkcipher_request_set_tfm(req, ctr_tfm);
653 rfc4106_set_hash_subkey_done,
656 ablkcipher_request_set_crypt(req, &req_data->
sg,
659 ret = crypto_ablkcipher_encrypt(req);
662 (&req_data->
result.completion);
664 ret = req_data->
result.err;
668 ablkcipher_request_free(req);
670 crypto_free_ablkcipher(ctr_tfm);
674 static int rfc4106_set_key(
struct crypto_aead *parent,
const u8 *key,
675 unsigned int key_len)
678 struct crypto_tfm *tfm = crypto_aead_tfm(parent);
682 aesni_rfc4106_gcm_ctx_get(cryptd_child);
683 u8 *new_key_align, *new_key_mem =
NULL;
707 new_key_align =
PTR_ALIGN(new_key_mem, AESNI_ALIGN);
708 memcpy(new_key_align, key, key_len);
721 if ((
unsigned long)(&(ctx->
hash_subkey[0])) % AESNI_ALIGN) {
725 ret = rfc4106_set_hash_subkey(ctx->
hash_subkey, key, key_len);
726 memcpy(child_ctx, ctx,
sizeof(*ctx));
734 static int rfc4106_set_authsize(
struct crypto_aead *parent,
735 unsigned int authsize)
748 crypto_aead_crt(parent)->authsize = authsize;
749 crypto_aead_crt(cryptd_child)->authsize = authsize;
762 memcpy(cryptd_req, req,
sizeof(*req));
763 aead_request_set_tfm(cryptd_req, &ctx->
cryptd_tfm->base);
764 return crypto_aead_encrypt(cryptd_req);
768 ret = cryptd_child->
base.crt_aead.encrypt(req);
783 memcpy(cryptd_req, req,
sizeof(*req));
784 aead_request_set_tfm(cryptd_req, &ctx->
cryptd_tfm->base);
785 return crypto_aead_decrypt(cryptd_req);
789 ret = cryptd_child->
base.crt_aead.decrypt(req);
795 static int __driver_rfc4106_encrypt(
struct aead_request *req)
797 u8 one_entry_in_sg = 0;
803 unsigned long auth_tag_len = crypto_aead_authsize(tfm);
817 for (i = 0; i < 4; i++)
819 for (i = 0; i < 8; i++)
820 *(iv+4+i) = req->
iv[
i];
821 *((
__be32 *)(iv+12)) = counter;
841 assoc = (src + req->
cryptlen + auth_tag_len);
848 aesni_gcm_enc(aes_ctx, dst, src, (
unsigned long)req->
cryptlen, iv,
850 + ((
unsigned long)req->
cryptlen), auth_tag_len);
854 if (one_entry_in_sg) {
856 scatterwalk_unmap(dst);
859 scatterwalk_unmap(src);
860 scatterwalk_unmap(assoc);
871 static int __driver_rfc4106_decrypt(
struct aead_request *req)
873 u8 one_entry_in_sg = 0;
875 unsigned long tempCipherLen = 0;
881 unsigned long auth_tag_len = crypto_aead_authsize(tfm);
884 u8 *authTag = iv + 16;
897 tempCipherLen = (
unsigned long)(req->
cryptlen - auth_tag_len);
899 for (i = 0; i < 4; i++)
901 for (i = 0; i < 8; i++)
902 *(iv+4+i) = req->
iv[
i];
903 *((
__be32 *)(iv+12)) = counter;
922 assoc = (src + req->
cryptlen + auth_tag_len);
929 aesni_gcm_dec(aes_ctx, dst, src, tempCipherLen, iv,
931 authTag, auth_tag_len);
934 retval =
memcmp(src + tempCipherLen, authTag, auth_tag_len) ?
937 if (one_entry_in_sg) {
939 scatterwalk_unmap(dst);
942 scatterwalk_unmap(src);
943 scatterwalk_unmap(assoc);
956 .cra_driver_name =
"aes-aesni",
968 .cia_setkey = aes_set_key,
969 .cia_encrypt = aes_encrypt,
970 .cia_decrypt = aes_decrypt
974 .cra_name =
"__aes-aesni",
975 .cra_driver_name =
"__driver-aes-aesni",
987 .cia_setkey = aes_set_key,
988 .cia_encrypt = __aes_encrypt,
989 .cia_decrypt = __aes_decrypt
993 .cra_name =
"__ecb-aes-aesni",
994 .cra_driver_name =
"__driver-ecb-aes-aesni",
1007 .setkey = aes_set_key,
1008 .encrypt = ecb_encrypt,
1009 .decrypt = ecb_decrypt,
1013 .cra_name =
"__cbc-aes-aesni",
1014 .cra_driver_name =
"__driver-cbc-aes-aesni",
1027 .setkey = aes_set_key,
1028 .encrypt = cbc_encrypt,
1029 .decrypt = cbc_decrypt,
1033 .cra_name =
"ecb(aes)",
1034 .cra_driver_name =
"ecb-aes-aesni",
1035 .cra_priority = 400,
1042 .cra_init = ablk_ecb_init,
1054 .cra_name =
"cbc(aes)",
1055 .cra_driver_name =
"cbc-aes-aesni",
1056 .cra_priority = 400,
1063 .cra_init = ablk_cbc_init,
1075 #ifdef CONFIG_X86_64
1077 .cra_name =
"__ctr-aes-aesni",
1078 .cra_driver_name =
"__driver-ctr-aes-aesni",
1092 .setkey = aes_set_key,
1093 .encrypt = ctr_crypt,
1094 .decrypt = ctr_crypt,
1098 .cra_name =
"ctr(aes)",
1099 .cra_driver_name =
"ctr-aes-aesni",
1100 .cra_priority = 400,
1107 .cra_init = ablk_ctr_init,
1121 .cra_name =
"__gcm-aes-aesni",
1122 .cra_driver_name =
"__driver-gcm-aes-aesni",
1133 .encrypt = __driver_rfc4106_encrypt,
1134 .decrypt = __driver_rfc4106_decrypt,
1138 .cra_name =
"rfc4106(gcm(aes))",
1139 .cra_driver_name =
"rfc4106-gcm-aesni",
1140 .cra_priority = 400,
1148 .cra_init = rfc4106_init,
1149 .cra_exit = rfc4106_exit,
1152 .setkey = rfc4106_set_key,
1153 .setauthsize = rfc4106_set_authsize,
1154 .encrypt = rfc4106_encrypt,
1155 .decrypt = rfc4106_decrypt,
1163 .cra_name =
"rfc3686(ctr(aes))",
1164 .cra_driver_name =
"rfc3686-ctr-aes-aesni",
1165 .cra_priority = 400,
1172 .cra_init = ablk_rfc3686_ctr_init,
1191 .cra_name =
"pcbc(aes)",
1192 .cra_driver_name =
"pcbc-aes-aesni",
1193 .cra_priority = 400,
1200 .cra_init = ablk_pcbc_init,
1214 .cra_name =
"__lrw-aes-aesni",
1215 .cra_driver_name =
"__driver-lrw-aes-aesni",
1223 .cra_exit = lrw_aesni_exit_tfm,
1229 .setkey = lrw_aesni_setkey,
1230 .encrypt = lrw_encrypt,
1231 .decrypt = lrw_decrypt,
1235 .cra_name =
"__xts-aes-aesni",
1236 .cra_driver_name =
"__driver-xts-aes-aesni",
1249 .setkey = xts_aesni_setkey,
1250 .encrypt = xts_encrypt,
1251 .decrypt = xts_decrypt,
1255 .cra_name =
"lrw(aes)",
1256 .cra_driver_name =
"lrw-aes-aesni",
1257 .cra_priority = 400,
1277 .cra_name =
"xts(aes)",
1278 .cra_driver_name =
"xts-aes-aesni",
1279 .cra_priority = 400,
1301 static const struct x86_cpu_id aesni_cpu_id[] = {
1307 static int __init aesni_init(
void)
1321 static void __exit aesni_exit(
void)
1331 MODULE_DESCRIPTION(
"Rijndael (AES) Cipher Algorithm, Intel AES-NI instructions optimized");