1 // SPDX-License-Identifier: GPL-2.0-only 1 2 /* Glue code for AES encryption optimized for 3 * 4 * This is based largely upon arch/x86/crypto/ 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 k 10 * interface for 64-bit kernels. 11 * Authors: Adrian Hoban <adrian.hoban@inte 12 * Gabriele Paoloni <gabriele.paol 13 * Tadeusz Struk (tadeusz.struk@in 14 * Aidan O'Mahony (aidan.o.mahony@ 15 * Copyright (c) 2010, Intel Corporation. 16 */ 17 18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fm 19 20 #include <linux/crypto.h> 21 #include <linux/init.h> 22 #include <linux/module.h> 23 #include <linux/mm.h> 24 #include <linux/types.h> 25 #include <crypto/algapi.h> 26 #include <crypto/aes.h> 27 #include <crypto/internal/skcipher.h> 28 29 #include <asm/fpumacro.h> 30 #include <asm/pstate.h> 31 #include <asm/elf.h> 32 33 #include "opcodes.h" 34 35 struct aes_ops { 36 void (*encrypt)(const u64 *key, const 37 void (*decrypt)(const u64 *key, const 38 void (*load_encrypt_keys)(const u64 *k 39 void (*load_decrypt_keys)(const u64 *k 40 void (*ecb_encrypt)(const u64 *key, co 41 unsigned int len); 42 void (*ecb_decrypt)(const u64 *key, co 43 unsigned int len); 44 void (*cbc_encrypt)(const u64 *key, co 45 unsigned int len, 46 void (*cbc_decrypt)(const u64 *key, co 47 unsigned int len, 48 void (*ctr_crypt)(const u64 *key, cons 49 unsigned int len, u6 50 }; 51 52 struct crypto_sparc64_aes_ctx { 53 struct aes_ops *ops; 54 u64 key[AES_MAX_KEYLENGTH / sizeof(u64 55 u32 key_length; 56 u32 expanded_key_length; 57 }; 58 59 extern void aes_sparc64_encrypt_128(const u64 60 u32 *outpu 61 extern void aes_sparc64_encrypt_192(const u64 62 u32 *outpu 63 extern void aes_sparc64_encrypt_256(const u64 64 u32 *outpu 65 66 extern void aes_sparc64_decrypt_128(const u64 67 u32 *outpu 68 extern void aes_sparc64_decrypt_192(const u64 69 u32 *outpu 70 extern void aes_sparc64_decrypt_256(const u64 71 u32 *outpu 72 73 extern void aes_sparc64_load_encrypt_keys_128( 74 extern void aes_sparc64_load_encrypt_keys_192( 75 extern void aes_sparc64_load_encrypt_keys_256( 76 77 extern void aes_sparc64_load_decrypt_keys_128( 78 extern void aes_sparc64_load_decrypt_keys_192( 79 extern void aes_sparc64_load_decrypt_keys_256( 80 81 extern void aes_sparc64_ecb_encrypt_128(const 82 u64 *o 83 extern void aes_sparc64_ecb_encrypt_192(const 84 u64 *o 85 extern void aes_sparc64_ecb_encrypt_256(const 86 u64 *o 87 88 extern void aes_sparc64_ecb_decrypt_128(const 89 u64 *o 90 extern void aes_sparc64_ecb_decrypt_192(const 91 u64 *o 92 extern void aes_sparc64_ecb_decrypt_256(const 93 u64 *o 94 95 extern void aes_sparc64_cbc_encrypt_128(const 96 u64 *o 97 u64 *i 98 99 extern void aes_sparc64_cbc_encrypt_192(const 100 u64 *o 101 u64 *i 102 103 extern void aes_sparc64_cbc_encrypt_256(const 104 u64 *o 105 u64 *i 106 107 extern void aes_sparc64_cbc_decrypt_128(const 108 u64 *o 109 u64 *i 110 111 extern void aes_sparc64_cbc_decrypt_192(const 112 u64 *o 113 u64 *i 114 115 extern void aes_sparc64_cbc_decrypt_256(const 116 u64 *o 117 u64 *i 118 119 extern void aes_sparc64_ctr_crypt_128(const u6 120 u64 *out 121 u64 *iv) 122 extern void aes_sparc64_ctr_crypt_192(const u6 123 u64 *out 124 u64 *iv) 125 extern void aes_sparc64_ctr_crypt_256(const u6 126 u64 *out 127 u64 *iv) 128 129 static struct aes_ops aes128_ops = { 130 .encrypt = aes_sparc64_ 131 .decrypt = aes_sparc64_ 132 .load_encrypt_keys = aes_sparc64_ 133 .load_decrypt_keys = aes_sparc64_ 134 .ecb_encrypt = aes_sparc64_ 135 .ecb_decrypt = aes_sparc64_ 136 .cbc_encrypt = aes_sparc64_ 137 .cbc_decrypt = aes_sparc64_ 138 .ctr_crypt = aes_sparc64_ 139 }; 140 141 static struct aes_ops aes192_ops = { 142 .encrypt = aes_sparc64_ 143 .decrypt = aes_sparc64_ 144 .load_encrypt_keys = aes_sparc64_ 145 .load_decrypt_keys = aes_sparc64_ 146 .ecb_encrypt = aes_sparc64_ 147 .ecb_decrypt = aes_sparc64_ 148 .cbc_encrypt = aes_sparc64_ 149 .cbc_decrypt = aes_sparc64_ 150 .ctr_crypt = aes_sparc64_ 151 }; 152 153 static struct aes_ops aes256_ops = { 154 .encrypt = aes_sparc64_ 155 .decrypt = aes_sparc64_ 156 .load_encrypt_keys = aes_sparc64_ 157 .load_decrypt_keys = aes_sparc64_ 158 .ecb_encrypt = aes_sparc64_ 159 .ecb_decrypt = aes_sparc64_ 160 .cbc_encrypt = aes_sparc64_ 161 .cbc_decrypt = aes_sparc64_ 162 .ctr_crypt = aes_sparc64_ 163 }; 164 165 extern void aes_sparc64_key_expand(const u32 * 166 unsigned in 167 168 static int aes_set_key(struct crypto_tfm *tfm, 169 unsigned int key_len) 170 { 171 struct crypto_sparc64_aes_ctx *ctx = c 172 173 switch (key_len) { 174 case AES_KEYSIZE_128: 175 ctx->expanded_key_length = 0xb 176 ctx->ops = &aes128_ops; 177 break; 178 179 case AES_KEYSIZE_192: 180 ctx->expanded_key_length = 0xd 181 ctx->ops = &aes192_ops; 182 break; 183 184 case AES_KEYSIZE_256: 185 ctx->expanded_key_length = 0xf 186 ctx->ops = &aes256_ops; 187 break; 188 189 default: 190 return -EINVAL; 191 } 192 193 aes_sparc64_key_expand((const u32 *)in 194 ctx->key_length = key_len; 195 196 return 0; 197 } 198 199 static int aes_set_key_skcipher(struct crypto_ 200 unsigned int k 201 { 202 return aes_set_key(crypto_skcipher_tfm 203 } 204 205 static void crypto_aes_encrypt(struct crypto_t 206 { 207 struct crypto_sparc64_aes_ctx *ctx = c 208 209 ctx->ops->encrypt(&ctx->key[0], (const 210 } 211 212 static void crypto_aes_decrypt(struct crypto_t 213 { 214 struct crypto_sparc64_aes_ctx *ctx = c 215 216 ctx->ops->decrypt(&ctx->key[0], (const 217 } 218 219 static int ecb_encrypt(struct skcipher_request 220 { 221 struct crypto_skcipher *tfm = crypto_s 222 const struct crypto_sparc64_aes_ctx *c 223 struct skcipher_walk walk; 224 unsigned int nbytes; 225 int err; 226 227 err = skcipher_walk_virt(&walk, req, t 228 if (err) 229 return err; 230 231 ctx->ops->load_encrypt_keys(&ctx->key[ 232 while ((nbytes = walk.nbytes) != 0) { 233 ctx->ops->ecb_encrypt(&ctx->ke 234 walk.dst 235 round_do 236 err = skcipher_walk_done(&walk 237 } 238 fprs_write(0); 239 return err; 240 } 241 242 static int ecb_decrypt(struct skcipher_request 243 { 244 struct crypto_skcipher *tfm = crypto_s 245 const struct crypto_sparc64_aes_ctx *c 246 const u64 *key_end; 247 struct skcipher_walk walk; 248 unsigned int nbytes; 249 int err; 250 251 err = skcipher_walk_virt(&walk, req, t 252 if (err) 253 return err; 254 255 ctx->ops->load_decrypt_keys(&ctx->key[ 256 key_end = &ctx->key[ctx->expanded_key_ 257 while ((nbytes = walk.nbytes) != 0) { 258 ctx->ops->ecb_decrypt(key_end, 259 walk.dst 260 round_do 261 err = skcipher_walk_done(&walk 262 } 263 fprs_write(0); 264 265 return err; 266 } 267 268 static int cbc_encrypt(struct skcipher_request 269 { 270 struct crypto_skcipher *tfm = crypto_s 271 const struct crypto_sparc64_aes_ctx *c 272 struct skcipher_walk walk; 273 unsigned int nbytes; 274 int err; 275 276 err = skcipher_walk_virt(&walk, req, t 277 if (err) 278 return err; 279 280 ctx->ops->load_encrypt_keys(&ctx->key[ 281 while ((nbytes = walk.nbytes) != 0) { 282 ctx->ops->cbc_encrypt(&ctx->ke 283 walk.dst 284 round_do 285 walk.iv) 286 err = skcipher_walk_done(&walk 287 } 288 fprs_write(0); 289 return err; 290 } 291 292 static int cbc_decrypt(struct skcipher_request 293 { 294 struct crypto_skcipher *tfm = crypto_s 295 const struct crypto_sparc64_aes_ctx *c 296 const u64 *key_end; 297 struct skcipher_walk walk; 298 unsigned int nbytes; 299 int err; 300 301 err = skcipher_walk_virt(&walk, req, t 302 if (err) 303 return err; 304 305 ctx->ops->load_decrypt_keys(&ctx->key[ 306 key_end = &ctx->key[ctx->expanded_key_ 307 while ((nbytes = walk.nbytes) != 0) { 308 ctx->ops->cbc_decrypt(key_end, 309 walk.dst 310 round_do 311 walk.iv) 312 err = skcipher_walk_done(&walk 313 } 314 fprs_write(0); 315 316 return err; 317 } 318 319 static void ctr_crypt_final(const struct crypt 320 struct skcipher_wa 321 { 322 u8 *ctrblk = walk->iv; 323 u64 keystream[AES_BLOCK_SIZE / sizeof( 324 u8 *src = walk->src.virt.addr; 325 u8 *dst = walk->dst.virt.addr; 326 unsigned int nbytes = walk->nbytes; 327 328 ctx->ops->ecb_encrypt(&ctx->key[0], (c 329 keystream, AES_B 330 crypto_xor_cpy(dst, (u8 *) keystream, 331 crypto_inc(ctrblk, AES_BLOCK_SIZE); 332 } 333 334 static int ctr_crypt(struct skcipher_request * 335 { 336 struct crypto_skcipher *tfm = crypto_s 337 const struct crypto_sparc64_aes_ctx *c 338 struct skcipher_walk walk; 339 unsigned int nbytes; 340 int err; 341 342 err = skcipher_walk_virt(&walk, req, t 343 if (err) 344 return err; 345 346 ctx->ops->load_encrypt_keys(&ctx->key[ 347 while ((nbytes = walk.nbytes) >= AES_B 348 ctx->ops->ctr_crypt(&ctx->key[ 349 walk.dst.v 350 round_down 351 walk.iv); 352 err = skcipher_walk_done(&walk 353 } 354 if (walk.nbytes) { 355 ctr_crypt_final(ctx, &walk); 356 err = skcipher_walk_done(&walk 357 } 358 fprs_write(0); 359 return err; 360 } 361 362 static struct crypto_alg cipher_alg = { 363 .cra_name = "aes", 364 .cra_driver_name = "aes-sparc64 365 .cra_priority = SPARC_CR_OPC 366 .cra_flags = CRYPTO_ALG_T 367 .cra_blocksize = AES_BLOCK_SI 368 .cra_ctxsize = sizeof(struc 369 .cra_alignmask = 3, 370 .cra_module = THIS_MODULE, 371 .cra_u = { 372 .cipher = { 373 .cia_min_keysize 374 .cia_max_keysize 375 .cia_setkey 376 .cia_encrypt 377 .cia_decrypt 378 } 379 } 380 }; 381 382 static struct skcipher_alg skcipher_algs[] = { 383 { 384 .base.cra_name = "ecb 385 .base.cra_driver_name = "ecb 386 .base.cra_priority = SPAR 387 .base.cra_blocksize = AES_ 388 .base.cra_ctxsize = size 389 .base.cra_alignmask = 7, 390 .base.cra_module = THIS 391 .min_keysize = AES_ 392 .max_keysize = AES_ 393 .setkey = aes_ 394 .encrypt = ecb_ 395 .decrypt = ecb_ 396 }, { 397 .base.cra_name = "cbc 398 .base.cra_driver_name = "cbc 399 .base.cra_priority = SPAR 400 .base.cra_blocksize = AES_ 401 .base.cra_ctxsize = size 402 .base.cra_alignmask = 7, 403 .base.cra_module = THIS 404 .min_keysize = AES_ 405 .max_keysize = AES_ 406 .ivsize = AES_ 407 .setkey = aes_ 408 .encrypt = cbc_ 409 .decrypt = cbc_ 410 }, { 411 .base.cra_name = "ctr 412 .base.cra_driver_name = "ctr 413 .base.cra_priority = SPAR 414 .base.cra_blocksize = 1, 415 .base.cra_ctxsize = size 416 .base.cra_alignmask = 7, 417 .base.cra_module = THIS 418 .min_keysize = AES_ 419 .max_keysize = AES_ 420 .ivsize = AES_ 421 .setkey = aes_ 422 .encrypt = ctr_ 423 .decrypt = ctr_ 424 .chunksize = AES_ 425 } 426 }; 427 428 static bool __init sparc64_has_aes_opcode(void 429 { 430 unsigned long cfr; 431 432 if (!(sparc64_elf_hwcap & HWCAP_SPARC_ 433 return false; 434 435 __asm__ __volatile__("rd %%asr26, %0" 436 if (!(cfr & CFR_AES)) 437 return false; 438 439 return true; 440 } 441 442 static int __init aes_sparc64_mod_init(void) 443 { 444 int err; 445 446 if (!sparc64_has_aes_opcode()) { 447 pr_info("sparc64 aes opcodes n 448 return -ENODEV; 449 } 450 pr_info("Using sparc64 aes opcodes opt 451 err = crypto_register_alg(&cipher_alg) 452 if (err) 453 return err; 454 err = crypto_register_skciphers(skciph 455 ARRAY_ 456 if (err) 457 crypto_unregister_alg(&cipher_ 458 return err; 459 } 460 461 static void __exit aes_sparc64_mod_fini(void) 462 { 463 crypto_unregister_alg(&cipher_alg); 464 crypto_unregister_skciphers(skcipher_a 465 } 466 467 module_init(aes_sparc64_mod_init); 468 module_exit(aes_sparc64_mod_fini); 469 470 MODULE_LICENSE("GPL"); 471 MODULE_DESCRIPTION("Rijndael (AES) Cipher Algo 472 473 MODULE_ALIAS_CRYPTO("aes"); 474 475 #include "crop_devid.c" 476
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.