blob: ea5815c5e12817912e1749d6aaf1a84606d7e7f3 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/pagemap.h>
22#include <linux/highmem.h>
Herbert Xu5c640972006-08-12 21:56:17 +100023#include <linux/scatterlist.h>
24
Herbert Xu5c640972006-08-12 21:56:17 +100025static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Herbert Xu5c640972006-08-12 21:56:17 +100027 void *src = out ? buf : sgdata;
28 void *dst = out ? sgdata : buf;
29
30 memcpy(dst, src, nbytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031}
32
33void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg)
34{
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 walk->sg = sg;
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 BUG_ON(!sg->length);
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 walk->offset = sg->offset;
40}
Herbert Xu5c640972006-08-12 21:56:17 +100041EXPORT_SYMBOL_GPL(scatterwalk_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Cong Wangf0dfc0b2011-11-25 23:14:17 +080043void *scatterwalk_map(struct scatter_walk *walk)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Cong Wangf0dfc0b2011-11-25 23:14:17 +080045 return kmap_atomic(scatterwalk_page(walk)) +
Herbert Xu5c640972006-08-12 21:56:17 +100046 offset_in_page(walk->offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
Herbert Xu5c640972006-08-12 21:56:17 +100048EXPORT_SYMBOL_GPL(scatterwalk_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
51 unsigned int more)
52{
Herbert Xu9f116722007-03-31 12:58:20 +100053 if (out) {
54 struct page *page;
55
Jens Axboe78c2f0b2007-10-22 19:40:16 +020056 page = sg_page(walk->sg) + ((walk->offset - 1) >> PAGE_SHIFT);
Herbert Xu16054402015-06-01 16:22:03 +080057 /* Test ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE first as
58 * PageSlab cannot be optimised away per se due to
59 * use of volatile pointer.
60 */
61 if (ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE && !PageSlab(page))
Herbert Xu4f3e7972009-02-09 14:22:14 +110062 flush_dcache_page(page);
Herbert Xu9f116722007-03-31 12:58:20 +100063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 if (more) {
Herbert Xu5c640972006-08-12 21:56:17 +100066 walk->offset += PAGE_SIZE - 1;
67 walk->offset &= PAGE_MASK;
68 if (walk->offset >= walk->sg->offset + walk->sg->length)
Cristian Stoica5be4d4c2015-01-20 10:06:16 +020069 scatterwalk_start(walk, sg_next(walk->sg));
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
71}
72
73void scatterwalk_done(struct scatter_walk *walk, int out, int more)
74{
David S. Miller85c62012010-05-19 14:06:29 +100075 if (!(scatterwalk_pagelen(walk) & (PAGE_SIZE - 1)) || !more)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 scatterwalk_pagedone(walk, out, more);
77}
Herbert Xu5c640972006-08-12 21:56:17 +100078EXPORT_SYMBOL_GPL(scatterwalk_done);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Herbert Xu5c640972006-08-12 21:56:17 +100080void scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
81 size_t nbytes, int out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Herbert Xu5c640972006-08-12 21:56:17 +100083 for (;;) {
84 unsigned int len_this_page = scatterwalk_pagelen(walk);
85 u8 *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Herbert Xu5c640972006-08-12 21:56:17 +100087 if (len_this_page > nbytes)
88 len_this_page = nbytes;
89
Cong Wangf0dfc0b2011-11-25 23:14:17 +080090 vaddr = scatterwalk_map(walk);
Herbert Xu5c640972006-08-12 21:56:17 +100091 memcpy_dir(buf, vaddr, len_this_page, out);
Cong Wangf0dfc0b2011-11-25 23:14:17 +080092 scatterwalk_unmap(vaddr);
Herbert Xu5c640972006-08-12 21:56:17 +100093
Herbert Xu4ee531a2007-03-31 12:16:20 +100094 scatterwalk_advance(walk, len_this_page);
J. Bruce Fieldsf70ee5e2007-03-21 08:50:12 +110095
Herbert Xu5c640972006-08-12 21:56:17 +100096 if (nbytes == len_this_page)
97 break;
98
99 buf += len_this_page;
100 nbytes -= len_this_page;
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 scatterwalk_pagedone(walk, out, 1);
Herbert Xuc774e932005-07-06 13:51:31 -0700103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
Herbert Xu5c640972006-08-12 21:56:17 +1000105EXPORT_SYMBOL_GPL(scatterwalk_copychunks);
Herbert Xu5fa0fea2007-08-29 16:31:34 +0800106
107void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg,
108 unsigned int start, unsigned int nbytes, int out)
109{
110 struct scatter_walk walk;
Herbert Xu74412fd2015-05-21 15:11:12 +0800111 struct scatterlist tmp[2];
Herbert Xu5fa0fea2007-08-29 16:31:34 +0800112
Herbert Xu6e050772007-12-14 00:44:11 +0800113 if (!nbytes)
114 return;
115
Herbert Xu74412fd2015-05-21 15:11:12 +0800116 sg = scatterwalk_ffwd(tmp, sg, start);
Herbert Xu5fa0fea2007-08-29 16:31:34 +0800117
Herbert Xu74412fd2015-05-21 15:11:12 +0800118 if (sg_page(sg) == virt_to_page(buf) &&
119 sg->offset == offset_in_page(buf))
120 return;
Herbert Xu5fa0fea2007-08-29 16:31:34 +0800121
Herbert Xu74412fd2015-05-21 15:11:12 +0800122 scatterwalk_start(&walk, sg);
Herbert Xu5fa0fea2007-08-29 16:31:34 +0800123 scatterwalk_copychunks(buf, &walk, nbytes, out);
124 scatterwalk_done(&walk, out, 0);
125}
126EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy);
Joel Fernandes257aff52013-08-17 21:42:22 -0500127
128int scatterwalk_bytes_sglen(struct scatterlist *sg, int num_bytes)
129{
130 int offset = 0, n = 0;
131
132 /* num_bytes is too small */
133 if (num_bytes < sg->length)
134 return -1;
135
136 do {
137 offset += sg->length;
138 n++;
Cristian Stoica5be4d4c2015-01-20 10:06:16 +0200139 sg = sg_next(sg);
Joel Fernandes257aff52013-08-17 21:42:22 -0500140
141 /* num_bytes is too large */
142 if (unlikely(!sg && (num_bytes < offset)))
143 return -1;
144 } while (sg && (num_bytes > offset));
145
146 return n;
147}
148EXPORT_SYMBOL_GPL(scatterwalk_bytes_sglen);
Herbert Xufc42bcb2015-05-21 15:10:59 +0800149
150struct scatterlist *scatterwalk_ffwd(struct scatterlist dst[2],
151 struct scatterlist *src,
152 unsigned int len)
153{
154 for (;;) {
155 if (!len)
156 return src;
157
158 if (src->length > len)
159 break;
160
161 len -= src->length;
162 src = sg_next(src);
163 }
164
Herbert Xufdaef752015-05-27 14:37:27 +0800165 sg_init_table(dst, 2);
Herbert Xufc42bcb2015-05-21 15:10:59 +0800166 sg_set_page(dst, sg_page(src), src->length - len, src->offset + len);
167 scatterwalk_crypto_chain(dst, sg_next(src), 0, 2);
168
169 return dst;
170}
171EXPORT_SYMBOL_GPL(scatterwalk_ffwd);