2 * Asynchronous block chaining cipher operations.
4 * This is the asynchronous version of blkcipher.c indicating completion
7 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
16 #include <crypto/internal/skcipher.h>
17 #include <linux/cpumask.h>
18 #include <linux/err.h>
19 #include <linux/kernel.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/seq_file.h>
24 #include <linux/cryptouser.h>
25 #include <net/netlink.h>
27 #include <crypto/scatterwalk.h>
31 struct ablkcipher_buffer
{
32 struct list_head entry
;
33 struct scatter_walk dst
;
39 ABLKCIPHER_WALK_SLOW
= 1 << 0,
42 static inline void ablkcipher_buffer_write(struct ablkcipher_buffer
*p
)
44 scatterwalk_copychunks(p
->data
, &p
->dst
, p
->len
, 1);
47 void __ablkcipher_walk_complete(struct ablkcipher_walk
*walk
)
49 struct ablkcipher_buffer
*p
, *tmp
;
51 list_for_each_entry_safe(p
, tmp
, &walk
->buffers
, entry
) {
52 ablkcipher_buffer_write(p
);
57 EXPORT_SYMBOL_GPL(__ablkcipher_walk_complete
);
59 static inline void ablkcipher_queue_write(struct ablkcipher_walk
*walk
,
60 struct ablkcipher_buffer
*p
)
63 list_add_tail(&p
->entry
, &walk
->buffers
);
66 /* Get a spot of the specified length that does not straddle a page.
67 * The caller needs to ensure that there is enough space for this operation.
69 static inline u8
*ablkcipher_get_spot(u8
*start
, unsigned int len
)
71 u8
*end_page
= (u8
*)(((unsigned long)(start
+ len
- 1)) & PAGE_MASK
);
73 return max(start
, end_page
);
76 static inline void ablkcipher_done_slow(struct ablkcipher_walk
*walk
,
80 unsigned int len_this_page
= scatterwalk_pagelen(&walk
->out
);
82 if (len_this_page
> n
)
84 scatterwalk_advance(&walk
->out
, n
);
85 if (n
== len_this_page
)
88 scatterwalk_start(&walk
->out
, sg_next(walk
->out
.sg
));
92 static inline void ablkcipher_done_fast(struct ablkcipher_walk
*walk
,
95 scatterwalk_advance(&walk
->in
, n
);
96 scatterwalk_advance(&walk
->out
, n
);
99 static int ablkcipher_walk_next(struct ablkcipher_request
*req
,
100 struct ablkcipher_walk
*walk
);
102 int ablkcipher_walk_done(struct ablkcipher_request
*req
,
103 struct ablkcipher_walk
*walk
, int err
)
105 struct crypto_tfm
*tfm
= req
->base
.tfm
;
106 unsigned int n
; /* bytes processed */
109 if (unlikely(err
< 0))
112 n
= walk
->nbytes
- err
;
114 more
= (walk
->total
!= 0);
116 if (likely(!(walk
->flags
& ABLKCIPHER_WALK_SLOW
))) {
117 ablkcipher_done_fast(walk
, n
);
120 /* unexpected case; didn't process all bytes */
124 ablkcipher_done_slow(walk
, n
);
127 scatterwalk_done(&walk
->in
, 0, more
);
128 scatterwalk_done(&walk
->out
, 1, more
);
131 crypto_yield(req
->base
.flags
);
132 return ablkcipher_walk_next(req
, walk
);
137 if (walk
->iv
!= req
->info
)
138 memcpy(req
->info
, walk
->iv
, tfm
->crt_ablkcipher
.ivsize
);
139 kfree(walk
->iv_buffer
);
142 EXPORT_SYMBOL_GPL(ablkcipher_walk_done
);
144 static inline int ablkcipher_next_slow(struct ablkcipher_request
*req
,
145 struct ablkcipher_walk
*walk
,
147 unsigned int alignmask
,
148 void **src_p
, void **dst_p
)
150 unsigned aligned_bsize
= ALIGN(bsize
, alignmask
+ 1);
151 struct ablkcipher_buffer
*p
;
152 void *src
, *dst
, *base
;
155 n
= ALIGN(sizeof(struct ablkcipher_buffer
), alignmask
+ 1);
156 n
+= (aligned_bsize
* 3 - (alignmask
+ 1) +
157 (alignmask
& ~(crypto_tfm_ctx_alignment() - 1)));
159 p
= kmalloc(n
, GFP_ATOMIC
);
161 return ablkcipher_walk_done(req
, walk
, -ENOMEM
);
165 dst
= (u8
*)ALIGN((unsigned long)base
, alignmask
+ 1);
166 src
= dst
= ablkcipher_get_spot(dst
, bsize
);
171 scatterwalk_copychunks(src
, &walk
->in
, bsize
, 0);
173 ablkcipher_queue_write(walk
, p
);
175 walk
->nbytes
= bsize
;
176 walk
->flags
|= ABLKCIPHER_WALK_SLOW
;
184 static inline int ablkcipher_copy_iv(struct ablkcipher_walk
*walk
,
185 struct crypto_tfm
*tfm
,
186 unsigned int alignmask
)
188 unsigned bs
= walk
->blocksize
;
189 unsigned int ivsize
= tfm
->crt_ablkcipher
.ivsize
;
190 unsigned aligned_bs
= ALIGN(bs
, alignmask
+ 1);
191 unsigned int size
= aligned_bs
* 2 + ivsize
+ max(aligned_bs
, ivsize
) -
195 size
+= alignmask
& ~(crypto_tfm_ctx_alignment() - 1);
196 walk
->iv_buffer
= kmalloc(size
, GFP_ATOMIC
);
197 if (!walk
->iv_buffer
)
200 iv
= (u8
*)ALIGN((unsigned long)walk
->iv_buffer
, alignmask
+ 1);
201 iv
= ablkcipher_get_spot(iv
, bs
) + aligned_bs
;
202 iv
= ablkcipher_get_spot(iv
, bs
) + aligned_bs
;
203 iv
= ablkcipher_get_spot(iv
, ivsize
);
205 walk
->iv
= memcpy(iv
, walk
->iv
, ivsize
);
209 static inline int ablkcipher_next_fast(struct ablkcipher_request
*req
,
210 struct ablkcipher_walk
*walk
)
212 walk
->src
.page
= scatterwalk_page(&walk
->in
);
213 walk
->src
.offset
= offset_in_page(walk
->in
.offset
);
214 walk
->dst
.page
= scatterwalk_page(&walk
->out
);
215 walk
->dst
.offset
= offset_in_page(walk
->out
.offset
);
220 static int ablkcipher_walk_next(struct ablkcipher_request
*req
,
221 struct ablkcipher_walk
*walk
)
223 struct crypto_tfm
*tfm
= req
->base
.tfm
;
224 unsigned int alignmask
, bsize
, n
;
228 alignmask
= crypto_tfm_alg_alignmask(tfm
);
230 if (unlikely(n
< crypto_tfm_alg_blocksize(tfm
))) {
231 req
->base
.flags
|= CRYPTO_TFM_RES_BAD_BLOCK_LEN
;
232 return ablkcipher_walk_done(req
, walk
, -EINVAL
);
235 walk
->flags
&= ~ABLKCIPHER_WALK_SLOW
;
238 bsize
= min(walk
->blocksize
, n
);
239 n
= scatterwalk_clamp(&walk
->in
, n
);
240 n
= scatterwalk_clamp(&walk
->out
, n
);
243 !scatterwalk_aligned(&walk
->in
, alignmask
) ||
244 !scatterwalk_aligned(&walk
->out
, alignmask
)) {
245 err
= ablkcipher_next_slow(req
, walk
, bsize
, alignmask
,
247 goto set_phys_lowmem
;
252 return ablkcipher_next_fast(req
, walk
);
256 walk
->src
.page
= virt_to_page(src
);
257 walk
->dst
.page
= virt_to_page(dst
);
258 walk
->src
.offset
= ((unsigned long)src
& (PAGE_SIZE
- 1));
259 walk
->dst
.offset
= ((unsigned long)dst
& (PAGE_SIZE
- 1));
265 static int ablkcipher_walk_first(struct ablkcipher_request
*req
,
266 struct ablkcipher_walk
*walk
)
268 struct crypto_tfm
*tfm
= req
->base
.tfm
;
269 unsigned int alignmask
;
271 alignmask
= crypto_tfm_alg_alignmask(tfm
);
272 if (WARN_ON_ONCE(in_irq()))
275 walk
->iv
= req
->info
;
276 walk
->nbytes
= walk
->total
;
277 if (unlikely(!walk
->total
))
280 walk
->iv_buffer
= NULL
;
281 if (unlikely(((unsigned long)walk
->iv
& alignmask
))) {
282 int err
= ablkcipher_copy_iv(walk
, tfm
, alignmask
);
288 scatterwalk_start(&walk
->in
, walk
->in
.sg
);
289 scatterwalk_start(&walk
->out
, walk
->out
.sg
);
291 return ablkcipher_walk_next(req
, walk
);
294 int ablkcipher_walk_phys(struct ablkcipher_request
*req
,
295 struct ablkcipher_walk
*walk
)
297 walk
->blocksize
= crypto_tfm_alg_blocksize(req
->base
.tfm
);
298 return ablkcipher_walk_first(req
, walk
);
300 EXPORT_SYMBOL_GPL(ablkcipher_walk_phys
);
302 static int setkey_unaligned(struct crypto_ablkcipher
*tfm
, const u8
*key
,
305 struct ablkcipher_alg
*cipher
= crypto_ablkcipher_alg(tfm
);
306 unsigned long alignmask
= crypto_ablkcipher_alignmask(tfm
);
308 u8
*buffer
, *alignbuffer
;
309 unsigned long absize
;
311 absize
= keylen
+ alignmask
;
312 buffer
= kmalloc(absize
, GFP_ATOMIC
);
316 alignbuffer
= (u8
*)ALIGN((unsigned long)buffer
, alignmask
+ 1);
317 memcpy(alignbuffer
, key
, keylen
);
318 ret
= cipher
->setkey(tfm
, alignbuffer
, keylen
);
319 memset(alignbuffer
, 0, keylen
);
324 static int setkey(struct crypto_ablkcipher
*tfm
, const u8
*key
,
327 struct ablkcipher_alg
*cipher
= crypto_ablkcipher_alg(tfm
);
328 unsigned long alignmask
= crypto_ablkcipher_alignmask(tfm
);
330 if (keylen
< cipher
->min_keysize
|| keylen
> cipher
->max_keysize
) {
331 crypto_ablkcipher_set_flags(tfm
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
335 if ((unsigned long)key
& alignmask
)
336 return setkey_unaligned(tfm
, key
, keylen
);
338 return cipher
->setkey(tfm
, key
, keylen
);
341 static unsigned int crypto_ablkcipher_ctxsize(struct crypto_alg
*alg
, u32 type
,
344 return alg
->cra_ctxsize
;
347 int skcipher_null_givencrypt(struct skcipher_givcrypt_request
*req
)
349 return crypto_ablkcipher_encrypt(&req
->creq
);
352 int skcipher_null_givdecrypt(struct skcipher_givcrypt_request
*req
)
354 return crypto_ablkcipher_decrypt(&req
->creq
);
357 static int crypto_init_ablkcipher_ops(struct crypto_tfm
*tfm
, u32 type
,
360 struct ablkcipher_alg
*alg
= &tfm
->__crt_alg
->cra_ablkcipher
;
361 struct ablkcipher_tfm
*crt
= &tfm
->crt_ablkcipher
;
363 if (alg
->ivsize
> PAGE_SIZE
/ 8)
366 crt
->setkey
= setkey
;
367 crt
->encrypt
= alg
->encrypt
;
368 crt
->decrypt
= alg
->decrypt
;
370 crt
->givencrypt
= skcipher_null_givencrypt
;
371 crt
->givdecrypt
= skcipher_null_givdecrypt
;
373 crt
->base
= __crypto_ablkcipher_cast(tfm
);
374 crt
->ivsize
= alg
->ivsize
;
380 static int crypto_ablkcipher_report(struct sk_buff
*skb
, struct crypto_alg
*alg
)
382 struct crypto_report_blkcipher rblkcipher
;
384 strncpy(rblkcipher
.type
, "ablkcipher", sizeof(rblkcipher
.type
));
385 strncpy(rblkcipher
.geniv
, alg
->cra_ablkcipher
.geniv
?: "<default>",
386 sizeof(rblkcipher
.geniv
));
387 rblkcipher
.geniv
[sizeof(rblkcipher
.geniv
) - 1] = '\0';
389 rblkcipher
.blocksize
= alg
->cra_blocksize
;
390 rblkcipher
.min_keysize
= alg
->cra_ablkcipher
.min_keysize
;
391 rblkcipher
.max_keysize
= alg
->cra_ablkcipher
.max_keysize
;
392 rblkcipher
.ivsize
= alg
->cra_ablkcipher
.ivsize
;
394 if (nla_put(skb
, CRYPTOCFGA_REPORT_BLKCIPHER
,
395 sizeof(struct crypto_report_blkcipher
), &rblkcipher
))
396 goto nla_put_failure
;
403 static int crypto_ablkcipher_report(struct sk_buff
*skb
, struct crypto_alg
*alg
)
409 static void crypto_ablkcipher_show(struct seq_file
*m
, struct crypto_alg
*alg
)
410 __attribute__ ((unused
));
411 static void crypto_ablkcipher_show(struct seq_file
*m
, struct crypto_alg
*alg
)
413 struct ablkcipher_alg
*ablkcipher
= &alg
->cra_ablkcipher
;
415 seq_printf(m
, "type : ablkcipher\n");
416 seq_printf(m
, "async : %s\n", alg
->cra_flags
& CRYPTO_ALG_ASYNC
?
418 seq_printf(m
, "blocksize : %u\n", alg
->cra_blocksize
);
419 seq_printf(m
, "min keysize : %u\n", ablkcipher
->min_keysize
);
420 seq_printf(m
, "max keysize : %u\n", ablkcipher
->max_keysize
);
421 seq_printf(m
, "ivsize : %u\n", ablkcipher
->ivsize
);
422 seq_printf(m
, "geniv : %s\n", ablkcipher
->geniv
?: "<default>");
425 const struct crypto_type crypto_ablkcipher_type
= {
426 .ctxsize
= crypto_ablkcipher_ctxsize
,
427 .init
= crypto_init_ablkcipher_ops
,
428 #ifdef CONFIG_PROC_FS
429 .show
= crypto_ablkcipher_show
,
431 .report
= crypto_ablkcipher_report
,
433 EXPORT_SYMBOL_GPL(crypto_ablkcipher_type
);
435 static int no_givdecrypt(struct skcipher_givcrypt_request
*req
)
440 static int crypto_init_givcipher_ops(struct crypto_tfm
*tfm
, u32 type
,
443 struct ablkcipher_alg
*alg
= &tfm
->__crt_alg
->cra_ablkcipher
;
444 struct ablkcipher_tfm
*crt
= &tfm
->crt_ablkcipher
;
446 if (alg
->ivsize
> PAGE_SIZE
/ 8)
449 crt
->setkey
= tfm
->__crt_alg
->cra_flags
& CRYPTO_ALG_GENIV
?
450 alg
->setkey
: setkey
;
451 crt
->encrypt
= alg
->encrypt
;
452 crt
->decrypt
= alg
->decrypt
;
453 crt
->givencrypt
= alg
->givencrypt
?: no_givdecrypt
;
454 crt
->givdecrypt
= alg
->givdecrypt
?: no_givdecrypt
;
455 crt
->base
= __crypto_ablkcipher_cast(tfm
);
456 crt
->ivsize
= alg
->ivsize
;
462 static int crypto_givcipher_report(struct sk_buff
*skb
, struct crypto_alg
*alg
)
464 struct crypto_report_blkcipher rblkcipher
;
466 strncpy(rblkcipher
.type
, "givcipher", sizeof(rblkcipher
.type
));
467 strncpy(rblkcipher
.geniv
, alg
->cra_ablkcipher
.geniv
?: "<built-in>",
468 sizeof(rblkcipher
.geniv
));
469 rblkcipher
.geniv
[sizeof(rblkcipher
.geniv
) - 1] = '\0';
471 rblkcipher
.blocksize
= alg
->cra_blocksize
;
472 rblkcipher
.min_keysize
= alg
->cra_ablkcipher
.min_keysize
;
473 rblkcipher
.max_keysize
= alg
->cra_ablkcipher
.max_keysize
;
474 rblkcipher
.ivsize
= alg
->cra_ablkcipher
.ivsize
;
476 if (nla_put(skb
, CRYPTOCFGA_REPORT_BLKCIPHER
,
477 sizeof(struct crypto_report_blkcipher
), &rblkcipher
))
478 goto nla_put_failure
;
485 static int crypto_givcipher_report(struct sk_buff
*skb
, struct crypto_alg
*alg
)
491 static void crypto_givcipher_show(struct seq_file
*m
, struct crypto_alg
*alg
)
492 __attribute__ ((unused
));
493 static void crypto_givcipher_show(struct seq_file
*m
, struct crypto_alg
*alg
)
495 struct ablkcipher_alg
*ablkcipher
= &alg
->cra_ablkcipher
;
497 seq_printf(m
, "type : givcipher\n");
498 seq_printf(m
, "async : %s\n", alg
->cra_flags
& CRYPTO_ALG_ASYNC
?
500 seq_printf(m
, "blocksize : %u\n", alg
->cra_blocksize
);
501 seq_printf(m
, "min keysize : %u\n", ablkcipher
->min_keysize
);
502 seq_printf(m
, "max keysize : %u\n", ablkcipher
->max_keysize
);
503 seq_printf(m
, "ivsize : %u\n", ablkcipher
->ivsize
);
504 seq_printf(m
, "geniv : %s\n", ablkcipher
->geniv
?: "<built-in>");
507 const struct crypto_type crypto_givcipher_type
= {
508 .ctxsize
= crypto_ablkcipher_ctxsize
,
509 .init
= crypto_init_givcipher_ops
,
510 #ifdef CONFIG_PROC_FS
511 .show
= crypto_givcipher_show
,
513 .report
= crypto_givcipher_report
,
515 EXPORT_SYMBOL_GPL(crypto_givcipher_type
);
517 const char *crypto_default_geniv(const struct crypto_alg
*alg
)
519 if (((alg
->cra_flags
& CRYPTO_ALG_TYPE_MASK
) ==
520 CRYPTO_ALG_TYPE_BLKCIPHER
? alg
->cra_blkcipher
.ivsize
:
521 alg
->cra_ablkcipher
.ivsize
) !=
528 static int crypto_givcipher_default(struct crypto_alg
*alg
, u32 type
, u32 mask
)
530 struct rtattr
*tb
[3];
533 struct crypto_attr_type data
;
537 struct crypto_attr_alg data
;
539 struct crypto_template
*tmpl
;
540 struct crypto_instance
*inst
;
541 struct crypto_alg
*larval
;
545 larval
= crypto_larval_lookup(alg
->cra_driver_name
,
546 (type
& ~CRYPTO_ALG_TYPE_MASK
) |
547 CRYPTO_ALG_TYPE_GIVCIPHER
,
548 mask
| CRYPTO_ALG_TYPE_MASK
);
549 err
= PTR_ERR(larval
);
554 if (!crypto_is_larval(larval
))
557 ptype
.attr
.rta_len
= sizeof(ptype
);
558 ptype
.attr
.rta_type
= CRYPTOA_TYPE
;
559 ptype
.data
.type
= type
| CRYPTO_ALG_GENIV
;
560 /* GENIV tells the template that we're making a default geniv. */
561 ptype
.data
.mask
= mask
| CRYPTO_ALG_GENIV
;
564 palg
.attr
.rta_len
= sizeof(palg
);
565 palg
.attr
.rta_type
= CRYPTOA_ALG
;
566 /* Must use the exact name to locate ourselves. */
567 memcpy(palg
.data
.name
, alg
->cra_driver_name
, CRYPTO_MAX_ALG_NAME
);
572 if ((alg
->cra_flags
& CRYPTO_ALG_TYPE_MASK
) ==
573 CRYPTO_ALG_TYPE_BLKCIPHER
)
574 geniv
= alg
->cra_blkcipher
.geniv
;
576 geniv
= alg
->cra_ablkcipher
.geniv
;
579 geniv
= crypto_default_geniv(alg
);
581 tmpl
= crypto_lookup_template(geniv
);
587 err
= tmpl
->create(tmpl
, tb
);
593 inst
= tmpl
->alloc(tb
);
598 err
= crypto_register_instance(tmpl
, inst
);
605 /* Redo the lookup to use the instance we just registered. */
609 crypto_tmpl_put(tmpl
);
611 crypto_larval_kill(larval
);
613 crypto_mod_put(larval
);
619 struct crypto_alg
*crypto_lookup_skcipher(const char *name
, u32 type
, u32 mask
)
621 struct crypto_alg
*alg
;
623 alg
= crypto_alg_mod_lookup(name
, type
, mask
);
627 if ((alg
->cra_flags
& CRYPTO_ALG_TYPE_MASK
) ==
628 CRYPTO_ALG_TYPE_GIVCIPHER
)
631 if (!((alg
->cra_flags
& CRYPTO_ALG_TYPE_MASK
) ==
632 CRYPTO_ALG_TYPE_BLKCIPHER
? alg
->cra_blkcipher
.ivsize
:
633 alg
->cra_ablkcipher
.ivsize
))
637 alg
= crypto_alg_mod_lookup(name
, type
| CRYPTO_ALG_TESTED
,
638 mask
& ~CRYPTO_ALG_TESTED
);
642 if ((alg
->cra_flags
& CRYPTO_ALG_TYPE_MASK
) ==
643 CRYPTO_ALG_TYPE_GIVCIPHER
) {
644 if (~alg
->cra_flags
& (type
^ ~mask
) & CRYPTO_ALG_TESTED
) {
646 alg
= ERR_PTR(-ENOENT
);
651 BUG_ON(!((alg
->cra_flags
& CRYPTO_ALG_TYPE_MASK
) ==
652 CRYPTO_ALG_TYPE_BLKCIPHER
? alg
->cra_blkcipher
.ivsize
:
653 alg
->cra_ablkcipher
.ivsize
));
655 return ERR_PTR(crypto_givcipher_default(alg
, type
, mask
));
657 EXPORT_SYMBOL_GPL(crypto_lookup_skcipher
);
659 int crypto_grab_skcipher(struct crypto_skcipher_spawn
*spawn
, const char *name
,
662 struct crypto_alg
*alg
;
665 type
= crypto_skcipher_type(type
);
666 mask
= crypto_skcipher_mask(mask
);
668 alg
= crypto_lookup_skcipher(name
, type
, mask
);
672 err
= crypto_init_spawn(&spawn
->base
, alg
, spawn
->base
.inst
, mask
);
676 EXPORT_SYMBOL_GPL(crypto_grab_skcipher
);
678 struct crypto_ablkcipher
*crypto_alloc_ablkcipher(const char *alg_name
,
681 struct crypto_tfm
*tfm
;
684 type
= crypto_skcipher_type(type
);
685 mask
= crypto_skcipher_mask(mask
);
688 struct crypto_alg
*alg
;
690 alg
= crypto_lookup_skcipher(alg_name
, type
, mask
);
696 tfm
= __crypto_alloc_tfm(alg
, type
, mask
);
698 return __crypto_ablkcipher_cast(tfm
);
706 if (fatal_signal_pending(current
)) {
714 EXPORT_SYMBOL_GPL(crypto_alloc_ablkcipher
);