This source file includes following definitions.
- crypto_kw_scatterlist_ff
- crypto_kw_decrypt
- crypto_kw_encrypt
- crypto_kw_create
- crypto_kw_init
- crypto_kw_exit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 #include <linux/module.h>
85 #include <linux/crypto.h>
86 #include <linux/scatterlist.h>
87 #include <crypto/scatterwalk.h>
88 #include <crypto/internal/skcipher.h>
89
90 struct crypto_kw_block {
91 #define SEMIBSIZE 8
92 __be64 A;
93 __be64 R;
94 };
95
96
97
98
99
100
101 static void crypto_kw_scatterlist_ff(struct scatter_walk *walk,
102 struct scatterlist *sg,
103 unsigned int end)
104 {
105 unsigned int skip = 0;
106
107
108 BUG_ON(end < SEMIBSIZE);
109
110 skip = end - SEMIBSIZE;
111 while (sg) {
112 if (sg->length > skip) {
113 scatterwalk_start(walk, sg);
114 scatterwalk_advance(walk, skip);
115 break;
116 } else
117 skip -= sg->length;
118
119 sg = sg_next(sg);
120 }
121 }
122
123 static int crypto_kw_decrypt(struct skcipher_request *req)
124 {
125 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
126 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
127 struct crypto_kw_block block;
128 struct scatterlist *src, *dst;
129 u64 t = 6 * ((req->cryptlen) >> 3);
130 unsigned int i;
131 int ret = 0;
132
133
134
135
136
137 if (req->cryptlen < (2 * SEMIBSIZE) || req->cryptlen % SEMIBSIZE)
138 return -EINVAL;
139
140
141 memcpy(&block.A, req->iv, SEMIBSIZE);
142
143
144
145
146
147
148 src = req->src;
149 dst = req->dst;
150
151 for (i = 0; i < 6; i++) {
152 struct scatter_walk src_walk, dst_walk;
153 unsigned int nbytes = req->cryptlen;
154
155 while (nbytes) {
156
157 crypto_kw_scatterlist_ff(&src_walk, src, nbytes);
158
159 scatterwalk_copychunks(&block.R, &src_walk, SEMIBSIZE,
160 false);
161
162
163 block.A ^= cpu_to_be64(t);
164 t--;
165
166 crypto_cipher_decrypt_one(cipher, (u8 *)&block,
167 (u8 *)&block);
168
169
170 crypto_kw_scatterlist_ff(&dst_walk, dst, nbytes);
171
172 scatterwalk_copychunks(&block.R, &dst_walk, SEMIBSIZE,
173 true);
174
175 nbytes -= SEMIBSIZE;
176 }
177
178
179 src = req->dst;
180 dst = req->dst;
181 }
182
183
184 if (block.A != cpu_to_be64(0xa6a6a6a6a6a6a6a6ULL))
185 ret = -EBADMSG;
186
187 memzero_explicit(&block, sizeof(struct crypto_kw_block));
188
189 return ret;
190 }
191
192 static int crypto_kw_encrypt(struct skcipher_request *req)
193 {
194 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
195 struct crypto_cipher *cipher = skcipher_cipher_simple(tfm);
196 struct crypto_kw_block block;
197 struct scatterlist *src, *dst;
198 u64 t = 1;
199 unsigned int i;
200
201
202
203
204
205
206
207 if (req->cryptlen < (2 * SEMIBSIZE) || req->cryptlen % SEMIBSIZE)
208 return -EINVAL;
209
210
211
212
213
214 block.A = cpu_to_be64(0xa6a6a6a6a6a6a6a6ULL);
215
216
217
218
219
220
221 src = req->src;
222 dst = req->dst;
223
224 for (i = 0; i < 6; i++) {
225 struct scatter_walk src_walk, dst_walk;
226 unsigned int nbytes = req->cryptlen;
227
228 scatterwalk_start(&src_walk, src);
229 scatterwalk_start(&dst_walk, dst);
230
231 while (nbytes) {
232
233 scatterwalk_copychunks(&block.R, &src_walk, SEMIBSIZE,
234 false);
235
236
237 crypto_cipher_encrypt_one(cipher, (u8 *)&block,
238 (u8 *)&block);
239
240 block.A ^= cpu_to_be64(t);
241 t++;
242
243
244 scatterwalk_copychunks(&block.R, &dst_walk, SEMIBSIZE,
245 true);
246
247 nbytes -= SEMIBSIZE;
248 }
249
250
251 src = req->dst;
252 dst = req->dst;
253 }
254
255
256 memcpy(req->iv, &block.A, SEMIBSIZE);
257
258 memzero_explicit(&block, sizeof(struct crypto_kw_block));
259
260 return 0;
261 }
262
263 static int crypto_kw_create(struct crypto_template *tmpl, struct rtattr **tb)
264 {
265 struct skcipher_instance *inst;
266 struct crypto_alg *alg;
267 int err;
268
269 inst = skcipher_alloc_instance_simple(tmpl, tb, &alg);
270 if (IS_ERR(inst))
271 return PTR_ERR(inst);
272
273 err = -EINVAL;
274
275 if (alg->cra_blocksize != sizeof(struct crypto_kw_block))
276 goto out_free_inst;
277
278 inst->alg.base.cra_blocksize = SEMIBSIZE;
279 inst->alg.base.cra_alignmask = 0;
280 inst->alg.ivsize = SEMIBSIZE;
281
282 inst->alg.encrypt = crypto_kw_encrypt;
283 inst->alg.decrypt = crypto_kw_decrypt;
284
285 err = skcipher_register_instance(tmpl, inst);
286 if (err)
287 goto out_free_inst;
288 goto out_put_alg;
289
290 out_free_inst:
291 inst->free(inst);
292 out_put_alg:
293 crypto_mod_put(alg);
294 return err;
295 }
296
297 static struct crypto_template crypto_kw_tmpl = {
298 .name = "kw",
299 .create = crypto_kw_create,
300 .module = THIS_MODULE,
301 };
302
303 static int __init crypto_kw_init(void)
304 {
305 return crypto_register_template(&crypto_kw_tmpl);
306 }
307
308 static void __exit crypto_kw_exit(void)
309 {
310 crypto_unregister_template(&crypto_kw_tmpl);
311 }
312
313 subsys_initcall(crypto_kw_init);
314 module_exit(crypto_kw_exit);
315
316 MODULE_LICENSE("Dual BSD/GPL");
317 MODULE_AUTHOR("Stephan Mueller <smueller@chronox.de>");
318 MODULE_DESCRIPTION("Key Wrapping (RFC3394 / NIST SP800-38F)");
319 MODULE_ALIAS_CRYPTO("kw");