]>
Commit | Line | Data |
---|---|---|
3d5b1ecd AZ |
1 | /* |
2 | * RSA padding templates. | |
3 | * | |
4 | * Copyright (c) 2015 Intel Corporation | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the Free | |
8 | * Software Foundation; either version 2 of the License, or (at your option) | |
9 | * any later version. | |
10 | */ | |
11 | ||
12 | #include <crypto/algapi.h> | |
13 | #include <crypto/akcipher.h> | |
14 | #include <crypto/internal/akcipher.h> | |
15 | #include <linux/err.h> | |
16 | #include <linux/init.h> | |
17 | #include <linux/kernel.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/random.h> | |
20 | ||
a49de377 TS |
21 | /* |
22 | * Hash algorithm OIDs plus ASN.1 DER wrappings [RFC4880 sec 5.2.2]. | |
23 | */ | |
24 | static const u8 rsa_digest_info_md5[] = { | |
25 | 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, | |
26 | 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, /* OID */ | |
27 | 0x05, 0x00, 0x04, 0x10 | |
28 | }; | |
29 | ||
30 | static const u8 rsa_digest_info_sha1[] = { | |
31 | 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, | |
32 | 0x2b, 0x0e, 0x03, 0x02, 0x1a, | |
33 | 0x05, 0x00, 0x04, 0x14 | |
34 | }; | |
35 | ||
36 | static const u8 rsa_digest_info_rmd160[] = { | |
37 | 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, | |
38 | 0x2b, 0x24, 0x03, 0x02, 0x01, | |
39 | 0x05, 0x00, 0x04, 0x14 | |
40 | }; | |
41 | ||
42 | static const u8 rsa_digest_info_sha224[] = { | |
43 | 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, | |
44 | 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, | |
45 | 0x05, 0x00, 0x04, 0x1c | |
46 | }; | |
47 | ||
48 | static const u8 rsa_digest_info_sha256[] = { | |
49 | 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, | |
50 | 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, | |
51 | 0x05, 0x00, 0x04, 0x20 | |
52 | }; | |
53 | ||
54 | static const u8 rsa_digest_info_sha384[] = { | |
55 | 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, | |
56 | 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, | |
57 | 0x05, 0x00, 0x04, 0x30 | |
58 | }; | |
59 | ||
60 | static const u8 rsa_digest_info_sha512[] = { | |
61 | 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, | |
62 | 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, | |
63 | 0x05, 0x00, 0x04, 0x40 | |
64 | }; | |
65 | ||
66 | static const struct rsa_asn1_template { | |
67 | const char *name; | |
68 | const u8 *data; | |
69 | size_t size; | |
70 | } rsa_asn1_templates[] = { | |
71 | #define _(X) { #X, rsa_digest_info_##X, sizeof(rsa_digest_info_##X) } | |
72 | _(md5), | |
73 | _(sha1), | |
74 | _(rmd160), | |
75 | _(sha256), | |
76 | _(sha384), | |
77 | _(sha512), | |
78 | _(sha224), | |
79 | { NULL } | |
80 | #undef _ | |
81 | }; | |
82 | ||
83 | static const struct rsa_asn1_template *rsa_lookup_asn1(const char *name) | |
84 | { | |
85 | const struct rsa_asn1_template *p; | |
86 | ||
87 | for (p = rsa_asn1_templates; p->name; p++) | |
88 | if (strcmp(name, p->name) == 0) | |
89 | return p; | |
90 | return NULL; | |
91 | } | |
92 | ||
3d5b1ecd AZ |
93 | struct pkcs1pad_ctx { |
94 | struct crypto_akcipher *child; | |
3d5b1ecd AZ |
95 | unsigned int key_size; |
96 | }; | |
97 | ||
a49de377 TS |
98 | struct pkcs1pad_inst_ctx { |
99 | struct crypto_akcipher_spawn spawn; | |
c0d20d22 | 100 | const struct rsa_asn1_template *digest_info; |
a49de377 TS |
101 | }; |
102 | ||
3d5b1ecd AZ |
103 | struct pkcs1pad_request { |
104 | struct akcipher_request child_req; | |
105 | ||
0f2c8319 | 106 | struct scatterlist in_sg[2], out_sg[1]; |
3d5b1ecd AZ |
107 | uint8_t *in_buf, *out_buf; |
108 | }; | |
109 | ||
110 | static int pkcs1pad_set_pub_key(struct crypto_akcipher *tfm, const void *key, | |
111 | unsigned int keylen) | |
112 | { | |
113 | struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); | |
114 | int err, size; | |
115 | ||
116 | err = crypto_akcipher_set_pub_key(ctx->child, key, keylen); | |
117 | ||
118 | if (!err) { | |
119 | /* Find out new modulus size from rsa implementation */ | |
120 | size = crypto_akcipher_maxsize(ctx->child); | |
121 | ||
122 | ctx->key_size = size > 0 ? size : 0; | |
123 | if (size <= 0) | |
124 | err = size; | |
125 | } | |
126 | ||
127 | return err; | |
128 | } | |
129 | ||
130 | static int pkcs1pad_set_priv_key(struct crypto_akcipher *tfm, const void *key, | |
131 | unsigned int keylen) | |
132 | { | |
133 | struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); | |
134 | int err, size; | |
135 | ||
136 | err = crypto_akcipher_set_priv_key(ctx->child, key, keylen); | |
137 | ||
138 | if (!err) { | |
139 | /* Find out new modulus size from rsa implementation */ | |
140 | size = crypto_akcipher_maxsize(ctx->child); | |
141 | ||
142 | ctx->key_size = size > 0 ? size : 0; | |
143 | if (size <= 0) | |
144 | err = size; | |
145 | } | |
146 | ||
147 | return err; | |
148 | } | |
149 | ||
150 | static int pkcs1pad_get_max_size(struct crypto_akcipher *tfm) | |
151 | { | |
152 | struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); | |
153 | ||
154 | /* | |
155 | * The maximum destination buffer size for the encrypt/sign operations | |
156 | * will be the same as for RSA, even though it's smaller for | |
157 | * decrypt/verify. | |
158 | */ | |
159 | ||
160 | return ctx->key_size ?: -EINVAL; | |
161 | } | |
162 | ||
163 | static void pkcs1pad_sg_set_buf(struct scatterlist *sg, void *buf, size_t len, | |
164 | struct scatterlist *next) | |
165 | { | |
0f2c8319 HX |
166 | int nsegs = next ? 2 : 1; |
167 | ||
168 | sg_init_table(sg, nsegs); | |
169 | sg_set_buf(sg, buf, len); | |
3d5b1ecd AZ |
170 | |
171 | if (next) | |
172 | sg_chain(sg, nsegs, next); | |
173 | } | |
174 | ||
175 | static int pkcs1pad_encrypt_sign_complete(struct akcipher_request *req, int err) | |
176 | { | |
177 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); | |
178 | struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); | |
179 | struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req); | |
5319216d AZ |
180 | size_t pad_len = ctx->key_size - req_ctx->child_req.dst_len; |
181 | size_t chunk_len, pad_left; | |
182 | struct sg_mapping_iter miter; | |
3d5b1ecd AZ |
183 | |
184 | if (!err) { | |
5319216d AZ |
185 | if (pad_len) { |
186 | sg_miter_start(&miter, req->dst, | |
187 | sg_nents_for_len(req->dst, pad_len), | |
188 | SG_MITER_ATOMIC | SG_MITER_TO_SG); | |
189 | ||
190 | pad_left = pad_len; | |
191 | while (pad_left) { | |
192 | sg_miter_next(&miter); | |
193 | ||
194 | chunk_len = min(miter.length, pad_left); | |
195 | memset(miter.addr, 0, chunk_len); | |
196 | pad_left -= chunk_len; | |
197 | } | |
198 | ||
199 | sg_miter_stop(&miter); | |
3d5b1ecd AZ |
200 | } |
201 | ||
202 | sg_pcopy_from_buffer(req->dst, | |
203 | sg_nents_for_len(req->dst, ctx->key_size), | |
204 | req_ctx->out_buf, req_ctx->child_req.dst_len, | |
5319216d | 205 | pad_len); |
3d5b1ecd AZ |
206 | } |
207 | req->dst_len = ctx->key_size; | |
208 | ||
209 | kfree(req_ctx->in_buf); | |
210 | kzfree(req_ctx->out_buf); | |
211 | ||
212 | return err; | |
213 | } | |
214 | ||
215 | static void pkcs1pad_encrypt_sign_complete_cb( | |
216 | struct crypto_async_request *child_async_req, int err) | |
217 | { | |
218 | struct akcipher_request *req = child_async_req->data; | |
219 | struct crypto_async_request async_req; | |
220 | ||
221 | if (err == -EINPROGRESS) | |
222 | return; | |
223 | ||
224 | async_req.data = req->base.data; | |
225 | async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req)); | |
226 | async_req.flags = child_async_req->flags; | |
227 | req->base.complete(&async_req, | |
228 | pkcs1pad_encrypt_sign_complete(req, err)); | |
229 | } | |
230 | ||
231 | static int pkcs1pad_encrypt(struct akcipher_request *req) | |
232 | { | |
233 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); | |
234 | struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); | |
235 | struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req); | |
236 | int err; | |
237 | unsigned int i, ps_end; | |
238 | ||
239 | if (!ctx->key_size) | |
240 | return -EINVAL; | |
241 | ||
242 | if (req->src_len > ctx->key_size - 11) | |
243 | return -EOVERFLOW; | |
244 | ||
245 | if (req->dst_len < ctx->key_size) { | |
246 | req->dst_len = ctx->key_size; | |
247 | return -EOVERFLOW; | |
248 | } | |
249 | ||
250 | if (ctx->key_size > PAGE_SIZE) | |
251 | return -ENOTSUPP; | |
252 | ||
253 | /* | |
254 | * Replace both input and output to add the padding in the input and | |
255 | * the potential missing leading zeros in the output. | |
256 | */ | |
257 | req_ctx->child_req.src = req_ctx->in_sg; | |
258 | req_ctx->child_req.src_len = ctx->key_size - 1; | |
259 | req_ctx->child_req.dst = req_ctx->out_sg; | |
260 | req_ctx->child_req.dst_len = ctx->key_size; | |
261 | ||
262 | req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len, | |
263 | (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? | |
264 | GFP_KERNEL : GFP_ATOMIC); | |
265 | if (!req_ctx->in_buf) | |
266 | return -ENOMEM; | |
267 | ||
268 | ps_end = ctx->key_size - req->src_len - 2; | |
269 | req_ctx->in_buf[0] = 0x02; | |
270 | for (i = 1; i < ps_end; i++) | |
271 | req_ctx->in_buf[i] = 1 + prandom_u32_max(255); | |
272 | req_ctx->in_buf[ps_end] = 0x00; | |
273 | ||
274 | pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf, | |
275 | ctx->key_size - 1 - req->src_len, req->src); | |
276 | ||
277 | req_ctx->out_buf = kmalloc(ctx->key_size, | |
278 | (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? | |
279 | GFP_KERNEL : GFP_ATOMIC); | |
280 | if (!req_ctx->out_buf) { | |
281 | kfree(req_ctx->in_buf); | |
282 | return -ENOMEM; | |
283 | } | |
284 | ||
285 | pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf, | |
286 | ctx->key_size, NULL); | |
287 | ||
288 | akcipher_request_set_tfm(&req_ctx->child_req, ctx->child); | |
289 | akcipher_request_set_callback(&req_ctx->child_req, req->base.flags, | |
290 | pkcs1pad_encrypt_sign_complete_cb, req); | |
291 | ||
292 | err = crypto_akcipher_encrypt(&req_ctx->child_req); | |
293 | if (err != -EINPROGRESS && | |
294 | (err != -EBUSY || | |
295 | !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))) | |
296 | return pkcs1pad_encrypt_sign_complete(req, err); | |
297 | ||
298 | return err; | |
299 | } | |
300 | ||
301 | static int pkcs1pad_decrypt_complete(struct akcipher_request *req, int err) | |
302 | { | |
303 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); | |
304 | struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); | |
305 | struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req); | |
306 | unsigned int pos; | |
307 | ||
308 | if (err == -EOVERFLOW) | |
309 | /* Decrypted value had no leading 0 byte */ | |
310 | err = -EINVAL; | |
311 | ||
312 | if (err) | |
313 | goto done; | |
314 | ||
315 | if (req_ctx->child_req.dst_len != ctx->key_size - 1) { | |
316 | err = -EINVAL; | |
317 | goto done; | |
318 | } | |
319 | ||
320 | if (req_ctx->out_buf[0] != 0x02) { | |
321 | err = -EINVAL; | |
322 | goto done; | |
323 | } | |
324 | for (pos = 1; pos < req_ctx->child_req.dst_len; pos++) | |
325 | if (req_ctx->out_buf[pos] == 0x00) | |
326 | break; | |
327 | if (pos < 9 || pos == req_ctx->child_req.dst_len) { | |
328 | err = -EINVAL; | |
329 | goto done; | |
330 | } | |
331 | pos++; | |
332 | ||
333 | if (req->dst_len < req_ctx->child_req.dst_len - pos) | |
334 | err = -EOVERFLOW; | |
335 | req->dst_len = req_ctx->child_req.dst_len - pos; | |
336 | ||
337 | if (!err) | |
338 | sg_copy_from_buffer(req->dst, | |
339 | sg_nents_for_len(req->dst, req->dst_len), | |
340 | req_ctx->out_buf + pos, req->dst_len); | |
341 | ||
342 | done: | |
343 | kzfree(req_ctx->out_buf); | |
344 | ||
345 | return err; | |
346 | } | |
347 | ||
348 | static void pkcs1pad_decrypt_complete_cb( | |
349 | struct crypto_async_request *child_async_req, int err) | |
350 | { | |
351 | struct akcipher_request *req = child_async_req->data; | |
352 | struct crypto_async_request async_req; | |
353 | ||
354 | if (err == -EINPROGRESS) | |
355 | return; | |
356 | ||
357 | async_req.data = req->base.data; | |
358 | async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req)); | |
359 | async_req.flags = child_async_req->flags; | |
360 | req->base.complete(&async_req, pkcs1pad_decrypt_complete(req, err)); | |
361 | } | |
362 | ||
363 | static int pkcs1pad_decrypt(struct akcipher_request *req) | |
364 | { | |
365 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); | |
366 | struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); | |
367 | struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req); | |
368 | int err; | |
369 | ||
370 | if (!ctx->key_size || req->src_len != ctx->key_size) | |
371 | return -EINVAL; | |
372 | ||
373 | if (ctx->key_size > PAGE_SIZE) | |
374 | return -ENOTSUPP; | |
375 | ||
376 | /* Reuse input buffer, output to a new buffer */ | |
377 | req_ctx->child_req.src = req->src; | |
378 | req_ctx->child_req.src_len = req->src_len; | |
379 | req_ctx->child_req.dst = req_ctx->out_sg; | |
6f0904ad | 380 | req_ctx->child_req.dst_len = ctx->key_size ; |
3d5b1ecd | 381 | |
6f0904ad | 382 | req_ctx->out_buf = kmalloc(ctx->key_size, |
3d5b1ecd AZ |
383 | (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? |
384 | GFP_KERNEL : GFP_ATOMIC); | |
385 | if (!req_ctx->out_buf) | |
386 | return -ENOMEM; | |
387 | ||
388 | pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf, | |
6f0904ad | 389 | ctx->key_size, NULL); |
3d5b1ecd AZ |
390 | |
391 | akcipher_request_set_tfm(&req_ctx->child_req, ctx->child); | |
392 | akcipher_request_set_callback(&req_ctx->child_req, req->base.flags, | |
393 | pkcs1pad_decrypt_complete_cb, req); | |
394 | ||
395 | err = crypto_akcipher_decrypt(&req_ctx->child_req); | |
396 | if (err != -EINPROGRESS && | |
397 | (err != -EBUSY || | |
398 | !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))) | |
399 | return pkcs1pad_decrypt_complete(req, err); | |
400 | ||
401 | return err; | |
402 | } | |
403 | ||
404 | static int pkcs1pad_sign(struct akcipher_request *req) | |
405 | { | |
406 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); | |
407 | struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); | |
408 | struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req); | |
c0d20d22 HX |
409 | struct akcipher_instance *inst = akcipher_alg_instance(tfm); |
410 | struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst); | |
411 | const struct rsa_asn1_template *digest_info = ictx->digest_info; | |
3d5b1ecd | 412 | int err; |
a49de377 | 413 | unsigned int ps_end, digest_size = 0; |
3d5b1ecd AZ |
414 | |
415 | if (!ctx->key_size) | |
416 | return -EINVAL; | |
417 | ||
c0d20d22 | 418 | digest_size = digest_info->size; |
a49de377 TS |
419 | |
420 | if (req->src_len + digest_size > ctx->key_size - 11) | |
3d5b1ecd AZ |
421 | return -EOVERFLOW; |
422 | ||
423 | if (req->dst_len < ctx->key_size) { | |
424 | req->dst_len = ctx->key_size; | |
425 | return -EOVERFLOW; | |
426 | } | |
427 | ||
428 | if (ctx->key_size > PAGE_SIZE) | |
429 | return -ENOTSUPP; | |
430 | ||
431 | /* | |
432 | * Replace both input and output to add the padding in the input and | |
433 | * the potential missing leading zeros in the output. | |
434 | */ | |
435 | req_ctx->child_req.src = req_ctx->in_sg; | |
436 | req_ctx->child_req.src_len = ctx->key_size - 1; | |
437 | req_ctx->child_req.dst = req_ctx->out_sg; | |
438 | req_ctx->child_req.dst_len = ctx->key_size; | |
439 | ||
440 | req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len, | |
441 | (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? | |
442 | GFP_KERNEL : GFP_ATOMIC); | |
443 | if (!req_ctx->in_buf) | |
444 | return -ENOMEM; | |
445 | ||
a49de377 | 446 | ps_end = ctx->key_size - digest_size - req->src_len - 2; |
3d5b1ecd AZ |
447 | req_ctx->in_buf[0] = 0x01; |
448 | memset(req_ctx->in_buf + 1, 0xff, ps_end - 1); | |
449 | req_ctx->in_buf[ps_end] = 0x00; | |
450 | ||
c0d20d22 HX |
451 | memcpy(req_ctx->in_buf + ps_end + 1, digest_info->data, |
452 | digest_info->size); | |
a49de377 | 453 | |
3d5b1ecd AZ |
454 | pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf, |
455 | ctx->key_size - 1 - req->src_len, req->src); | |
456 | ||
457 | req_ctx->out_buf = kmalloc(ctx->key_size, | |
458 | (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? | |
459 | GFP_KERNEL : GFP_ATOMIC); | |
460 | if (!req_ctx->out_buf) { | |
461 | kfree(req_ctx->in_buf); | |
462 | return -ENOMEM; | |
463 | } | |
464 | ||
465 | pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf, | |
466 | ctx->key_size, NULL); | |
467 | ||
468 | akcipher_request_set_tfm(&req_ctx->child_req, ctx->child); | |
469 | akcipher_request_set_callback(&req_ctx->child_req, req->base.flags, | |
470 | pkcs1pad_encrypt_sign_complete_cb, req); | |
471 | ||
472 | err = crypto_akcipher_sign(&req_ctx->child_req); | |
473 | if (err != -EINPROGRESS && | |
474 | (err != -EBUSY || | |
475 | !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))) | |
476 | return pkcs1pad_encrypt_sign_complete(req, err); | |
477 | ||
478 | return err; | |
479 | } | |
480 | ||
481 | static int pkcs1pad_verify_complete(struct akcipher_request *req, int err) | |
482 | { | |
483 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); | |
484 | struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); | |
485 | struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req); | |
c0d20d22 HX |
486 | struct akcipher_instance *inst = akcipher_alg_instance(tfm); |
487 | struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst); | |
488 | const struct rsa_asn1_template *digest_info = ictx->digest_info; | |
3d5b1ecd AZ |
489 | unsigned int pos; |
490 | ||
491 | if (err == -EOVERFLOW) | |
492 | /* Decrypted value had no leading 0 byte */ | |
493 | err = -EINVAL; | |
494 | ||
495 | if (err) | |
496 | goto done; | |
497 | ||
498 | if (req_ctx->child_req.dst_len != ctx->key_size - 1) { | |
499 | err = -EINVAL; | |
500 | goto done; | |
501 | } | |
502 | ||
a49de377 TS |
503 | err = -EBADMSG; |
504 | if (req_ctx->out_buf[0] != 0x01) | |
3d5b1ecd | 505 | goto done; |
a49de377 | 506 | |
3d5b1ecd AZ |
507 | for (pos = 1; pos < req_ctx->child_req.dst_len; pos++) |
508 | if (req_ctx->out_buf[pos] != 0xff) | |
509 | break; | |
a49de377 | 510 | |
3d5b1ecd | 511 | if (pos < 9 || pos == req_ctx->child_req.dst_len || |
a49de377 | 512 | req_ctx->out_buf[pos] != 0x00) |
3d5b1ecd | 513 | goto done; |
3d5b1ecd AZ |
514 | pos++; |
515 | ||
c0d20d22 HX |
516 | if (memcmp(req_ctx->out_buf + pos, digest_info->data, |
517 | digest_info->size)) | |
518 | goto done; | |
a49de377 | 519 | |
c0d20d22 | 520 | pos += digest_info->size; |
a49de377 TS |
521 | |
522 | err = 0; | |
523 | ||
3d5b1ecd AZ |
524 | if (req->dst_len < req_ctx->child_req.dst_len - pos) |
525 | err = -EOVERFLOW; | |
526 | req->dst_len = req_ctx->child_req.dst_len - pos; | |
527 | ||
528 | if (!err) | |
529 | sg_copy_from_buffer(req->dst, | |
530 | sg_nents_for_len(req->dst, req->dst_len), | |
531 | req_ctx->out_buf + pos, req->dst_len); | |
3d5b1ecd AZ |
532 | done: |
533 | kzfree(req_ctx->out_buf); | |
534 | ||
535 | return err; | |
536 | } | |
537 | ||
538 | static void pkcs1pad_verify_complete_cb( | |
539 | struct crypto_async_request *child_async_req, int err) | |
540 | { | |
541 | struct akcipher_request *req = child_async_req->data; | |
542 | struct crypto_async_request async_req; | |
543 | ||
544 | if (err == -EINPROGRESS) | |
545 | return; | |
546 | ||
547 | async_req.data = req->base.data; | |
548 | async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req)); | |
549 | async_req.flags = child_async_req->flags; | |
550 | req->base.complete(&async_req, pkcs1pad_verify_complete(req, err)); | |
551 | } | |
552 | ||
553 | /* | |
554 | * The verify operation is here for completeness similar to the verification | |
555 | * defined in RFC2313 section 10.2 except that block type 0 is not accepted, | |
556 | * as in RFC2437. RFC2437 section 9.2 doesn't define any operation to | |
557 | * retrieve the DigestInfo from a signature, instead the user is expected | |
558 | * to call the sign operation to generate the expected signature and compare | |
559 | * signatures instead of the message-digests. | |
560 | */ | |
561 | static int pkcs1pad_verify(struct akcipher_request *req) | |
562 | { | |
563 | struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); | |
564 | struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); | |
565 | struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req); | |
566 | int err; | |
567 | ||
a49de377 | 568 | if (!ctx->key_size || req->src_len < ctx->key_size) |
3d5b1ecd AZ |
569 | return -EINVAL; |
570 | ||
571 | if (ctx->key_size > PAGE_SIZE) | |
572 | return -ENOTSUPP; | |
573 | ||
574 | /* Reuse input buffer, output to a new buffer */ | |
575 | req_ctx->child_req.src = req->src; | |
576 | req_ctx->child_req.src_len = req->src_len; | |
577 | req_ctx->child_req.dst = req_ctx->out_sg; | |
6f0904ad | 578 | req_ctx->child_req.dst_len = ctx->key_size; |
3d5b1ecd | 579 | |
6f0904ad | 580 | req_ctx->out_buf = kmalloc(ctx->key_size, |
3d5b1ecd AZ |
581 | (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? |
582 | GFP_KERNEL : GFP_ATOMIC); | |
583 | if (!req_ctx->out_buf) | |
584 | return -ENOMEM; | |
585 | ||
586 | pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf, | |
6f0904ad | 587 | ctx->key_size, NULL); |
3d5b1ecd AZ |
588 | |
589 | akcipher_request_set_tfm(&req_ctx->child_req, ctx->child); | |
590 | akcipher_request_set_callback(&req_ctx->child_req, req->base.flags, | |
591 | pkcs1pad_verify_complete_cb, req); | |
592 | ||
593 | err = crypto_akcipher_verify(&req_ctx->child_req); | |
594 | if (err != -EINPROGRESS && | |
595 | (err != -EBUSY || | |
596 | !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))) | |
597 | return pkcs1pad_verify_complete(req, err); | |
598 | ||
599 | return err; | |
600 | } | |
601 | ||
602 | static int pkcs1pad_init_tfm(struct crypto_akcipher *tfm) | |
603 | { | |
604 | struct akcipher_instance *inst = akcipher_alg_instance(tfm); | |
a49de377 | 605 | struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst); |
3d5b1ecd AZ |
606 | struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); |
607 | struct crypto_akcipher *child_tfm; | |
608 | ||
c0d20d22 | 609 | child_tfm = crypto_spawn_akcipher(&ictx->spawn); |
3d5b1ecd AZ |
610 | if (IS_ERR(child_tfm)) |
611 | return PTR_ERR(child_tfm); | |
612 | ||
613 | ctx->child = child_tfm; | |
3d5b1ecd AZ |
614 | return 0; |
615 | } | |
616 | ||
617 | static void pkcs1pad_exit_tfm(struct crypto_akcipher *tfm) | |
618 | { | |
619 | struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); | |
620 | ||
621 | crypto_free_akcipher(ctx->child); | |
622 | } | |
623 | ||
624 | static void pkcs1pad_free(struct akcipher_instance *inst) | |
625 | { | |
a49de377 TS |
626 | struct pkcs1pad_inst_ctx *ctx = akcipher_instance_ctx(inst); |
627 | struct crypto_akcipher_spawn *spawn = &ctx->spawn; | |
3d5b1ecd AZ |
628 | |
629 | crypto_drop_akcipher(spawn); | |
3d5b1ecd AZ |
630 | kfree(inst); |
631 | } | |
632 | ||
633 | static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb) | |
634 | { | |
c0d20d22 | 635 | const struct rsa_asn1_template *digest_info; |
3d5b1ecd AZ |
636 | struct crypto_attr_type *algt; |
637 | struct akcipher_instance *inst; | |
a49de377 | 638 | struct pkcs1pad_inst_ctx *ctx; |
3d5b1ecd AZ |
639 | struct crypto_akcipher_spawn *spawn; |
640 | struct akcipher_alg *rsa_alg; | |
641 | const char *rsa_alg_name; | |
a49de377 | 642 | const char *hash_name; |
3d5b1ecd AZ |
643 | int err; |
644 | ||
645 | algt = crypto_get_attr_type(tb); | |
646 | if (IS_ERR(algt)) | |
647 | return PTR_ERR(algt); | |
648 | ||
649 | if ((algt->type ^ CRYPTO_ALG_TYPE_AKCIPHER) & algt->mask) | |
650 | return -EINVAL; | |
651 | ||
652 | rsa_alg_name = crypto_attr_alg_name(tb[1]); | |
653 | if (IS_ERR(rsa_alg_name)) | |
654 | return PTR_ERR(rsa_alg_name); | |
655 | ||
a49de377 TS |
656 | hash_name = crypto_attr_alg_name(tb[2]); |
657 | if (IS_ERR(hash_name)) | |
c0d20d22 HX |
658 | return PTR_ERR(hash_name); |
659 | ||
660 | digest_info = rsa_lookup_asn1(hash_name); | |
661 | if (!digest_info) | |
662 | return -EINVAL; | |
a49de377 TS |
663 | |
664 | inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); | |
3d5b1ecd AZ |
665 | if (!inst) |
666 | return -ENOMEM; | |
667 | ||
a49de377 TS |
668 | ctx = akcipher_instance_ctx(inst); |
669 | spawn = &ctx->spawn; | |
c0d20d22 | 670 | ctx->digest_info = digest_info; |
a49de377 | 671 | |
3d5b1ecd AZ |
672 | crypto_set_spawn(&spawn->base, akcipher_crypto_instance(inst)); |
673 | err = crypto_grab_akcipher(spawn, rsa_alg_name, 0, | |
674 | crypto_requires_sync(algt->type, algt->mask)); | |
675 | if (err) | |
676 | goto out_free_inst; | |
677 | ||
678 | rsa_alg = crypto_spawn_akcipher_alg(spawn); | |
679 | ||
680 | err = -ENAMETOOLONG; | |
a49de377 | 681 | |
c0d20d22 HX |
682 | if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, |
683 | "pkcs1pad(%s,%s)", rsa_alg->base.cra_name, hash_name) >= | |
684 | CRYPTO_MAX_ALG_NAME || | |
685 | snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, | |
686 | "pkcs1pad(%s,%s)", | |
687 | rsa_alg->base.cra_driver_name, hash_name) >= | |
688 | CRYPTO_MAX_ALG_NAME) | |
3d5b1ecd AZ |
689 | goto out_drop_alg; |
690 | ||
691 | inst->alg.base.cra_flags = rsa_alg->base.cra_flags & CRYPTO_ALG_ASYNC; | |
692 | inst->alg.base.cra_priority = rsa_alg->base.cra_priority; | |
693 | inst->alg.base.cra_ctxsize = sizeof(struct pkcs1pad_ctx); | |
694 | ||
695 | inst->alg.init = pkcs1pad_init_tfm; | |
696 | inst->alg.exit = pkcs1pad_exit_tfm; | |
697 | ||
698 | inst->alg.encrypt = pkcs1pad_encrypt; | |
699 | inst->alg.decrypt = pkcs1pad_decrypt; | |
700 | inst->alg.sign = pkcs1pad_sign; | |
701 | inst->alg.verify = pkcs1pad_verify; | |
702 | inst->alg.set_pub_key = pkcs1pad_set_pub_key; | |
703 | inst->alg.set_priv_key = pkcs1pad_set_priv_key; | |
704 | inst->alg.max_size = pkcs1pad_get_max_size; | |
705 | inst->alg.reqsize = sizeof(struct pkcs1pad_request) + rsa_alg->reqsize; | |
706 | ||
707 | inst->free = pkcs1pad_free; | |
708 | ||
709 | err = akcipher_register_instance(tmpl, inst); | |
710 | if (err) | |
c0d20d22 | 711 | goto out_drop_alg; |
3d5b1ecd AZ |
712 | |
713 | return 0; | |
714 | ||
715 | out_drop_alg: | |
716 | crypto_drop_akcipher(spawn); | |
717 | out_free_inst: | |
718 | kfree(inst); | |
719 | return err; | |
720 | } | |
721 | ||
722 | struct crypto_template rsa_pkcs1pad_tmpl = { | |
723 | .name = "pkcs1pad", | |
724 | .create = pkcs1pad_create, | |
725 | .module = THIS_MODULE, | |
726 | }; |