1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Linear symmetric key cipher operations. 4 * 5 * Generic encrypt/decrypt wrapper for ciphers. 6 * 7 * Copyright (c) 2023 Herbert Xu <herbert@gondor.apana.org.au> 8 */ 9 10 #include <linux/cryptouser.h> 11 #include <linux/err.h> 12 #include <linux/export.h> 13 #include <linux/kernel.h> 14 #include <linux/seq_file.h> 15 #include <linux/slab.h> 16 #include <linux/string.h> 17 #include <net/netlink.h> 18 #include "skcipher.h" 19 20 static inline struct crypto_lskcipher *__crypto_lskcipher_cast( 21 struct crypto_tfm *tfm) 22 { 23 return container_of(tfm, struct crypto_lskcipher, base); 24 } 25 26 static inline struct lskcipher_alg *__crypto_lskcipher_alg( 27 struct crypto_alg *alg) 28 { 29 return container_of(alg, struct lskcipher_alg, co.base); 30 } 31 32 static int lskcipher_setkey_unaligned(struct crypto_lskcipher *tfm, 33 const u8 *key, unsigned int keylen) 34 { 35 unsigned long alignmask = crypto_lskcipher_alignmask(tfm); 36 struct lskcipher_alg *cipher = crypto_lskcipher_alg(tfm); 37 u8 *buffer, *alignbuffer; 38 unsigned long absize; 39 int ret; 40 41 absize = keylen + alignmask; 42 buffer = kmalloc(absize, GFP_ATOMIC); 43 if (!buffer) 44 return -ENOMEM; 45 46 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); 47 memcpy(alignbuffer, key, keylen); 48 ret = cipher->setkey(tfm, alignbuffer, keylen); 49 kfree_sensitive(buffer); 50 return ret; 51 } 52 53 int crypto_lskcipher_setkey(struct crypto_lskcipher *tfm, const u8 *key, 54 unsigned int keylen) 55 { 56 unsigned long alignmask = crypto_lskcipher_alignmask(tfm); 57 struct lskcipher_alg *cipher = crypto_lskcipher_alg(tfm); 58 59 if (keylen < cipher->co.min_keysize || keylen > cipher->co.max_keysize) 60 return -EINVAL; 61 62 if ((unsigned long)key & alignmask) 63 return lskcipher_setkey_unaligned(tfm, key, keylen); 64 else 65 return cipher->setkey(tfm, key, keylen); 66 } 67 EXPORT_SYMBOL_GPL(crypto_lskcipher_setkey); 68 69 static int crypto_lskcipher_crypt_unaligned( 70 struct crypto_lskcipher *tfm, const u8 *src, u8 *dst, unsigned len, 71 u8 *iv, int (*crypt)(struct crypto_lskcipher *tfm, const u8 *src, 72 u8 *dst, unsigned len, u8 *iv, u32 flags)) 73 { 74 unsigned statesize = crypto_lskcipher_statesize(tfm); 75 unsigned ivsize = crypto_lskcipher_ivsize(tfm); 76 unsigned bs = crypto_lskcipher_blocksize(tfm); 77 unsigned cs = crypto_lskcipher_chunksize(tfm); 78 u32 flags = 0; 79 int err; 80 u8 *tiv; 81 u8 *p; 82 83 BUILD_BUG_ON(MAX_CIPHER_BLOCKSIZE > PAGE_SIZE || 84 MAX_CIPHER_ALIGNMASK >= PAGE_SIZE); 85 86 tiv = kmalloc(PAGE_SIZE, GFP_ATOMIC); 87 if (!tiv) 88 return -ENOMEM; 89 90 memcpy(tiv, iv, ivsize + statesize); 91 92 p = kmalloc(PAGE_SIZE, GFP_ATOMIC); 93 err = -ENOMEM; 94 if (!p) 95 goto out; 96 97 while (len >= bs) { 98 unsigned chunk = min((unsigned)PAGE_SIZE, len); 99 100 if (chunk > cs) 101 chunk &= ~(cs - 1); 102 if (chunk == len) 103 flags |= CRYPTO_LSKCIPHER_FLAG_FINAL; 104 105 memcpy(p, src, chunk); 106 err = crypt(tfm, p, p, chunk, tiv, flags); 107 if (err) 108 goto out; 109 110 memcpy(dst, p, chunk); 111 flags |= CRYPTO_LSKCIPHER_FLAG_CONT; 112 src += chunk; 113 dst += chunk; 114 len -= chunk; 115 } 116 117 err = len ? -EINVAL : 0; 118 119 out: 120 memcpy(iv, tiv, ivsize + statesize); 121 kfree_sensitive(p); 122 kfree_sensitive(tiv); 123 return err; 124 } 125 126 static int crypto_lskcipher_crypt(struct crypto_lskcipher *tfm, const u8 *src, 127 u8 *dst, unsigned len, u8 *iv, 128 int (*crypt)(struct crypto_lskcipher *tfm, 129 const u8 *src, u8 *dst, 130 unsigned len, u8 *iv, 131 u32 flags)) 132 { 133 unsigned long alignmask = crypto_lskcipher_alignmask(tfm); 134 135 if (((unsigned long)src | (unsigned long)dst | (unsigned long)iv) & 136 alignmask) 137 return crypto_lskcipher_crypt_unaligned(tfm, src, dst, len, iv, 138 crypt); 139 140 return crypt(tfm, src, dst, len, iv, CRYPTO_LSKCIPHER_FLAG_FINAL); 141 } 142 143 int crypto_lskcipher_encrypt(struct crypto_lskcipher *tfm, const u8 *src, 144 u8 *dst, unsigned len, u8 *iv) 145 { 146 struct lskcipher_alg *alg = crypto_lskcipher_alg(tfm); 147 148 return crypto_lskcipher_crypt(tfm, src, dst, len, iv, alg->encrypt); 149 } 150 EXPORT_SYMBOL_GPL(crypto_lskcipher_encrypt); 151 152 int crypto_lskcipher_decrypt(struct crypto_lskcipher *tfm, const u8 *src, 153 u8 *dst, unsigned len, u8 *iv) 154 { 155 struct lskcipher_alg *alg = crypto_lskcipher_alg(tfm); 156 157 return crypto_lskcipher_crypt(tfm, src, dst, len, iv, alg->decrypt); 158 } 159 EXPORT_SYMBOL_GPL(crypto_lskcipher_decrypt); 160 161 static int crypto_lskcipher_crypt_sg(struct skcipher_request *req, 162 int (*crypt)(struct crypto_lskcipher *tfm, 163 const u8 *src, u8 *dst, 164 unsigned len, u8 *ivs, 165 u32 flags)) 166 { 167 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req); 168 struct crypto_lskcipher **ctx = crypto_skcipher_ctx(skcipher); 169 u8 *ivs = skcipher_request_ctx(req); 170 struct crypto_lskcipher *tfm = *ctx; 171 struct skcipher_walk walk; 172 unsigned ivsize; 173 u32 flags; 174 int err; 175 176 ivsize = crypto_lskcipher_ivsize(tfm); 177 ivs = PTR_ALIGN(ivs, crypto_skcipher_alignmask(skcipher) + 1); 178 memcpy(ivs, req->iv, ivsize); 179 180 flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP; 181 182 if (req->base.flags & CRYPTO_SKCIPHER_REQ_CONT) 183 flags |= CRYPTO_LSKCIPHER_FLAG_CONT; 184 185 if (!(req->base.flags & CRYPTO_SKCIPHER_REQ_NOTFINAL)) 186 flags |= CRYPTO_LSKCIPHER_FLAG_FINAL; 187 188 err = skcipher_walk_virt(&walk, req, false); 189 190 while (walk.nbytes) { 191 err = crypt(tfm, walk.src.virt.addr, walk.dst.virt.addr, 192 walk.nbytes, ivs, 193 flags & ~(walk.nbytes == walk.total ? 194 0 : CRYPTO_LSKCIPHER_FLAG_FINAL)); 195 err = skcipher_walk_done(&walk, err); 196 flags |= CRYPTO_LSKCIPHER_FLAG_CONT; 197 } 198 199 memcpy(req->iv, ivs, ivsize); 200 201 return err; 202 } 203 204 int crypto_lskcipher_encrypt_sg(struct skcipher_request *req) 205 { 206 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req); 207 struct crypto_lskcipher **ctx = crypto_skcipher_ctx(skcipher); 208 struct lskcipher_alg *alg = crypto_lskcipher_alg(*ctx); 209 210 return crypto_lskcipher_crypt_sg(req, alg->encrypt); 211 } 212 213 int crypto_lskcipher_decrypt_sg(struct skcipher_request *req) 214 { 215 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req); 216 struct crypto_lskcipher **ctx = crypto_skcipher_ctx(skcipher); 217 struct lskcipher_alg *alg = crypto_lskcipher_alg(*ctx); 218 219 return crypto_lskcipher_crypt_sg(req, alg->decrypt); 220 } 221 222 static void crypto_lskcipher_exit_tfm(struct crypto_tfm *tfm) 223 { 224 struct crypto_lskcipher *skcipher = __crypto_lskcipher_cast(tfm); 225 struct lskcipher_alg *alg = crypto_lskcipher_alg(skcipher); 226 227 alg->exit(skcipher); 228 } 229 230 static int crypto_lskcipher_init_tfm(struct crypto_tfm *tfm) 231 { 232 struct crypto_lskcipher *skcipher = __crypto_lskcipher_cast(tfm); 233 struct lskcipher_alg *alg = crypto_lskcipher_alg(skcipher); 234 235 if (alg->exit) 236 skcipher->base.exit = crypto_lskcipher_exit_tfm; 237 238 if (alg->init) 239 return alg->init(skcipher); 240 241 return 0; 242 } 243 244 static void crypto_lskcipher_free_instance(struct crypto_instance *inst) 245 { 246 struct lskcipher_instance *skcipher = 247 container_of(inst, struct lskcipher_instance, s.base); 248 249 skcipher->free(skcipher); 250 } 251 252 static void __maybe_unused crypto_lskcipher_show( 253 struct seq_file *m, struct crypto_alg *alg) 254 { 255 struct lskcipher_alg *skcipher = __crypto_lskcipher_alg(alg); 256 257 seq_printf(m, "type : lskcipher\n"); 258 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); 259 seq_printf(m, "min keysize : %u\n", skcipher->co.min_keysize); 260 seq_printf(m, "max keysize : %u\n", skcipher->co.max_keysize); 261 seq_printf(m, "ivsize : %u\n", skcipher->co.ivsize); 262 seq_printf(m, "chunksize : %u\n", skcipher->co.chunksize); 263 seq_printf(m, "statesize : %u\n", skcipher->co.statesize); 264 } 265 266 static int __maybe_unused crypto_lskcipher_report( 267 struct sk_buff *skb, struct crypto_alg *alg) 268 { 269 struct lskcipher_alg *skcipher = __crypto_lskcipher_alg(alg); 270 struct crypto_report_blkcipher rblkcipher = { 271 .type = "lskcipher", 272 .geniv = "<none>", 273 }; 274 275 rblkcipher.blocksize = alg->cra_blocksize; 276 rblkcipher.min_keysize = skcipher->co.min_keysize; 277 rblkcipher.max_keysize = skcipher->co.max_keysize; 278 rblkcipher.ivsize = skcipher->co.ivsize; 279 280 return nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER, 281 sizeof(rblkcipher), &rblkcipher); 282 } 283 284 static const struct crypto_type crypto_lskcipher_type = { 285 .extsize = crypto_alg_extsize, 286 .init_tfm = crypto_lskcipher_init_tfm, 287 .free = crypto_lskcipher_free_instance, 288 #ifdef CONFIG_PROC_FS 289 .show = crypto_lskcipher_show, 290 #endif 291 #if IS_ENABLED(CONFIG_CRYPTO_USER) 292 .report = crypto_lskcipher_report, 293 #endif 294 .maskclear = ~CRYPTO_ALG_TYPE_MASK, 295 .maskset = CRYPTO_ALG_TYPE_MASK, 296 .type = CRYPTO_ALG_TYPE_LSKCIPHER, 297 .tfmsize = offsetof(struct crypto_lskcipher, base), 298 .algsize = offsetof(struct lskcipher_alg, co.base), 299 }; 300 301 static void crypto_lskcipher_exit_tfm_sg(struct crypto_tfm *tfm) 302 { 303 struct crypto_lskcipher **ctx = crypto_tfm_ctx(tfm); 304 305 crypto_free_lskcipher(*ctx); 306 } 307 308 int crypto_init_lskcipher_ops_sg(struct crypto_tfm *tfm) 309 { 310 struct crypto_lskcipher **ctx = crypto_tfm_ctx(tfm); 311 struct crypto_alg *calg = tfm->__crt_alg; 312 struct crypto_lskcipher *skcipher; 313 314 if (!crypto_mod_get(calg)) 315 return -EAGAIN; 316 317 skcipher = crypto_create_tfm(calg, &crypto_lskcipher_type); 318 if (IS_ERR(skcipher)) { 319 crypto_mod_put(calg); 320 return PTR_ERR(skcipher); 321 } 322 323 *ctx = skcipher; 324 tfm->exit = crypto_lskcipher_exit_tfm_sg; 325 326 return 0; 327 } 328 329 int crypto_grab_lskcipher(struct crypto_lskcipher_spawn *spawn, 330 struct crypto_instance *inst, 331 const char *name, u32 type, u32 mask) 332 { 333 spawn->base.frontend = &crypto_lskcipher_type; 334 return crypto_grab_spawn(&spawn->base, inst, name, type, mask); 335 } 336 EXPORT_SYMBOL_GPL(crypto_grab_lskcipher); 337 338 struct crypto_lskcipher *crypto_alloc_lskcipher(const char *alg_name, 339 u32 type, u32 mask) 340 { 341 return crypto_alloc_tfm(alg_name, &crypto_lskcipher_type, type, mask); 342 } 343 EXPORT_SYMBOL_GPL(crypto_alloc_lskcipher); 344 345 static int lskcipher_prepare_alg(struct lskcipher_alg *alg) 346 { 347 struct crypto_alg *base = &alg->co.base; 348 int err; 349 350 err = skcipher_prepare_alg_common(&alg->co); 351 if (err) 352 return err; 353 354 if (alg->co.chunksize & (alg->co.chunksize - 1)) 355 return -EINVAL; 356 357 base->cra_type = &crypto_lskcipher_type; 358 base->cra_flags |= CRYPTO_ALG_TYPE_LSKCIPHER; 359 360 return 0; 361 } 362 363 int crypto_register_lskcipher(struct lskcipher_alg *alg) 364 { 365 struct crypto_alg *base = &alg->co.base; 366 int err; 367 368 err = lskcipher_prepare_alg(alg); 369 if (err) 370 return err; 371 372 return crypto_register_alg(base); 373 } 374 EXPORT_SYMBOL_GPL(crypto_register_lskcipher); 375 376 void crypto_unregister_lskcipher(struct lskcipher_alg *alg) 377 { 378 crypto_unregister_alg(&alg->co.base); 379 } 380 EXPORT_SYMBOL_GPL(crypto_unregister_lskcipher); 381 382 int crypto_register_lskciphers(struct lskcipher_alg *algs, int count) 383 { 384 int i, ret; 385 386 for (i = 0; i < count; i++) { 387 ret = crypto_register_lskcipher(&algs[i]); 388 if (ret) { 389 crypto_unregister_lskciphers(algs, i); 390 return ret; 391 } 392 } 393 394 return 0; 395 } 396 EXPORT_SYMBOL_GPL(crypto_register_lskciphers); 397 398 void crypto_unregister_lskciphers(struct lskcipher_alg *algs, int count) 399 { 400 int i; 401 402 for (i = count - 1; i >= 0; --i) 403 crypto_unregister_lskcipher(&algs[i]); 404 } 405 EXPORT_SYMBOL_GPL(crypto_unregister_lskciphers); 406 407 int lskcipher_register_instance(struct crypto_template *tmpl, 408 struct lskcipher_instance *inst) 409 { 410 int err; 411 412 if (WARN_ON(!inst->free)) 413 return -EINVAL; 414 415 err = lskcipher_prepare_alg(&inst->alg); 416 if (err) 417 return err; 418 419 return crypto_register_instance(tmpl, lskcipher_crypto_instance(inst)); 420 } 421 EXPORT_SYMBOL_GPL(lskcipher_register_instance); 422 423 static int lskcipher_setkey_simple(struct crypto_lskcipher *tfm, const u8 *key, 424 unsigned int keylen) 425 { 426 struct crypto_lskcipher *cipher = lskcipher_cipher_simple(tfm); 427 428 crypto_lskcipher_clear_flags(cipher, CRYPTO_TFM_REQ_MASK); 429 crypto_lskcipher_set_flags(cipher, crypto_lskcipher_get_flags(tfm) & 430 CRYPTO_TFM_REQ_MASK); 431 return crypto_lskcipher_setkey(cipher, key, keylen); 432 } 433 434 static int lskcipher_init_tfm_simple(struct crypto_lskcipher *tfm) 435 { 436 struct lskcipher_instance *inst = lskcipher_alg_instance(tfm); 437 struct crypto_lskcipher **ctx = crypto_lskcipher_ctx(tfm); 438 struct crypto_lskcipher_spawn *spawn; 439 struct crypto_lskcipher *cipher; 440 441 spawn = lskcipher_instance_ctx(inst); 442 cipher = crypto_spawn_lskcipher(spawn); 443 if (IS_ERR(cipher)) 444 return PTR_ERR(cipher); 445 446 *ctx = cipher; 447 return 0; 448 } 449 450 static void lskcipher_exit_tfm_simple(struct crypto_lskcipher *tfm) 451 { 452 struct crypto_lskcipher **ctx = crypto_lskcipher_ctx(tfm); 453 454 crypto_free_lskcipher(*ctx); 455 } 456 457 static void lskcipher_free_instance_simple(struct lskcipher_instance *inst) 458 { 459 crypto_drop_lskcipher(lskcipher_instance_ctx(inst)); 460 kfree(inst); 461 } 462 463 /** 464 * lskcipher_alloc_instance_simple - allocate instance of simple block cipher 465 * 466 * Allocate an lskcipher_instance for a simple block cipher mode of operation, 467 * e.g. cbc or ecb. The instance context will have just a single crypto_spawn, 468 * that for the underlying cipher. The {min,max}_keysize, ivsize, blocksize, 469 * alignmask, and priority are set from the underlying cipher but can be 470 * overridden if needed. The tfm context defaults to 471 * struct crypto_lskcipher *, and default ->setkey(), ->init(), and 472 * ->exit() methods are installed. 473 * 474 * @tmpl: the template being instantiated 475 * @tb: the template parameters 476 * 477 * Return: a pointer to the new instance, or an ERR_PTR(). The caller still 478 * needs to register the instance. 479 */ 480 struct lskcipher_instance *lskcipher_alloc_instance_simple( 481 struct crypto_template *tmpl, struct rtattr **tb) 482 { 483 u32 mask; 484 struct lskcipher_instance *inst; 485 struct crypto_lskcipher_spawn *spawn; 486 char ecb_name[CRYPTO_MAX_ALG_NAME]; 487 struct lskcipher_alg *cipher_alg; 488 const char *cipher_name; 489 int err; 490 491 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_LSKCIPHER, &mask); 492 if (err) 493 return ERR_PTR(err); 494 495 cipher_name = crypto_attr_alg_name(tb[1]); 496 if (IS_ERR(cipher_name)) 497 return ERR_CAST(cipher_name); 498 499 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); 500 if (!inst) 501 return ERR_PTR(-ENOMEM); 502 503 spawn = lskcipher_instance_ctx(inst); 504 err = crypto_grab_lskcipher(spawn, 505 lskcipher_crypto_instance(inst), 506 cipher_name, 0, mask); 507 508 ecb_name[0] = 0; 509 if (err == -ENOENT && !!memcmp(tmpl->name, "ecb", 4)) { 510 err = -ENAMETOOLONG; 511 if (snprintf(ecb_name, CRYPTO_MAX_ALG_NAME, "ecb(%s)", 512 cipher_name) >= CRYPTO_MAX_ALG_NAME) 513 goto err_free_inst; 514 515 err = crypto_grab_lskcipher(spawn, 516 lskcipher_crypto_instance(inst), 517 ecb_name, 0, mask); 518 } 519 520 if (err) 521 goto err_free_inst; 522 523 cipher_alg = crypto_lskcipher_spawn_alg(spawn); 524 525 err = crypto_inst_setname(lskcipher_crypto_instance(inst), tmpl->name, 526 &cipher_alg->co.base); 527 if (err) 528 goto err_free_inst; 529 530 if (ecb_name[0]) { 531 int len; 532 533 err = -EINVAL; 534 len = strscpy(ecb_name, &cipher_alg->co.base.cra_name[4]); 535 if (len < 2) 536 goto err_free_inst; 537 538 if (ecb_name[len - 1] != ')') 539 goto err_free_inst; 540 541 ecb_name[len - 1] = 0; 542 543 err = -ENAMETOOLONG; 544 if (snprintf(inst->alg.co.base.cra_name, CRYPTO_MAX_ALG_NAME, 545 "%s(%s)", tmpl->name, ecb_name) >= 546 CRYPTO_MAX_ALG_NAME) 547 goto err_free_inst; 548 549 if (strcmp(ecb_name, cipher_name) && 550 snprintf(inst->alg.co.base.cra_driver_name, 551 CRYPTO_MAX_ALG_NAME, 552 "%s(%s)", tmpl->name, cipher_name) >= 553 CRYPTO_MAX_ALG_NAME) 554 goto err_free_inst; 555 } else { 556 /* Don't allow nesting. */ 557 err = -ELOOP; 558 if ((cipher_alg->co.base.cra_flags & CRYPTO_ALG_INSTANCE)) 559 goto err_free_inst; 560 } 561 562 err = -EINVAL; 563 if (cipher_alg->co.ivsize) 564 goto err_free_inst; 565 566 inst->free = lskcipher_free_instance_simple; 567 568 /* Default algorithm properties, can be overridden */ 569 inst->alg.co.base.cra_blocksize = cipher_alg->co.base.cra_blocksize; 570 inst->alg.co.base.cra_alignmask = cipher_alg->co.base.cra_alignmask; 571 inst->alg.co.base.cra_priority = cipher_alg->co.base.cra_priority; 572 inst->alg.co.min_keysize = cipher_alg->co.min_keysize; 573 inst->alg.co.max_keysize = cipher_alg->co.max_keysize; 574 inst->alg.co.ivsize = cipher_alg->co.base.cra_blocksize; 575 inst->alg.co.statesize = cipher_alg->co.statesize; 576 577 /* Use struct crypto_lskcipher * by default, can be overridden */ 578 inst->alg.co.base.cra_ctxsize = sizeof(struct crypto_lskcipher *); 579 inst->alg.setkey = lskcipher_setkey_simple; 580 inst->alg.init = lskcipher_init_tfm_simple; 581 inst->alg.exit = lskcipher_exit_tfm_simple; 582 583 return inst; 584 585 err_free_inst: 586 lskcipher_free_instance_simple(inst); 587 return ERR_PTR(err); 588 } 589 EXPORT_SYMBOL_GPL(lskcipher_alloc_instance_simple); 590