xref: /linux/crypto/skcipher.c (revision b60a5b8dcf49af9f2c60ae82e0383ee8e62a9a52)
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/compiler.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/rtnetlink.h>
26 #include <linux/seq_file.h>
27 #include <net/netlink.h>
28 
29 #include "internal.h"
30 
31 enum {
32 	SKCIPHER_WALK_PHYS = 1 << 0,
33 	SKCIPHER_WALK_SLOW = 1 << 1,
34 	SKCIPHER_WALK_COPY = 1 << 2,
35 	SKCIPHER_WALK_DIFF = 1 << 3,
36 	SKCIPHER_WALK_SLEEP = 1 << 4,
37 };
38 
39 struct skcipher_walk_buffer {
40 	struct list_head entry;
41 	struct scatter_walk dst;
42 	unsigned int len;
43 	u8 *data;
44 	u8 buffer[];
45 };
46 
47 static int skcipher_walk_next(struct skcipher_walk *walk);
48 
49 static inline void skcipher_unmap(struct scatter_walk *walk, void *vaddr)
50 {
51 	if (PageHighMem(scatterwalk_page(walk)))
52 		kunmap_atomic(vaddr);
53 }
54 
55 static inline void *skcipher_map(struct scatter_walk *walk)
56 {
57 	struct page *page = scatterwalk_page(walk);
58 
59 	return (PageHighMem(page) ? kmap_atomic(page) : page_address(page)) +
60 	       offset_in_page(walk->offset);
61 }
62 
63 static inline void skcipher_map_src(struct skcipher_walk *walk)
64 {
65 	walk->src.virt.addr = skcipher_map(&walk->in);
66 }
67 
68 static inline void skcipher_map_dst(struct skcipher_walk *walk)
69 {
70 	walk->dst.virt.addr = skcipher_map(&walk->out);
71 }
72 
73 static inline void skcipher_unmap_src(struct skcipher_walk *walk)
74 {
75 	skcipher_unmap(&walk->in, walk->src.virt.addr);
76 }
77 
78 static inline void skcipher_unmap_dst(struct skcipher_walk *walk)
79 {
80 	skcipher_unmap(&walk->out, walk->dst.virt.addr);
81 }
82 
83 static inline gfp_t skcipher_walk_gfp(struct skcipher_walk *walk)
84 {
85 	return walk->flags & SKCIPHER_WALK_SLEEP ? GFP_KERNEL : GFP_ATOMIC;
86 }
87 
88 /* Get a spot of the specified length that does not straddle a page.
89  * The caller needs to ensure that there is enough space for this operation.
90  */
91 static inline u8 *skcipher_get_spot(u8 *start, unsigned int len)
92 {
93 	u8 *end_page = (u8 *)(((unsigned long)(start + len - 1)) & PAGE_MASK);
94 
95 	return max(start, end_page);
96 }
97 
98 static void skcipher_done_slow(struct skcipher_walk *walk, unsigned int bsize)
99 {
100 	u8 *addr;
101 
102 	addr = (u8 *)ALIGN((unsigned long)walk->buffer, walk->alignmask + 1);
103 	addr = skcipher_get_spot(addr, bsize);
104 	scatterwalk_copychunks(addr, &walk->out, bsize,
105 			       (walk->flags & SKCIPHER_WALK_PHYS) ? 2 : 1);
106 }
107 
108 int skcipher_walk_done(struct skcipher_walk *walk, int err)
109 {
110 	unsigned int n; /* bytes processed */
111 	bool more;
112 
113 	if (unlikely(err < 0))
114 		goto finish;
115 
116 	n = walk->nbytes - err;
117 	walk->total -= n;
118 	more = (walk->total != 0);
119 
120 	if (likely(!(walk->flags & (SKCIPHER_WALK_PHYS |
121 				    SKCIPHER_WALK_SLOW |
122 				    SKCIPHER_WALK_COPY |
123 				    SKCIPHER_WALK_DIFF)))) {
124 unmap_src:
125 		skcipher_unmap_src(walk);
126 	} else if (walk->flags & SKCIPHER_WALK_DIFF) {
127 		skcipher_unmap_dst(walk);
128 		goto unmap_src;
129 	} else if (walk->flags & SKCIPHER_WALK_COPY) {
130 		skcipher_map_dst(walk);
131 		memcpy(walk->dst.virt.addr, walk->page, n);
132 		skcipher_unmap_dst(walk);
133 	} else if (unlikely(walk->flags & SKCIPHER_WALK_SLOW)) {
134 		if (WARN_ON(err)) {
135 			/* unexpected case; didn't process all bytes */
136 			err = -EINVAL;
137 			goto finish;
138 		}
139 		skcipher_done_slow(walk, n);
140 		goto already_advanced;
141 	}
142 
143 	scatterwalk_advance(&walk->in, n);
144 	scatterwalk_advance(&walk->out, n);
145 already_advanced:
146 	scatterwalk_done(&walk->in, 0, more);
147 	scatterwalk_done(&walk->out, 1, more);
148 
149 	if (more) {
150 		crypto_yield(walk->flags & SKCIPHER_WALK_SLEEP ?
151 			     CRYPTO_TFM_REQ_MAY_SLEEP : 0);
152 		return skcipher_walk_next(walk);
153 	}
154 	err = 0;
155 finish:
156 	walk->nbytes = 0;
157 
158 	/* Short-circuit for the common/fast path. */
159 	if (!((unsigned long)walk->buffer | (unsigned long)walk->page))
160 		goto out;
161 
162 	if (walk->flags & SKCIPHER_WALK_PHYS)
163 		goto out;
164 
165 	if (walk->iv != walk->oiv)
166 		memcpy(walk->oiv, walk->iv, walk->ivsize);
167 	if (walk->buffer != walk->page)
168 		kfree(walk->buffer);
169 	if (walk->page)
170 		free_page((unsigned long)walk->page);
171 
172 out:
173 	return err;
174 }
175 EXPORT_SYMBOL_GPL(skcipher_walk_done);
176 
177 void skcipher_walk_complete(struct skcipher_walk *walk, int err)
178 {
179 	struct skcipher_walk_buffer *p, *tmp;
180 
181 	list_for_each_entry_safe(p, tmp, &walk->buffers, entry) {
182 		u8 *data;
183 
184 		if (err)
185 			goto done;
186 
187 		data = p->data;
188 		if (!data) {
189 			data = PTR_ALIGN(&p->buffer[0], walk->alignmask + 1);
190 			data = skcipher_get_spot(data, walk->stride);
191 		}
192 
193 		scatterwalk_copychunks(data, &p->dst, p->len, 1);
194 
195 		if (offset_in_page(p->data) + p->len + walk->stride >
196 		    PAGE_SIZE)
197 			free_page((unsigned long)p->data);
198 
199 done:
200 		list_del(&p->entry);
201 		kfree(p);
202 	}
203 
204 	if (!err && walk->iv != walk->oiv)
205 		memcpy(walk->oiv, walk->iv, walk->ivsize);
206 	if (walk->buffer != walk->page)
207 		kfree(walk->buffer);
208 	if (walk->page)
209 		free_page((unsigned long)walk->page);
210 }
211 EXPORT_SYMBOL_GPL(skcipher_walk_complete);
212 
213 static void skcipher_queue_write(struct skcipher_walk *walk,
214 				 struct skcipher_walk_buffer *p)
215 {
216 	p->dst = walk->out;
217 	list_add_tail(&p->entry, &walk->buffers);
218 }
219 
220 static int skcipher_next_slow(struct skcipher_walk *walk, unsigned int bsize)
221 {
222 	bool phys = walk->flags & SKCIPHER_WALK_PHYS;
223 	unsigned alignmask = walk->alignmask;
224 	struct skcipher_walk_buffer *p;
225 	unsigned a;
226 	unsigned n;
227 	u8 *buffer;
228 	void *v;
229 
230 	if (!phys) {
231 		if (!walk->buffer)
232 			walk->buffer = walk->page;
233 		buffer = walk->buffer;
234 		if (buffer)
235 			goto ok;
236 	}
237 
238 	/* Start with the minimum alignment of kmalloc. */
239 	a = crypto_tfm_ctx_alignment() - 1;
240 	n = bsize;
241 
242 	if (phys) {
243 		/* Calculate the minimum alignment of p->buffer. */
244 		a &= (sizeof(*p) ^ (sizeof(*p) - 1)) >> 1;
245 		n += sizeof(*p);
246 	}
247 
248 	/* Minimum size to align p->buffer by alignmask. */
249 	n += alignmask & ~a;
250 
251 	/* Minimum size to ensure p->buffer does not straddle a page. */
252 	n += (bsize - 1) & ~(alignmask | a);
253 
254 	v = kzalloc(n, skcipher_walk_gfp(walk));
255 	if (!v)
256 		return skcipher_walk_done(walk, -ENOMEM);
257 
258 	if (phys) {
259 		p = v;
260 		p->len = bsize;
261 		skcipher_queue_write(walk, p);
262 		buffer = p->buffer;
263 	} else {
264 		walk->buffer = v;
265 		buffer = v;
266 	}
267 
268 ok:
269 	walk->dst.virt.addr = PTR_ALIGN(buffer, alignmask + 1);
270 	walk->dst.virt.addr = skcipher_get_spot(walk->dst.virt.addr, bsize);
271 	walk->src.virt.addr = walk->dst.virt.addr;
272 
273 	scatterwalk_copychunks(walk->src.virt.addr, &walk->in, bsize, 0);
274 
275 	walk->nbytes = bsize;
276 	walk->flags |= SKCIPHER_WALK_SLOW;
277 
278 	return 0;
279 }
280 
281 static int skcipher_next_copy(struct skcipher_walk *walk)
282 {
283 	struct skcipher_walk_buffer *p;
284 	u8 *tmp = walk->page;
285 
286 	skcipher_map_src(walk);
287 	memcpy(tmp, walk->src.virt.addr, walk->nbytes);
288 	skcipher_unmap_src(walk);
289 
290 	walk->src.virt.addr = tmp;
291 	walk->dst.virt.addr = tmp;
292 
293 	if (!(walk->flags & SKCIPHER_WALK_PHYS))
294 		return 0;
295 
296 	p = kmalloc(sizeof(*p), skcipher_walk_gfp(walk));
297 	if (!p)
298 		return -ENOMEM;
299 
300 	p->data = walk->page;
301 	p->len = walk->nbytes;
302 	skcipher_queue_write(walk, p);
303 
304 	if (offset_in_page(walk->page) + walk->nbytes + walk->stride >
305 	    PAGE_SIZE)
306 		walk->page = NULL;
307 	else
308 		walk->page += walk->nbytes;
309 
310 	return 0;
311 }
312 
313 static int skcipher_next_fast(struct skcipher_walk *walk)
314 {
315 	unsigned long diff;
316 
317 	walk->src.phys.page = scatterwalk_page(&walk->in);
318 	walk->src.phys.offset = offset_in_page(walk->in.offset);
319 	walk->dst.phys.page = scatterwalk_page(&walk->out);
320 	walk->dst.phys.offset = offset_in_page(walk->out.offset);
321 
322 	if (walk->flags & SKCIPHER_WALK_PHYS)
323 		return 0;
324 
325 	diff = walk->src.phys.offset - walk->dst.phys.offset;
326 	diff |= walk->src.virt.page - walk->dst.virt.page;
327 
328 	skcipher_map_src(walk);
329 	walk->dst.virt.addr = walk->src.virt.addr;
330 
331 	if (diff) {
332 		walk->flags |= SKCIPHER_WALK_DIFF;
333 		skcipher_map_dst(walk);
334 	}
335 
336 	return 0;
337 }
338 
339 static int skcipher_walk_next(struct skcipher_walk *walk)
340 {
341 	unsigned int bsize;
342 	unsigned int n;
343 	int err;
344 
345 	walk->flags &= ~(SKCIPHER_WALK_SLOW | SKCIPHER_WALK_COPY |
346 			 SKCIPHER_WALK_DIFF);
347 
348 	n = walk->total;
349 	bsize = min(walk->stride, max(n, walk->blocksize));
350 	n = scatterwalk_clamp(&walk->in, n);
351 	n = scatterwalk_clamp(&walk->out, n);
352 
353 	if (unlikely(n < bsize)) {
354 		if (unlikely(walk->total < walk->blocksize))
355 			return skcipher_walk_done(walk, -EINVAL);
356 
357 slow_path:
358 		err = skcipher_next_slow(walk, bsize);
359 		goto set_phys_lowmem;
360 	}
361 
362 	if (unlikely((walk->in.offset | walk->out.offset) & walk->alignmask)) {
363 		if (!walk->page) {
364 			gfp_t gfp = skcipher_walk_gfp(walk);
365 
366 			walk->page = (void *)__get_free_page(gfp);
367 			if (!walk->page)
368 				goto slow_path;
369 		}
370 
371 		walk->nbytes = min_t(unsigned, n,
372 				     PAGE_SIZE - offset_in_page(walk->page));
373 		walk->flags |= SKCIPHER_WALK_COPY;
374 		err = skcipher_next_copy(walk);
375 		goto set_phys_lowmem;
376 	}
377 
378 	walk->nbytes = n;
379 
380 	return skcipher_next_fast(walk);
381 
382 set_phys_lowmem:
383 	if (!err && (walk->flags & SKCIPHER_WALK_PHYS)) {
384 		walk->src.phys.page = virt_to_page(walk->src.virt.addr);
385 		walk->dst.phys.page = virt_to_page(walk->dst.virt.addr);
386 		walk->src.phys.offset &= PAGE_SIZE - 1;
387 		walk->dst.phys.offset &= PAGE_SIZE - 1;
388 	}
389 	return err;
390 }
391 
392 static int skcipher_copy_iv(struct skcipher_walk *walk)
393 {
394 	unsigned a = crypto_tfm_ctx_alignment() - 1;
395 	unsigned alignmask = walk->alignmask;
396 	unsigned ivsize = walk->ivsize;
397 	unsigned bs = walk->stride;
398 	unsigned aligned_bs;
399 	unsigned size;
400 	u8 *iv;
401 
402 	aligned_bs = ALIGN(bs, alignmask + 1);
403 
404 	/* Minimum size to align buffer by alignmask. */
405 	size = alignmask & ~a;
406 
407 	if (walk->flags & SKCIPHER_WALK_PHYS)
408 		size += ivsize;
409 	else {
410 		size += aligned_bs + ivsize;
411 
412 		/* Minimum size to ensure buffer does not straddle a page. */
413 		size += (bs - 1) & ~(alignmask | a);
414 	}
415 
416 	walk->buffer = kmalloc(size, skcipher_walk_gfp(walk));
417 	if (!walk->buffer)
418 		return -ENOMEM;
419 
420 	iv = PTR_ALIGN(walk->buffer, alignmask + 1);
421 	iv = skcipher_get_spot(iv, bs) + aligned_bs;
422 
423 	walk->iv = memcpy(iv, walk->iv, walk->ivsize);
424 	return 0;
425 }
426 
427 static int skcipher_walk_first(struct skcipher_walk *walk)
428 {
429 	if (WARN_ON_ONCE(in_irq()))
430 		return -EDEADLK;
431 
432 	walk->buffer = NULL;
433 	if (unlikely(((unsigned long)walk->iv & walk->alignmask))) {
434 		int err = skcipher_copy_iv(walk);
435 		if (err)
436 			return err;
437 	}
438 
439 	walk->page = NULL;
440 
441 	return skcipher_walk_next(walk);
442 }
443 
444 static int skcipher_walk_skcipher(struct skcipher_walk *walk,
445 				  struct skcipher_request *req)
446 {
447 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
448 
449 	walk->total = req->cryptlen;
450 	walk->nbytes = 0;
451 	walk->iv = req->iv;
452 	walk->oiv = req->iv;
453 
454 	if (unlikely(!walk->total))
455 		return 0;
456 
457 	scatterwalk_start(&walk->in, req->src);
458 	scatterwalk_start(&walk->out, req->dst);
459 
460 	walk->flags &= ~SKCIPHER_WALK_SLEEP;
461 	walk->flags |= req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
462 		       SKCIPHER_WALK_SLEEP : 0;
463 
464 	walk->blocksize = crypto_skcipher_blocksize(tfm);
465 	walk->stride = crypto_skcipher_walksize(tfm);
466 	walk->ivsize = crypto_skcipher_ivsize(tfm);
467 	walk->alignmask = crypto_skcipher_alignmask(tfm);
468 
469 	return skcipher_walk_first(walk);
470 }
471 
472 int skcipher_walk_virt(struct skcipher_walk *walk,
473 		       struct skcipher_request *req, bool atomic)
474 {
475 	int err;
476 
477 	might_sleep_if(req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
478 
479 	walk->flags &= ~SKCIPHER_WALK_PHYS;
480 
481 	err = skcipher_walk_skcipher(walk, req);
482 
483 	walk->flags &= atomic ? ~SKCIPHER_WALK_SLEEP : ~0;
484 
485 	return err;
486 }
487 EXPORT_SYMBOL_GPL(skcipher_walk_virt);
488 
489 void skcipher_walk_atomise(struct skcipher_walk *walk)
490 {
491 	walk->flags &= ~SKCIPHER_WALK_SLEEP;
492 }
493 EXPORT_SYMBOL_GPL(skcipher_walk_atomise);
494 
495 int skcipher_walk_async(struct skcipher_walk *walk,
496 			struct skcipher_request *req)
497 {
498 	walk->flags |= SKCIPHER_WALK_PHYS;
499 
500 	INIT_LIST_HEAD(&walk->buffers);
501 
502 	return skcipher_walk_skcipher(walk, req);
503 }
504 EXPORT_SYMBOL_GPL(skcipher_walk_async);
505 
506 static int skcipher_walk_aead_common(struct skcipher_walk *walk,
507 				     struct aead_request *req, bool atomic)
508 {
509 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
510 	int err;
511 
512 	walk->nbytes = 0;
513 	walk->iv = req->iv;
514 	walk->oiv = req->iv;
515 
516 	if (unlikely(!walk->total))
517 		return 0;
518 
519 	walk->flags &= ~SKCIPHER_WALK_PHYS;
520 
521 	scatterwalk_start(&walk->in, req->src);
522 	scatterwalk_start(&walk->out, req->dst);
523 
524 	scatterwalk_copychunks(NULL, &walk->in, req->assoclen, 2);
525 	scatterwalk_copychunks(NULL, &walk->out, req->assoclen, 2);
526 
527 	scatterwalk_done(&walk->in, 0, walk->total);
528 	scatterwalk_done(&walk->out, 0, walk->total);
529 
530 	if (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP)
531 		walk->flags |= SKCIPHER_WALK_SLEEP;
532 	else
533 		walk->flags &= ~SKCIPHER_WALK_SLEEP;
534 
535 	walk->blocksize = crypto_aead_blocksize(tfm);
536 	walk->stride = crypto_aead_chunksize(tfm);
537 	walk->ivsize = crypto_aead_ivsize(tfm);
538 	walk->alignmask = crypto_aead_alignmask(tfm);
539 
540 	err = skcipher_walk_first(walk);
541 
542 	if (atomic)
543 		walk->flags &= ~SKCIPHER_WALK_SLEEP;
544 
545 	return err;
546 }
547 
548 int skcipher_walk_aead(struct skcipher_walk *walk, struct aead_request *req,
549 		       bool atomic)
550 {
551 	walk->total = req->cryptlen;
552 
553 	return skcipher_walk_aead_common(walk, req, atomic);
554 }
555 EXPORT_SYMBOL_GPL(skcipher_walk_aead);
556 
557 int skcipher_walk_aead_encrypt(struct skcipher_walk *walk,
558 			       struct aead_request *req, bool atomic)
559 {
560 	walk->total = req->cryptlen;
561 
562 	return skcipher_walk_aead_common(walk, req, atomic);
563 }
564 EXPORT_SYMBOL_GPL(skcipher_walk_aead_encrypt);
565 
566 int skcipher_walk_aead_decrypt(struct skcipher_walk *walk,
567 			       struct aead_request *req, bool atomic)
568 {
569 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
570 
571 	walk->total = req->cryptlen - crypto_aead_authsize(tfm);
572 
573 	return skcipher_walk_aead_common(walk, req, atomic);
574 }
575 EXPORT_SYMBOL_GPL(skcipher_walk_aead_decrypt);
576 
577 static unsigned int crypto_skcipher_extsize(struct crypto_alg *alg)
578 {
579 	if (alg->cra_type == &crypto_blkcipher_type)
580 		return sizeof(struct crypto_blkcipher *);
581 
582 	if (alg->cra_type == &crypto_ablkcipher_type)
583 		return sizeof(struct crypto_ablkcipher *);
584 
585 	return crypto_alg_extsize(alg);
586 }
587 
588 static void skcipher_set_needkey(struct crypto_skcipher *tfm)
589 {
590 	if (tfm->keysize)
591 		crypto_skcipher_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
592 }
593 
594 static int skcipher_setkey_blkcipher(struct crypto_skcipher *tfm,
595 				     const u8 *key, unsigned int keylen)
596 {
597 	struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
598 	struct crypto_blkcipher *blkcipher = *ctx;
599 	int err;
600 
601 	crypto_blkcipher_clear_flags(blkcipher, ~0);
602 	crypto_blkcipher_set_flags(blkcipher, crypto_skcipher_get_flags(tfm) &
603 					      CRYPTO_TFM_REQ_MASK);
604 	err = crypto_blkcipher_setkey(blkcipher, key, keylen);
605 	crypto_skcipher_set_flags(tfm, crypto_blkcipher_get_flags(blkcipher) &
606 				       CRYPTO_TFM_RES_MASK);
607 	if (unlikely(err)) {
608 		skcipher_set_needkey(tfm);
609 		return err;
610 	}
611 
612 	crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
613 	return 0;
614 }
615 
616 static int skcipher_crypt_blkcipher(struct skcipher_request *req,
617 				    int (*crypt)(struct blkcipher_desc *,
618 						 struct scatterlist *,
619 						 struct scatterlist *,
620 						 unsigned int))
621 {
622 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
623 	struct crypto_blkcipher **ctx = crypto_skcipher_ctx(tfm);
624 	struct blkcipher_desc desc = {
625 		.tfm = *ctx,
626 		.info = req->iv,
627 		.flags = req->base.flags,
628 	};
629 
630 
631 	return crypt(&desc, req->dst, req->src, req->cryptlen);
632 }
633 
634 static int skcipher_encrypt_blkcipher(struct skcipher_request *req)
635 {
636 	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
637 	struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
638 	struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
639 
640 	return skcipher_crypt_blkcipher(req, alg->encrypt);
641 }
642 
643 static int skcipher_decrypt_blkcipher(struct skcipher_request *req)
644 {
645 	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
646 	struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
647 	struct blkcipher_alg *alg = &tfm->__crt_alg->cra_blkcipher;
648 
649 	return skcipher_crypt_blkcipher(req, alg->decrypt);
650 }
651 
652 static void crypto_exit_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
653 {
654 	struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
655 
656 	crypto_free_blkcipher(*ctx);
657 }
658 
659 static int crypto_init_skcipher_ops_blkcipher(struct crypto_tfm *tfm)
660 {
661 	struct crypto_alg *calg = tfm->__crt_alg;
662 	struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
663 	struct crypto_blkcipher **ctx = crypto_tfm_ctx(tfm);
664 	struct crypto_blkcipher *blkcipher;
665 	struct crypto_tfm *btfm;
666 
667 	if (!crypto_mod_get(calg))
668 		return -EAGAIN;
669 
670 	btfm = __crypto_alloc_tfm(calg, CRYPTO_ALG_TYPE_BLKCIPHER,
671 					CRYPTO_ALG_TYPE_MASK);
672 	if (IS_ERR(btfm)) {
673 		crypto_mod_put(calg);
674 		return PTR_ERR(btfm);
675 	}
676 
677 	blkcipher = __crypto_blkcipher_cast(btfm);
678 	*ctx = blkcipher;
679 	tfm->exit = crypto_exit_skcipher_ops_blkcipher;
680 
681 	skcipher->setkey = skcipher_setkey_blkcipher;
682 	skcipher->encrypt = skcipher_encrypt_blkcipher;
683 	skcipher->decrypt = skcipher_decrypt_blkcipher;
684 
685 	skcipher->ivsize = crypto_blkcipher_ivsize(blkcipher);
686 	skcipher->keysize = calg->cra_blkcipher.max_keysize;
687 
688 	skcipher_set_needkey(skcipher);
689 
690 	return 0;
691 }
692 
693 static int skcipher_setkey_ablkcipher(struct crypto_skcipher *tfm,
694 				      const u8 *key, unsigned int keylen)
695 {
696 	struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
697 	struct crypto_ablkcipher *ablkcipher = *ctx;
698 	int err;
699 
700 	crypto_ablkcipher_clear_flags(ablkcipher, ~0);
701 	crypto_ablkcipher_set_flags(ablkcipher,
702 				    crypto_skcipher_get_flags(tfm) &
703 				    CRYPTO_TFM_REQ_MASK);
704 	err = crypto_ablkcipher_setkey(ablkcipher, key, keylen);
705 	crypto_skcipher_set_flags(tfm,
706 				  crypto_ablkcipher_get_flags(ablkcipher) &
707 				  CRYPTO_TFM_RES_MASK);
708 	if (unlikely(err)) {
709 		skcipher_set_needkey(tfm);
710 		return err;
711 	}
712 
713 	crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
714 	return 0;
715 }
716 
717 static int skcipher_crypt_ablkcipher(struct skcipher_request *req,
718 				     int (*crypt)(struct ablkcipher_request *))
719 {
720 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
721 	struct crypto_ablkcipher **ctx = crypto_skcipher_ctx(tfm);
722 	struct ablkcipher_request *subreq = skcipher_request_ctx(req);
723 
724 	ablkcipher_request_set_tfm(subreq, *ctx);
725 	ablkcipher_request_set_callback(subreq, skcipher_request_flags(req),
726 					req->base.complete, req->base.data);
727 	ablkcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
728 				     req->iv);
729 
730 	return crypt(subreq);
731 }
732 
733 static int skcipher_encrypt_ablkcipher(struct skcipher_request *req)
734 {
735 	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
736 	struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
737 	struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
738 
739 	return skcipher_crypt_ablkcipher(req, alg->encrypt);
740 }
741 
742 static int skcipher_decrypt_ablkcipher(struct skcipher_request *req)
743 {
744 	struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req);
745 	struct crypto_tfm *tfm = crypto_skcipher_tfm(skcipher);
746 	struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
747 
748 	return skcipher_crypt_ablkcipher(req, alg->decrypt);
749 }
750 
751 static void crypto_exit_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
752 {
753 	struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
754 
755 	crypto_free_ablkcipher(*ctx);
756 }
757 
758 static int crypto_init_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
759 {
760 	struct crypto_alg *calg = tfm->__crt_alg;
761 	struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
762 	struct crypto_ablkcipher **ctx = crypto_tfm_ctx(tfm);
763 	struct crypto_ablkcipher *ablkcipher;
764 	struct crypto_tfm *abtfm;
765 
766 	if (!crypto_mod_get(calg))
767 		return -EAGAIN;
768 
769 	abtfm = __crypto_alloc_tfm(calg, 0, 0);
770 	if (IS_ERR(abtfm)) {
771 		crypto_mod_put(calg);
772 		return PTR_ERR(abtfm);
773 	}
774 
775 	ablkcipher = __crypto_ablkcipher_cast(abtfm);
776 	*ctx = ablkcipher;
777 	tfm->exit = crypto_exit_skcipher_ops_ablkcipher;
778 
779 	skcipher->setkey = skcipher_setkey_ablkcipher;
780 	skcipher->encrypt = skcipher_encrypt_ablkcipher;
781 	skcipher->decrypt = skcipher_decrypt_ablkcipher;
782 
783 	skcipher->ivsize = crypto_ablkcipher_ivsize(ablkcipher);
784 	skcipher->reqsize = crypto_ablkcipher_reqsize(ablkcipher) +
785 			    sizeof(struct ablkcipher_request);
786 	skcipher->keysize = calg->cra_ablkcipher.max_keysize;
787 
788 	skcipher_set_needkey(skcipher);
789 
790 	return 0;
791 }
792 
793 static int skcipher_setkey_unaligned(struct crypto_skcipher *tfm,
794 				     const u8 *key, unsigned int keylen)
795 {
796 	unsigned long alignmask = crypto_skcipher_alignmask(tfm);
797 	struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
798 	u8 *buffer, *alignbuffer;
799 	unsigned long absize;
800 	int ret;
801 
802 	absize = keylen + alignmask;
803 	buffer = kmalloc(absize, GFP_ATOMIC);
804 	if (!buffer)
805 		return -ENOMEM;
806 
807 	alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
808 	memcpy(alignbuffer, key, keylen);
809 	ret = cipher->setkey(tfm, alignbuffer, keylen);
810 	kzfree(buffer);
811 	return ret;
812 }
813 
814 static int skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
815 			   unsigned int keylen)
816 {
817 	struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
818 	unsigned long alignmask = crypto_skcipher_alignmask(tfm);
819 	int err;
820 
821 	if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
822 		crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
823 		return -EINVAL;
824 	}
825 
826 	if ((unsigned long)key & alignmask)
827 		err = skcipher_setkey_unaligned(tfm, key, keylen);
828 	else
829 		err = cipher->setkey(tfm, key, keylen);
830 
831 	if (unlikely(err)) {
832 		skcipher_set_needkey(tfm);
833 		return err;
834 	}
835 
836 	crypto_skcipher_clear_flags(tfm, CRYPTO_TFM_NEED_KEY);
837 	return 0;
838 }
839 
840 static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
841 {
842 	struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
843 	struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
844 
845 	alg->exit(skcipher);
846 }
847 
848 static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
849 {
850 	struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
851 	struct skcipher_alg *alg = crypto_skcipher_alg(skcipher);
852 
853 	if (tfm->__crt_alg->cra_type == &crypto_blkcipher_type)
854 		return crypto_init_skcipher_ops_blkcipher(tfm);
855 
856 	if (tfm->__crt_alg->cra_type == &crypto_ablkcipher_type)
857 		return crypto_init_skcipher_ops_ablkcipher(tfm);
858 
859 	skcipher->setkey = skcipher_setkey;
860 	skcipher->encrypt = alg->encrypt;
861 	skcipher->decrypt = alg->decrypt;
862 	skcipher->ivsize = alg->ivsize;
863 	skcipher->keysize = alg->max_keysize;
864 
865 	skcipher_set_needkey(skcipher);
866 
867 	if (alg->exit)
868 		skcipher->base.exit = crypto_skcipher_exit_tfm;
869 
870 	if (alg->init)
871 		return alg->init(skcipher);
872 
873 	return 0;
874 }
875 
876 static void crypto_skcipher_free_instance(struct crypto_instance *inst)
877 {
878 	struct skcipher_instance *skcipher =
879 		container_of(inst, struct skcipher_instance, s.base);
880 
881 	skcipher->free(skcipher);
882 }
883 
884 static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
885 	__maybe_unused;
886 static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
887 {
888 	struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
889 						     base);
890 
891 	seq_printf(m, "type         : skcipher\n");
892 	seq_printf(m, "async        : %s\n",
893 		   alg->cra_flags & CRYPTO_ALG_ASYNC ?  "yes" : "no");
894 	seq_printf(m, "blocksize    : %u\n", alg->cra_blocksize);
895 	seq_printf(m, "min keysize  : %u\n", skcipher->min_keysize);
896 	seq_printf(m, "max keysize  : %u\n", skcipher->max_keysize);
897 	seq_printf(m, "ivsize       : %u\n", skcipher->ivsize);
898 	seq_printf(m, "chunksize    : %u\n", skcipher->chunksize);
899 	seq_printf(m, "walksize     : %u\n", skcipher->walksize);
900 }
901 
902 #ifdef CONFIG_NET
903 static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
904 {
905 	struct crypto_report_blkcipher rblkcipher;
906 	struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
907 						     base);
908 
909 	memset(&rblkcipher, 0, sizeof(rblkcipher));
910 
911 	strscpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type));
912 	strscpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
913 
914 	rblkcipher.blocksize = alg->cra_blocksize;
915 	rblkcipher.min_keysize = skcipher->min_keysize;
916 	rblkcipher.max_keysize = skcipher->max_keysize;
917 	rblkcipher.ivsize = skcipher->ivsize;
918 
919 	return nla_put(skb, CRYPTOCFGA_REPORT_BLKCIPHER,
920 		       sizeof(rblkcipher), &rblkcipher);
921 }
922 #else
923 static int crypto_skcipher_report(struct sk_buff *skb, struct crypto_alg *alg)
924 {
925 	return -ENOSYS;
926 }
927 #endif
928 
929 static const struct crypto_type crypto_skcipher_type2 = {
930 	.extsize = crypto_skcipher_extsize,
931 	.init_tfm = crypto_skcipher_init_tfm,
932 	.free = crypto_skcipher_free_instance,
933 #ifdef CONFIG_PROC_FS
934 	.show = crypto_skcipher_show,
935 #endif
936 	.report = crypto_skcipher_report,
937 	.maskclear = ~CRYPTO_ALG_TYPE_MASK,
938 	.maskset = CRYPTO_ALG_TYPE_BLKCIPHER_MASK,
939 	.type = CRYPTO_ALG_TYPE_SKCIPHER,
940 	.tfmsize = offsetof(struct crypto_skcipher, base),
941 };
942 
943 int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
944 			  const char *name, u32 type, u32 mask)
945 {
946 	spawn->base.frontend = &crypto_skcipher_type2;
947 	return crypto_grab_spawn(&spawn->base, name, type, mask);
948 }
949 EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
950 
951 struct crypto_skcipher *crypto_alloc_skcipher(const char *alg_name,
952 					      u32 type, u32 mask)
953 {
954 	return crypto_alloc_tfm(alg_name, &crypto_skcipher_type2, type, mask);
955 }
956 EXPORT_SYMBOL_GPL(crypto_alloc_skcipher);
957 
958 struct crypto_sync_skcipher *crypto_alloc_sync_skcipher(
959 				const char *alg_name, u32 type, u32 mask)
960 {
961 	struct crypto_skcipher *tfm;
962 
963 	/* Only sync algorithms allowed. */
964 	mask |= CRYPTO_ALG_ASYNC;
965 
966 	tfm = crypto_alloc_tfm(alg_name, &crypto_skcipher_type2, type, mask);
967 
968 	/*
969 	 * Make sure we do not allocate something that might get used with
970 	 * an on-stack request: check the request size.
971 	 */
972 	if (!IS_ERR(tfm) && WARN_ON(crypto_skcipher_reqsize(tfm) >
973 				    MAX_SYNC_SKCIPHER_REQSIZE)) {
974 		crypto_free_skcipher(tfm);
975 		return ERR_PTR(-EINVAL);
976 	}
977 
978 	return (struct crypto_sync_skcipher *)tfm;
979 }
980 EXPORT_SYMBOL_GPL(crypto_alloc_sync_skcipher);
981 
982 int crypto_has_skcipher2(const char *alg_name, u32 type, u32 mask)
983 {
984 	return crypto_type_has_alg(alg_name, &crypto_skcipher_type2,
985 				   type, mask);
986 }
987 EXPORT_SYMBOL_GPL(crypto_has_skcipher2);
988 
989 static int skcipher_prepare_alg(struct skcipher_alg *alg)
990 {
991 	struct crypto_alg *base = &alg->base;
992 
993 	if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8 ||
994 	    alg->walksize > PAGE_SIZE / 8)
995 		return -EINVAL;
996 
997 	if (!alg->chunksize)
998 		alg->chunksize = base->cra_blocksize;
999 	if (!alg->walksize)
1000 		alg->walksize = alg->chunksize;
1001 
1002 	base->cra_type = &crypto_skcipher_type2;
1003 	base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;
1004 	base->cra_flags |= CRYPTO_ALG_TYPE_SKCIPHER;
1005 
1006 	return 0;
1007 }
1008 
1009 int crypto_register_skcipher(struct skcipher_alg *alg)
1010 {
1011 	struct crypto_alg *base = &alg->base;
1012 	int err;
1013 
1014 	err = skcipher_prepare_alg(alg);
1015 	if (err)
1016 		return err;
1017 
1018 	return crypto_register_alg(base);
1019 }
1020 EXPORT_SYMBOL_GPL(crypto_register_skcipher);
1021 
1022 void crypto_unregister_skcipher(struct skcipher_alg *alg)
1023 {
1024 	crypto_unregister_alg(&alg->base);
1025 }
1026 EXPORT_SYMBOL_GPL(crypto_unregister_skcipher);
1027 
1028 int crypto_register_skciphers(struct skcipher_alg *algs, int count)
1029 {
1030 	int i, ret;
1031 
1032 	for (i = 0; i < count; i++) {
1033 		ret = crypto_register_skcipher(&algs[i]);
1034 		if (ret)
1035 			goto err;
1036 	}
1037 
1038 	return 0;
1039 
1040 err:
1041 	for (--i; i >= 0; --i)
1042 		crypto_unregister_skcipher(&algs[i]);
1043 
1044 	return ret;
1045 }
1046 EXPORT_SYMBOL_GPL(crypto_register_skciphers);
1047 
1048 void crypto_unregister_skciphers(struct skcipher_alg *algs, int count)
1049 {
1050 	int i;
1051 
1052 	for (i = count - 1; i >= 0; --i)
1053 		crypto_unregister_skcipher(&algs[i]);
1054 }
1055 EXPORT_SYMBOL_GPL(crypto_unregister_skciphers);
1056 
1057 int skcipher_register_instance(struct crypto_template *tmpl,
1058 			   struct skcipher_instance *inst)
1059 {
1060 	int err;
1061 
1062 	err = skcipher_prepare_alg(&inst->alg);
1063 	if (err)
1064 		return err;
1065 
1066 	return crypto_register_instance(tmpl, skcipher_crypto_instance(inst));
1067 }
1068 EXPORT_SYMBOL_GPL(skcipher_register_instance);
1069 
1070 static int skcipher_setkey_simple(struct crypto_skcipher *tfm, const u8 *key,
1071 				  unsigned int keylen)
1072 {
1073 	struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
1074 	int err;
1075 
1076 	crypto_cipher_clear_flags(cipher, CRYPTO_TFM_REQ_MASK);
1077 	crypto_cipher_set_flags(cipher, crypto_skcipher_get_flags(tfm) &
1078 				CRYPTO_TFM_REQ_MASK);
1079 	err = crypto_cipher_setkey(cipher, key, keylen);
1080 	crypto_skcipher_set_flags(tfm, crypto_cipher_get_flags(cipher) &
1081 				  CRYPTO_TFM_RES_MASK);
1082 	return err;
1083 }
1084 
1085 static int skcipher_init_tfm_simple(struct crypto_skcipher *tfm)
1086 {
1087 	struct skcipher_instance *inst = skcipher_alg_instance(tfm);
1088 	struct crypto_spawn *spawn = skcipher_instance_ctx(inst);
1089 	struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
1090 	struct crypto_cipher *cipher;
1091 
1092 	cipher = crypto_spawn_cipher(spawn);
1093 	if (IS_ERR(cipher))
1094 		return PTR_ERR(cipher);
1095 
1096 	ctx->cipher = cipher;
1097 	return 0;
1098 }
1099 
1100 static void skcipher_exit_tfm_simple(struct crypto_skcipher *tfm)
1101 {
1102 	struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
1103 
1104 	crypto_free_cipher(ctx->cipher);
1105 }
1106 
1107 static void skcipher_free_instance_simple(struct skcipher_instance *inst)
1108 {
1109 	crypto_drop_spawn(skcipher_instance_ctx(inst));
1110 	kfree(inst);
1111 }
1112 
1113 /**
1114  * skcipher_alloc_instance_simple - allocate instance of simple block cipher mode
1115  *
1116  * Allocate an skcipher_instance for a simple block cipher mode of operation,
1117  * e.g. cbc or ecb.  The instance context will have just a single crypto_spawn,
1118  * that for the underlying cipher.  The {min,max}_keysize, ivsize, blocksize,
1119  * alignmask, and priority are set from the underlying cipher but can be
1120  * overridden if needed.  The tfm context defaults to skcipher_ctx_simple, and
1121  * default ->setkey(), ->init(), and ->exit() methods are installed.
1122  *
1123  * @tmpl: the template being instantiated
1124  * @tb: the template parameters
1125  * @cipher_alg_ret: on success, a pointer to the underlying cipher algorithm is
1126  *		    returned here.  It must be dropped with crypto_mod_put().
1127  *
1128  * Return: a pointer to the new instance, or an ERR_PTR().  The caller still
1129  *	   needs to register the instance.
1130  */
1131 struct skcipher_instance *
1132 skcipher_alloc_instance_simple(struct crypto_template *tmpl, struct rtattr **tb,
1133 			       struct crypto_alg **cipher_alg_ret)
1134 {
1135 	struct crypto_attr_type *algt;
1136 	struct crypto_alg *cipher_alg;
1137 	struct skcipher_instance *inst;
1138 	struct crypto_spawn *spawn;
1139 	u32 mask;
1140 	int err;
1141 
1142 	algt = crypto_get_attr_type(tb);
1143 	if (IS_ERR(algt))
1144 		return ERR_CAST(algt);
1145 
1146 	if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask)
1147 		return ERR_PTR(-EINVAL);
1148 
1149 	mask = CRYPTO_ALG_TYPE_MASK |
1150 		crypto_requires_off(algt->type, algt->mask,
1151 				    CRYPTO_ALG_NEED_FALLBACK);
1152 
1153 	cipher_alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER, mask);
1154 	if (IS_ERR(cipher_alg))
1155 		return ERR_CAST(cipher_alg);
1156 
1157 	inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
1158 	if (!inst) {
1159 		err = -ENOMEM;
1160 		goto err_put_cipher_alg;
1161 	}
1162 	spawn = skcipher_instance_ctx(inst);
1163 
1164 	err = crypto_inst_setname(skcipher_crypto_instance(inst), tmpl->name,
1165 				  cipher_alg);
1166 	if (err)
1167 		goto err_free_inst;
1168 
1169 	err = crypto_init_spawn(spawn, cipher_alg,
1170 				skcipher_crypto_instance(inst),
1171 				CRYPTO_ALG_TYPE_MASK);
1172 	if (err)
1173 		goto err_free_inst;
1174 	inst->free = skcipher_free_instance_simple;
1175 
1176 	/* Default algorithm properties, can be overridden */
1177 	inst->alg.base.cra_blocksize = cipher_alg->cra_blocksize;
1178 	inst->alg.base.cra_alignmask = cipher_alg->cra_alignmask;
1179 	inst->alg.base.cra_priority = cipher_alg->cra_priority;
1180 	inst->alg.min_keysize = cipher_alg->cra_cipher.cia_min_keysize;
1181 	inst->alg.max_keysize = cipher_alg->cra_cipher.cia_max_keysize;
1182 	inst->alg.ivsize = cipher_alg->cra_blocksize;
1183 
1184 	/* Use skcipher_ctx_simple by default, can be overridden */
1185 	inst->alg.base.cra_ctxsize = sizeof(struct skcipher_ctx_simple);
1186 	inst->alg.setkey = skcipher_setkey_simple;
1187 	inst->alg.init = skcipher_init_tfm_simple;
1188 	inst->alg.exit = skcipher_exit_tfm_simple;
1189 
1190 	*cipher_alg_ret = cipher_alg;
1191 	return inst;
1192 
1193 err_free_inst:
1194 	kfree(inst);
1195 err_put_cipher_alg:
1196 	crypto_mod_put(cipher_alg);
1197 	return ERR_PTR(err);
1198 }
1199 EXPORT_SYMBOL_GPL(skcipher_alloc_instance_simple);
1200 
1201 MODULE_LICENSE("GPL");
1202 MODULE_DESCRIPTION("Symmetric key cipher type");
1203