1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Support for Intel AES-NI instructions. This file contains glue 4 * code, the real AES implementation is in intel-aes_asm.S. 5 * 6 * Copyright (C) 2008, Intel Corp. 7 * Author: Huang Ying <ying.huang@intel.com> 8 * 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. 16 */ 17 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> 29 #include <asm/simd.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> 36 #ifdef CONFIG_X86_64 37 #include <asm/crypto/glue_helper.h> 38 #endif 39 40 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) 48 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. 52 */ 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; 56 u8 nonce[4]; 57 }; 58 59 struct generic_gcmaes_ctx { 60 u8 hash_subkey[16] AESNI_ALIGN_ATTR; 61 struct crypto_aes_ctx aes_key_expanded AESNI_ALIGN_ATTR; 62 }; 63 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; 67 }; 68 69 #define GCM_BLOCK_LEN 16 70 71 struct gcm_context_data { 72 /* init, update and finalize context data */ 73 u8 aad_hash[GCM_BLOCK_LEN]; 74 u64 aad_length; 75 u64 in_length; 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; 80 u64 unused; 81 u8 hash_keys[GCM_BLOCK_LEN * 16]; 82 }; 83 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); 100 101 #define AVX_GEN2_OPTSIZE 640 102 #define AVX_GEN4_OPTSIZE 4096 103 104 #ifdef CONFIG_X86_64 105 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); 110 111 asmlinkage void aesni_xts_crypt8(const struct crypto_aes_ctx *ctx, u8 *out, 112 const u8 *in, bool enc, le128 *iv); 113 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. 128 */ 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); 134 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. 150 */ 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); 156 157 /* Scatter / Gather routines, with args similar to above */ 158 asmlinkage void aesni_gcm_init(void *ctx, 159 struct gcm_context_data *gdata, 160 u8 *iv, 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, 168 const u8 *in, 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); 173 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); 183 } *aesni_gcm_tfm; 184 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, 190 }; 191 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); 198 /* 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. 202 */ 203 asmlinkage void aesni_gcm_init_avx_gen2(void *my_ctx_data, 204 struct gcm_context_data *gdata, 205 u8 *iv, 206 u8 *hash_subkey, 207 const u8 *aad, 208 unsigned long aad_len); 209 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, 215 const u8 *in, 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); 220 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); 226 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); 232 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, 238 }; 239 240 /* 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. 244 */ 245 asmlinkage void aesni_gcm_init_avx_gen4(void *my_ctx_data, 246 struct gcm_context_data *gdata, 247 u8 *iv, 248 u8 *hash_subkey, 249 const u8 *aad, 250 unsigned long aad_len); 251 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, 257 const u8 *in, 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); 262 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); 268 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); 274 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, 280 }; 281 282 static inline struct 283 aesni_rfc4106_gcm_ctx *aesni_rfc4106_gcm_ctx_get(struct crypto_aead *tfm) 284 { 285 unsigned long align = AESNI_ALIGN; 286 287 if (align <= crypto_tfm_ctx_alignment()) 288 align = 1; 289 return PTR_ALIGN(crypto_aead_ctx(tfm), align); 290 } 291 292 static inline struct 293 generic_gcmaes_ctx *generic_gcmaes_ctx_get(struct crypto_aead *tfm) 294 { 295 unsigned long align = AESNI_ALIGN; 296 297 if (align <= crypto_tfm_ctx_alignment()) 298 align = 1; 299 return PTR_ALIGN(crypto_aead_ctx(tfm), align); 300 } 301 #endif 302 303 static inline struct crypto_aes_ctx *aes_ctx(void *raw_ctx) 304 { 305 unsigned long addr = (unsigned long)raw_ctx; 306 unsigned long align = AESNI_ALIGN; 307 308 if (align <= crypto_tfm_ctx_alignment()) 309 align = 1; 310 return (struct crypto_aes_ctx *)ALIGN(addr, align); 311 } 312 313 static int aes_set_key_common(struct crypto_tfm *tfm, void *raw_ctx, 314 const u8 *in_key, unsigned int key_len) 315 { 316 struct crypto_aes_ctx *ctx = aes_ctx(raw_ctx); 317 int err; 318 319 if (key_len != AES_KEYSIZE_128 && key_len != AES_KEYSIZE_192 && 320 key_len != AES_KEYSIZE_256) 321 return -EINVAL; 322 323 if (!crypto_simd_usable()) 324 err = aes_expandkey(ctx, in_key, key_len); 325 else { 326 kernel_fpu_begin(); 327 err = aesni_set_key(ctx, in_key, key_len); 328 kernel_fpu_end(); 329 } 330 331 return err; 332 } 333 334 static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key, 335 unsigned int key_len) 336 { 337 return aes_set_key_common(tfm, crypto_tfm_ctx(tfm), in_key, key_len); 338 } 339 340 static void aesni_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 341 { 342 struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm)); 343 344 if (!crypto_simd_usable()) { 345 aes_encrypt(ctx, dst, src); 346 } else { 347 kernel_fpu_begin(); 348 aesni_enc(ctx, dst, src); 349 kernel_fpu_end(); 350 } 351 } 352 353 static void aesni_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src) 354 { 355 struct crypto_aes_ctx *ctx = aes_ctx(crypto_tfm_ctx(tfm)); 356 357 if (!crypto_simd_usable()) { 358 aes_decrypt(ctx, dst, src); 359 } else { 360 kernel_fpu_begin(); 361 aesni_dec(ctx, dst, src); 362 kernel_fpu_end(); 363 } 364 } 365 366 static int aesni_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key, 367 unsigned int len) 368 { 369 return aes_set_key_common(crypto_skcipher_tfm(tfm), 370 crypto_skcipher_ctx(tfm), key, len); 371 } 372 373 static int ecb_encrypt(struct skcipher_request *req) 374 { 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; 378 unsigned int nbytes; 379 int err; 380 381 err = skcipher_walk_virt(&walk, req, true); 382 383 kernel_fpu_begin(); 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); 389 } 390 kernel_fpu_end(); 391 392 return err; 393 } 394 395 static int ecb_decrypt(struct skcipher_request *req) 396 { 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; 400 unsigned int nbytes; 401 int err; 402 403 err = skcipher_walk_virt(&walk, req, true); 404 405 kernel_fpu_begin(); 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); 411 } 412 kernel_fpu_end(); 413 414 return err; 415 } 416 417 static int cbc_encrypt(struct skcipher_request *req) 418 { 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; 422 unsigned int nbytes; 423 int err; 424 425 err = skcipher_walk_virt(&walk, req, true); 426 427 kernel_fpu_begin(); 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); 433 } 434 kernel_fpu_end(); 435 436 return err; 437 } 438 439 static int cbc_decrypt(struct skcipher_request *req) 440 { 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; 444 unsigned int nbytes; 445 int err; 446 447 err = skcipher_walk_virt(&walk, req, true); 448 449 kernel_fpu_begin(); 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); 455 } 456 kernel_fpu_end(); 457 458 return err; 459 } 460 461 static int cts_cbc_encrypt(struct skcipher_request *req) 462 { 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; 470 int err; 471 472 skcipher_request_set_tfm(&subreq, tfm); 473 skcipher_request_set_callback(&subreq, skcipher_request_flags(req), 474 NULL, NULL); 475 476 if (req->cryptlen <= AES_BLOCK_SIZE) { 477 if (req->cryptlen < AES_BLOCK_SIZE) 478 return -EINVAL; 479 cbc_blocks = 1; 480 } 481 482 if (cbc_blocks > 0) { 483 skcipher_request_set_crypt(&subreq, req->src, req->dst, 484 cbc_blocks * AES_BLOCK_SIZE, 485 req->iv); 486 487 err = cbc_encrypt(&subreq); 488 if (err) 489 return err; 490 491 if (req->cryptlen == AES_BLOCK_SIZE) 492 return 0; 493 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, 497 subreq.cryptlen); 498 } 499 500 /* handle ciphertext stealing */ 501 skcipher_request_set_crypt(&subreq, src, dst, 502 req->cryptlen - cbc_blocks * AES_BLOCK_SIZE, 503 req->iv); 504 505 err = skcipher_walk_virt(&walk, &subreq, false); 506 if (err) 507 return err; 508 509 kernel_fpu_begin(); 510 aesni_cts_cbc_enc(ctx, walk.dst.virt.addr, walk.src.virt.addr, 511 walk.nbytes, walk.iv); 512 kernel_fpu_end(); 513 514 return skcipher_walk_done(&walk, 0); 515 } 516 517 static int cts_cbc_decrypt(struct skcipher_request *req) 518 { 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; 526 int err; 527 528 skcipher_request_set_tfm(&subreq, tfm); 529 skcipher_request_set_callback(&subreq, skcipher_request_flags(req), 530 NULL, NULL); 531 532 if (req->cryptlen <= AES_BLOCK_SIZE) { 533 if (req->cryptlen < AES_BLOCK_SIZE) 534 return -EINVAL; 535 cbc_blocks = 1; 536 } 537 538 if (cbc_blocks > 0) { 539 skcipher_request_set_crypt(&subreq, req->src, req->dst, 540 cbc_blocks * AES_BLOCK_SIZE, 541 req->iv); 542 543 err = cbc_decrypt(&subreq); 544 if (err) 545 return err; 546 547 if (req->cryptlen == AES_BLOCK_SIZE) 548 return 0; 549 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, 553 subreq.cryptlen); 554 } 555 556 /* handle ciphertext stealing */ 557 skcipher_request_set_crypt(&subreq, src, dst, 558 req->cryptlen - cbc_blocks * AES_BLOCK_SIZE, 559 req->iv); 560 561 err = skcipher_walk_virt(&walk, &subreq, false); 562 if (err) 563 return err; 564 565 kernel_fpu_begin(); 566 aesni_cts_cbc_dec(ctx, walk.dst.virt.addr, walk.src.virt.addr, 567 walk.nbytes, walk.iv); 568 kernel_fpu_end(); 569 570 return skcipher_walk_done(&walk, 0); 571 } 572 573 #ifdef CONFIG_X86_64 574 static void ctr_crypt_final(struct crypto_aes_ctx *ctx, 575 struct skcipher_walk *walk) 576 { 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; 582 583 aesni_enc(ctx, keystream, ctrblk); 584 crypto_xor_cpy(dst, keystream, src, nbytes); 585 586 crypto_inc(ctrblk, AES_BLOCK_SIZE); 587 } 588 589 static void aesni_ctr_enc_avx_tfm(struct crypto_aes_ctx *ctx, u8 *out, 590 const u8 *in, unsigned int len, u8 *iv) 591 { 592 /* 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 596 * {128,192,256} 597 */ 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); 602 else 603 aes_ctr_enc_256_avx_by8(in, iv, (void *)ctx, out, len); 604 } 605 606 static int ctr_crypt(struct skcipher_request *req) 607 { 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; 611 unsigned int nbytes; 612 int err; 613 614 err = skcipher_walk_virt(&walk, req, true); 615 616 kernel_fpu_begin(); 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); 622 } 623 if (walk.nbytes) { 624 ctr_crypt_final(ctx, &walk); 625 err = skcipher_walk_done(&walk, 0); 626 } 627 kernel_fpu_end(); 628 629 return err; 630 } 631 632 static int xts_aesni_setkey(struct crypto_skcipher *tfm, const u8 *key, 633 unsigned int keylen) 634 { 635 struct aesni_xts_ctx *ctx = crypto_skcipher_ctx(tfm); 636 int err; 637 638 err = xts_verify_key(tfm, key, keylen); 639 if (err) 640 return err; 641 642 keylen /= 2; 643 644 /* first half of xts-key is for crypt */ 645 err = aes_set_key_common(crypto_skcipher_tfm(tfm), ctx->raw_crypt_ctx, 646 key, keylen); 647 if (err) 648 return err; 649 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); 653 } 654 655 656 static void aesni_xts_enc(const void *ctx, u8 *dst, const u8 *src, le128 *iv) 657 { 658 glue_xts_crypt_128bit_one(ctx, dst, src, iv, aesni_enc); 659 } 660 661 static void aesni_xts_dec(const void *ctx, u8 *dst, const u8 *src, le128 *iv) 662 { 663 glue_xts_crypt_128bit_one(ctx, dst, src, iv, aesni_dec); 664 } 665 666 static void aesni_xts_enc8(const void *ctx, u8 *dst, const u8 *src, le128 *iv) 667 { 668 aesni_xts_crypt8(ctx, dst, src, true, iv); 669 } 670 671 static void aesni_xts_dec8(const void *ctx, u8 *dst, const u8 *src, le128 *iv) 672 { 673 aesni_xts_crypt8(ctx, dst, src, false, iv); 674 } 675 676 static const struct common_glue_ctx aesni_enc_xts = { 677 .num_funcs = 2, 678 .fpu_blocks_limit = 1, 679 680 .funcs = { { 681 .num_blocks = 8, 682 .fn_u = { .xts = aesni_xts_enc8 } 683 }, { 684 .num_blocks = 1, 685 .fn_u = { .xts = aesni_xts_enc } 686 } } 687 }; 688 689 static const struct common_glue_ctx aesni_dec_xts = { 690 .num_funcs = 2, 691 .fpu_blocks_limit = 1, 692 693 .funcs = { { 694 .num_blocks = 8, 695 .fn_u = { .xts = aesni_xts_dec8 } 696 }, { 697 .num_blocks = 1, 698 .fn_u = { .xts = aesni_xts_dec } 699 } } 700 }; 701 702 static int xts_encrypt(struct skcipher_request *req) 703 { 704 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 705 struct aesni_xts_ctx *ctx = crypto_skcipher_ctx(tfm); 706 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), 710 false); 711 } 712 713 static int xts_decrypt(struct skcipher_request *req) 714 { 715 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 716 struct aesni_xts_ctx *ctx = crypto_skcipher_ctx(tfm); 717 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), 721 true); 722 } 723 724 static int 725 rfc4106_set_hash_subkey(u8 *hash_subkey, const u8 *key, unsigned int key_len) 726 { 727 struct crypto_aes_ctx ctx; 728 int ret; 729 730 ret = aes_expandkey(&ctx, key, key_len); 731 if (ret) 732 return ret; 733 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); 737 738 aes_encrypt(&ctx, hash_subkey, hash_subkey); 739 740 memzero_explicit(&ctx, sizeof(ctx)); 741 return 0; 742 } 743 744 static int common_rfc4106_set_key(struct crypto_aead *aead, const u8 *key, 745 unsigned int key_len) 746 { 747 struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(aead); 748 749 if (key_len < 4) 750 return -EINVAL; 751 752 /*Account for 4 byte nonce at the end.*/ 753 key_len -= 4; 754 755 memcpy(ctx->nonce, key + key_len, sizeof(ctx->nonce)); 756 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); 760 } 761 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) 766 { 767 switch (authsize) { 768 case 8: 769 case 12: 770 case 16: 771 break; 772 default: 773 return -EINVAL; 774 } 775 776 return 0; 777 } 778 779 static int generic_gcmaes_set_authsize(struct crypto_aead *tfm, 780 unsigned int authsize) 781 { 782 switch (authsize) { 783 case 4: 784 case 8: 785 case 12: 786 case 13: 787 case 14: 788 case 15: 789 case 16: 790 break; 791 default: 792 return -EINVAL; 793 } 794 795 return 0; 796 } 797 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) 801 { 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; 816 u8 *assocmem = NULL; 817 u8 authTag[16]; 818 819 if (!enc) 820 left -= auth_tag_len; 821 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; 826 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); 833 } else { 834 /* assoc can be any length, so must be on heap */ 835 assocmem = kmalloc(assoclen, GFP_ATOMIC); 836 if (unlikely(!assocmem)) 837 return -ENOMEM; 838 assoc = assocmem; 839 840 scatterwalk_map_and_copy(assoc, req->src, 0, assoclen, 0); 841 } 842 843 if (left) { 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, 848 req->assoclen); 849 scatterwalk_start(&dst_sg_walk, dst_sg); 850 } 851 } 852 853 kernel_fpu_begin(); 854 gcm_tfm->init(aes_ctx, &data, iv, 855 hash_subkey, assoc, assoclen); 856 if (req->src != req->dst) { 857 while (left) { 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); 863 if (len) { 864 if (enc) 865 gcm_tfm->enc_update(aes_ctx, &data, 866 dst, src, len); 867 else 868 gcm_tfm->dec_update(aes_ctx, &data, 869 dst, src, len); 870 } 871 left -= len; 872 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); 879 } 880 } else { 881 while (left) { 882 dst = src = scatterwalk_map(&src_sg_walk); 883 len = scatterwalk_clamp(&src_sg_walk, left); 884 if (len) { 885 if (enc) 886 gcm_tfm->enc_update(aes_ctx, &data, 887 src, src, len); 888 else 889 gcm_tfm->dec_update(aes_ctx, &data, 890 src, src, len); 891 } 892 left -= len; 893 scatterwalk_unmap(src); 894 scatterwalk_advance(&src_sg_walk, len); 895 scatterwalk_done(&src_sg_walk, 1, left); 896 } 897 } 898 gcm_tfm->finalize(aes_ctx, &data, authTag, auth_tag_len); 899 kernel_fpu_end(); 900 901 if (!assocmem) 902 scatterwalk_unmap(assoc); 903 else 904 kfree(assocmem); 905 906 if (!enc) { 907 u8 authTagMsg[16]; 908 909 /* Copy out original authTag */ 910 scatterwalk_map_and_copy(authTagMsg, req->src, 911 req->assoclen + req->cryptlen - 912 auth_tag_len, 913 auth_tag_len, 0); 914 915 /* Compare generated tag with passed in tag. */ 916 return crypto_memneq(authTagMsg, authTag, auth_tag_len) ? 917 -EBADMSG : 0; 918 } 919 920 /* Copy in the authTag */ 921 scatterwalk_map_and_copy(authTag, req->dst, 922 req->assoclen + req->cryptlen, 923 auth_tag_len, 1); 924 925 return 0; 926 } 927 928 static int gcmaes_encrypt(struct aead_request *req, unsigned int assoclen, 929 u8 *hash_subkey, u8 *iv, void *aes_ctx) 930 { 931 return gcmaes_crypt_by_sg(true, req, assoclen, hash_subkey, iv, 932 aes_ctx); 933 } 934 935 static int gcmaes_decrypt(struct aead_request *req, unsigned int assoclen, 936 u8 *hash_subkey, u8 *iv, void *aes_ctx) 937 { 938 return gcmaes_crypt_by_sg(false, req, assoclen, hash_subkey, iv, 939 aes_ctx); 940 } 941 942 static int helper_rfc4106_encrypt(struct aead_request *req) 943 { 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))); 948 unsigned int i; 949 __be32 counter = cpu_to_be32(1); 950 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)) 955 return -EINVAL; 956 957 /* IV below built */ 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; 963 964 return gcmaes_encrypt(req, req->assoclen - 8, ctx->hash_subkey, iv, 965 aes_ctx); 966 } 967 968 static int helper_rfc4106_decrypt(struct aead_request *req) 969 { 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))); 975 unsigned int i; 976 977 if (unlikely(req->assoclen != 16 && req->assoclen != 20)) 978 return -EINVAL; 979 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 */ 983 984 /* IV below built */ 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; 990 991 return gcmaes_decrypt(req, req->assoclen - 8, ctx->hash_subkey, iv, 992 aes_ctx); 993 } 994 #endif 995 996 static struct crypto_alg aesni_cipher_alg = { 997 .cra_name = "aes", 998 .cra_driver_name = "aes-aesni", 999 .cra_priority = 300, 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, 1004 .cra_u = { 1005 .cipher = { 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 1011 } 1012 } 1013 }; 1014 1015 static struct skcipher_alg aesni_skciphers[] = { 1016 { 1017 .base = { 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, 1025 }, 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, 1031 }, { 1032 .base = { 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, 1040 }, 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, 1047 }, { 1048 .base = { 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, 1056 }, 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 1065 }, { 1066 .base = { 1067 .cra_name = "__ctr(aes)", 1068 .cra_driver_name = "__ctr-aes-aesni", 1069 .cra_priority = 400, 1070 .cra_flags = CRYPTO_ALG_INTERNAL, 1071 .cra_blocksize = 1, 1072 .cra_ctxsize = CRYPTO_AES_CTX_SIZE, 1073 .cra_module = THIS_MODULE, 1074 }, 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, 1082 }, { 1083 .base = { 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, 1091 }, 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, 1098 #endif 1099 } 1100 }; 1101 1102 static 1103 struct simd_skcipher_alg *aesni_simd_skciphers[ARRAY_SIZE(aesni_skciphers)]; 1104 1105 #ifdef CONFIG_X86_64 1106 static int generic_gcmaes_set_key(struct crypto_aead *aead, const u8 *key, 1107 unsigned int key_len) 1108 { 1109 struct generic_gcmaes_ctx *ctx = generic_gcmaes_ctx_get(aead); 1110 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); 1114 } 1115 1116 static int generic_gcmaes_encrypt(struct aead_request *req) 1117 { 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); 1123 1124 memcpy(iv, req->iv, 12); 1125 *((__be32 *)(iv+12)) = counter; 1126 1127 return gcmaes_encrypt(req, req->assoclen, ctx->hash_subkey, iv, 1128 aes_ctx); 1129 } 1130 1131 static int generic_gcmaes_decrypt(struct aead_request *req) 1132 { 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))); 1138 1139 memcpy(iv, req->iv, 12); 1140 *((__be32 *)(iv+12)) = counter; 1141 1142 return gcmaes_decrypt(req, req->assoclen, ctx->hash_subkey, iv, 1143 aes_ctx); 1144 } 1145 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, 1152 .maxauthsize = 16, 1153 .base = { 1154 .cra_name = "__rfc4106(gcm(aes))", 1155 .cra_driver_name = "__rfc4106-gcm-aesni", 1156 .cra_priority = 400, 1157 .cra_flags = CRYPTO_ALG_INTERNAL, 1158 .cra_blocksize = 1, 1159 .cra_ctxsize = sizeof(struct aesni_rfc4106_gcm_ctx), 1160 .cra_alignmask = AESNI_ALIGN - 1, 1161 .cra_module = THIS_MODULE, 1162 }, 1163 }, { 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, 1169 .maxauthsize = 16, 1170 .base = { 1171 .cra_name = "__gcm(aes)", 1172 .cra_driver_name = "__generic-gcm-aesni", 1173 .cra_priority = 400, 1174 .cra_flags = CRYPTO_ALG_INTERNAL, 1175 .cra_blocksize = 1, 1176 .cra_ctxsize = sizeof(struct generic_gcmaes_ctx), 1177 .cra_alignmask = AESNI_ALIGN - 1, 1178 .cra_module = THIS_MODULE, 1179 }, 1180 } }; 1181 #else 1182 static struct aead_alg aesni_aeads[0]; 1183 #endif 1184 1185 static struct simd_aead_alg *aesni_simd_aeads[ARRAY_SIZE(aesni_aeads)]; 1186 1187 static const struct x86_cpu_id aesni_cpu_id[] = { 1188 X86_MATCH_FEATURE(X86_FEATURE_AES, NULL), 1189 {} 1190 }; 1191 MODULE_DEVICE_TABLE(x86cpu, aesni_cpu_id); 1192 1193 static int __init aesni_init(void) 1194 { 1195 int err; 1196 1197 if (!x86_match_cpu(aesni_cpu_id)) 1198 return -ENODEV; 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; 1203 } else 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; 1207 } else { 1208 pr_info("SSE version of gcm_enc/dec engaged.\n"); 1209 aesni_gcm_tfm = &aesni_gcm_tfm_sse; 1210 } 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"); 1216 } 1217 #endif 1218 1219 err = crypto_register_alg(&aesni_cipher_alg); 1220 if (err) 1221 return err; 1222 1223 err = simd_register_skciphers_compat(aesni_skciphers, 1224 ARRAY_SIZE(aesni_skciphers), 1225 aesni_simd_skciphers); 1226 if (err) 1227 goto unregister_cipher; 1228 1229 err = simd_register_aeads_compat(aesni_aeads, ARRAY_SIZE(aesni_aeads), 1230 aesni_simd_aeads); 1231 if (err) 1232 goto unregister_skciphers; 1233 1234 return 0; 1235 1236 unregister_skciphers: 1237 simd_unregister_skciphers(aesni_skciphers, ARRAY_SIZE(aesni_skciphers), 1238 aesni_simd_skciphers); 1239 unregister_cipher: 1240 crypto_unregister_alg(&aesni_cipher_alg); 1241 return err; 1242 } 1243 1244 static void __exit aesni_exit(void) 1245 { 1246 simd_unregister_aeads(aesni_aeads, ARRAY_SIZE(aesni_aeads), 1247 aesni_simd_aeads); 1248 simd_unregister_skciphers(aesni_skciphers, ARRAY_SIZE(aesni_skciphers), 1249 aesni_simd_skciphers); 1250 crypto_unregister_alg(&aesni_cipher_alg); 1251 } 1252 1253 late_initcall(aesni_init); 1254 module_exit(aesni_exit); 1255 1256 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algorithm, Intel AES-NI instructions optimized"); 1257 MODULE_LICENSE("GPL"); 1258 MODULE_ALIAS_CRYPTO("aes"); 1259