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)
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>
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
,
37 struct pkcs1pad_ctx
*ctx
= akcipher_tfm_ctx(tfm
);
40 err
= crypto_akcipher_set_pub_key(ctx
->child
, key
, keylen
);
43 /* Find out new modulus size from rsa implementation */
44 size
= crypto_akcipher_maxsize(ctx
->child
);
46 ctx
->key_size
= size
> 0 ? size
: 0;
54 static int pkcs1pad_set_priv_key(struct crypto_akcipher
*tfm
, const void *key
,
57 struct pkcs1pad_ctx
*ctx
= akcipher_tfm_ctx(tfm
);
60 err
= crypto_akcipher_set_priv_key(ctx
->child
, key
, keylen
);
63 /* Find out new modulus size from rsa implementation */
64 size
= crypto_akcipher_maxsize(ctx
->child
);
66 ctx
->key_size
= size
> 0 ? size
: 0;
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
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
) {
94 sg_init_table(sg
, nsegs
);
95 sg_set_buf(sg
, buf
, len
);
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
);
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
;
119 sg_miter_start(&miter
, req
->dst
,
120 sg_nents_for_len(req
->dst
, pad_len
),
121 SG_MITER_ATOMIC
| SG_MITER_TO_SG
);
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
,
140 req
->dst_len
= ctx
->key_size
;
142 kfree(req_ctx
->in_buf
);
143 kzfree(req_ctx
->out_buf
);
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
)
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
);
170 unsigned int i
, ps_end
;
175 if (req
->src_len
> ctx
->key_size
- 11)
178 if (req
->dst_len
< ctx
->key_size
) {
179 req
->dst_len
= ctx
->key_size
;
183 if (ctx
->key_size
> PAGE_SIZE
)
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
)
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
);
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
&&
228 !(req
->base
.flags
& CRYPTO_TFM_REQ_MAY_BACKLOG
)))
229 return pkcs1pad_encrypt_sign_complete(req
, 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
);
241 if (err
== -EOVERFLOW
)
242 /* Decrypted value had no leading 0 byte */
248 if (req_ctx
->child_req
.dst_len
!= ctx
->key_size
- 1) {
253 if (req_ctx
->out_buf
[0] != 0x02) {
257 for (pos
= 1; pos
< req_ctx
->child_req
.dst_len
; pos
++)
258 if (req_ctx
->out_buf
[pos
] == 0x00)
260 if (pos
< 9 || pos
== req_ctx
->child_req
.dst_len
) {
266 if (req
->dst_len
< req_ctx
->child_req
.dst_len
- pos
)
268 req
->dst_len
= req_ctx
->child_req
.dst_len
- pos
;
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
);
276 kzfree(req_ctx
->out_buf
);
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
)
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
);
303 if (!ctx
->key_size
|| req
->src_len
!= ctx
->key_size
)
306 if (ctx
->key_size
> PAGE_SIZE
)
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
)
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
&&
331 !(req
->base
.flags
& CRYPTO_TFM_REQ_MAY_BACKLOG
)))
332 return pkcs1pad_decrypt_complete(req
, 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
);
348 if (req
->src_len
> ctx
->key_size
- 11)
351 if (req
->dst_len
< ctx
->key_size
) {
352 req
->dst_len
= ctx
->key_size
;
356 if (ctx
->key_size
> PAGE_SIZE
)
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
)
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
);
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
&&
400 !(req
->base
.flags
& CRYPTO_TFM_REQ_MAY_BACKLOG
)))
401 return pkcs1pad_encrypt_sign_complete(req
, 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
);
413 if (err
== -EOVERFLOW
)
414 /* Decrypted value had no leading 0 byte */
420 if (req_ctx
->child_req
.dst_len
!= ctx
->key_size
- 1) {
425 if (req_ctx
->out_buf
[0] != 0x01) {
429 for (pos
= 1; pos
< req_ctx
->child_req
.dst_len
; pos
++)
430 if (req_ctx
->out_buf
[pos
] != 0xff)
432 if (pos
< 9 || pos
== req_ctx
->child_req
.dst_len
||
433 req_ctx
->out_buf
[pos
] != 0x00) {
439 if (req
->dst_len
< req_ctx
->child_req
.dst_len
- pos
)
441 req
->dst_len
= req_ctx
->child_req
.dst_len
- pos
;
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
);
449 kzfree(req_ctx
->out_buf
);
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
)
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
);
484 if (!ctx
->key_size
|| req
->src_len
!= ctx
->key_size
)
487 if (ctx
->key_size
> PAGE_SIZE
)
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
)
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
&&
512 !(req
->base
.flags
& CRYPTO_TFM_REQ_MAY_BACKLOG
)))
513 return pkcs1pad_verify_complete(req
, 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
;
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
);
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
;
558 algt
= crypto_get_attr_type(tb
);
560 return PTR_ERR(algt
);
562 if ((algt
->type
^ CRYPTO_ALG_TYPE_AKCIPHER
) & algt
->mask
)
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
);
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
));
580 rsa_alg
= crypto_spawn_akcipher_alg(spawn
);
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
) >=
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
);
618 crypto_drop_akcipher(spawn
);
624 struct crypto_template rsa_pkcs1pad_tmpl
= {
626 .create
= pkcs1pad_create
,
627 .module
= THIS_MODULE
,