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