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