0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <crypto/internal/aead.h>
0013 #include <crypto/internal/cipher.h>
0014 #include <crypto/internal/skcipher.h>
0015 #include <crypto/scatterwalk.h>
0016 #include <linux/bug.h>
0017 #include <linux/cryptouser.h>
0018 #include <linux/compiler.h>
0019 #include <linux/list.h>
0020 #include <linux/module.h>
0021 #include <linux/rtnetlink.h>
0022 #include <linux/seq_file.h>
0023 #include <net/netlink.h>
0024
0025 #include "internal.h"
0026
0027 enum {
0028 SKCIPHER_WALK_PHYS = 1 << 0,
0029 SKCIPHER_WALK_SLOW = 1 << 1,
0030 SKCIPHER_WALK_COPY = 1 << 2,
0031 SKCIPHER_WALK_DIFF = 1 << 3,
0032 SKCIPHER_WALK_SLEEP = 1 << 4,
0033 };
0034
0035 struct skcipher_walk_buffer {
0036 struct list_head entry;
0037 struct scatter_walk dst;
0038 unsigned int len;
0039 u8 *data;
0040 u8 buffer[];
0041 };
0042
0043 static int skcipher_walk_next(struct skcipher_walk *walk);
0044
0045 static inline void skcipher_unmap(struct scatter_walk *walk, void *vaddr)
0046 {
0047 if (PageHighMem(scatterwalk_page(walk)))
0048 kunmap_atomic(vaddr);
0049 }
0050
0051 static inline void *skcipher_map(struct scatter_walk *walk)
0052 {
0053 struct page *page = scatterwalk_page(walk);
0054
0055 return (PageHighMem(page) ? kmap_atomic(page) : page_address(page)) +
0056 offset_in_page(walk->offset);
0057 }
0058
0059 static inline void skcipher_map_src(struct skcipher_walk *walk)
0060 {
0061 walk->src.virt.addr = skcipher_map(&walk->in);
0062 }
0063
0064 static inline void skcipher_map_dst(struct skcipher_walk *walk)
0065 {
0066 walk->dst.virt.addr = skcipher_map(&walk->out);
0067 }
0068
0069 static inline void skcipher_unmap_src(struct skcipher_walk *walk)
0070 {
0071 skcipher_unmap(&walk->in, walk->src.virt.addr);
0072 }
0073
0074 static inline void skcipher_unmap_dst(struct skcipher_walk *walk)
0075 {
0076 skcipher_unmap(&walk->out, walk->dst.virt.addr);
0077 }
0078
0079 static inline gfp_t skcipher_walk_gfp(struct skcipher_walk *walk)
0080 {
0081 return walk->flags & SKCIPHER_WALK_SLEEP ? GFP_KERNEL : GFP_ATOMIC;
0082 }
0083
0084
0085
0086
0087 static inline u8 *skcipher_get_spot(u8 *start, unsigned int len)
0088 {
0089 u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
0090
0091 return max(start, end_page);
0092 }
0093
0094 static int skcipher_done_slow(struct skcipher_walk *walk, unsigned int bsize)
0095 {
0096 u8 *addr;
0097
0098 addr = (u8 *)ALIGN((unsigned long)walk->buffer, walk->alignmask + 1);
0099 addr = skcipher_get_spot(addr, bsize);
0100 scatterwalk_copychunks(addr, &walk->out, bsize,
0101 (walk->flags & SKCIPHER_WALK_PHYS) ? 2 : 1);
0102 return 0;
0103 }
0104
0105 int skcipher_walk_done(struct skcipher_walk *walk, int err)
0106 {
0107 unsigned int n = walk->nbytes;
0108 unsigned int nbytes = 0;
0109
0110 if (!n)
0111 goto finish;
0112
0113 if (likely(err >= 0)) {
0114 n -= err;
0115 nbytes = walk->total - n;
0116 }
0117
0118 if (likely(!(walk->flags & (SKCIPHER_WALK_PHYS |
0119 SKCIPHER_WALK_SLOW |
0120 SKCIPHER_WALK_COPY |
0121 SKCIPHER_WALK_DIFF)))) {
0122 unmap_src:
0123 skcipher_unmap_src(walk);
0124 } else if (walk->flags & SKCIPHER_WALK_DIFF) {
0125 skcipher_unmap_dst(walk);
0126 goto unmap_src;
0127 } else if (walk->flags & SKCIPHER_WALK_COPY) {
0128 skcipher_map_dst(walk);
0129 memcpy(walk->dst.virt.addr, walk->page, n);
0130 skcipher_unmap_dst(walk);
0131 } else if (unlikely(walk->flags & SKCIPHER_WALK_SLOW)) {
0132 if (err > 0) {
0133
0134
0135
0136
0137
0138
0139 err = -EINVAL;
0140 nbytes = 0;
0141 } else
0142 n = skcipher_done_slow(walk, n);
0143 }
0144
0145 if (err > 0)
0146 err = 0;
0147
0148 walk->total = nbytes;
0149 walk->nbytes = 0;
0150
0151 scatterwalk_advance(&walk->in, n);
0152 scatterwalk_advance(&walk->out, n);
0153 scatterwalk_done(&walk->in, 0, nbytes);
0154 scatterwalk_done(&walk->out, 1, nbytes);
0155
0156 if (nbytes) {
0157 crypto_yield(walk->flags & SKCIPHER_WALK_SLEEP ?
0158 CRYPTO_TFM_REQ_MAY_SLEEP : 0);
0159 return skcipher_walk_next(walk);
0160 }
0161
0162 finish:
0163
0164 if (!((unsigned long)walk->buffer | (unsigned long)walk->page))
0165 goto out;
0166
0167 if (walk->flags & SKCIPHER_WALK_PHYS)
0168 goto out;
0169
0170 if (walk->iv != walk->oiv)
0171 memcpy(walk->oiv, walk->iv, walk->ivsize);
0172 if (walk->buffer != walk->page)
0173 kfree(walk->buffer);
0174 if (walk->page)
0175 free_page((unsigned long)walk->page);
0176
0177 out:
0178 return err;
0179 }
0180 EXPORT_SYMBOL_GPL(skcipher_walk_done);
0181
0182 void skcipher_walk_complete(struct skcipher_walk *walk, int err)
0183 {
0184 struct skcipher_walk_buffer *p, *tmp;
0185
0186 list_for_each_entry_safe(p, tmp, &walk->buffers, entry) {
0187 u8 *data;
0188
0189 if (err)
0190 goto done;
0191
0192 data = p->data;
0193 if (!data) {
0194 data = PTR_ALIGN(&p->buffer[0], walk->alignmask + 1);
0195 data = skcipher_get_spot(data, walk->stride);
0196 }
0197
0198 scatterwalk_copychunks(data, &p->dst, p->len, 1);
0199
0200 if (offset_in_page(p->data) + p->len + walk->stride >
0201 PAGE_SIZE)
0202 free_page((unsigned long)p->data);
0203
0204 done:
0205 list_del(&p->entry);
0206 kfree(p);
0207 }
0208
0209 if (!err && walk->iv != walk->oiv)
0210 memcpy(walk->oiv, walk->iv, walk->ivsize);
0211 if (walk->buffer != walk->page)
0212 kfree(walk->buffer);
0213 if (walk->page)
0214 free_page((unsigned long)walk->page);
0215 }
0216 EXPORT_SYMBOL_GPL(skcipher_walk_complete);
0217
0218 static void skcipher_queue_write(struct skcipher_walk *walk,
0219 struct skcipher_walk_buffer *p)
0220 {
0221 p->dst = walk->out;
0222 list_add_tail(&p->entry, &walk->buffers);
0223 }
0224
0225 static int skcipher_next_slow(struct skcipher_walk *walk, unsigned int bsize)
0226 {
0227 bool phys = walk->flags & SKCIPHER_WALK_PHYS;
0228 unsigned alignmask = walk->alignmask;
0229 struct skcipher_walk_buffer *p;
0230 unsigned a;
0231 unsigned n;
0232 u8 *buffer;
0233 void *v;
0234
0235 if (!phys) {
0236 if (!walk->buffer)
0237 walk->buffer = walk->page;
0238 buffer = walk->buffer;
0239 if (buffer)
0240 goto ok;
0241 }
0242
0243
0244 a = crypto_tfm_ctx_alignment() - 1;
0245 n = bsize;
0246
0247 if (phys) {
0248
0249 a &= (sizeof(*p) ^ (sizeof(*p) - 1)) >> 1;
0250 n += sizeof(*p);
0251 }
0252
0253
0254 n += alignmask & ~a;
0255
0256
0257 n += (bsize - 1) & ~(alignmask | a);
0258
0259 v = kzalloc(n, skcipher_walk_gfp(walk));
0260 if (!v)
0261 return skcipher_walk_done(walk, -ENOMEM);
0262
0263 if (phys) {
0264 p = v;
0265 p->len = bsize;
0266 skcipher_queue_write(walk, p);
0267 buffer = p->buffer;
0268 } else {
0269 walk->buffer = v;
0270 buffer = v;
0271 }
0272
0273 ok:
0274 walk->dst.virt.addr = PTR_ALIGN(buffer, alignmask + 1);
0275 walk->dst.virt.addr = skcipher_get_spot(walk->dst.virt.addr, bsize);
0276 walk->src.virt.addr = walk->dst.virt.addr;
0277
0278 scatterwalk_copychunks(walk->src.virt.addr, &walk->in, bsize, 0);
0279
0280 walk->nbytes = bsize;
0281 walk->flags |= SKCIPHER_WALK_SLOW;
0282
0283 return 0;
0284 }
0285
0286 static int skcipher_next_copy(struct skcipher_walk *walk)
0287 {
0288 struct skcipher_walk_buffer *p;
0289 u8 *tmp = walk->page;
0290
0291 skcipher_map_src(walk);
0292 memcpy(tmp, walk->src.virt.addr, walk->nbytes);
0293 skcipher_unmap_src(walk);
0294
0295 walk->src.virt.addr = tmp;
0296 walk->dst.virt.addr = tmp;
0297
0298 if (!(walk->flags & SKCIPHER_WALK_PHYS))
0299 return 0;
0300
0301 p = kmalloc(sizeof(*p), skcipher_walk_gfp(walk));
0302 if (!p)
0303 return -ENOMEM;
0304
0305 p->data = walk->page;
0306 p->len = walk->nbytes;
0307 skcipher_queue_write(walk, p);
0308
0309 if (offset_in_page(walk->page) + walk->nbytes + walk->stride >
0310 PAGE_SIZE)
0311 walk->page = NULL;
0312 else
0313 walk->page += walk->nbytes;
0314
0315 return 0;
0316 }
0317
0318 static int skcipher_next_fast(struct skcipher_walk *walk)
0319 {
0320 unsigned long diff;
0321
0322 walk->src.phys.page = scatterwalk_page(&walk->in);
0323 walk->src.phys.offset = offset_in_page(walk->in.offset);
0324 walk->dst.phys.page = scatterwalk_page(&walk->out);
0325 walk->dst.phys.offset = offset_in_page(walk->out.offset);
0326
0327 if (walk->flags & SKCIPHER_WALK_PHYS)
0328 return 0;
0329
0330 diff = walk->src.phys.offset - walk->dst.phys.offset;
0331 diff |= walk->src.virt.page - walk->dst.virt.page;
0332
0333 skcipher_map_src(walk);
0334 walk->dst.virt.addr = walk->src.virt.addr;
0335
0336 if (diff) {
0337 walk->flags |= SKCIPHER_WALK_DIFF;
0338 skcipher_map_dst(walk);
0339 }
0340
0341 return 0;
0342 }
0343
0344 static int skcipher_walk_next(struct skcipher_walk *walk)
0345 {
0346 unsigned int bsize;
0347 unsigned int n;
0348 int err;
0349
0350 walk->flags &= ~(SKCIPHER_WALK_SLOW | SKCIPHER_WALK_COPY |
0351 SKCIPHER_WALK_DIFF);
0352
0353 n = walk->total;
0354 bsize = min(walk->stride, max(n, walk->blocksize));
0355 n = scatterwalk_clamp(&walk->in, n);
0356 n = scatterwalk_clamp(&walk->out, n);
0357
0358 if (unlikely(n < bsize)) {
0359 if (unlikely(walk->total < walk->blocksize))
0360 return skcipher_walk_done(walk, -EINVAL);
0361
0362 slow_path:
0363 err = skcipher_next_slow(walk, bsize);
0364 goto set_phys_lowmem;
0365 }
0366
0367 if (unlikely((walk->in.offset | walk->out.offset) & walk->alignmask)) {
0368 if (!walk->page) {
0369 gfp_t gfp = skcipher_walk_gfp(walk);
0370
0371 walk->page = (void *)__get_free_page(gfp);
0372 if (!walk->page)
0373 goto slow_path;
0374 }
0375
0376 walk->nbytes = min_t(unsigned, n,
0377 PAGE_SIZE - offset_in_page(walk->page));
0378 walk->flags |= SKCIPHER_WALK_COPY;
0379 err = skcipher_next_copy(walk);
0380 goto set_phys_lowmem;
0381 }
0382
0383 walk->nbytes = n;
0384
0385 return skcipher_next_fast(walk);
0386
0387 set_phys_lowmem:
0388 if (!err && (walk->flags & SKCIPHER_WALK_PHYS)) {
0389 walk->src.phys.page = virt_to_page(walk->src.virt.addr);
0390 walk->dst.phys.page = virt_to_page(walk->dst.virt.addr);
0391 walk->src.phys.offset &= PAGE_SIZE - 1;
0392 walk->dst.phys.offset &= PAGE_SIZE - 1;
0393 }
0394 return err;
0395 }
0396
0397 static int skcipher_copy_iv(struct skcipher_walk *walk)
0398 {
0399 unsigned a = crypto_tfm_ctx_alignment() - 1;
0400 unsigned alignmask = walk->alignmask;
0401 unsigned ivsize = walk->ivsize;
0402 unsigned bs = walk->stride;
0403 unsigned aligned_bs;
0404 unsigned size;
0405 u8 *iv;
0406
0407 aligned_bs = ALIGN(bs, alignmask + 1);
0408
0409
0410 size = alignmask & ~a;
0411
0412 if (walk->flags & SKCIPHER_WALK_PHYS)
0413 size += ivsize;
0414 else {
0415 size += aligned_bs + ivsize;
0416
0417
0418 size += (bs - 1) & ~(alignmask | a);
0419 }
0420
0421 walk->buffer = kmalloc(size, skcipher_walk_gfp(walk));
0422 if (!walk->buffer)
0423 return -ENOMEM;
0424
0425 iv = PTR_ALIGN(walk->buffer, alignmask + 1);
0426 iv = skcipher_get_spot(iv, bs) + aligned_bs;
0427
0428 walk->iv = memcpy(iv, walk->iv, walk->ivsize);
0429 return 0;
0430 }
0431
0432 static int skcipher_walk_first(struct skcipher_walk *walk)
0433 {
0434 if (WARN_ON_ONCE(in_hardirq()))
0435 return -EDEADLK;
0436
0437 walk->buffer = NULL;
0438 if (unlikely(((unsigned long)walk->iv & walk->alignmask))) {
0439 int err = skcipher_copy_iv(walk);
0440 if (err)
0441 return err;
0442 }
0443
0444 walk->page = NULL;
0445
0446 return skcipher_walk_next(walk);
0447 }
0448
0449 static int skcipher_walk_skcipher(struct skcipher_walk *walk,
0450 struct skcipher_request *req)
0451 {
0452 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
0453
0454 walk->total = req->cryptlen;
0455 walk->nbytes = 0;
0456 walk->iv = req->iv;
0457 walk->oiv = req->iv;
0458
0459 if (unlikely(!walk->total))
0460 return 0;
0461
0462 scatterwalk_start(&walk->in, req->src);
0463 scatterwalk_start(&walk->out, req->dst);
0464
0465 walk->flags &= ~SKCIPHER_WALK_SLEEP;
0466 walk->flags |= req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
0467 SKCIPHER_WALK_SLEEP : 0;
0468
0469 walk->blocksize = crypto_skcipher_blocksize(tfm);
0470 walk->stride = crypto_skcipher_walksize(tfm);
0471 walk->ivsize = crypto_skcipher_ivsize(tfm);
0472 walk->alignmask = crypto_skcipher_alignmask(tfm);
0473
0474 return skcipher_walk_first(walk);
0475 }
0476
0477 int skcipher_walk_virt(struct skcipher_walk *walk,
0478 struct skcipher_request *req, bool atomic)
0479 {
0480 int err;
0481
0482 might_sleep_if(req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
0483
0484 walk->flags &= ~SKCIPHER_WALK_PHYS;
0485
0486 err = skcipher_walk_skcipher(walk, req);
0487
0488 walk->flags &= atomic ? ~SKCIPHER_WALK_SLEEP : ~0;
0489
0490 return err;
0491 }
0492 EXPORT_SYMBOL_GPL(skcipher_walk_virt);
0493
0494 int skcipher_walk_async(struct skcipher_walk *walk,
0495 struct skcipher_request *req)
0496 {
0497 walk->flags |= SKCIPHER_WALK_PHYS;
0498
0499 INIT_LIST_HEAD(&walk->buffers);
0500
0501 return skcipher_walk_skcipher(walk, req);
0502 }
0503 EXPORT_SYMBOL_GPL(skcipher_walk_async);
0504
0505 static int skcipher_walk_aead_common(struct skcipher_walk *walk,
0506 struct aead_request *req, bool atomic)
0507 {
0508 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
0509 int err;
0510
0511 walk->nbytes = 0;
0512 walk->iv = req->iv;
0513 walk->oiv = req->iv;
0514
0515 if (unlikely(!walk->total))
0516 return 0;
0517
0518 walk->flags &= ~SKCIPHER_WALK_PHYS;
0519
0520 scatterwalk_start(&walk->in, req->src);
0521 scatterwalk_start(&walk->out, req->dst);
0522
0523 scatterwalk_copychunks(NULL, &walk->in, req->assoclen, 2);
0524 scatterwalk_copychunks(NULL, &walk->out, req->assoclen, 2);
0525
0526 scatterwalk_done(&walk->in, 0, walk->total);
0527 scatterwalk_done(&walk->out, 0, walk->total);
0528
0529 if (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP)
0530 walk->flags |= SKCIPHER_WALK_SLEEP;
0531 else
0532 walk->flags &= ~SKCIPHER_WALK_SLEEP;
0533
0534 walk->blocksize = crypto_aead_blocksize(tfm);
0535 walk->stride = crypto_aead_chunksize(tfm);
0536 walk->ivsize = crypto_aead_ivsize(tfm);
0537 walk->alignmask = crypto_aead_alignmask(tfm);
0538
0539 err = skcipher_walk_first(walk);
0540
0541 if (atomic)
0542 walk->flags &= ~SKCIPHER_WALK_SLEEP;
0543
0544 return err;
0545 }
0546
0547 int skcipher_walk_aead_encrypt(struct skcipher_walk *walk,
0548 struct aead_request *req, bool atomic)
0549 {
0550 walk->total = req->cryptlen;
0551
0552 return skcipher_walk_aead_common(walk, req, atomic);
0553 }
0554 EXPORT_SYMBOL_GPL(skcipher_walk_aead_encrypt);
0555
0556 int skcipher_walk_aead_decrypt(struct skcipher_walk *walk,
0557 struct aead_request *req, bool atomic)
0558 {
0559 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
0560
0561 walk->total = req->cryptlen - crypto_aead_authsize(tfm);
0562
0563 return skcipher_walk_aead_common(walk, req, atomic);
0564 }
0565 EXPORT_SYMBOL_GPL(skcipher_walk_aead_decrypt);
0566
0567 static void skcipher_set_needkey(struct crypto_skcipher *tfm)
0568 {
0569 if (crypto_skcipher_max_keysize(tfm) != 0)
0570 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
0571 }
0572
0573 static int skcipher_setkey_unaligned(struct crypto_skcipher *tfm,
0574 const u8 *key, unsigned int keylen)
0575 {
0576 unsigned long alignmask = crypto_skcipher_alignmask(tfm);
0577 struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
0578 u8 *buffer, *alignbuffer;
0579 unsigned long absize;
0580 int ret;
0581
0582 absize = keylen + alignmask;
0583 buffer = kmalloc(absize, GFP_ATOMIC);
0584 if (!buffer)
0585 return -ENOMEM;
0586
0587 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
0588 memcpy(alignbuffer, key, keylen);
0589 ret = cipher->setkey(tfm, alignbuffer, keylen);
0590 kfree_sensitive(buffer);
0591 return ret;
0592 }
0593
0594 int crypto_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
0595 unsigned int keylen)
0596 {
0597 struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
0598 unsigned long alignmask = crypto_skcipher_alignmask(tfm);
0599 int err;
0600
0601 if (keylen < cipher->min_keysize || keylen > cipher->max_keysize)
0602 return -EINVAL;
0603
0604 if ((unsigned long)key & alignmask)
0605 err = skcipher_setkey_unaligned(tfm, key, keylen);
0606 else
0607 err = cipher->setkey(tfm, key, keylen);
0608
0609 if (unlikely(err)) {
0610 skcipher_set_needkey(tfm);
0611 return err;
0612 }
0613
0614 crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
0615 return 0;
0616 }
0617 EXPORT_SYMBOL_GPL(crypto_skcipher_setkey);
0618
0619 int crypto_skcipher_encrypt(struct skcipher_request *req)
0620 {
0621 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
0622 struct crypto_alg *alg = tfm->base.__crt_alg;
0623 unsigned int cryptlen = req->cryptlen;
0624 int ret;
0625
0626 crypto_stats_get(alg);
0627 if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
0628 ret = -ENOKEY;
0629 else
0630 ret = crypto_skcipher_alg(tfm)->encrypt(req);
0631 crypto_stats_skcipher_encrypt(cryptlen, ret, alg);
0632 return ret;
0633 }
0634 EXPORT_SYMBOL_GPL(crypto_skcipher_encrypt);
0635
0636 int crypto_skcipher_decrypt(struct skcipher_request *req)
0637 {
0638 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
0639 struct crypto_alg *alg = tfm->base.__crt_alg;
0640 unsigned int cryptlen = req->cryptlen;
0641 int ret;
0642
0643 crypto_stats_get(alg);
0644 if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
0645 ret = -ENOKEY;
0646 else
0647 ret = crypto_skcipher_alg(tfm)->decrypt(req);
0648 crypto_stats_skcipher_decrypt(cryptlen, ret, alg);
0649 return ret;
0650 }
0651 EXPORT_SYMBOL_GPL(crypto_skcipher_decrypt);
0652
0653 static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
0654 {
0655 struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
0656 struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
0657
0658 alg->exit(skcipher);
0659 }
0660
0661 static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
0662 {
0663 struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
0664 struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
0665
0666 skcipher_set_needkey(skcipher);
0667
0668 if (alg->exit)
0669 skcipher->base.exit = crypto_skcipher_exit_tfm;
0670
0671 if (alg->init)
0672 return alg->init(skcipher);
0673
0674 return 0;
0675 }
0676
0677 static void crypto_skcipher_free_instance(struct crypto_instance *inst)
0678 {
0679 struct skcipher_instance *skcipher =
0680 container_of(inst, struct skcipher_instance, s.base);
0681
0682 skcipher->free(skcipher);
0683 }
0684
0685 static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
0686 __maybe_unused;
0687 static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
0688 {
0689 struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
0690 base);
0691
0692 seq_printf(m, "type : skcipher\n");
0693 seq_printf(m, "async : %s\n",
0694 alg->cra_flags & CRYPTO_ALG_ASYNC ? "yes" : "no");
0695 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
0696 seq_printf(m, "min keysize : %u\n", skcipher->min_keysize);
0697 seq_printf(m, "max keysize : %u\n", skcipher->max_keysize);
0698 seq_printf(m, "ivsize : %u\n", skcipher->ivsize);
0699 seq_printf(m, "chunksize : %u\n", skcipher->chunksize);
0700 seq_printf(m, "walksize : %u\n", skcipher->walksize);
0701 }
0702
0703 #ifdef CONFIG_NET
0704 static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
0705 {
0706 struct crypto_report_blkcipher rblkcipher;
0707 struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
0708 base);
0709
0710 memset(&rblkcipher, 0, sizeof(rblkcipher));
0711
0712 strscpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type));
0713 strscpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
0714
0715 rblkcipher.blocksize = alg->cra_blocksize;
0716 rblkcipher.min_keysize = skcipher->min_keysize;
0717 rblkcipher.max_keysize = skcipher->max_keysize;
0718 rblkcipher.ivsize = skcipher->ivsize;
0719
0720 return nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
0721 sizeof(rblkcipher), &rblkcipher);
0722 }
0723 #else
0724 static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
0725 {
0726 return -ENOSYS;
0727 }
0728 #endif
0729
0730 static const struct crypto_type crypto_skcipher_type = {
0731 .extsize = crypto_alg_extsize,
0732 .init_tfm = crypto_skcipher_init_tfm,
0733 .free = crypto_skcipher_free_instance,
0734 #ifdef CONFIG_PROC_FS
0735 .show = crypto_skcipher_show,
0736 #endif
0737 .report = crypto_skcipher_report,
0738 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
0739 .maskset = CRYPTO_ALG_TYPE_MASK,
0740 .type = CRYPTO_ALG_TYPE_SKCIPHER,
0741 .tfmsize = offsetof(struct crypto_skcipher, base),
0742 };
0743
0744 int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
0745 struct crypto_instance *inst,
0746 const char *name, u32 type, u32 mask)
0747 {
0748 spawn->base.frontend = &crypto_skcipher_type;
0749 return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
0750 }
0751 EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
0752
0753 struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
0754 u32 type, u32 mask)
0755 {
0756 return crypto_alloc_tfm(alg_name, &crypto_skcipher_type, type, mask);
0757 }
0758 EXPORT_SYMBOL_GPL(crypto_alloc_skcipher);
0759
0760 struct crypto_sync_skcipher *crypto_alloc_sync_skcipher(
0761 const char *alg_name, u32 type, u32 mask)
0762 {
0763 struct crypto_skcipher *tfm;
0764
0765
0766 mask |= CRYPTO_ALG_ASYNC;
0767
0768 tfm = crypto_alloc_tfm(alg_name, &crypto_skcipher_type, type, mask);
0769
0770
0771
0772
0773
0774 if (!IS_ERR(tfm) && WARN_ON(crypto_skcipher_reqsize(tfm) >
0775 MAX_SYNC_SKCIPHER_REQSIZE)) {
0776 crypto_free_skcipher(tfm);
0777 return ERR_PTR(-EINVAL);
0778 }
0779
0780 return (struct crypto_sync_skcipher *)tfm;
0781 }
0782 EXPORT_SYMBOL_GPL(crypto_alloc_sync_skcipher);
0783
0784 int crypto_has_skcipher(const char *alg_name, u32 type, u32 mask)
0785 {
0786 return crypto_type_has_alg(alg_name, &crypto_skcipher_type, type, mask);
0787 }
0788 EXPORT_SYMBOL_GPL(crypto_has_skcipher);
0789
0790 static int skcipher_prepare_alg(struct skcipher_alg *alg)
0791 {
0792 struct crypto_alg *base = &alg->base;
0793
0794 if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8 ||
0795 alg->walksize > PAGE_SIZE / 8)
0796 return -EINVAL;
0797
0798 if (!alg->chunksize)
0799 alg->chunksize = base->cra_blocksize;
0800 if (!alg->walksize)
0801 alg->walksize = alg->chunksize;
0802
0803 base->cra_type = &crypto_skcipher_type;
0804 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
0805 base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER;
0806
0807 return 0;
0808 }
0809
0810 int crypto_register_skcipher(struct skcipher_alg *alg)
0811 {
0812 struct crypto_alg *base = &alg->base;
0813 int err;
0814
0815 err = skcipher_prepare_alg(alg);
0816 if (err)
0817 return err;
0818
0819 return crypto_register_alg(base);
0820 }
0821 EXPORT_SYMBOL_GPL(crypto_register_skcipher);
0822
0823 void crypto_unregister_skcipher(struct skcipher_alg *alg)
0824 {
0825 crypto_unregister_alg(&alg->base);
0826 }
0827 EXPORT_SYMBOL_GPL(crypto_unregister_skcipher);
0828
0829 int crypto_register_skciphers(struct skcipher_alg *algs, int count)
0830 {
0831 int i, ret;
0832
0833 for (i = 0; i < count; i++) {
0834 ret = crypto_register_skcipher(&algs[i]);
0835 if (ret)
0836 goto err;
0837 }
0838
0839 return 0;
0840
0841 err:
0842 for (--i; i >= 0; --i)
0843 crypto_unregister_skcipher(&algs[i]);
0844
0845 return ret;
0846 }
0847 EXPORT_SYMBOL_GPL(crypto_register_skciphers);
0848
0849 void crypto_unregister_skciphers(struct skcipher_alg *algs, int count)
0850 {
0851 int i;
0852
0853 for (i = count - 1; i >= 0; --i)
0854 crypto_unregister_skcipher(&algs[i]);
0855 }
0856 EXPORT_SYMBOL_GPL(crypto_unregister_skciphers);
0857
0858 int skcipher_register_instance(struct crypto_template *tmpl,
0859 struct skcipher_instance *inst)
0860 {
0861 int err;
0862
0863 if (WARN_ON(!inst->free))
0864 return -EINVAL;
0865
0866 err = skcipher_prepare_alg(&inst->alg);
0867 if (err)
0868 return err;
0869
0870 return crypto_register_instance(tmpl, skcipher_crypto_instance(inst));
0871 }
0872 EXPORT_SYMBOL_GPL(skcipher_register_instance);
0873
0874 static int skcipher_setkey_simple(struct crypto_skcipher *tfm, const u8 *key,
0875 unsigned int keylen)
0876 {
0877 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
0878
0879 crypto_cipher_clear_flags(cipher, CRYPTO_TFM_REQ_MASK);
0880 crypto_cipher_set_flags(cipher, crypto_skcipher_get_flags(tfm) &
0881 CRYPTO_TFM_REQ_MASK);
0882 return crypto_cipher_setkey(cipher, key, keylen);
0883 }
0884
0885 static int skcipher_init_tfm_simple(struct crypto_skcipher *tfm)
0886 {
0887 struct skcipher_instance *inst = skcipher_alg_instance(tfm);
0888 struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst);
0889 struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
0890 struct crypto_cipher *cipher;
0891
0892 cipher = crypto_spawn_cipher(spawn);
0893 if (IS_ERR(cipher))
0894 return PTR_ERR(cipher);
0895
0896 ctx->cipher = cipher;
0897 return 0;
0898 }
0899
0900 static void skcipher_exit_tfm_simple(struct crypto_skcipher *tfm)
0901 {
0902 struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
0903
0904 crypto_free_cipher(ctx->cipher);
0905 }
0906
0907 static void skcipher_free_instance_simple(struct skcipher_instance *inst)
0908 {
0909 crypto_drop_cipher(skcipher_instance_ctx(inst));
0910 kfree(inst);
0911 }
0912
0913
0914
0915
0916
0917
0918
0919
0920
0921
0922
0923
0924
0925
0926
0927
0928
0929 struct skcipher_instance *skcipher_alloc_instance_simple(
0930 struct crypto_template *tmpl, struct rtattr **tb)
0931 {
0932 u32 mask;
0933 struct skcipher_instance *inst;
0934 struct crypto_cipher_spawn *spawn;
0935 struct crypto_alg *cipher_alg;
0936 int err;
0937
0938 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
0939 if (err)
0940 return ERR_PTR(err);
0941
0942 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
0943 if (!inst)
0944 return ERR_PTR(-ENOMEM);
0945 spawn = skcipher_instance_ctx(inst);
0946
0947 err = crypto_grab_cipher(spawn, skcipher_crypto_instance(inst),
0948 crypto_attr_alg_name(tb[1]), 0, mask);
0949 if (err)
0950 goto err_free_inst;
0951 cipher_alg = crypto_spawn_cipher_alg(spawn);
0952
0953 err = crypto_inst_setname(skcipher_crypto_instance(inst), tmpl->name,
0954 cipher_alg);
0955 if (err)
0956 goto err_free_inst;
0957
0958 inst->free = skcipher_free_instance_simple;
0959
0960
0961 inst->alg.base.cra_blocksize = cipher_alg->cra_blocksize;
0962 inst->alg.base.cra_alignmask = cipher_alg->cra_alignmask;
0963 inst->alg.base.cra_priority = cipher_alg->cra_priority;
0964 inst->alg.min_keysize = cipher_alg->cra_cipher.cia_min_keysize;
0965 inst->alg.max_keysize = cipher_alg->cra_cipher.cia_max_keysize;
0966 inst->alg.ivsize = cipher_alg->cra_blocksize;
0967
0968
0969 inst->alg.base.cra_ctxsize = sizeof(struct skcipher_ctx_simple);
0970 inst->alg.setkey = skcipher_setkey_simple;
0971 inst->alg.init = skcipher_init_tfm_simple;
0972 inst->alg.exit = skcipher_exit_tfm_simple;
0973
0974 return inst;
0975
0976 err_free_inst:
0977 skcipher_free_instance_simple(inst);
0978 return ERR_PTR(err);
0979 }
0980 EXPORT_SYMBOL_GPL(skcipher_alloc_instance_simple);
0981
0982 MODULE_LICENSE("GPL");
0983 MODULE_DESCRIPTION("Symmetric key cipher type");
0984 MODULE_IMPORT_NS(CRYPTO_INTERNAL);