]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | */ | |
42c271c6 HX |
16 | |
17 | #include <crypto/scatterwalk.h> | |
1da177e4 LT |
18 | #include <linux/kernel.h> |
19 | #include <linux/mm.h> | |
5c64097a | 20 | #include <linux/module.h> |
1da177e4 LT |
21 | #include <linux/pagemap.h> |
22 | #include <linux/highmem.h> | |
5c64097a HX |
23 | #include <linux/scatterlist.h> |
24 | ||
5c64097a | 25 | static inline void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out) |
1da177e4 | 26 | { |
5c64097a HX |
27 | void *src = out ? buf : sgdata; |
28 | void *dst = out ? sgdata : buf; | |
29 | ||
30 | memcpy(dst, src, nbytes); | |
1da177e4 LT |
31 | } |
32 | ||
33 | void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg) | |
34 | { | |
1da177e4 LT |
35 | walk->sg = sg; |
36 | ||
1da177e4 LT |
37 | BUG_ON(!sg->length); |
38 | ||
1da177e4 LT |
39 | walk->offset = sg->offset; |
40 | } | |
5c64097a | 41 | EXPORT_SYMBOL_GPL(scatterwalk_start); |
1da177e4 | 42 | |
f0dfc0b0 | 43 | void *scatterwalk_map(struct scatter_walk *walk) |
1da177e4 | 44 | { |
f0dfc0b0 | 45 | return kmap_atomic(scatterwalk_page(walk)) + |
5c64097a | 46 | offset_in_page(walk->offset); |
1da177e4 | 47 | } |
5c64097a | 48 | EXPORT_SYMBOL_GPL(scatterwalk_map); |
1da177e4 LT |
49 | |
50 | static void scatterwalk_pagedone(struct scatter_walk *walk, int out, | |
51 | unsigned int more) | |
52 | { | |
9f116727 HX |
53 | if (out) { |
54 | struct page *page; | |
55 | ||
78c2f0b8 | 56 | page = sg_page(walk->sg) + ((walk->offset - 1) >> PAGE_SHIFT); |
16054407 HX |
57 | /* 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)) | |
4f3e797a | 62 | flush_dcache_page(page); |
9f116727 | 63 | } |
1da177e4 LT |
64 | |
65 | if (more) { | |
5c64097a HX |
66 | walk->offset += PAGE_SIZE - 1; |
67 | walk->offset &= PAGE_MASK; | |
68 | if (walk->offset >= walk->sg->offset + walk->sg->length) | |
5be4d4c9 | 69 | scatterwalk_start(walk, sg_next(walk->sg)); |
1da177e4 LT |
70 | } |
71 | } | |
72 | ||
73 | void scatterwalk_done(struct scatter_walk *walk, int out, int more) | |
74 | { | |
85c6201a | 75 | if (!(scatterwalk_pagelen(walk) & (PAGE_SIZE - 1)) || !more) |
1da177e4 LT |
76 | scatterwalk_pagedone(walk, out, more); |
77 | } | |
5c64097a | 78 | EXPORT_SYMBOL_GPL(scatterwalk_done); |
1da177e4 | 79 | |
5c64097a HX |
80 | void scatterwalk_copychunks(void *buf, struct scatter_walk *walk, |
81 | size_t nbytes, int out) | |
1da177e4 | 82 | { |
5c64097a HX |
83 | for (;;) { |
84 | unsigned int len_this_page = scatterwalk_pagelen(walk); | |
85 | u8 *vaddr; | |
86 | ||
87 | if (len_this_page > nbytes) | |
88 | len_this_page = nbytes; | |
89 | ||
f0dfc0b0 | 90 | vaddr = scatterwalk_map(walk); |
5c64097a | 91 | memcpy_dir(buf, vaddr, len_this_page, out); |
f0dfc0b0 | 92 | scatterwalk_unmap(vaddr); |
5c64097a | 93 | |
4ee531a3 | 94 | scatterwalk_advance(walk, len_this_page); |
f70ee5ec | 95 | |
5c64097a HX |
96 | if (nbytes == len_this_page) |
97 | break; | |
98 | ||
99 | buf += len_this_page; | |
100 | nbytes -= len_this_page; | |
1da177e4 | 101 | |
1da177e4 | 102 | scatterwalk_pagedone(walk, out, 1); |
c774e93e | 103 | } |
1da177e4 | 104 | } |
5c64097a | 105 | EXPORT_SYMBOL_GPL(scatterwalk_copychunks); |
5fa0fea2 HX |
106 | |
107 | void scatterwalk_map_and_copy(void *buf, struct scatterlist *sg, | |
108 | unsigned int start, unsigned int nbytes, int out) | |
109 | { | |
110 | struct scatter_walk walk; | |
74412fd5 | 111 | struct scatterlist tmp[2]; |
5fa0fea2 | 112 | |
6e050778 HX |
113 | if (!nbytes) |
114 | return; | |
115 | ||
74412fd5 | 116 | sg = scatterwalk_ffwd(tmp, sg, start); |
5fa0fea2 | 117 | |
74412fd5 HX |
118 | if (sg_page(sg) == virt_to_page(buf) && |
119 | sg->offset == offset_in_page(buf)) | |
120 | return; | |
5fa0fea2 | 121 | |
74412fd5 | 122 | scatterwalk_start(&walk, sg); |
5fa0fea2 HX |
123 | scatterwalk_copychunks(buf, &walk, nbytes, out); |
124 | scatterwalk_done(&walk, out, 0); | |
125 | } | |
126 | EXPORT_SYMBOL_GPL(scatterwalk_map_and_copy); | |
257aff51 JF |
127 | |
128 | int 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++; | |
5be4d4c9 | 139 | sg = sg_next(sg); |
257aff51 JF |
140 | |
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 | } | |
148 | EXPORT_SYMBOL_GPL(scatterwalk_bytes_sglen); | |
fc42bcba HX |
149 | |
150 | struct 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 | ||
fdaef75f | 165 | sg_init_table(dst, 2); |
fc42bcba HX |
166 | 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 | } | |
171 | EXPORT_SYMBOL_GPL(scatterwalk_ffwd); |