Merge tag 'hwmon-for-linus-v4.19-rc2' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6/btrfs-unstable.git] / crypto / scatterwalk.c
blobd0b92c1cd6e9b2a47d5a9dd1728ba70a13dc481c
1 /*
2 * Cryptographic API.
4 * Cipher operations.
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>
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.
17 #include <crypto/scatterwalk.h>
18 #include <linux/kernel.h>
19 #include <linux/mm.h>
20 #include <linux/module.h>
21 #include <linux/scatterlist.h>
23 static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
25 void *src = out ? buf : sgdata;
26 void *dst = out ? sgdata : buf;
28 memcpy(dst, src, nbytes);
31 void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
32 size_t nbytes, int out)
34 for (;;) {
35 unsigned int len_this_page = scatterwalk_pagelen(walk);
36 u8 *vaddr;
38 if (len_this_page > nbytes)
39 len_this_page = nbytes;
41 if (out != 2) {
42 vaddr = scatterwalk_map(walk);
43 memcpy_dir(buf, vaddr, len_this_page, out);
44 scatterwalk_unmap(vaddr);
47 scatterwalk_advance(walk, len_this_page);
49 if (nbytes == len_this_page)
50 break;
52 buf += len_this_page;
53 nbytes -= len_this_page;
55 scatterwalk_pagedone(walk, out & 1, 1);
58 EXPORT_SYMBOL_GPL(scatterwalk_copychunks);
60 void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
61 unsigned int start, unsigned int nbytes, int out)
63 struct scatter_walk walk;
64 struct scatterlist tmp[2];
66 if (!nbytes)
67 return;
69 sg = scatterwalk_ffwd(tmp, sg, start);
71 scatterwalk_start(&walk, sg);
72 scatterwalk_copychunks(buf, &walk, nbytes, out);
73 scatterwalk_done(&walk, out, 0);
75 EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy);
77 struct scatterlist *scatterwalk_ffwd(struct scatterlist dst[2],
78 struct scatterlist *src,
79 unsigned int len)
81 for (;;) {
82 if (!len)
83 return src;
85 if (src->length > len)
86 break;
88 len -= src->length;
89 src = sg_next(src);
92 sg_init_table(dst, 2);
93 sg_set_page(dst, sg_page(src), src->length - len, src->offset + len);
94 scatterwalk_crypto_chain(dst, sg_next(src), 2);
96 return dst;
98 EXPORT_SYMBOL_GPL(scatterwalk_ffwd);