-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[3.6] Rsa: use the CRT to generate base blinding values #10513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
3be31bf
b13033d
8b0ee34
fbd7388
30c2fa0
f90c04d
83e3b37
ec5bc19
d251d73
f6f837a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Bugfix | ||
| * Partially fix a performance regression in RSA operations introduced by a | ||
| security fix in 3.6.5, by improving the performance of RSA private key | ||
| operations when MBEDTLS_RSA_NO_CRT is disabled, which is the default. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1101,7 +1101,6 @@ int mbedtls_rsa_gen_key(mbedtls_rsa_context *ctx, | |||||||||||||||||||||||||||||||||
| * if it exists (FIPS 186-4 §B.3.1 criterion 2(a)) */ | ||||||||||||||||||||||||||||||||||
| ret = mbedtls_rsa_deduce_private_exponent(&ctx->P, &ctx->Q, &ctx->E, &ctx->D); | ||||||||||||||||||||||||||||||||||
| if (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) { | ||||||||||||||||||||||||||||||||||
| mbedtls_mpi_lset(&ctx->D, 0); /* needed for the next call */ | ||||||||||||||||||||||||||||||||||
| continue; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| if (ret != 0) { | ||||||||||||||||||||||||||||||||||
|
|
@@ -1268,6 +1267,104 @@ int mbedtls_rsa_public(mbedtls_rsa_context *ctx, | |||||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| #if !defined(MBEDTLS_RSA_NO_CRT) | ||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||
| * Compute T such that T = TP mod P and T = TP mod Q. | ||||||||||||||||||||||||||||||||||
davidhorstmann-arm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||
| * (This is the Chinese Remainder Theorem - CRT.) | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
| * WARNING: uses TP as a temporary, so its value is lost! | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| static int rsa_apply_crt(mbedtls_mpi *T, | ||||||||||||||||||||||||||||||||||
| mbedtls_mpi *TP, | ||||||||||||||||||||||||||||||||||
| const mbedtls_mpi *TQ, | ||||||||||||||||||||||||||||||||||
| const mbedtls_rsa_context *ctx) | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| int ret; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||
| * T = (TP - TQ) * (Q^-1 mod P) mod P | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(T, TP, TQ)); | ||||||||||||||||||||||||||||||||||
| MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(TP, T, &ctx->QP)); | ||||||||||||||||||||||||||||||||||
| MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(T, TP, &ctx->P)); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||
| * T = TQ + T * Q | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(TP, T, &ctx->Q)); | ||||||||||||||||||||||||||||||||||
| MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(T, TQ, TP)); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
| MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(TP, T, &ctx->QP)); | |
| MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(T, TP, &ctx->P)); | |
| /* | |
| * T = TQ + T * Q | |
| */ | |
| MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(TP, T, &ctx->Q)); | |
| MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(T, TQ, TP)); | |
| MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(T, T, &ctx->QP)); | |
| MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(T, T, &ctx->P)); | |
| /* | |
| * T = TQ + T * Q | |
| */ | |
| MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(T, T, &ctx->Q)); | |
| MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(T, T, TQ)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. In addition to the readability issue inside the function, having to warn that it modifies one of its input parameters was not great, even for a static function .
Uh oh!
There was an error while loading. Please reload this page.