blob: 16f6ba896fb63bdc3cc9ad1ab56881270fd2c18d [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 Torvalds1da177e2005-04-16 15:20:36 -070010 */
Herbert Xu42c271c2007-12-07 18:52:49 +080011
12#include <crypto/scatterwalk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/mm.h>
Herbert Xu5c640972006-08-12 21:56:17 +100015#include <linux/module.h>
Herbert Xu5c640972006-08-12 21:56:17 +100016#include <linux/scatterlist.h>
17
Herbert Xu5c640972006-08-12 21:56:17 +100018static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070019{
Herbert Xu5c640972006-08-12 21:56:17 +100020 void *src = out ? buf : sgdata;
21 void *dst = out ? sgdata : buf;
22
23 memcpy(dst, src, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024}
25
Herbert Xu5c640972006-08-12 21:56:17 +100026void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
27 size_t nbytes, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Herbert Xu5c640972006-08-12 21:56:17 +100029 for (;;) {
30 unsigned int len_this_page = scatterwalk_pagelen(walk);
31 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Herbert Xu5c640972006-08-12 21:56:17 +100033 if (len_this_page > nbytes)
34 len_this_page = nbytes;
35
Herbert Xu85eccdd2016-07-12 13:17:55 +080036 if (out != 2) {
37 vaddr = scatterwalk_map(walk);
38 memcpy_dir(buf, vaddr, len_this_page, out);
39 scatterwalk_unmap(vaddr);
40 }
Herbert Xu5c640972006-08-12 21:56:17 +100041
Herbert Xu4ee531a2007-03-31 12:16:20 +100042 scatterwalk_advance(walk, len_this_page);
J. Bruce Fieldsf70ee5e2007-03-21 08:50:12 +110043
Herbert Xu5c640972006-08-12 21:56:17 +100044 if (nbytes == len_this_page)
45 break;
46
47 buf += len_this_page;
48 nbytes -= len_this_page;
49
Herbert Xu85eccdd2016-07-12 13:17:55 +080050 scatterwalk_pagedone(walk, out & 1, 1);
Herbert Xuc774e932005-07-06 13:51:31 -070051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
Herbert Xu5c640972006-08-12 21:56:17 +100053EXPORT_SYMBOL_GPL(scatterwalk_copychunks);
Herbert Xu5fa0fea2007-08-29 16:31:34 +080054
55void 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 Xu74412fd2015-05-21 15:11:12 +080059 struct scatterlist tmp[2];
Herbert Xu5fa0fea2007-08-29 16:31:34 +080060
Herbert Xu6e050772007-12-14 00:44:11 +080061 if (!nbytes)
62 return;
63
Herbert Xu74412fd2015-05-21 15:11:12 +080064 sg = scatterwalk_ffwd(tmp, sg, start);
Herbert Xu5fa0fea2007-08-29 16:31:34 +080065
Herbert Xu74412fd2015-05-21 15:11:12 +080066 scatterwalk_start(&walk, sg);
Herbert Xu5fa0fea2007-08-29 16:31:34 +080067 scatterwalk_copychunks(buf, &walk, nbytes, out);
68 scatterwalk_done(&walk, out, 0);
69}
70EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy);
Joel Fernandes257aff52013-08-17 21:42:22 -050071
Herbert Xufc42bcb2015-05-21 15:10:59 +080072struct 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 Xufdaef752015-05-27 14:37:27 +080087 sg_init_table(dst, 2);
Herbert Xufc42bcb2015-05-21 15:10:59 +080088 sg_set_page(dst, sg_page(src), src->length - len, src->offset + len);
Eric Biggers8c30fbe2018-07-23 10:01:33 -070089 scatterwalk_crypto_chain(dst, sg_next(src), 2);
Herbert Xufc42bcb2015-05-21 15:10:59 +080090
91 return dst;
92}
93EXPORT_SYMBOL_GPL(scatterwalk_ffwd);