linux/crypto/skcipher.c
<<
>>
Prefs
   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/skcipher.h>
  14#include <crypto/scatterwalk.h>
  15#include <linux/bug.h>
  16#include <linux/cryptouser.h>
  17#include <linux/compiler.h>
  18#include <linux/list.h>
  19#include <linux/module.h>
  20#include <linux/rtnetlink.h>
  21#include <linux/seq_file.h>
  22#include <net/netlink.h>
  23
  24#include "internal.h"
  25
  26enum {
  27        SKCIPHER_WALK_PHYS = 1 << 0,
  28        SKCIPHER_WALK_SLOW = 1 << 1,
  29        SKCIPHER_WALK_COPY = 1 << 2,
  30        SKCIPHER_WALK_DIFF = 1 << 3,
  31        SKCIPHER_WALK_SLEEP = 1 << 4,
  32};
  33
  34struct skcipher_walk_buffer {
  35        struct list_head entry;
  36        struct scatter_walk dst;
  37        unsigned int len;
  38        u8 *data;
  39        u8 buffer[];
  40};
  41
  42static int skcipher_walk_next(struct skcipher_walk *walk);
  43
  44static inline void skcipher_unmap(struct scatter_walk *walk, void *vaddr)
  45{
  46        if (PageHighMem(scatterwalk_page(walk)))
  47                kunmap_atomic(vaddr);
  48}
  49
  50static inline void *skcipher_map(struct scatter_walk *walk)
  51{
  52        struct page *page = scatterwalk_page(walk);
  53
  54        return (PageHighMem(page) ? kmap_atomic(page) : page_address(page)) +
  55               offset_in_page(walk->offset);
  56}
  57
  58static inline void skcipher_map_src(struct skcipher_walk *walk)
  59{
  60        walk->src.virt.addr = skcipher_map(&walk->in);
  61}
  62
  63static inline void skcipher_map_dst(struct skcipher_walk *walk)
  64{
  65        walk->dst.virt.addr = skcipher_map(&walk->out);
  66}
  67
  68static inline void skcipher_unmap_src(struct skcipher_walk *walk)
  69{
  70        skcipher_unmap(&walk->in, walk->src.virt.addr);
  71}
  72
  73static inline void skcipher_unmap_dst(struct skcipher_walk *walk)
  74{
  75        skcipher_unmap(&walk->out, walk->dst.virt.addr);
  76}
  77
  78static inline gfp_t skcipher_walk_gfp(struct skcipher_walk *walk)
  79{
  80        return walk->flags & SKCIPHER_WALK_SLEEP ? GFP_KERNEL : GFP_ATOMIC;
  81}
  82
  83/* Get a spot of the specified length that does not straddle a page.
  84 * The caller needs to ensure that there is enough space for this operation.
  85 */
  86static inline u8 *skcipher_get_spot(u8 *start, unsigned int len)
  87{
  88        u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
  89
  90        return max(start, end_page);
  91}
  92
  93static int skcipher_done_slow(struct skcipher_walk *walk, unsigned int bsize)
  94{
  95        u8 *addr;
  96
  97        addr = (u8 *)ALIGN((unsigned long)walk->buffer, walk->alignmask + 1);
  98        addr = skcipher_get_spot(addr, bsize);
  99        scatterwalk_copychunks(addr, &walk->out, bsize,
 100                               (walk->flags & SKCIPHER_WALK_PHYS) ? 2 : 1);
 101        return 0;
 102}
 103
 104int skcipher_walk_done(struct skcipher_walk *walk, int err)
 105{
 106        unsigned int n = walk->nbytes;
 107        unsigned int nbytes = 0;
 108
 109        if (!n)
 110                goto finish;
 111
 112        if (likely(err >= 0)) {
 113                n -= err;
 114                nbytes = walk->total - n;
 115        }
 116
 117        if (likely(!(walk->flags & (SKCIPHER_WALK_PHYS |
 118                                    SKCIPHER_WALK_SLOW |
 119                                    SKCIPHER_WALK_COPY |
 120                                    SKCIPHER_WALK_DIFF)))) {
 121unmap_src:
 122                skcipher_unmap_src(walk);
 123        } else if (walk->flags & SKCIPHER_WALK_DIFF) {
 124                skcipher_unmap_dst(walk);
 125                goto unmap_src;
 126        } else if (walk->flags & SKCIPHER_WALK_COPY) {
 127                skcipher_map_dst(walk);
 128                memcpy(walk->dst.virt.addr, walk->page, n);
 129                skcipher_unmap_dst(walk);
 130        } else if (unlikely(walk->flags & SKCIPHER_WALK_SLOW)) {
 131                if (err > 0) {
 132                        /*
 133                         * Didn't process all bytes.  Either the algorithm is
 134                         * broken, or this was the last step and it turned out
 135                         * the message wasn't evenly divisible into blocks but
 136                         * the algorithm requires it.
 137                         */
 138                        err = -EINVAL;
 139                        nbytes = 0;
 140                } else
 141                        n = skcipher_done_slow(walk, n);
 142        }
 143
 144        if (err > 0)
 145                err = 0;
 146
 147        walk->total = nbytes;
 148        walk->nbytes = 0;
 149
 150        scatterwalk_advance(&walk->in, n);
 151        scatterwalk_advance(&walk->out, n);
 152        scatterwalk_done(&walk->in, 0, nbytes);
 153        scatterwalk_done(&walk->out, 1, nbytes);
 154
 155        if (nbytes) {
 156                crypto_yield(walk->flags & SKCIPHER_WALK_SLEEP ?
 157                             CRYPTO_TFM_REQ_MAY_SLEEP : 0);
 158                return skcipher_walk_next(walk);
 159        }
 160
 161finish:
 162        /* Short-circuit for the common/fast path. */
 163        if (!((unsigned long)walk->buffer | (unsigned long)walk->page))
 164                goto out;
 165
 166        if (walk->flags & SKCIPHER_WALK_PHYS)
 167                goto out;
 168
 169        if (walk->iv != walk->oiv)
 170                memcpy(walk->oiv, walk->iv, walk->ivsize);
 171        if (walk->buffer != walk->page)
 172                kfree(walk->buffer);
 173        if (walk->page)
 174                free_page((unsigned long)walk->page);
 175
 176out:
 177        return err;
 178}
 179EXPORT_SYMBOL_GPL(skcipher_walk_done);
 180
 181void skcipher_walk_complete(struct skcipher_walk *walk, int err)
 182{
 183        struct skcipher_walk_buffer *p, *tmp;
 184
 185        list_for_each_entry_safe(p, tmp, &walk->buffers, entry) {
 186                u8 *data;
 187
 188                if (err)
 189                        goto done;
 190
 191                data = p->data;
 192                if (!data) {
 193                        data = PTR_ALIGN(&p->buffer[0], walk->alignmask + 1);
 194                        data = skcipher_get_spot(data, walk->stride);
 195                }
 196
 197                scatterwalk_copychunks(data, &p->dst, p->len, 1);
 198
 199                if (offset_in_page(p->data) + p->len + walk->stride >
 200                    PAGE_SIZE)
 201                        free_page((unsigned long)p->data);
 202
 203done:
 204                list_del(&p->entry);
 205                kfree(p);
 206        }
 207
 208        if (!err && walk->iv != walk->oiv)
 209                memcpy(walk->oiv, walk->iv, walk->ivsize);
 210        if (walk->buffer != walk->page)
 211                kfree(walk->buffer);
 212        if (walk->page)
 213                free_page((unsigned long)walk->page);
 214}
 215EXPORT_SYMBOL_GPL(skcipher_walk_complete);
 216
 217static void skcipher_queue_write(struct skcipher_walk *walk,
 218                                 struct skcipher_walk_buffer *p)
 219{
 220        p->dst = walk->out;
 221        list_add_tail(&p->entry, &walk->buffers);
 222}
 223
 224static int skcipher_next_slow(struct skcipher_walk *walk, unsigned int bsize)
 225{
 226        bool phys = walk->flags & SKCIPHER_WALK_PHYS;
 227        unsigned alignmask = walk->alignmask;
 228        struct skcipher_walk_buffer *p;
 229        unsigned a;
 230        unsigned n;
 231        u8 *buffer;
 232        void *v;
 233
 234        if (!phys) {
 235                if (!walk->buffer)
 236                        walk->buffer = walk->page;
 237                buffer = walk->buffer;
 238                if (buffer)
 239                        goto ok;
 240        }
 241
 242        /* Start with the minimum alignment of kmalloc. */
 243        a = crypto_tfm_ctx_alignment() - 1;
 244        n = bsize;
 245
 246        if (phys) {
 247                /* Calculate the minimum alignment of p->buffer. */
 248                a &= (sizeof(*p) ^ (sizeof(*p) - 1)) >> 1;
 249                n += sizeof(*p);
 250        }
 251
 252        /* Minimum size to align p->buffer by alignmask. */
 253        n += alignmask & ~a;
 254
 255        /* Minimum size to ensure p->buffer does not straddle a page. */
 256        n += (bsize - 1) & ~(alignmask | a);
 257
 258        v = kzalloc(n, skcipher_walk_gfp(walk));
 259        if (!v)
 260                return skcipher_walk_done(walk, -ENOMEM);
 261
 262        if (phys) {
 263                p = v;
 264                p->len = bsize;
 265                skcipher_queue_write(walk, p);
 266                buffer = p->buffer;
 267        } else {
 268                walk->buffer = v;
 269                buffer = v;
 270        }
 271
 272ok:
 273        walk->dst.virt.addr = PTR_ALIGN(buffer, alignmask + 1);
 274        walk->dst.virt.addr = skcipher_get_spot(walk->dst.virt.addr, bsize);
 275        walk->src.virt.addr = walk->dst.virt.addr;
 276
 277        scatterwalk_copychunks(walk->src.virt.addr, &walk->in, bsize, 0);
 278
 279        walk->nbytes = bsize;
 280        walk->flags |= SKCIPHER_WALK_SLOW;
 281
 282        return 0;
 283}
 284
 285static int skcipher_next_copy(struct skcipher_walk *walk)
 286{
 287        struct skcipher_walk_buffer *p;
 288        u8 *tmp = walk->page;
 289
 290        skcipher_map_src(walk);
 291        memcpy(tmp, walk->src.virt.addr, walk->nbytes);
 292        skcipher_unmap_src(walk);
 293
 294        walk->src.virt.addr = tmp;
 295        walk->dst.virt.addr = tmp;
 296
 297        if (!(walk->flags & SKCIPHER_WALK_PHYS))
 298                return 0;
 299
 300        p = kmalloc(sizeof(*p), skcipher_walk_gfp(walk));
 301        if (!p)
 302                return -ENOMEM;
 303
 304        p->data = walk->page;
 305        p->len = walk->nbytes;
 306        skcipher_queue_write(walk, p);
 307
 308        if (offset_in_page(walk->page) + walk->nbytes + walk->stride >
 309            PAGE_SIZE)
 310                walk->page = NULL;
 311        else
 312                walk->page += walk->nbytes;
 313
 314        return 0;
 315}
 316
 317static int skcipher_next_fast(struct skcipher_walk *walk)
 318{
 319        unsigned long diff;
 320
 321        walk->src.phys.page = scatterwalk_page(&walk->in);
 322        walk->src.phys.offset = offset_in_page(walk->in.offset);
 323        walk->dst.phys.page = scatterwalk_page(&walk->out);
 324        walk->dst.phys.offset = offset_in_page(walk->out.offset);
 325
 326        if (walk->flags & SKCIPHER_WALK_PHYS)
 327                return 0;
 328
 329        diff = walk->src.phys.offset - walk->dst.phys.offset;
 330        diff |= walk->src.virt.page - walk->dst.virt.page;
 331
 332        skcipher_map_src(walk);
 333        walk->dst.virt.addr = walk->src.virt.addr;
 334
 335        if (diff) {
 336                walk->flags |= SKCIPHER_WALK_DIFF;
 337                skcipher_map_dst(walk);
 338        }
 339
 340        return 0;
 341}
 342
 343static int skcipher_walk_next(struct skcipher_walk *walk)
 344{
 345        unsigned int bsize;
 346        unsigned int n;
 347        int err;
 348
 349        walk->flags &= ~(SKCIPHER_WALK_SLOW | SKCIPHER_WALK_COPY |
 350                         SKCIPHER_WALK_DIFF);
 351
 352        n = walk->total;
 353        bsize = min(walk->stride, max(n, walk->blocksize));
 354        n = scatterwalk_clamp(&walk->in, n);
 355        n = scatterwalk_clamp(&walk->out, n);
 356
 357        if (unlikely(n < bsize)) {
 358                if (unlikely(walk->total < walk->blocksize))
 359                        return skcipher_walk_done(walk, -EINVAL);
 360
 361slow_path:
 362                err = skcipher_next_slow(walk, bsize);
 363                goto set_phys_lowmem;
 364        }
 365
 366        if (unlikely((walk->in.offset | walk->out.offset) & walk->alignmask)) {
 367                if (!walk->page) {
 368                        gfp_t gfp = skcipher_walk_gfp(walk);
 369
 370                        walk->page = (void *)__get_free_page(gfp);
 371                        if (!walk->page)
 372                                goto slow_path;
 373                }
 374
 375                walk->nbytes = min_t(unsigned, n,
 376                                     PAGE_SIZE - offset_in_page(walk->page));
 377                walk->flags |= SKCIPHER_WALK_COPY;
 378                err = skcipher_next_copy(walk);
 379                goto set_phys_lowmem;
 380        }
 381
 382        walk->nbytes = n;
 383
 384        return skcipher_next_fast(walk);
 385
 386set_phys_lowmem:
 387        if (!err && (walk->flags & SKCIPHER_WALK_PHYS)) {
 388                walk->src.phys.page = virt_to_page(walk->src.virt.addr);
 389                walk->dst.phys.page = virt_to_page(walk->dst.virt.addr);
 390                walk->src.phys.offset &= PAGE_SIZE - 1;
 391                walk->dst.phys.offset &= PAGE_SIZE - 1;
 392        }
 393        return err;
 394}
 395
 396static int skcipher_copy_iv(struct skcipher_walk *walk)
 397{
 398        unsigned a = crypto_tfm_ctx_alignment() - 1;
 399        unsigned alignmask = walk->alignmask;
 400        unsigned ivsize = walk->ivsize;
 401        unsigned bs = walk->stride;
 402        unsigned aligned_bs;
 403        unsigned size;
 404        u8 *iv;
 405
 406        aligned_bs = ALIGN(bs, alignmask + 1);
 407
 408        /* Minimum size to align buffer by alignmask. */
 409        size = alignmask & ~a;
 410
 411        if (walk->flags & SKCIPHER_WALK_PHYS)
 412                size += ivsize;
 413        else {
 414                size += aligned_bs + ivsize;
 415
 416                /* Minimum size to ensure buffer does not straddle a page. */
 417                size += (bs - 1) & ~(alignmask | a);
 418        }
 419
 420        walk->buffer = kmalloc(size, skcipher_walk_gfp(walk));
 421        if (!walk->buffer)
 422                return -ENOMEM;
 423
 424        iv = PTR_ALIGN(walk->buffer, alignmask + 1);
 425        iv = skcipher_get_spot(iv, bs) + aligned_bs;
 426
 427        walk->iv = memcpy(iv, walk->iv, walk->ivsize);
 428        return 0;
 429}
 430
 431static int skcipher_walk_first(struct skcipher_walk *walk)
 432{
 433        if (WARN_ON_ONCE(in_irq()))
 434                return -EDEADLK;
 435
 436        walk->buffer = NULL;
 437        if (unlikely(((unsigned long)walk->iv & walk->alignmask))) {
 438                int err = skcipher_copy_iv(walk);
 439                if (err)
 440                        return err;
 441        }
 442
 443        walk->page = NULL;
 444
 445        return skcipher_walk_next(walk);
 446}
 447
 448static int skcipher_walk_skcipher(struct skcipher_walk *walk,
 449                                  struct skcipher_request *req)
 450{
 451        struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
 452
 453        walk->total = req->cryptlen;
 454        walk->nbytes = 0;
 455        walk->iv = req->iv;
 456        walk->oiv = req->iv;
 457
 458        if (unlikely(!walk->total))
 459                return 0;
 460
 461        scatterwalk_start(&walk->in, req->src);
 462        scatterwalk_start(&walk->out, req->dst);
 463
 464        walk->flags &= ~SKCIPHER_WALK_SLEEP;
 465        walk->flags |= req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
 466                       SKCIPHER_WALK_SLEEP : 0;
 467
 468        walk->blocksize = crypto_skcipher_blocksize(tfm);
 469        walk->stride = crypto_skcipher_walksize(tfm);
 470        walk->ivsize = crypto_skcipher_ivsize(tfm);
 471        walk->alignmask = crypto_skcipher_alignmask(tfm);
 472
 473        return skcipher_walk_first(walk);
 474}
 475
 476int skcipher_walk_virt(struct skcipher_walk *walk,
 477                       struct skcipher_request *req, bool atomic)
 478{
 479        int err;
 480
 481        might_sleep_if(req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
 482
 483        walk->flags &= ~SKCIPHER_WALK_PHYS;
 484
 485        err = skcipher_walk_skcipher(walk, req);
 486
 487        walk->flags &= atomic ? ~SKCIPHER_WALK_SLEEP : ~0;
 488
 489        return err;
 490}
 491EXPORT_SYMBOL_GPL(skcipher_walk_virt);
 492
 493void skcipher_walk_atomise(struct skcipher_walk *walk)
 494{
 495        walk->flags &= ~SKCIPHER_WALK_SLEEP;
 496}
 497EXPORT_SYMBOL_GPL(skcipher_walk_atomise);
 498
 499int skcipher_walk_async(struct skcipher_walk *walk,
 500                        struct skcipher_request *req)
 501{
 502        walk->flags |= SKCIPHER_WALK_PHYS;
 503
 504        INIT_LIST_HEAD(&walk->buffers);
 505
 506        return skcipher_walk_skcipher(walk, req);
 507}
 508EXPORT_SYMBOL_GPL(skcipher_walk_async);
 509
 510static int skcipher_walk_aead_common(struct skcipher_walk *walk,
 511                                     struct aead_request *req, bool atomic)
 512{
 513        struct crypto_aead *tfm = crypto_aead_reqtfm(req);
 514        int err;
 515
 516        walk->nbytes = 0;
 517        walk->iv = req->iv;
 518        walk->oiv = req->iv;
 519
 520        if (unlikely(!walk->total))
 521                return 0;
 522
 523        walk->flags &= ~SKCIPHER_WALK_PHYS;
 524
 525        scatterwalk_start(&walk->in, req->src);
 526        scatterwalk_start(&walk->out, req->dst);
 527
 528        scatterwalk_copychunks(NULL, &walk->in, req->assoclen, 2);
 529        scatterwalk_copychunks(NULL, &walk->out, req->assoclen, 2);
 530
 531        scatterwalk_done(&walk->in, 0, walk->total);
 532        scatterwalk_done(&walk->out, 0, walk->total);
 533
 534        if (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP)
 535                walk->flags |= SKCIPHER_WALK_SLEEP;
 536        else
 537                walk->flags &= ~SKCIPHER_WALK_SLEEP;
 538
 539        walk->blocksize = crypto_aead_blocksize(tfm);
 540        walk->stride = crypto_aead_chunksize(tfm);
 541        walk->ivsize = crypto_aead_ivsize(tfm);
 542        walk->alignmask = crypto_aead_alignmask(tfm);
 543
 544        err = skcipher_walk_first(walk);
 545
 546        if (atomic)
 547                walk->flags &= ~SKCIPHER_WALK_SLEEP;
 548
 549        return err;
 550}
 551
 552int skcipher_walk_aead(struct skcipher_walk *walk, struct aead_request *req,
 553                       bool atomic)
 554{
 555        walk->total = req->cryptlen;
 556
 557        return skcipher_walk_aead_common(walk, req, atomic);
 558}
 559EXPORT_SYMBOL_GPL(skcipher_walk_aead);
 560
 561int skcipher_walk_aead_encrypt(struct skcipher_walk *walk,
 562                               struct aead_request *req, bool atomic)
 563{
 564        walk->total = req->cryptlen;
 565
 566        return skcipher_walk_aead_common(walk, req, atomic);
 567}
 568EXPORT_SYMBOL_GPL(skcipher_walk_aead_encrypt);
 569
 570int skcipher_walk_aead_decrypt(struct skcipher_walk *walk,
 571                               struct aead_request *req, bool atomic)
 572{
 573        struct crypto_aead *tfm = crypto_aead_reqtfm(req);
 574
 575        walk->total = req->cryptlen - crypto_aead_authsize(tfm);
 576
 577        return skcipher_walk_aead_common(walk, req, atomic);
 578}
 579EXPORT_SYMBOL_GPL(skcipher_walk_aead_decrypt);
 580
 581static unsigned int crypto_skcipher_extsize(struct crypto_alg *alg)
 582{
 583        if (alg->cra_type == &crypto_blkcipher_type)
 584                return sizeof(struct crypto_blkcipher *);
 585
 586        if (alg->cra_type == &crypto_ablkcipher_type)
 587                return sizeof(struct crypto_ablkcipher *);
 588
 589        return crypto_alg_extsize(alg);
 590}
 591
 592static void skcipher_set_needkey(struct crypto_skcipher *tfm)
 593{
 594        if (tfm->keysize)
 595                crypto_skcipher_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
 596}
 597
 598static int skcipher_setkey_blkcipher(struct crypto_skcipher *tfm,
 599                                     const u8 *key, unsigned int keylen)
 600{
 601        struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
 602        struct crypto_blkcipher *blkcipher = *ctx;
 603        int err;
 604
 605        crypto_blkcipher_clear_flags(blkcipher, ~0);
 606        crypto_blkcipher_set_flags(blkcipher, crypto_skcipher_get_flags(tfm) &
 607                                              CRYPTO_TFM_REQ_MASK);
 608        err = crypto_blkcipher_setkey(blkcipher, key, keylen);
 609        crypto_skcipher_set_flags(tfm, crypto_blkcipher_get_flags(blkcipher) &
 610                                       CRYPTO_TFM_RES_MASK);
 611        if (unlikely(err)) {
 612                skcipher_set_needkey(tfm);
 613                return err;
 614        }
 615
 616        crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
 617        return 0;
 618}
 619
 620static int skcipher_setkeytype_blkcipher(struct crypto_skcipher *tfm,
 621                                         const u8 *key, unsigned int keylen)
 622{
 623        struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
 624        struct crypto_blkcipher *blkcipher = *ctx;
 625        int err;
 626
 627        crypto_blkcipher_clear_flags(blkcipher, ~0);
 628        crypto_blkcipher_set_flags(blkcipher, crypto_skcipher_get_flags(tfm) &
 629                        CRYPTO_TFM_REQ_MASK);
 630        err = crypto_blkcipher_setkeytype(blkcipher, key, keylen);
 631        crypto_skcipher_set_flags(tfm, crypto_blkcipher_get_flags(blkcipher) &
 632                        CRYPTO_TFM_RES_MASK);
 633
 634        return err;
 635}
 636
 637static int skcipher_crypt_blkcipher(struct skcipher_request *req,
 638                                    int (*crypt)(struct blkcipher_desc *,
 639                                                 struct scatterlist *,
 640                                                 struct scatterlist *,
 641                                                 unsigned int))
 642{
 643        struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
 644        struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
 645        struct blkcipher_desc desc = {
 646                .tfm = *ctx,
 647                .info = req->iv,
 648                .flags = req->base.flags,
 649        };
 650
 651
 652        return crypt(&desc, req->dst, req->src, req->cryptlen);
 653}
 654
 655static int skcipher_encrypt_blkcipher(struct skcipher_request *req)
 656{
 657        struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
 658        struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
 659        struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
 660
 661        return skcipher_crypt_blkcipher(req, alg->encrypt);
 662}
 663
 664static int skcipher_decrypt_blkcipher(struct skcipher_request *req)
 665{
 666        struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
 667        struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
 668        struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
 669
 670        return skcipher_crypt_blkcipher(req, alg->decrypt);
 671}
 672
 673static void crypto_exit_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
 674{
 675        struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
 676
 677        crypto_free_blkcipher(*ctx);
 678}
 679
 680static int crypto_init_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
 681{
 682        struct crypto_alg *calg = tfm->__crt_alg;
 683        struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
 684        struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
 685        struct crypto_blkcipher *blkcipher;
 686        struct crypto_tfm *btfm;
 687
 688        if (!crypto_mod_get(calg))
 689                return -EAGAIN;
 690
 691        btfm = __crypto_alloc_tfm(calg, CRYPTO_ALG_TYPE_BLKCIPHER,
 692                                        CRYPTO_ALG_TYPE_MASK);
 693        if (IS_ERR(btfm)) {
 694                crypto_mod_put(calg);
 695                return PTR_ERR(btfm);
 696        }
 697
 698        blkcipher = __crypto_blkcipher_cast(btfm);
 699        *ctx = blkcipher;
 700        tfm->exit = crypto_exit_skcipher_ops_blkcipher;
 701
 702        skcipher->setkey = skcipher_setkey_blkcipher;
 703        skcipher->setkeytype = skcipher_setkeytype_blkcipher;
 704        skcipher->encrypt = skcipher_encrypt_blkcipher;
 705        skcipher->decrypt = skcipher_decrypt_blkcipher;
 706
 707        skcipher->ivsize = crypto_blkcipher_ivsize(blkcipher);
 708        skcipher->keysize = calg->cra_blkcipher.max_keysize;
 709
 710        skcipher_set_needkey(skcipher);
 711
 712        return 0;
 713}
 714
 715static int skcipher_setkey_ablkcipher(struct crypto_skcipher *tfm,
 716                                      const u8 *key, unsigned int keylen)
 717{
 718        struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
 719        struct crypto_ablkcipher *ablkcipher = *ctx;
 720        int err;
 721
 722        crypto_ablkcipher_clear_flags(ablkcipher, ~0);
 723        crypto_ablkcipher_set_flags(ablkcipher,
 724                                    crypto_skcipher_get_flags(tfm) &
 725                                    CRYPTO_TFM_REQ_MASK);
 726        err = crypto_ablkcipher_setkey(ablkcipher, key, keylen);
 727        crypto_skcipher_set_flags(tfm,
 728                                  crypto_ablkcipher_get_flags(ablkcipher) &
 729                                  CRYPTO_TFM_RES_MASK);
 730        if (unlikely(err)) {
 731                skcipher_set_needkey(tfm);
 732                return err;
 733        }
 734
 735        crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
 736        return 0;
 737}
 738
 739static int skcipher_crypt_ablkcipher(struct skcipher_request *req,
 740                                     int (*crypt)(struct ablkcipher_request *))
 741{
 742        struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
 743        struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
 744        struct ablkcipher_request *subreq = skcipher_request_ctx(req);
 745
 746        ablkcipher_request_set_tfm(subreq, *ctx);
 747        ablkcipher_request_set_callback(subreq, skcipher_request_flags(req),
 748                                        req->base.complete, req->base.data);
 749        ablkcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
 750                                     req->iv);
 751
 752        return crypt(subreq);
 753}
 754
 755static int skcipher_encrypt_ablkcipher(struct skcipher_request *req)
 756{
 757        struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
 758        struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
 759        struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
 760
 761        return skcipher_crypt_ablkcipher(req, alg->encrypt);
 762}
 763
 764static int skcipher_decrypt_ablkcipher(struct skcipher_request *req)
 765{
 766        struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
 767        struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
 768        struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
 769
 770        return skcipher_crypt_ablkcipher(req, alg->decrypt);
 771}
 772
 773static void crypto_exit_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
 774{
 775        struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
 776
 777        crypto_free_ablkcipher(*ctx);
 778}
 779
 780static int crypto_init_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
 781{
 782        struct crypto_alg *calg = tfm->__crt_alg;
 783        struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
 784        struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
 785        struct crypto_ablkcipher *ablkcipher;
 786        struct crypto_tfm *abtfm;
 787
 788        if (!crypto_mod_get(calg))
 789                return -EAGAIN;
 790
 791        abtfm = __crypto_alloc_tfm(calg, 0, 0);
 792        if (IS_ERR(abtfm)) {
 793                crypto_mod_put(calg);
 794                return PTR_ERR(abtfm);
 795        }
 796
 797        ablkcipher = __crypto_ablkcipher_cast(abtfm);
 798        *ctx = ablkcipher;
 799        tfm->exit = crypto_exit_skcipher_ops_ablkcipher;
 800
 801        skcipher->setkey = skcipher_setkey_ablkcipher;
 802        skcipher->encrypt = skcipher_encrypt_ablkcipher;
 803        skcipher->decrypt = skcipher_decrypt_ablkcipher;
 804
 805        skcipher->ivsize = crypto_ablkcipher_ivsize(ablkcipher);
 806        skcipher->reqsize = crypto_ablkcipher_reqsize(ablkcipher) +
 807                            sizeof(struct ablkcipher_request);
 808        skcipher->keysize = calg->cra_ablkcipher.max_keysize;
 809
 810        skcipher_set_needkey(skcipher);
 811
 812        return 0;
 813}
 814
 815static int skcipher_setkey_unaligned(struct crypto_skcipher *tfm,
 816                                     const u8 *key, unsigned int keylen)
 817{
 818        unsigned long alignmask = crypto_skcipher_alignmask(tfm);
 819        struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
 820        u8 *buffer, *alignbuffer;
 821        unsigned long absize;
 822        int ret;
 823
 824        absize = keylen + alignmask;
 825        buffer = kmalloc(absize, GFP_ATOMIC);
 826        if (!buffer)
 827                return -ENOMEM;
 828
 829        alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
 830        memcpy(alignbuffer, key, keylen);
 831        ret = cipher->setkey(tfm, alignbuffer, keylen);
 832        kzfree(buffer);
 833        return ret;
 834}
 835
 836static int skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
 837                           unsigned int keylen)
 838{
 839        struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
 840        unsigned long alignmask = crypto_skcipher_alignmask(tfm);
 841        int err;
 842
 843        if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
 844                crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
 845                return -EINVAL;
 846        }
 847
 848        if ((unsigned long)key & alignmask)
 849                err = skcipher_setkey_unaligned(tfm, key, keylen);
 850        else
 851                err = cipher->setkey(tfm, key, keylen);
 852
 853        if (unlikely(err)) {
 854                skcipher_set_needkey(tfm);
 855                return err;
 856        }
 857
 858        crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
 859        return 0;
 860}
 861
 862int crypto_skcipher_encrypt(struct skcipher_request *req)
 863{
 864        struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
 865        struct crypto_alg *alg = tfm->base.__crt_alg;
 866        unsigned int cryptlen = req->cryptlen;
 867        int ret;
 868
 869        crypto_stats_get(alg);
 870        if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
 871                ret = -ENOKEY;
 872        else
 873                ret = tfm->encrypt(req);
 874        crypto_stats_skcipher_encrypt(cryptlen, ret, alg);
 875        return ret;
 876}
 877EXPORT_SYMBOL_GPL(crypto_skcipher_encrypt);
 878
 879int crypto_skcipher_decrypt(struct skcipher_request *req)
 880{
 881        struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
 882        struct crypto_alg *alg = tfm->base.__crt_alg;
 883        unsigned int cryptlen = req->cryptlen;
 884        int ret;
 885
 886        crypto_stats_get(alg);
 887        if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
 888                ret = -ENOKEY;
 889        else
 890                ret = tfm->decrypt(req);
 891        crypto_stats_skcipher_decrypt(cryptlen, ret, alg);
 892        return ret;
 893}
 894EXPORT_SYMBOL_GPL(crypto_skcipher_decrypt);
 895
 896static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
 897{
 898        struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
 899        struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
 900
 901        alg->exit(skcipher);
 902}
 903
 904static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
 905{
 906        struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
 907        struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
 908
 909        if (tfm->__crt_alg->cra_type == &crypto_blkcipher_type)
 910                return crypto_init_skcipher_ops_blkcipher(tfm);
 911
 912        if (tfm->__crt_alg->cra_type == &crypto_ablkcipher_type)
 913                return crypto_init_skcipher_ops_ablkcipher(tfm);
 914
 915        skcipher->setkey = skcipher_setkey;
 916        skcipher->encrypt = alg->encrypt;
 917        skcipher->decrypt = alg->decrypt;
 918        skcipher->ivsize = alg->ivsize;
 919        skcipher->keysize = alg->max_keysize;
 920
 921        skcipher_set_needkey(skcipher);
 922
 923        if (alg->exit)
 924                skcipher->base.exit = crypto_skcipher_exit_tfm;
 925
 926        if (alg->init)
 927                return alg->init(skcipher);
 928
 929        return 0;
 930}
 931
 932static void crypto_skcipher_free_instance(struct crypto_instance *inst)
 933{
 934        struct skcipher_instance *skcipher =
 935                container_of(inst, struct skcipher_instance, s.base);
 936
 937        skcipher->free(skcipher);
 938}
 939
 940static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
 941        __maybe_unused;
 942static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
 943{
 944        struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
 945                                                     base);
 946
 947        seq_printf(m, "type         : skcipher\n");
 948        seq_printf(m, "async        : %s\n",
 949                   alg->cra_flags & CRYPTO_ALG_ASYNC ?  "yes" : "no");
 950        seq_printf(m, "blocksize    : %u\n", alg->cra_blocksize);
 951        seq_printf(m, "min keysize  : %u\n", skcipher->min_keysize);
 952        seq_printf(m, "max keysize  : %u\n", skcipher->max_keysize);
 953        seq_printf(m, "ivsize       : %u\n", skcipher->ivsize);
 954        seq_printf(m, "chunksize    : %u\n", skcipher->chunksize);
 955        seq_printf(m, "walksize     : %u\n", skcipher->walksize);
 956}
 957
 958#ifdef CONFIG_NET
 959static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
 960{
 961        struct crypto_report_blkcipher rblkcipher;
 962        struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
 963                                                     base);
 964
 965        memset(&rblkcipher, 0, sizeof(rblkcipher));
 966
 967        strscpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type));
 968        strscpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
 969
 970        rblkcipher.blocksize = alg->cra_blocksize;
 971        rblkcipher.min_keysize = skcipher->min_keysize;
 972        rblkcipher.max_keysize = skcipher->max_keysize;
 973        rblkcipher.ivsize = skcipher->ivsize;
 974
 975        return nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
 976                       sizeof(rblkcipher), &rblkcipher);
 977}
 978#else
 979static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
 980{
 981        return -ENOSYS;
 982}
 983#endif
 984
 985static const struct crypto_type crypto_skcipher_type2 = {
 986        .extsize = crypto_skcipher_extsize,
 987        .init_tfm = crypto_skcipher_init_tfm,
 988        .free = crypto_skcipher_free_instance,
 989#ifdef CONFIG_PROC_FS
 990        .show = crypto_skcipher_show,
 991#endif
 992        .report = crypto_skcipher_report,
 993        .maskclear = ~CRYPTO_ALG_TYPE_MASK,
 994        .maskset = CRYPTO_ALG_TYPE_BLKCIPHER_MASK,
 995        .type = CRYPTO_ALG_TYPE_SKCIPHER,
 996        .tfmsize = offsetof(struct crypto_skcipher, base),
 997};
 998
 999int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
