Merge tag 'mips_fixes_5.2_2' of git://git.kernel.org/pub/scm/linux/kernel/git/mips...
[linux/fpc-iii.git] / crypto / scatterwalk.c
blob16f6ba896fb63bdc3cc9ad1ab56881270fd2c18d
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Cryptographic API.
5 * Cipher operations.
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>
12 #include <crypto/scatterwalk.h>
13 #include <linux/kernel.h>
14 #include <linux/mm.h>
15 #include <linux/module.h>
16 #include <linux/scatterlist.h>
18 static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
20 void *src = out ? buf : sgdata;
21 void *dst = out ? sgdata : buf;
23 memcpy(dst, src, nbytes);
26 void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
27 size_t nbytes, int out)
29 for (;;) {
30 unsigned int len_this_page = scatterwalk_pagelen(walk);
31 u8 *vaddr;
33 if (len_this_page > nbytes)
34 len_this_page = nbytes;
36 if (out != 2) {
37 vaddr = scatterwalk_map(walk);
38 memcpy_dir(buf, vaddr, len_this_page, out);
39 scatterwalk_unmap(vaddr);
42 scatterwalk_advance(walk, len_this_page);
44 if (nbytes == len_this_page)
45 break;
47 buf += len_this_page;
48 nbytes -= len_this_page;
50 scatterwalk_pagedone(walk, out & 1, 1);
53 EXPORT_SYMBOL_GPL(scatterwalk_copychunks);
55 void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
56 unsigned int start, unsigned int nbytes, int out)
58 struct scatter_walk walk;
59 struct scatterlist tmp[2];
61 if (!nbytes)
62 return;
64 sg = scatterwalk_ffwd(tmp, sg, start);
66 scatterwalk_start(&walk, sg);
67 scatterwalk_copychunks(buf, &walk, nbytes, out);
68 scatterwalk_done(&walk, out, 0);
70 EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy);
72 struct scatterlist *scatterwalk_ffwd(struct scatterlist dst[2],
73 struct scatterlist *src,
74 unsigned int len)
76 for (;;) {
77 if (!len)
78 return src;
80 if (src->length > len)
81 break;
83 len -= src->length;
84 src = sg_next(src);
87 sg_init_table(dst, 2);
88 sg_set_page(dst, sg_page(src), src->length - len, src->offset + len);
89 scatterwalk_crypto_chain(dst, sg_next(src), 2);
91 return dst;
93 EXPORT_SYMBOL_GPL(scatterwalk_ffwd);