]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - crypto/skcipher.c
Merge tag 'xfs-for-linus-4.10-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-artful-kernel.git] / crypto / skcipher.c
1 /*
2 * Symmetric key cipher operations.
3 *
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.
7 *
8 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
9 *
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.
14 *
15 */
16
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>
27
28 #include "internal.h"
29
30 enum {
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,
36 };
37
38 struct skcipher_walk_buffer {
39 struct list_head entry;
40 struct scatter_walk dst;
41 unsigned int len;
42 u8 *data;
43 u8 buffer[];
44 };
45
46 static int skcipher_walk_next(struct skcipher_walk *walk);
47
48 static inline void skcipher_unmap(struct scatter_walk *walk, void *vaddr)
49 {
50 if (PageHighMem(scatterwalk_page(walk)))
51 kunmap_atomic(vaddr);
52 }
53
54 static inline void *skcipher_map(struct scatter_walk *walk)
55 {
56 struct page *page = scatterwalk_page(walk);
57
58 return (PageHighMem(page) ? kmap_atomic(page) : page_address(page)) +
59 offset_in_page(walk->offset);
60 }
61
62 static inline void skcipher_map_src(struct skcipher_walk *walk)
63 {
64 walk->src.virt.addr = skcipher_map(&walk->in);
65 }
66
67 static inline void skcipher_map_dst(struct skcipher_walk *walk)
68 {
69 walk->dst.virt.addr = skcipher_map(&walk->out);
70 }
71
72 static inline void skcipher_unmap_src(struct skcipher_walk *walk)
73 {
74 skcipher_unmap(&walk->in, walk->src.virt.addr);
75 }
76
77 static inline void skcipher_unmap_dst(struct skcipher_walk *walk)
78 {
79 skcipher_unmap(&walk->out, walk->dst.virt.addr);
80 }
81
82 static inline gfp_t skcipher_walk_gfp(struct skcipher_walk *walk)
83 {
84 return walk->flags & SKCIPHER_WALK_SLEEP ? GFP_KERNEL : GFP_ATOMIC;
85 }
86
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.
89 */
90 static inline u8 *skcipher_get_spot(u8 *start, unsigned int len)
91 {
92 u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
93
94 return max(start, end_page);
95 }
96
97 static int skcipher_done_slow(struct skcipher_walk *walk, unsigned int bsize)
98 {
99 u8 *addr;
100
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);
105 return 0;
106 }
107
108 int skcipher_walk_done(struct skcipher_walk *walk, int err)
109 {
110 unsigned int n = walk->nbytes - err;
111 unsigned int nbytes;
112
113 nbytes = walk->total - n;
114
115 if (unlikely(err < 0)) {
116 nbytes = 0;
117 n = 0;
118 } else if (likely(!(walk->flags & (SKCIPHER_WALK_PHYS |
119 SKCIPHER_WALK_SLOW |
120 SKCIPHER_WALK_COPY |
121 SKCIPHER_WALK_DIFF)))) {
122 unmap_src:
123 skcipher_unmap_src(walk);
124 } else if (walk->flags & SKCIPHER_WALK_DIFF) {
125 skcipher_unmap_dst(walk);
126 goto unmap_src;
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)) {
132 if (WARN_ON(err)) {
133 err = -EINVAL;
134 nbytes = 0;
135 } else
136 n = skcipher_done_slow(walk, n);
137 }
138
139 if (err > 0)
140 err = 0;
141
142 walk->total = nbytes;
143 walk->nbytes = nbytes;
144
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);
149
150 if (nbytes) {
151 crypto_yield(walk->flags & SKCIPHER_WALK_SLEEP ?
152 CRYPTO_TFM_REQ_MAY_SLEEP : 0);
153 return skcipher_walk_next(walk);
154 }
155
156 /* Short-circuit for the common/fast path. */
157 if (!((unsigned long)walk->buffer | (unsigned long)walk->page))
158 goto out;
159
160 if (walk->flags & SKCIPHER_WALK_PHYS)
161 goto out;
162
163 if (walk->iv != walk->oiv)
164 memcpy(walk->oiv, walk->iv, walk->ivsize);
165 if (walk->buffer != walk->page)
166 kfree(walk->buffer);
167 if (walk->page)
168 free_page((unsigned long)walk->page);
169
170 out:
171 return err;
172 }
173 EXPORT_SYMBOL_GPL(skcipher_walk_done);
174
175 void skcipher_walk_complete(struct skcipher_walk *walk, int err)
176 {
177 struct skcipher_walk_buffer *p, *tmp;
178
179 list_for_each_entry_safe(p, tmp, &walk->buffers, entry) {
180 u8 *data;
181
182 if (err)
183 goto done;
184
185 data = p->data;
186 if (!data) {
187 data = PTR_ALIGN(&p->buffer[0], walk->alignmask + 1);
188 data = skcipher_get_spot(data, walk->chunksize);
189 }
190
191 scatterwalk_copychunks(data, &p->dst, p->len, 1);
192
193 if (offset_in_page(p->data) + p->len + walk->chunksize >
194 PAGE_SIZE)
195 free_page((unsigned long)p->data);
196
197 done:
198 list_del(&p->entry);
199 kfree(p);
200 }
201
202 if (!err && walk->iv != walk->oiv)
203 memcpy(walk->oiv, walk->iv, walk->ivsize);
204 if (walk->buffer != walk->page)
205 kfree(walk->buffer);
206 if (walk->page)
207 free_page((unsigned long)walk->page);
208 }
209 EXPORT_SYMBOL_GPL(skcipher_walk_complete);
210
211 static void skcipher_queue_write(struct skcipher_walk *walk,
212 struct skcipher_walk_buffer *p)
213 {
214 p->dst = walk->out;
215 list_add_tail(&p->entry, &walk->buffers);
216 }
217
218 static int skcipher_next_slow(struct skcipher_walk *walk, unsigned int bsize)
219 {
220 bool phys = walk->flags & SKCIPHER_WALK_PHYS;
221 unsigned alignmask = walk->alignmask;
222 struct skcipher_walk_buffer *p;
223 unsigned a;
224 unsigned n;
225 u8 *buffer;
226 void *v;
227
228 if (!phys) {
229 buffer = walk->buffer ?: walk->page;
230 if (buffer)
231 goto ok;
232 }
233
234 /* Start with the minimum alignment of kmalloc. */
235 a = crypto_tfm_ctx_alignment() - 1;
236 n = bsize;
237
238 if (phys) {
239 /* Calculate the minimum alignment of p->buffer. */
240 a &= (sizeof(*p) ^ (sizeof(*p) - 1)) >> 1;
241 n += sizeof(*p);
242 }
243
244 /* Minimum size to align p->buffer by alignmask. */
245 n += alignmask & ~a;
246
247 /* Minimum size to ensure p->buffer does not straddle a page. */
248 n += (bsize - 1) & ~(alignmask | a);
249
250 v = kzalloc(n, skcipher_walk_gfp(walk));
251 if (!v)
252 return skcipher_walk_done(walk, -ENOMEM);
253
254 if (phys) {
255 p = v;
256 p->len = bsize;
257 skcipher_queue_write(walk, p);
258 buffer = p->buffer;
259 } else {
260 walk->buffer = v;
261 buffer = v;
262 }
263
264 ok:
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;
268
269 scatterwalk_copychunks(walk->src.virt.addr, &walk->in, bsize, 0);
270
271 walk->nbytes = bsize;
272 walk->flags |= SKCIPHER_WALK_SLOW;
273
274 return 0;
275 }
276
277 static int skcipher_next_copy(struct skcipher_walk *walk)
278 {
279 struct skcipher_walk_buffer *p;
280 u8 *tmp = walk->page;
281
282 skcipher_map_src(walk);
283 memcpy(tmp, walk->src.virt.addr, walk->nbytes);
284 skcipher_unmap_src(walk);
285
286 walk->src.virt.addr = tmp;
287 walk->dst.virt.addr = tmp;
288
289 if (!(walk->flags & SKCIPHER_WALK_PHYS))
290 return 0;
291
292 p = kmalloc(sizeof(*p), skcipher_walk_gfp(walk));
293 if (!p)
294 return -ENOMEM;
295
296 p->data = walk->page;
297 p->len = walk->nbytes;
298 skcipher_queue_write(walk, p);
299
300 if (offset_in_page(walk->page) + walk->nbytes + walk->chunksize >
301 PAGE_SIZE)
302 walk->page = NULL;
303 else
304 walk->page += walk->nbytes;
305
306 return 0;
307 }
308
309 static int skcipher_next_fast(struct skcipher_walk *walk)
310 {
311 unsigned long diff;
312
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);
317
318 if (walk->flags & SKCIPHER_WALK_PHYS)
319 return 0;
320
321 diff = walk->src.phys.offset - walk->dst.phys.offset;
322 diff |= walk->src.virt.page - walk->dst.virt.page;
323
324 skcipher_map_src(walk);
325 walk->dst.virt.addr = walk->src.virt.addr;
326
327 if (diff) {
328 walk->flags |= SKCIPHER_WALK_DIFF;
329 skcipher_map_dst(walk);
330 }
331
332 return 0;
333 }
334
335 static int skcipher_walk_next(struct skcipher_walk *walk)
336 {
337 unsigned int bsize;
338 unsigned int n;
339 int err;
340
341 walk->flags &= ~(SKCIPHER_WALK_SLOW | SKCIPHER_WALK_COPY |
342 SKCIPHER_WALK_DIFF);
343
344 n = walk->total;
345 bsize = min(walk->chunksize, max(n, walk->blocksize));
346 n = scatterwalk_clamp(&walk->in, n);
347 n = scatterwalk_clamp(&walk->out, n);
348
349 if (unlikely(n < bsize)) {
350 if (unlikely(walk->total < walk->blocksize))
351 return skcipher_walk_done(walk, -EINVAL);
352
353 slow_path:
354 err = skcipher_next_slow(walk, bsize);
355 goto set_phys_lowmem;
356 }
357
358 if (unlikely((walk->in.offset | walk->out.offset) & walk->alignmask)) {
359 if (!walk->page) {
360 gfp_t gfp = skcipher_walk_gfp(walk);
361
362 walk->page = (void *)__get_free_page(gfp);
363 if (!walk->page)
364 goto slow_path;
365 }
366
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;
372 }
373
374 walk->nbytes = n;
375
376 return skcipher_next_fast(walk);
377
378 set_phys_lowmem:
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;
384 }
385 return err;
386 }
387 EXPORT_SYMBOL_GPL(skcipher_walk_next);
388
389 static int skcipher_copy_iv(struct skcipher_walk *walk)
390 {
391 unsigned a = crypto_tfm_ctx_alignment() - 1;
392 unsigned alignmask = walk->alignmask;
393 unsigned ivsize = walk->ivsize;
394 unsigned bs = walk->chunksize;
395 unsigned aligned_bs;
396 unsigned size;
397 u8 *iv;
398
399 aligned_bs = ALIGN(bs, alignmask);
400
401 /* Minimum size to align buffer by alignmask. */
402 size = alignmask & ~a;
403
404 if (walk->flags & SKCIPHER_WALK_PHYS)
405 size += ivsize;
406 else {
407 size += aligned_bs + ivsize;
408
409 /* Minimum size to ensure buffer does not straddle a page. */
410 size += (bs - 1) & ~(alignmask | a);
411 }
412
413 walk->buffer = kmalloc(size, skcipher_walk_gfp(walk));
414 if (!walk->buffer)
415 return -ENOMEM;
416
417 iv = PTR_ALIGN(walk->buffer, alignmask + 1);
418 iv = skcipher_get_spot(iv, bs) + aligned_bs;
419
420 walk->iv = memcpy(iv, walk->iv, walk->ivsize);
421 return 0;
422 }
423
424 static int skcipher_walk_first(struct skcipher_walk *walk)
425 {
426 walk->nbytes = 0;
427
428 if (WARN_ON_ONCE(in_irq()))
429 return -EDEADLK;
430
431 if (unlikely(!walk->total))
432 return 0;
433
434 walk->buffer = NULL;
435 if (unlikely(((unsigned long)walk->iv & walk->alignmask))) {
436 int err = skcipher_copy_iv(walk);
437 if (err)
438 return err;
439 }
440
441 walk->page = NULL;
442 walk->nbytes = walk->total;
443
444 return skcipher_walk_next(walk);
445 }
446
447 static int skcipher_walk_skcipher(struct skcipher_walk *walk,
448 struct skcipher_request *req)
449 {
450 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
451
452 scatterwalk_start(&walk->in, req->src);
453 scatterwalk_start(&walk->out, req->dst);
454
455 walk->total = req->cryptlen;
456 walk->iv = req->iv;
457 walk->oiv = req->iv;
458
459 walk->flags &= ~SKCIPHER_WALK_SLEEP;
460 walk->flags |= req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
461 SKCIPHER_WALK_SLEEP : 0;
462
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);
467
468 return skcipher_walk_first(walk);
469 }
470
471 int skcipher_walk_virt(struct skcipher_walk *walk,
472 struct skcipher_request *req, bool atomic)
473 {
474 int err;
475
476 walk->flags &= ~SKCIPHER_WALK_PHYS;
477
478 err = skcipher_walk_skcipher(walk, req);
479
480 walk->flags &= atomic ? ~SKCIPHER_WALK_SLEEP : ~0;
481
482 return err;
483 }
484 EXPORT_SYMBOL_GPL(skcipher_walk_virt);
485
486 void skcipher_walk_atomise(struct skcipher_walk *walk)
487 {
488 walk->flags &= ~SKCIPHER_WALK_SLEEP;
489 }
490 EXPORT_SYMBOL_GPL(skcipher_walk_atomise);
491
492 int skcipher_walk_async(struct skcipher_walk *walk,
493 struct skcipher_request *req)
494 {
495 walk->flags |= SKCIPHER_WALK_PHYS;
496
497 INIT_LIST_HEAD(&walk->buffers);
498
499 return skcipher_walk_skcipher(walk, req);
500 }
501 EXPORT_SYMBOL_GPL(skcipher_walk_async);
502
503 static int skcipher_walk_aead_common(struct skcipher_walk *walk,
504 struct aead_request *req, bool atomic)
505 {
506 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
507 int err;
508
509 walk->flags &= ~SKCIPHER_WALK_PHYS;
510
511 scatterwalk_start(&walk->in, req->src);
512 scatterwalk_start(&walk->out, req->dst);
513
514 scatterwalk_copychunks(NULL, &walk->in, req->assoclen, 2);
515 scatterwalk_copychunks(NULL, &walk->out, req->assoclen, 2);
516
517 walk->iv = req->iv;
518 walk->oiv = req->iv;
519
520 if (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP)
521 walk->flags |= SKCIPHER_WALK_SLEEP;
522 else
523 walk->flags &= ~SKCIPHER_WALK_SLEEP;
524
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);
529
530 err = skcipher_walk_first(walk);
531
532 if (atomic)
533 walk->flags &= ~SKCIPHER_WALK_SLEEP;
534
535 return err;
536 }
537
538 int skcipher_walk_aead(struct skcipher_walk *walk, struct aead_request *req,
539 bool atomic)
540 {
541 walk->total = req->cryptlen;
542
543 return skcipher_walk_aead_common(walk, req, atomic);
544 }
545 EXPORT_SYMBOL_GPL(skcipher_walk_aead);
546
547 int skcipher_walk_aead_encrypt(struct skcipher_walk *walk,
548 struct aead_request *req, bool atomic)
549 {
550 walk->total = req->cryptlen;
551
552 return skcipher_walk_aead_common(walk, req, atomic);
553 }
554 EXPORT_SYMBOL_GPL(skcipher_walk_aead_encrypt);
555
556 int skcipher_walk_aead_decrypt(struct skcipher_walk *walk,
557 struct aead_request *req, bool atomic)
558 {
559 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
560
561 walk->total = req->cryptlen - crypto_aead_authsize(tfm);
562
563 return skcipher_walk_aead_common(walk, req, atomic);
564 }
565 EXPORT_SYMBOL_GPL(skcipher_walk_aead_decrypt);
566
567 static unsigned int crypto_skcipher_extsize(struct crypto_alg *alg)
568 {
569 if (alg->cra_type == &crypto_blkcipher_type)
570 return sizeof(struct crypto_blkcipher *);
571
572 if (alg->cra_type == &crypto_ablkcipher_type ||
573 alg->cra_type == &crypto_givcipher_type)
574 return sizeof(struct crypto_ablkcipher *);
575
576 return crypto_alg_extsize(alg);
577 }
578
579 static int skcipher_setkey_blkcipher(struct crypto_skcipher *tfm,
580 const u8 *key, unsigned int keylen)
581 {
582 struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
583 struct crypto_blkcipher *blkcipher = *ctx;
584 int err;
585
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);
592
593 return err;
594 }
595
596 static int skcipher_crypt_blkcipher(struct skcipher_request *req,
597 int (*crypt)(struct blkcipher_desc *,
598 struct scatterlist *,
599 struct scatterlist *,
600 unsigned int))
601 {
602 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
603 struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
604 struct blkcipher_desc desc = {
605 .tfm = *ctx,
606 .info = req->iv,
607 .flags = req->base.flags,
608 };
609
610
611 return crypt(&desc, req->dst, req->src, req->cryptlen);
612 }
613
614 static int skcipher_encrypt_blkcipher(struct skcipher_request *req)
615 {
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;
619
620 return skcipher_crypt_blkcipher(req, alg->encrypt);
621 }
622
623 static int skcipher_decrypt_blkcipher(struct skcipher_request *req)
624 {
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;
628
629 return skcipher_crypt_blkcipher(req, alg->decrypt);
630 }
631
632 static void crypto_exit_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
633 {
634 struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
635
636 crypto_free_blkcipher(*ctx);
637 }
638
639 static int crypto_init_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
640 {
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;
646
647 if (!crypto_mod_get(calg))
648 return -EAGAIN;
649
650 btfm = __crypto_alloc_tfm(calg, CRYPTO_ALG_TYPE_BLKCIPHER,
651 CRYPTO_ALG_TYPE_MASK);
652 if (IS_ERR(btfm)) {
653 crypto_mod_put(calg);
654 return PTR_ERR(btfm);
655 }
656
657 blkcipher = __crypto_blkcipher_cast(btfm);
658 *ctx = blkcipher;
659 tfm->exit = crypto_exit_skcipher_ops_blkcipher;
660
661 skcipher->setkey = skcipher_setkey_blkcipher;
662 skcipher->encrypt = skcipher_encrypt_blkcipher;
663 skcipher->decrypt = skcipher_decrypt_blkcipher;
664
665 skcipher->ivsize = crypto_blkcipher_ivsize(blkcipher);
666 skcipher->keysize = calg->cra_blkcipher.max_keysize;
667
668 return 0;
669 }
670
671 static int skcipher_setkey_ablkcipher(struct crypto_skcipher *tfm,
672 const u8 *key, unsigned int keylen)
673 {
674 struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
675 struct crypto_ablkcipher *ablkcipher = *ctx;
676 int err;
677
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);
686
687 return err;
688 }
689
690 static int skcipher_crypt_ablkcipher(struct skcipher_request *req,
691 int (*crypt)(struct ablkcipher_request *))
692 {
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);
696
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,
701 req->iv);
702
703 return crypt(subreq);
704 }
705
706 static int skcipher_encrypt_ablkcipher(struct skcipher_request *req)
707 {
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;
711
712 return skcipher_crypt_ablkcipher(req, alg->encrypt);
713 }
714
715 static int skcipher_decrypt_ablkcipher(struct skcipher_request *req)
716 {
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;
720
721 return skcipher_crypt_ablkcipher(req, alg->decrypt);
722 }
723
724 static void crypto_exit_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
725 {
726 struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
727
728 crypto_free_ablkcipher(*ctx);
729 }
730
731 static int crypto_init_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
732 {
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;
738
739 if (!crypto_mod_get(calg))
740 return -EAGAIN;
741
742 abtfm = __crypto_alloc_tfm(calg, 0, 0);
743 if (IS_ERR(abtfm)) {
744 crypto_mod_put(calg);
745 return PTR_ERR(abtfm);
746 }
747
748 ablkcipher = __crypto_ablkcipher_cast(abtfm);
749 *ctx = ablkcipher;
750 tfm->exit = crypto_exit_skcipher_ops_ablkcipher;
751
752 skcipher->setkey = skcipher_setkey_ablkcipher;
753 skcipher->encrypt = skcipher_encrypt_ablkcipher;
754 skcipher->decrypt = skcipher_decrypt_ablkcipher;
755
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;
760
761 return 0;
762 }
763
764 static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
765 {
766 struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
767 struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
768
769 alg->exit(skcipher);
770 }
771
772 static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
773 {
774 struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
775 struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
776
777 if (tfm->__crt_alg->cra_type == &crypto_blkcipher_type)
778 return crypto_init_skcipher_ops_blkcipher(tfm);
779
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);
783
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;
789
790 if (alg->exit)
791 skcipher->base.exit = crypto_skcipher_exit_tfm;
792
793 if (alg->init)
794 return alg->init(skcipher);
795
796 return 0;
797 }
798
799 static void crypto_skcipher_free_instance(struct crypto_instance *inst)
800 {
801 struct skcipher_instance *skcipher =
802 container_of(inst, struct skcipher_instance, s.base);
803
804 skcipher->free(skcipher);
805 }
806
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)
810 {
811 struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
812 base);
813
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);
822 }
823
824 #ifdef CONFIG_NET
825 static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
826 {
827 struct crypto_report_blkcipher rblkcipher;
828 struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
829 base);
830
831 strncpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type));
832 strncpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
833
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;
838
839 if (nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
840 sizeof(struct crypto_report_blkcipher), &rblkcipher))
841 goto nla_put_failure;
842 return 0;
843
844 nla_put_failure:
845 return -EMSGSIZE;
846 }
847 #else
848 static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
849 {
850 return -ENOSYS;
851 }
852 #endif
853
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,
860 #endif
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),
866 };
867
868 int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
869 const char *name, u32 type, u32 mask)
870 {
871 spawn->base.frontend = &crypto_skcipher_type2;
872 return crypto_grab_spawn(&spawn->base, name, type, mask);
873 }
874 EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
875
876 struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
877 u32 type, u32 mask)
878 {
879 return crypto_alloc_tfm(alg_name, &crypto_skcipher_type2, type, mask);
880 }
881 EXPORT_SYMBOL_GPL(crypto_alloc_skcipher);
882
883 int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask)
884 {
885 return crypto_type_has_alg(alg_name, &crypto_skcipher_type2,
886 type, mask);
887 }
888 EXPORT_SYMBOL_GPL(crypto_has_skcipher2);
889
890 static int skcipher_prepare_alg(struct skcipher_alg *alg)
891 {
892 struct crypto_alg *base = &alg->base;
893
894 if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8)
895 return -EINVAL;
896
897 if (!alg->chunksize)
898 alg->chunksize = base->cra_blocksize;
899
900 base->cra_type = &crypto_skcipher_type2;
901 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
902 base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER;
903
904 return 0;
905 }
906
907 int crypto_register_skcipher(struct skcipher_alg *alg)
908 {
909 struct crypto_alg *base = &alg->base;
910 int err;
911
912 err = skcipher_prepare_alg(alg);
913 if (err)
914 return err;
915
916 return crypto_register_alg(base);
917 }
918 EXPORT_SYMBOL_GPL(crypto_register_skcipher);
919
920 void crypto_unregister_skcipher(struct skcipher_alg *alg)
921 {
922 crypto_unregister_alg(&alg->base);
923 }
924 EXPORT_SYMBOL_GPL(crypto_unregister_skcipher);
925
926 int crypto_register_skciphers(struct skcipher_alg *algs, int count)
927 {
928 int i, ret;
929
930 for (i = 0; i < count; i++) {
931 ret = crypto_register_skcipher(&algs[i]);
932 if (ret)
933 goto err;
934 }
935
936 return 0;
937
938 err:
939 for (--i; i >= 0; --i)
940 crypto_unregister_skcipher(&algs[i]);
941
942 return ret;
943 }
944 EXPORT_SYMBOL_GPL(crypto_register_skciphers);
945
946 void crypto_unregister_skciphers(struct skcipher_alg *algs, int count)
947 {
948 int i;
949
950 for (i = count - 1; i >= 0; --i)
951 crypto_unregister_skcipher(&algs[i]);
952 }
953 EXPORT_SYMBOL_GPL(crypto_unregister_skciphers);
954
955 int skcipher_register_instance(struct crypto_template *tmpl,
956 struct skcipher_instance *inst)
957 {
958 int err;
959
960 err = skcipher_prepare_alg(&inst->alg);
961 if (err)
962 return err;
963
964 return crypto_register_instance(tmpl, skcipher_crypto_instance(inst));
965 }
966 EXPORT_SYMBOL_GPL(skcipher_register_instance);
967
968 MODULE_LICENSE("GPL");
969 MODULE_DESCRIPTION("Symmetric key cipher type");