1000                          const char *name, u32 type, u32 mask)
1001{
1002        spawn->base.frontend = &crypto_skcipher_type2;
1003        return crypto_grab_spawn(&spawn->base, name, type, mask);
1004}
1005EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
1006
1007struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
1008                                              u32 type, u32 mask)
1009{
1010        return crypto_alloc_tfm(alg_name, &crypto_skcipher_type2, type, mask);
1011}
1012EXPORT_SYMBOL_GPL(crypto_alloc_skcipher);
1013
1014struct crypto_sync_skcipher *crypto_alloc_sync_skcipher(
1015                                const char *alg_name, u32 type, u32 mask)
1016{
1017        struct crypto_skcipher *tfm;
1018
1019        /* Only sync algorithms allowed. */
1020        mask |= CRYPTO_ALG_ASYNC;
1021
1022        tfm = crypto_alloc_tfm(alg_name, &crypto_skcipher_type2, type, mask);
1023
1024        /*
1025         * Make sure we do not allocate something that might get used with
1026         * an on-stack request: check the request size.
1027         */
1028        if (!IS_ERR(tfm) && WARN_ON(crypto_skcipher_reqsize(tfm) >
1029                                    MAX_SYNC_SKCIPHER_REQSIZE)) {
1030                crypto_free_skcipher(tfm);
1031                return ERR_PTR(-EINVAL);
1032        }
1033
1034        return (struct crypto_sync_skcipher *)tfm;
1035}
1036EXPORT_SYMBOL_GPL(crypto_alloc_sync_skcipher);
1037
1038int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask)
1039{
1040        return crypto_type_has_alg(alg_name, &crypto_skcipher_type2,
1041                                   type, mask);
1042}
1043EXPORT_SYMBOL_GPL(crypto_has_skcipher2);
1044
1045static int skcipher_prepare_alg(struct skcipher_alg *alg)
1046{
1047        struct crypto_alg *base = &alg->base;
1048
1049        if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8 ||
1050            alg->walksize > PAGE_SIZE / 8)
1051                return -EINVAL;
1052
1053        if (!alg->chunksize)
1054                alg->chunksize = base->cra_blocksize;
1055        if (!alg->walksize)
1056                alg->walksize = alg->chunksize;
1057
1058        base->cra_type = &crypto_skcipher_type2;
1059        base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
1060        base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER;
1061
1062        return 0;
1063}
1064
1065int crypto_register_skcipher(struct skcipher_alg *alg)
1066{
1067        struct crypto_alg *base = &alg->base;
1068        int err;
1069
1070        err = skcipher_prepare_alg(alg);
1071        if (err)
1072                return err;
1073
1074        return crypto_register_alg(base);
1075}
1076EXPORT_SYMBOL_GPL(crypto_register_skcipher);
1077
1078void crypto_unregister_skcipher(struct skcipher_alg *alg)
1079{
1080        crypto_unregister_alg(&alg->base);
1081}
1082EXPORT_SYMBOL_GPL(crypto_unregister_skcipher);
1083
1084int crypto_register_skciphers(struct skcipher_alg *algs, int count)
1085{
1086        int i, ret;
1087
1088        for (i = 0; i < count; i++) {
1089                ret = crypto_register_skcipher(&algs[i]);
1090                if (ret)
1091                        goto err;
1092        }
1093
1094        return 0;
1095
1096err:
1097        for (--i; i >= 0; --i)
1098                crypto_unregister_skcipher(&algs[i]);
1099
1100        return ret;
1101}
1102EXPORT_SYMBOL_GPL(crypto_register_skciphers);
1103
1104void crypto_unregister_skciphers(struct skcipher_alg *algs, int count)
1105{
1106        int i;
1107
1108        for (i = count - 1; i >= 0; --i)
1109                crypto_unregister_skcipher(&algs[i]);
1110}
1111EXPORT_SYMBOL_GPL(crypto_unregister_skciphers);
1112
1113int skcipher_register_instance(struct crypto_template *tmpl,
1114                           struct skcipher_instance *inst)
1115{
1116        int err;
1117
1118        err = skcipher_prepare_alg(&inst->alg);
1119        if (err)
1120                return err;
1121
1122        return crypto_register_instance(tmpl, skcipher_crypto_instance(inst));
1123}
1124EXPORT_SYMBOL_GPL(skcipher_register_instance);
1125
1126static int skcipher_setkey_simple(struct crypto_skcipher *tfm, const u8 *key,
1127                                  unsigned int keylen)
1128{
1129        struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
1130        int err;
1131
1132        crypto_cipher_clear_flags(cipher, CRYPTO_TFM_REQ_MASK);
1133        crypto_cipher_set_flags(cipher, crypto_skcipher_get_flags(tfm) &
1134                                CRYPTO_TFM_REQ_MASK);
1135        err = crypto_cipher_setkey(cipher, key, keylen);
1136        crypto_skcipher_set_flags(tfm, crypto_cipher_get_flags(cipher) &
1137                                  CRYPTO_TFM_RES_MASK);
1138        return err;
1139}
1140
1141static int skcipher_init_tfm_simple(struct crypto_skcipher *tfm)
1142{
1143        struct skcipher_instance *inst = skcipher_alg_instance(tfm);
1144        struct crypto_spawn *spawn = skcipher_instance_ctx(inst);
1145        struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
1146        struct crypto_cipher *cipher;
1147
1148        cipher = crypto_spawn_cipher(spawn);
1149        if (IS_ERR(cipher))
1150                return PTR_ERR(cipher);
1151
1152        ctx->cipher = cipher;
1153        return 0;
1154}
1155
1156static void skcipher_exit_tfm_simple(struct crypto_skcipher *tfm)
1157{
1158        struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
1159
1160        crypto_free_cipher(ctx->cipher);
1161}
1162
1163static void skcipher_free_instance_simple(struct skcipher_instance *inst)
1164{
1165        crypto_drop_spawn(skcipher_instance_ctx(inst));
1166        kfree(inst);
1167}
1168
1169/**
1170 * skcipher_alloc_instance_simple - allocate instance of simple block cipher mode
1171 *
1172 * Allocate an skcipher_instance for a simple block cipher mode of operation,
1173 * e.g. cbc or ecb.  The instance context will have just a single crypto_spawn,
1174 * that for the underlying cipher.  The {min,max}_keysize, ivsize, blocksize,
1175 * alignmask, and priority are set from the underlying cipher but can be
1176 * overridden if needed.  The tfm context defaults to skcipher_ctx_simple, and
1177 * default ->setkey(), ->init(), and ->exit() methods are installed.
1178 *
1179 * @tmpl: the template being instantiated
1180 * @tb: the template parameters
1181 * @cipher_alg_ret: on success, a pointer to the underlying cipher algorithm is
1182 *                  returned here.  It must be dropped with crypto_mod_put().
1183 *
1184 * Return: a pointer to the new instance, or an ERR_PTR().  The caller still
1185 *         needs to register the instance.
1186 */
1187struct skcipher_instance *
1188skcipher_alloc_instance_simple(struct crypto_template *tmpl, struct rtattr **tb,
1189                               struct crypto_alg **cipher_alg_ret)
1190{
1191        struct crypto_attr_type *algt;
1192        struct crypto_alg *cipher_alg;
1193        struct skcipher_instance *inst;
1194        struct crypto_spawn *spawn;
1195        u32 mask;
1196        int err;
1197
1198        algt = crypto_get_attr_type(tb);
1199        if (IS_ERR(algt))
1200                return ERR_CAST(algt);
1201
1202        if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask)
1203                return ERR_PTR(-EINVAL);
1204
1205        mask = CRYPTO_ALG_TYPE_MASK |
1206                crypto_requires_off(algt->type, algt->mask,
1207                                    CRYPTO_ALG_NEED_FALLBACK);
1208
1209        cipher_alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, mask);
1210        if (IS_ERR(cipher_alg))
1211                return ERR_CAST(cipher_alg);
1212
1213        inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
1214        if (!inst) {
1215                err = -ENOMEM;
1216                goto err_put_cipher_alg;
1217        }
1218        spawn = skcipher_instance_ctx(inst);
1219
1220        err = crypto_inst_setname(skcipher_crypto_instance(inst), tmpl->name,
1221                                  cipher_alg);
1222        if (err)
1223                goto err_free_inst;
1224
1225        err = crypto_init_spawn(spawn, cipher_alg,
1226                                skcipher_crypto_instance(inst),
1227                                CRYPTO_ALG_TYPE_MASK);
1228        if (err)
1229                goto err_free_inst;
1230        inst->free = skcipher_free_instance_simple;
1231
1232        /* Default algorithm properties, can be overridden */
1233        inst->alg.base.cra_blocksize = cipher_alg->cra_blocksize;
1234        inst->alg.base.cra_alignmask = cipher_alg->cra_alignmask;
1235        inst->alg.base.cra_priority = cipher_alg->cra_priority;
1236        inst->alg.min_keysize = cipher_alg->cra_cipher.cia_min_keysize;
1237        inst->alg.max_keysize = cipher_alg->cra_cipher.cia_max_keysize;
1238        inst->alg.ivsize = cipher_alg->cra_blocksize;
1239
1240        /* Use skcipher_ctx_simple by default, can be overridden */
1241        inst->alg.base.cra_ctxsize = sizeof(struct skcipher_ctx_simple);
1242        inst->alg.setkey = skcipher_setkey_simple;
1243        inst->alg.init = skcipher_init_tfm_simple;
1244        inst->alg.exit = skcipher_exit_tfm_simple;
1245
1246        *cipher_alg_ret = cipher_alg;
1247        return inst;
1248
1249err_free_inst:
1250        kfree(inst);
1251err_put_cipher_alg:
1252        crypto_mod_put(cipher_alg);
1253        return ERR_PTR(err);
1254}
1255EXPORT_SYMBOL_GPL(skcipher_alloc_instance_simple);
1256
1257MODULE_LICENSE("GPL");
1258MODULE_DESCRIPTION("Symmetric key cipher type");
1259