Skip to content

Commit 80cc811

Browse files
committed
Parse RSA parameters DP, DQ and QP from PKCS1 private keys
Otherwise these values are recomputed in mbedtls_rsa_deduce_crt, which currently suffers from side channel issues in the computation of QP (see https://eprint.iacr.org/2020/055). By loading the pre-computed values not only is the side channel avoided, but runtime overhead of loading RSA keys is reduced. Discussion in #347
1 parent d27a884 commit 80cc811

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

library/pkparse.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -769,14 +769,29 @@ static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
769769
goto cleanup;
770770
p += len;
771771

772-
/* Complete the RSA private key */
773-
if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 )
772+
/* Import DP */
773+
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
774+
MBEDTLS_ASN1_INTEGER ) ) != 0 ||
775+
( ret = mbedtls_mpi_read_binary( &rsa->DP, p, len ) ) != 0 )
774776
goto cleanup;
777+
p += len;
775778

776-
/* Check optional parameters */
777-
if( ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
778-
( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
779-
( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 )
779+
/* Import DQ */
780+
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
781+
MBEDTLS_ASN1_INTEGER ) ) != 0 ||
782+
( ret = mbedtls_mpi_read_binary( &rsa->DQ, p, len ) ) != 0 )
783+
goto cleanup;
784+
p += len;
785+
786+
/* Import QP */
787+
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
788+
MBEDTLS_ASN1_INTEGER ) ) != 0 ||
789+
( ret = mbedtls_mpi_read_binary( &rsa->QP, p, len ) ) != 0 )
790+
goto cleanup;
791+
p += len;
792+
793+
/* Complete the RSA private key */
794+
if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 )
780795
goto cleanup;
781796

782797
if( p != end )

library/rsa.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv,
249249
int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
250250
{
251251
int ret = 0;
252-
int have_N, have_P, have_Q, have_D, have_E;
252+
int have_N, have_P, have_Q, have_D, have_E, have_DP, have_DQ, have_QP;
253253
int n_missing, pq_missing, d_missing, is_pub, is_priv;
254254

255255
RSA_VALIDATE_RET( ctx != NULL );
@@ -259,6 +259,10 @@ int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
259259
have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 );
260260
have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
261261
have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
262+
have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
263+
have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
264+
have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
265+
262266

263267
/*
264268
* Check whether provided parameters are enough
@@ -325,7 +329,7 @@ int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
325329
*/
326330

327331
#if !defined(MBEDTLS_RSA_NO_CRT)
328-
if( is_priv )
332+
if( is_priv && !(have_DP && have_DQ && have_QP))
329333
{
330334
ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
331335
&ctx->DP, &ctx->DQ, &ctx->QP );

0 commit comments

Comments
 (0)