Skip to content

Commit c0611a5

Browse files
Merge pull request #352 from jack-fortanix/jack/parse-rsa-crt-params
Parse RSA parameters DP, DQ and QP from PKCS1 private keys
2 parents 819799c + 2e9eef4 commit c0611a5

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

library/pkparse.c

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -769,14 +769,40 @@ 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 )
774-
goto cleanup;
772+
#if !defined(MBEDTLS_RSA_NO_CRT)
773+
/*
774+
* The RSA CRT parameters DP, DQ and QP are nominally redundant, in
775+
* that they can be easily recomputed from D, P and Q. However by
776+
* parsing them from the PKCS1 structure it is possible to avoid
777+
* recalculating them which both reduces the overhead of loading
778+
* RSA private keys into memory and also avoids side channels which
779+
* can arise when computing those values, since all of D, P, and Q
780+
* are secret. See https://eprint.iacr.org/2020/055 for a
781+
* description of one such attack.
782+
*/
783+
784+
/* Import DP */
785+
if( ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->DP ) ) != 0)
786+
goto cleanup;
787+
788+
/* Import DQ */
789+
if( ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0)
790+
goto cleanup;
791+
792+
/* Import QP */
793+
if( ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->QP ) ) != 0)
794+
goto cleanup;
775795

776-
/* Check optional parameters */
796+
#else
797+
/* Verify existance of the CRT params */
777798
if( ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
778799
( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
779800
( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 )
801+
goto cleanup;
802+
#endif
803+
804+
/* Complete the RSA private key */
805+
if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 )
780806
goto cleanup;
781807

782808
if( p != end )

library/rsa.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
250250
{
251251
int ret = 0;
252252
int have_N, have_P, have_Q, have_D, have_E;
253+
#if !defined(MBEDTLS_RSA_NO_CRT)
254+
int have_DP, have_DQ, have_QP;
255+
#endif
253256
int n_missing, pq_missing, d_missing, is_pub, is_priv;
254257

255258
RSA_VALIDATE_RET( ctx != NULL );
@@ -260,6 +263,12 @@ int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
260263
have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 );
261264
have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 );
262265

266+
#if !defined(MBEDTLS_RSA_NO_CRT)
267+
have_DP = ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) != 0 );
268+
have_DQ = ( mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) != 0 );
269+
have_QP = ( mbedtls_mpi_cmp_int( &ctx->QP, 0 ) != 0 );
270+
#endif
271+
263272
/*
264273
* Check whether provided parameters are enough
265274
* to deduce all others. The following incomplete
@@ -325,7 +334,7 @@ int mbedtls_rsa_complete( mbedtls_rsa_context *ctx )
325334
*/
326335

327336
#if !defined(MBEDTLS_RSA_NO_CRT)
328-
if( is_priv )
337+
if( is_priv && ! ( have_DP && have_DQ && have_QP ) )
329338
{
330339
ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D,
331340
&ctx->DP, &ctx->DQ, &ctx->QP );

0 commit comments

Comments
 (0)