blob: 0a6680ca8cb6fc09b003806d0b40c5561c8526dc [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>
Eric Biggersa1180cf2019-01-10 12:17:53 -080015#include <crypto/internal/rsa.h>
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +010016#include <linux/err.h>
17#include <linux/init.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/random.h>
21
Tadeusz Struka49de372016-03-03 21:49:26 +000022/*
23 * Hash algorithm OIDs plus ASN.1 DER wrappings [RFC4880 sec 5.2.2].
24 */
25static const u8 rsa_digest_info_md5[] = {
26 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08,
27 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, /* OID */
28 0x05, 0x00, 0x04, 0x10
29};
30
31static const u8 rsa_digest_info_sha1[] = {
32 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
33 0x2b, 0x0e, 0x03, 0x02, 0x1a,
34 0x05, 0x00, 0x04, 0x14
35};
36
37static const u8 rsa_digest_info_rmd160[] = {
38 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
39 0x2b, 0x24, 0x03, 0x02, 0x01,
40 0x05, 0x00, 0x04, 0x14
41};
42
43static const u8 rsa_digest_info_sha224[] = {
44 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09,
45 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04,
46 0x05, 0x00, 0x04, 0x1c
47};
48
49static const u8 rsa_digest_info_sha256[] = {
50 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09,
51 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
52 0x05, 0x00, 0x04, 0x20
53};
54
55static const u8 rsa_digest_info_sha384[] = {
56 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09,
57 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
58 0x05, 0x00, 0x04, 0x30
59};
60
61static const u8 rsa_digest_info_sha512[] = {
62 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09,
63 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
64 0x05, 0x00, 0x04, 0x40
65};
66
67static const struct rsa_asn1_template {
68 const char *name;
69 const u8 *data;
70 size_t size;
71} rsa_asn1_templates[] = {
72#define _(X) { #X, rsa_digest_info_##X, sizeof(rsa_digest_info_##X) }
73 _(md5),
74 _(sha1),
75 _(rmd160),
76 _(sha256),
77 _(sha384),
78 _(sha512),
79 _(sha224),
80 { NULL }
81#undef _
82};
83
84static const struct rsa_asn1_template *rsa_lookup_asn1(const char *name)
85{
86 const struct rsa_asn1_template *p;
87
88 for (p = rsa_asn1_templates; p->name; p++)
89 if (strcmp(name, p->name) == 0)
90 return p;
91 return NULL;
92}
93
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +010094struct pkcs1pad_ctx {
95 struct crypto_akcipher *child;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +010096 unsigned int key_size;
97};
98
Tadeusz Struka49de372016-03-03 21:49:26 +000099struct pkcs1pad_inst_ctx {
100 struct crypto_akcipher_spawn spawn;
Herbert Xuc0d20d22016-06-29 19:32:23 +0800101 const struct rsa_asn1_template *digest_info;
Tadeusz Struka49de372016-03-03 21:49:26 +0000102};
103
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100104struct pkcs1pad_request {
Herbert Xu0f2c8312016-06-29 19:32:24 +0800105 struct scatterlist in_sg[2], out_sg[1];
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100106 uint8_t *in_buf, *out_buf;
Tadeusz Struka6d7bfd2016-07-14 20:39:18 -0700107 struct akcipher_request child_req;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100108};
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 Xu73f791892016-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 Xu73f791892016-06-29 19:32:27 +0800119 if (err)
120 return err;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100121
Herbert Xu73f791892016-06-29 19:32:27 +0800122 /* Find out new modulus size from rsa implementation */
123 err = crypto_akcipher_maxsize(ctx->child);
Herbert Xu73f791892016-06-29 19:32:27 +0800124 if (err > PAGE_SIZE)
125 return -ENOTSUPP;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100126
Herbert Xu73f791892016-06-29 19:32:27 +0800127 ctx->key_size = err;
128 return 0;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100129}
130
131static int pkcs1pad_set_priv_key(struct crypto_akcipher *tfm, const void *key,
132 unsigned int keylen)
133{
134 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
Herbert Xu73f791892016-06-29 19:32:27 +0800135 int err;
136
137 ctx->key_size = 0;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100138
139 err = crypto_akcipher_set_priv_key(ctx->child, key, keylen);
Herbert Xu73f791892016-06-29 19:32:27 +0800140 if (err)
141 return err;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100142
Herbert Xu73f791892016-06-29 19:32:27 +0800143 /* Find out new modulus size from rsa implementation */
144 err = crypto_akcipher_maxsize(ctx->child);
Herbert Xu73f791892016-06-29 19:32:27 +0800145 if (err > PAGE_SIZE)
146 return -ENOTSUPP;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100147
Herbert Xu73f791892016-06-29 19:32:27 +0800148 ctx->key_size = err;
149 return 0;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100150}
151
Tudor-Dan Ambarus543de102017-05-25 10:18:16 +0300152static unsigned int pkcs1pad_get_max_size(struct crypto_akcipher *tfm)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100153{
154 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
155
156 /*
157 * The maximum destination buffer size for the encrypt/sign operations
158 * will be the same as for RSA, even though it's smaller for
159 * decrypt/verify.
160 */
161
Tudor-Dan Ambarus543de102017-05-25 10:18:16 +0300162 return ctx->key_size;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100163}
164
165static void pkcs1pad_sg_set_buf(struct scatterlist *sg, void *buf, size_t len,
166 struct scatterlist *next)
167{
Herbert Xu0f2c8312016-06-29 19:32:24 +0800168 int nsegs = next ? 2 : 1;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100169
Herbert Xu0f2c8312016-06-29 19:32:24 +0800170 sg_init_table(sg, nsegs);
171 sg_set_buf(sg, buf, len);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100172
173 if (next)
174 sg_chain(sg, nsegs, next);
175}
176
177static int pkcs1pad_encrypt_sign_complete(struct akcipher_request *req, int err)
178{
179 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
180 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
181 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
Herbert Xud858b072016-06-29 19:32:28 +0800182 unsigned int pad_len;
183 unsigned int len;
184 u8 *out_buf;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100185
Herbert Xud858b072016-06-29 19:32:28 +0800186 if (err)
187 goto out;
Andrzej Zaborowski53192162015-12-12 00:03:51 -0500188
Herbert Xud858b072016-06-29 19:32:28 +0800189 len = req_ctx->child_req.dst_len;
190 pad_len = ctx->key_size - len;
Andrzej Zaborowski53192162015-12-12 00:03:51 -0500191
Herbert Xud858b072016-06-29 19:32:28 +0800192 /* Four billion to one */
193 if (likely(!pad_len))
194 goto out;
Andrzej Zaborowski53192162015-12-12 00:03:51 -0500195
Jia-Ju Bai1ca28092018-01-25 17:57:54 +0800196 out_buf = kzalloc(ctx->key_size, GFP_KERNEL);
Herbert Xud858b072016-06-29 19:32:28 +0800197 err = -ENOMEM;
198 if (!out_buf)
199 goto out;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100200
Herbert Xud858b072016-06-29 19:32:28 +0800201 sg_copy_to_buffer(req->dst, sg_nents_for_len(req->dst, len),
202 out_buf + pad_len, len);
203 sg_copy_from_buffer(req->dst,
204 sg_nents_for_len(req->dst, ctx->key_size),
205 out_buf, ctx->key_size);
206 kzfree(out_buf);
207
208out:
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100209 req->dst_len = ctx->key_size;
210
211 kfree(req_ctx->in_buf);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100212
213 return err;
214}
215
216static void pkcs1pad_encrypt_sign_complete_cb(
217 struct crypto_async_request *child_async_req, int err)
218{
219 struct akcipher_request *req = child_async_req->data;
220 struct crypto_async_request async_req;
221
222 if (err == -EINPROGRESS)
223 return;
224
225 async_req.data = req->base.data;
226 async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
227 async_req.flags = child_async_req->flags;
228 req->base.complete(&async_req,
229 pkcs1pad_encrypt_sign_complete(req, err));
230}
231
232static int pkcs1pad_encrypt(struct akcipher_request *req)
233{
234 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
235 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
236 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
237 int err;
238 unsigned int i, ps_end;
239
240 if (!ctx->key_size)
241 return -EINVAL;
242
243 if (req->src_len > ctx->key_size - 11)
244 return -EOVERFLOW;
245
246 if (req->dst_len < ctx->key_size) {
247 req->dst_len = ctx->key_size;
248 return -EOVERFLOW;
249 }
250
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100251 req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len,
Herbert Xu3a32ce52016-06-29 19:32:26 +0800252 GFP_KERNEL);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100253 if (!req_ctx->in_buf)
254 return -ENOMEM;
255
256 ps_end = ctx->key_size - req->src_len - 2;
257 req_ctx->in_buf[0] = 0x02;
258 for (i = 1; i < ps_end; i++)
259 req_ctx->in_buf[i] = 1 + prandom_u32_max(255);
260 req_ctx->in_buf[ps_end] = 0x00;
261
262 pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
263 ctx->key_size - 1 - req->src_len, req->src);
264
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100265 akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
266 akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
267 pkcs1pad_encrypt_sign_complete_cb, req);
268
Herbert Xud858b072016-06-29 19:32:28 +0800269 /* Reuse output buffer */
270 akcipher_request_set_crypt(&req_ctx->child_req, req_ctx->in_sg,
271 req->dst, ctx->key_size - 1, req->dst_len);
272
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100273 err = crypto_akcipher_encrypt(&req_ctx->child_req);
Gilad Ben-Yossef4e5b0ad2017-10-18 08:00:36 +0100274 if (err != -EINPROGRESS && err != -EBUSY)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100275 return pkcs1pad_encrypt_sign_complete(req, err);
276
277 return err;
278}
279
280static int pkcs1pad_decrypt_complete(struct akcipher_request *req, int err)
281{
282 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
283 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
284 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
Herbert Xu0cf43f52016-09-22 17:04:57 +0800285 unsigned int dst_len;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100286 unsigned int pos;
Herbert Xu0cf43f52016-09-22 17:04:57 +0800287 u8 *out_buf;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100288
289 if (err)
290 goto done;
291
Herbert Xu0cf43f52016-09-22 17:04:57 +0800292 err = -EINVAL;
293 dst_len = req_ctx->child_req.dst_len;
294 if (dst_len < ctx->key_size - 1)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100295 goto done;
Herbert Xu0cf43f52016-09-22 17:04:57 +0800296
297 out_buf = req_ctx->out_buf;
298 if (dst_len == ctx->key_size) {
299 if (out_buf[0] != 0x00)
300 /* Decrypted value had no leading 0 byte */
301 goto done;
302
303 dst_len--;
304 out_buf++;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100305 }
306
Herbert Xu0cf43f52016-09-22 17:04:57 +0800307 if (out_buf[0] != 0x02)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100308 goto done;
Herbert Xu0cf43f52016-09-22 17:04:57 +0800309
310 for (pos = 1; pos < dst_len; pos++)
311 if (out_buf[pos] == 0x00)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100312 break;
Herbert Xu0cf43f52016-09-22 17:04:57 +0800313 if (pos < 9 || pos == dst_len)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100314 goto done;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100315 pos++;
316
Herbert Xu0cf43f52016-09-22 17:04:57 +0800317 err = 0;
318
319 if (req->dst_len < dst_len - pos)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100320 err = -EOVERFLOW;
Herbert Xu0cf43f52016-09-22 17:04:57 +0800321 req->dst_len = dst_len - pos;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100322
323 if (!err)
324 sg_copy_from_buffer(req->dst,
325 sg_nents_for_len(req->dst, req->dst_len),
Herbert Xu0cf43f52016-09-22 17:04:57 +0800326 out_buf + pos, req->dst_len);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100327
328done:
329 kzfree(req_ctx->out_buf);
330
331 return err;
332}
333
334static void pkcs1pad_decrypt_complete_cb(
335 struct crypto_async_request *child_async_req, int err)
336{
337 struct akcipher_request *req = child_async_req->data;
338 struct crypto_async_request async_req;
339
340 if (err == -EINPROGRESS)
341 return;
342
343 async_req.data = req->base.data;
344 async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
345 async_req.flags = child_async_req->flags;
346 req->base.complete(&async_req, pkcs1pad_decrypt_complete(req, err));
347}
348
349static int pkcs1pad_decrypt(struct akcipher_request *req)
350{
351 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
352 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
353 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
354 int err;
355
356 if (!ctx->key_size || req->src_len != ctx->key_size)
357 return -EINVAL;
358
Herbert Xu3a32ce52016-06-29 19:32:26 +0800359 req_ctx->out_buf = kmalloc(ctx->key_size, GFP_KERNEL);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100360 if (!req_ctx->out_buf)
361 return -ENOMEM;
362
363 pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
Tadeusz Struk6f0904a2016-04-06 14:42:32 -0700364 ctx->key_size, NULL);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100365
366 akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
367 akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
368 pkcs1pad_decrypt_complete_cb, req);
369
Herbert Xud858b072016-06-29 19:32:28 +0800370 /* Reuse input buffer, output to a new buffer */
371 akcipher_request_set_crypt(&req_ctx->child_req, req->src,
372 req_ctx->out_sg, req->src_len,
373 ctx->key_size);
374
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100375 err = crypto_akcipher_decrypt(&req_ctx->child_req);
Gilad Ben-Yossef4e5b0ad2017-10-18 08:00:36 +0100376 if (err != -EINPROGRESS && err != -EBUSY)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100377 return pkcs1pad_decrypt_complete(req, err);
378
379 return err;
380}
381
382static int pkcs1pad_sign(struct akcipher_request *req)
383{
384 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
385 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
386 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
Herbert Xuc0d20d22016-06-29 19:32:23 +0800387 struct akcipher_instance *inst = akcipher_alg_instance(tfm);
388 struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
389 const struct rsa_asn1_template *digest_info = ictx->digest_info;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100390 int err;
Tadeusz Struka49de372016-03-03 21:49:26 +0000391 unsigned int ps_end, digest_size = 0;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100392
393 if (!ctx->key_size)
394 return -EINVAL;
395
Denis Kenziorb3a8c8a2018-10-09 17:47:53 +0100396 if (digest_info)
397 digest_size = digest_info->size;
Tadeusz Struka49de372016-03-03 21:49:26 +0000398
399 if (req->src_len + digest_size > ctx->key_size - 11)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100400 return -EOVERFLOW;
401
402 if (req->dst_len < ctx->key_size) {
403 req->dst_len = ctx->key_size;
404 return -EOVERFLOW;
405 }
406
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100407 req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len,
Herbert Xu3a32ce52016-06-29 19:32:26 +0800408 GFP_KERNEL);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100409 if (!req_ctx->in_buf)
410 return -ENOMEM;
411
Tadeusz Struka49de372016-03-03 21:49:26 +0000412 ps_end = ctx->key_size - digest_size - req->src_len - 2;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100413 req_ctx->in_buf[0] = 0x01;
414 memset(req_ctx->in_buf + 1, 0xff, ps_end - 1);
415 req_ctx->in_buf[ps_end] = 0x00;
416
Denis Kenziorb3a8c8a2018-10-09 17:47:53 +0100417 if (digest_info)
418 memcpy(req_ctx->in_buf + ps_end + 1, digest_info->data,
419 digest_info->size);
Tadeusz Struka49de372016-03-03 21:49:26 +0000420
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100421 pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
422 ctx->key_size - 1 - req->src_len, req->src);
423
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100424 akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
425 akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
426 pkcs1pad_encrypt_sign_complete_cb, req);
427
Herbert Xud858b072016-06-29 19:32:28 +0800428 /* Reuse output buffer */
429 akcipher_request_set_crypt(&req_ctx->child_req, req_ctx->in_sg,
430 req->dst, ctx->key_size - 1, req->dst_len);
431
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100432 err = crypto_akcipher_sign(&req_ctx->child_req);
Gilad Ben-Yossef4e5b0ad2017-10-18 08:00:36 +0100433 if (err != -EINPROGRESS && err != -EBUSY)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100434 return pkcs1pad_encrypt_sign_complete(req, err);
435
436 return err;
437}
438
439static int pkcs1pad_verify_complete(struct akcipher_request *req, int err)
440{
441 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
442 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
443 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
Herbert Xuc0d20d22016-06-29 19:32:23 +0800444 struct akcipher_instance *inst = akcipher_alg_instance(tfm);
445 struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
446 const struct rsa_asn1_template *digest_info = ictx->digest_info;
Herbert Xu27710b82016-07-03 10:46:11 +0800447 unsigned int dst_len;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100448 unsigned int pos;
Herbert Xu27710b82016-07-03 10:46:11 +0800449 u8 *out_buf;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100450
451 if (err)
452 goto done;
453
Herbert Xu27710b82016-07-03 10:46:11 +0800454 err = -EINVAL;
455 dst_len = req_ctx->child_req.dst_len;
456 if (dst_len < ctx->key_size - 1)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100457 goto done;
Herbert Xu27710b82016-07-03 10:46:11 +0800458
459 out_buf = req_ctx->out_buf;
460 if (dst_len == ctx->key_size) {
461 if (out_buf[0] != 0x00)
462 /* Decrypted value had no leading 0 byte */
463 goto done;
464
465 dst_len--;
466 out_buf++;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100467 }
468
Tadeusz Struka49de372016-03-03 21:49:26 +0000469 err = -EBADMSG;
Herbert Xu27710b82016-07-03 10:46:11 +0800470 if (out_buf[0] != 0x01)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100471 goto done;
Tadeusz Struka49de372016-03-03 21:49:26 +0000472
Herbert Xu27710b82016-07-03 10:46:11 +0800473 for (pos = 1; pos < dst_len; pos++)
474 if (out_buf[pos] != 0xff)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100475 break;
Tadeusz Struka49de372016-03-03 21:49:26 +0000476
Herbert Xu27710b82016-07-03 10:46:11 +0800477 if (pos < 9 || pos == dst_len || out_buf[pos] != 0x00)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100478 goto done;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100479 pos++;
480
Denis Kenziorb3a8c8a2018-10-09 17:47:53 +0100481 if (digest_info) {
482 if (crypto_memneq(out_buf + pos, digest_info->data,
483 digest_info->size))
484 goto done;
Tadeusz Struka49de372016-03-03 21:49:26 +0000485
Denis Kenziorb3a8c8a2018-10-09 17:47:53 +0100486 pos += digest_info->size;
487 }
Tadeusz Struka49de372016-03-03 21:49:26 +0000488
489 err = 0;
490
Herbert Xu27710b82016-07-03 10:46:11 +0800491 if (req->dst_len < dst_len - pos)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100492 err = -EOVERFLOW;
Herbert Xu27710b82016-07-03 10:46:11 +0800493 req->dst_len = dst_len - pos;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100494
495 if (!err)
496 sg_copy_from_buffer(req->dst,
497 sg_nents_for_len(req->dst, req->dst_len),
Herbert Xu27710b82016-07-03 10:46:11 +0800498 out_buf + pos, req->dst_len);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100499done:
500 kzfree(req_ctx->out_buf);
501
502 return err;
503}
504
505static void pkcs1pad_verify_complete_cb(
506 struct crypto_async_request *child_async_req, int err)
507{
508 struct akcipher_request *req = child_async_req->data;
509 struct crypto_async_request async_req;
510
511 if (err == -EINPROGRESS)
512 return;
513
514 async_req.data = req->base.data;
515 async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
516 async_req.flags = child_async_req->flags;
517 req->base.complete(&async_req, pkcs1pad_verify_complete(req, err));
518}
519
520/*
521 * The verify operation is here for completeness similar to the verification
522 * defined in RFC2313 section 10.2 except that block type 0 is not accepted,
523 * as in RFC2437. RFC2437 section 9.2 doesn't define any operation to
524 * retrieve the DigestInfo from a signature, instead the user is expected
525 * to call the sign operation to generate the expected signature and compare
526 * signatures instead of the message-digests.
527 */
528static int pkcs1pad_verify(struct akcipher_request *req)
529{
530 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
531 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
532 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
533 int err;
534
Tadeusz Struka49de372016-03-03 21:49:26 +0000535 if (!ctx->key_size || req->src_len < ctx->key_size)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100536 return -EINVAL;
537
Herbert Xu3a32ce52016-06-29 19:32:26 +0800538 req_ctx->out_buf = kmalloc(ctx->key_size, GFP_KERNEL);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100539 if (!req_ctx->out_buf)
540 return -ENOMEM;
541
542 pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
Tadeusz Struk6f0904a2016-04-06 14:42:32 -0700543 ctx->key_size, NULL);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100544
545 akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
546 akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
547 pkcs1pad_verify_complete_cb, req);
548
Herbert Xud858b072016-06-29 19:32:28 +0800549 /* Reuse input buffer, output to a new buffer */
550 akcipher_request_set_crypt(&req_ctx->child_req, req->src,
551 req_ctx->out_sg, req->src_len,
552 ctx->key_size);
553
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100554 err = crypto_akcipher_verify(&req_ctx->child_req);
Gilad Ben-Yossef4e5b0ad2017-10-18 08:00:36 +0100555 if (err != -EINPROGRESS && err != -EBUSY)
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100556 return pkcs1pad_verify_complete(req, err);
557
558 return err;
559}
560
561static int pkcs1pad_init_tfm(struct crypto_akcipher *tfm)
562{
563 struct akcipher_instance *inst = akcipher_alg_instance(tfm);
Tadeusz Struka49de372016-03-03 21:49:26 +0000564 struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100565 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
566 struct crypto_akcipher *child_tfm;
567
Herbert Xuc0d20d22016-06-29 19:32:23 +0800568 child_tfm = crypto_spawn_akcipher(&ictx->spawn);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100569 if (IS_ERR(child_tfm))
570 return PTR_ERR(child_tfm);
571
572 ctx->child = child_tfm;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100573 return 0;
574}
575
576static void pkcs1pad_exit_tfm(struct crypto_akcipher *tfm)
577{
578 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
579
580 crypto_free_akcipher(ctx->child);
581}
582
583static void pkcs1pad_free(struct akcipher_instance *inst)
584{
Tadeusz Struka49de372016-03-03 21:49:26 +0000585 struct pkcs1pad_inst_ctx *ctx = akcipher_instance_ctx(inst);
586 struct crypto_akcipher_spawn *spawn = &ctx->spawn;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100587
588 crypto_drop_akcipher(spawn);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100589 kfree(inst);
590}
591
592static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb)
593{
Herbert Xuc0d20d22016-06-29 19:32:23 +0800594 const struct rsa_asn1_template *digest_info;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100595 struct crypto_attr_type *algt;
596 struct akcipher_instance *inst;
Tadeusz Struka49de372016-03-03 21:49:26 +0000597 struct pkcs1pad_inst_ctx *ctx;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100598 struct crypto_akcipher_spawn *spawn;
599 struct akcipher_alg *rsa_alg;
600 const char *rsa_alg_name;
Tadeusz Struka49de372016-03-03 21:49:26 +0000601 const char *hash_name;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100602 int err;
603
604 algt = crypto_get_attr_type(tb);
605 if (IS_ERR(algt))
606 return PTR_ERR(algt);
607
608 if ((algt->type ^ CRYPTO_ALG_TYPE_AKCIPHER) & algt->mask)
609 return -EINVAL;
610
611 rsa_alg_name = crypto_attr_alg_name(tb[1]);
612 if (IS_ERR(rsa_alg_name))
613 return PTR_ERR(rsa_alg_name);
614
Tadeusz Struka49de372016-03-03 21:49:26 +0000615 hash_name = crypto_attr_alg_name(tb[2]);
616 if (IS_ERR(hash_name))
Denis Kenziorb3a8c8a2018-10-09 17:47:53 +0100617 hash_name = NULL;
Herbert Xuc0d20d22016-06-29 19:32:23 +0800618
Denis Kenziorb3a8c8a2018-10-09 17:47:53 +0100619 if (hash_name) {
620 digest_info = rsa_lookup_asn1(hash_name);
621 if (!digest_info)
622 return -EINVAL;
623 } else
624 digest_info = NULL;
Tadeusz Struka49de372016-03-03 21:49:26 +0000625
626 inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100627 if (!inst)
628 return -ENOMEM;
629
Tadeusz Struka49de372016-03-03 21:49:26 +0000630 ctx = akcipher_instance_ctx(inst);
631 spawn = &ctx->spawn;
Herbert Xuc0d20d22016-06-29 19:32:23 +0800632 ctx->digest_info = digest_info;
Tadeusz Struka49de372016-03-03 21:49:26 +0000633
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100634 crypto_set_spawn(&spawn->base, akcipher_crypto_instance(inst));
635 err = crypto_grab_akcipher(spawn, rsa_alg_name, 0,
636 crypto_requires_sync(algt->type, algt->mask));
637 if (err)
638 goto out_free_inst;
639
640 rsa_alg = crypto_spawn_akcipher_alg(spawn);
641
642 err = -ENAMETOOLONG;
Tadeusz Struka49de372016-03-03 21:49:26 +0000643
Denis Kenziorb3a8c8a2018-10-09 17:47:53 +0100644 if (!hash_name) {
645 if (snprintf(inst->alg.base.cra_name,
646 CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s)",
647 rsa_alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
648 goto out_drop_alg;
649
650 if (snprintf(inst->alg.base.cra_driver_name,
651 CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s)",
652 rsa_alg->base.cra_driver_name) >=
653 CRYPTO_MAX_ALG_NAME)
654 goto out_drop_alg;
655 } else {
656 if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
657 "pkcs1pad(%s,%s)", rsa_alg->base.cra_name,
658 hash_name) >= CRYPTO_MAX_ALG_NAME)
659 goto out_drop_alg;
660
661 if (snprintf(inst->alg.base.cra_driver_name,
662 CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s,%s)",
663 rsa_alg->base.cra_driver_name,
664 hash_name) >= CRYPTO_MAX_ALG_NAME)
665 goto out_drop_alg;
666 }
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100667
668 inst->alg.base.cra_flags = rsa_alg->base.cra_flags & CRYPTO_ALG_ASYNC;
669 inst->alg.base.cra_priority = rsa_alg->base.cra_priority;
670 inst->alg.base.cra_ctxsize = sizeof(struct pkcs1pad_ctx);
671
672 inst->alg.init = pkcs1pad_init_tfm;
673 inst->alg.exit = pkcs1pad_exit_tfm;
674
675 inst->alg.encrypt = pkcs1pad_encrypt;
676 inst->alg.decrypt = pkcs1pad_decrypt;
677 inst->alg.sign = pkcs1pad_sign;
678 inst->alg.verify = pkcs1pad_verify;
679 inst->alg.set_pub_key = pkcs1pad_set_pub_key;
680 inst->alg.set_priv_key = pkcs1pad_set_priv_key;
681 inst->alg.max_size = pkcs1pad_get_max_size;
682 inst->alg.reqsize = sizeof(struct pkcs1pad_request) + rsa_alg->reqsize;
683
684 inst->free = pkcs1pad_free;
685
686 err = akcipher_register_instance(tmpl, inst);
687 if (err)
Herbert Xuc0d20d22016-06-29 19:32:23 +0800688 goto out_drop_alg;
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100689
690 return 0;
691
Andrzej Zaborowski3d5b1ec2015-12-05 17:09:34 +0100692out_drop_alg:
693 crypto_drop_akcipher(spawn);
694out_free_inst:
695 kfree(inst);
696 return err;
697}
698
699struct crypto_template rsa_pkcs1pad_tmpl = {
700 .name = "pkcs1pad",
701 .create = pkcs1pad_create,
702 .module = THIS_MODULE,
703};