Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Cryptographic API. |
| 4 | * |
| 5 | * Cipher operations. |
| 6 | * |
| 7 | * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> |
| 8 | * 2002 Adam J. Richter <adam@yggdrasil.com> |
| 9 | * 2004 Jean-Luc Cooke <jlcooke@certainkey.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
Herbert Xu | 42c271c | 2007-12-07 18:52:49 +0800 | [diff] [blame] | 11 | |
| 12 | #include <crypto/scatterwalk.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/mm.h> |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 15 | #include <linux/module.h> |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 16 | #include <linux/scatterlist.h> |
| 17 | |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 18 | static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | { |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 20 | void *src = out ? buf : sgdata; |
| 21 | void *dst = out ? sgdata : buf; |
| 22 | |
| 23 | memcpy(dst, src, nbytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | } |
| 25 | |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 26 | void scatterwalk_copychunks(void *buf, struct scatter_walk *walk, |
| 27 | size_t nbytes, int out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 29 | for (;;) { |
| 30 | unsigned int len_this_page = scatterwalk_pagelen(walk); |
| 31 | u8 *vaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 33 | if (len_this_page > nbytes) |
| 34 | len_this_page = nbytes; |
| 35 | |
Herbert Xu | 85eccdd | 2016-07-12 13:17:55 +0800 | [diff] [blame] | 36 | if (out != 2) { |
| 37 | vaddr = scatterwalk_map(walk); |
| 38 | memcpy_dir(buf, vaddr, len_this_page, out); |
| 39 | scatterwalk_unmap(vaddr); |
| 40 | } |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 41 | |
Herbert Xu | 4ee531a | 2007-03-31 12:16:20 +1000 | [diff] [blame] | 42 | scatterwalk_advance(walk, len_this_page); |
J. Bruce Fields | f70ee5e | 2007-03-21 08:50:12 +1100 | [diff] [blame] | 43 | |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 44 | if (nbytes == len_this_page) |
| 45 | break; |
| 46 | |
| 47 | buf += len_this_page; |
| 48 | nbytes -= len_this_page; |
| 49 | |
Herbert Xu | 85eccdd | 2016-07-12 13:17:55 +0800 | [diff] [blame] | 50 | scatterwalk_pagedone(walk, out & 1, 1); |
Herbert Xu | c774e93 | 2005-07-06 13:51:31 -0700 | [diff] [blame] | 51 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
Herbert Xu | 5c64097 | 2006-08-12 21:56:17 +1000 | [diff] [blame] | 53 | EXPORT_SYMBOL_GPL(scatterwalk_copychunks); |
Herbert Xu | 5fa0fea | 2007-08-29 16:31:34 +0800 | [diff] [blame] | 54 | |
| 55 | void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg, |
| 56 | unsigned int start, unsigned int nbytes, int out) |
| 57 | { |
| 58 | struct scatter_walk walk; |
Herbert Xu | 74412fd | 2015-05-21 15:11:12 +0800 | [diff] [blame] | 59 | struct scatterlist tmp[2]; |
Herbert Xu | 5fa0fea | 2007-08-29 16:31:34 +0800 | [diff] [blame] | 60 | |
Herbert Xu | 6e05077 | 2007-12-14 00:44:11 +0800 | [diff] [blame] | 61 | if (!nbytes) |
| 62 | return; |
| 63 | |
Herbert Xu | 74412fd | 2015-05-21 15:11:12 +0800 | [diff] [blame] | 64 | sg = scatterwalk_ffwd(tmp, sg, start); |
Herbert Xu | 5fa0fea | 2007-08-29 16:31:34 +0800 | [diff] [blame] | 65 | |
Herbert Xu | 74412fd | 2015-05-21 15:11:12 +0800 | [diff] [blame] | 66 | scatterwalk_start(&walk, sg); |
Herbert Xu | 5fa0fea | 2007-08-29 16:31:34 +0800 | [diff] [blame] | 67 | scatterwalk_copychunks(buf, &walk, nbytes, out); |
| 68 | scatterwalk_done(&walk, out, 0); |
| 69 | } |
| 70 | EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy); |
Joel Fernandes | 257aff5 | 2013-08-17 21:42:22 -0500 | [diff] [blame] | 71 | |
Herbert Xu | fc42bcb | 2015-05-21 15:10:59 +0800 | [diff] [blame] | 72 | struct scatterlist *scatterwalk_ffwd(struct scatterlist dst[2], |
| 73 | struct scatterlist *src, |
| 74 | unsigned int len) |
| 75 | { |
| 76 | for (;;) { |
| 77 | if (!len) |
| 78 | return src; |
| 79 | |
| 80 | if (src->length > len) |
| 81 | break; |
| 82 | |
| 83 | len -= src->length; |
| 84 | src = sg_next(src); |
| 85 | } |
| 86 | |
Herbert Xu | fdaef75 | 2015-05-27 14:37:27 +0800 | [diff] [blame] | 87 | sg_init_table(dst, 2); |
Herbert Xu | fc42bcb | 2015-05-21 15:10:59 +0800 | [diff] [blame] | 88 | sg_set_page(dst, sg_page(src), src->length - len, src->offset + len); |
Eric Biggers | 8c30fbe | 2018-07-23 10:01:33 -0700 | [diff] [blame] | 89 | scatterwalk_crypto_chain(dst, sg_next(src), 2); |
Herbert Xu | fc42bcb | 2015-05-21 15:10:59 +0800 | [diff] [blame] | 90 | |
| 91 | return dst; |
| 92 | } |
| 93 | EXPORT_SYMBOL_GPL(scatterwalk_ffwd); |