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)
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/list.h>
23 #include <linux/module.h>
24 #include <linux/rtnetlink.h>
25 #include <linux/seq_file.h>
26 #include <net/netlink.h>
31 SKCIPHER_WALK_PHYS
= 1 << 0,
32 SKCIPHER_WALK_SLOW
= 1 << 1,
33 SKCIPHER_WALK_COPY
= 1 << 2,
34 SKCIPHER_WALK_DIFF
= 1 << 3,
35 SKCIPHER_WALK_SLEEP
= 1 << 4,
38 struct skcipher_walk_buffer
{
39 struct list_head entry
;
40 struct scatter_walk dst
;
46 static int skcipher_walk_next(struct skcipher_walk
*walk
);
48 static inline void skcipher_unmap(struct scatter_walk
*walk
, void *vaddr
)
50 if (PageHighMem(scatterwalk_page(walk
)))
54 static inline void *skcipher_map(struct scatter_walk
*walk
)
56 struct page
*page
= scatterwalk_page(walk
);
58 return (PageHighMem(page
) ? kmap_atomic(page
) : page_address(page
)) +
59 offset_in_page(walk
->offset
);
62 static inline void skcipher_map_src(struct skcipher_walk
*walk
)
64 walk
->src
.virt
.addr
= skcipher_map(&walk
->in
);
67 static inline void skcipher_map_dst(struct skcipher_walk
*walk
)
69 walk
->dst
.virt
.addr
= skcipher_map(&walk
->out
);
72 static inline void skcipher_unmap_src(struct skcipher_walk
*walk
)
74 skcipher_unmap(&walk
->in
, walk
->src
.virt
.addr
);
77 static inline void skcipher_unmap_dst(struct skcipher_walk
*walk
)
79 skcipher_unmap(&walk
->out
, walk
->dst
.virt
.addr
);
82 static inline gfp_t
skcipher_walk_gfp(struct skcipher_walk
*walk
)
84 return walk
->flags
& SKCIPHER_WALK_SLEEP
? GFP_KERNEL
: GFP_ATOMIC
;
87 /* Get a spot of the specified length that does not straddle a page.
88 * The caller needs to ensure that there is enough space for this operation.
90 static inline u8
*skcipher_get_spot(u8
*start
, unsigned int len
)
92 u8
*end_page
= (u8
*)(((unsigned long)(start
+ len
- 1)) & PAGE_MASK
);
94 return max(start
, end_page
);
97 static int skcipher_done_slow(struct skcipher_walk
*walk
, unsigned int bsize
)
101 addr
= (u8
*)ALIGN((unsigned long)walk
->buffer
, walk
->alignmask
+ 1);
102 addr
= skcipher_get_spot(addr
, bsize
);
103 scatterwalk_copychunks(addr
, &walk
->out
, bsize
,
104 (walk
->flags
& SKCIPHER_WALK_PHYS
) ? 2 : 1);
108 int skcipher_walk_done(struct skcipher_walk
*walk
, int err
)
110 unsigned int n
= walk
->nbytes
- err
;
113 nbytes
= walk
->total
- n
;
115 if (unlikely(err
< 0)) {
118 } else if (likely(!(walk
->flags
& (SKCIPHER_WALK_PHYS
|
121 SKCIPHER_WALK_DIFF
)))) {
123 skcipher_unmap_src(walk
);
124 } else if (walk
->flags
& SKCIPHER_WALK_DIFF
) {
125 skcipher_unmap_dst(walk
);
127 } else if (walk
->flags
& SKCIPHER_WALK_COPY
) {
128 skcipher_map_dst(walk
);
129 memcpy(walk
->dst
.virt
.addr
, walk
->page
, n
);
130 skcipher_unmap_dst(walk
);
131 } else if (unlikely(walk
->flags
& SKCIPHER_WALK_SLOW
)) {
136 n
= skcipher_done_slow(walk
, n
);
142 walk
->total
= nbytes
;
143 walk
->nbytes
= nbytes
;
145 scatterwalk_advance(&walk
->in
, n
);
146 scatterwalk_advance(&walk
->out
, n
);
147 scatterwalk_done(&walk
->in
, 0, nbytes
);
148 scatterwalk_done(&walk
->out
, 1, nbytes
);
151 crypto_yield(walk
->flags
& SKCIPHER_WALK_SLEEP
?
152 CRYPTO_TFM_REQ_MAY_SLEEP
: 0);
153 return skcipher_walk_next(walk
);
156 /* Short-circuit for the common/fast path. */
157 if (!((unsigned long)walk
->buffer
| (unsigned long)walk
->page
))
160 if (walk
->flags
& SKCIPHER_WALK_PHYS
)
163 if (walk
->iv
!= walk
->oiv
)
164 memcpy(walk
->oiv
, walk
->iv
, walk
->ivsize
);
165 if (walk
->buffer
!= walk
->page
)
168 free_page((unsigned long)walk
->page
);
173 EXPORT_SYMBOL_GPL(skcipher_walk_done
);
175 void skcipher_walk_complete(struct skcipher_walk
*walk
, int err
)
177 struct skcipher_walk_buffer
*p
, *tmp
;
179 list_for_each_entry_safe(p
, tmp
, &walk
->buffers
, entry
) {
187 data
= PTR_ALIGN(&p
->buffer
[0], walk
->alignmask
+ 1);
188 data
= skcipher_get_spot(data
, walk
->chunksize
);
191 scatterwalk_copychunks(data
, &p
->dst
, p
->len
, 1);
193 if (offset_in_page(p
->data
) + p
->len
+ walk
->chunksize
>
195 free_page((unsigned long)p
->data
);
202 if (!err
&& walk
->iv
!= walk
->oiv
)
203 memcpy(walk
->oiv
, walk
->iv
, walk
->ivsize
);
204 if (walk
->buffer
!= walk
->page
)
207 free_page((unsigned long)walk
->page
);
209 EXPORT_SYMBOL_GPL(skcipher_walk_complete
);
211 static void skcipher_queue_write(struct skcipher_walk
*walk
,
212 struct skcipher_walk_buffer
*p
)
215 list_add_tail(&p
->entry
, &walk
->buffers
);
218 static int skcipher_next_slow(struct skcipher_walk
*walk
, unsigned int bsize
)
220 bool phys
= walk
->flags
& SKCIPHER_WALK_PHYS
;
221 unsigned alignmask
= walk
->alignmask
;
222 struct skcipher_walk_buffer
*p
;
229 buffer
= walk
->buffer
?: walk
->page
;
234 /* Start with the minimum alignment of kmalloc. */
235 a
= crypto_tfm_ctx_alignment() - 1;
239 /* Calculate the minimum alignment of p->buffer. */
240 a
&= (sizeof(*p
) ^ (sizeof(*p
) - 1)) >> 1;
244 /* Minimum size to align p->buffer by alignmask. */
247 /* Minimum size to ensure p->buffer does not straddle a page. */
248 n
+= (bsize
- 1) & ~(alignmask
| a
);
250 v
= kzalloc(n
, skcipher_walk_gfp(walk
));
252 return skcipher_walk_done(walk
, -ENOMEM
);
257 skcipher_queue_write(walk
, p
);
265 walk
->dst
.virt
.addr
= PTR_ALIGN(buffer
, alignmask
+ 1);
266 walk
->dst
.virt
.addr
= skcipher_get_spot(walk
->dst
.virt
.addr
, bsize
);
267 walk
->src
.virt
.addr
= walk
->dst
.virt
.addr
;
269 scatterwalk_copychunks(walk
->src
.virt
.addr
, &walk
->in
, bsize
, 0);
271 walk
->nbytes
= bsize
;
272 walk
->flags
|= SKCIPHER_WALK_SLOW
;
277 static int skcipher_next_copy(struct skcipher_walk
*walk
)
279 struct skcipher_walk_buffer
*p
;
280 u8
*tmp
= walk
->page
;
282 skcipher_map_src(walk
);
283 memcpy(tmp
, walk
->src
.virt
.addr
, walk
->nbytes
);
284 skcipher_unmap_src(walk
);
286 walk
->src
.virt
.addr
= tmp
;
287 walk
->dst
.virt
.addr
= tmp
;
289 if (!(walk
->flags
& SKCIPHER_WALK_PHYS
))
292 p
= kmalloc(sizeof(*p
), skcipher_walk_gfp(walk
));
296 p
->data
= walk
->page
;
297 p
->len
= walk
->nbytes
;
298 skcipher_queue_write(walk
, p
);
300 if (offset_in_page(walk
->page
) + walk
->nbytes
+ walk
->chunksize
>
304 walk
->page
+= walk
->nbytes
;
309 static int skcipher_next_fast(struct skcipher_walk
*walk
)
313 walk
->src
.phys
.page
= scatterwalk_page(&walk
->in
);
314 walk
->src
.phys
.offset
= offset_in_page(walk
->in
.offset
);
315 walk
->dst
.phys
.page
= scatterwalk_page(&walk
->out
);
316 walk
->dst
.phys
.offset
= offset_in_page(walk
->out
.offset
);
318 if (walk
->flags
& SKCIPHER_WALK_PHYS
)
321 diff
= walk
->src
.phys
.offset
- walk
->dst
.phys
.offset
;
322 diff
|= walk
->src
.virt
.page
- walk
->dst
.virt
.page
;
324 skcipher_map_src(walk
);
325 walk
->dst
.virt
.addr
= walk
->src
.virt
.addr
;
328 walk
->flags
|= SKCIPHER_WALK_DIFF
;
329 skcipher_map_dst(walk
);
335 static int skcipher_walk_next(struct skcipher_walk
*walk
)
341 walk
->flags
&= ~(SKCIPHER_WALK_SLOW
| SKCIPHER_WALK_COPY
|
345 bsize
= min(walk
->chunksize
, max(n
, walk
->blocksize
));
346 n
= scatterwalk_clamp(&walk
->in
, n
);
347 n
= scatterwalk_clamp(&walk
->out
, n
);
349 if (unlikely(n
< bsize
)) {
350 if (unlikely(walk
->total
< walk
->blocksize
))
351 return skcipher_walk_done(walk
, -EINVAL
);
354 err
= skcipher_next_slow(walk
, bsize
);
355 goto set_phys_lowmem
;
358 if (unlikely((walk
->in
.offset
| walk
->out
.offset
) & walk
->alignmask
)) {
360 gfp_t gfp
= skcipher_walk_gfp(walk
);
362 walk
->page
= (void *)__get_free_page(gfp
);
367 walk
->nbytes
= min_t(unsigned, n
,
368 PAGE_SIZE
- offset_in_page(walk
->page
));
369 walk
->flags
|= SKCIPHER_WALK_COPY
;
370 err
= skcipher_next_copy(walk
);
371 goto set_phys_lowmem
;
376 return skcipher_next_fast(walk
);
379 if (!err
&& (walk
->flags
& SKCIPHER_WALK_PHYS
)) {
380 walk
->src
.phys
.page
= virt_to_page(walk
->src
.virt
.addr
);
381 walk
->dst
.phys
.page
= virt_to_page(walk
->dst
.virt
.addr
);
382 walk
->src
.phys
.offset
&= PAGE_SIZE
- 1;
383 walk
->dst
.phys
.offset
&= PAGE_SIZE
- 1;
387 EXPORT_SYMBOL_GPL(skcipher_walk_next
);
389 static int skcipher_copy_iv(struct skcipher_walk
*walk
)
391 unsigned a
= crypto_tfm_ctx_alignment() - 1;
392 unsigned alignmask
= walk
->alignmask
;
393 unsigned ivsize
= walk
->ivsize
;
394 unsigned bs
= walk
->chunksize
;
399 aligned_bs
= ALIGN(bs
, alignmask
);
401 /* Minimum size to align buffer by alignmask. */
402 size
= alignmask
& ~a
;
404 if (walk
->flags
& SKCIPHER_WALK_PHYS
)
407 size
+= aligned_bs
+ ivsize
;
409 /* Minimum size to ensure buffer does not straddle a page. */
410 size
+= (bs
- 1) & ~(alignmask
| a
);
413 walk
->buffer
= kmalloc(size
, skcipher_walk_gfp(walk
));
417 iv
= PTR_ALIGN(walk
->buffer
, alignmask
+ 1);
418 iv
= skcipher_get_spot(iv
, bs
) + aligned_bs
;
420 walk
->iv
= memcpy(iv
, walk
->iv
, walk
->ivsize
);
424 static int skcipher_walk_first(struct skcipher_walk
*walk
)
428 if (WARN_ON_ONCE(in_irq()))
431 if (unlikely(!walk
->total
))
435 if (unlikely(((unsigned long)walk
->iv
& walk
->alignmask
))) {
436 int err
= skcipher_copy_iv(walk
);
442 walk
->nbytes
= walk
->total
;
444 return skcipher_walk_next(walk
);
447 static int skcipher_walk_skcipher(struct skcipher_walk
*walk
,
448 struct skcipher_request
*req
)
450 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
452 scatterwalk_start(&walk
->in
, req
->src
);
453 scatterwalk_start(&walk
->out
, req
->dst
);
455 walk
->total
= req
->cryptlen
;
459 walk
->flags
&= ~SKCIPHER_WALK_SLEEP
;
460 walk
->flags
|= req
->base
.flags
& CRYPTO_TFM_REQ_MAY_SLEEP
?
461 SKCIPHER_WALK_SLEEP
: 0;
463 walk
->blocksize
= crypto_skcipher_blocksize(tfm
);
464 walk
->chunksize
= crypto_skcipher_chunksize(tfm
);
465 walk
->ivsize
= crypto_skcipher_ivsize(tfm
);
466 walk
->alignmask
= crypto_skcipher_alignmask(tfm
);
468 return skcipher_walk_first(walk
);
471 int skcipher_walk_virt(struct skcipher_walk
*walk
,
472 struct skcipher_request
*req
, bool atomic
)
476 walk
->flags
&= ~SKCIPHER_WALK_PHYS
;
478 err
= skcipher_walk_skcipher(walk
, req
);
480 walk
->flags
&= atomic
? ~SKCIPHER_WALK_SLEEP
: ~0;
484 EXPORT_SYMBOL_GPL(skcipher_walk_virt
);
486 void skcipher_walk_atomise(struct skcipher_walk
*walk
)
488 walk
->flags
&= ~SKCIPHER_WALK_SLEEP
;
490 EXPORT_SYMBOL_GPL(skcipher_walk_atomise
);
492 int skcipher_walk_async(struct skcipher_walk
*walk
,
493 struct skcipher_request
*req
)
495 walk
->flags
|= SKCIPHER_WALK_PHYS
;
497 INIT_LIST_HEAD(&walk
->buffers
);
499 return skcipher_walk_skcipher(walk
, req
);
501 EXPORT_SYMBOL_GPL(skcipher_walk_async
);
503 static int skcipher_walk_aead_common(struct skcipher_walk
*walk
,
504 struct aead_request
*req
, bool atomic
)
506 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
509 walk
->flags
&= ~SKCIPHER_WALK_PHYS
;
511 scatterwalk_start(&walk
->in
, req
->src
);
512 scatterwalk_start(&walk
->out
, req
->dst
);
514 scatterwalk_copychunks(NULL
, &walk
->in
, req
->assoclen
, 2);
515 scatterwalk_copychunks(NULL
, &walk
->out
, req
->assoclen
, 2);
520 if (req
->base
.flags
& CRYPTO_TFM_REQ_MAY_SLEEP
)
521 walk
->flags
|= SKCIPHER_WALK_SLEEP
;
523 walk
->flags
&= ~SKCIPHER_WALK_SLEEP
;
525 walk
->blocksize
= crypto_aead_blocksize(tfm
);
526 walk
->chunksize
= crypto_aead_chunksize(tfm
);
527 walk
->ivsize
= crypto_aead_ivsize(tfm
);
528 walk
->alignmask
= crypto_aead_alignmask(tfm
);
530 err
= skcipher_walk_first(walk
);
533 walk
->flags
&= ~SKCIPHER_WALK_SLEEP
;
538 int skcipher_walk_aead(struct skcipher_walk
*walk
, struct aead_request
*req
,
541 walk
->total
= req
->cryptlen
;
543 return skcipher_walk_aead_common(walk
, req
, atomic
);
545 EXPORT_SYMBOL_GPL(skcipher_walk_aead
);
547 int skcipher_walk_aead_encrypt(struct skcipher_walk
*walk
,
548 struct aead_request
*req
, bool atomic
)
550 walk
->total
= req
->cryptlen
;
552 return skcipher_walk_aead_common(walk
, req
, atomic
);
554 EXPORT_SYMBOL_GPL(skcipher_walk_aead_encrypt
);
556 int skcipher_walk_aead_decrypt(struct skcipher_walk
*walk
,
557 struct aead_request
*req
, bool atomic
)
559 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
561 walk
->total
= req
->cryptlen
- crypto_aead_authsize(tfm
);
563 return skcipher_walk_aead_common(walk
, req
, atomic
);
565 EXPORT_SYMBOL_GPL(skcipher_walk_aead_decrypt
);
567 static unsigned int crypto_skcipher_extsize(struct crypto_alg
*alg
)
569 if (alg
->cra_type
== &crypto_blkcipher_type
)
570 return sizeof(struct crypto_blkcipher
*);
572 if (alg
->cra_type
== &crypto_ablkcipher_type
||
573 alg
->cra_type
== &crypto_givcipher_type
)
574 return sizeof(struct crypto_ablkcipher
*);
576 return crypto_alg_extsize(alg
);
579 static int skcipher_setkey_blkcipher(struct crypto_skcipher
*tfm
,
580 const u8
*key
, unsigned int keylen
)
582 struct crypto_blkcipher
**ctx
= crypto_skcipher_ctx(tfm
);
583 struct crypto_blkcipher
*blkcipher
= *ctx
;
586 crypto_blkcipher_clear_flags(blkcipher
, ~0);
587 crypto_blkcipher_set_flags(blkcipher
, crypto_skcipher_get_flags(tfm
) &
588 CRYPTO_TFM_REQ_MASK
);
589 err
= crypto_blkcipher_setkey(blkcipher
, key
, keylen
);
590 crypto_skcipher_set_flags(tfm
, crypto_blkcipher_get_flags(blkcipher
) &
591 CRYPTO_TFM_RES_MASK
);
596 static int skcipher_crypt_blkcipher(struct skcipher_request
*req
,
597 int (*crypt
)(struct blkcipher_desc
*,
598 struct scatterlist
*,
599 struct scatterlist
*,
602 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
603 struct crypto_blkcipher
**ctx
= crypto_skcipher_ctx(tfm
);
604 struct blkcipher_desc desc
= {
607 .flags
= req
->base
.flags
,
611 return crypt(&desc
, req
->dst
, req
->src
, req
->cryptlen
);
614 static int skcipher_encrypt_blkcipher(struct skcipher_request
*req
)
616 struct crypto_skcipher
*skcipher
= crypto_skcipher_reqtfm(req
);
617 struct crypto_tfm
*tfm
= crypto_skcipher_tfm(skcipher
);
618 struct blkcipher_alg
*alg
= &tfm
->__crt_alg
->cra_blkcipher
;
620 return skcipher_crypt_blkcipher(req
, alg
->encrypt
);
623 static int skcipher_decrypt_blkcipher(struct skcipher_request
*req
)
625 struct crypto_skcipher
*skcipher
= crypto_skcipher_reqtfm(req
);
626 struct crypto_tfm
*tfm
= crypto_skcipher_tfm(skcipher
);
627 struct blkcipher_alg
*alg
= &tfm
->__crt_alg
->cra_blkcipher
;
629 return skcipher_crypt_blkcipher(req
, alg
->decrypt
);
632 static void crypto_exit_skcipher_ops_blkcipher(struct crypto_tfm
*tfm
)
634 struct crypto_blkcipher
**ctx
= crypto_tfm_ctx(tfm
);
636 crypto_free_blkcipher(*ctx
);
639 static int crypto_init_skcipher_ops_blkcipher(struct crypto_tfm
*tfm
)
641 struct crypto_alg
*calg
= tfm
->__crt_alg
;
642 struct crypto_skcipher
*skcipher
= __crypto_skcipher_cast(tfm
);
643 struct crypto_blkcipher
**ctx
= crypto_tfm_ctx(tfm
);
644 struct crypto_blkcipher
*blkcipher
;
645 struct crypto_tfm
*btfm
;
647 if (!crypto_mod_get(calg
))
650 btfm
= __crypto_alloc_tfm(calg
, CRYPTO_ALG_TYPE_BLKCIPHER
,
651 CRYPTO_ALG_TYPE_MASK
);
653 crypto_mod_put(calg
);
654 return PTR_ERR(btfm
);
657 blkcipher
= __crypto_blkcipher_cast(btfm
);
659 tfm
->exit
= crypto_exit_skcipher_ops_blkcipher
;
661 skcipher
->setkey
= skcipher_setkey_blkcipher
;
662 skcipher
->encrypt
= skcipher_encrypt_blkcipher
;
663 skcipher
->decrypt
= skcipher_decrypt_blkcipher
;
665 skcipher
->ivsize
= crypto_blkcipher_ivsize(blkcipher
);
666 skcipher
->keysize
= calg
->cra_blkcipher
.max_keysize
;
671 static int skcipher_setkey_ablkcipher(struct crypto_skcipher
*tfm
,
672 const u8
*key
, unsigned int keylen
)
674 struct crypto_ablkcipher
**ctx
= crypto_skcipher_ctx(tfm
);
675 struct crypto_ablkcipher
*ablkcipher
= *ctx
;
678 crypto_ablkcipher_clear_flags(ablkcipher
, ~0);
679 crypto_ablkcipher_set_flags(ablkcipher
,
680 crypto_skcipher_get_flags(tfm
) &
681 CRYPTO_TFM_REQ_MASK
);
682 err
= crypto_ablkcipher_setkey(ablkcipher
, key
, keylen
);
683 crypto_skcipher_set_flags(tfm
,
684 crypto_ablkcipher_get_flags(ablkcipher
) &
685 CRYPTO_TFM_RES_MASK
);
690 static int skcipher_crypt_ablkcipher(struct skcipher_request
*req
,
691 int (*crypt
)(struct ablkcipher_request
*))
693 struct crypto_skcipher
*tfm
= crypto_skcipher_reqtfm(req
);
694 struct crypto_ablkcipher
**ctx
= crypto_skcipher_ctx(tfm
);
695 struct ablkcipher_request
*subreq
= skcipher_request_ctx(req
);
697 ablkcipher_request_set_tfm(subreq
, *ctx
);
698 ablkcipher_request_set_callback(subreq
, skcipher_request_flags(req
),
699 req
->base
.complete
, req
->base
.data
);
700 ablkcipher_request_set_crypt(subreq
, req
->src
, req
->dst
, req
->cryptlen
,
703 return crypt(subreq
);
706 static int skcipher_encrypt_ablkcipher(struct skcipher_request
*req
)
708 struct crypto_skcipher
*skcipher
= crypto_skcipher_reqtfm(req
);
709 struct crypto_tfm
*tfm
= crypto_skcipher_tfm(skcipher
);
710 struct ablkcipher_alg
*alg
= &tfm
->__crt_alg
->cra_ablkcipher
;
712 return skcipher_crypt_ablkcipher(req
, alg
->encrypt
);
715 static int skcipher_decrypt_ablkcipher(struct skcipher_request
*req
)
717 struct crypto_skcipher
*skcipher
= crypto_skcipher_reqtfm(req
);
718 struct crypto_tfm
*tfm
= crypto_skcipher_tfm(skcipher
);
719 struct ablkcipher_alg
*alg
= &tfm
->__crt_alg
->cra_ablkcipher
;
721 return skcipher_crypt_ablkcipher(req
, alg
->decrypt
);
724 static void crypto_exit_skcipher_ops_ablkcipher(struct crypto_tfm
*tfm
)
726 struct crypto_ablkcipher
**ctx
= crypto_tfm_ctx(tfm
);
728 crypto_free_ablkcipher(*ctx
);
731 static int crypto_init_skcipher_ops_ablkcipher(struct crypto_tfm
*tfm
)
733 struct crypto_alg
*calg
= tfm
->__crt_alg
;
734 struct crypto_skcipher
*skcipher
= __crypto_skcipher_cast(tfm
);
735 struct crypto_ablkcipher
**ctx
= crypto_tfm_ctx(tfm
);
736 struct crypto_ablkcipher
*ablkcipher
;
737 struct crypto_tfm
*abtfm
;
739 if (!crypto_mod_get(calg
))
742 abtfm
= __crypto_alloc_tfm(calg
, 0, 0);
744 crypto_mod_put(calg
);
745 return PTR_ERR(abtfm
);
748 ablkcipher
= __crypto_ablkcipher_cast(abtfm
);
750 tfm
->exit
= crypto_exit_skcipher_ops_ablkcipher
;
752 skcipher
->setkey
= skcipher_setkey_ablkcipher
;
753 skcipher
->encrypt
= skcipher_encrypt_ablkcipher
;
754 skcipher
->decrypt
= skcipher_decrypt_ablkcipher
;
756 skcipher
->ivsize
= crypto_ablkcipher_ivsize(ablkcipher
);
757 skcipher
->reqsize
= crypto_ablkcipher_reqsize(ablkcipher
) +
758 sizeof(struct ablkcipher_request
);
759 skcipher
->keysize
= calg
->cra_ablkcipher
.max_keysize
;
764 static void crypto_skcipher_exit_tfm(struct crypto_tfm
*tfm
)
766 struct crypto_skcipher
*skcipher
= __crypto_skcipher_cast(tfm
);
767 struct skcipher_alg
*alg
= crypto_skcipher_alg(skcipher
);
772 static int crypto_skcipher_init_tfm(struct crypto_tfm
*tfm
)
774 struct crypto_skcipher
*skcipher
= __crypto_skcipher_cast(tfm
);
775 struct skcipher_alg
*alg
= crypto_skcipher_alg(skcipher
);
777 if (tfm
->__crt_alg
->cra_type
== &crypto_blkcipher_type
)
778 return crypto_init_skcipher_ops_blkcipher(tfm
);
780 if (tfm
->__crt_alg
->cra_type
== &crypto_ablkcipher_type
||
781 tfm
->__crt_alg
->cra_type
== &crypto_givcipher_type
)
782 return crypto_init_skcipher_ops_ablkcipher(tfm
);
784 skcipher
->setkey
= alg
->setkey
;
785 skcipher
->encrypt
= alg
->encrypt
;
786 skcipher
->decrypt
= alg
->decrypt
;
787 skcipher
->ivsize
= alg
->ivsize
;
788 skcipher
->keysize
= alg
->max_keysize
;
791 skcipher
->base
.exit
= crypto_skcipher_exit_tfm
;
794 return alg
->init(skcipher
);
799 static void crypto_skcipher_free_instance(struct crypto_instance
*inst
)
801 struct skcipher_instance
*skcipher
=
802 container_of(inst
, struct skcipher_instance
, s
.base
);
804 skcipher
->free(skcipher
);
807 static void crypto_skcipher_show(struct seq_file
*m
, struct crypto_alg
*alg
)
808 __attribute__ ((unused
));
809 static void crypto_skcipher_show(struct seq_file
*m
, struct crypto_alg
*alg
)
811 struct skcipher_alg
*skcipher
= container_of(alg
, struct skcipher_alg
,
814 seq_printf(m
, "type : skcipher\n");
815 seq_printf(m
, "async : %s\n",
816 alg
->cra_flags
& CRYPTO_ALG_ASYNC
? "yes" : "no");
817 seq_printf(m
, "blocksize : %u\n", alg
->cra_blocksize
);
818 seq_printf(m
, "min keysize : %u\n", skcipher
->min_keysize
);
819 seq_printf(m
, "max keysize : %u\n", skcipher
->max_keysize
);
820 seq_printf(m
, "ivsize : %u\n", skcipher
->ivsize
);
821 seq_printf(m
, "chunksize : %u\n", skcipher
->chunksize
);
825 static int crypto_skcipher_report(struct sk_buff
*skb
, struct crypto_alg
*alg
)
827 struct crypto_report_blkcipher rblkcipher
;
828 struct skcipher_alg
*skcipher
= container_of(alg
, struct skcipher_alg
,
831 strncpy(rblkcipher
.type
, "skcipher", sizeof(rblkcipher
.type
));
832 strncpy(rblkcipher
.geniv
, "<none>", sizeof(rblkcipher
.geniv
));
834 rblkcipher
.blocksize
= alg
->cra_blocksize
;
835 rblkcipher
.min_keysize
= skcipher
->min_keysize
;
836 rblkcipher
.max_keysize
= skcipher
->max_keysize
;
837 rblkcipher
.ivsize
= skcipher
->ivsize
;
839 if (nla_put(skb
, CRYPTOCFGA_REPORT_BLKCIPHER
,
840 sizeof(struct crypto_report_blkcipher
), &rblkcipher
))
841 goto nla_put_failure
;
848 static int crypto_skcipher_report(struct sk_buff
*skb
, struct crypto_alg
*alg
)
854 static const struct crypto_type crypto_skcipher_type2
= {
855 .extsize
= crypto_skcipher_extsize
,
856 .init_tfm
= crypto_skcipher_init_tfm
,
857 .free
= crypto_skcipher_free_instance
,
858 #ifdef CONFIG_PROC_FS
859 .show
= crypto_skcipher_show
,
861 .report
= crypto_skcipher_report
,
862 .maskclear
= ~CRYPTO_ALG_TYPE_MASK
,
863 .maskset
= CRYPTO_ALG_TYPE_BLKCIPHER_MASK
,
864 .type
= CRYPTO_ALG_TYPE_SKCIPHER
,
865 .tfmsize
= offsetof(struct crypto_skcipher
, base
),
868 int crypto_grab_skcipher(struct crypto_skcipher_spawn
*spawn
,
869 const char *name
, u32 type
, u32 mask
)
871 spawn
->base
.frontend
= &crypto_skcipher_type2
;
872 return crypto_grab_spawn(&spawn
->base
, name
, type
, mask
);
874 EXPORT_SYMBOL_GPL(crypto_grab_skcipher
);
876 struct crypto_skcipher
*crypto_alloc_skcipher(const char *alg_name
,
879 return crypto_alloc_tfm(alg_name
, &crypto_skcipher_type2
, type
, mask
);
881 EXPORT_SYMBOL_GPL(crypto_alloc_skcipher
);
883 int crypto_has_skcipher2(const char *alg_name
, u32 type
, u32 mask
)
885 return crypto_type_has_alg(alg_name
, &crypto_skcipher_type2
,
888 EXPORT_SYMBOL_GPL(crypto_has_skcipher2
);
890 static int skcipher_prepare_alg(struct skcipher_alg
*alg
)
892 struct crypto_alg
*base
= &alg
->base
;
894 if (alg
->ivsize
> PAGE_SIZE
/ 8 || alg
->chunksize
> PAGE_SIZE
/ 8)
898 alg
->chunksize
= base
->cra_blocksize
;
900 base
->cra_type
= &crypto_skcipher_type2
;
901 base
->cra_flags
&= ~CRYPTO_ALG_TYPE_MASK
;
902 base
->cra_flags
|= CRYPTO_ALG_TYPE_SKCIPHER
;
907 int crypto_register_skcipher(struct skcipher_alg
*alg
)
909 struct crypto_alg
*base
= &alg
->base
;
912 err
= skcipher_prepare_alg(alg
);
916 return crypto_register_alg(base
);
918 EXPORT_SYMBOL_GPL(crypto_register_skcipher
);
920 void crypto_unregister_skcipher(struct skcipher_alg
*alg
)
922 crypto_unregister_alg(&alg
->base
);
924 EXPORT_SYMBOL_GPL(crypto_unregister_skcipher
);
926 int crypto_register_skciphers(struct skcipher_alg
*algs
, int count
)
930 for (i
= 0; i
< count
; i
++) {
931 ret
= crypto_register_skcipher(&algs
[i
]);
939 for (--i
; i
>= 0; --i
)
940 crypto_unregister_skcipher(&algs
[i
]);
944 EXPORT_SYMBOL_GPL(crypto_register_skciphers
);
946 void crypto_unregister_skciphers(struct skcipher_alg
*algs
, int count
)
950 for (i
= count
- 1; i
>= 0; --i
)
951 crypto_unregister_skcipher(&algs
[i
]);
953 EXPORT_SYMBOL_GPL(crypto_unregister_skciphers
);
955 int skcipher_register_instance(struct crypto_template
*tmpl
,
956 struct skcipher_instance
*inst
)
960 err
= skcipher_prepare_alg(&inst
->alg
);
964 return crypto_register_instance(tmpl
, skcipher_crypto_instance(inst
));
966 EXPORT_SYMBOL_GPL(skcipher_register_instance
);
968 MODULE_LICENSE("GPL");
969 MODULE_DESCRIPTION("Symmetric key cipher type");