blob: 0f3071991b13c2d6ead9d2870d3d9609aeb45950 [file] [log] [blame]
Herbert Xu7a7ffe62015-08-20 15:21:45 +08001/*
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
Herbert Xub286d8b2016-11-22 20:08:12 +080017#include <crypto/internal/aead.h>
Herbert Xu7a7ffe62015-08-20 15:21:45 +080018#include <crypto/internal/skcipher.h>
Herbert Xub286d8b2016-11-22 20:08:12 +080019#include <crypto/scatterwalk.h>
Herbert Xu7a7ffe62015-08-20 15:21:45 +080020#include <linux/bug.h>
Herbert Xu4e6c3df2016-07-12 13:17:31 +080021#include <linux/cryptouser.h>
Herbert Xub286d8b2016-11-22 20:08:12 +080022#include <linux/list.h>
Herbert Xu7a7ffe62015-08-20 15:21:45 +080023#include <linux/module.h>
Herbert Xu4e6c3df2016-07-12 13:17:31 +080024#include <linux/rtnetlink.h>
25#include <linux/seq_file.h>
26#include <net/netlink.h>
Herbert Xu7a7ffe62015-08-20 15:21:45 +080027
28#include "internal.h"
29
Herbert Xub286d8b2016-11-22 20:08:12 +080030enum {
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
38struct 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
46static int skcipher_walk_next(struct skcipher_walk *walk);
47
48static inline void skcipher_unmap(struct scatter_walk *walk, void *vaddr)
49{
50 if (PageHighMem(scatterwalk_page(walk)))
51 kunmap_atomic(vaddr);
52}
53
54static 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
62static inline void skcipher_map_src(struct skcipher_walk *walk)
63{
64 walk->src.virt.addr = skcipher_map(&walk->in);
65}
66
67static inline void skcipher_map_dst(struct skcipher_walk *walk)
68{
69 walk->dst.virt.addr = skcipher_map(&walk->out);
70}
71
72static inline void skcipher_unmap_src(struct skcipher_walk *walk)
73{
74 skcipher_unmap(&walk->in, walk->src.virt.addr);
75}
76
77static inline void skcipher_unmap_dst(struct skcipher_walk *walk)
78{
79 skcipher_unmap(&walk->out, walk->dst.virt.addr);
80}
81
82static 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 */
90static 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
97static 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
108int 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)))) {
122unmap_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
170out:
171 return err;
172}
173EXPORT_SYMBOL_GPL(skcipher_walk_done);
174
175void 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
197done:
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}
209EXPORT_SYMBOL_GPL(skcipher_walk_complete);
210
211static 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
218static 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
264ok:
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
277static 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
309static 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
335static 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
353slow_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
378set_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}
387EXPORT_SYMBOL_GPL(skcipher_walk_next);
388
389static 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
424static 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
447static 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
471int 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}
484EXPORT_SYMBOL_GPL(skcipher_walk_virt);
485
486void skcipher_walk_atomise(struct skcipher_walk *walk)
487{
488 walk->flags &= ~SKCIPHER_WALK_SLEEP;
489}
490EXPORT_SYMBOL_GPL(skcipher_walk_atomise);
491
492int 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}
501EXPORT_SYMBOL_GPL(skcipher_walk_async);
502
503int skcipher_walk_aead(struct skcipher_walk *walk, struct aead_request *req,
504 bool atomic)
505{
506 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
507 int err;
508
509 scatterwalk_start(&walk->in, req->src);
510 scatterwalk_start(&walk->out, req->dst);
511
512 scatterwalk_copychunks(NULL, &walk->in, req->assoclen, 2);
513 scatterwalk_copychunks(NULL, &walk->out, req->assoclen, 2);
514
515 walk->total = req->cryptlen;
516 walk->iv = req->iv;
517 walk->oiv = req->iv;
518
519 if (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP)
520 walk->flags |= SKCIPHER_WALK_SLEEP;
521 else
522 walk->flags &= ~SKCIPHER_WALK_SLEEP;
523
524 walk->blocksize = crypto_aead_blocksize(tfm);
525 walk->chunksize = crypto_aead_chunksize(tfm);
526 walk->ivsize = crypto_aead_ivsize(tfm);
527 walk->alignmask = crypto_aead_alignmask(tfm);
528
529 err = skcipher_walk_first(walk);
530
531 if (atomic)
532 walk->flags &= ~SKCIPHER_WALK_SLEEP;
533
534 return err;
535}
536EXPORT_SYMBOL_GPL(skcipher_walk_aead);
537
Herbert Xu7a7ffe62015-08-20 15:21:45 +0800538static unsigned int crypto_skcipher_extsize(struct crypto_alg *alg)
539{
540 if (alg->cra_type == &crypto_blkcipher_type)
541 return sizeof(struct crypto_blkcipher *);
542
Herbert Xu4e6c3df2016-07-12 13:17:31 +0800543 if (alg->cra_type == &crypto_ablkcipher_type ||
544 alg->cra_type == &crypto_givcipher_type)
545 return sizeof(struct crypto_ablkcipher *);
Herbert Xu7a7ffe62015-08-20 15:21:45 +0800546
Herbert Xu4e6c3df2016-07-12 13:17:31 +0800547 return crypto_alg_extsize(alg);
Herbert Xu7a7ffe62015-08-20 15:21:45 +0800548}
549
550static int skcipher_setkey_blkcipher(struct crypto_skcipher *tfm,
551 const u8 *key, unsigned int keylen)
552{
553 struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
554 struct crypto_blkcipher *blkcipher = *ctx;
555 int err;
556
557 crypto_blkcipher_clear_flags(blkcipher, ~0);
558 crypto_blkcipher_set_flags(blkcipher, crypto_skcipher_get_flags(tfm) &
559 CRYPTO_TFM_REQ_MASK);
560 err = crypto_blkcipher_setkey(blkcipher, key, keylen);
561 crypto_skcipher_set_flags(tfm, crypto_blkcipher_get_flags(blkcipher) &
562 CRYPTO_TFM_RES_MASK);
563
564 return err;
565}
566
567static int skcipher_crypt_blkcipher(struct skcipher_request *req,
568 int (*crypt)(struct blkcipher_desc *,
569 struct scatterlist *,
570 struct scatterlist *,
571 unsigned int))
572{
573 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
574 struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
575 struct blkcipher_desc desc = {
576 .tfm = *ctx,
577 .info = req->iv,
578 .flags = req->base.flags,
579 };
580
581
582 return crypt(&desc, req->dst, req->src, req->cryptlen);
583}
584
585static int skcipher_encrypt_blkcipher(struct skcipher_request *req)
586{
587 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
588 struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
589 struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
590
591 return skcipher_crypt_blkcipher(req, alg->encrypt);
592}
593
594static int skcipher_decrypt_blkcipher(struct skcipher_request *req)
595{
596 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
597 struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
598 struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
599
600 return skcipher_crypt_blkcipher(req, alg->decrypt);
601}
602
603static void crypto_exit_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
604{
605 struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
606
607 crypto_free_blkcipher(*ctx);
608}
609
Geliang Tangecdd6be2015-09-27 22:47:05 +0800610static int crypto_init_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
Herbert Xu7a7ffe62015-08-20 15:21:45 +0800611{
612 struct crypto_alg *calg = tfm->__crt_alg;
613 struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
614 struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
615 struct crypto_blkcipher *blkcipher;
616 struct crypto_tfm *btfm;
617
618 if (!crypto_mod_get(calg))
619 return -EAGAIN;
620
621 btfm = __crypto_alloc_tfm(calg, CRYPTO_ALG_TYPE_BLKCIPHER,
622 CRYPTO_ALG_TYPE_MASK);
623 if (IS_ERR(btfm)) {
624 crypto_mod_put(calg);
625 return PTR_ERR(btfm);
626 }
627
628 blkcipher = __crypto_blkcipher_cast(btfm);
629 *ctx = blkcipher;
630 tfm->exit = crypto_exit_skcipher_ops_blkcipher;
631
632 skcipher->setkey = skcipher_setkey_blkcipher;
633 skcipher->encrypt = skcipher_encrypt_blkcipher;
634 skcipher->decrypt = skcipher_decrypt_blkcipher;
635
636 skcipher->ivsize = crypto_blkcipher_ivsize(blkcipher);
Herbert Xu973fb3f2016-01-21 17:10:56 +0800637 skcipher->keysize = calg->cra_blkcipher.max_keysize;
Herbert Xu7a7ffe62015-08-20 15:21:45 +0800638
639 return 0;
640}
641
642static int skcipher_setkey_ablkcipher(struct crypto_skcipher *tfm,
643 const u8 *key, unsigned int keylen)
644{
645 struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
646 struct crypto_ablkcipher *ablkcipher = *ctx;
647 int err;
648
649 crypto_ablkcipher_clear_flags(ablkcipher, ~0);
650 crypto_ablkcipher_set_flags(ablkcipher,
651 crypto_skcipher_get_flags(tfm) &
652 CRYPTO_TFM_REQ_MASK);
653 err = crypto_ablkcipher_setkey(ablkcipher, key, keylen);
654 crypto_skcipher_set_flags(tfm,
655 crypto_ablkcipher_get_flags(ablkcipher) &
656 CRYPTO_TFM_RES_MASK);
657
658 return err;
659}
660
661static int skcipher_crypt_ablkcipher(struct skcipher_request *req,
662 int (*crypt)(struct ablkcipher_request *))
663{
664 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
665 struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
666 struct ablkcipher_request *subreq = skcipher_request_ctx(req);
667
668 ablkcipher_request_set_tfm(subreq, *ctx);
669 ablkcipher_request_set_callback(subreq, skcipher_request_flags(req),
670 req->base.complete, req->base.data);
671 ablkcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
672 req->iv);
673
674 return crypt(subreq);
675}
676
677static int skcipher_encrypt_ablkcipher(struct skcipher_request *req)
678{
679 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
680 struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
681 struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
682
683 return skcipher_crypt_ablkcipher(req, alg->encrypt);
684}
685
686static int skcipher_decrypt_ablkcipher(struct skcipher_request *req)
687{
688 struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
689 struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
690 struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
691
692 return skcipher_crypt_ablkcipher(req, alg->decrypt);
693}
694
695static void crypto_exit_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
696{
697 struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
698
699 crypto_free_ablkcipher(*ctx);
700}
701
Geliang Tangecdd6be2015-09-27 22:47:05 +0800702static int crypto_init_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
Herbert Xu7a7ffe62015-08-20 15:21:45 +0800703{
704 struct crypto_alg *calg = tfm->__crt_alg;
705 struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
706 struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
707 struct crypto_ablkcipher *ablkcipher;
708 struct crypto_tfm *abtfm;
709
710 if (!crypto_mod_get(calg))
711 return -EAGAIN;
712
713 abtfm = __crypto_alloc_tfm(calg, 0, 0);
714 if (IS_ERR(abtfm)) {
715 crypto_mod_put(calg);
716 return PTR_ERR(abtfm);
717 }
718
719 ablkcipher = __crypto_ablkcipher_cast(abtfm);
720 *ctx = ablkcipher;
721 tfm->exit = crypto_exit_skcipher_ops_ablkcipher;
722
723 skcipher->setkey = skcipher_setkey_ablkcipher;
724 skcipher->encrypt = skcipher_encrypt_ablkcipher;
725 skcipher->decrypt = skcipher_decrypt_ablkcipher;
726
727 skcipher->ivsize = crypto_ablkcipher_ivsize(ablkcipher);
728 skcipher->reqsize = crypto_ablkcipher_reqsize(ablkcipher) +
729 sizeof(struct ablkcipher_request);
Herbert Xu973fb3f2016-01-21 17:10:56 +0800730 skcipher->keysize = calg->cra_ablkcipher.max_keysize;
Herbert Xu7a7ffe62015-08-20 15:21:45 +0800731
732 return 0;
733}
734
Herbert Xu4e6c3df2016-07-12 13:17:31 +0800735static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
736{
737 struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
738 struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
739
740 alg->exit(skcipher);
741}
742
Herbert Xu7a7ffe62015-08-20 15:21:45 +0800743static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
744{
Herbert Xu4e6c3df2016-07-12 13:17:31 +0800745 struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
746 struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
747
Herbert Xu7a7ffe62015-08-20 15:21:45 +0800748 if (tfm->__crt_alg->cra_type == &crypto_blkcipher_type)
749 return crypto_init_skcipher_ops_blkcipher(tfm);
750
Herbert Xu4e6c3df2016-07-12 13:17:31 +0800751 if (tfm->__crt_alg->cra_type == &crypto_ablkcipher_type ||
752 tfm->__crt_alg->cra_type == &crypto_givcipher_type)
753 return crypto_init_skcipher_ops_ablkcipher(tfm);
Herbert Xu7a7ffe62015-08-20 15:21:45 +0800754
Herbert Xu4e6c3df2016-07-12 13:17:31 +0800755 skcipher->setkey = alg->setkey;
756 skcipher->encrypt = alg->encrypt;
757 skcipher->decrypt = alg->decrypt;
758 skcipher->ivsize = alg->ivsize;
759 skcipher->keysize = alg->max_keysize;
760
761 if (alg->exit)
762 skcipher->base.exit = crypto_skcipher_exit_tfm;
763
764 if (alg->init)
765 return alg->init(skcipher);
766
767 return 0;
Herbert Xu7a7ffe62015-08-20 15:21:45 +0800768}
769
Herbert Xu4e6c3df2016-07-12 13:17:31 +0800770static void crypto_skcipher_free_instance(struct crypto_instance *inst)
771{
772 struct skcipher_instance *skcipher =
773 container_of(inst, struct skcipher_instance, s.base);
774
775 skcipher->free(skcipher);
776}
777
778static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
779 __attribute__ ((unused));
780static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
781{
782 struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
783 base);
784
785 seq_printf(m, "type : skcipher\n");
786 seq_printf(m, "async : %s\n",
787 alg->cra_flags & CRYPTO_ALG_ASYNC ? "yes" : "no");
788 seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
789 seq_printf(m, "min keysize : %u\n", skcipher->min_keysize);
790 seq_printf(m, "max keysize : %u\n", skcipher->max_keysize);
791 seq_printf(m, "ivsize : %u\n", skcipher->ivsize);
792 seq_printf(m, "chunksize : %u\n", skcipher->chunksize);
793}
794
795#ifdef CONFIG_NET
796static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
797{
798 struct crypto_report_blkcipher rblkcipher;
799 struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
800 base);
801
802 strncpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type));
803 strncpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
804
805 rblkcipher.blocksize = alg->cra_blocksize;
806 rblkcipher.min_keysize = skcipher->min_keysize;
807 rblkcipher.max_keysize = skcipher->max_keysize;
808 rblkcipher.ivsize = skcipher->ivsize;
809
810 if (nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
811 sizeof(struct crypto_report_blkcipher), &rblkcipher))
812 goto nla_put_failure;
813 return 0;
814
815nla_put_failure:
816 return -EMSGSIZE;
817}
818#else
819static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
820{
821 return -ENOSYS;
822}
823#endif
824
Herbert Xu7a7ffe62015-08-20 15:21:45 +0800825static const struct crypto_type crypto_skcipher_type2 = {
826 .extsize = crypto_skcipher_extsize,
827 .init_tfm = crypto_skcipher_init_tfm,
Herbert Xu4e6c3df2016-07-12 13:17:31 +0800828 .free = crypto_skcipher_free_instance,
829#ifdef CONFIG_PROC_FS
830 .show = crypto_skcipher_show,
831#endif
832 .report = crypto_skcipher_report,
Herbert Xu7a7ffe62015-08-20 15:21:45 +0800833 .maskclear = ~CRYPTO_ALG_TYPE_MASK,
834 .maskset = CRYPTO_ALG_TYPE_BLKCIPHER_MASK,
Herbert Xu4e6c3df2016-07-12 13:17:31 +0800835 .type = CRYPTO_ALG_TYPE_SKCIPHER,
Herbert Xu7a7ffe62015-08-20 15:21:45 +0800836 .tfmsize = offsetof(struct crypto_skcipher, base),
837};
838
Herbert Xu3a01d0e2016-07-12 13:17:50 +0800839int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
Herbert Xu4e6c3df2016-07-12 13:17:31 +0800840 const char *name, u32 type, u32 mask)
841{
842 spawn->base.frontend = &crypto_skcipher_type2;
843 return crypto_grab_spawn(&spawn->base, name, type, mask);
844}
Herbert Xu3a01d0e2016-07-12 13:17:50 +0800845EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
Herbert Xu4e6c3df2016-07-12 13:17:31 +0800846
Herbert Xu7a7ffe62015-08-20 15:21:45 +0800847struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
848 u32 type, u32 mask)
849{
850 return crypto_alloc_tfm(alg_name, &crypto_skcipher_type2, type, mask);
851}
852EXPORT_SYMBOL_GPL(crypto_alloc_skcipher);
853
Herbert Xu4e6c3df2016-07-12 13:17:31 +0800854int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask)
855{
856 return crypto_type_has_alg(alg_name, &crypto_skcipher_type2,
857 type, mask);
858}
859EXPORT_SYMBOL_GPL(crypto_has_skcipher2);
860
861static int skcipher_prepare_alg(struct skcipher_alg *alg)
862{
863 struct crypto_alg *base = &alg->base;
864
865 if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8)
866 return -EINVAL;
867
868 if (!alg->chunksize)
869 alg->chunksize = base->cra_blocksize;
870
871 base->cra_type = &crypto_skcipher_type2;
872 base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
873 base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER;
874
875 return 0;
876}
877
878int crypto_register_skcipher(struct skcipher_alg *alg)
879{
880 struct crypto_alg *base = &alg->base;
881 int err;
882
883 err = skcipher_prepare_alg(alg);
884 if (err)
885 return err;
886
887 return crypto_register_alg(base);
888}
889EXPORT_SYMBOL_GPL(crypto_register_skcipher);
890
891void crypto_unregister_skcipher(struct skcipher_alg *alg)
892{
893 crypto_unregister_alg(&alg->base);
894}
895EXPORT_SYMBOL_GPL(crypto_unregister_skcipher);
896
897int crypto_register_skciphers(struct skcipher_alg *algs, int count)
898{
899 int i, ret;
900
901 for (i = 0; i < count; i++) {
902 ret = crypto_register_skcipher(&algs[i]);
903 if (ret)
904 goto err;
905 }
906
907 return 0;
908
909err:
910 for (--i; i >= 0; --i)
911 crypto_unregister_skcipher(&algs[i]);
912
913 return ret;
914}
915EXPORT_SYMBOL_GPL(crypto_register_skciphers);
916
917void crypto_unregister_skciphers(struct skcipher_alg *algs, int count)
918{
919 int i;
920
921 for (i = count - 1; i >= 0; --i)
922 crypto_unregister_skcipher(&algs[i]);
923}
924EXPORT_SYMBOL_GPL(crypto_unregister_skciphers);
925
926int skcipher_register_instance(struct crypto_template *tmpl,
927 struct skcipher_instance *inst)
928{
929 int err;
930
931 err = skcipher_prepare_alg(&inst->alg);
932 if (err)
933 return err;
934
935 return crypto_register_instance(tmpl, skcipher_crypto_instance(inst));
936}
937EXPORT_SYMBOL_GPL(skcipher_register_instance);
938
Herbert Xu7a7ffe62015-08-20 15:21:45 +0800939MODULE_LICENSE("GPL");
940MODULE_DESCRIPTION("Symmetric key cipher type");