blob: ebd851474a9db451e52eddb3a59283ebae6f7cab [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
Herbert Xu0f2c8312016-06-29 19:32:24 +0800106 struct scatterlist in_sg[2], out_sg[1];
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100107 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);
Herbert Xu73f79182016-06-29 19:32:27 +0800114 int err;
115
116 ctx->key_size = 0;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100117
118 err = crypto_akcipher_set_pub_key(ctx->child, key, keylen);
Herbert Xu73f79182016-06-29 19:32:27 +0800119 if (err)
120 return err;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100121
Herbert Xu73f79182016-06-29 19:32:27 +0800122 /* Find out new modulus size from rsa implementation */
123 err = crypto_akcipher_maxsize(ctx->child);
124 if (err < 0)
125 return err;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100126
Herbert Xu73f79182016-06-29 19:32:27 +0800127 if (err > PAGE_SIZE)
128 return -ENOTSUPP;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100129
Herbert Xu73f79182016-06-29 19:32:27 +0800130 ctx->key_size = err;
131 return 0;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100132}
133
134static int pkcs1pad_set_priv_key(struct crypto_akcipher *tfm, const void *key,
135 unsigned int keylen)
136{
137 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
Herbert Xu73f79182016-06-29 19:32:27 +0800138 int err;
139
140 ctx->key_size = 0;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100141
142 err = crypto_akcipher_set_priv_key(ctx->child, key, keylen);
Herbert Xu73f79182016-06-29 19:32:27 +0800143 if (err)
144 return err;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100145
Herbert Xu73f79182016-06-29 19:32:27 +0800146 /* Find out new modulus size from rsa implementation */
147 err = crypto_akcipher_maxsize(ctx->child);
148 if (err < 0)
149 return err;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100150
Herbert Xu73f79182016-06-29 19:32:27 +0800151 if (err > PAGE_SIZE)
152 return -ENOTSUPP;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100153
Herbert Xu73f79182016-06-29 19:32:27 +0800154 ctx->key_size = err;
155 return 0;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100156}
157
158static int pkcs1pad_get_max_size(struct crypto_akcipher *tfm)
159{
160 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
161
162 /*
163 * The maximum destination buffer size for the encrypt/sign operations
164 * will be the same as for RSA, even though it's smaller for
165 * decrypt/verify.
166 */
167
168 return ctx->key_size ?: -EINVAL;
169}
170
171static void pkcs1pad_sg_set_buf(struct scatterlist *sg, void *buf, size_t len,
172 struct scatterlist *next)
173{
Herbert Xu0f2c8312016-06-29 19:32:24 +0800174 int nsegs = next ? 2 : 1;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100175
Herbert Xu0f2c8312016-06-29 19:32:24 +0800176 sg_init_table(sg, nsegs);
177 sg_set_buf(sg, buf, len);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100178
179 if (next)
180 sg_chain(sg, nsegs, next);
181}
182
183static int pkcs1pad_encrypt_sign_complete(struct akcipher_request *req, int err)
184{
185 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
186 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
187 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
Andrzej Zaborowski53192162015-12-12 00:03:51 -0500188 size_t pad_len = ctx->key_size - req_ctx->child_req.dst_len;
189 size_t chunk_len, pad_left;
190 struct sg_mapping_iter miter;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100191
192 if (!err) {
Andrzej Zaborowski53192162015-12-12 00:03:51 -0500193 if (pad_len) {
194 sg_miter_start(&miter, req->dst,
195 sg_nents_for_len(req->dst, pad_len),
196 SG_MITER_ATOMIC | SG_MITER_TO_SG);
197
198 pad_left = pad_len;
199 while (pad_left) {
200 sg_miter_next(&miter);
201
202 chunk_len = min(miter.length, pad_left);
203 memset(miter.addr, 0, chunk_len);
204 pad_left -= chunk_len;
205 }
206
207 sg_miter_stop(&miter);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100208 }
209
210 sg_pcopy_from_buffer(req->dst,
211 sg_nents_for_len(req->dst, ctx->key_size),
212 req_ctx->out_buf, req_ctx->child_req.dst_len,
Andrzej Zaborowski53192162015-12-12 00:03:51 -0500213 pad_len);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100214 }
215 req->dst_len = ctx->key_size;
216
217 kfree(req_ctx->in_buf);
218 kzfree(req_ctx->out_buf);
219
220 return err;
221}
222
223static void pkcs1pad_encrypt_sign_complete_cb(
224 struct crypto_async_request *child_async_req, int err)
225{
226 struct akcipher_request *req = child_async_req->data;
227 struct crypto_async_request async_req;
228
229 if (err == -EINPROGRESS)
230 return;
231
232 async_req.data = req->base.data;
233 async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
234 async_req.flags = child_async_req->flags;
235 req->base.complete(&async_req,
236 pkcs1pad_encrypt_sign_complete(req, err));
237}
238
239static int pkcs1pad_encrypt(struct akcipher_request *req)
240{
241 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
242 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
243 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
244 int err;
245 unsigned int i, ps_end;
246
247 if (!ctx->key_size)
248 return -EINVAL;
249
250 if (req->src_len > ctx->key_size - 11)
251 return -EOVERFLOW;
252
253 if (req->dst_len < ctx->key_size) {
254 req->dst_len = ctx->key_size;
255 return -EOVERFLOW;
256 }
257
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100258 /*
259 * Replace both input and output to add the padding in the input and
260 * the potential missing leading zeros in the output.
261 */
262 req_ctx->child_req.src = req_ctx->in_sg;
263 req_ctx->child_req.src_len = ctx->key_size - 1;
264 req_ctx->child_req.dst = req_ctx->out_sg;
265 req_ctx->child_req.dst_len = ctx->key_size;
266
267 req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len,
Herbert Xu3a32ce52016-06-29 19:32:26 +0800268 GFP_KERNEL);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100269 if (!req_ctx->in_buf)
270 return -ENOMEM;
271
272 ps_end = ctx->key_size - req->src_len - 2;
273 req_ctx->in_buf[0] = 0x02;
274 for (i = 1; i < ps_end; i++)
275 req_ctx->in_buf[i] = 1 + prandom_u32_max(255);
276 req_ctx->in_buf[ps_end] = 0x00;
277
278 pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
279 ctx->key_size - 1 - req->src_len, req->src);
280
Herbert Xu3a32ce52016-06-29 19:32:26 +0800281 req_ctx->out_buf = kmalloc(ctx->key_size, GFP_KERNEL);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100282 if (!req_ctx->out_buf) {
283 kfree(req_ctx->in_buf);
284 return -ENOMEM;
285 }
286
287 pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
288 ctx->key_size, NULL);
289
290 akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
291 akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
292 pkcs1pad_encrypt_sign_complete_cb, req);
293
294 err = crypto_akcipher_encrypt(&req_ctx->child_req);
295 if (err != -EINPROGRESS &&
296 (err != -EBUSY ||
297 !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))
298 return pkcs1pad_encrypt_sign_complete(req, err);
299
300 return err;
301}
302
303static int pkcs1pad_decrypt_complete(struct akcipher_request *req, int err)
304{
305 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
306 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
307 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
308 unsigned int pos;
309
310 if (err == -EOVERFLOW)
311 /* Decrypted value had no leading 0 byte */
312 err = -EINVAL;
313
314 if (err)
315 goto done;
316
317 if (req_ctx->child_req.dst_len != ctx->key_size - 1) {
318 err = -EINVAL;
319 goto done;
320 }
321
322 if (req_ctx->out_buf[0] != 0x02) {
323 err = -EINVAL;
324 goto done;
325 }
326 for (pos = 1; pos < req_ctx->child_req.dst_len; pos++)
327 if (req_ctx->out_buf[pos] == 0x00)
328 break;
329 if (pos < 9 || pos == req_ctx->child_req.dst_len) {
330 err = -EINVAL;
331 goto done;
332 }
333 pos++;
334
335 if (req->dst_len < req_ctx->child_req.dst_len - pos)
336 err = -EOVERFLOW;
337 req->dst_len = req_ctx->child_req.dst_len - pos;
338
339 if (!err)
340 sg_copy_from_buffer(req->dst,
341 sg_nents_for_len(req->dst, req->dst_len),
342 req_ctx->out_buf + pos, req->dst_len);
343
344done:
345 kzfree(req_ctx->out_buf);
346
347 return err;
348}
349
350static void pkcs1pad_decrypt_complete_cb(
351 struct crypto_async_request *child_async_req, int err)
352{
353 struct akcipher_request *req = child_async_req->data;
354 struct crypto_async_request async_req;
355
356 if (err == -EINPROGRESS)
357 return;
358
359 async_req.data = req->base.data;
360 async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
361 async_req.flags = child_async_req->flags;
362 req->base.complete(&async_req, pkcs1pad_decrypt_complete(req, err));
363}
364
365static int pkcs1pad_decrypt(struct akcipher_request *req)
366{
367 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
368 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
369 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
370 int err;
371
372 if (!ctx->key_size || req->src_len != ctx->key_size)
373 return -EINVAL;
374
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100375 /* Reuse input buffer, output to a new buffer */
376 req_ctx->child_req.src = req->src;
377 req_ctx->child_req.src_len = req->src_len;
378 req_ctx->child_req.dst = req_ctx->out_sg;
Tadeusz Struk6f0904a2016-04-06 14:42:32 -0700379 req_ctx->child_req.dst_len = ctx->key_size ;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100380
Herbert Xu3a32ce52016-06-29 19:32:26 +0800381 req_ctx->out_buf = kmalloc(ctx->key_size, GFP_KERNEL);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100382 if (!req_ctx->out_buf)
383 return -ENOMEM;
384
385 pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
Tadeusz Struk6f0904a2016-04-06 14:42:32 -0700386 ctx->key_size, NULL);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100387
388 akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
389 akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
390 pkcs1pad_decrypt_complete_cb, req);
391
392 err = crypto_akcipher_decrypt(&req_ctx->child_req);
393 if (err != -EINPROGRESS &&
394 (err != -EBUSY ||
395 !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))
396 return pkcs1pad_decrypt_complete(req, err);
397
398 return err;
399}
400
401static int pkcs1pad_sign(struct akcipher_request *req)
402{
403 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
404 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
405 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
Herbert Xuc0d20d22016-06-29 19:32:23 +0800406 struct akcipher_instance *inst = akcipher_alg_instance(tfm);
407 struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
408 const struct rsa_asn1_template *digest_info = ictx->digest_info;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100409 int err;
Tadeusz Struka49de372016-03-03 21:49:26 +0000410 unsigned int ps_end, digest_size = 0;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100411
412 if (!ctx->key_size)
413 return -EINVAL;
414
Herbert Xuc0d20d22016-06-29 19:32:23 +0800415 digest_size = digest_info->size;
Tadeusz Struka49de372016-03-03 21:49:26 +0000416
417 if (req->src_len + digest_size > ctx->key_size - 11)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100418 return -EOVERFLOW;
419
420 if (req->dst_len < ctx->key_size) {
421 req->dst_len = ctx->key_size;
422 return -EOVERFLOW;
423 }
424
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100425 /*
426 * Replace both input and output to add the padding in the input and
427 * the potential missing leading zeros in the output.
428 */
429 req_ctx->child_req.src = req_ctx->in_sg;
430 req_ctx->child_req.src_len = ctx->key_size - 1;
431 req_ctx->child_req.dst = req_ctx->out_sg;
432 req_ctx->child_req.dst_len = ctx->key_size;
433
434 req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len,
Herbert Xu3a32ce52016-06-29 19:32:26 +0800435 GFP_KERNEL);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100436 if (!req_ctx->in_buf)
437 return -ENOMEM;
438
Tadeusz Struka49de372016-03-03 21:49:26 +0000439 ps_end = ctx->key_size - digest_size - req->src_len - 2;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100440 req_ctx->in_buf[0] = 0x01;
441 memset(req_ctx->in_buf + 1, 0xff, ps_end - 1);
442 req_ctx->in_buf[ps_end] = 0x00;
443
Herbert Xuc0d20d22016-06-29 19:32:23 +0800444 memcpy(req_ctx->in_buf + ps_end + 1, digest_info->data,
445 digest_info->size);
Tadeusz Struka49de372016-03-03 21:49:26 +0000446
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100447 pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
448 ctx->key_size - 1 - req->src_len, req->src);
449
Herbert Xu3a32ce52016-06-29 19:32:26 +0800450 req_ctx->out_buf = kmalloc(ctx->key_size, GFP_KERNEL);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100451 if (!req_ctx->out_buf) {
452 kfree(req_ctx->in_buf);
453 return -ENOMEM;
454 }
455
456 pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
457 ctx->key_size, NULL);
458
459 akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
460 akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
461 pkcs1pad_encrypt_sign_complete_cb, req);
462
463 err = crypto_akcipher_sign(&req_ctx->child_req);
464 if (err != -EINPROGRESS &&
465 (err != -EBUSY ||
466 !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))
467 return pkcs1pad_encrypt_sign_complete(req, err);
468
469 return err;
470}
471
472static int pkcs1pad_verify_complete(struct akcipher_request *req, int err)
473{
474 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
475 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
476 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
Herbert Xuc0d20d22016-06-29 19:32:23 +0800477 struct akcipher_instance *inst = akcipher_alg_instance(tfm);
478 struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
479 const struct rsa_asn1_template *digest_info = ictx->digest_info;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100480 unsigned int pos;
481
482 if (err == -EOVERFLOW)
483 /* Decrypted value had no leading 0 byte */
484 err = -EINVAL;
485
486 if (err)
487 goto done;
488
489 if (req_ctx->child_req.dst_len != ctx->key_size - 1) {
490 err = -EINVAL;
491 goto done;
492 }
493
Tadeusz Struka49de372016-03-03 21:49:26 +0000494 err = -EBADMSG;
495 if (req_ctx->out_buf[0] != 0x01)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100496 goto done;
Tadeusz Struka49de372016-03-03 21:49:26 +0000497
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100498 for (pos = 1; pos < req_ctx->child_req.dst_len; pos++)
499 if (req_ctx->out_buf[pos] != 0xff)
500 break;
Tadeusz Struka49de372016-03-03 21:49:26 +0000501
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100502 if (pos < 9 || pos == req_ctx->child_req.dst_len ||
Tadeusz Struka49de372016-03-03 21:49:26 +0000503 req_ctx->out_buf[pos] != 0x00)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100504 goto done;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100505 pos++;
506
Herbert Xuc0d20d22016-06-29 19:32:23 +0800507 if (memcmp(req_ctx->out_buf + pos, digest_info->data,
508 digest_info->size))
509 goto done;
Tadeusz Struka49de372016-03-03 21:49:26 +0000510
Herbert Xuc0d20d22016-06-29 19:32:23 +0800511 pos += digest_info->size;
Tadeusz Struka49de372016-03-03 21:49:26 +0000512
513 err = 0;
514
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100515 if (req->dst_len < req_ctx->child_req.dst_len - pos)
516 err = -EOVERFLOW;
517 req->dst_len = req_ctx->child_req.dst_len - pos;
518
519 if (!err)
520 sg_copy_from_buffer(req->dst,
521 sg_nents_for_len(req->dst, req->dst_len),
522 req_ctx->out_buf + pos, req->dst_len);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100523done:
524 kzfree(req_ctx->out_buf);
525
526 return err;
527}
528
529static void pkcs1pad_verify_complete_cb(
530 struct crypto_async_request *child_async_req, int err)
531{
532 struct akcipher_request *req = child_async_req->data;
533 struct crypto_async_request async_req;
534
535 if (err == -EINPROGRESS)
536 return;
537
538 async_req.data = req->base.data;
539 async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
540 async_req.flags = child_async_req->flags;
541 req->base.complete(&async_req, pkcs1pad_verify_complete(req, err));
542}
543
544/*
545 * The verify operation is here for completeness similar to the verification
546 * defined in RFC2313 section 10.2 except that block type 0 is not accepted,
547 * as in RFC2437. RFC2437 section 9.2 doesn't define any operation to
548 * retrieve the DigestInfo from a signature, instead the user is expected
549 * to call the sign operation to generate the expected signature and compare
550 * signatures instead of the message-digests.
551 */
552static int pkcs1pad_verify(struct akcipher_request *req)
553{
554 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
555 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
556 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
557 int err;
558
Tadeusz Struka49de372016-03-03 21:49:26 +0000559 if (!ctx->key_size || req->src_len < ctx->key_size)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100560 return -EINVAL;
561
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100562 /* Reuse input buffer, output to a new buffer */
563 req_ctx->child_req.src = req->src;
564 req_ctx->child_req.src_len = req->src_len;
565 req_ctx->child_req.dst = req_ctx->out_sg;
Tadeusz Struk6f0904a2016-04-06 14:42:32 -0700566 req_ctx->child_req.dst_len = ctx->key_size;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100567
Herbert Xu3a32ce52016-06-29 19:32:26 +0800568 req_ctx->out_buf = kmalloc(ctx->key_size, GFP_KERNEL);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100569 if (!req_ctx->out_buf)
570 return -ENOMEM;
571
572 pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
Tadeusz Struk6f0904a2016-04-06 14:42:32 -0700573 ctx->key_size, NULL);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100574
575 akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
576 akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
577 pkcs1pad_verify_complete_cb, req);
578
579 err = crypto_akcipher_verify(&req_ctx->child_req);
580 if (err != -EINPROGRESS &&
581 (err != -EBUSY ||
582 !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))
583 return pkcs1pad_verify_complete(req, err);
584
585 return err;
586}
587
588static int pkcs1pad_init_tfm(struct crypto_akcipher *tfm)
589{
590 struct akcipher_instance *inst = akcipher_alg_instance(tfm);
Tadeusz Struka49de372016-03-03 21:49:26 +0000591 struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100592 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
593 struct crypto_akcipher *child_tfm;
594
Herbert Xuc0d20d22016-06-29 19:32:23 +0800595 child_tfm = crypto_spawn_akcipher(&ictx->spawn);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100596 if (IS_ERR(child_tfm))
597 return PTR_ERR(child_tfm);
598
599 ctx->child = child_tfm;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100600 return 0;
601}
602
603static void pkcs1pad_exit_tfm(struct crypto_akcipher *tfm)
604{
605 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
606
607 crypto_free_akcipher(ctx->child);
608}
609
610static void pkcs1pad_free(struct akcipher_instance *inst)
611{
Tadeusz Struka49de372016-03-03 21:49:26 +0000612 struct pkcs1pad_inst_ctx *ctx = akcipher_instance_ctx(inst);
613 struct crypto_akcipher_spawn *spawn = &ctx->spawn;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100614
615 crypto_drop_akcipher(spawn);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100616 kfree(inst);
617}
618
619static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb)
620{
Herbert Xuc0d20d22016-06-29 19:32:23 +0800621 const struct rsa_asn1_template *digest_info;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100622 struct crypto_attr_type *algt;
623 struct akcipher_instance *inst;
Tadeusz Struka49de372016-03-03 21:49:26 +0000624 struct pkcs1pad_inst_ctx *ctx;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100625 struct crypto_akcipher_spawn *spawn;
626 struct akcipher_alg *rsa_alg;
627 const char *rsa_alg_name;
Tadeusz Struka49de372016-03-03 21:49:26 +0000628 const char *hash_name;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100629 int err;
630
631 algt = crypto_get_attr_type(tb);
632 if (IS_ERR(algt))
633 return PTR_ERR(algt);
634
635 if ((algt->type ^ CRYPTO_ALG_TYPE_AKCIPHER) & algt->mask)
636 return -EINVAL;
637
638 rsa_alg_name = crypto_attr_alg_name(tb[1]);
639 if (IS_ERR(rsa_alg_name))
640 return PTR_ERR(rsa_alg_name);
641
Tadeusz Struka49de372016-03-03 21:49:26 +0000642 hash_name = crypto_attr_alg_name(tb[2]);
643 if (IS_ERR(hash_name))
Herbert Xuc0d20d22016-06-29 19:32:23 +0800644 return PTR_ERR(hash_name);
645
646 digest_info = rsa_lookup_asn1(hash_name);
647 if (!digest_info)
648 return -EINVAL;
Tadeusz Struka49de372016-03-03 21:49:26 +0000649
650 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100651 if (!inst)
652 return -ENOMEM;
653
Tadeusz Struka49de372016-03-03 21:49:26 +0000654 ctx = akcipher_instance_ctx(inst);
655 spawn = &ctx->spawn;
Herbert Xuc0d20d22016-06-29 19:32:23 +0800656 ctx->digest_info = digest_info;
Tadeusz Struka49de372016-03-03 21:49:26 +0000657
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100658 crypto_set_spawn(&spawn->base, akcipher_crypto_instance(inst));
659 err = crypto_grab_akcipher(spawn, rsa_alg_name, 0,
660 crypto_requires_sync(algt->type, algt->mask));
661 if (err)
662 goto out_free_inst;
663
664 rsa_alg = crypto_spawn_akcipher_alg(spawn);
665
666 err = -ENAMETOOLONG;
Tadeusz Struka49de372016-03-03 21:49:26 +0000667
Herbert Xuc0d20d22016-06-29 19:32:23 +0800668 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
669 "pkcs1pad(%s,%s)", rsa_alg->base.cra_name, hash_name) >=
670 CRYPTO_MAX_ALG_NAME ||
671 snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
672 "pkcs1pad(%s,%s)",
673 rsa_alg->base.cra_driver_name, hash_name) >=
674 CRYPTO_MAX_ALG_NAME)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100675 goto out_drop_alg;
676
677 inst->alg.base.cra_flags = rsa_alg->base.cra_flags & CRYPTO_ALG_ASYNC;
678 inst->alg.base.cra_priority = rsa_alg->base.cra_priority;
679 inst->alg.base.cra_ctxsize = sizeof(struct pkcs1pad_ctx);
680
681 inst->alg.init = pkcs1pad_init_tfm;
682 inst->alg.exit = pkcs1pad_exit_tfm;
683
684 inst->alg.encrypt = pkcs1pad_encrypt;
685 inst->alg.decrypt = pkcs1pad_decrypt;
686 inst->alg.sign = pkcs1pad_sign;
687 inst->alg.verify = pkcs1pad_verify;
688 inst->alg.set_pub_key = pkcs1pad_set_pub_key;
689 inst->alg.set_priv_key = pkcs1pad_set_priv_key;
690 inst->alg.max_size = pkcs1pad_get_max_size;
691 inst->alg.reqsize = sizeof(struct pkcs1pad_request) + rsa_alg->reqsize;
692
693 inst->free = pkcs1pad_free;
694
695 err = akcipher_register_instance(tmpl, inst);
696 if (err)
Herbert Xuc0d20d22016-06-29 19:32:23 +0800697 goto out_drop_alg;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100698
699 return 0;
700
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100701out_drop_alg:
702 crypto_drop_akcipher(spawn);
703out_free_inst:
704 kfree(inst);
705 return err;
706}
707
708struct crypto_template rsa_pkcs1pad_tmpl = {
709 .name = "pkcs1pad",
710 .create = pkcs1pad_create,
711 .module = THIS_MODULE,
712};