Skip to content

Commit 27710b8

Browse files
committed
crypto: rsa-pkcs1pad - Fix regression from leading zeros
As the software RSA implementation now produces fixed-length output, we need to eliminate leading zeros in the calling code instead. This patch does just that for pkcs1pad signature verification. Fixes: 9b45b7b ("crypto: rsa - Generate fixed-length output") Reported-by: Stephan Mueller <smueller@chronox.de> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 98eca72 commit 27710b8

1 file changed

Lines changed: 22 additions & 16 deletions

File tree

‎crypto/rsa-pkcs1pad.c‎

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -456,49 +456,55 @@ static int pkcs1pad_verify_complete(struct akcipher_request *req, int err)
456456
struct akcipher_instance *inst = akcipher_alg_instance(tfm);
457457
struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
458458
const struct rsa_asn1_template *digest_info = ictx->digest_info;
459+
unsigned int dst_len;
459460
unsigned int pos;
460-
461-
if (err == -EOVERFLOW)
462-
/* Decrypted value had no leading 0 byte */
463-
err = -EINVAL;
461+
u8 *out_buf;
464462

465463
if (err)
466464
goto done;
467465

468-
if (req_ctx->child_req.dst_len != ctx->key_size - 1) {
469-
err = -EINVAL;
466+
err = -EINVAL;
467+
dst_len = req_ctx->child_req.dst_len;
468+
if (dst_len < ctx->key_size - 1)
470469
goto done;
470+
471+
out_buf = req_ctx->out_buf;
472+
if (dst_len == ctx->key_size) {
473+
if (out_buf[0] != 0x00)
474+
/* Decrypted value had no leading 0 byte */
475+
goto done;
476+
477+
dst_len--;
478+
out_buf++;
471479
}
472480

473481
err = -EBADMSG;
474-
if (req_ctx->out_buf[0] != 0x01)
482+
if (out_buf[0] != 0x01)
475483
goto done;
476484

477-
for (pos = 1; pos < req_ctx->child_req.dst_len; pos++)
478-
if (req_ctx->out_buf[pos] != 0xff)
485+
for (pos = 1; pos < dst_len; pos++)
486+
if (out_buf[pos] != 0xff)
479487
break;
480488

481-
if (pos < 9 || pos == req_ctx->child_req.dst_len ||
482-
req_ctx->out_buf[pos] != 0x00)
489+
if (pos < 9 || pos == dst_len || out_buf[pos] != 0x00)
483490
goto done;
484491
pos++;
485492

486-
if (memcmp(req_ctx->out_buf + pos, digest_info->data,
487-
digest_info->size))
493+
if (memcmp(out_buf + pos, digest_info->data, digest_info->size))
488494
goto done;
489495

490496
pos += digest_info->size;
491497

492498
err = 0;
493499

494-
if (req->dst_len < req_ctx->child_req.dst_len - pos)
500+
if (req->dst_len < dst_len - pos)
495501
err = -EOVERFLOW;
496-
req->dst_len = req_ctx->child_req.dst_len - pos;
502+
req->dst_len = dst_len - pos;
497503

498504
if (!err)
499505
sg_copy_from_buffer(req->dst,
500506
sg_nents_for_len(req->dst, req->dst_len),
501-
req_ctx->out_buf + pos, req->dst_len);
507+
out_buf + pos, req->dst_len);
502508
done:
503509
kzfree(req_ctx->out_buf);
504510

0 commit comments

Comments
 (0)