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