1 /* 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 21 struct pkcs1pad_ctx { 22 struct crypto_akcipher *child; 23 24 unsigned int key_size; 25 }; 26 27 struct pkcs1pad_request { 28 struct akcipher_request child_req; 29 30 struct scatterlist in_sg[3], out_sg[2]; 31 uint8_t *in_buf, *out_buf; 32 }; 33 34 static int pkcs1pad_set_pub_key(struct crypto_akcipher *tfm, const void *key, 35 unsigned int keylen) 36 { 37 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); 38 int err, size; 39 40 err = crypto_akcipher_set_pub_key(ctx->child, key, keylen); 41 42 if (!err) { 43 /* Find out new modulus size from rsa implementation */ 44 size = crypto_akcipher_maxsize(ctx->child); 45 46 ctx->key_size = size > 0 ? size : 0; 47 if (size <= 0) 48 err = size; 49 } 50 51 return err; 52 } 53 54 static int pkcs1pad_set_priv_key(struct crypto_akcipher *tfm, const void *key, 55 unsigned int keylen) 56 { 57 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); 58 int err, size; 59 60 err = crypto_akcipher_set_priv_key(ctx->child, key, keylen); 61 62 if (!err) { 63 /* Find out new modulus size from rsa implementation */ 64 size = crypto_akcipher_maxsize(ctx->child); 65 66 ctx->key_size = size > 0 ? size : 0; 67 if (size <= 0) 68 err = size; 69 } 70 71 return err; 72 } 73 74 static int pkcs1pad_get_max_size(struct crypto_akcipher *tfm) 75 { 76 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); 77 78 /* 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. 82 */ 83 84 return ctx->key_size ?: -EINVAL; 85 } 86 87 static void pkcs1pad_sg_set_buf(struct scatterlist *sg, void *buf, size_t len, 88 struct scatterlist *next) 89 { 90 int nsegs = next ? 1 : 0; 91 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); 102 } 103 104 if (next) 105 sg_chain(sg, nsegs, next); 106 } 107 108 static int pkcs1pad_encrypt_sign_complete(struct akcipher_request *req, int err) 109 { 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; 116 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); 122 123 pad_left = pad_len; 124 while (pad_left) { 125 sg_miter_next(&miter); 126 127 chunk_len = min(miter.length, pad_left); 128 memset(miter.addr, 0, chunk_len); 129 pad_left -= chunk_len; 130 } 131 132 sg_miter_stop(&miter); 133 } 134 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); 139 } 140 req->dst_len = ctx->key_size; 141 142 kfree(req_ctx->in_buf); 143 kzfree(req_ctx->out_buf); 144 145 return err; 146 } 147 148 static void pkcs1pad_encrypt_sign_complete_cb( 149 struct crypto_async_request *child_async_req, int err) 150 { 151 struct akcipher_request *req = child_async_req->data; 152 struct crypto_async_request async_req; 153 154 if (err == -EINPROGRESS) 155 return; 156 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)); 162 } 163 164 static int pkcs1pad_encrypt(struct akcipher_request *req) 165 { 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; 171 172 if (!ctx->key_size) 173 return -EINVAL; 174 175 if (req->src_len > ctx->key_size - 11) 176 return -EOVERFLOW; 177 178 if (req->dst_len < ctx->key_size) { 179 req->dst_len = ctx->key_size; 180 return -EOVERFLOW; 181 } 182 183 if (ctx->key_size > PAGE_SIZE) 184 return -ENOTSUPP; 185 186 /* 187 * Replace both input and output to add the padding in the input and 188 * the potential missing leading zeros in the output. 189 */ 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; 194 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; 200 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; 206 207 pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf, 208 ctx->key_size - 1 - req->src_len, req->src); 209 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; 216 } 217 218 pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf, 219 ctx->key_size, NULL); 220 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); 224 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); 230 231 return err; 232 } 233 234 static int pkcs1pad_decrypt_complete(struct akcipher_request *req, int err) 235 { 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; 240 241 if (err == -EOVERFLOW) 242 /* Decrypted value had no leading 0 byte */ 243 err = -EINVAL; 244 245 if (err) 246 goto done; 247 248 if (req_ctx->child_req.dst_len != ctx->key_size - 1) { 249 err = -EINVAL; 250 goto done; 251 } 252 253 if (req_ctx->out_buf[0] != 0x02) { 254 err = -EINVAL; 255 goto done; 256 } 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; 263 } 264 pos++; 265 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; 269 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); 274 275 done: 276 kzfree(req_ctx->out_buf); 277 278 return err; 279 } 280 281 static void pkcs1pad_decrypt_complete_cb( 282 struct crypto_async_request *child_async_req, int err) 283 { 284 struct akcipher_request *req = child_async_req->data; 285 struct crypto_async_request async_req; 286 287 if (err == -EINPROGRESS) 288 return; 289 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)); 294 } 295 296 static int pkcs1pad_decrypt(struct akcipher_request *req) 297 { 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; 302 303 if (!ctx->key_size || req->src_len != ctx->key_size) 304 return -EINVAL; 305 306 if (ctx->key_size > PAGE_SIZE) 307 return -ENOTSUPP; 308 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; 314 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; 320 321 pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf, 322 ctx->key_size - 1, NULL); 323 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); 327 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); 333 334 return err; 335 } 336 337 static int pkcs1pad_sign(struct akcipher_request *req) 338 { 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; 344 345 if (!ctx->key_size) 346 return -EINVAL; 347 348 if (req->src_len > ctx->key_size - 11) 349 return -EOVERFLOW; 350 351 if (req->dst_len < ctx->key_size) { 352 req->dst_len = ctx->key_size; 353 return -EOVERFLOW; 354 } 355 356 if (ctx->key_size > PAGE_SIZE) 357 return -ENOTSUPP; 358 359 /* 360 * Replace both input and output to add the padding in the input and 361 * the potential missing leading zeros in the output. 362 */ 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; 367 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; 373 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; 378 379 pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf, 380 ctx->key_size - 1 - req->src_len, req->src); 381 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; 388 } 389 390 pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf, 391 ctx->key_size, NULL); 392 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); 396 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); 402 403 return err; 404 } 405 406 static int pkcs1pad_verify_complete(struct akcipher_request *req, int err) 407 { 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; 412 413 if (err == -EOVERFLOW) 414 /* Decrypted value had no leading 0 byte */ 415 err = -EINVAL; 416 417 if (err) 418 goto done; 419 420 if (req_ctx->child_req.dst_len != ctx->key_size - 1) { 421 err = -EINVAL; 422 goto done; 423 } 424 425 if (req_ctx->out_buf[0] != 0x01) { 426 err = -EINVAL; 427 goto done; 428 } 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; 436 } 437 pos++; 438 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; 442 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); 447 448 done: 449 kzfree(req_ctx->out_buf); 450 451 return err; 452 } 453 454 static void pkcs1pad_verify_complete_cb( 455 struct crypto_async_request *child_async_req, int err) 456 { 457 struct akcipher_request *req = child_async_req->data; 458 struct crypto_async_request async_req; 459 460 if (err == -EINPROGRESS) 461 return; 462 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)); 467 } 468 469 /* 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. 476 */ 477 static int pkcs1pad_verify(struct akcipher_request *req) 478 { 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; 483 484 if (!ctx->key_size || req->src_len != ctx->key_size) 485 return -EINVAL; 486 487 if (ctx->key_size > PAGE_SIZE) 488 return -ENOTSUPP; 489 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; 495 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; 501 502 pkcs1pad_sg_set_buf(req_ctx->out_sg, req_ctx->out_buf, 503 ctx->key_size - 1, NULL); 504 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); 508 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); 514 515 return err; 516 } 517 518 static int pkcs1pad_init_tfm(struct crypto_akcipher *tfm) 519 { 520 struct akcipher_instance *inst = akcipher_alg_instance(tfm); 521 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); 522 struct crypto_akcipher *child_tfm; 523 524 child_tfm = crypto_spawn_akcipher(akcipher_instance_ctx(inst)); 525 if (IS_ERR(child_tfm)) 526 return PTR_ERR(child_tfm); 527 528 ctx->child = child_tfm; 529 530 return 0; 531 } 532 533 static void pkcs1pad_exit_tfm(struct crypto_akcipher *tfm) 534 { 535 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); 536 537 crypto_free_akcipher(ctx->child); 538 } 539 540 static void pkcs1pad_free(struct akcipher_instance *inst) 541 { 542 struct crypto_akcipher_spawn *spawn = akcipher_instance_ctx(inst); 543 544 crypto_drop_akcipher(spawn); 545 546 kfree(inst); 547 } 548 549 static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb) 550 { 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; 557 558 algt = crypto_get_attr_type(tb); 559 if (IS_ERR(algt)) 560 return PTR_ERR(algt); 561 562 if ((algt->type ^ CRYPTO_ALG_TYPE_AKCIPHER) & algt->mask) 563 return -EINVAL; 564 565 rsa_alg_name = crypto_attr_alg_name(tb[1]); 566 if (IS_ERR(rsa_alg_name)) 567 return PTR_ERR(rsa_alg_name); 568 569 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL); 570 if (!inst) 571 return -ENOMEM; 572 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; 579 580 rsa_alg = crypto_spawn_akcipher_alg(spawn); 581 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; 592 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); 596 597 inst->alg.init = pkcs1pad_init_tfm; 598 inst->alg.exit = pkcs1pad_exit_tfm; 599 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; 608 609 inst->free = pkcs1pad_free; 610 611 err = akcipher_register_instance(tmpl, inst); 612 if (err) 613 goto out_drop_alg; 614 615 return 0; 616 617 out_drop_alg: 618 crypto_drop_akcipher(spawn); 619 out_free_inst: 620 kfree(inst); 621 return err; 622 } 623 624 struct crypto_template rsa_pkcs1pad_tmpl = { 625 .name = "pkcs1pad", 626 .create = pkcs1pad_create, 627 .module = THIS_MODULE, 628 }; 629