blob: 5c1c78e21f841b88fef4702fe0b79c30d540258c [file] [log] [blame]
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +01001/*
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
Tadeusz Struka49de372016-03-03 21:49:26 +000021/*
22 * Hash algorithm OIDs plus ASN.1 DER wrappings [RFC4880 sec 5.2.2].
23 */
24static 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
30static 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
36static 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
42static 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
48static 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
54static 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
60static 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
66static 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
83static 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
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +010093struct pkcs1pad_ctx {
94 struct crypto_akcipher *child;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +010095 unsigned int key_size;
96};
97
Tadeusz Struka49de372016-03-03 21:49:26 +000098struct pkcs1pad_inst_ctx {
99 struct crypto_akcipher_spawn spawn;
Herbert Xuc0d20d22016-06-29 19:32:23 +0800100 const struct rsa_asn1_template *digest_info;
Tadeusz Struka49de372016-03-03 21:49:26 +0000101};
102
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100103struct pkcs1pad_request {
104 struct akcipher_request child_req;
105
106 struct scatterlist in_sg[3], out_sg[2];
107 uint8_t *in_buf, *out_buf;
108};
109
110static 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
130static 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
150static 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
163static void pkcs1pad_sg_set_buf(struct scatterlist *sg, void *buf, size_t len,
164 struct scatterlist *next)
165{
166 int nsegs = next ? 1 : 0;
167
168 if (offset_in_page(buf) + len <= PAGE_SIZE) {
169 nsegs += 1;
170 sg_init_table(sg, nsegs);
171 sg_set_buf(sg, buf, len);
172 } else {
173 nsegs += 2;
174 sg_init_table(sg, nsegs);
175 sg_set_buf(sg + 0, buf, PAGE_SIZE - offset_in_page(buf));
176 sg_set_buf(sg + 1, buf + PAGE_SIZE - offset_in_page(buf),
177 offset_in_page(buf) + len - PAGE_SIZE);
178 }
179
180 if (next)
181 sg_chain(sg, nsegs, next);
182}
183
184static int pkcs1pad_encrypt_sign_complete(struct akcipher_request *req, int err)
185{
186 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
187 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
188 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
Andrzej Zaborowski53192162015-12-12 00:03:51 -0500189 size_t pad_len = ctx->key_size - req_ctx->child_req.dst_len;
190 size_t chunk_len, pad_left;
191 struct sg_mapping_iter miter;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100192
193 if (!err) {
Andrzej Zaborowski53192162015-12-12 00:03:51 -0500194 if (pad_len) {
195 sg_miter_start(&miter, req->dst,
196 sg_nents_for_len(req->dst, pad_len),
197 SG_MITER_ATOMIC | SG_MITER_TO_SG);
198
199 pad_left = pad_len;
200 while (pad_left) {
201 sg_miter_next(&miter);
202
203 chunk_len = min(miter.length, pad_left);
204 memset(miter.addr, 0, chunk_len);
205 pad_left -= chunk_len;
206 }
207
208 sg_miter_stop(&miter);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100209 }
210
211 sg_pcopy_from_buffer(req->dst,
212 sg_nents_for_len(req->dst, ctx->key_size),
213 req_ctx->out_buf, req_ctx->child_req.dst_len,
Andrzej Zaborowski53192162015-12-12 00:03:51 -0500214 pad_len);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100215 }
216 req->dst_len = ctx->key_size;
217
218 kfree(req_ctx->in_buf);
219 kzfree(req_ctx->out_buf);
220
221 return err;
222}
223
224static void pkcs1pad_encrypt_sign_complete_cb(
225 struct crypto_async_request *child_async_req, int err)
226{
227 struct akcipher_request *req = child_async_req->data;
228 struct crypto_async_request async_req;
229
230 if (err == -EINPROGRESS)
231 return;
232
233 async_req.data = req->base.data;
234 async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
235 async_req.flags = child_async_req->flags;
236 req->base.complete(&async_req,
237 pkcs1pad_encrypt_sign_complete(req, err));
238}
239
240static int pkcs1pad_encrypt(struct akcipher_request *req)
241{
242 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
243 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
244 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
245 int err;
246 unsigned int i, ps_end;
247
248 if (!ctx->key_size)
249 return -EINVAL;
250
251 if (req->src_len > ctx->key_size - 11)
252 return -EOVERFLOW;
253
254 if (req->dst_len < ctx->key_size) {
255 req->dst_len = ctx->key_size;
256 return -EOVERFLOW;
257 }
258
259 if (ctx->key_size > PAGE_SIZE)
260 return -ENOTSUPP;
261
262 /*
263 * Replace both input and output to add the padding in the input and
264 * the potential missing leading zeros in the output.
265 */
266 req_ctx->child_req.src = req_ctx->in_sg;
267 req_ctx->child_req.src_len = ctx->key_size - 1;
268 req_ctx->child_req.dst = req_ctx->out_sg;
269 req_ctx->child_req.dst_len = ctx->key_size;
270
271 req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len,
272 (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
273 GFP_KERNEL : GFP_ATOMIC);
274 if (!req_ctx->in_buf)
275 return -ENOMEM;
276
277 ps_end = ctx->key_size - req->src_len - 2;
278 req_ctx->in_buf[0] = 0x02;
279 for (i = 1; i < ps_end; i++)
280 req_ctx->in_buf[i] = 1 + prandom_u32_max(255);
281 req_ctx->in_buf[ps_end] = 0x00;
282
283 pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
284 ctx->key_size - 1 - req->src_len, req->src);
285
286 req_ctx->out_buf = kmalloc(ctx->key_size,
287 (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
288 GFP_KERNEL : GFP_ATOMIC);
289 if (!req_ctx->out_buf) {
290 kfree(req_ctx->in_buf);
291 return -ENOMEM;
292 }
293
294 pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
295 ctx->key_size, NULL);
296
297 akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
298 akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
299 pkcs1pad_encrypt_sign_complete_cb, req);
300
301 err = crypto_akcipher_encrypt(&req_ctx->child_req);
302 if (err != -EINPROGRESS &&
303 (err != -EBUSY ||
304 !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))
305 return pkcs1pad_encrypt_sign_complete(req, err);
306
307 return err;
308}
309
310static int pkcs1pad_decrypt_complete(struct akcipher_request *req, int err)
311{
312 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
313 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
314 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
315 unsigned int pos;
316
317 if (err == -EOVERFLOW)
318 /* Decrypted value had no leading 0 byte */
319 err = -EINVAL;
320
321 if (err)
322 goto done;
323
324 if (req_ctx->child_req.dst_len != ctx->key_size - 1) {
325 err = -EINVAL;
326 goto done;
327 }
328
329 if (req_ctx->out_buf[0] != 0x02) {
330 err = -EINVAL;
331 goto done;
332 }
333 for (pos = 1; pos < req_ctx->child_req.dst_len; pos++)
334 if (req_ctx->out_buf[pos] == 0x00)
335 break;
336 if (pos < 9 || pos == req_ctx->child_req.dst_len) {
337 err = -EINVAL;
338 goto done;
339 }
340 pos++;
341
342 if (req->dst_len < req_ctx->child_req.dst_len - pos)
343 err = -EOVERFLOW;
344 req->dst_len = req_ctx->child_req.dst_len - pos;
345
346 if (!err)
347 sg_copy_from_buffer(req->dst,
348 sg_nents_for_len(req->dst, req->dst_len),
349 req_ctx->out_buf + pos, req->dst_len);
350
351done:
352 kzfree(req_ctx->out_buf);
353
354 return err;
355}
356
357static void pkcs1pad_decrypt_complete_cb(
358 struct crypto_async_request *child_async_req, int err)
359{
360 struct akcipher_request *req = child_async_req->data;
361 struct crypto_async_request async_req;
362
363 if (err == -EINPROGRESS)
364 return;
365
366 async_req.data = req->base.data;
367 async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
368 async_req.flags = child_async_req->flags;
369 req->base.complete(&async_req, pkcs1pad_decrypt_complete(req, err));
370}
371
372static int pkcs1pad_decrypt(struct akcipher_request *req)
373{
374 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
375 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
376 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
377 int err;
378
379 if (!ctx->key_size || req->src_len != ctx->key_size)
380 return -EINVAL;
381
382 if (ctx->key_size > PAGE_SIZE)
383 return -ENOTSUPP;
384
385 /* Reuse input buffer, output to a new buffer */
386 req_ctx->child_req.src = req->src;
387 req_ctx->child_req.src_len = req->src_len;
388 req_ctx->child_req.dst = req_ctx->out_sg;
Tadeusz Struk6f0904a2016-04-06 14:42:32 -0700389 req_ctx->child_req.dst_len = ctx->key_size ;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100390
Tadeusz Struk6f0904a2016-04-06 14:42:32 -0700391 req_ctx->out_buf = kmalloc(ctx->key_size,
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100392 (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
393 GFP_KERNEL : GFP_ATOMIC);
394 if (!req_ctx->out_buf)
395 return -ENOMEM;
396
397 pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
Tadeusz Struk6f0904a2016-04-06 14:42:32 -0700398 ctx->key_size, NULL);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100399
400 akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
401 akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
402 pkcs1pad_decrypt_complete_cb, req);
403
404 err = crypto_akcipher_decrypt(&req_ctx->child_req);
405 if (err != -EINPROGRESS &&
406 (err != -EBUSY ||
407 !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))
408 return pkcs1pad_decrypt_complete(req, err);
409
410 return err;
411}
412
413static int pkcs1pad_sign(struct akcipher_request *req)
414{
415 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
416 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
417 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
Herbert Xuc0d20d22016-06-29 19:32:23 +0800418 struct akcipher_instance *inst = akcipher_alg_instance(tfm);
419 struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
420 const struct rsa_asn1_template *digest_info = ictx->digest_info;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100421 int err;
Tadeusz Struka49de372016-03-03 21:49:26 +0000422 unsigned int ps_end, digest_size = 0;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100423
424 if (!ctx->key_size)
425 return -EINVAL;
426
Herbert Xuc0d20d22016-06-29 19:32:23 +0800427 digest_size = digest_info->size;
Tadeusz Struka49de372016-03-03 21:49:26 +0000428
429 if (req->src_len + digest_size > ctx->key_size - 11)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100430 return -EOVERFLOW;
431
432 if (req->dst_len < ctx->key_size) {
433 req->dst_len = ctx->key_size;
434 return -EOVERFLOW;
435 }
436
437 if (ctx->key_size > PAGE_SIZE)
438 return -ENOTSUPP;
439
440 /*
441 * Replace both input and output to add the padding in the input and
442 * the potential missing leading zeros in the output.
443 */
444 req_ctx->child_req.src = req_ctx->in_sg;
445 req_ctx->child_req.src_len = ctx->key_size - 1;
446 req_ctx->child_req.dst = req_ctx->out_sg;
447 req_ctx->child_req.dst_len = ctx->key_size;
448
449 req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len,
450 (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
451 GFP_KERNEL : GFP_ATOMIC);
452 if (!req_ctx->in_buf)
453 return -ENOMEM;
454
Tadeusz Struka49de372016-03-03 21:49:26 +0000455 ps_end = ctx->key_size - digest_size - req->src_len - 2;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100456 req_ctx->in_buf[0] = 0x01;
457 memset(req_ctx->in_buf + 1, 0xff, ps_end - 1);
458 req_ctx->in_buf[ps_end] = 0x00;
459
Herbert Xuc0d20d22016-06-29 19:32:23 +0800460 memcpy(req_ctx->in_buf + ps_end + 1, digest_info->data,
461 digest_info->size);
Tadeusz Struka49de372016-03-03 21:49:26 +0000462
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100463 pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
464 ctx->key_size - 1 - req->src_len, req->src);
465
466 req_ctx->out_buf = kmalloc(ctx->key_size,
467 (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
468 GFP_KERNEL : GFP_ATOMIC);
469 if (!req_ctx->out_buf) {
470 kfree(req_ctx->in_buf);
471 return -ENOMEM;
472 }
473
474 pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
475 ctx->key_size, NULL);
476
477 akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
478 akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
479 pkcs1pad_encrypt_sign_complete_cb, req);
480
481 err = crypto_akcipher_sign(&req_ctx->child_req);
482 if (err != -EINPROGRESS &&
483 (err != -EBUSY ||
484 !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))
485 return pkcs1pad_encrypt_sign_complete(req, err);
486
487 return err;
488}
489
490static int pkcs1pad_verify_complete(struct akcipher_request *req, int err)
491{
492 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
493 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
494 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
Herbert Xuc0d20d22016-06-29 19:32:23 +0800495 struct akcipher_instance *inst = akcipher_alg_instance(tfm);
496 struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
497 const struct rsa_asn1_template *digest_info = ictx->digest_info;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100498 unsigned int pos;
499
500 if (err == -EOVERFLOW)
501 /* Decrypted value had no leading 0 byte */
502 err = -EINVAL;
503
504 if (err)
505 goto done;
506
507 if (req_ctx->child_req.dst_len != ctx->key_size - 1) {
508 err = -EINVAL;
509 goto done;
510 }
511
Tadeusz Struka49de372016-03-03 21:49:26 +0000512 err = -EBADMSG;
513 if (req_ctx->out_buf[0] != 0x01)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100514 goto done;
Tadeusz Struka49de372016-03-03 21:49:26 +0000515
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100516 for (pos = 1; pos < req_ctx->child_req.dst_len; pos++)
517 if (req_ctx->out_buf[pos] != 0xff)
518 break;
Tadeusz Struka49de372016-03-03 21:49:26 +0000519
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100520 if (pos < 9 || pos == req_ctx->child_req.dst_len ||
Tadeusz Struka49de372016-03-03 21:49:26 +0000521 req_ctx->out_buf[pos] != 0x00)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100522 goto done;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100523 pos++;
524
Herbert Xuc0d20d22016-06-29 19:32:23 +0800525 if (memcmp(req_ctx->out_buf + pos, digest_info->data,
526 digest_info->size))
527 goto done;
Tadeusz Struka49de372016-03-03 21:49:26 +0000528
Herbert Xuc0d20d22016-06-29 19:32:23 +0800529 pos += digest_info->size;
Tadeusz Struka49de372016-03-03 21:49:26 +0000530
531 err = 0;
532
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100533 if (req->dst_len < req_ctx->child_req.dst_len - pos)
534 err = -EOVERFLOW;
535 req->dst_len = req_ctx->child_req.dst_len - pos;
536
537 if (!err)
538 sg_copy_from_buffer(req->dst,
539 sg_nents_for_len(req->dst, req->dst_len),
540 req_ctx->out_buf + pos, req->dst_len);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100541done:
542 kzfree(req_ctx->out_buf);
543
544 return err;
545}
546
547static void pkcs1pad_verify_complete_cb(
548 struct crypto_async_request *child_async_req, int err)
549{
550 struct akcipher_request *req = child_async_req->data;
551 struct crypto_async_request async_req;
552
553 if (err == -EINPROGRESS)
554 return;
555
556 async_req.data = req->base.data;
557 async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
558 async_req.flags = child_async_req->flags;
559 req->base.complete(&async_req, pkcs1pad_verify_complete(req, err));
560}
561
562/*
563 * The verify operation is here for completeness similar to the verification
564 * defined in RFC2313 section 10.2 except that block type 0 is not accepted,
565 * as in RFC2437. RFC2437 section 9.2 doesn't define any operation to
566 * retrieve the DigestInfo from a signature, instead the user is expected
567 * to call the sign operation to generate the expected signature and compare
568 * signatures instead of the message-digests.
569 */
570static int pkcs1pad_verify(struct akcipher_request *req)
571{
572 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
573 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
574 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
575 int err;
576
Tadeusz Struka49de372016-03-03 21:49:26 +0000577 if (!ctx->key_size || req->src_len < ctx->key_size)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100578 return -EINVAL;
579
580 if (ctx->key_size > PAGE_SIZE)
581 return -ENOTSUPP;
582
583 /* Reuse input buffer, output to a new buffer */
584 req_ctx->child_req.src = req->src;
585 req_ctx->child_req.src_len = req->src_len;
586 req_ctx->child_req.dst = req_ctx->out_sg;
Tadeusz Struk6f0904a2016-04-06 14:42:32 -0700587 req_ctx->child_req.dst_len = ctx->key_size;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100588
Tadeusz Struk6f0904a2016-04-06 14:42:32 -0700589 req_ctx->out_buf = kmalloc(ctx->key_size,
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100590 (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
591 GFP_KERNEL : GFP_ATOMIC);
592 if (!req_ctx->out_buf)
593 return -ENOMEM;
594
595 pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
Tadeusz Struk6f0904a2016-04-06 14:42:32 -0700596 ctx->key_size, NULL);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100597
598 akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
599 akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
600 pkcs1pad_verify_complete_cb, req);
601
602 err = crypto_akcipher_verify(&req_ctx->child_req);
603 if (err != -EINPROGRESS &&
604 (err != -EBUSY ||
605 !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))
606 return pkcs1pad_verify_complete(req, err);
607
608 return err;
609}
610
611static int pkcs1pad_init_tfm(struct crypto_akcipher *tfm)
612{
613 struct akcipher_instance *inst = akcipher_alg_instance(tfm);
Tadeusz Struka49de372016-03-03 21:49:26 +0000614 struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100615 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
616 struct crypto_akcipher *child_tfm;
617
Herbert Xuc0d20d22016-06-29 19:32:23 +0800618 child_tfm = crypto_spawn_akcipher(&ictx->spawn);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100619 if (IS_ERR(child_tfm))
620 return PTR_ERR(child_tfm);
621
622 ctx->child = child_tfm;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100623 return 0;
624}
625
626static void pkcs1pad_exit_tfm(struct crypto_akcipher *tfm)
627{
628 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
629
630 crypto_free_akcipher(ctx->child);
631}
632
633static void pkcs1pad_free(struct akcipher_instance *inst)
634{
Tadeusz Struka49de372016-03-03 21:49:26 +0000635 struct pkcs1pad_inst_ctx *ctx = akcipher_instance_ctx(inst);
636 struct crypto_akcipher_spawn *spawn = &ctx->spawn;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100637
638 crypto_drop_akcipher(spawn);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100639 kfree(inst);
640}
641
642static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb)
643{
Herbert Xuc0d20d22016-06-29 19:32:23 +0800644 const struct rsa_asn1_template *digest_info;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100645 struct crypto_attr_type *algt;
646 struct akcipher_instance *inst;
Tadeusz Struka49de372016-03-03 21:49:26 +0000647 struct pkcs1pad_inst_ctx *ctx;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100648 struct crypto_akcipher_spawn *spawn;
649 struct akcipher_alg *rsa_alg;
650 const char *rsa_alg_name;
Tadeusz Struka49de372016-03-03 21:49:26 +0000651 const char *hash_name;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100652 int err;
653
654 algt = crypto_get_attr_type(tb);
655 if (IS_ERR(algt))
656 return PTR_ERR(algt);
657
658 if ((algt->type ^ CRYPTO_ALG_TYPE_AKCIPHER) & algt->mask)
659 return -EINVAL;
660
661 rsa_alg_name = crypto_attr_alg_name(tb[1]);
662 if (IS_ERR(rsa_alg_name))
663 return PTR_ERR(rsa_alg_name);
664
Tadeusz Struka49de372016-03-03 21:49:26 +0000665 hash_name = crypto_attr_alg_name(tb[2]);
666 if (IS_ERR(hash_name))
Herbert Xuc0d20d22016-06-29 19:32:23 +0800667 return PTR_ERR(hash_name);
668
669 digest_info = rsa_lookup_asn1(hash_name);
670 if (!digest_info)
671 return -EINVAL;
Tadeusz Struka49de372016-03-03 21:49:26 +0000672
673 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100674 if (!inst)
675 return -ENOMEM;
676
Tadeusz Struka49de372016-03-03 21:49:26 +0000677 ctx = akcipher_instance_ctx(inst);
678 spawn = &ctx->spawn;
Herbert Xuc0d20d22016-06-29 19:32:23 +0800679 ctx->digest_info = digest_info;
Tadeusz Struka49de372016-03-03 21:49:26 +0000680
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100681 crypto_set_spawn(&spawn->base, akcipher_crypto_instance(inst));
682 err = crypto_grab_akcipher(spawn, rsa_alg_name, 0,
683 crypto_requires_sync(algt->type, algt->mask));
684 if (err)
685 goto out_free_inst;
686
687 rsa_alg = crypto_spawn_akcipher_alg(spawn);
688
689 err = -ENAMETOOLONG;
Tadeusz Struka49de372016-03-03 21:49:26 +0000690
Herbert Xuc0d20d22016-06-29 19:32:23 +0800691 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
692 "pkcs1pad(%s,%s)", rsa_alg->base.cra_name, hash_name) >=
693 CRYPTO_MAX_ALG_NAME ||
694 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
695 "pkcs1pad(%s,%s)",
696 rsa_alg->base.cra_driver_name, hash_name) >=
697 CRYPTO_MAX_ALG_NAME)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100698 goto out_drop_alg;
699
700 inst->alg.base.cra_flags = rsa_alg->base.cra_flags & CRYPTO_ALG_ASYNC;
701 inst->alg.base.cra_priority = rsa_alg->base.cra_priority;
702 inst->alg.base.cra_ctxsize = sizeof(struct pkcs1pad_ctx);
703
704 inst->alg.init = pkcs1pad_init_tfm;
705 inst->alg.exit = pkcs1pad_exit_tfm;
706
707 inst->alg.encrypt = pkcs1pad_encrypt;
708 inst->alg.decrypt = pkcs1pad_decrypt;
709 inst->alg.sign = pkcs1pad_sign;
710 inst->alg.verify = pkcs1pad_verify;
711 inst->alg.set_pub_key = pkcs1pad_set_pub_key;
712 inst->alg.set_priv_key = pkcs1pad_set_priv_key;
713 inst->alg.max_size = pkcs1pad_get_max_size;
714 inst->alg.reqsize = sizeof(struct pkcs1pad_request) + rsa_alg->reqsize;
715
716 inst->free = pkcs1pad_free;
717
718 err = akcipher_register_instance(tmpl, inst);
719 if (err)
Herbert Xuc0d20d22016-06-29 19:32:23 +0800720 goto out_drop_alg;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100721
722 return 0;
723
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100724out_drop_alg:
725 crypto_drop_akcipher(spawn);
726out_free_inst:
727 kfree(inst);
728 return err;
729}
730
731struct crypto_template rsa_pkcs1pad_tmpl = {
732 .name = "pkcs1pad",
733 .create = pkcs1pad_create,
734 .module = THIS_MODULE,
735};