blob: c16c94f88733e738f4ee1dff7a1334f9c2e39eb4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Xu42c271c2007-12-07 18:52:49 +080016
17#include <crypto/scatterwalk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19#include <linux/mm.h>
Herbert Xu5c640972006-08-12 21:56:17 +100020#include <linux/module.h>
Herbert Xu5c640972006-08-12 21:56:17 +100021#include <linux/scatterlist.h>
22
Herbert Xu5c640972006-08-12 21:56:17 +100023static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
Herbert Xu5c640972006-08-12 21:56:17 +100025 void *src = out ? buf : sgdata;
26 void *dst = out ? sgdata : buf;
27
28 memcpy(dst, src, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029}
30
Herbert Xu5c640972006-08-12 21:56:17 +100031void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
32 size_t nbytes, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Herbert Xu5c640972006-08-12 21:56:17 +100034 for (;;) {
35 unsigned int len_this_page = scatterwalk_pagelen(walk);
36 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Herbert Xu5c640972006-08-12 21:56:17 +100038 if (len_this_page > nbytes)
39 len_this_page = nbytes;
40
Herbert Xu85eccdd2016-07-12 13:17:55 +080041 if (out != 2) {
42 vaddr = scatterwalk_map(walk);
43 memcpy_dir(buf, vaddr, len_this_page, out);
44 scatterwalk_unmap(vaddr);
45 }
Herbert Xu5c640972006-08-12 21:56:17 +100046
Herbert Xu4ee531a2007-03-31 12:16:20 +100047 scatterwalk_advance(walk, len_this_page);
J. Bruce Fieldsf70ee5e2007-03-21 08:50:12 +110048
Herbert Xu5c640972006-08-12 21:56:17 +100049 if (nbytes == len_this_page)
50 break;
51
52 buf += len_this_page;
53 nbytes -= len_this_page;
54
Herbert Xu85eccdd2016-07-12 13:17:55 +080055 scatterwalk_pagedone(walk, out & 1, 1);
Herbert Xuc774e932005-07-06 13:51:31 -070056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
Herbert Xu5c640972006-08-12 21:56:17 +100058EXPORT_SYMBOL_GPL(scatterwalk_copychunks);
Herbert Xu5fa0fea2007-08-29 16:31:34 +080059
60void 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 Xu74412fd2015-05-21 15:11:12 +080064 struct scatterlist tmp[2];
Herbert Xu5fa0fea2007-08-29 16:31:34 +080065
Herbert Xu6e050772007-12-14 00:44:11 +080066 if (!nbytes)
67 return;
68
Herbert Xu74412fd2015-05-21 15:11:12 +080069 sg = scatterwalk_ffwd(tmp, sg, start);
Herbert Xu5fa0fea2007-08-29 16:31:34 +080070
Herbert Xu74412fd2015-05-21 15:11:12 +080071 scatterwalk_start(&walk, sg);
Herbert Xu5fa0fea2007-08-29 16:31:34 +080072 scatterwalk_copychunks(buf, &walk, nbytes, out);
73 scatterwalk_done(&walk, out, 0);
74}
75EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy);
Joel Fernandes257aff52013-08-17 21:42:22 -050076
Herbert Xufc42bcb2015-05-21 15:10:59 +080077struct 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 Xufdaef752015-05-27 14:37:27 +080092 sg_init_table(dst, 2);
Herbert Xufc42bcb2015-05-21 15:10:59 +080093 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}
98EXPORT_SYMBOL_GPL(scatterwalk_ffwd);