Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog.d/rsa-private-perf.txt
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.
133 changes: 101 additions & 32 deletions library/rsa.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
* (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));

cleanup:
return ret;
}
#endif

/* Generate random A and B such that A^-1 = B mod N */
static int rsa_gen_rand_with_inverse(const mbedtls_rsa_context *ctx,
mbedtls_mpi *A,
mbedtls_mpi *B,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng)
{
#if defined(MBEDTLS_RSA_NO_CRT)
int ret;
mbedtls_mpi G;

mbedtls_mpi_init(&G);

MBEDTLS_MPI_CHK(mbedtls_mpi_random(A, 1, &ctx->N, f_rng, p_rng));
MBEDTLS_MPI_CHK(mbedtls_mpi_gcd_modinv_odd(&G, B, A, &ctx->N));

if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
/* This happens if we're unlucky enough to draw a multiple of P or Q,
* or if one of them is not a prime and G is one of its factors. */
ret = MBEDTLS_ERR_RSA_RNG_FAILED;
goto cleanup;
}

cleanup:
mbedtls_mpi_free(&G);

return ret;
#else
int ret;
mbedtls_mpi Ap, Aq, Bp, Bq, G;

mbedtls_mpi_init(&Ap); mbedtls_mpi_init(&Aq);
mbedtls_mpi_init(&Bp); mbedtls_mpi_init(&Bq);
mbedtls_mpi_init(&G);

/* Generate Ap in [1, P) and compute Bp = Ap^-1 mod P */
MBEDTLS_MPI_CHK(mbedtls_mpi_random(&Ap, 1, &ctx->P, f_rng, p_rng));
MBEDTLS_MPI_CHK(mbedtls_mpi_gcd_modinv_odd(&G, &Bp, &Ap, &ctx->P));
if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
/* This can only happen if P was not a prime. */
ret = MBEDTLS_ERR_RSA_RNG_FAILED;
goto cleanup;
}

/* Generate Ap in [1, Q) and compute Bq = Aq^-1 mod P */
MBEDTLS_MPI_CHK(mbedtls_mpi_random(&Aq, 1, &ctx->Q, f_rng, p_rng));
MBEDTLS_MPI_CHK(mbedtls_mpi_gcd_modinv_odd(&G, &Bq, &Aq, &ctx->Q));
if (mbedtls_mpi_cmp_int(&G, 1) != 0) {
/* This can only happen if Q was not a prime. */
ret = MBEDTLS_ERR_RSA_RNG_FAILED;
goto cleanup;
}

/* Reconstruct A and B */
MBEDTLS_MPI_CHK(rsa_apply_crt(A, &Ap, &Aq, ctx));
MBEDTLS_MPI_CHK(rsa_apply_crt(B, &Bp, &Bq, ctx));

cleanup:
mbedtls_mpi_free(&Ap); mbedtls_mpi_free(&Aq);
mbedtls_mpi_free(&Bp); mbedtls_mpi_free(&Bq);
mbedtls_mpi_free(&G);

return ret;
#endif
}

/*
* Generate or update blinding values, see section 10 of:
* KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Expand All @@ -1277,41 +1374,25 @@ int mbedtls_rsa_public(mbedtls_rsa_context *ctx,
static int rsa_prepare_blinding(mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
{
int ret, count = 0;
mbedtls_mpi R;

mbedtls_mpi_init(&R);
int ret;

if (ctx->Vf.p != NULL) {
/* We already have blinding values, just update them by squaring */
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vi, &ctx->Vi, &ctx->Vi));
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vi, &ctx->Vi, &ctx->N));
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&ctx->Vf, &ctx->Vf, &ctx->Vf));
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&ctx->Vf, &ctx->Vf, &ctx->N));

goto cleanup;
}

/* Unblinding value: Vf = random number, invertible mod N */
mbedtls_mpi_lset(&R, 0);
do {
if (count++ > 10) {
ret = MBEDTLS_ERR_RSA_RNG_FAILED;
goto cleanup;
}

MBEDTLS_MPI_CHK(mbedtls_mpi_random(&ctx->Vf, 1, &ctx->N, f_rng, p_rng));
MBEDTLS_MPI_CHK(mbedtls_mpi_gcd_modinv_odd(&R, &ctx->Vi, &ctx->Vf, &ctx->N));
} while (mbedtls_mpi_cmp_int(&R, 1) != 0);
MBEDTLS_MPI_CHK(rsa_gen_rand_with_inverse(ctx, &ctx->Vf, &ctx->Vi, f_rng, p_rng));

/* Blinding value: Vi = Vf^(-e) mod N
* (Vi already contains Vf^-1 at this point) */
MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&ctx->Vi, &ctx->Vi, &ctx->E, &ctx->N, &ctx->RN));


cleanup:
mbedtls_mpi_free(&R);

return ret;
}

Expand Down Expand Up @@ -1511,19 +1592,7 @@ int mbedtls_rsa_private(mbedtls_rsa_context *ctx,

MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TP, &T, &DP_blind, &ctx->P, &ctx->RP));
MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&TQ, &T, &DQ_blind, &ctx->Q, &ctx->RQ));

/*
* 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(rsa_apply_crt(&T, &TP, &TQ, ctx));
#endif /* MBEDTLS_RSA_NO_CRT */

/* Verify the result to prevent glitching attacks. */
Expand Down
2 changes: 1 addition & 1 deletion library/rsa_alt_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ int mbedtls_rsa_deduce_private_exponent(mbedtls_mpi const *P,
int ret = 0;
mbedtls_mpi K, L;

if (D == NULL || mbedtls_mpi_cmp_int(D, 0) != 0) {
if (D == NULL) {
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
}

Expand Down