tools: PCI: Fix compilation warnings
[linux-stable.git] / crypto / skcipher.c
blob915bbae3d5bc4e7a8c7bd0b926a47fac05c5cf42
1 /*
2 * Symmetric key cipher operations.
4 * Generic encrypt/decrypt wrapper for ciphers, handles operations across
5 * multiple page boundaries by using temporary blocks. In user context,
6 * the kernel is given a chance to schedule us once per page.
8 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
17 #include <crypto/internal/aead.h>
18 #include <crypto/internal/skcipher.h>
19 #include <crypto/scatterwalk.h>
20 #include <linux/bug.h>
21 #include <linux/cryptouser.h>
22 #include <linux/compiler.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/rtnetlink.h>
26 #include <linux/seq_file.h>
27 #include <net/netlink.h>
29 #include "internal.h"
31 enum {
32 SKCIPHER_WALK_PHYS = 1 << 0,
33 SKCIPHER_WALK_SLOW = 1 << 1,
34 SKCIPHER_WALK_COPY = 1 << 2,
35 SKCIPHER_WALK_DIFF = 1 << 3,
36 SKCIPHER_WALK_SLEEP = 1 << 4,
39 struct skcipher_walk_buffer {
40 struct list_head entry;
41 struct scatter_walk dst;
42 unsigned int len;
43 u8 *data;
44 u8 buffer[];
47 static int skcipher_walk_next(struct skcipher_walk *walk);
49 static inline void skcipher_unmap(struct scatter_walk *walk, void *vaddr)
51 if (PageHighMem(scatterwalk_page(walk)))
52 kunmap_atomic(vaddr);
55 static inline void *skcipher_map(struct scatter_walk *walk)
57 struct page *page = scatterwalk_page(walk);
59 return (PageHighMem(page) ? kmap_atomic(page) : page_address(page)) +
60 offset_in_page(walk->offset);
63 static inline void skcipher_map_src(struct skcipher_walk *walk)
65 walk->src.virt.addr = skcipher_map(&walk->in);
68 static inline void skcipher_map_dst(struct skcipher_walk *walk)
70 walk->dst.virt.addr = skcipher_map(&walk->out);
73 static inline void skcipher_unmap_src(struct skcipher_walk *walk)
75 skcipher_unmap(&walk->in, walk->src.virt.addr);
78 static inline void skcipher_unmap_dst(struct skcipher_walk *walk)
80 skcipher_unmap(&walk->out, walk->dst.virt.addr);
83 static inline gfp_t skcipher_walk_gfp(struct skcipher_walk *walk)
85 return walk->flags & SKCIPHER_WALK_SLEEP ? GFP_KERNEL : GFP_ATOMIC;
88 /* Get a spot of the specified length that does not straddle a page.
89 * The caller needs to ensure that there is enough space for this operation.
91 static inline u8 *skcipher_get_spot(u8 *start, unsigned int len)
93 u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
95 return max(start, end_page);
98 static int skcipher_done_slow(struct skcipher_walk *walk, unsigned int bsize)
100 u8 *addr;
102 addr = (u8 *)ALIGN((unsigned long)walk->buffer, walk->alignmask + 1);
103 addr = skcipher_get_spot(addr, bsize);
104 scatterwalk_copychunks(addr, &walk->out, bsize,
105 (walk->flags & SKCIPHER_WALK_PHYS) ? 2 : 1);
106 return 0;
109 int skcipher_walk_done(struct skcipher_walk *walk, int err)
111 unsigned int n = walk->nbytes;
112 unsigned int nbytes = 0;
114 if (!n)
115 goto finish;
117 if (likely(err >= 0)) {
118 n -= err;
119 nbytes = walk->total - n;
122 if (likely(!(walk->flags & (SKCIPHER_WALK_PHYS |
123 SKCIPHER_WALK_SLOW |
124 SKCIPHER_WALK_COPY |
125 SKCIPHER_WALK_DIFF)))) {
126 unmap_src:
127 skcipher_unmap_src(walk);
128 } else if (walk->flags & SKCIPHER_WALK_DIFF) {
129 skcipher_unmap_dst(walk);
130 goto unmap_src;
131 } else if (walk->flags & SKCIPHER_WALK_COPY) {
132 skcipher_map_dst(walk);
133 memcpy(walk->dst.virt.addr, walk->page, n);
134 skcipher_unmap_dst(walk);
135 } else if (unlikely(walk->flags & SKCIPHER_WALK_SLOW)) {
136 if (err > 0) {
138 * Didn't process all bytes. Either the algorithm is
139 * broken, or this was the last step and it turned out
140 * the message wasn't evenly divisible into blocks but
141 * the algorithm requires it.
143 err = -EINVAL;
144 nbytes = 0;
145 } else
146 n = skcipher_done_slow(walk, n);
149 if (err > 0)
150 err = 0;
152 walk->total = nbytes;
153 walk->nbytes = 0;
155 scatterwalk_advance(&walk->in, n);
156 scatterwalk_advance(&walk->out, n);
157 scatterwalk_done(&walk->in, 0, nbytes);
158 scatterwalk_done(&walk->out, 1, nbytes);
160 if (nbytes) {
161 crypto_yield(walk->flags & SKCIPHER_WALK_SLEEP ?
162 CRYPTO_TFM_REQ_MAY_SLEEP : 0);
163 return skcipher_walk_next(walk);
166 finish:
167 /* Short-circuit for the common/fast path. */
168 if (!((unsigned long)walk->buffer | (unsigned long)walk->page))
169 goto out;
171 if (walk->flags & SKCIPHER_WALK_PHYS)
172 goto out;
174 if (walk->iv != walk->oiv)
175 memcpy(walk->oiv, walk->iv, walk->ivsize);
176 if (walk->buffer != walk->page)
177 kfree(walk->buffer);
178 if (walk->page)
179 free_page((unsigned long)walk->page);
181 out:
182 return err;
184 EXPORT_SYMBOL_GPL(skcipher_walk_done);
186 void skcipher_walk_complete(struct skcipher_walk *walk, int err)
188 struct skcipher_walk_buffer *p, *tmp;
190 list_for_each_entry_safe(p, tmp, &walk->buffers, entry) {
191 u8 *data;
193 if (err)
194 goto done;
196 data = p->data;
197 if (!data) {
198 data = PTR_ALIGN(&p->buffer[0], walk->alignmask + 1);
199 data = skcipher_get_spot(data, walk->stride);
202 scatterwalk_copychunks(data, &p->dst, p->len, 1);
204 if (offset_in_page(p->data) + p->len + walk->stride >
205 PAGE_SIZE)
206 free_page((unsigned long)p->data);
208 done:
209 list_del(&p->entry);
210 kfree(p);
213 if (!err && walk->iv != walk->oiv)
214 memcpy(walk->oiv, walk->iv, walk->ivsize);
215 if (walk->buffer != walk->page)
216 kfree(walk->buffer);
217 if (walk->page)
218 free_page((unsigned long)walk->page);
220 EXPORT_SYMBOL_GPL(skcipher_walk_complete);
222 static void skcipher_queue_write(struct skcipher_walk *walk,
223 struct skcipher_walk_buffer *p)
225 p->dst = walk->out;
226 list_add_tail(&p->entry, &walk->buffers);
229 static int skcipher_next_slow(struct skcipher_walk *walk, unsigned int bsize)
231 bool phys = walk->flags & SKCIPHER_WALK_PHYS;
232 unsigned alignmask = walk->alignmask;
233 struct skcipher_walk_buffer *p;
234 unsigned a;
235 unsigned n;
236 u8 *buffer;
237 void *v;
239 if (!phys) {
240 if (!walk->buffer)
241 walk->buffer = walk->page;
242 buffer = walk->buffer;
243 if (buffer)
244 goto ok;
247 /* Start with the minimum alignment of kmalloc. */
248 a = crypto_tfm_ctx_alignment() - 1;
249 n = bsize;
251 if (phys) {
252 /* Calculate the minimum alignment of p->buffer. */
253 a &= (sizeof(*p) ^ (sizeof(*p) - 1)) >> 1;
254 n += sizeof(*p);
257 /* Minimum size to align p->buffer by alignmask. */
258 n += alignmask & ~a;
260 /* Minimum size to ensure p->buffer does not straddle a page. */
261 n += (bsize - 1) & ~(alignmask | a);
263 v = kzalloc(n, skcipher_walk_gfp(walk));
264 if (!v)
265 return skcipher_walk_done(walk, -ENOMEM);
267 if (phys) {
268 p = v;
269 p->len = bsize;
270 skcipher_queue_write(walk, p);
271 buffer = p->buffer;
272 } else {
273 walk->buffer = v;
274 buffer = v;
278 walk->dst.virt.addr = PTR_ALIGN(buffer, alignmask + 1);
279 walk->dst.virt.addr = skcipher_get_spot(walk->dst.virt.addr, bsize);
280 walk->src.virt.addr = walk->dst.virt.addr;
282 scatterwalk_copychunks(walk->src.virt.addr, &walk->in, bsize, 0);
284 walk->nbytes = bsize;
285 walk->flags |= SKCIPHER_WALK_SLOW;
287 return 0;
290 static int skcipher_next_copy(struct skcipher_walk *walk)
292 struct skcipher_walk_buffer *p;
293 u8 *tmp = walk->page;
295 skcipher_map_src(walk);
296 memcpy(tmp, walk->src.virt.addr, walk->nbytes);
297 skcipher_unmap_src(walk);
299 walk->src.virt.addr = tmp;
300 walk->dst.virt.addr = tmp;
302 if (!(walk->flags & SKCIPHER_WALK_PHYS))
303 return 0;
305 p = kmalloc(sizeof(*p), skcipher_walk_gfp(walk));
306 if (!p)
307 return -ENOMEM;
309 p->data = walk->page;
310 p->len = walk->nbytes;
311 skcipher_queue_write(walk, p);
313 if (offset_in_page(walk->page) + walk->nbytes + walk->stride >
314 PAGE_SIZE)
315 walk->page = NULL;
316 else
317 walk->page += walk->nbytes;
319 return 0;
322 static int skcipher_next_fast(struct skcipher_walk *walk)
324 unsigned long diff;
326 walk->src.phys.page = scatterwalk_page(&walk->in);
327 walk->src.phys.offset = offset_in_page(walk->in.offset);
328 walk->dst.phys.page = scatterwalk_page(&walk->out);
329 walk->dst.phys.offset = offset_in_page(walk->out.offset);
331 if (walk->flags & SKCIPHER_WALK_PHYS)
332 return 0;
334 diff = walk->src.phys.offset - walk->dst.phys.offset;
335 diff |= walk->src.virt.page - walk->dst.virt.page;
337 skcipher_map_src(walk);
338 walk->dst.virt.addr = walk->src.virt.addr;
340 if (diff) {
341 walk->flags |= SKCIPHER_WALK_DIFF;
342 skcipher_map_dst(walk);
345 return 0;
348 static int skcipher_walk_next(struct skcipher_walk *walk)
350 unsigned int bsize;
351 unsigned int n;
352 int err;
354 walk->flags &= ~(SKCIPHER_WALK_SLOW | SKCIPHER_WALK_COPY |
355 SKCIPHER_WALK_DIFF);
357 n = walk->total;
358 bsize = min(walk->stride, max(n, walk->blocksize));
359 n = scatterwalk_clamp(&walk->in, n);
360 n = scatterwalk_clamp(&walk->out, n);
362 if (unlikely(n < bsize)) {
363 if (unlikely(walk->total < walk->blocksize))
364 return skcipher_walk_done(walk, -EINVAL);
366 slow_path:
367 err = skcipher_next_slow(walk, bsize);
368 goto set_phys_lowmem;
371 if (unlikely((walk->in.offset | walk->out.offset) & walk->alignmask)) {
372 if (!walk->page) {
373 gfp_t gfp = skcipher_walk_gfp(walk);
375 walk->page = (void *)__get_free_page(gfp);
376 if (!walk->page)
377 goto slow_path;
380 walk->nbytes = min_t(unsigned, n,
381 PAGE_SIZE - offset_in_page(walk->page));
382 walk->flags |= SKCIPHER_WALK_COPY;
383 err = skcipher_next_copy(walk);
384 goto set_phys_lowmem;
387 walk->nbytes = n;
389 return skcipher_next_fast(walk);
391 set_phys_lowmem:
392 if (!err && (walk->flags & SKCIPHER_WALK_PHYS)) {
393 walk->src.phys.page = virt_to_page(walk->src.virt.addr);
394 walk->dst.phys.page = virt_to_page(walk->dst.virt.addr);
395 walk->src.phys.offset &= PAGE_SIZE - 1;
396 walk->dst.phys.offset &= PAGE_SIZE - 1;
398 return err;
400 EXPORT_SYMBOL_GPL(skcipher_walk_next);
402 static int skcipher_copy_iv(struct skcipher_walk *walk)
404 unsigned a = crypto_tfm_ctx_alignment() - 1;
405 unsigned alignmask = walk->alignmask;
406 unsigned ivsize = walk->ivsize;
407 unsigned bs = walk->stride;
408 unsigned aligned_bs;
409 unsigned size;
410 u8 *iv;
412 aligned_bs = ALIGN(bs, alignmask + 1);
414 /* Minimum size to align buffer by alignmask. */
415 size = alignmask & ~a;
417 if (walk->flags & SKCIPHER_WALK_PHYS)
418 size += ivsize;
419 else {
420 size += aligned_bs + ivsize;
422 /* Minimum size to ensure buffer does not straddle a page. */
423 size += (bs - 1) & ~(alignmask | a);
426 walk->buffer = kmalloc(size, skcipher_walk_gfp(walk));
427 if (!walk->buffer)
428 return -ENOMEM;
430 iv = PTR_ALIGN(walk->buffer, alignmask + 1);
431 iv = skcipher_get_spot(iv, bs) + aligned_bs;
433 walk->iv = memcpy(iv, walk->iv, walk->ivsize);
434 return 0;
437 static int skcipher_walk_first(struct skcipher_walk *walk)
439 if (WARN_ON_ONCE(in_irq()))
440 return -EDEADLK;
442 walk->buffer = NULL;
443 if (unlikely(((unsigned long)walk->iv & walk->alignmask))) {
444 int err = skcipher_copy_iv(walk);
445 if (err)
446 return err;
449 walk->page = NULL;
450 walk->nbytes = walk->total;
452 return skcipher_walk_next(walk);
455 static int skcipher_walk_skcipher(struct skcipher_walk *walk,
456 struct skcipher_request *req)
458 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
460 walk->total = req->cryptlen;
461 walk->nbytes = 0;
462 walk->iv = req->iv;
463 walk->oiv = req->iv;
465 if (unlikely(!walk->total))
466 return 0;
468 scatterwalk_start(&walk->in, req->src);
469 scatterwalk_start(&walk->out, req->dst);
471 walk->flags &= ~SKCIPHER_WALK_SLEEP;
472 walk->flags |= req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
473 SKCIPHER_WALK_SLEEP : 0;
475 walk->blocksize = crypto_skcipher_blocksize(tfm);
476 walk->stride = crypto_skcipher_walksize(tfm);
477 walk->ivsize = crypto_skcipher_ivsize(tfm);
478 walk->alignmask = crypto_skcipher_alignmask(tfm);
480 return skcipher_walk_first(walk);
483 int skcipher_walk_virt(struct skcipher_walk *walk,
484 struct skcipher_request *req, bool atomic)
486 int err;
488 walk->flags &= ~SKCIPHER_WALK_PHYS;
490 err = skcipher_walk_skcipher(walk, req);
492 walk->flags &= atomic ? ~SKCIPHER_WALK_SLEEP : ~0;
494 return err;
496 EXPORT_SYMBOL_GPL(skcipher_walk_virt);
498 void skcipher_walk_atomise(struct skcipher_walk *walk)
500 walk->flags &= ~SKCIPHER_WALK_SLEEP;
502 EXPORT_SYMBOL_GPL(skcipher_walk_atomise);
504 int skcipher_walk_async(struct skcipher_walk *walk,
505 struct skcipher_request *req)
507 walk->flags |= SKCIPHER_WALK_PHYS;
509 INIT_LIST_HEAD(&walk->buffers);
511 return skcipher_walk_skcipher(walk, req);
513 EXPORT_SYMBOL_GPL(skcipher_walk_async);
515 static int skcipher_walk_aead_common(struct skcipher_walk *walk,
516 struct aead_request *req, bool atomic)
518 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
519 int err;
521 walk->nbytes = 0;
522 walk->iv = req->iv;
523 walk->oiv = req->iv;
525 if (unlikely(!walk->total))
526 return 0;
528 walk->flags &= ~SKCIPHER_WALK_PHYS;
530 scatterwalk_start(&walk->in, req->src);
531 scatterwalk_start(&walk->out, req->dst);
533 scatterwalk_copychunks(NULL, &walk->in, req->assoclen, 2);
534 scatterwalk_copychunks(NULL, &walk->out, req->assoclen, 2);
536 scatterwalk_done(&walk->in, 0, walk->total);
537 scatterwalk_done(&walk->out, 0, walk->total);
539 if (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP)
540 walk->flags |= SKCIPHER_WALK_SLEEP;
541 else
542 walk->flags &= ~SKCIPHER_WALK_SLEEP;
544 walk->blocksize = crypto_aead_blocksize(tfm);
545 walk->stride = crypto_aead_chunksize(tfm);
546 walk->ivsize = crypto_aead_ivsize(tfm);
547 walk->alignmask = crypto_aead_alignmask(tfm);
549 err = skcipher_walk_first(walk);
551 if (atomic)
552 walk->flags &= ~SKCIPHER_WALK_SLEEP;
554 return err;
557 int skcipher_walk_aead(struct skcipher_walk *walk, struct aead_request *req,
558 bool atomic)
560 walk->total = req->cryptlen;
562 return skcipher_walk_aead_common(walk, req, atomic);
564 EXPORT_SYMBOL_GPL(skcipher_walk_aead);
566 int skcipher_walk_aead_encrypt(struct skcipher_walk *walk,
567 struct aead_request *req, bool atomic)
569 walk->total = req->cryptlen;
571 return skcipher_walk_aead_common(walk, req, atomic);
573 EXPORT_SYMBOL_GPL(skcipher_walk_aead_encrypt);
575 int skcipher_walk_aead_decrypt(struct skcipher_walk *walk,
576 struct aead_request *req, bool atomic)
578 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
580 walk->total = req->cryptlen - crypto_aead_authsize(tfm);
582 return skcipher_walk_aead_common(walk, req, atomic);
584 EXPORT_SYMBOL_GPL(skcipher_walk_aead_decrypt);
586 static unsigned int crypto_skcipher_extsize(struct crypto_alg *alg)
588 if (alg->cra_type == &crypto_blkcipher_type)
589 return sizeof(struct crypto_blkcipher *);
591 if (alg->cra_type == &crypto_ablkcipher_type ||
592 alg->cra_type == &crypto_givcipher_type)
593 return sizeof(struct crypto_ablkcipher *);
595 return crypto_alg_extsize(alg);
598 static int skcipher_setkey_blkcipher(struct crypto_skcipher *tfm,
599 const u8 *key, unsigned int keylen)
601 struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
602 struct crypto_blkcipher *blkcipher = *ctx;
603 int err;
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);
612 return err;
615 static int skcipher_crypt_blkcipher(struct skcipher_request *req,
616 int (*crypt)(struct blkcipher_desc *,
617 struct scatterlist *,
618 struct scatterlist *,
619 unsigned int))
621 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
622 struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
623 struct blkcipher_desc desc = {
624 .tfm = *ctx,
625 .info = req->iv,
626 .flags = req->base.flags,
630 return crypt(&desc, req->dst, req->src, req->cryptlen);
633 static int skcipher_encrypt_blkcipher(struct skcipher_request *req)
635 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
636 struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
637 struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
639 return skcipher_crypt_blkcipher(req, alg->encrypt);
642 static int skcipher_decrypt_blkcipher(struct skcipher_request *req)
644 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
645 struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
646 struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
648 return skcipher_crypt_blkcipher(req, alg->decrypt);
651 static void crypto_exit_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
653 struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
655 crypto_free_blkcipher(*ctx);
658 static int crypto_init_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
660 struct crypto_alg *calg = tfm->__crt_alg;
661 struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
662 struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
663 struct crypto_blkcipher *blkcipher;
664 struct crypto_tfm *btfm;
666 if (!crypto_mod_get(calg))
667 return -EAGAIN;
669 btfm = __crypto_alloc_tfm(calg, CRYPTO_ALG_TYPE_BLKCIPHER,
670 CRYPTO_ALG_TYPE_MASK);
671 if (IS_ERR(btfm)) {
672 crypto_mod_put(calg);
673 return PTR_ERR(btfm);
676 blkcipher = __crypto_blkcipher_cast(btfm);
677 *ctx = blkcipher;
678 tfm->exit = crypto_exit_skcipher_ops_blkcipher;
680 skcipher->setkey = skcipher_setkey_blkcipher;
681 skcipher->encrypt = skcipher_encrypt_blkcipher;
682 skcipher->decrypt = skcipher_decrypt_blkcipher;
684 skcipher->ivsize = crypto_blkcipher_ivsize(blkcipher);
685 skcipher->keysize = calg->cra_blkcipher.max_keysize;
687 return 0;
690 static int skcipher_setkey_ablkcipher(struct crypto_skcipher *tfm,
691 const u8 *key, unsigned int keylen)
693 struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
694 struct crypto_ablkcipher *ablkcipher = *ctx;
695 int err;
697 crypto_ablkcipher_clear_flags(ablkcipher, ~0);
698 crypto_ablkcipher_set_flags(ablkcipher,
699 crypto_skcipher_get_flags(tfm) &
700 CRYPTO_TFM_REQ_MASK);
701 err = crypto_ablkcipher_setkey(ablkcipher, key, keylen);
702 crypto_skcipher_set_flags(tfm,
703 crypto_ablkcipher_get_flags(ablkcipher) &
704 CRYPTO_TFM_RES_MASK);
706 return err;
709 static int skcipher_crypt_ablkcipher(struct skcipher_request *req,
710 int (*crypt)(struct ablkcipher_request *))
712 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
713 struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
714 struct ablkcipher_request *subreq = skcipher_request_ctx(req);
716 ablkcipher_request_set_tfm(subreq, *ctx);
717 ablkcipher_request_set_callback(subreq, skcipher_request_flags(req),
718 req->base.complete, req->base.data);
719 ablkcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
720 req->iv);
722 return crypt(subreq);
725 static int skcipher_encrypt_ablkcipher(struct skcipher_request *req)
727 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
728 struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
729 struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
731 return skcipher_crypt_ablkcipher(req, alg->encrypt);
734 static int skcipher_decrypt_ablkcipher(struct skcipher_request *req)
736 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
737 struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
738 struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
740 return skcipher_crypt_ablkcipher(req, alg->decrypt);
743 static void crypto_exit_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
745 struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
747 crypto_free_ablkcipher(*ctx);
750 static int crypto_init_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
752 struct crypto_alg *calg = tfm->__crt_alg;
753 struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
754 struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
755 struct crypto_ablkcipher *ablkcipher;
756 struct crypto_tfm *abtfm;
758 if (!crypto_mod_get(calg))
759 return -EAGAIN;
761 abtfm = __crypto_alloc_tfm(calg, 0, 0);
762 if (IS_ERR(abtfm)) {
763 crypto_mod_put(calg);
764 return PTR_ERR(abtfm);
767 ablkcipher = __crypto_ablkcipher_cast(abtfm);
768 *ctx = ablkcipher;
769 tfm->exit = crypto_exit_skcipher_ops_ablkcipher;
771 skcipher->setkey = skcipher_setkey_ablkcipher;
772 skcipher->encrypt = skcipher_encrypt_ablkcipher;
773 skcipher->decrypt = skcipher_decrypt_ablkcipher;
775 skcipher->ivsize = crypto_ablkcipher_ivsize(ablkcipher);
776 skcipher->reqsize = crypto_ablkcipher_reqsize(ablkcipher) +
777 sizeof(struct ablkcipher_request);
778 skcipher->keysize = calg->cra_ablkcipher.max_keysize;
780 return 0;
783 static int skcipher_setkey_unaligned(struct crypto_skcipher *tfm,
784 const u8 *key, unsigned int keylen)
786 unsigned long alignmask = crypto_skcipher_alignmask(tfm);
787 struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
788 u8 *buffer, *alignbuffer;
789 unsigned long absize;
790 int ret;
792 absize = keylen + alignmask;
793 buffer = kmalloc(absize, GFP_ATOMIC);
794 if (!buffer)
795 return -ENOMEM;
797 alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
798 memcpy(alignbuffer, key, keylen);
799 ret = cipher->setkey(tfm, alignbuffer, keylen);
800 kzfree(buffer);
801 return ret;
804 static int skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
805 unsigned int keylen)
807 struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
808 unsigned long alignmask = crypto_skcipher_alignmask(tfm);
810 if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
811 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
812 return -EINVAL;
815 if ((unsigned long)key & alignmask)
816 return skcipher_setkey_unaligned(tfm, key, keylen);
818 return cipher->setkey(tfm, key, keylen);
821 static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
823 struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
824 struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
826 alg->exit(skcipher);
829 static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
831 struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
832 struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
834 if (tfm->__crt_alg->cra_type == &crypto_blkcipher_type)
835 return crypto_init_skcipher_ops_blkcipher(tfm);
837 if (tfm->__crt_alg->cra_type == &crypto_ablkcipher_type ||
838 tfm->__crt_alg->cra_type == &crypto_givcipher_type)
839 return crypto_init_skcipher_ops_ablkcipher(tfm);
841 skcipher->setkey = skcipher_setkey;
842 skcipher->encrypt = alg->encrypt;
843 skcipher->decrypt = alg->decrypt;
844 skcipher->ivsize = alg->ivsize;
845 skcipher->keysize = alg->max_keysize;
847 if (alg->exit)
848 skcipher->base.exit = crypto_skcipher_exit_tfm;
850 if (alg->init)
851 return alg->init(skcipher);
853 return 0;
856 static void crypto_skcipher_free_instance(struct crypto_instance *inst)
858 struct skcipher_instance *skcipher =
859 container_of(inst, struct skcipher_instance, s.base);
861 skcipher->free(skcipher);
864 static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
865 __maybe_unused;
866 static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
868 struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
869 base);
871 seq_printf(m, "type : skcipher\n");
872 seq_printf(m, "async : %s\n",
873 alg->cra_flags & CRYPTO_ALG_ASYNC ? "yes" : "no");
874 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
875 seq_printf(m, "min keysize : %u\n", skcipher->min_keysize);
876 seq_printf(m, "max keysize : %u\n", skcipher->max_keysize);
877 seq_printf(m, "ivsize : %u\n", skcipher->ivsize);
878 seq_printf(m, "chunksize : %u\n", skcipher->chunksize);
879 seq_printf(m, "walksize : %u\n", skcipher->walksize);
882 #ifdef CONFIG_NET
883 static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
885 struct crypto_report_blkcipher rblkcipher;
886 struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
887 base);
889 strncpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type));
890 strncpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
892 rblkcipher.blocksize = alg->cra_blocksize;
893 rblkcipher.min_keysize = skcipher->min_keysize;
894 rblkcipher.max_keysize = skcipher->max_keysize;
895 rblkcipher.ivsize = skcipher->ivsize;
897 if (nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
898 sizeof(struct crypto_report_blkcipher), &rblkcipher))
899 goto nla_put_failure;
900 return 0;
902 nla_put_failure:
903 return -EMSGSIZE;
905 #else
906 static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
908 return -ENOSYS;
910 #endif
912 static const struct crypto_type crypto_skcipher_type2 = {
913 .extsize = crypto_skcipher_extsize,
914 .init_tfm = crypto_skcipher_init_tfm,
915 .free = crypto_skcipher_free_instance,
916 #ifdef CONFIG_PROC_FS
917 .show = crypto_skcipher_show,
918 #endif
919 .report = crypto_skcipher_report,
920 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
921 .maskset = CRYPTO_ALG_TYPE_BLKCIPHER_MASK,
922 .type = CRYPTO_ALG_TYPE_SKCIPHER,
923 .tfmsize = offsetof(struct crypto_skcipher, base),
926 int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
927 const char *name, u32 type, u32 mask)
929 spawn->base.frontend = &crypto_skcipher_type2;
930 return crypto_grab_spawn(&spawn->base, name, type, mask);
932 EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
934 struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
935 u32 type, u32 mask)
937 return crypto_alloc_tfm(alg_name, &crypto_skcipher_type2, type, mask);
939 EXPORT_SYMBOL_GPL(crypto_alloc_skcipher);
941 int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask)
943 return crypto_type_has_alg(alg_name, &crypto_skcipher_type2,
944 type, mask);
946 EXPORT_SYMBOL_GPL(crypto_has_skcipher2);
948 static int skcipher_prepare_alg(struct skcipher_alg *alg)
950 struct crypto_alg *base = &alg->base;
952 if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8 ||
953 alg->walksize > PAGE_SIZE / 8)
954 return -EINVAL;
956 if (!alg->chunksize)
957 alg->chunksize = base->cra_blocksize;
958 if (!alg->walksize)
959 alg->walksize = alg->chunksize;
961 base->cra_type = &crypto_skcipher_type2;
962 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
963 base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER;
965 return 0;
968 int crypto_register_skcipher(struct skcipher_alg *alg)
970 struct crypto_alg *base = &alg->base;
971 int err;
973 err = skcipher_prepare_alg(alg);
974 if (err)
975 return err;
977 return crypto_register_alg(base);
979 EXPORT_SYMBOL_GPL(crypto_register_skcipher);
981 void crypto_unregister_skcipher(struct skcipher_alg *alg)
983 crypto_unregister_alg(&alg->base);
985 EXPORT_SYMBOL_GPL(crypto_unregister_skcipher);
987 int crypto_register_skciphers(struct skcipher_alg *algs, int count)
989 int i, ret;
991 for (i = 0; i < count; i++) {
992 ret = crypto_register_skcipher(&algs[i]);
993 if (ret)
994 goto err;
997 return 0;
999 err:
1000 for (--i; i >= 0; --i)
1001 crypto_unregister_skcipher(&algs[i]);
1003 return ret;
1005 EXPORT_SYMBOL_GPL(crypto_register_skciphers);
1007 void crypto_unregister_skciphers(struct skcipher_alg *algs, int count)
1009 int i;
1011 for (i = count - 1; i >= 0; --i)
1012 crypto_unregister_skcipher(&algs[i]);
1014 EXPORT_SYMBOL_GPL(crypto_unregister_skciphers);
1016 int skcipher_register_instance(struct crypto_template *tmpl,
1017 struct skcipher_instance *inst)
1019 int err;
1021 err = skcipher_prepare_alg(&inst->alg);
1022 if (err)
1023 return err;
1025 return crypto_register_instance(tmpl, skcipher_crypto_instance(inst));
1027 EXPORT_SYMBOL_GPL(skcipher_register_instance);
1029 MODULE_LICENSE("GPL");
1030 MODULE_DESCRIPTION("Symmetric key cipher type");