From 10828180037a9defde04e4bc93ae368481654af8 Mon Sep 17 00:00:00 2001 From: Neil Armstrong Date: Fri, 22 Apr 2022 15:02:27 +0200 Subject: [PATCH 1/5] Implement PK Opaque RSA decrypt Signed-off-by: Neil Armstrong --- library/pk_wrap.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/library/pk_wrap.c b/library/pk_wrap.c index 852c46ef6..4303d1e3d 100644 --- a/library/pk_wrap.c +++ b/library/pk_wrap.c @@ -1602,6 +1602,32 @@ const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = { NULL, /* debug - could be done later, or even left NULL */ }; +#if defined(MBEDTLS_RSA_C) +static int pk_opaque_rsa_decrypt( void *ctx, + const unsigned char *input, size_t ilen, + unsigned char *output, size_t *olen, size_t osize, + int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) +{ + const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx; + psa_status_t status; + + /* PSA has its own RNG */ + (void) f_rng; + (void) p_rng; + + status = psa_asymmetric_decrypt( *key, PSA_ALG_RSA_PKCS1V15_CRYPT, + input, ilen, + NULL, 0, + output, osize, olen ); + if( status != PSA_SUCCESS ) + { + return( mbedtls_pk_error_from_psa_rsa( status ) ); + } + + return 0; +} +#endif + const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = { MBEDTLS_PK_OPAQUE, "Opaque", @@ -1613,7 +1639,11 @@ const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = { NULL, /* restartable verify - not relevant */ NULL, /* restartable sign - not relevant */ #endif - NULL, /* decrypt - will be done later */ +#if defined(MBEDTLS_RSA_C) + pk_opaque_rsa_decrypt, +#else + NULL, /* decrypt */ +#endif /* MBEDTLS_RSA_C */ NULL, /* encrypt - will be done later */ NULL, /* check_pair - could be done later or left NULL */ pk_opaque_alloc_wrap, From b32ae72e273fbea0363a7b129f8664de8baf19a0 Mon Sep 17 00:00:00 2001 From: Neil Armstrong Date: Fri, 22 Apr 2022 15:02:47 +0200 Subject: [PATCH 2/5] Add PK Opaque RSA decrypt tests Signed-off-by: Neil Armstrong --- tests/suites/test_suite_pk.data | 8 +++ tests/suites/test_suite_pk.function | 77 +++++++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 4 deletions(-) diff --git a/tests/suites/test_suite_pk.data b/tests/suites/test_suite_pk.data index a0844ea2e..f17711d49 100644 --- a/tests/suites/test_suite_pk.data +++ b/tests/suites/test_suite_pk.data @@ -153,6 +153,14 @@ RSA decrypt test vector #2 depends_on:MBEDTLS_PKCS1_V15 pk_rsa_decrypt_test_vec:"a42eda41e56235e666e7faaa77100197f657288a1bf183e4820f0c37ce2c456b960278d6003e0bbcd4be4a969f8e8fd9231e1f492414f00ed09844994c86ec32db7cde3bec7f0c3dbf6ae55baeb2712fa609f5fc3207a824eb3dace31849cd6a6084318523912bccb84cf42e3c6d6d1685131d69bb545acec827d2b0dfdd5568b7dcc4f5a11d6916583fefa689d367f8c9e1d95dcd2240895a9470b0c1730f97cd6e8546860bd254801769f54be96e16362ddcbf34d56035028890199e0f48db38642cb66a4181e028a6443a404feb284ce02b4614b683367d40874e505611d23142d49f06feea831d52d347b13610b413c4efc43a6de9f0b08d2a951dc503b6":2048:16:"e79a373182bfaa722eb035f772ad2a9464bd842de59432c18bbab3a7dfeae318c9b915ee487861ab665a40bd6cda560152578e8579016c929df99fea05b4d64efca1d543850bc8164b40d71ed7f3fa4105df0fb9b9ad2a18ce182c8a4f4f975bea9aa0b9a1438a27a28e97ac8330ef37383414d1bd64607d6979ac050424fd17":16:"c6749cbb0db8c5a177672d4728a8b22392b2fc4d3b8361d5c0d5055a1b4e46d821f757c24eef2a51c561941b93b3ace7340074c058c9bb48e7e7414f42c41da4cccb5c2ba91deb30c586b7fb18af12a52995592ad139d3be429add6547e044becedaf31fa3b39421e24ee034fbf367d11f6b8f88ee483d163b431e1654ad3e89":16:"b38ac65c8141f7f5c96e14470e851936a67bf94cc6821a39ac12c05f7c0b06d9e6ddba2224703b02e25f31452f9c4a8417b62675fdc6df46b94813bc7b9769a892c482b830bfe0ad42e46668ace68903617faf6681f4babf1cc8e4b0420d3c7f61dc45434c6b54e2c3ee0fc07908509d79c9826e673bf8363255adb0add2401039a7bcd1b4ecf0fbe6ec8369d2da486eec59559dd1d54c9b24190965eafbdab203b35255765261cd0909acf93c3b8b8428cbb448de4715d1b813d0c94829c229543d391ce0adab5351f97a3810c1f73d7b1458b97daed4209c50e16d064d2d5bfda8c23893d755222793146d0a78c3d64f35549141486c3b0961a7b4c1a2034f":16:"3":"4E636AF98E40F3ADCFCCB698F4E80B9F":MBEDTLS_ERR_RSA_INVALID_PADDING +RSA Opaque decrypt test vector #1 +depends_on:MBEDTLS_PKCS1_V15 +pk_wrap_rsa_decrypt_test_vec:"a42eda41e56235e666e7faaa77100197f657288a1bf183e4820f0c37ce2c456b960278d6003e0bbcd4be4a969f8e8fd9231e1f492414f00ed09844994c86ec32db7cde3bec7f0c3dbf6ae55baeb2712fa609f5fc3207a824eb3dace31849cd6a6084318523912bccb84cf42e3c6d6d1685131d69bb545acec827d2b0dfdd5568b7dcc4f5a11d6916583fefa689d367f8c9e1d95dcd2240895a9470b0c1730f97cd6e8546860bd254801769f54be96e16362ddcbf34d56035028890199e0f48db38642cb66a4181e028a6443a404fea284ce02b4614b683367d40874e505611d23142d49f06feea831d52d347b13610b413c4efc43a6de9f0b08d2a951dc503b6":2048:16:"e79a373182bfaa722eb035f772ad2a9464bd842de59432c18bbab3a7dfeae318c9b915ee487861ab665a40bd6cda560152578e8579016c929df99fea05b4d64efca1d543850bc8164b40d71ed7f3fa4105df0fb9b9ad2a18ce182c8a4f4f975bea9aa0b9a1438a27a28e97ac8330ef37383414d1bd64607d6979ac050424fd17":16:"c6749cbb0db8c5a177672d4728a8b22392b2fc4d3b8361d5c0d5055a1b4e46d821f757c24eef2a51c561941b93b3ace7340074c058c9bb48e7e7414f42c41da4cccb5c2ba91deb30c586b7fb18af12a52995592ad139d3be429add6547e044becedaf31fa3b39421e24ee034fbf367d11f6b8f88ee483d163b431e1654ad3e89":16:"b38ac65c8141f7f5c96e14470e851936a67bf94cc6821a39ac12c05f7c0b06d9e6ddba2224703b02e25f31452f9c4a8417b62675fdc6df46b94813bc7b9769a892c482b830bfe0ad42e46668ace68903617faf6681f4babf1cc8e4b0420d3c7f61dc45434c6b54e2c3ee0fc07908509d79c9826e673bf8363255adb0add2401039a7bcd1b4ecf0fbe6ec8369d2da486eec59559dd1d54c9b24190965eafbdab203b35255765261cd0909acf93c3b8b8428cbb448de4715d1b813d0c94829c229543d391ce0adab5351f97a3810c1f73d7b1458b97daed4209c50e16d064d2d5bfda8c23893d755222793146d0a78c3d64f35549141486c3b0961a7b4c1a2034f":16:"3":"4E636AF98E40F3ADCFCCB698F4E80B9F":0 + +RSA Opaque decrypt test vector #2 +depends_on:MBEDTLS_PKCS1_V15 +pk_wrap_rsa_decrypt_test_vec:"a42eda41e56235e666e7faaa77100197f657288a1bf183e4820f0c37ce2c456b960278d6003e0bbcd4be4a969f8e8fd9231e1f492414f00ed09844994c86ec32db7cde3bec7f0c3dbf6ae55baeb2712fa609f5fc3207a824eb3dace31849cd6a6084318523912bccb84cf42e3c6d6d1685131d69bb545acec827d2b0dfdd5568b7dcc4f5a11d6916583fefa689d367f8c9e1d95dcd2240895a9470b0c1730f97cd6e8546860bd254801769f54be96e16362ddcbf34d56035028890199e0f48db38642cb66a4181e028a6443a404feb284ce02b4614b683367d40874e505611d23142d49f06feea831d52d347b13610b413c4efc43a6de9f0b08d2a951dc503b6":2048:16:"e79a373182bfaa722eb035f772ad2a9464bd842de59432c18bbab3a7dfeae318c9b915ee487861ab665a40bd6cda560152578e8579016c929df99fea05b4d64efca1d543850bc8164b40d71ed7f3fa4105df0fb9b9ad2a18ce182c8a4f4f975bea9aa0b9a1438a27a28e97ac8330ef37383414d1bd64607d6979ac050424fd17":16:"c6749cbb0db8c5a177672d4728a8b22392b2fc4d3b8361d5c0d5055a1b4e46d821f757c24eef2a51c561941b93b3ace7340074c058c9bb48e7e7414f42c41da4cccb5c2ba91deb30c586b7fb18af12a52995592ad139d3be429add6547e044becedaf31fa3b39421e24ee034fbf367d11f6b8f88ee483d163b431e1654ad3e89":16:"b38ac65c8141f7f5c96e14470e851936a67bf94cc6821a39ac12c05f7c0b06d9e6ddba2224703b02e25f31452f9c4a8417b62675fdc6df46b94813bc7b9769a892c482b830bfe0ad42e46668ace68903617faf6681f4babf1cc8e4b0420d3c7f61dc45434c6b54e2c3ee0fc07908509d79c9826e673bf8363255adb0add2401039a7bcd1b4ecf0fbe6ec8369d2da486eec59559dd1d54c9b24190965eafbdab203b35255765261cd0909acf93c3b8b8428cbb448de4715d1b813d0c94829c229543d391ce0adab5351f97a3810c1f73d7b1458b97daed4209c50e16d064d2d5bfda8c23893d755222793146d0a78c3d64f35549141486c3b0961a7b4c1a2034f":16:"3":"4E636AF98E40F3ADCFCCB698F4E80B9F":MBEDTLS_ERR_RSA_INVALID_PADDING + EC nocrypt depends_on:MBEDTLS_ECP_C pk_ec_nocrypt:MBEDTLS_PK_ECKEY diff --git a/tests/suites/test_suite_pk.function b/tests/suites/test_suite_pk.function index 32c264406..a73b034fa 100644 --- a/tests/suites/test_suite_pk.function +++ b/tests/suites/test_suite_pk.function @@ -204,10 +204,13 @@ void pk_psa_utils( int key_is_rsa ) TEST_ASSERT( mbedtls_pk_verify( &pk, md_alg, b1, sizeof( b1), b2, sizeof( b2 ) ) == MBEDTLS_ERR_PK_TYPE_MISMATCH ); - TEST_ASSERT( mbedtls_pk_decrypt( &pk, b1, sizeof( b1 ), - b2, &len, sizeof( b2 ), - NULL, NULL ) - == MBEDTLS_ERR_PK_TYPE_MISMATCH ); + if( key_is_rsa == 0 ) + { + TEST_ASSERT( mbedtls_pk_decrypt( &pk, b1, sizeof( b1 ), + b2, &len, sizeof( b2 ), + NULL, NULL ) + == MBEDTLS_ERR_PK_TYPE_MISMATCH ); + } TEST_ASSERT( mbedtls_pk_encrypt( &pk, b1, sizeof( b1 ), b2, &len, sizeof( b2 ), NULL, NULL ) @@ -916,6 +919,72 @@ exit: } /* END_CASE */ +/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_USE_PSA_CRYPTO */ +void pk_wrap_rsa_decrypt_test_vec( data_t * cipher, int mod, int radix_P, + char * input_P, int radix_Q, char * input_Q, + int radix_N, char * input_N, int radix_E, + char * input_E, data_t * clear, int ret ) +{ + unsigned char output[256]; + mbedtls_test_rnd_pseudo_info rnd_info; + mbedtls_mpi N, P, Q, E; + mbedtls_rsa_context *rsa; + mbedtls_pk_context pk; + mbedtls_svc_key_id_t key_id; + size_t olen; + + USE_PSA_INIT( ); + + mbedtls_pk_init( &pk ); + mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); + mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E ); + + memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) ); + + /* init pk-rsa context */ + TEST_EQUAL( mbedtls_pk_setup( &pk, + mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ), 0 ); + rsa = mbedtls_pk_rsa( pk ); + + /* load public key */ + TEST_EQUAL( mbedtls_test_read_mpi( &N, radix_N, input_N ), 0 ); + TEST_EQUAL( mbedtls_test_read_mpi( &E, radix_E, input_E ), 0 ); + + /* load private key */ + TEST_EQUAL( mbedtls_test_read_mpi( &P, radix_P, input_P ), 0 ); + TEST_EQUAL( mbedtls_test_read_mpi( &Q, radix_Q, input_Q ), 0 ); + TEST_EQUAL( mbedtls_rsa_import( rsa, &N, &P, &Q, NULL, &E ), 0 ); + TEST_EQUAL( mbedtls_rsa_get_len( rsa ), (size_t) ( mod / 8 ) ); + TEST_EQUAL( mbedtls_rsa_complete( rsa ), 0 ); + + /* Turn PK context into an opaque one. */ + TEST_EQUAL( mbedtls_pk_wrap_as_opaque( &pk, &key_id, + PSA_ALG_RSA_PKCS1V15_CRYPT, + PSA_KEY_USAGE_DECRYPT, + PSA_ALG_NONE ), 0 ); + + /* decryption test */ + memset( output, 0, sizeof( output ) ); + olen = 0; + TEST_EQUAL( mbedtls_pk_decrypt( &pk, cipher->x, cipher->len, + output, &olen, sizeof( output ), + mbedtls_test_rnd_pseudo_rand, &rnd_info ), ret ); + if( ret == 0 ) + { + TEST_EQUAL( olen, clear->len ); + TEST_EQUAL( memcmp( output, clear->x, olen ), 0 ); + } + + TEST_ASSERT( PSA_SUCCESS == psa_destroy_key( key_id ) ); + +exit: + mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); + mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E ); + mbedtls_pk_free( &pk ); + USE_PSA_DONE( ); +} +/* END_CASE */ + /* BEGIN_CASE */ void pk_ec_nocrypt( int type ) { From 6c26adc9004ec14bae288c1fe958de8f3488b325 Mon Sep 17 00:00:00 2001 From: Neil Armstrong Date: Mon, 2 May 2022 14:43:04 +0200 Subject: [PATCH 3/5] Do not make pk_opaque_rsa_decrypt() depend on MBEDTLS_RSA_C Signed-off-by: Neil Armstrong --- library/pk_wrap.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/library/pk_wrap.c b/library/pk_wrap.c index 4303d1e3d..36bb8c9f4 100644 --- a/library/pk_wrap.c +++ b/library/pk_wrap.c @@ -1602,7 +1602,6 @@ const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = { NULL, /* debug - could be done later, or even left NULL */ }; -#if defined(MBEDTLS_RSA_C) static int pk_opaque_rsa_decrypt( void *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, size_t osize, @@ -1626,7 +1625,6 @@ static int pk_opaque_rsa_decrypt( void *ctx, return 0; } -#endif const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = { MBEDTLS_PK_OPAQUE, @@ -1639,11 +1637,7 @@ const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = { NULL, /* restartable verify - not relevant */ NULL, /* restartable sign - not relevant */ #endif -#if defined(MBEDTLS_RSA_C) pk_opaque_rsa_decrypt, -#else - NULL, /* decrypt */ -#endif /* MBEDTLS_RSA_C */ NULL, /* encrypt - will be done later */ NULL, /* check_pair - could be done later or left NULL */ pk_opaque_alloc_wrap, From 30beca35f1bc67b9f1fb73473f03bf2a4859b97b Mon Sep 17 00:00:00 2001 From: Neil Armstrong Date: Tue, 3 May 2022 15:42:13 +0200 Subject: [PATCH 4/5] Guard pk_opaque_rsa_decrypt() with PSA_WANT_KEY_TYPE_RSA_KEY_PAIR Then mbedtls_pk_error_from_psa_rsa() also needs to be guarded with PSA_WANT_KEY_TYPE_RSA_KEY_PAIR to be used by pk_opaque_rsa_decrypt() Signed-off-by: Neil Armstrong --- library/pk_wrap.c | 11 +++++++++-- library/pk_wrap.h | 5 +++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/library/pk_wrap.c b/library/pk_wrap.c index 36bb8c9f4..e234a8de3 100644 --- a/library/pk_wrap.c +++ b/library/pk_wrap.c @@ -100,7 +100,8 @@ int mbedtls_pk_error_from_psa( psa_status_t status ) } } -#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) +#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \ + defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) int mbedtls_pk_error_from_psa_rsa( psa_status_t status ) { switch( status ) @@ -121,7 +122,7 @@ int mbedtls_pk_error_from_psa_rsa( psa_status_t status ) return( mbedtls_pk_error_from_psa( status ) ); } } -#endif +#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */ #endif /* MBEDTLS_PSA_CRYPTO_C */ @@ -1602,6 +1603,7 @@ const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = { NULL, /* debug - could be done later, or even left NULL */ }; +#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) static int pk_opaque_rsa_decrypt( void *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen, size_t osize, @@ -1625,6 +1627,7 @@ static int pk_opaque_rsa_decrypt( void *ctx, return 0; } +#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */ const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = { MBEDTLS_PK_OPAQUE, @@ -1637,7 +1640,11 @@ const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = { NULL, /* restartable verify - not relevant */ NULL, /* restartable sign - not relevant */ #endif +#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) pk_opaque_rsa_decrypt, +#else + NULL, /* decrypt - not available */ +#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */ NULL, /* encrypt - will be done later */ NULL, /* check_pair - could be done later or left NULL */ pk_opaque_alloc_wrap, diff --git a/library/pk_wrap.h b/library/pk_wrap.h index 5e81a652a..fc756db65 100644 --- a/library/pk_wrap.h +++ b/library/pk_wrap.h @@ -148,9 +148,10 @@ int mbedtls_pk_error_from_psa_ecdsa( psa_status_t status ); #if defined(MBEDTLS_PSA_CRYPTO_C) int mbedtls_pk_error_from_psa( psa_status_t status ); -#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) +#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \ + defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) int mbedtls_pk_error_from_psa_rsa( psa_status_t status ); -#endif +#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */ #if defined(MBEDTLS_RSA_C) int mbedtls_pk_psa_rsa_sign_ext( psa_algorithm_t psa_alg_md, From 814562afaaf07cac67090358752099dabe52527b Mon Sep 17 00:00:00 2001 From: Neil Armstrong Date: Wed, 11 May 2022 09:29:57 +0200 Subject: [PATCH 5/5] Switch last TEST_ASSERT() in TEST_EQUAL() in pk_wrap_rsa_decrypt_test_vec() Signed-off-by: Neil Armstrong --- tests/suites/test_suite_pk.function | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/suites/test_suite_pk.function b/tests/suites/test_suite_pk.function index a73b034fa..2d69d1b77 100644 --- a/tests/suites/test_suite_pk.function +++ b/tests/suites/test_suite_pk.function @@ -975,7 +975,7 @@ void pk_wrap_rsa_decrypt_test_vec( data_t * cipher, int mod, int radix_P, TEST_EQUAL( memcmp( output, clear->x, olen ), 0 ); } - TEST_ASSERT( PSA_SUCCESS == psa_destroy_key( key_id ) ); + TEST_EQUAL( PSA_SUCCESS, psa_destroy_key( key_id ) ); exit: mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );