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