Merge tag 'dmaengine-fix-4.5-rc5' of git://git.infradead.org/users/vkoul/slave-dma
[linux/fpc-iii.git] / crypto / rsa-pkcs1pad.c
blob50f5c97e10873cb727654ee68e10be536f75bed8
1 /*
2 * RSA padding templates.
4 * Copyright (c) 2015 Intel Corporation
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.
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>
21 struct pkcs1pad_ctx {
22 struct crypto_akcipher *child;
24 unsigned int key_size;
27 struct pkcs1pad_request {
28 struct akcipher_request child_req;
30 struct scatterlist in_sg[3], out_sg[2];
31 uint8_t *in_buf, *out_buf;
34 static int pkcs1pad_set_pub_key(struct crypto_akcipher *tfm, const void *key,
35 unsigned int keylen)
37 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
38 int err, size;
40 err = crypto_akcipher_set_pub_key(ctx->child, key, keylen);
42 if (!err) {
43 /* Find out new modulus size from rsa implementation */
44 size = crypto_akcipher_maxsize(ctx->child);
46 ctx->key_size = size > 0 ? size : 0;
47 if (size <= 0)
48 err = size;
51 return err;
54 static int pkcs1pad_set_priv_key(struct crypto_akcipher *tfm, const void *key,
55 unsigned int keylen)
57 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
58 int err, size;
60 err = crypto_akcipher_set_priv_key(ctx->child, key, keylen);
62 if (!err) {
63 /* Find out new modulus size from rsa implementation */
64 size = crypto_akcipher_maxsize(ctx->child);
66 ctx->key_size = size > 0 ? size : 0;
67 if (size <= 0)
68 err = size;
71 return err;
74 static int pkcs1pad_get_max_size(struct crypto_akcipher *tfm)
76 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
79 * The maximum destination buffer size for the encrypt/sign operations
80 * will be the same as for RSA, even though it's smaller for
81 * decrypt/verify.
84 return ctx->key_size ?: -EINVAL;
87 static void pkcs1pad_sg_set_buf(struct scatterlist *sg, void *buf, size_t len,
88 struct scatterlist *next)
90 int nsegs = next ? 1 : 0;
92 if (offset_in_page(buf) + len <= PAGE_SIZE) {
93 nsegs += 1;
94 sg_init_table(sg, nsegs);
95 sg_set_buf(sg, buf, len);
96 } else {
97 nsegs += 2;
98 sg_init_table(sg, nsegs);
99 sg_set_buf(sg + 0, buf, PAGE_SIZE - offset_in_page(buf));
100 sg_set_buf(sg + 1, buf + PAGE_SIZE - offset_in_page(buf),
101 offset_in_page(buf) + len - PAGE_SIZE);
104 if (next)
105 sg_chain(sg, nsegs, next);
108 static int pkcs1pad_encrypt_sign_complete(struct akcipher_request *req, int err)
110 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
111 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
112 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
113 size_t pad_len = ctx->key_size - req_ctx->child_req.dst_len;
114 size_t chunk_len, pad_left;
115 struct sg_mapping_iter miter;
117 if (!err) {
118 if (pad_len) {
119 sg_miter_start(&miter, req->dst,
120 sg_nents_for_len(req->dst, pad_len),
121 SG_MITER_ATOMIC | SG_MITER_TO_SG);
123 pad_left = pad_len;
124 while (pad_left) {
125 sg_miter_next(&miter);
127 chunk_len = min(miter.length, pad_left);
128 memset(miter.addr, 0, chunk_len);
129 pad_left -= chunk_len;
132 sg_miter_stop(&miter);
135 sg_pcopy_from_buffer(req->dst,
136 sg_nents_for_len(req->dst, ctx->key_size),
137 req_ctx->out_buf, req_ctx->child_req.dst_len,
138 pad_len);
140 req->dst_len = ctx->key_size;
142 kfree(req_ctx->in_buf);
143 kzfree(req_ctx->out_buf);
145 return err;
148 static void pkcs1pad_encrypt_sign_complete_cb(
149 struct crypto_async_request *child_async_req, int err)
151 struct akcipher_request *req = child_async_req->data;
152 struct crypto_async_request async_req;
154 if (err == -EINPROGRESS)
155 return;
157 async_req.data = req->base.data;
158 async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
159 async_req.flags = child_async_req->flags;
160 req->base.complete(&async_req,
161 pkcs1pad_encrypt_sign_complete(req, err));
164 static int pkcs1pad_encrypt(struct akcipher_request *req)
166 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
167 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
168 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
169 int err;
170 unsigned int i, ps_end;
172 if (!ctx->key_size)
173 return -EINVAL;
175 if (req->src_len > ctx->key_size - 11)
176 return -EOVERFLOW;
178 if (req->dst_len < ctx->key_size) {
179 req->dst_len = ctx->key_size;
180 return -EOVERFLOW;
183 if (ctx->key_size > PAGE_SIZE)
184 return -ENOTSUPP;
187 * Replace both input and output to add the padding in the input and
188 * the potential missing leading zeros in the output.
190 req_ctx->child_req.src = req_ctx->in_sg;
191 req_ctx->child_req.src_len = ctx->key_size - 1;
192 req_ctx->child_req.dst = req_ctx->out_sg;
193 req_ctx->child_req.dst_len = ctx->key_size;
195 req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len,
196 (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
197 GFP_KERNEL : GFP_ATOMIC);
198 if (!req_ctx->in_buf)
199 return -ENOMEM;
201 ps_end = ctx->key_size - req->src_len - 2;
202 req_ctx->in_buf[0] = 0x02;
203 for (i = 1; i < ps_end; i++)
204 req_ctx->in_buf[i] = 1 + prandom_u32_max(255);
205 req_ctx->in_buf[ps_end] = 0x00;
207 pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
208 ctx->key_size - 1 - req->src_len, req->src);
210 req_ctx->out_buf = kmalloc(ctx->key_size,
211 (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
212 GFP_KERNEL : GFP_ATOMIC);
213 if (!req_ctx->out_buf) {
214 kfree(req_ctx->in_buf);
215 return -ENOMEM;
218 pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
219 ctx->key_size, NULL);
221 akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
222 akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
223 pkcs1pad_encrypt_sign_complete_cb, req);
225 err = crypto_akcipher_encrypt(&req_ctx->child_req);
226 if (err != -EINPROGRESS &&
227 (err != -EBUSY ||
228 !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))
229 return pkcs1pad_encrypt_sign_complete(req, err);
231 return err;
234 static int pkcs1pad_decrypt_complete(struct akcipher_request *req, int err)
236 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
237 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
238 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
239 unsigned int pos;
241 if (err == -EOVERFLOW)
242 /* Decrypted value had no leading 0 byte */
243 err = -EINVAL;
245 if (err)
246 goto done;
248 if (req_ctx->child_req.dst_len != ctx->key_size - 1) {
249 err = -EINVAL;
250 goto done;
253 if (req_ctx->out_buf[0] != 0x02) {
254 err = -EINVAL;
255 goto done;
257 for (pos = 1; pos < req_ctx->child_req.dst_len; pos++)
258 if (req_ctx->out_buf[pos] == 0x00)
259 break;
260 if (pos < 9 || pos == req_ctx->child_req.dst_len) {
261 err = -EINVAL;
262 goto done;
264 pos++;
266 if (req->dst_len < req_ctx->child_req.dst_len - pos)
267 err = -EOVERFLOW;
268 req->dst_len = req_ctx->child_req.dst_len - pos;
270 if (!err)
271 sg_copy_from_buffer(req->dst,
272 sg_nents_for_len(req->dst, req->dst_len),
273 req_ctx->out_buf + pos, req->dst_len);
275 done:
276 kzfree(req_ctx->out_buf);
278 return err;
281 static void pkcs1pad_decrypt_complete_cb(
282 struct crypto_async_request *child_async_req, int err)
284 struct akcipher_request *req = child_async_req->data;
285 struct crypto_async_request async_req;
287 if (err == -EINPROGRESS)
288 return;
290 async_req.data = req->base.data;
291 async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
292 async_req.flags = child_async_req->flags;
293 req->base.complete(&async_req, pkcs1pad_decrypt_complete(req, err));
296 static int pkcs1pad_decrypt(struct akcipher_request *req)
298 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
299 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
300 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
301 int err;
303 if (!ctx->key_size || req->src_len != ctx->key_size)
304 return -EINVAL;
306 if (ctx->key_size > PAGE_SIZE)
307 return -ENOTSUPP;
309 /* Reuse input buffer, output to a new buffer */
310 req_ctx->child_req.src = req->src;
311 req_ctx->child_req.src_len = req->src_len;
312 req_ctx->child_req.dst = req_ctx->out_sg;
313 req_ctx->child_req.dst_len = ctx->key_size - 1;
315 req_ctx->out_buf = kmalloc(ctx->key_size - 1,
316 (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
317 GFP_KERNEL : GFP_ATOMIC);
318 if (!req_ctx->out_buf)
319 return -ENOMEM;
321 pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
322 ctx->key_size - 1, NULL);
324 akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
325 akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
326 pkcs1pad_decrypt_complete_cb, req);
328 err = crypto_akcipher_decrypt(&req_ctx->child_req);
329 if (err != -EINPROGRESS &&
330 (err != -EBUSY ||
331 !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))
332 return pkcs1pad_decrypt_complete(req, err);
334 return err;
337 static int pkcs1pad_sign(struct akcipher_request *req)
339 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
340 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
341 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
342 int err;
343 unsigned int ps_end;
345 if (!ctx->key_size)
346 return -EINVAL;
348 if (req->src_len > ctx->key_size - 11)
349 return -EOVERFLOW;
351 if (req->dst_len < ctx->key_size) {
352 req->dst_len = ctx->key_size;
353 return -EOVERFLOW;
356 if (ctx->key_size > PAGE_SIZE)
357 return -ENOTSUPP;
360 * Replace both input and output to add the padding in the input and
361 * the potential missing leading zeros in the output.
363 req_ctx->child_req.src = req_ctx->in_sg;
364 req_ctx->child_req.src_len = ctx->key_size - 1;
365 req_ctx->child_req.dst = req_ctx->out_sg;
366 req_ctx->child_req.dst_len = ctx->key_size;
368 req_ctx->in_buf = kmalloc(ctx->key_size - 1 - req->src_len,
369 (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
370 GFP_KERNEL : GFP_ATOMIC);
371 if (!req_ctx->in_buf)
372 return -ENOMEM;
374 ps_end = ctx->key_size - req->src_len - 2;
375 req_ctx->in_buf[0] = 0x01;
376 memset(req_ctx->in_buf + 1, 0xff, ps_end - 1);
377 req_ctx->in_buf[ps_end] = 0x00;
379 pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
380 ctx->key_size - 1 - req->src_len, req->src);
382 req_ctx->out_buf = kmalloc(ctx->key_size,
383 (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
384 GFP_KERNEL : GFP_ATOMIC);
385 if (!req_ctx->out_buf) {
386 kfree(req_ctx->in_buf);
387 return -ENOMEM;
390 pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
391 ctx->key_size, NULL);
393 akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
394 akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
395 pkcs1pad_encrypt_sign_complete_cb, req);
397 err = crypto_akcipher_sign(&req_ctx->child_req);
398 if (err != -EINPROGRESS &&
399 (err != -EBUSY ||
400 !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))
401 return pkcs1pad_encrypt_sign_complete(req, err);
403 return err;
406 static int pkcs1pad_verify_complete(struct akcipher_request *req, int err)
408 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
409 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
410 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
411 unsigned int pos;
413 if (err == -EOVERFLOW)
414 /* Decrypted value had no leading 0 byte */
415 err = -EINVAL;
417 if (err)
418 goto done;
420 if (req_ctx->child_req.dst_len != ctx->key_size - 1) {
421 err = -EINVAL;
422 goto done;
425 if (req_ctx->out_buf[0] != 0x01) {
426 err = -EINVAL;
427 goto done;
429 for (pos = 1; pos < req_ctx->child_req.dst_len; pos++)
430 if (req_ctx->out_buf[pos] != 0xff)
431 break;
432 if (pos < 9 || pos == req_ctx->child_req.dst_len ||
433 req_ctx->out_buf[pos] != 0x00) {
434 err = -EINVAL;
435 goto done;
437 pos++;
439 if (req->dst_len < req_ctx->child_req.dst_len - pos)
440 err = -EOVERFLOW;
441 req->dst_len = req_ctx->child_req.dst_len - pos;
443 if (!err)
444 sg_copy_from_buffer(req->dst,
445 sg_nents_for_len(req->dst, req->dst_len),
446 req_ctx->out_buf + pos, req->dst_len);
448 done:
449 kzfree(req_ctx->out_buf);
451 return err;
454 static void pkcs1pad_verify_complete_cb(
455 struct crypto_async_request *child_async_req, int err)
457 struct akcipher_request *req = child_async_req->data;
458 struct crypto_async_request async_req;
460 if (err == -EINPROGRESS)
461 return;
463 async_req.data = req->base.data;
464 async_req.tfm = crypto_akcipher_tfm(crypto_akcipher_reqtfm(req));
465 async_req.flags = child_async_req->flags;
466 req->base.complete(&async_req, pkcs1pad_verify_complete(req, err));
470 * The verify operation is here for completeness similar to the verification
471 * defined in RFC2313 section 10.2 except that block type 0 is not accepted,
472 * as in RFC2437. RFC2437 section 9.2 doesn't define any operation to
473 * retrieve the DigestInfo from a signature, instead the user is expected
474 * to call the sign operation to generate the expected signature and compare
475 * signatures instead of the message-digests.
477 static int pkcs1pad_verify(struct akcipher_request *req)
479 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
480 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
481 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
482 int err;
484 if (!ctx->key_size || req->src_len != ctx->key_size)
485 return -EINVAL;
487 if (ctx->key_size > PAGE_SIZE)
488 return -ENOTSUPP;
490 /* Reuse input buffer, output to a new buffer */
491 req_ctx->child_req.src = req->src;
492 req_ctx->child_req.src_len = req->src_len;
493 req_ctx->child_req.dst = req_ctx->out_sg;
494 req_ctx->child_req.dst_len = ctx->key_size - 1;
496 req_ctx->out_buf = kmalloc(ctx->key_size - 1,
497 (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ?
498 GFP_KERNEL : GFP_ATOMIC);
499 if (!req_ctx->out_buf)
500 return -ENOMEM;
502 pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf,
503 ctx->key_size - 1, NULL);
505 akcipher_request_set_tfm(&req_ctx->child_req, ctx->child);
506 akcipher_request_set_callback(&req_ctx->child_req, req->base.flags,
507 pkcs1pad_verify_complete_cb, req);
509 err = crypto_akcipher_verify(&req_ctx->child_req);
510 if (err != -EINPROGRESS &&
511 (err != -EBUSY ||
512 !(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)))
513 return pkcs1pad_verify_complete(req, err);
515 return err;
518 static int pkcs1pad_init_tfm(struct crypto_akcipher *tfm)
520 struct akcipher_instance *inst = akcipher_alg_instance(tfm);
521 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
522 struct crypto_akcipher *child_tfm;
524 child_tfm = crypto_spawn_akcipher(akcipher_instance_ctx(inst));
525 if (IS_ERR(child_tfm))
526 return PTR_ERR(child_tfm);
528 ctx->child = child_tfm;
530 return 0;
533 static void pkcs1pad_exit_tfm(struct crypto_akcipher *tfm)
535 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
537 crypto_free_akcipher(ctx->child);
540 static void pkcs1pad_free(struct akcipher_instance *inst)
542 struct crypto_akcipher_spawn *spawn = akcipher_instance_ctx(inst);
544 crypto_drop_akcipher(spawn);
546 kfree(inst);
549 static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb)
551 struct crypto_attr_type *algt;
552 struct akcipher_instance *inst;
553 struct crypto_akcipher_spawn *spawn;
554 struct akcipher_alg *rsa_alg;
555 const char *rsa_alg_name;
556 int err;
558 algt = crypto_get_attr_type(tb);
559 if (IS_ERR(algt))
560 return PTR_ERR(algt);
562 if ((algt->type ^ CRYPTO_ALG_TYPE_AKCIPHER) & algt->mask)
563 return -EINVAL;
565 rsa_alg_name = crypto_attr_alg_name(tb[1]);
566 if (IS_ERR(rsa_alg_name))
567 return PTR_ERR(rsa_alg_name);
569 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
570 if (!inst)
571 return -ENOMEM;
573 spawn = akcipher_instance_ctx(inst);
574 crypto_set_spawn(&spawn->base, akcipher_crypto_instance(inst));
575 err = crypto_grab_akcipher(spawn, rsa_alg_name, 0,
576 crypto_requires_sync(algt->type, algt->mask));
577 if (err)
578 goto out_free_inst;
580 rsa_alg = crypto_spawn_akcipher_alg(spawn);
582 err = -ENAMETOOLONG;
583 if (snprintf(inst->alg.base.cra_name,
584 CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s)",
585 rsa_alg->base.cra_name) >=
586 CRYPTO_MAX_ALG_NAME ||
587 snprintf(inst->alg.base.cra_driver_name,
588 CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s)",
589 rsa_alg->base.cra_driver_name) >=
590 CRYPTO_MAX_ALG_NAME)
591 goto out_drop_alg;
593 inst->alg.base.cra_flags = rsa_alg->base.cra_flags & CRYPTO_ALG_ASYNC;
594 inst->alg.base.cra_priority = rsa_alg->base.cra_priority;
595 inst->alg.base.cra_ctxsize = sizeof(struct pkcs1pad_ctx);
597 inst->alg.init = pkcs1pad_init_tfm;
598 inst->alg.exit = pkcs1pad_exit_tfm;
600 inst->alg.encrypt = pkcs1pad_encrypt;
601 inst->alg.decrypt = pkcs1pad_decrypt;
602 inst->alg.sign = pkcs1pad_sign;
603 inst->alg.verify = pkcs1pad_verify;
604 inst->alg.set_pub_key = pkcs1pad_set_pub_key;
605 inst->alg.set_priv_key = pkcs1pad_set_priv_key;
606 inst->alg.max_size = pkcs1pad_get_max_size;
607 inst->alg.reqsize = sizeof(struct pkcs1pad_request) + rsa_alg->reqsize;
609 inst->free = pkcs1pad_free;
611 err = akcipher_register_instance(tmpl, inst);
612 if (err)
613 goto out_drop_alg;
615 return 0;
617 out_drop_alg:
618 crypto_drop_akcipher(spawn);
619 out_free_inst:
620 kfree(inst);
621 return err;
624 struct crypto_template rsa_pkcs1pad_tmpl = {
625 .name = "pkcs1pad",
626 .create = pkcs1pad_create,
627 .module = THIS_MODULE,