1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Symmetric key cipher operations.
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.
9 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
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/compiler.h>
19 #include <linux/list.h>
20 #include <linux/module.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/seq_file.h>
23 #include <net/netlink.h>
28 SKCIPHER_WALK_PHYS = 1 << 0,
29 SKCIPHER_WALK_SLOW = 1 << 1,
30 SKCIPHER_WALK_COPY = 1 << 2,
31 SKCIPHER_WALK_DIFF = 1 << 3,
32 SKCIPHER_WALK_SLEEP = 1 << 4,
35 struct skcipher_walk_buffer {
36 struct list_head entry;
37 struct scatter_walk dst;
43 static int skcipher_walk_next(struct skcipher_walk *walk);
45 static inline void skcipher_map_src(struct skcipher_walk *walk)
47 walk->src.virt.addr = scatterwalk_map(&walk->in);
50 static inline void skcipher_map_dst(struct skcipher_walk *walk)
52 walk->dst.virt.addr = scatterwalk_map(&walk->out);
55 static inline void skcipher_unmap_src(struct skcipher_walk *walk)
57 scatterwalk_unmap(walk->src.virt.addr);
60 static inline void skcipher_unmap_dst(struct skcipher_walk *walk)
62 scatterwalk_unmap(walk->dst.virt.addr);
65 static inline gfp_t skcipher_walk_gfp(struct skcipher_walk *walk)
67 return walk->flags & SKCIPHER_WALK_SLEEP ? GFP_KERNEL : GFP_ATOMIC;
70 /* Get a spot of the specified length that does not straddle a page.
71 * The caller needs to ensure that there is enough space for this operation.
73 static inline u8 *skcipher_get_spot(u8 *start, unsigned int len)
75 u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
77 return max(start, end_page);
80 static int skcipher_done_slow(struct skcipher_walk *walk, unsigned int bsize)
84 addr = (u8 *)ALIGN((unsigned long)walk->buffer, walk->alignmask + 1);
85 addr = skcipher_get_spot(addr, bsize);
86 scatterwalk_copychunks(addr, &walk->out, bsize,
87 (walk->flags & SKCIPHER_WALK_PHYS) ? 2 : 1);
91 int skcipher_walk_done(struct skcipher_walk *walk, int err)
93 unsigned int n = walk->nbytes;
94 unsigned int nbytes = 0;
99 if (likely(err >= 0)) {
101 nbytes = walk->total - n;
104 if (likely(!(walk->flags & (SKCIPHER_WALK_PHYS |
107 SKCIPHER_WALK_DIFF)))) {
109 skcipher_unmap_src(walk);
110 } else if (walk->flags & SKCIPHER_WALK_DIFF) {
111 skcipher_unmap_dst(walk);
113 } else if (walk->flags & SKCIPHER_WALK_COPY) {
114 skcipher_map_dst(walk);
115 memcpy(walk->dst.virt.addr, walk->page, n);
116 skcipher_unmap_dst(walk);
117 } else if (unlikely(walk->flags & SKCIPHER_WALK_SLOW)) {
120 * Didn't process all bytes. Either the algorithm is
121 * broken, or this was the last step and it turned out
122 * the message wasn't evenly divisible into blocks but
123 * the algorithm requires it.
128 n = skcipher_done_slow(walk, n);
134 walk->total = nbytes;
137 scatterwalk_advance(&walk->in, n);
138 scatterwalk_advance(&walk->out, n);
139 scatterwalk_done(&walk->in, 0, nbytes);
140 scatterwalk_done(&walk->out, 1, nbytes);
143 crypto_yield(walk->flags & SKCIPHER_WALK_SLEEP ?
144 CRYPTO_TFM_REQ_MAY_SLEEP : 0);
145 return skcipher_walk_next(walk);
149 /* Short-circuit for the common/fast path. */
150 if (!((unsigned long)walk->buffer | (unsigned long)walk->page))
153 if (walk->flags & SKCIPHER_WALK_PHYS)
156 if (walk->iv != walk->oiv)
157 memcpy(walk->oiv, walk->iv, walk->ivsize);
158 if (walk->buffer != walk->page)
161 free_page((unsigned long)walk->page);
166 EXPORT_SYMBOL_GPL(skcipher_walk_done);
168 void skcipher_walk_complete(struct skcipher_walk *walk, int err)
170 struct skcipher_walk_buffer *p, *tmp;
172 list_for_each_entry_safe(p, tmp, &walk->buffers, entry) {
180 data = PTR_ALIGN(&p->buffer[0], walk->alignmask + 1);
181 data = skcipher_get_spot(data, walk->stride);
184 scatterwalk_copychunks(data, &p->dst, p->len, 1);
186 if (offset_in_page(p->data) + p->len + walk->stride >
188 free_page((unsigned long)p->data);
195 if (!err && walk->iv != walk->oiv)
196 memcpy(walk->oiv, walk->iv, walk->ivsize);
197 if (walk->buffer != walk->page)
200 free_page((unsigned long)walk->page);
202 EXPORT_SYMBOL_GPL(skcipher_walk_complete);
204 static void skcipher_queue_write(struct skcipher_walk *walk,
205 struct skcipher_walk_buffer *p)
208 list_add_tail(&p->entry, &walk->buffers);
211 static int skcipher_next_slow(struct skcipher_walk *walk, unsigned int bsize)
213 bool phys = walk->flags & SKCIPHER_WALK_PHYS;
214 unsigned alignmask = walk->alignmask;
215 struct skcipher_walk_buffer *p;
223 walk->buffer = walk->page;
224 buffer = walk->buffer;
229 /* Start with the minimum alignment of kmalloc. */
230 a = crypto_tfm_ctx_alignment() - 1;
234 /* Calculate the minimum alignment of p->buffer. */
235 a &= (sizeof(*p) ^ (sizeof(*p) - 1)) >> 1;
239 /* Minimum size to align p->buffer by alignmask. */
242 /* Minimum size to ensure p->buffer does not straddle a page. */
243 n += (bsize - 1) & ~(alignmask | a);
245 v = kzalloc(n, skcipher_walk_gfp(walk));
247 return skcipher_walk_done(walk, -ENOMEM);
252 skcipher_queue_write(walk, p);
260 walk->dst.virt.addr = PTR_ALIGN(buffer, alignmask + 1);
261 walk->dst.virt.addr = skcipher_get_spot(walk->dst.virt.addr, bsize);
262 walk->src.virt.addr = walk->dst.virt.addr;
264 scatterwalk_copychunks(walk->src.virt.addr, &walk->in, bsize, 0);
266 walk->nbytes = bsize;
267 walk->flags |= SKCIPHER_WALK_SLOW;
272 static int skcipher_next_copy(struct skcipher_walk *walk)
274 struct skcipher_walk_buffer *p;
275 u8 *tmp = walk->page;
277 skcipher_map_src(walk);
278 memcpy(tmp, walk->src.virt.addr, walk->nbytes);
279 skcipher_unmap_src(walk);
281 walk->src.virt.addr = tmp;
282 walk->dst.virt.addr = tmp;
284 if (!(walk->flags & SKCIPHER_WALK_PHYS))
287 p = kmalloc(sizeof(*p), skcipher_walk_gfp(walk));
291 p->data = walk->page;
292 p->len = walk->nbytes;
293 skcipher_queue_write(walk, p);
295 if (offset_in_page(walk->page) + walk->nbytes + walk->stride >
299 walk->page += walk->nbytes;
304 static int skcipher_next_fast(struct skcipher_walk *walk)
308 walk->src.phys.page = scatterwalk_page(&walk->in);
309 walk->src.phys.offset = offset_in_page(walk->in.offset);
310 walk->dst.phys.page = scatterwalk_page(&walk->out);
311 walk->dst.phys.offset = offset_in_page(walk->out.offset);
313 if (walk->flags & SKCIPHER_WALK_PHYS)
316 diff = walk->src.phys.offset - walk->dst.phys.offset;
317 diff |= walk->src.virt.page - walk->dst.virt.page;
319 skcipher_map_src(walk);
320 walk->dst.virt.addr = walk->src.virt.addr;
323 walk->flags |= SKCIPHER_WALK_DIFF;
324 skcipher_map_dst(walk);
330 static int skcipher_walk_next(struct skcipher_walk *walk)
336 walk->flags &= ~(SKCIPHER_WALK_SLOW | SKCIPHER_WALK_COPY |
340 bsize = min(walk->stride, max(n, walk->blocksize));
341 n = scatterwalk_clamp(&walk->in, n);
342 n = scatterwalk_clamp(&walk->out, n);
344 if (unlikely(n < bsize)) {
345 if (unlikely(walk->total < walk->blocksize))
346 return skcipher_walk_done(walk, -EINVAL);
349 err = skcipher_next_slow(walk, bsize);
350 goto set_phys_lowmem;
353 if (unlikely((walk->in.offset | walk->out.offset) & walk->alignmask)) {
355 gfp_t gfp = skcipher_walk_gfp(walk);
357 walk->page = (void *)__get_free_page(gfp);
362 walk->nbytes = min_t(unsigned, n,
363 PAGE_SIZE - offset_in_page(walk->page));
364 walk->flags |= SKCIPHER_WALK_COPY;
365 err = skcipher_next_copy(walk);
366 goto set_phys_lowmem;
371 return skcipher_next_fast(walk);
374 if (!err && (walk->flags & SKCIPHER_WALK_PHYS)) {
375 walk->src.phys.page = virt_to_page(walk->src.virt.addr);
376 walk->dst.phys.page = virt_to_page(walk->dst.virt.addr);
377 walk->src.phys.offset &= PAGE_SIZE - 1;
378 walk->dst.phys.offset &= PAGE_SIZE - 1;
383 static int skcipher_copy_iv(struct skcipher_walk *walk)
385 unsigned a = crypto_tfm_ctx_alignment() - 1;
386 unsigned alignmask = walk->alignmask;
387 unsigned ivsize = walk->ivsize;
388 unsigned bs = walk->stride;
393 aligned_bs = ALIGN(bs, alignmask + 1);
395 /* Minimum size to align buffer by alignmask. */
396 size = alignmask & ~a;
398 if (walk->flags & SKCIPHER_WALK_PHYS)
401 size += aligned_bs + ivsize;
403 /* Minimum size to ensure buffer does not straddle a page. */
404 size += (bs - 1) & ~(alignmask | a);
407 walk->buffer = kmalloc(size, skcipher_walk_gfp(walk));
411 iv = PTR_ALIGN(walk->buffer, alignmask + 1);
412 iv = skcipher_get_spot(iv, bs) + aligned_bs;
414 walk->iv = memcpy(iv, walk->iv, walk->ivsize);
418 static int skcipher_walk_first(struct skcipher_walk *walk)
420 if (WARN_ON_ONCE(in_hardirq()))
424 if (unlikely(((unsigned long)walk->iv & walk->alignmask))) {
425 int err = skcipher_copy_iv(walk);
432 return skcipher_walk_next(walk);
435 static int skcipher_walk_skcipher(struct skcipher_walk *walk,
436 struct skcipher_request *req)
438 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
440 walk->total = req->cryptlen;
445 if (unlikely(!walk->total))
448 scatterwalk_start(&walk->in, req->src);
449 scatterwalk_start(&walk->out, req->dst);
451 walk->flags &= ~SKCIPHER_WALK_SLEEP;
452 walk->flags |= req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
453 SKCIPHER_WALK_SLEEP : 0;
455 walk->blocksize = crypto_skcipher_blocksize(tfm);
456 walk->stride = crypto_skcipher_walksize(tfm);
457 walk->ivsize = crypto_skcipher_ivsize(tfm);
458 walk->alignmask = crypto_skcipher_alignmask(tfm);
460 return skcipher_walk_first(walk);
463 int skcipher_walk_virt(struct skcipher_walk *walk,
464 struct skcipher_request *req, bool atomic)
468 might_sleep_if(req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
470 walk->flags &= ~SKCIPHER_WALK_PHYS;
472 err = skcipher_walk_skcipher(walk, req);
474 walk->flags &= atomic ? ~SKCIPHER_WALK_SLEEP : ~0;
478 EXPORT_SYMBOL_GPL(skcipher_walk_virt);
480 int skcipher_walk_async(struct skcipher_walk *walk,
481 struct skcipher_request *req)
483 walk->flags |= SKCIPHER_WALK_PHYS;
485 INIT_LIST_HEAD(&walk->buffers);
487 return skcipher_walk_skcipher(walk, req);
489 EXPORT_SYMBOL_GPL(skcipher_walk_async);
491 static int skcipher_walk_aead_common(struct skcipher_walk *walk,
492 struct aead_request *req, bool atomic)
494 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
501 if (unlikely(!walk->total))
504 walk->flags &= ~SKCIPHER_WALK_PHYS;
506 scatterwalk_start(&walk->in, req->src);
507 scatterwalk_start(&walk->out, req->dst);
509 scatterwalk_copychunks(NULL, &walk->in, req->assoclen, 2);
510 scatterwalk_copychunks(NULL, &walk->out, req->assoclen, 2);
512 scatterwalk_done(&walk->in, 0, walk->total);
513 scatterwalk_done(&walk->out, 0, walk->total);
515 if (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP)
516 walk->flags |= SKCIPHER_WALK_SLEEP;
518 walk->flags &= ~SKCIPHER_WALK_SLEEP;
520 walk->blocksize = crypto_aead_blocksize(tfm);
521 walk->stride = crypto_aead_chunksize(tfm);
522 walk->ivsize = crypto_aead_ivsize(tfm);
523 walk->alignmask = crypto_aead_alignmask(tfm);
525 err = skcipher_walk_first(walk);
528 walk->flags &= ~SKCIPHER_WALK_SLEEP;
533 int skcipher_walk_aead_encrypt(struct skcipher_walk *walk,
534 struct aead_request *req, bool atomic)
536 walk->total = req->cryptlen;
538 return skcipher_walk_aead_common(walk, req, atomic);
540 EXPORT_SYMBOL_GPL(skcipher_walk_aead_encrypt);
542 int skcipher_walk_aead_decrypt(struct skcipher_walk *walk,
543 struct aead_request *req, bool atomic)
545 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
547 walk->total = req->cryptlen - crypto_aead_authsize(tfm);
549 return skcipher_walk_aead_common(walk, req, atomic);
551 EXPORT_SYMBOL_GPL(skcipher_walk_aead_decrypt);
553 static void skcipher_set_needkey(struct crypto_skcipher *tfm)
555 if (crypto_skcipher_max_keysize(tfm) != 0)
556 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
559 static int skcipher_setkey_unaligned(struct crypto_skcipher *tfm,
560 const u8 *key, unsigned int keylen)
562 unsigned long alignmask = crypto_skcipher_alignmask(tfm);
563 struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
564 u8 *buffer, *alignbuffer;
565 unsigned long absize;
568 absize = keylen + alignmask;
569 buffer = kmalloc(absize, GFP_ATOMIC);
573 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
574 memcpy(alignbuffer, key, keylen);
575 ret = cipher->setkey(tfm, alignbuffer, keylen);
576 kfree_sensitive(buffer);
580 int crypto_skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
583 struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
584 unsigned long alignmask = crypto_skcipher_alignmask(tfm);
587 if (keylen < cipher->min_keysize || keylen > cipher->max_keysize)
590 if ((unsigned long)key & alignmask)
591 err = skcipher_setkey_unaligned(tfm, key, keylen);
593 err = cipher->setkey(tfm, key, keylen);
596 skcipher_set_needkey(tfm);
600 crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
603 EXPORT_SYMBOL_GPL(crypto_skcipher_setkey);
605 int crypto_skcipher_encrypt(struct skcipher_request *req)
607 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
608 struct crypto_alg *alg = tfm->base.__crt_alg;
609 unsigned int cryptlen = req->cryptlen;
612 crypto_stats_get(alg);
613 if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
616 ret = crypto_skcipher_alg(tfm)->encrypt(req);
617 crypto_stats_skcipher_encrypt(cryptlen, ret, alg);
620 EXPORT_SYMBOL_GPL(crypto_skcipher_encrypt);
622 int crypto_skcipher_decrypt(struct skcipher_request *req)
624 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
625 struct crypto_alg *alg = tfm->base.__crt_alg;
626 unsigned int cryptlen = req->cryptlen;
629 crypto_stats_get(alg);
630 if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
633 ret = crypto_skcipher_alg(tfm)->decrypt(req);
634 crypto_stats_skcipher_decrypt(cryptlen, ret, alg);
637 EXPORT_SYMBOL_GPL(crypto_skcipher_decrypt);
639 static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
641 struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
642 struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
647 static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
649 struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
650 struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
652 skcipher_set_needkey(skcipher);
655 skcipher->base.exit = crypto_skcipher_exit_tfm;
658 return alg->init(skcipher);
663 static void crypto_skcipher_free_instance(struct crypto_instance *inst)
665 struct skcipher_instance *skcipher =
666 container_of(inst, struct skcipher_instance, s.base);
668 skcipher->free(skcipher);
671 static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
673 static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
675 struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
678 seq_printf(m, "type : skcipher\n");
679 seq_printf(m, "async : %s\n",
680 alg->cra_flags & CRYPTO_ALG_ASYNC ? "yes" : "no");
681 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
682 seq_printf(m, "min keysize : %u\n", skcipher->min_keysize);
683 seq_printf(m, "max keysize : %u\n", skcipher->max_keysize);
684 seq_printf(m, "ivsize : %u\n", skcipher->ivsize);
685 seq_printf(m, "chunksize : %u\n", skcipher->chunksize);
686 seq_printf(m, "walksize : %u\n", skcipher->walksize);
690 static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
692 struct crypto_report_blkcipher rblkcipher;
693 struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
696 memset(&rblkcipher, 0, sizeof(rblkcipher));
698 strscpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type));
699 strscpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
701 rblkcipher.blocksize = alg->cra_blocksize;
702 rblkcipher.min_keysize = skcipher->min_keysize;
703 rblkcipher.max_keysize = skcipher->max_keysize;
704 rblkcipher.ivsize = skcipher->ivsize;
706 return nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
707 sizeof(rblkcipher), &rblkcipher);
710 static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
716 static const struct crypto_type crypto_skcipher_type = {
717 .extsize = crypto_alg_extsize,
718 .init_tfm = crypto_skcipher_init_tfm,
719 .free = crypto_skcipher_free_instance,
720 #ifdef CONFIG_PROC_FS
721 .show = crypto_skcipher_show,
723 .report = crypto_skcipher_report,
724 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
725 .maskset = CRYPTO_ALG_TYPE_MASK,
726 .type = CRYPTO_ALG_TYPE_SKCIPHER,
727 .tfmsize = offsetof(struct crypto_skcipher, base),
730 int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
731 struct crypto_instance *inst,
732 const char *name, u32 type, u32 mask)
734 spawn->base.frontend = &crypto_skcipher_type;
735 return crypto_grab_spawn(&spawn->base, inst, name, type, mask);
737 EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
739 struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
742 return crypto_alloc_tfm(alg_name, &crypto_skcipher_type, type, mask);
744 EXPORT_SYMBOL_GPL(crypto_alloc_skcipher);
746 struct crypto_sync_skcipher *crypto_alloc_sync_skcipher(
747 const char *alg_name, u32 type, u32 mask)
749 struct crypto_skcipher *tfm;
751 /* Only sync algorithms allowed. */
752 mask |= CRYPTO_ALG_ASYNC | CRYPTO_ALG_SKCIPHER_REQSIZE_LARGE;
754 tfm = crypto_alloc_tfm(alg_name, &crypto_skcipher_type, type, mask);
757 * Make sure we do not allocate something that might get used with
758 * an on-stack request: check the request size.
760 if (!IS_ERR(tfm) && WARN_ON(crypto_skcipher_reqsize(tfm) >
761 MAX_SYNC_SKCIPHER_REQSIZE)) {
762 crypto_free_skcipher(tfm);
763 return ERR_PTR(-EINVAL);
766 return (struct crypto_sync_skcipher *)tfm;
768 EXPORT_SYMBOL_GPL(crypto_alloc_sync_skcipher);
770 int crypto_has_skcipher(const char *alg_name, u32 type, u32 mask)
772 return crypto_type_has_alg(alg_name, &crypto_skcipher_type, type, mask);
774 EXPORT_SYMBOL_GPL(crypto_has_skcipher);
776 static int skcipher_prepare_alg(struct skcipher_alg *alg)
778 struct crypto_alg *base = &alg->base;
780 if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8 ||
781 alg->walksize > PAGE_SIZE / 8)
785 alg->chunksize = base->cra_blocksize;
787 alg->walksize = alg->chunksize;
789 base->cra_type = &crypto_skcipher_type;
790 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
791 base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER;
796 int crypto_register_skcipher(struct skcipher_alg *alg)
798 struct crypto_alg *base = &alg->base;
801 err = skcipher_prepare_alg(alg);
805 return crypto_register_alg(base);
807 EXPORT_SYMBOL_GPL(crypto_register_skcipher);
809 void crypto_unregister_skcipher(struct skcipher_alg *alg)
811 crypto_unregister_alg(&alg->base);
813 EXPORT_SYMBOL_GPL(crypto_unregister_skcipher);
815 int crypto_register_skciphers(struct skcipher_alg *algs, int count)
819 for (i = 0; i < count; i++) {
820 ret = crypto_register_skcipher(&algs[i]);
828 for (--i; i >= 0; --i)
829 crypto_unregister_skcipher(&algs[i]);
833 EXPORT_SYMBOL_GPL(crypto_register_skciphers);
835 void crypto_unregister_skciphers(struct skcipher_alg *algs, int count)
839 for (i = count - 1; i >= 0; --i)
840 crypto_unregister_skcipher(&algs[i]);
842 EXPORT_SYMBOL_GPL(crypto_unregister_skciphers);
844 int skcipher_register_instance(struct crypto_template *tmpl,
845 struct skcipher_instance *inst)
849 if (WARN_ON(!inst->free))
852 err = skcipher_prepare_alg(&inst->alg);
856 return crypto_register_instance(tmpl, skcipher_crypto_instance(inst));
858 EXPORT_SYMBOL_GPL(skcipher_register_instance);
860 static int skcipher_setkey_simple(struct crypto_skcipher *tfm, const u8 *key,
863 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
865 crypto_cipher_clear_flags(cipher, CRYPTO_TFM_REQ_MASK);
866 crypto_cipher_set_flags(cipher, crypto_skcipher_get_flags(tfm) &
867 CRYPTO_TFM_REQ_MASK);
868 return crypto_cipher_setkey(cipher, key, keylen);
871 static int skcipher_init_tfm_simple(struct crypto_skcipher *tfm)
873 struct skcipher_instance *inst = skcipher_alg_instance(tfm);
874 struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst);
875 struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
876 struct crypto_cipher *cipher;
878 cipher = crypto_spawn_cipher(spawn);
880 return PTR_ERR(cipher);
882 ctx->cipher = cipher;
886 static void skcipher_exit_tfm_simple(struct crypto_skcipher *tfm)
888 struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
890 crypto_free_cipher(ctx->cipher);
893 static void skcipher_free_instance_simple(struct skcipher_instance *inst)
895 crypto_drop_cipher(skcipher_instance_ctx(inst));
900 * skcipher_alloc_instance_simple - allocate instance of simple block cipher mode
902 * Allocate an skcipher_instance for a simple block cipher mode of operation,
903 * e.g. cbc or ecb. The instance context will have just a single crypto_spawn,
904 * that for the underlying cipher. The {min,max}_keysize, ivsize, blocksize,
905 * alignmask, and priority are set from the underlying cipher but can be
906 * overridden if needed. The tfm context defaults to skcipher_ctx_simple, and
907 * default ->setkey(), ->init(), and ->exit() methods are installed.
909 * @tmpl: the template being instantiated
910 * @tb: the template parameters
912 * Return: a pointer to the new instance, or an ERR_PTR(). The caller still
913 * needs to register the instance.
915 struct skcipher_instance *skcipher_alloc_instance_simple(
916 struct crypto_template *tmpl, struct rtattr **tb)
919 struct skcipher_instance *inst;
920 struct crypto_cipher_spawn *spawn;
921 struct crypto_alg *cipher_alg;
924 err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
928 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
930 return ERR_PTR(-ENOMEM);
931 spawn = skcipher_instance_ctx(inst);
933 err = crypto_grab_cipher(spawn, skcipher_crypto_instance(inst),
934 crypto_attr_alg_name(tb[1]), 0, mask);
937 cipher_alg = crypto_spawn_cipher_alg(spawn);
939 err = crypto_inst_setname(skcipher_crypto_instance(inst), tmpl->name,
944 inst->free = skcipher_free_instance_simple;
946 /* Default algorithm properties, can be overridden */
947 inst->alg.base.cra_blocksize = cipher_alg->cra_blocksize;
948 inst->alg.base.cra_alignmask = cipher_alg->cra_alignmask;
949 inst->alg.base.cra_priority = cipher_alg->cra_priority;
950 inst->alg.min_keysize = cipher_alg->cra_cipher.cia_min_keysize;
951 inst->alg.max_keysize = cipher_alg->cra_cipher.cia_max_keysize;
952 inst->alg.ivsize = cipher_alg->cra_blocksize;
954 /* Use skcipher_ctx_simple by default, can be overridden */
955 inst->alg.base.cra_ctxsize = sizeof(struct skcipher_ctx_simple);
956 inst->alg.setkey = skcipher_setkey_simple;
957 inst->alg.init = skcipher_init_tfm_simple;
958 inst->alg.exit = skcipher_exit_tfm_simple;
963 skcipher_free_instance_simple(inst);
966 EXPORT_SYMBOL_GPL(skcipher_alloc_instance_simple);
968 MODULE_LICENSE("GPL");
969 MODULE_DESCRIPTION("Symmetric key cipher type");
970 MODULE_IMPORT_NS(CRYPTO_INTERNAL);