1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Symmetric key cipher operations. 4 * 5 * Generic encrypt/decrypt wrapper for ciphers, handles operations across 6 * multiple page boundaries by using temporary blocks. In user context, 7 * the kernel is given a chance to schedule us once per page. 8 * 9 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au> 10 */ 11 12 #include <crypto/internal/aead.h> 13 #include <crypto/internal/cipher.h> 14 #include <crypto/internal/skcipher.h> 15 #include <crypto/scatterwalk.h> 16 #include <linux/bug.h> 17 #include <linux/cryptouser.h> 18 #include <linux/err.h> 19 #include <linux/kernel.h> 20 #include <linux/mm.h> 21 #include <linux/module.h> 22 #include <linux/seq_file.h> 23 #include <linux/slab.h> 24 #include <linux/string.h> 25 #include <linux/string_choices.h> 26 #include <net/netlink.h> 27 #include "skcipher.h" 28 29 #define CRYPTO_ALG_TYPE_SKCIPHER_MASK 0x0000000e 30 31 enum { 32 SKCIPHER_WALK_SLOW = 1 << 0, 33 SKCIPHER_WALK_COPY = 1 << 1, 34 SKCIPHER_WALK_DIFF = 1 << 2, 35 SKCIPHER_WALK_SLEEP = 1 << 3, 36 }; 37 38 static const struct crypto_type crypto_skcipher_type; 39 40 static int skcipher_walk_next(struct skcipher_walk *walk); 41 42 static inline void skcipher_map_src(struct skcipher_walk *walk) 43 { 44 walk->src.virt.addr = scatterwalk_map(&walk->in); 45 } 46 47 static inline void skcipher_map_dst(struct skcipher_walk *walk) 48 { 49 walk->dst.virt.addr = scatterwalk_map(&walk->out); 50 } 51 52 static inline gfp_t skcipher_walk_gfp(struct skcipher_walk *walk) 53 { 54 return walk->flags & SKCIPHER_WALK_SLEEP ? GFP_KERNEL : GFP_ATOMIC; 55 } 56 57 static inline struct skcipher_alg *__crypto_skcipher_alg( 58 struct crypto_alg *alg) 59 { 60 return container_of(alg, struct skcipher_alg, base); 61 } 62 63 /** 64 * skcipher_walk_done() - finish one step of a skcipher_walk 65 * @walk: the skcipher_walk 66 * @res: number of bytes *not* processed (>= 0) from walk->nbytes, 67 * or a -errno value to terminate the walk due to an error 68 * 69 * This function cleans up after one step of walking through the source and 70 * destination scatterlists, and advances to the next step if applicable. 71 * walk->nbytes is set to the number of bytes available in the next step, 72 * walk->total is set to the new total number of bytes remaining, and 73 * walk->{src,dst}.virt.addr is set to the next pair of data pointers. If there 74 * is no more data, or if an error occurred (i.e. -errno return), then 75 * walk->nbytes and walk->total are set to 0 and all resources owned by the 76 * skcipher_walk are freed. 77 * 78 * Return: 0 or a -errno value. If @res was a -errno value then it will be 79 * returned, but other errors may occur too. 80 */ 81 int skcipher_walk_done(struct skcipher_walk *walk, int res) 82 { 83 unsigned int n = walk->nbytes; /* num bytes processed this step */ 84 unsigned int total = 0; /* new total remaining */ 85 86 if (!n) 87 goto finish; 88 89 if (likely(res >= 0)) { 90 n -= res; /* subtract num bytes *not* processed */ 91 total = walk->total - n; 92 } 93 94 if (likely(!(walk->flags & (SKCIPHER_WALK_SLOW | 95 SKCIPHER_WALK_COPY | 96 SKCIPHER_WALK_DIFF)))) { 97 scatterwalk_advance(&walk->in, n); 98 } else if (walk->flags & SKCIPHER_WALK_DIFF) { 99 scatterwalk_unmap(walk->src.virt.addr); 100 scatterwalk_advance(&walk->in, n); 101 } else if (walk->flags & SKCIPHER_WALK_COPY) { 102 scatterwalk_advance(&walk->in, n); 103 skcipher_map_dst(walk); 104 memcpy(walk->dst.virt.addr, walk->page, n); 105 } else { /* SKCIPHER_WALK_SLOW */ 106 if (res > 0) { 107 /* 108 * Didn't process all bytes. Either the algorithm is 109 * broken, or this was the last step and it turned out 110 * the message wasn't evenly divisible into blocks but 111 * the algorithm requires it. 112 */ 113 res = -EINVAL; 114 total = 0; 115 } else { 116 u8 *buf = PTR_ALIGN(walk->buffer, walk->alignmask + 1); 117 118 memcpy_to_scatterwalk(&walk->out, buf, n); 119 } 120 goto dst_done; 121 } 122 123 scatterwalk_done_dst(&walk->out, walk->dst.virt.addr, n); 124 dst_done: 125 126 if (res > 0) 127 res = 0; 128 129 walk->total = total; 130 walk->nbytes = 0; 131 132 if (total) { 133 if (walk->flags & SKCIPHER_WALK_SLEEP) 134 cond_resched(); 135 walk->flags &= ~(SKCIPHER_WALK_SLOW | SKCIPHER_WALK_COPY | 136 SKCIPHER_WALK_DIFF); 137 return skcipher_walk_next(walk); 138 } 139 140 finish: 141 /* Short-circuit for the common/fast path. */ 142 if (!((unsigned long)walk->buffer | (unsigned long)walk->page)) 143 goto out; 144 145 if (walk->iv != walk->oiv) 146 memcpy(walk->oiv, walk->iv, walk->ivsize); 147 if (walk->buffer != walk->page) 148 kfree(walk->buffer); 149 if (walk->page) 150 free_page((unsigned long)walk->page); 151 152 out: 153 return res; 154 } 155 EXPORT_SYMBOL_GPL(skcipher_walk_done); 156 157 static int skcipher_next_slow(struct skcipher_walk *walk, unsigned int bsize) 158 { 159 unsigned alignmask = walk->alignmask; 160 unsigned n; 161 u8 *buffer; 162 163 if (!walk->buffer) 164 walk->buffer = walk->page; 165 buffer = walk->buffer; 166 if (!buffer) { 167 /* Min size for a buffer of bsize bytes aligned to alignmask */ 168 n = bsize + (alignmask & ~(crypto_tfm_ctx_alignment() - 1)); 169 170 buffer = kzalloc(n, skcipher_walk_gfp(walk)); 171 if (!buffer) 172 return skcipher_walk_done(walk, -ENOMEM); 173 walk->buffer = buffer; 174 } 175 walk->dst.virt.addr = PTR_ALIGN(buffer, alignmask + 1); 176 walk->src.virt.addr = walk->dst.virt.addr; 177 178 memcpy_from_scatterwalk(walk->src.virt.addr, &walk->in, bsize); 179 180 walk->nbytes = bsize; 181 walk->flags |= SKCIPHER_WALK_SLOW; 182 183 return 0; 184 } 185 186 static int skcipher_next_copy(struct skcipher_walk *walk) 187 { 188 u8 *tmp = walk->page; 189 190 skcipher_map_src(walk); 191 memcpy(tmp, walk->src.virt.addr, walk->nbytes); 192 scatterwalk_unmap(walk->src.virt.addr); 193 /* 194 * walk->in is advanced later when the number of bytes actually 195 * processed (which might be less than walk->nbytes) is known. 196 */ 197 198 walk->src.virt.addr = tmp; 199 walk->dst.virt.addr = tmp; 200 return 0; 201 } 202 203 static int skcipher_next_fast(struct skcipher_walk *walk) 204 { 205 unsigned long diff; 206 207 diff = offset_in_page(walk->in.offset) - 208 offset_in_page(walk->out.offset); 209 diff |= (u8 *)(sg_page(walk->in.sg) + (walk->in.offset >> PAGE_SHIFT)) - 210 (u8 *)(sg_page(walk->out.sg) + (walk->out.offset >> PAGE_SHIFT)); 211 212 skcipher_map_src(walk); 213 walk->dst.virt.addr = walk->src.virt.addr; 214 215 if (diff) { 216 walk->flags |= SKCIPHER_WALK_DIFF; 217 skcipher_map_dst(walk); 218 } 219 220 return 0; 221 } 222 223 static int skcipher_walk_next(struct skcipher_walk *walk) 224 { 225 unsigned int bsize; 226 unsigned int n; 227 228 n = walk->total; 229 bsize = min(walk->stride, max(n, walk->blocksize)); 230 n = scatterwalk_clamp(&walk->in, n); 231 n = scatterwalk_clamp(&walk->out, n); 232 233 if (unlikely(n < bsize)) { 234 if (unlikely(walk->total < walk->blocksize)) 235 return skcipher_walk_done(walk, -EINVAL); 236 237 slow_path: 238 return skcipher_next_slow(walk, bsize); 239 } 240 walk->nbytes = n; 241 242 if (unlikely((walk->in.offset | walk->out.offset) & walk->alignmask)) { 243 if (!walk->page) { 244 gfp_t gfp = skcipher_walk_gfp(walk); 245 246 walk->page = (void *)__get_free_page(gfp); 247 if (!walk->page) 248 goto slow_path; 249 } 250 walk->flags |= SKCIPHER_WALK_COPY; 251 return skcipher_next_copy(walk); 252 } 253 254 return skcipher_next_fast(walk); 255 } 256 257 static int skcipher_copy_iv(struct skcipher_walk *walk) 258 { 259 unsigned alignmask = walk->alignmask; 260 unsigned ivsize = walk->ivsize; 261 unsigned aligned_stride = ALIGN(walk->stride, alignmask + 1); 262 unsigned size; 263 u8 *iv; 264 265 /* Min size for a buffer of stride + ivsize, aligned to alignmask */ 266 size = aligned_stride + ivsize + 267 (alignmask & ~(crypto_tfm_ctx_alignment() - 1)); 268 269 walk->buffer = kmalloc(size, skcipher_walk_gfp(walk)); 270 if (!walk->buffer) 271 return -ENOMEM; 272 273 iv = PTR_ALIGN(walk->buffer, alignmask + 1) + aligned_stride; 274 275 walk->iv = memcpy(iv, walk->iv, walk->ivsize); 276 return 0; 277 } 278 279 static int skcipher_walk_first(struct skcipher_walk *walk) 280 { 281 if (WARN_ON_ONCE(in_hardirq())) 282 return -EDEADLK; 283 284 walk->buffer = NULL; 285 if (unlikely(((unsigned long)walk->iv & walk->alignmask))) { 286 int err = skcipher_copy_iv(walk); 287 if (err) 288 return err; 289 } 290 291 walk->page = NULL; 292 293 return skcipher_walk_next(walk); 294 } 295 296 int skcipher_walk_virt(struct skcipher_walk *__restrict walk, 297 struct skcipher_request *__restrict req, bool atomic) 298 { 299 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 300 struct skcipher_alg *alg; 301 302 might_sleep_if(req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP); 303 304 alg = crypto_skcipher_alg(tfm); 305 306 walk->total = req->cryptlen; 307 walk->nbytes = 0; 308 walk->iv = req->iv; 309 walk->oiv = req->iv; 310 if ((req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) && !atomic) 311 walk->flags = SKCIPHER_WALK_SLEEP; 312 else 313 walk->flags = 0; 314 315 if (unlikely(!walk->total)) 316 return 0; 317 318 scatterwalk_start(&walk->in, req->src); 319 scatterwalk_start(&walk->out, req->dst); 320 321 walk->blocksize = crypto_skcipher_blocksize(tfm); 322 walk->ivsize = crypto_skcipher_ivsize(tfm); 323 walk->alignmask = crypto_skcipher_alignmask(tfm); 324 325 if (alg->co.base.cra_type != &crypto_skcipher_type) 326 walk->stride = alg->co.chunksize; 327 else 328 walk->stride = alg->walksize; 329 330 return skcipher_walk_first(walk); 331 } 332 EXPORT_SYMBOL_GPL(skcipher_walk_virt); 333 334 static int skcipher_walk_aead_common(struct skcipher_walk *__restrict walk, 335 struct aead_request *__restrict req, 336 bool atomic) 337 { 338 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 339 340 walk->nbytes = 0; 341 walk->iv = req->iv; 342 walk->oiv = req->iv; 343 if ((req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) && !atomic) 344 walk->flags = SKCIPHER_WALK_SLEEP; 345 else 346 walk->flags = 0; 347 348 if (unlikely(!walk->total)) 349 return 0; 350 351 scatterwalk_start_at_pos(&walk->in, req->src, req->assoclen); 352 scatterwalk_start_at_pos(&walk->out, req->dst, req->assoclen); 353 354 walk->blocksize = crypto_aead_blocksize(tfm); 355 walk->stride = crypto_aead_chunksize(tfm); 356 walk->ivsize = crypto_aead_ivsize(tfm); 357 walk->alignmask = crypto_aead_alignmask(tfm); 358 359 return skcipher_walk_first(walk); 360 } 361 362 int skcipher_walk_aead_encrypt(struct skcipher_walk *__restrict walk, 363 struct aead_request *__restrict req, 364 bool atomic) 365 { 366 walk->total = req->cryptlen; 367 368 return skcipher_walk_aead_common(walk, req, atomic); 369 } 370 EXPORT_SYMBOL_GPL(skcipher_walk_aead_encrypt); 371 372 int skcipher_walk_aead_decrypt(struct skcipher_walk *__restrict walk, 373 struct aead_request *__restrict req, 374 bool atomic) 375 { 376 struct crypto_aead *tfm = crypto_aead_reqtfm(req); 377 378 walk->total = req->cryptlen - crypto_aead_authsize(tfm); 379 380 return skcipher_walk_aead_common(walk, req, atomic); 381 } 382 EXPORT_SYMBOL_GPL(skcipher_walk_aead_decrypt); 383 384 static void skcipher_set_needkey(struct crypto_skcipher *tfm) 385 { 386 if (crypto_skcipher_max_keysize(tfm) != 0) 387 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_NEED_KEY); 388 } 389 390 static int skcipher_setkey_unaligned(struct crypto_skcipher *tfm, 391 const u8 *key, unsigned int keylen) 392 { 393 unsigned long alignmask = crypto_skcipher_alignmask(tfm); 394 struct skcipher_alg *cipher = crypto_skcipher_alg(tfm); 395 u8 *buffer, *alignbuffer; 396 unsigned long absize; 397 int ret; 398 399 absize = keylen + alignmask; 400 buffer = kmalloc(absize, GFP_ATOMIC); 401 if (!buffer) 402 return -ENOMEM; 403 404 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1); 405 memcpy(alignbuffer, key, keylen); 406 ret = cipher->setkey(tfm, alignbuffer, keylen); 407 kfree_sensitive(buffer); 408 return ret; 409 } 410 411 int crypto_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key, 412 unsigned int keylen) 413 { 414 struct skcipher_alg *cipher = crypto_skcipher_alg(tfm); 415 unsigned long alignmask = crypto_skcipher_alignmask(tfm); 416 int err; 417 418 if (cipher->co.base.cra_type != &crypto_skcipher_type) { 419 struct crypto_lskcipher **ctx = crypto_skcipher_ctx(tfm); 420 421 crypto_lskcipher_clear_flags(*ctx, CRYPTO_TFM_REQ_MASK); 422 crypto_lskcipher_set_flags(*ctx, 423 crypto_skcipher_get_flags(tfm) & 424 CRYPTO_TFM_REQ_MASK); 425 err = crypto_lskcipher_setkey(*ctx, key, keylen); 426 goto out; 427 } 428 429 if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) 430 return -EINVAL; 431 432 if ((unsigned long)key & alignmask) 433 err = skcipher_setkey_unaligned(tfm, key, keylen); 434 else 435 err = cipher->setkey(tfm, key, keylen); 436 437 out: 438 if (unlikely(err)) { 439 skcipher_set_needkey(tfm); 440 return err; 441 } 442 443 crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY); 444 return 0; 445 } 446 EXPORT_SYMBOL_GPL(crypto_skcipher_setkey); 447 448 int crypto_skcipher_encrypt(struct skcipher_request *req) 449 { 450 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 451 struct skcipher_alg *alg = crypto_skcipher_alg(tfm); 452 453 if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) 454 return -ENOKEY; 455 if (alg->co.base.cra_type != &crypto_skcipher_type) 456 return crypto_lskcipher_encrypt_sg(req); 457 return alg->encrypt(req); 458 } 459 EXPORT_SYMBOL_GPL(crypto_skcipher_encrypt); 460 461 int crypto_skcipher_decrypt(struct skcipher_request *req) 462 { 463 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 464 struct skcipher_alg *alg = crypto_skcipher_alg(tfm); 465 466 if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) 467 return -ENOKEY; 468 if (alg->co.base.cra_type != &crypto_skcipher_type) 469 return crypto_lskcipher_decrypt_sg(req); 470 return alg->decrypt(req); 471 } 472 EXPORT_SYMBOL_GPL(crypto_skcipher_decrypt); 473 474 static int crypto_lskcipher_export(struct skcipher_request *req, void *out) 475 { 476 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 477 u8 *ivs = skcipher_request_ctx(req); 478 479 ivs = PTR_ALIGN(ivs, crypto_skcipher_alignmask(tfm) + 1); 480 481 memcpy(out, ivs + crypto_skcipher_ivsize(tfm), 482 crypto_skcipher_statesize(tfm)); 483 484 return 0; 485 } 486 487 static int crypto_lskcipher_import(struct skcipher_request *req, const void *in) 488 { 489 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 490 u8 *ivs = skcipher_request_ctx(req); 491 492 ivs = PTR_ALIGN(ivs, crypto_skcipher_alignmask(tfm) + 1); 493 494 memcpy(ivs + crypto_skcipher_ivsize(tfm), in, 495 crypto_skcipher_statesize(tfm)); 496 497 return 0; 498 } 499 500 static int skcipher_noexport(struct skcipher_request *req, void *out) 501 { 502 return 0; 503 } 504 505 static int skcipher_noimport(struct skcipher_request *req, const void *in) 506 { 507 return 0; 508 } 509 510 int crypto_skcipher_export(struct skcipher_request *req, void *out) 511 { 512 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 513 struct skcipher_alg *alg = crypto_skcipher_alg(tfm); 514 515 if (alg->co.base.cra_type != &crypto_skcipher_type) 516 return crypto_lskcipher_export(req, out); 517 return alg->export(req, out); 518 } 519 EXPORT_SYMBOL_GPL(crypto_skcipher_export); 520 521 int crypto_skcipher_import(struct skcipher_request *req, const void *in) 522 { 523 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 524 struct skcipher_alg *alg = crypto_skcipher_alg(tfm); 525 526 if (alg->co.base.cra_type != &crypto_skcipher_type) 527 return crypto_lskcipher_import(req, in); 528 return alg->import(req, in); 529 } 530 EXPORT_SYMBOL_GPL(crypto_skcipher_import); 531 532 static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm) 533 { 534 struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm); 535 struct skcipher_alg *alg = crypto_skcipher_alg(skcipher); 536 537 alg->exit(skcipher); 538 } 539 540 static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm) 541 { 542 struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm); 543 struct skcipher_alg *alg = crypto_skcipher_alg(skcipher); 544 545 skcipher_set_needkey(skcipher); 546 547 if (tfm->__crt_alg->cra_type != &crypto_skcipher_type) { 548 unsigned am = crypto_skcipher_alignmask(skcipher); 549 unsigned reqsize; 550 551 reqsize = am & ~(crypto_tfm_ctx_alignment() - 1); 552 reqsize += crypto_skcipher_ivsize(skcipher); 553 reqsize += crypto_skcipher_statesize(skcipher); 554 crypto_skcipher_set_reqsize(skcipher, reqsize); 555 556 return crypto_init_lskcipher_ops_sg(tfm); 557 } 558 559 if (alg->exit) 560 skcipher->base.exit = crypto_skcipher_exit_tfm; 561 562 if (alg->init) 563 return alg->init(skcipher); 564 565 return 0; 566 } 567 568 static unsigned int crypto_skcipher_extsize(struct crypto_alg *alg) 569 { 570 if (alg->cra_type != &crypto_skcipher_type) 571 return sizeof(struct crypto_lskcipher *); 572 573 return crypto_alg_extsize(alg); 574 } 575 576 static void crypto_skcipher_free_instance(struct crypto_instance *inst) 577 { 578 struct skcipher_instance *skcipher = 579 container_of(inst, struct skcipher_instance, s.base); 580 581 skcipher->free(skcipher); 582 } 583 584 static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg) 585 __maybe_unused; 586 static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg) 587 { 588 struct skcipher_alg *skcipher = __crypto_skcipher_alg(alg); 589 590 seq_printf(m, "type : skcipher\n"); 591 seq_printf(m, "async : %s\n", 592 str_yes_no(alg->cra_flags & CRYPTO_ALG_ASYNC)); 593 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize); 594 seq_printf(m, "min keysize : %u\n", skcipher->min_keysize); 595 seq_printf(m, "max keysize : %u\n", skcipher->max_keysize); 596 seq_printf(m, "ivsize : %u\n", skcipher->ivsize); 597 seq_printf(m, "chunksize : %u\n", skcipher->chunksize); 598 seq_printf(m, "walksize : %u\n", skcipher->walksize); 599 seq_printf(m, "statesize : %u\n", skcipher->statesize); 600 } 601 602 static int __maybe_unused crypto_skcipher_report( 603 struct sk_buff *skb, struct crypto_alg *alg) 604 { 605 struct skcipher_alg *skcipher = __crypto_skcipher_alg(alg); 606 struct crypto_report_blkcipher rblkcipher; 607 608 memset(&rblkcipher, 0, sizeof(rblkcipher)); 609 610 strscpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type)); 611 strscpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv)); 612 613 rblkcipher.blocksize = alg->cra_blocksize; 614 rblkcipher.min_keysize = skcipher->min_keysize; 615 rblkcipher.max_keysize = skcipher->max_keysize; 616 rblkcipher.ivsize = skcipher->ivsize; 617 618 return nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER, 619 sizeof(rblkcipher), &rblkcipher); 620 } 621 622 static const struct crypto_type crypto_skcipher_type = { 623 .extsize = crypto_skcipher_extsize, 624 .init_tfm = crypto_skcipher_init_tfm, 625 .free = crypto_skcipher_free_instance, 626 #ifdef CONFIG_PROC_FS 627 .show = crypto_skcipher_show, 628 #endif 629 #if IS_ENABLED(CONFIG_CRYPTO_USER) 630 .report = crypto_skcipher_report, 631 #endif 632 .maskclear = ~CRYPTO_ALG_TYPE_MASK, 633 .maskset = CRYPTO_ALG_TYPE_SKCIPHER_MASK, 634 .type = CRYPTO_ALG_TYPE_SKCIPHER, 635 .tfmsize = offsetof(struct crypto_skcipher, base), 636 }; 637 638 int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, 639 struct crypto_instance *inst, 640 const char *name, u32 type, u32 mask) 641 { 642 spawn->base.frontend = &crypto_skcipher_type; 643 return crypto_grab_spawn(&spawn->base, inst, name, type, mask); 644 } 645 EXPORT_SYMBOL_GPL(crypto_grab_skcipher); 646 647 struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name, 648 u32 type, u32 mask) 649 { 650 return crypto_alloc_tfm(alg_name, &crypto_skcipher_type, type, mask); 651 } 652 EXPORT_SYMBOL_GPL(crypto_alloc_skcipher); 653 654 struct crypto_sync_skcipher *crypto_alloc_sync_skcipher( 655 const char *alg_name, u32 type, u32 mask) 656 { 657 struct crypto_skcipher *tfm; 658 659 /* Only sync algorithms allowed. */ 660 mask |= CRYPTO_ALG_ASYNC | CRYPTO_ALG_SKCIPHER_REQSIZE_LARGE; 661 type &= ~(CRYPTO_ALG_ASYNC | CRYPTO_ALG_SKCIPHER_REQSIZE_LARGE); 662 663 tfm = crypto_alloc_tfm(alg_name, &crypto_skcipher_type, type, mask); 664 665 /* 666 * Make sure we do not allocate something that might get used with 667 * an on-stack request: check the request size. 668 */ 669 if (!IS_ERR(tfm) && WARN_ON(crypto_skcipher_reqsize(tfm) > 670 MAX_SYNC_SKCIPHER_REQSIZE)) { 671 crypto_free_skcipher(tfm); 672 return ERR_PTR(-EINVAL); 673 } 674 675 return (struct crypto_sync_skcipher *)tfm; 676 } 677 EXPORT_SYMBOL_GPL(crypto_alloc_sync_skcipher); 678 679 int crypto_has_skcipher(const char *alg_name, u32 type, u32 mask) 680 { 681 return crypto_type_has_alg(alg_name, &crypto_skcipher_type, type, mask); 682 } 683 EXPORT_SYMBOL_GPL(crypto_has_skcipher); 684 685 int skcipher_prepare_alg_common(struct skcipher_alg_common *alg) 686 { 687 struct crypto_alg *base = &alg->base; 688 689 if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8 || 690 alg->statesize > PAGE_SIZE / 2 || 691 (alg->ivsize + alg->statesize) > PAGE_SIZE / 2) 692 return -EINVAL; 693 694 if (!alg->chunksize) 695 alg->chunksize = base->cra_blocksize; 696 697 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; 698 699 return 0; 700 } 701 702 static int skcipher_prepare_alg(struct skcipher_alg *alg) 703 { 704 struct crypto_alg *base = &alg->base; 705 int err; 706 707 err = skcipher_prepare_alg_common(&alg->co); 708 if (err) 709 return err; 710 711 if (alg->walksize > PAGE_SIZE / 8) 712 return -EINVAL; 713 714 if (!alg->walksize) 715 alg->walksize = alg->chunksize; 716 717 if (!alg->statesize) { 718 alg->import = skcipher_noimport; 719 alg->export = skcipher_noexport; 720 } else if (!(alg->import && alg->export)) 721 return -EINVAL; 722 723 base->cra_type = &crypto_skcipher_type; 724 base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER; 725 726 return 0; 727 } 728 729 int crypto_register_skcipher(struct skcipher_alg *alg) 730 { 731 struct crypto_alg *base = &alg->base; 732 int err; 733 734 err = skcipher_prepare_alg(alg); 735 if (err) 736 return err; 737 738 return crypto_register_alg(base); 739 } 740 EXPORT_SYMBOL_GPL(crypto_register_skcipher); 741 742 void crypto_unregister_skcipher(struct skcipher_alg *alg) 743 { 744 crypto_unregister_alg(&alg->base); 745 } 746 EXPORT_SYMBOL_GPL(crypto_unregister_skcipher); 747 748 int crypto_register_skciphers(struct skcipher_alg *algs, int count) 749 { 750 int i, ret; 751 752 for (i = 0; i < count; i++) { 753 ret = crypto_register_skcipher(&algs[i]); 754 if (ret) 755 goto err; 756 } 757 758 return 0; 759 760 err: 761 for (--i; i >= 0; --i) 762 crypto_unregister_skcipher(&algs[i]); 763 764 return ret; 765 } 766 EXPORT_SYMBOL_GPL(crypto_register_skciphers); 767 768 void crypto_unregister_skciphers(struct skcipher_alg *algs, int count) 769 { 770 int i; 771 772 for (i = count - 1; i >= 0; --i) 773 crypto_unregister_skcipher(&algs[i]); 774 } 775 EXPORT_SYMBOL_GPL(crypto_unregister_skciphers); 776 777 int skcipher_register_instance(struct crypto_template *tmpl, 778 struct skcipher_instance *inst) 779 { 780 int err; 781 782 if (WARN_ON(!inst->free)) 783 return -EINVAL; 784 785 err = skcipher_prepare_alg(&inst->alg); 786 if (err) 787 return err; 788 789 return crypto_register_instance(tmpl, skcipher_crypto_instance(inst)); 790 } 791 EXPORT_SYMBOL_GPL(skcipher_register_instance); 792 793 static int skcipher_setkey_simple(struct crypto_skcipher *tfm, const u8 *key, 794 unsigned int keylen) 795 { 796 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm); 797 798 crypto_cipher_clear_flags(cipher, CRYPTO_TFM_REQ_MASK); 799 crypto_cipher_set_flags(cipher, crypto_skcipher_get_flags(tfm) & 800 CRYPTO_TFM_REQ_MASK); 801 return crypto_cipher_setkey(cipher, key, keylen); 802 } 803 804 static int skcipher_init_tfm_simple(struct crypto_skcipher *tfm) 805 { 806 struct skcipher_instance *inst = skcipher_alg_instance(tfm); 807 struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst); 808 struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm); 809 struct crypto_cipher *cipher; 810 811 cipher = crypto_spawn_cipher(spawn); 812 if (IS_ERR(cipher)) 813 return PTR_ERR(cipher); 814 815 ctx->cipher = cipher; 816 return 0; 817 } 818 819 static void skcipher_exit_tfm_simple(struct crypto_skcipher *tfm) 820 { 821 struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm); 822 823 crypto_free_cipher(ctx->cipher); 824 } 825 826 static void skcipher_free_instance_simple(struct skcipher_instance *inst) 827 { 828 crypto_drop_cipher(skcipher_instance_ctx(inst)); 829 kfree(inst); 830 } 831 832 /** 833 * skcipher_alloc_instance_simple - allocate instance of simple block cipher mode 834 * 835 * Allocate an skcipher_instance for a simple block cipher mode of operation, 836 * e.g. cbc or ecb. The instance context will have just a single crypto_spawn, 837 * that for the underlying cipher. The {min,max}_keysize, ivsize, blocksize, 838 * alignmask, and priority are set from the underlying cipher but can be 839 * overridden if needed. The tfm context defaults to skcipher_ctx_simple, and 840 * default ->setkey(), ->init(), and ->exit() methods are installed. 841 * 842 * @tmpl: the template being instantiated 843 * @tb: the template parameters 844 * 845 * Return: a pointer to the new instance, or an ERR_PTR(). The caller still 846 * needs to register the instance. 847 */ 848 struct skcipher_instance *skcipher_alloc_instance_simple( 849 struct crypto_template *tmpl, struct rtattr **tb) 850 { 851 u32 mask; 852 struct skcipher_instance *inst; 853 struct crypto_cipher_spawn *spawn; 854 struct crypto_alg *cipher_alg; 855 int err; 856 857 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask); 858 if (err) 859 return ERR_PTR(err); 860 861 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); 862 if (!inst) 863 return ERR_PTR(-ENOMEM); 864 spawn = skcipher_instance_ctx(inst); 865 866 err = crypto_grab_cipher(spawn, skcipher_crypto_instance(inst), 867 crypto_attr_alg_name(tb[1]), 0, mask); 868 if (err) 869 goto err_free_inst; 870 cipher_alg = crypto_spawn_cipher_alg(spawn); 871 872 err = crypto_inst_setname(skcipher_crypto_instance(inst), tmpl->name, 873 cipher_alg); 874 if (err) 875 goto err_free_inst; 876 877 inst->free = skcipher_free_instance_simple; 878 879 /* Default algorithm properties, can be overridden */ 880 inst->alg.base.cra_blocksize = cipher_alg->cra_blocksize; 881 inst->alg.base.cra_alignmask = cipher_alg->cra_alignmask; 882 inst->alg.base.cra_priority = cipher_alg->cra_priority; 883 inst->alg.min_keysize = cipher_alg->cra_cipher.cia_min_keysize; 884 inst->alg.max_keysize = cipher_alg->cra_cipher.cia_max_keysize; 885 inst->alg.ivsize = cipher_alg->cra_blocksize; 886 887 /* Use skcipher_ctx_simple by default, can be overridden */ 888 inst->alg.base.cra_ctxsize = sizeof(struct skcipher_ctx_simple); 889 inst->alg.setkey = skcipher_setkey_simple; 890 inst->alg.init = skcipher_init_tfm_simple; 891 inst->alg.exit = skcipher_exit_tfm_simple; 892 893 return inst; 894 895 err_free_inst: 896 skcipher_free_instance_simple(inst); 897 return ERR_PTR(err); 898 } 899 EXPORT_SYMBOL_GPL(skcipher_alloc_instance_simple); 900 901 MODULE_LICENSE("GPL"); 902 MODULE_DESCRIPTION("Symmetric key cipher type"); 903 MODULE_IMPORT_NS("CRYPTO_INTERNAL"); 904