Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include "mbedtls/rsa.h" |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 3 | #include "mbedtls/rsa_internal.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 4 | #include "mbedtls/md2.h" |
| 5 | #include "mbedtls/md4.h" |
| 6 | #include "mbedtls/md5.h" |
| 7 | #include "mbedtls/sha1.h" |
| 8 | #include "mbedtls/sha256.h" |
| 9 | #include "mbedtls/sha512.h" |
| 10 | #include "mbedtls/entropy.h" |
| 11 | #include "mbedtls/ctr_drbg.h" |
Hanno Becker | 47deec4 | 2017-07-24 12:27:09 +0100 | [diff] [blame] | 12 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 13 | /* END_HEADER */ |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 14 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 15 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 16 | * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 17 | * END_DEPENDENCIES |
| 18 | */ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 19 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 20 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 21 | void mbedtls_rsa_pkcs1_sign( char *message_hex_string, int padding_mode, int digest, |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 22 | int mod, int radix_P, char *input_P, int radix_Q, |
| 23 | char *input_Q, int radix_N, char *input_N, int radix_E, |
| 24 | char *input_E, char *result_hex_str, int result ) |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 25 | { |
| 26 | unsigned char message_str[1000]; |
| 27 | unsigned char hash_result[1000]; |
| 28 | unsigned char output[1000]; |
| 29 | unsigned char output_str[1000]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | mbedtls_rsa_context ctx; |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 31 | mbedtls_mpi N, P, Q, E; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 32 | int msg_len; |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 33 | rnd_pseudo_info rnd_info; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 34 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 35 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); |
| 36 | mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 38 | |
| 39 | memset( message_str, 0x00, 1000 ); |
| 40 | memset( hash_result, 0x00, 1000 ); |
| 41 | memset( output, 0x00, 1000 ); |
| 42 | memset( output_str, 0x00, 1000 ); |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 43 | memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 44 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 45 | TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 ); |
| 46 | TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 ); |
| 47 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 48 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 49 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 50 | TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 ); |
| 51 | TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); |
Hanno Becker | 7f25f85 | 2017-10-10 16:56:22 +0100 | [diff] [blame] | 52 | TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 54 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 55 | msg_len = unhexify( message_str, message_hex_string ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 56 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 57 | if( mbedtls_md_info_from_type( digest ) != NULL ) |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 58 | TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), |
| 59 | message_str, msg_len, hash_result ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 60 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 61 | TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info, |
| 62 | MBEDTLS_RSA_PRIVATE, digest, 0, |
| 63 | hash_result, output ) == result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 64 | if( result == 0 ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 65 | { |
| 66 | hexify( output_str, output, ctx.len ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 67 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 68 | TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 69 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 70 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 71 | exit: |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 72 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); |
| 73 | mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 74 | mbedtls_rsa_free( &ctx ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 75 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 76 | /* END_CASE */ |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 77 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 78 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 79 | void mbedtls_rsa_pkcs1_verify( char *message_hex_string, int padding_mode, int digest, |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 80 | int mod, int radix_N, char *input_N, int radix_E, |
| 81 | char *input_E, char *result_hex_str, int result ) |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 82 | { |
| 83 | unsigned char message_str[1000]; |
| 84 | unsigned char hash_result[1000]; |
| 85 | unsigned char result_str[1000]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 86 | mbedtls_rsa_context ctx; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 87 | int msg_len; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 88 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 89 | mbedtls_mpi N, E; |
| 90 | |
| 91 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 92 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 93 | memset( message_str, 0x00, 1000 ); |
| 94 | memset( hash_result, 0x00, 1000 ); |
| 95 | memset( result_str, 0x00, 1000 ); |
| 96 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 97 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 98 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
| 99 | TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 ); |
| 100 | TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 101 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 102 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 103 | msg_len = unhexify( message_str, message_hex_string ); |
| 104 | unhexify( result_str, result_hex_str ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 105 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 106 | if( mbedtls_md_info_from_type( digest ) != NULL ) |
| 107 | TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 108 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 109 | TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, digest, 0, hash_result, result_str ) == result ); |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 110 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 111 | exit: |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 112 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 113 | mbedtls_rsa_free( &ctx ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 114 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 115 | /* END_CASE */ |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 116 | |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 117 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 118 | /* BEGIN_CASE */ |
| 119 | void rsa_pkcs1_sign_raw( char *message_hex_string, char *hash_result_string, |
| 120 | int padding_mode, int mod, int radix_P, char *input_P, |
| 121 | int radix_Q, char *input_Q, int radix_N, |
| 122 | char *input_N, int radix_E, char *input_E, |
| 123 | char *result_hex_str ) |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 124 | { |
| 125 | unsigned char message_str[1000]; |
| 126 | unsigned char hash_result[1000]; |
| 127 | unsigned char output[1000]; |
| 128 | unsigned char output_str[1000]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 129 | mbedtls_rsa_context ctx; |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 130 | mbedtls_mpi N, P, Q, E; |
Paul Bakker | eaf90d9 | 2011-07-13 14:21:52 +0000 | [diff] [blame] | 131 | int hash_len; |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 132 | rnd_pseudo_info rnd_info; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 133 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 134 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 135 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); |
| 136 | mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 137 | |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 138 | memset( message_str, 0x00, 1000 ); |
| 139 | memset( hash_result, 0x00, 1000 ); |
| 140 | memset( output, 0x00, 1000 ); |
| 141 | memset( output_str, 0x00, 1000 ); |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 142 | memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 143 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 144 | TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 ); |
| 145 | TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 ); |
| 146 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 147 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 148 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 149 | TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 ); |
| 150 | TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); |
Hanno Becker | 7f25f85 | 2017-10-10 16:56:22 +0100 | [diff] [blame] | 151 | TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 152 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 153 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 154 | unhexify( message_str, message_hex_string ); |
| 155 | hash_len = unhexify( hash_result, hash_result_string ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 156 | |
Hanno Becker | 8fd5548 | 2017-08-23 14:07:48 +0100 | [diff] [blame] | 157 | TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info, |
| 158 | MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_NONE, |
| 159 | hash_len, hash_result, output ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 160 | |
| 161 | hexify( output_str, output, ctx.len ); |
| 162 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 163 | TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 164 | |
Manuel Pégourié-Gonnard | 43be6cd | 2017-06-20 09:53:42 +0200 | [diff] [blame] | 165 | #if defined(MBEDTLS_PKCS1_V15) |
Manuel Pégourié-Gonnard | fbf0915 | 2014-02-03 11:58:55 +0100 | [diff] [blame] | 166 | /* For PKCS#1 v1.5, there is an alternative way to generate signatures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | if( padding_mode == MBEDTLS_RSA_PKCS_V15 ) |
Manuel Pégourié-Gonnard | fbf0915 | 2014-02-03 11:58:55 +0100 | [diff] [blame] | 168 | { |
Manuel Pégourié-Gonnard | 1ba8a3f | 2018-03-13 13:27:14 +0100 | [diff] [blame^] | 169 | int res; |
Manuel Pégourié-Gonnard | fbf0915 | 2014-02-03 11:58:55 +0100 | [diff] [blame] | 170 | memset( output, 0x00, 1000 ); |
| 171 | memset( output_str, 0x00, 1000 ); |
| 172 | |
Hanno Becker | f8b56d4 | 2017-10-05 10:16:37 +0100 | [diff] [blame] | 173 | res = mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 174 | &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, |
Hanno Becker | f8b56d4 | 2017-10-05 10:16:37 +0100 | [diff] [blame] | 175 | hash_len, hash_result, output ); |
Manuel Pégourié-Gonnard | fbf0915 | 2014-02-03 11:58:55 +0100 | [diff] [blame] | 176 | |
Hanno Becker | f8b56d4 | 2017-10-05 10:16:37 +0100 | [diff] [blame] | 177 | #if !defined(MBEDTLS_RSA_ALT) |
| 178 | TEST_ASSERT( res == 0 ); |
| 179 | #else |
| 180 | TEST_ASSERT( ( res == 0 ) || |
| 181 | ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) ); |
| 182 | #endif |
Manuel Pégourié-Gonnard | fbf0915 | 2014-02-03 11:58:55 +0100 | [diff] [blame] | 183 | |
Hanno Becker | f8b56d4 | 2017-10-05 10:16:37 +0100 | [diff] [blame] | 184 | if( res == 0 ) |
| 185 | { |
| 186 | hexify( output_str, output, ctx.len ); |
| 187 | TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 ); |
| 188 | } |
Manuel Pégourié-Gonnard | fbf0915 | 2014-02-03 11:58:55 +0100 | [diff] [blame] | 189 | } |
Manuel Pégourié-Gonnard | 43be6cd | 2017-06-20 09:53:42 +0200 | [diff] [blame] | 190 | #endif /* MBEDTLS_PKCS1_V15 */ |
Manuel Pégourié-Gonnard | fbf0915 | 2014-02-03 11:58:55 +0100 | [diff] [blame] | 191 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 192 | exit: |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 193 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); |
| 194 | mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E ); |
| 195 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 196 | mbedtls_rsa_free( &ctx ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 197 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 198 | /* END_CASE */ |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 199 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 200 | /* BEGIN_CASE */ |
| 201 | void rsa_pkcs1_verify_raw( char *message_hex_string, char *hash_result_string, |
| 202 | int padding_mode, int mod, int radix_N, |
| 203 | char *input_N, int radix_E, char *input_E, |
| 204 | char *result_hex_str, int correct ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 205 | { |
| 206 | unsigned char message_str[1000]; |
| 207 | unsigned char hash_result[1000]; |
| 208 | unsigned char result_str[1000]; |
Manuel Pégourié-Gonnard | fbf0915 | 2014-02-03 11:58:55 +0100 | [diff] [blame] | 209 | unsigned char output[1000]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 210 | mbedtls_rsa_context ctx; |
Manuel Pégourié-Gonnard | 43be6cd | 2017-06-20 09:53:42 +0200 | [diff] [blame] | 211 | size_t hash_len; |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 212 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 213 | mbedtls_mpi N, E; |
| 214 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E ); |
| 215 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 216 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 217 | memset( message_str, 0x00, 1000 ); |
| 218 | memset( hash_result, 0x00, 1000 ); |
| 219 | memset( result_str, 0x00, 1000 ); |
Manuel Pégourié-Gonnard | fbf0915 | 2014-02-03 11:58:55 +0100 | [diff] [blame] | 220 | memset( output, 0x00, sizeof( output ) ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 221 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 222 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 223 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 224 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 225 | TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 ); |
| 226 | TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 228 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 229 | unhexify( message_str, message_hex_string ); |
| 230 | hash_len = unhexify( hash_result, hash_result_string ); |
| 231 | unhexify( result_str, result_hex_str ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 232 | |
Hanno Becker | 8fd5548 | 2017-08-23 14:07:48 +0100 | [diff] [blame] | 233 | TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, |
| 234 | MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_NONE, |
| 235 | hash_len, hash_result, |
| 236 | result_str ) == correct ); |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 237 | |
Manuel Pégourié-Gonnard | 43be6cd | 2017-06-20 09:53:42 +0200 | [diff] [blame] | 238 | #if defined(MBEDTLS_PKCS1_V15) |
Manuel Pégourié-Gonnard | fbf0915 | 2014-02-03 11:58:55 +0100 | [diff] [blame] | 239 | /* For PKCS#1 v1.5, there is an alternative way to verify signatures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 240 | if( padding_mode == MBEDTLS_RSA_PKCS_V15 ) |
Manuel Pégourié-Gonnard | fbf0915 | 2014-02-03 11:58:55 +0100 | [diff] [blame] | 241 | { |
Manuel Pégourié-Gonnard | 1ba8a3f | 2018-03-13 13:27:14 +0100 | [diff] [blame^] | 242 | int res; |
Manuel Pégourié-Gonnard | fbf0915 | 2014-02-03 11:58:55 +0100 | [diff] [blame] | 243 | int ok; |
Manuel Pégourié-Gonnard | 43be6cd | 2017-06-20 09:53:42 +0200 | [diff] [blame] | 244 | size_t olen; |
Manuel Pégourié-Gonnard | fbf0915 | 2014-02-03 11:58:55 +0100 | [diff] [blame] | 245 | |
Hanno Becker | f8b56d4 | 2017-10-05 10:16:37 +0100 | [diff] [blame] | 246 | res = mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 247 | NULL, NULL, MBEDTLS_RSA_PUBLIC, |
Hanno Becker | f8b56d4 | 2017-10-05 10:16:37 +0100 | [diff] [blame] | 248 | &olen, result_str, output, sizeof( output ) ); |
Manuel Pégourié-Gonnard | fbf0915 | 2014-02-03 11:58:55 +0100 | [diff] [blame] | 249 | |
Hanno Becker | f8b56d4 | 2017-10-05 10:16:37 +0100 | [diff] [blame] | 250 | #if !defined(MBEDTLS_RSA_ALT) |
| 251 | TEST_ASSERT( res == 0 ); |
| 252 | #else |
| 253 | TEST_ASSERT( ( res == 0 ) || |
| 254 | ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) ); |
| 255 | #endif |
| 256 | |
| 257 | if( res == 0 ) |
| 258 | { |
| 259 | ok = olen == hash_len && memcmp( output, hash_result, olen ) == 0; |
| 260 | if( correct == 0 ) |
| 261 | TEST_ASSERT( ok == 1 ); |
| 262 | else |
| 263 | TEST_ASSERT( ok == 0 ); |
| 264 | } |
Manuel Pégourié-Gonnard | fbf0915 | 2014-02-03 11:58:55 +0100 | [diff] [blame] | 265 | } |
Manuel Pégourié-Gonnard | 43be6cd | 2017-06-20 09:53:42 +0200 | [diff] [blame] | 266 | #endif /* MBEDTLS_PKCS1_V15 */ |
Manuel Pégourié-Gonnard | fbf0915 | 2014-02-03 11:58:55 +0100 | [diff] [blame] | 267 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 268 | exit: |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 269 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | mbedtls_rsa_free( &ctx ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 271 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 272 | /* END_CASE */ |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 273 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 274 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 275 | void mbedtls_rsa_pkcs1_encrypt( char *message_hex_string, int padding_mode, int mod, |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 276 | int radix_N, char *input_N, int radix_E, char *input_E, |
| 277 | char *result_hex_str, int result ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 278 | { |
| 279 | unsigned char message_str[1000]; |
| 280 | unsigned char output[1000]; |
| 281 | unsigned char output_str[1000]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 282 | mbedtls_rsa_context ctx; |
Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 283 | size_t msg_len; |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 284 | rnd_pseudo_info rnd_info; |
| 285 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 286 | mbedtls_mpi N, E; |
| 287 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E ); |
| 288 | |
Paul Bakker | 997bbd1 | 2011-03-13 15:45:42 +0000 | [diff] [blame] | 289 | memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 290 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 291 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 292 | memset( message_str, 0x00, 1000 ); |
| 293 | memset( output, 0x00, 1000 ); |
| 294 | memset( output_str, 0x00, 1000 ); |
| 295 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 296 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 297 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 298 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 299 | TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 ); |
| 300 | TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 301 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 302 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 303 | msg_len = unhexify( message_str, message_hex_string ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 304 | |
Hanno Becker | f8b56d4 | 2017-10-05 10:16:37 +0100 | [diff] [blame] | 305 | TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info, |
| 306 | MBEDTLS_RSA_PUBLIC, msg_len, |
| 307 | message_str, output ) == result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 308 | if( result == 0 ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 309 | { |
| 310 | hexify( output_str, output, ctx.len ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 311 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 312 | TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 313 | } |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 314 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 315 | exit: |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 316 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 317 | mbedtls_rsa_free( &ctx ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 318 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 319 | /* END_CASE */ |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 320 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 321 | /* BEGIN_CASE */ |
| 322 | void rsa_pkcs1_encrypt_bad_rng( char *message_hex_string, int padding_mode, |
| 323 | int mod, int radix_N, char *input_N, |
| 324 | int radix_E, char *input_E, |
| 325 | char *result_hex_str, int result ) |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 326 | { |
| 327 | unsigned char message_str[1000]; |
| 328 | unsigned char output[1000]; |
| 329 | unsigned char output_str[1000]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 330 | mbedtls_rsa_context ctx; |
Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 331 | size_t msg_len; |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 332 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 333 | mbedtls_mpi N, E; |
| 334 | |
| 335 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 336 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 337 | memset( message_str, 0x00, 1000 ); |
| 338 | memset( output, 0x00, 1000 ); |
| 339 | memset( output_str, 0x00, 1000 ); |
| 340 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 341 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 342 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 343 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 344 | TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 ); |
| 345 | TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 346 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 347 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 348 | msg_len = unhexify( message_str, message_hex_string ); |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 349 | |
Hanno Becker | f8b56d4 | 2017-10-05 10:16:37 +0100 | [diff] [blame] | 350 | TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL, |
| 351 | MBEDTLS_RSA_PUBLIC, msg_len, |
| 352 | message_str, output ) == result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 353 | if( result == 0 ) |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 354 | { |
| 355 | hexify( output_str, output, ctx.len ); |
| 356 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 357 | TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 ); |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 358 | } |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 359 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 360 | exit: |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 361 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 362 | mbedtls_rsa_free( &ctx ); |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 363 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 364 | /* END_CASE */ |
Paul Bakker | a665685 | 2010-07-18 19:47:14 +0000 | [diff] [blame] | 365 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 366 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 367 | void mbedtls_rsa_pkcs1_decrypt( char *message_hex_string, int padding_mode, int mod, |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 368 | int radix_P, char *input_P, int radix_Q, char *input_Q, |
| 369 | int radix_N, char *input_N, int radix_E, char *input_E, |
| 370 | int max_output, char *result_hex_str, int result ) |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 371 | { |
| 372 | unsigned char message_str[1000]; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 373 | unsigned char output[1000]; |
| 374 | unsigned char output_str[1000]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 375 | mbedtls_rsa_context ctx; |
Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 376 | size_t output_len; |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 377 | rnd_pseudo_info rnd_info; |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 378 | mbedtls_mpi N, P, Q, E; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 379 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 380 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); |
| 381 | mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E ); |
| 382 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 383 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 384 | |
| 385 | memset( message_str, 0x00, 1000 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 386 | memset( output, 0x00, 1000 ); |
| 387 | memset( output_str, 0x00, 1000 ); |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 388 | memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 389 | |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 390 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 391 | TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 ); |
| 392 | TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 ); |
| 393 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 394 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 395 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 396 | TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 ); |
| 397 | TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); |
Hanno Becker | 7f25f85 | 2017-10-10 16:56:22 +0100 | [diff] [blame] | 398 | TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 399 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 400 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 401 | unhexify( message_str, message_hex_string ); |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 402 | output_len = 0; |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 403 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 404 | TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, &output_len, message_str, output, max_output ) == result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 405 | if( result == 0 ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 406 | { |
| 407 | hexify( output_str, output, ctx.len ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 408 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 409 | TEST_ASSERT( strncasecmp( (char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 410 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 411 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 412 | exit: |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 413 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); |
| 414 | mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 415 | mbedtls_rsa_free( &ctx ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 416 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 417 | /* END_CASE */ |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 418 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 419 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 420 | void mbedtls_rsa_public( char *message_hex_string, int mod, int radix_N, char *input_N, |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 421 | int radix_E, char *input_E, char *result_hex_str, int result ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 422 | { |
| 423 | unsigned char message_str[1000]; |
| 424 | unsigned char output[1000]; |
| 425 | unsigned char output_str[1000]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 426 | mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */ |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 427 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 428 | mbedtls_mpi N, E; |
| 429 | |
| 430 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 431 | mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 ); |
| 432 | mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 433 | memset( message_str, 0x00, 1000 ); |
| 434 | memset( output, 0x00, 1000 ); |
| 435 | memset( output_str, 0x00, 1000 ); |
| 436 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 437 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 438 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 439 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 440 | TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 ); |
| 441 | TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 442 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 443 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 444 | unhexify( message_str, message_hex_string ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 445 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 446 | TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str, output ) == result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 447 | if( result == 0 ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 448 | { |
| 449 | hexify( output_str, output, ctx.len ); |
| 450 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 451 | TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 452 | } |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 453 | |
Manuel Pégourié-Gonnard | c4919bc | 2014-02-03 11:16:44 +0100 | [diff] [blame] | 454 | /* And now with the copy */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 455 | TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 456 | /* clear the original to be sure */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 457 | mbedtls_rsa_free( &ctx ); |
Manuel Pégourié-Gonnard | c4919bc | 2014-02-03 11:16:44 +0100 | [diff] [blame] | 458 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 459 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 ); |
Manuel Pégourié-Gonnard | c4919bc | 2014-02-03 11:16:44 +0100 | [diff] [blame] | 460 | |
| 461 | memset( output, 0x00, 1000 ); |
| 462 | memset( output_str, 0x00, 1000 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 463 | TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str, output ) == result ); |
Manuel Pégourié-Gonnard | c4919bc | 2014-02-03 11:16:44 +0100 | [diff] [blame] | 464 | if( result == 0 ) |
| 465 | { |
| 466 | hexify( output_str, output, ctx2.len ); |
| 467 | |
| 468 | TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 ); |
| 469 | } |
| 470 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 471 | exit: |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 472 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 473 | mbedtls_rsa_free( &ctx ); |
| 474 | mbedtls_rsa_free( &ctx2 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 475 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 476 | /* END_CASE */ |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 477 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 478 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 479 | void mbedtls_rsa_private( char *message_hex_string, int mod, int radix_P, char *input_P, |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 480 | int radix_Q, char *input_Q, int radix_N, char *input_N, |
| 481 | int radix_E, char *input_E, char *result_hex_str, int result ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 482 | { |
| 483 | unsigned char message_str[1000]; |
| 484 | unsigned char output[1000]; |
| 485 | unsigned char output_str[1000]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 486 | mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */ |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 487 | mbedtls_mpi N, P, Q, E; |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 488 | rnd_pseudo_info rnd_info; |
Manuel Pégourié-Gonnard | 735b8fc | 2013-09-13 12:57:23 +0200 | [diff] [blame] | 489 | int i; |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 490 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 491 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); |
| 492 | mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 493 | mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 ); |
| 494 | mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 495 | |
| 496 | memset( message_str, 0x00, 1000 ); |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 497 | memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 498 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 499 | TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 ); |
| 500 | TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 ); |
| 501 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 502 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 503 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 504 | TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 ); |
| 505 | TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) ); |
Hanno Becker | 7f25f85 | 2017-10-10 16:56:22 +0100 | [diff] [blame] | 506 | TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 507 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 508 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 509 | unhexify( message_str, message_hex_string ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 510 | |
Manuel Pégourié-Gonnard | 735b8fc | 2013-09-13 12:57:23 +0200 | [diff] [blame] | 511 | /* repeat three times to test updating of blinding values */ |
| 512 | for( i = 0; i < 3; i++ ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 513 | { |
Manuel Pégourié-Gonnard | 735b8fc | 2013-09-13 12:57:23 +0200 | [diff] [blame] | 514 | memset( output, 0x00, 1000 ); |
| 515 | memset( output_str, 0x00, 1000 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 516 | TEST_ASSERT( mbedtls_rsa_private( &ctx, rnd_pseudo_rand, &rnd_info, |
Manuel Pégourié-Gonnard | 735b8fc | 2013-09-13 12:57:23 +0200 | [diff] [blame] | 517 | message_str, output ) == result ); |
| 518 | if( result == 0 ) |
| 519 | { |
| 520 | hexify( output_str, output, ctx.len ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 521 | |
Manuel Pégourié-Gonnard | 735b8fc | 2013-09-13 12:57:23 +0200 | [diff] [blame] | 522 | TEST_ASSERT( strcasecmp( (char *) output_str, |
| 523 | result_hex_str ) == 0 ); |
| 524 | } |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 525 | } |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 526 | |
Manuel Pégourié-Gonnard | c4919bc | 2014-02-03 11:16:44 +0100 | [diff] [blame] | 527 | /* And now one more time with the copy */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 528 | TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 529 | /* clear the original to be sure */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 530 | mbedtls_rsa_free( &ctx ); |
Manuel Pégourié-Gonnard | c4919bc | 2014-02-03 11:16:44 +0100 | [diff] [blame] | 531 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 532 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 ); |
Manuel Pégourié-Gonnard | c4919bc | 2014-02-03 11:16:44 +0100 | [diff] [blame] | 533 | |
| 534 | memset( output, 0x00, 1000 ); |
| 535 | memset( output_str, 0x00, 1000 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 536 | TEST_ASSERT( mbedtls_rsa_private( &ctx2, rnd_pseudo_rand, &rnd_info, |
Manuel Pégourié-Gonnard | c4919bc | 2014-02-03 11:16:44 +0100 | [diff] [blame] | 537 | message_str, output ) == result ); |
| 538 | if( result == 0 ) |
| 539 | { |
| 540 | hexify( output_str, output, ctx2.len ); |
| 541 | |
| 542 | TEST_ASSERT( strcasecmp( (char *) output_str, |
| 543 | result_hex_str ) == 0 ); |
| 544 | } |
| 545 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 546 | exit: |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 547 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); |
| 548 | mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E ); |
| 549 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 550 | mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 551 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 552 | /* END_CASE */ |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 553 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 554 | /* BEGIN_CASE */ |
| 555 | void rsa_check_privkey_null() |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 556 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 557 | mbedtls_rsa_context ctx; |
| 558 | memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 559 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 560 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 561 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 562 | /* END_CASE */ |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 563 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 564 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 565 | void mbedtls_rsa_check_pubkey( int radix_N, char *input_N, int radix_E, char *input_E, |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 566 | int result ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 567 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 568 | mbedtls_rsa_context ctx; |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 569 | mbedtls_mpi N, E; |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 570 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 571 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 572 | mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 573 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 574 | if( strlen( input_N ) ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 575 | { |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 576 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 577 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 578 | if( strlen( input_E ) ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 579 | { |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 580 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 583 | TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 584 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result ); |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 585 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 586 | exit: |
Hanno Becker | ceb7a9d | 2017-08-23 08:33:08 +0100 | [diff] [blame] | 587 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 588 | mbedtls_rsa_free( &ctx ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 589 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 590 | /* END_CASE */ |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 591 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 592 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 593 | void mbedtls_rsa_check_privkey( int mod, int radix_P, char *input_P, int radix_Q, |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 594 | char *input_Q, int radix_N, char *input_N, |
| 595 | int radix_E, char *input_E, int radix_D, char *input_D, |
| 596 | int radix_DP, char *input_DP, int radix_DQ, |
| 597 | char *input_DQ, int radix_QP, char *input_QP, |
| 598 | int result ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 599 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 600 | mbedtls_rsa_context ctx; |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 601 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 602 | mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 603 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 604 | ctx.len = mod / 8; |
| 605 | if( strlen( input_P ) ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 606 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 607 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 608 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 609 | if( strlen( input_Q ) ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 610 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 611 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 612 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 613 | if( strlen( input_N ) ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 614 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 615 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 616 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 617 | if( strlen( input_E ) ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 618 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 619 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 620 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 621 | if( strlen( input_D ) ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 622 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 623 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 624 | } |
Hanno Becker | 131134f | 2017-08-23 08:31:07 +0100 | [diff] [blame] | 625 | #if !defined(MBEDTLS_RSA_NO_CRT) |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 626 | if( strlen( input_DP ) ) |
Paul Bakker | 31417a7 | 2012-09-27 20:41:37 +0000 | [diff] [blame] | 627 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 628 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 ); |
Paul Bakker | 31417a7 | 2012-09-27 20:41:37 +0000 | [diff] [blame] | 629 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 630 | if( strlen( input_DQ ) ) |
Paul Bakker | 31417a7 | 2012-09-27 20:41:37 +0000 | [diff] [blame] | 631 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 632 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 ); |
Paul Bakker | 31417a7 | 2012-09-27 20:41:37 +0000 | [diff] [blame] | 633 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 634 | if( strlen( input_QP ) ) |
Paul Bakker | 31417a7 | 2012-09-27 20:41:37 +0000 | [diff] [blame] | 635 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 636 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 ); |
Paul Bakker | 31417a7 | 2012-09-27 20:41:37 +0000 | [diff] [blame] | 637 | } |
Hanno Becker | 131134f | 2017-08-23 08:31:07 +0100 | [diff] [blame] | 638 | #else |
| 639 | ((void) radix_DP); ((void) input_DP); |
| 640 | ((void) radix_DQ); ((void) input_DQ); |
| 641 | ((void) radix_QP); ((void) input_QP); |
| 642 | #endif |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 643 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 644 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result ); |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 645 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 646 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 647 | mbedtls_rsa_free( &ctx ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 648 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 649 | /* END_CASE */ |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 650 | |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 651 | /* BEGIN_CASE */ |
| 652 | void rsa_check_pubpriv( int mod, int radix_Npub, char *input_Npub, |
| 653 | int radix_Epub, char *input_Epub, |
| 654 | int radix_P, char *input_P, int radix_Q, |
| 655 | char *input_Q, int radix_N, char *input_N, |
| 656 | int radix_E, char *input_E, int radix_D, char *input_D, |
| 657 | int radix_DP, char *input_DP, int radix_DQ, |
| 658 | char *input_DQ, int radix_QP, char *input_QP, |
| 659 | int result ) |
| 660 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 661 | mbedtls_rsa_context pub, prv; |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 662 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 663 | mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 ); |
| 664 | mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 665 | |
| 666 | pub.len = mod / 8; |
| 667 | prv.len = mod / 8; |
| 668 | |
| 669 | if( strlen( input_Npub ) ) |
| 670 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 671 | TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 672 | } |
| 673 | if( strlen( input_Epub ) ) |
| 674 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 675 | TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | if( strlen( input_P ) ) |
| 679 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 680 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 681 | } |
| 682 | if( strlen( input_Q ) ) |
| 683 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 684 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 685 | } |
| 686 | if( strlen( input_N ) ) |
| 687 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 688 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 689 | } |
| 690 | if( strlen( input_E ) ) |
| 691 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 692 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 693 | } |
| 694 | if( strlen( input_D ) ) |
| 695 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 696 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 697 | } |
Hanno Becker | 131134f | 2017-08-23 08:31:07 +0100 | [diff] [blame] | 698 | #if !defined(MBEDTLS_RSA_NO_CRT) |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 699 | if( strlen( input_DP ) ) |
| 700 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 701 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 702 | } |
| 703 | if( strlen( input_DQ ) ) |
| 704 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 705 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 706 | } |
| 707 | if( strlen( input_QP ) ) |
| 708 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 709 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 710 | } |
Hanno Becker | 131134f | 2017-08-23 08:31:07 +0100 | [diff] [blame] | 711 | #else |
| 712 | ((void) radix_DP); ((void) input_DP); |
| 713 | ((void) radix_DQ); ((void) input_DQ); |
| 714 | ((void) radix_QP); ((void) input_QP); |
| 715 | #endif |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 716 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 717 | TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 718 | |
| 719 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 720 | mbedtls_rsa_free( &pub ); |
| 721 | mbedtls_rsa_free( &prv ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 722 | } |
| 723 | /* END_CASE */ |
| 724 | |
Hanno Becker | d4a872e | 2017-09-07 08:09:33 +0100 | [diff] [blame] | 725 | /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 726 | void mbedtls_rsa_gen_key( int nrbits, int exponent, int result) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 727 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 728 | mbedtls_rsa_context ctx; |
| 729 | mbedtls_entropy_context entropy; |
| 730 | mbedtls_ctr_drbg_context ctr_drbg; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 731 | const char *pers = "test_suite_rsa"; |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 732 | |
Manuel Pégourié-Gonnard | 8d128ef | 2015-04-28 22:38:08 +0200 | [diff] [blame] | 733 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 734 | mbedtls_entropy_init( &entropy ); |
Hanno Becker | 7e8e57c | 2017-07-23 10:19:29 +0100 | [diff] [blame] | 735 | mbedtls_rsa_init ( &ctx, 0, 0 ); |
Paul Bakker | c0a1a31 | 2011-12-04 17:12:15 +0000 | [diff] [blame] | 736 | |
Hanno Becker | a47023e | 2017-12-22 17:08:03 +0000 | [diff] [blame] | 737 | TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, |
| 738 | &entropy, (const unsigned char *) pers, |
| 739 | strlen( pers ) ) == 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 740 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 741 | TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 742 | if( result == 0 ) |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 743 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 744 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); |
Janos Follath | ef44178 | 2016-09-21 13:18:12 +0100 | [diff] [blame] | 745 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 746 | } |
Paul Bakker | 58ef6ec | 2013-01-03 11:33:48 +0100 | [diff] [blame] | 747 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 748 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 749 | mbedtls_rsa_free( &ctx ); |
| 750 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 751 | mbedtls_entropy_free( &entropy ); |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 752 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 753 | /* END_CASE */ |
Paul Bakker | 821fb08 | 2009-07-12 13:26:42 +0000 | [diff] [blame] | 754 | |
Hanno Becker | e78fd8d | 2017-08-23 11:00:44 +0100 | [diff] [blame] | 755 | /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */ |
Hanno Becker | 0f65e0c | 2017-10-03 14:39:16 +0100 | [diff] [blame] | 756 | void mbedtls_rsa_deduce_primes( int radix_N, char *input_N, |
Hanno Becker | e78fd8d | 2017-08-23 11:00:44 +0100 | [diff] [blame] | 757 | int radix_D, char *input_D, |
| 758 | int radix_E, char *input_E, |
| 759 | int radix_P, char *output_P, |
| 760 | int radix_Q, char *output_Q, |
| 761 | int corrupt, int result ) |
| 762 | { |
| 763 | mbedtls_mpi N, P, Pp, Q, Qp, D, E; |
| 764 | |
Hanno Becker | e78fd8d | 2017-08-23 11:00:44 +0100 | [diff] [blame] | 765 | mbedtls_mpi_init( &N ); |
| 766 | mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); |
| 767 | mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp ); |
| 768 | mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); |
| 769 | |
Hanno Becker | e78fd8d | 2017-08-23 11:00:44 +0100 | [diff] [blame] | 770 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 771 | TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 ); |
| 772 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
| 773 | TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 ); |
| 774 | TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 ); |
| 775 | |
| 776 | if( corrupt ) |
| 777 | TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 ); |
| 778 | |
| 779 | /* Try to deduce P, Q from N, D, E only. */ |
Hanno Becker | f9e184b | 2017-10-10 16:49:26 +0100 | [diff] [blame] | 780 | TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result ); |
Hanno Becker | e78fd8d | 2017-08-23 11:00:44 +0100 | [diff] [blame] | 781 | |
| 782 | if( !corrupt ) |
| 783 | { |
| 784 | /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */ |
| 785 | TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) || |
| 786 | ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) ); |
| 787 | } |
| 788 | |
| 789 | exit: |
Hanno Becker | e78fd8d | 2017-08-23 11:00:44 +0100 | [diff] [blame] | 790 | mbedtls_mpi_free( &N ); |
| 791 | mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); |
| 792 | mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp ); |
| 793 | mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); |
Hanno Becker | e78fd8d | 2017-08-23 11:00:44 +0100 | [diff] [blame] | 794 | } |
| 795 | /* END_CASE */ |
| 796 | |
Hanno Becker | 6b4ce49 | 2017-08-23 11:00:21 +0100 | [diff] [blame] | 797 | /* BEGIN_CASE */ |
Hanno Becker | 8ba6ce4 | 2017-10-03 14:36:26 +0100 | [diff] [blame] | 798 | void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P, |
| 799 | int radix_Q, char *input_Q, |
| 800 | int radix_E, char *input_E, |
| 801 | int radix_D, char *output_D, |
| 802 | int corrupt, int result ) |
Hanno Becker | 6b4ce49 | 2017-08-23 11:00:21 +0100 | [diff] [blame] | 803 | { |
| 804 | mbedtls_mpi P, Q, D, Dp, E, R, Rp; |
| 805 | |
| 806 | mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); |
| 807 | mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp ); |
| 808 | mbedtls_mpi_init( &E ); |
| 809 | mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp ); |
| 810 | |
| 811 | TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 ); |
| 812 | TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 ); |
| 813 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
| 814 | TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 ); |
| 815 | |
| 816 | if( corrupt ) |
| 817 | { |
| 818 | /* Make E even */ |
| 819 | TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 ); |
| 820 | } |
| 821 | |
| 822 | /* Try to deduce D from N, P, Q, E. */ |
Hanno Becker | 8ba6ce4 | 2017-10-03 14:36:26 +0100 | [diff] [blame] | 823 | TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q, |
| 824 | &E, &D ) == result ); |
Hanno Becker | 6b4ce49 | 2017-08-23 11:00:21 +0100 | [diff] [blame] | 825 | |
| 826 | if( !corrupt ) |
| 827 | { |
| 828 | /* |
| 829 | * Check that D and Dp agree modulo LCM(P-1, Q-1). |
| 830 | */ |
| 831 | |
| 832 | /* Replace P,Q by P-1, Q-1 */ |
| 833 | TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 ); |
| 834 | TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 ); |
| 835 | |
| 836 | /* Check D == Dp modulo P-1 */ |
| 837 | TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 ); |
| 838 | TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 ); |
| 839 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 ); |
| 840 | |
| 841 | /* Check D == Dp modulo Q-1 */ |
| 842 | TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 ); |
| 843 | TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 ); |
| 844 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 ); |
| 845 | } |
| 846 | |
| 847 | exit: |
| 848 | |
| 849 | mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); |
| 850 | mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp ); |
| 851 | mbedtls_mpi_free( &E ); |
| 852 | mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp ); |
| 853 | } |
| 854 | /* END_CASE */ |
| 855 | |
Hanno Becker | f40cdf9 | 2017-12-22 11:03:27 +0000 | [diff] [blame] | 856 | /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */ |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 857 | void mbedtls_rsa_import( int radix_N, char *input_N, |
| 858 | int radix_P, char *input_P, |
| 859 | int radix_Q, char *input_Q, |
| 860 | int radix_D, char *input_D, |
| 861 | int radix_E, char *input_E, |
| 862 | int successive, |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 863 | int is_priv, |
Hanno Becker | 04877a4 | 2017-10-11 10:01:33 +0100 | [diff] [blame] | 864 | int res_check, |
| 865 | int res_complete ) |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 866 | { |
| 867 | mbedtls_mpi N, P, Q, D, E; |
| 868 | mbedtls_rsa_context ctx; |
| 869 | |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 870 | /* Buffers used for encryption-decryption test */ |
| 871 | unsigned char *buf_orig = NULL; |
| 872 | unsigned char *buf_enc = NULL; |
| 873 | unsigned char *buf_dec = NULL; |
| 874 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 875 | mbedtls_entropy_context entropy; |
| 876 | mbedtls_ctr_drbg_context ctr_drbg; |
| 877 | const char *pers = "test_suite_rsa"; |
| 878 | |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 879 | const int have_N = ( strlen( input_N ) > 0 ); |
| 880 | const int have_P = ( strlen( input_P ) > 0 ); |
| 881 | const int have_Q = ( strlen( input_Q ) > 0 ); |
| 882 | const int have_D = ( strlen( input_D ) > 0 ); |
| 883 | const int have_E = ( strlen( input_E ) > 0 ); |
| 884 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 885 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 886 | mbedtls_entropy_init( &entropy ); |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 887 | mbedtls_rsa_init( &ctx, 0, 0 ); |
| 888 | |
| 889 | mbedtls_mpi_init( &N ); |
| 890 | mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); |
| 891 | mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); |
| 892 | |
Hanno Becker | d4d6057 | 2018-01-10 07:12:01 +0000 | [diff] [blame] | 893 | TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, |
| 894 | (const unsigned char *) pers, strlen( pers ) ) == 0 ); |
| 895 | |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 896 | if( have_N ) |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 897 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 898 | |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 899 | if( have_P ) |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 900 | TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 ); |
| 901 | |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 902 | if( have_Q ) |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 903 | TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 ); |
| 904 | |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 905 | if( have_D ) |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 906 | TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 ); |
| 907 | |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 908 | if( have_E ) |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 909 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
| 910 | |
| 911 | if( !successive ) |
| 912 | { |
| 913 | TEST_ASSERT( mbedtls_rsa_import( &ctx, |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 914 | have_N ? &N : NULL, |
| 915 | have_P ? &P : NULL, |
| 916 | have_Q ? &Q : NULL, |
| 917 | have_D ? &D : NULL, |
| 918 | have_E ? &E : NULL ) == 0 ); |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 919 | } |
| 920 | else |
| 921 | { |
| 922 | /* Import N, P, Q, D, E separately. |
| 923 | * This should make no functional difference. */ |
| 924 | |
| 925 | TEST_ASSERT( mbedtls_rsa_import( &ctx, |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 926 | have_N ? &N : NULL, |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 927 | NULL, NULL, NULL, NULL ) == 0 ); |
| 928 | |
| 929 | TEST_ASSERT( mbedtls_rsa_import( &ctx, |
| 930 | NULL, |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 931 | have_P ? &P : NULL, |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 932 | NULL, NULL, NULL ) == 0 ); |
| 933 | |
| 934 | TEST_ASSERT( mbedtls_rsa_import( &ctx, |
| 935 | NULL, NULL, |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 936 | have_Q ? &Q : NULL, |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 937 | NULL, NULL ) == 0 ); |
| 938 | |
| 939 | TEST_ASSERT( mbedtls_rsa_import( &ctx, |
| 940 | NULL, NULL, NULL, |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 941 | have_D ? &D : NULL, |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 942 | NULL ) == 0 ); |
| 943 | |
| 944 | TEST_ASSERT( mbedtls_rsa_import( &ctx, |
| 945 | NULL, NULL, NULL, NULL, |
Hanno Becker | 4d6e834 | 2017-09-29 11:50:18 +0100 | [diff] [blame] | 946 | have_E ? &E : NULL ) == 0 ); |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 947 | } |
| 948 | |
Hanno Becker | 04877a4 | 2017-10-11 10:01:33 +0100 | [diff] [blame] | 949 | TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete ); |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 950 | |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 951 | /* On expected success, perform some public and private |
| 952 | * key operations to check if the key is working properly. */ |
Hanno Becker | 04877a4 | 2017-10-11 10:01:33 +0100 | [diff] [blame] | 953 | if( res_complete == 0 ) |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 954 | { |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 955 | if( is_priv ) |
Hanno Becker | 04877a4 | 2017-10-11 10:01:33 +0100 | [diff] [blame] | 956 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check ); |
| 957 | else |
| 958 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check ); |
| 959 | |
| 960 | if( res_check != 0 ) |
| 961 | goto exit; |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 962 | |
| 963 | buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) ); |
| 964 | buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) ); |
| 965 | buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) ); |
| 966 | if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL ) |
| 967 | goto exit; |
| 968 | |
| 969 | TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg, |
| 970 | buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 ); |
| 971 | |
| 972 | /* Make sure the number we're generating is smaller than the modulus */ |
| 973 | buf_orig[0] = 0x00; |
| 974 | |
| 975 | TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 ); |
| 976 | |
| 977 | if( is_priv ) |
| 978 | { |
| 979 | TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random, |
| 980 | &ctr_drbg, buf_enc, |
| 981 | buf_dec ) == 0 ); |
| 982 | |
| 983 | TEST_ASSERT( memcmp( buf_orig, buf_dec, |
| 984 | mbedtls_rsa_get_len( &ctx ) ) == 0 ); |
| 985 | } |
| 986 | } |
| 987 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 988 | exit: |
| 989 | |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 990 | mbedtls_free( buf_orig ); |
| 991 | mbedtls_free( buf_enc ); |
| 992 | mbedtls_free( buf_dec ); |
| 993 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 994 | mbedtls_rsa_free( &ctx ); |
| 995 | |
| 996 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 997 | mbedtls_entropy_free( &entropy ); |
| 998 | |
| 999 | mbedtls_mpi_free( &N ); |
| 1000 | mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); |
| 1001 | mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); |
| 1002 | } |
| 1003 | /* END_CASE */ |
| 1004 | |
Hanno Becker | 417f2d6 | 2017-08-23 11:44:51 +0100 | [diff] [blame] | 1005 | /* BEGIN_CASE */ |
| 1006 | void mbedtls_rsa_export( int radix_N, char *input_N, |
| 1007 | int radix_P, char *input_P, |
| 1008 | int radix_Q, char *input_Q, |
| 1009 | int radix_D, char *input_D, |
| 1010 | int radix_E, char *input_E, |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1011 | int is_priv, |
Hanno Becker | 417f2d6 | 2017-08-23 11:44:51 +0100 | [diff] [blame] | 1012 | int successive ) |
| 1013 | { |
| 1014 | /* Original MPI's with which we set up the RSA context */ |
| 1015 | mbedtls_mpi N, P, Q, D, E; |
| 1016 | |
| 1017 | /* Exported MPI's */ |
| 1018 | mbedtls_mpi Ne, Pe, Qe, De, Ee; |
| 1019 | |
| 1020 | const int have_N = ( strlen( input_N ) > 0 ); |
| 1021 | const int have_P = ( strlen( input_P ) > 0 ); |
| 1022 | const int have_Q = ( strlen( input_Q ) > 0 ); |
| 1023 | const int have_D = ( strlen( input_D ) > 0 ); |
| 1024 | const int have_E = ( strlen( input_E ) > 0 ); |
| 1025 | |
Hanno Becker | 417f2d6 | 2017-08-23 11:44:51 +0100 | [diff] [blame] | 1026 | mbedtls_rsa_context ctx; |
| 1027 | |
| 1028 | mbedtls_rsa_init( &ctx, 0, 0 ); |
| 1029 | |
| 1030 | mbedtls_mpi_init( &N ); |
| 1031 | mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); |
| 1032 | mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); |
| 1033 | |
| 1034 | mbedtls_mpi_init( &Ne ); |
| 1035 | mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe ); |
| 1036 | mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee ); |
| 1037 | |
| 1038 | /* Setup RSA context */ |
| 1039 | |
| 1040 | if( have_N ) |
| 1041 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 1042 | |
| 1043 | if( have_P ) |
| 1044 | TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 ); |
| 1045 | |
| 1046 | if( have_Q ) |
| 1047 | TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 ); |
| 1048 | |
| 1049 | if( have_D ) |
| 1050 | TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 ); |
| 1051 | |
| 1052 | if( have_E ) |
| 1053 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
| 1054 | |
| 1055 | TEST_ASSERT( mbedtls_rsa_import( &ctx, |
| 1056 | strlen( input_N ) ? &N : NULL, |
| 1057 | strlen( input_P ) ? &P : NULL, |
| 1058 | strlen( input_Q ) ? &Q : NULL, |
| 1059 | strlen( input_D ) ? &D : NULL, |
| 1060 | strlen( input_E ) ? &E : NULL ) == 0 ); |
| 1061 | |
Hanno Becker | 7f25f85 | 2017-10-10 16:56:22 +0100 | [diff] [blame] | 1062 | TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 ); |
Hanno Becker | 417f2d6 | 2017-08-23 11:44:51 +0100 | [diff] [blame] | 1063 | |
| 1064 | /* |
| 1065 | * Export parameters and compare to original ones. |
| 1066 | */ |
| 1067 | |
| 1068 | /* N and E must always be present. */ |
| 1069 | if( !successive ) |
| 1070 | { |
| 1071 | TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 ); |
| 1072 | } |
| 1073 | else |
| 1074 | { |
| 1075 | TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 ); |
| 1076 | TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 ); |
| 1077 | } |
| 1078 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 ); |
| 1079 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 ); |
| 1080 | |
| 1081 | /* If we were providing enough information to setup a complete private context, |
| 1082 | * we expect to be able to export all core parameters. */ |
| 1083 | |
| 1084 | if( is_priv ) |
| 1085 | { |
| 1086 | if( !successive ) |
| 1087 | { |
| 1088 | TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe, |
| 1089 | &De, NULL ) == 0 ); |
| 1090 | } |
| 1091 | else |
| 1092 | { |
| 1093 | TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL, |
| 1094 | NULL, NULL ) == 0 ); |
| 1095 | TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe, |
| 1096 | NULL, NULL ) == 0 ); |
| 1097 | TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, |
| 1098 | &De, NULL ) == 0 ); |
| 1099 | } |
| 1100 | |
| 1101 | if( have_P ) |
| 1102 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 ); |
| 1103 | |
| 1104 | if( have_Q ) |
| 1105 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 ); |
| 1106 | |
| 1107 | if( have_D ) |
| 1108 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 ); |
| 1109 | |
| 1110 | /* While at it, perform a sanity check */ |
Hanno Becker | 750e8b4 | 2017-08-25 07:54:27 +0100 | [diff] [blame] | 1111 | TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee, |
| 1112 | NULL, NULL ) == 0 ); |
Hanno Becker | 417f2d6 | 2017-08-23 11:44:51 +0100 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | exit: |
| 1116 | |
| 1117 | mbedtls_rsa_free( &ctx ); |
| 1118 | |
| 1119 | mbedtls_mpi_free( &N ); |
| 1120 | mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); |
| 1121 | mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); |
| 1122 | |
| 1123 | mbedtls_mpi_free( &Ne ); |
| 1124 | mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe ); |
| 1125 | mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee ); |
| 1126 | } |
| 1127 | /* END_CASE */ |
| 1128 | |
Hanno Becker | f40cdf9 | 2017-12-22 11:03:27 +0000 | [diff] [blame] | 1129 | /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */ |
Hanno Becker | 750e8b4 | 2017-08-25 07:54:27 +0100 | [diff] [blame] | 1130 | void mbedtls_rsa_validate_params( int radix_N, char *input_N, |
| 1131 | int radix_P, char *input_P, |
| 1132 | int radix_Q, char *input_Q, |
| 1133 | int radix_D, char *input_D, |
| 1134 | int radix_E, char *input_E, |
| 1135 | int prng, int result ) |
Hanno Becker | ce00263 | 2017-08-23 13:22:36 +0100 | [diff] [blame] | 1136 | { |
| 1137 | /* Original MPI's with which we set up the RSA context */ |
| 1138 | mbedtls_mpi N, P, Q, D, E; |
| 1139 | |
| 1140 | const int have_N = ( strlen( input_N ) > 0 ); |
| 1141 | const int have_P = ( strlen( input_P ) > 0 ); |
| 1142 | const int have_Q = ( strlen( input_Q ) > 0 ); |
| 1143 | const int have_D = ( strlen( input_D ) > 0 ); |
| 1144 | const int have_E = ( strlen( input_E ) > 0 ); |
| 1145 | |
| 1146 | mbedtls_entropy_context entropy; |
| 1147 | mbedtls_ctr_drbg_context ctr_drbg; |
| 1148 | const char *pers = "test_suite_rsa"; |
| 1149 | |
| 1150 | mbedtls_mpi_init( &N ); |
| 1151 | mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); |
| 1152 | mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); |
| 1153 | |
| 1154 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
| 1155 | mbedtls_entropy_init( &entropy ); |
| 1156 | TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, |
| 1157 | &entropy, (const unsigned char *) pers, |
| 1158 | strlen( pers ) ) == 0 ); |
| 1159 | |
| 1160 | if( have_N ) |
| 1161 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); |
| 1162 | |
| 1163 | if( have_P ) |
| 1164 | TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 ); |
| 1165 | |
| 1166 | if( have_Q ) |
| 1167 | TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 ); |
| 1168 | |
| 1169 | if( have_D ) |
| 1170 | TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 ); |
| 1171 | |
| 1172 | if( have_E ) |
| 1173 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); |
| 1174 | |
Hanno Becker | 750e8b4 | 2017-08-25 07:54:27 +0100 | [diff] [blame] | 1175 | TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL, |
| 1176 | have_P ? &P : NULL, |
| 1177 | have_Q ? &Q : NULL, |
| 1178 | have_D ? &D : NULL, |
| 1179 | have_E ? &E : NULL, |
| 1180 | prng ? mbedtls_ctr_drbg_random : NULL, |
| 1181 | prng ? &ctr_drbg : NULL ) == result ); |
Hanno Becker | ce00263 | 2017-08-23 13:22:36 +0100 | [diff] [blame] | 1182 | exit: |
| 1183 | |
| 1184 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 1185 | mbedtls_entropy_free( &entropy ); |
| 1186 | |
| 1187 | mbedtls_mpi_free( &N ); |
| 1188 | mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); |
| 1189 | mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); |
| 1190 | } |
| 1191 | /* END_CASE */ |
| 1192 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1193 | /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */ |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1194 | void mbedtls_rsa_export_raw( char *input_N, char *input_P, |
| 1195 | char *input_Q, char *input_D, |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1196 | char *input_E, int is_priv, |
| 1197 | int successive ) |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1198 | { |
| 1199 | /* Original raw buffers with which we set up the RSA context */ |
| 1200 | unsigned char bufN[1000]; |
| 1201 | unsigned char bufP[1000]; |
| 1202 | unsigned char bufQ[1000]; |
| 1203 | unsigned char bufD[1000]; |
| 1204 | unsigned char bufE[1000]; |
| 1205 | |
| 1206 | size_t lenN = 0; |
| 1207 | size_t lenP = 0; |
| 1208 | size_t lenQ = 0; |
| 1209 | size_t lenD = 0; |
| 1210 | size_t lenE = 0; |
| 1211 | |
| 1212 | /* Exported buffers */ |
| 1213 | unsigned char bufNe[ sizeof( bufN ) ]; |
| 1214 | unsigned char bufPe[ sizeof( bufP ) ]; |
| 1215 | unsigned char bufQe[ sizeof( bufQ ) ]; |
| 1216 | unsigned char bufDe[ sizeof( bufD ) ]; |
| 1217 | unsigned char bufEe[ sizeof( bufE ) ]; |
| 1218 | |
| 1219 | const int have_N = ( strlen( input_N ) > 0 ); |
| 1220 | const int have_P = ( strlen( input_P ) > 0 ); |
| 1221 | const int have_Q = ( strlen( input_Q ) > 0 ); |
| 1222 | const int have_D = ( strlen( input_D ) > 0 ); |
| 1223 | const int have_E = ( strlen( input_E ) > 0 ); |
| 1224 | |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1225 | mbedtls_rsa_context ctx; |
| 1226 | |
| 1227 | mbedtls_rsa_init( &ctx, 0, 0 ); |
| 1228 | |
| 1229 | /* Setup RSA context */ |
| 1230 | |
| 1231 | if( have_N ) |
| 1232 | lenN = unhexify( bufN, input_N ); |
| 1233 | |
| 1234 | if( have_P ) |
| 1235 | lenP = unhexify( bufP, input_P ); |
| 1236 | |
| 1237 | if( have_Q ) |
| 1238 | lenQ = unhexify( bufQ, input_Q ); |
| 1239 | |
| 1240 | if( have_D ) |
| 1241 | lenD = unhexify( bufD, input_D ); |
| 1242 | |
| 1243 | if( have_E ) |
| 1244 | lenE = unhexify( bufE, input_E ); |
| 1245 | |
| 1246 | TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, |
| 1247 | have_N ? bufN : NULL, lenN, |
| 1248 | have_P ? bufP : NULL, lenP, |
| 1249 | have_Q ? bufQ : NULL, lenQ, |
| 1250 | have_D ? bufD : NULL, lenD, |
| 1251 | have_E ? bufE : NULL, lenE ) == 0 ); |
| 1252 | |
Hanno Becker | 7f25f85 | 2017-10-10 16:56:22 +0100 | [diff] [blame] | 1253 | TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 ); |
Hanno Becker | f1b9a2c | 2017-08-23 11:49:22 +0100 | [diff] [blame] | 1254 | |
| 1255 | /* |
| 1256 | * Export parameters and compare to original ones. |
| 1257 | */ |
| 1258 | |
| 1259 | /* N and E must always be present. */ |
| 1260 | if( !successive ) |
| 1261 | { |
| 1262 | TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, lenN, |
| 1263 | NULL, 0, NULL, 0, NULL, 0, |
| 1264 | bufEe, lenE ) == 0 ); |
| 1265 | } |
| 1266 | else |
| 1267 | { |
| 1268 | TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, lenN, |
| 1269 | NULL, 0, NULL, 0, NULL, 0, |
| 1270 | NULL, 0 ) == 0 ); |
| 1271 | TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, |
| 1272 | NULL, 0, NULL, 0, NULL, 0, |
| 1273 | bufEe, lenE ) == 0 ); |
| 1274 | } |
| 1275 | TEST_ASSERT( memcmp( bufN, bufNe, lenN ) == 0 ); |
| 1276 | TEST_ASSERT( memcmp( bufE, bufEe, lenE ) == 0 ); |
| 1277 | |
| 1278 | /* If we were providing enough information to setup a complete private context, |
| 1279 | * we expect to be able to export all core parameters. */ |
| 1280 | |
| 1281 | if( is_priv ) |
| 1282 | { |
| 1283 | if( !successive ) |
| 1284 | { |
| 1285 | TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, |
| 1286 | bufPe, lenP ? lenP : sizeof( bufPe ), |
| 1287 | bufQe, lenQ ? lenQ : sizeof( bufQe ), |
| 1288 | bufDe, lenD ? lenD : sizeof( bufDe ), |
| 1289 | NULL, 0 ) == 0 ); |
| 1290 | } |
| 1291 | else |
| 1292 | { |
| 1293 | TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, |
| 1294 | bufPe, lenP ? lenP : sizeof( bufPe ), |
| 1295 | NULL, 0, NULL, 0, |
| 1296 | NULL, 0 ) == 0 ); |
| 1297 | |
| 1298 | TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, |
| 1299 | bufQe, lenQ ? lenQ : sizeof( bufQe ), |
| 1300 | NULL, 0, NULL, 0 ) == 0 ); |
| 1301 | |
| 1302 | TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, |
| 1303 | NULL, 0, bufDe, lenD ? lenD : sizeof( bufDe ), |
| 1304 | NULL, 0 ) == 0 ); |
| 1305 | } |
| 1306 | |
| 1307 | if( have_P ) |
| 1308 | TEST_ASSERT( memcmp( bufP, bufPe, lenP ) == 0 ); |
| 1309 | |
| 1310 | if( have_Q ) |
| 1311 | TEST_ASSERT( memcmp( bufQ, bufQe, lenQ ) == 0 ); |
| 1312 | |
| 1313 | if( have_D ) |
| 1314 | TEST_ASSERT( memcmp( bufD, bufDe, lenD ) == 0 ); |
| 1315 | |
| 1316 | } |
| 1317 | |
| 1318 | exit: |
| 1319 | mbedtls_rsa_free( &ctx ); |
| 1320 | } |
| 1321 | /* END_CASE */ |
| 1322 | |
Hanno Becker | f40cdf9 | 2017-12-22 11:03:27 +0000 | [diff] [blame] | 1323 | /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */ |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1324 | void mbedtls_rsa_import_raw( char *input_N, |
| 1325 | char *input_P, char *input_Q, |
| 1326 | char *input_D, char *input_E, |
| 1327 | int successive, |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1328 | int is_priv, |
Hanno Becker | 04877a4 | 2017-10-11 10:01:33 +0100 | [diff] [blame] | 1329 | int res_check, |
| 1330 | int res_complete ) |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1331 | { |
| 1332 | unsigned char bufN[1000]; |
| 1333 | unsigned char bufP[1000]; |
| 1334 | unsigned char bufQ[1000]; |
| 1335 | unsigned char bufD[1000]; |
| 1336 | unsigned char bufE[1000]; |
| 1337 | |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1338 | /* Buffers used for encryption-decryption test */ |
| 1339 | unsigned char *buf_orig = NULL; |
| 1340 | unsigned char *buf_enc = NULL; |
| 1341 | unsigned char *buf_dec = NULL; |
| 1342 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1343 | size_t lenN = 0; |
| 1344 | size_t lenP = 0; |
| 1345 | size_t lenQ = 0; |
| 1346 | size_t lenD = 0; |
| 1347 | size_t lenE = 0; |
| 1348 | |
| 1349 | mbedtls_rsa_context ctx; |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1350 | mbedtls_entropy_context entropy; |
| 1351 | mbedtls_ctr_drbg_context ctr_drbg; |
Hanno Becker | 3f3ae85 | 2017-10-02 10:08:39 +0100 | [diff] [blame] | 1352 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1353 | const char *pers = "test_suite_rsa"; |
| 1354 | |
| 1355 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1356 | mbedtls_entropy_init( &entropy ); |
Hanno Becker | 3f3ae85 | 2017-10-02 10:08:39 +0100 | [diff] [blame] | 1357 | mbedtls_rsa_init( &ctx, 0, 0 ); |
| 1358 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1359 | TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, |
| 1360 | &entropy, (const unsigned char *) pers, |
| 1361 | strlen( pers ) ) == 0 ); |
| 1362 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1363 | if( strlen( input_N ) ) |
| 1364 | lenN = unhexify( bufN, input_N ); |
| 1365 | |
| 1366 | if( strlen( input_P ) ) |
| 1367 | lenP = unhexify( bufP, input_P ); |
| 1368 | |
| 1369 | if( strlen( input_Q ) ) |
| 1370 | lenQ = unhexify( bufQ, input_Q ); |
| 1371 | |
| 1372 | if( strlen( input_D ) ) |
| 1373 | lenD = unhexify( bufD, input_D ); |
| 1374 | |
| 1375 | if( strlen( input_E ) ) |
| 1376 | lenE = unhexify( bufE, input_E ); |
| 1377 | |
| 1378 | if( !successive ) |
| 1379 | { |
| 1380 | TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, |
| 1381 | ( lenN > 0 ) ? bufN : NULL, lenN, |
| 1382 | ( lenP > 0 ) ? bufP : NULL, lenP, |
| 1383 | ( lenQ > 0 ) ? bufQ : NULL, lenQ, |
| 1384 | ( lenD > 0 ) ? bufD : NULL, lenD, |
| 1385 | ( lenE > 0 ) ? bufE : NULL, lenE ) == 0 ); |
| 1386 | } |
| 1387 | else |
| 1388 | { |
| 1389 | /* Import N, P, Q, D, E separately. |
| 1390 | * This should make no functional difference. */ |
| 1391 | |
| 1392 | TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, |
| 1393 | ( lenN > 0 ) ? bufN : NULL, lenN, |
| 1394 | NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 ); |
| 1395 | |
| 1396 | TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, |
| 1397 | NULL, 0, |
| 1398 | ( lenP > 0 ) ? bufP : NULL, lenP, |
| 1399 | NULL, 0, NULL, 0, NULL, 0 ) == 0 ); |
| 1400 | |
| 1401 | TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, |
| 1402 | NULL, 0, NULL, 0, |
| 1403 | ( lenQ > 0 ) ? bufQ : NULL, lenQ, |
| 1404 | NULL, 0, NULL, 0 ) == 0 ); |
| 1405 | |
| 1406 | TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, |
| 1407 | NULL, 0, NULL, 0, NULL, 0, |
| 1408 | ( lenD > 0 ) ? bufD : NULL, lenD, |
| 1409 | NULL, 0 ) == 0 ); |
| 1410 | |
| 1411 | TEST_ASSERT( mbedtls_rsa_import_raw( &ctx, |
| 1412 | NULL, 0, NULL, 0, NULL, 0, NULL, 0, |
| 1413 | ( lenE > 0 ) ? bufE : NULL, lenE ) == 0 ); |
| 1414 | } |
| 1415 | |
Hanno Becker | 04877a4 | 2017-10-11 10:01:33 +0100 | [diff] [blame] | 1416 | TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete ); |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1417 | |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1418 | /* On expected success, perform some public and private |
| 1419 | * key operations to check if the key is working properly. */ |
Hanno Becker | 04877a4 | 2017-10-11 10:01:33 +0100 | [diff] [blame] | 1420 | if( res_complete == 0 ) |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1421 | { |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1422 | if( is_priv ) |
Hanno Becker | 04877a4 | 2017-10-11 10:01:33 +0100 | [diff] [blame] | 1423 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check ); |
| 1424 | else |
| 1425 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check ); |
| 1426 | |
| 1427 | if( res_check != 0 ) |
| 1428 | goto exit; |
Hanno Becker | e1582a8 | 2017-09-29 11:51:05 +0100 | [diff] [blame] | 1429 | |
| 1430 | buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) ); |
| 1431 | buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) ); |
| 1432 | buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) ); |
| 1433 | if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL ) |
| 1434 | goto exit; |
| 1435 | |
| 1436 | TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg, |
| 1437 | buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 ); |
| 1438 | |
| 1439 | /* Make sure the number we're generating is smaller than the modulus */ |
| 1440 | buf_orig[0] = 0x00; |
| 1441 | |
| 1442 | TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 ); |
| 1443 | |
| 1444 | if( is_priv ) |
| 1445 | { |
| 1446 | TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random, |
| 1447 | &ctr_drbg, buf_enc, |
| 1448 | buf_dec ) == 0 ); |
| 1449 | |
| 1450 | TEST_ASSERT( memcmp( buf_orig, buf_dec, |
| 1451 | mbedtls_rsa_get_len( &ctx ) ) == 0 ); |
| 1452 | } |
| 1453 | } |
| 1454 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1455 | exit: |
| 1456 | |
Hanno Becker | 3f3ae85 | 2017-10-02 10:08:39 +0100 | [diff] [blame] | 1457 | mbedtls_free( buf_orig ); |
| 1458 | mbedtls_free( buf_enc ); |
| 1459 | mbedtls_free( buf_dec ); |
| 1460 | |
Hanno Becker | c77ab89 | 2017-08-23 11:01:06 +0100 | [diff] [blame] | 1461 | mbedtls_rsa_free( &ctx ); |
| 1462 | |
| 1463 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 1464 | mbedtls_entropy_free( &entropy ); |
| 1465 | |
| 1466 | } |
| 1467 | /* END_CASE */ |
| 1468 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1469 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1470 | void rsa_selftest() |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 1471 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 1472 | TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 ); |
Paul Bakker | 42a29bf | 2009-07-07 20:18:41 +0000 | [diff] [blame] | 1473 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1474 | /* END_CASE */ |