mbed TLS Build
tests/suites/test_suite_rsa.function@0:cdf462088d13, 2017-01-05 (annotated)
- Committer:
- markrad
- Date:
- Thu Jan 05 00:18:44 2017 +0000
- Revision:
- 0:cdf462088d13
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
markrad | 0:cdf462088d13 | 1 | /* BEGIN_HEADER */ |
markrad | 0:cdf462088d13 | 2 | #include "mbedtls/rsa.h" |
markrad | 0:cdf462088d13 | 3 | #include "mbedtls/md2.h" |
markrad | 0:cdf462088d13 | 4 | #include "mbedtls/md4.h" |
markrad | 0:cdf462088d13 | 5 | #include "mbedtls/md5.h" |
markrad | 0:cdf462088d13 | 6 | #include "mbedtls/sha1.h" |
markrad | 0:cdf462088d13 | 7 | #include "mbedtls/sha256.h" |
markrad | 0:cdf462088d13 | 8 | #include "mbedtls/sha512.h" |
markrad | 0:cdf462088d13 | 9 | #include "mbedtls/entropy.h" |
markrad | 0:cdf462088d13 | 10 | #include "mbedtls/ctr_drbg.h" |
markrad | 0:cdf462088d13 | 11 | /* END_HEADER */ |
markrad | 0:cdf462088d13 | 12 | |
markrad | 0:cdf462088d13 | 13 | /* BEGIN_DEPENDENCIES |
markrad | 0:cdf462088d13 | 14 | * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME |
markrad | 0:cdf462088d13 | 15 | * END_DEPENDENCIES |
markrad | 0:cdf462088d13 | 16 | */ |
markrad | 0:cdf462088d13 | 17 | |
markrad | 0:cdf462088d13 | 18 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 19 | void mbedtls_rsa_pkcs1_sign( char *message_hex_string, int padding_mode, int digest, |
markrad | 0:cdf462088d13 | 20 | int mod, int radix_P, char *input_P, int radix_Q, |
markrad | 0:cdf462088d13 | 21 | char *input_Q, int radix_N, char *input_N, int radix_E, |
markrad | 0:cdf462088d13 | 22 | char *input_E, char *result_hex_str, int result ) |
markrad | 0:cdf462088d13 | 23 | { |
markrad | 0:cdf462088d13 | 24 | unsigned char message_str[1000]; |
markrad | 0:cdf462088d13 | 25 | unsigned char hash_result[1000]; |
markrad | 0:cdf462088d13 | 26 | unsigned char output[1000]; |
markrad | 0:cdf462088d13 | 27 | unsigned char output_str[1000]; |
markrad | 0:cdf462088d13 | 28 | mbedtls_rsa_context ctx; |
markrad | 0:cdf462088d13 | 29 | mbedtls_mpi P1, Q1, H, G; |
markrad | 0:cdf462088d13 | 30 | int msg_len; |
markrad | 0:cdf462088d13 | 31 | rnd_pseudo_info rnd_info; |
markrad | 0:cdf462088d13 | 32 | |
markrad | 0:cdf462088d13 | 33 | mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G ); |
markrad | 0:cdf462088d13 | 34 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
markrad | 0:cdf462088d13 | 35 | |
markrad | 0:cdf462088d13 | 36 | memset( message_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 37 | memset( hash_result, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 38 | memset( output, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 39 | memset( output_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 40 | memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) ); |
markrad | 0:cdf462088d13 | 41 | |
markrad | 0:cdf462088d13 | 42 | ctx.len = mod / 8; |
markrad | 0:cdf462088d13 | 43 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 ); |
markrad | 0:cdf462088d13 | 44 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 ); |
markrad | 0:cdf462088d13 | 45 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 ); |
markrad | 0:cdf462088d13 | 46 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 ); |
markrad | 0:cdf462088d13 | 47 | |
markrad | 0:cdf462088d13 | 48 | TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 ); |
markrad | 0:cdf462088d13 | 49 | TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 ); |
markrad | 0:cdf462088d13 | 50 | TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 ); |
markrad | 0:cdf462088d13 | 51 | TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H ) == 0 ); |
markrad | 0:cdf462088d13 | 52 | TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 ); |
markrad | 0:cdf462088d13 | 53 | TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 ); |
markrad | 0:cdf462088d13 | 54 | TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 ); |
markrad | 0:cdf462088d13 | 55 | TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 ); |
markrad | 0:cdf462088d13 | 56 | |
markrad | 0:cdf462088d13 | 57 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); |
markrad | 0:cdf462088d13 | 58 | |
markrad | 0:cdf462088d13 | 59 | msg_len = unhexify( message_str, message_hex_string ); |
markrad | 0:cdf462088d13 | 60 | |
markrad | 0:cdf462088d13 | 61 | if( mbedtls_md_info_from_type( digest ) != NULL ) |
markrad | 0:cdf462088d13 | 62 | TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 ); |
markrad | 0:cdf462088d13 | 63 | |
markrad | 0:cdf462088d13 | 64 | TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, digest, 0, hash_result, output ) == result ); |
markrad | 0:cdf462088d13 | 65 | if( result == 0 ) |
markrad | 0:cdf462088d13 | 66 | { |
markrad | 0:cdf462088d13 | 67 | hexify( output_str, output, ctx.len ); |
markrad | 0:cdf462088d13 | 68 | |
markrad | 0:cdf462088d13 | 69 | TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 ); |
markrad | 0:cdf462088d13 | 70 | } |
markrad | 0:cdf462088d13 | 71 | |
markrad | 0:cdf462088d13 | 72 | exit: |
markrad | 0:cdf462088d13 | 73 | mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G ); |
markrad | 0:cdf462088d13 | 74 | mbedtls_rsa_free( &ctx ); |
markrad | 0:cdf462088d13 | 75 | } |
markrad | 0:cdf462088d13 | 76 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 77 | |
markrad | 0:cdf462088d13 | 78 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 79 | void mbedtls_rsa_pkcs1_verify( char *message_hex_string, int padding_mode, int digest, |
markrad | 0:cdf462088d13 | 80 | int mod, int radix_N, char *input_N, int radix_E, |
markrad | 0:cdf462088d13 | 81 | char *input_E, char *result_hex_str, int result ) |
markrad | 0:cdf462088d13 | 82 | { |
markrad | 0:cdf462088d13 | 83 | unsigned char message_str[1000]; |
markrad | 0:cdf462088d13 | 84 | unsigned char hash_result[1000]; |
markrad | 0:cdf462088d13 | 85 | unsigned char result_str[1000]; |
markrad | 0:cdf462088d13 | 86 | mbedtls_rsa_context ctx; |
markrad | 0:cdf462088d13 | 87 | int msg_len; |
markrad | 0:cdf462088d13 | 88 | |
markrad | 0:cdf462088d13 | 89 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
markrad | 0:cdf462088d13 | 90 | memset( message_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 91 | memset( hash_result, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 92 | memset( result_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 93 | |
markrad | 0:cdf462088d13 | 94 | ctx.len = mod / 8; |
markrad | 0:cdf462088d13 | 95 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 ); |
markrad | 0:cdf462088d13 | 96 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 ); |
markrad | 0:cdf462088d13 | 97 | |
markrad | 0:cdf462088d13 | 98 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); |
markrad | 0:cdf462088d13 | 99 | |
markrad | 0:cdf462088d13 | 100 | msg_len = unhexify( message_str, message_hex_string ); |
markrad | 0:cdf462088d13 | 101 | unhexify( result_str, result_hex_str ); |
markrad | 0:cdf462088d13 | 102 | |
markrad | 0:cdf462088d13 | 103 | if( mbedtls_md_info_from_type( digest ) != NULL ) |
markrad | 0:cdf462088d13 | 104 | TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 ); |
markrad | 0:cdf462088d13 | 105 | |
markrad | 0:cdf462088d13 | 106 | TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, digest, 0, hash_result, result_str ) == result ); |
markrad | 0:cdf462088d13 | 107 | |
markrad | 0:cdf462088d13 | 108 | exit: |
markrad | 0:cdf462088d13 | 109 | mbedtls_rsa_free( &ctx ); |
markrad | 0:cdf462088d13 | 110 | } |
markrad | 0:cdf462088d13 | 111 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 112 | |
markrad | 0:cdf462088d13 | 113 | |
markrad | 0:cdf462088d13 | 114 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 115 | void rsa_pkcs1_sign_raw( char *message_hex_string, char *hash_result_string, |
markrad | 0:cdf462088d13 | 116 | int padding_mode, int mod, int radix_P, char *input_P, |
markrad | 0:cdf462088d13 | 117 | int radix_Q, char *input_Q, int radix_N, |
markrad | 0:cdf462088d13 | 118 | char *input_N, int radix_E, char *input_E, |
markrad | 0:cdf462088d13 | 119 | char *result_hex_str ) |
markrad | 0:cdf462088d13 | 120 | { |
markrad | 0:cdf462088d13 | 121 | unsigned char message_str[1000]; |
markrad | 0:cdf462088d13 | 122 | unsigned char hash_result[1000]; |
markrad | 0:cdf462088d13 | 123 | unsigned char output[1000]; |
markrad | 0:cdf462088d13 | 124 | unsigned char output_str[1000]; |
markrad | 0:cdf462088d13 | 125 | mbedtls_rsa_context ctx; |
markrad | 0:cdf462088d13 | 126 | mbedtls_mpi P1, Q1, H, G; |
markrad | 0:cdf462088d13 | 127 | int hash_len; |
markrad | 0:cdf462088d13 | 128 | rnd_pseudo_info rnd_info; |
markrad | 0:cdf462088d13 | 129 | |
markrad | 0:cdf462088d13 | 130 | mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G ); |
markrad | 0:cdf462088d13 | 131 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
markrad | 0:cdf462088d13 | 132 | |
markrad | 0:cdf462088d13 | 133 | memset( message_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 134 | memset( hash_result, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 135 | memset( output, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 136 | memset( output_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 137 | memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) ); |
markrad | 0:cdf462088d13 | 138 | |
markrad | 0:cdf462088d13 | 139 | ctx.len = mod / 8; |
markrad | 0:cdf462088d13 | 140 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 ); |
markrad | 0:cdf462088d13 | 141 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 ); |
markrad | 0:cdf462088d13 | 142 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 ); |
markrad | 0:cdf462088d13 | 143 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 ); |
markrad | 0:cdf462088d13 | 144 | |
markrad | 0:cdf462088d13 | 145 | TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 ); |
markrad | 0:cdf462088d13 | 146 | TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 ); |
markrad | 0:cdf462088d13 | 147 | TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 ); |
markrad | 0:cdf462088d13 | 148 | TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H ) == 0 ); |
markrad | 0:cdf462088d13 | 149 | TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 ); |
markrad | 0:cdf462088d13 | 150 | TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 ); |
markrad | 0:cdf462088d13 | 151 | TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 ); |
markrad | 0:cdf462088d13 | 152 | TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 ); |
markrad | 0:cdf462088d13 | 153 | |
markrad | 0:cdf462088d13 | 154 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); |
markrad | 0:cdf462088d13 | 155 | |
markrad | 0:cdf462088d13 | 156 | unhexify( message_str, message_hex_string ); |
markrad | 0:cdf462088d13 | 157 | hash_len = unhexify( hash_result, hash_result_string ); |
markrad | 0:cdf462088d13 | 158 | |
markrad | 0:cdf462088d13 | 159 | TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_NONE, hash_len, hash_result, output ) == 0 ); |
markrad | 0:cdf462088d13 | 160 | |
markrad | 0:cdf462088d13 | 161 | hexify( output_str, output, ctx.len ); |
markrad | 0:cdf462088d13 | 162 | |
markrad | 0:cdf462088d13 | 163 | TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 ); |
markrad | 0:cdf462088d13 | 164 | |
markrad | 0:cdf462088d13 | 165 | /* For PKCS#1 v1.5, there is an alternative way to generate signatures */ |
markrad | 0:cdf462088d13 | 166 | if( padding_mode == MBEDTLS_RSA_PKCS_V15 ) |
markrad | 0:cdf462088d13 | 167 | { |
markrad | 0:cdf462088d13 | 168 | memset( output, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 169 | memset( output_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 170 | |
markrad | 0:cdf462088d13 | 171 | TEST_ASSERT( mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, |
markrad | 0:cdf462088d13 | 172 | &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, |
markrad | 0:cdf462088d13 | 173 | hash_len, hash_result, output ) == 0 ); |
markrad | 0:cdf462088d13 | 174 | |
markrad | 0:cdf462088d13 | 175 | hexify( output_str, output, ctx.len ); |
markrad | 0:cdf462088d13 | 176 | |
markrad | 0:cdf462088d13 | 177 | TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 ); |
markrad | 0:cdf462088d13 | 178 | } |
markrad | 0:cdf462088d13 | 179 | |
markrad | 0:cdf462088d13 | 180 | exit: |
markrad | 0:cdf462088d13 | 181 | mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G ); |
markrad | 0:cdf462088d13 | 182 | mbedtls_rsa_free( &ctx ); |
markrad | 0:cdf462088d13 | 183 | } |
markrad | 0:cdf462088d13 | 184 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 185 | |
markrad | 0:cdf462088d13 | 186 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 187 | void rsa_pkcs1_verify_raw( char *message_hex_string, char *hash_result_string, |
markrad | 0:cdf462088d13 | 188 | int padding_mode, int mod, int radix_N, |
markrad | 0:cdf462088d13 | 189 | char *input_N, int radix_E, char *input_E, |
markrad | 0:cdf462088d13 | 190 | char *result_hex_str, int correct ) |
markrad | 0:cdf462088d13 | 191 | { |
markrad | 0:cdf462088d13 | 192 | unsigned char message_str[1000]; |
markrad | 0:cdf462088d13 | 193 | unsigned char hash_result[1000]; |
markrad | 0:cdf462088d13 | 194 | unsigned char result_str[1000]; |
markrad | 0:cdf462088d13 | 195 | unsigned char output[1000]; |
markrad | 0:cdf462088d13 | 196 | mbedtls_rsa_context ctx; |
markrad | 0:cdf462088d13 | 197 | size_t hash_len, olen; |
markrad | 0:cdf462088d13 | 198 | |
markrad | 0:cdf462088d13 | 199 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
markrad | 0:cdf462088d13 | 200 | memset( message_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 201 | memset( hash_result, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 202 | memset( result_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 203 | memset( output, 0x00, sizeof( output ) ); |
markrad | 0:cdf462088d13 | 204 | |
markrad | 0:cdf462088d13 | 205 | ctx.len = mod / 8; |
markrad | 0:cdf462088d13 | 206 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 ); |
markrad | 0:cdf462088d13 | 207 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 ); |
markrad | 0:cdf462088d13 | 208 | |
markrad | 0:cdf462088d13 | 209 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); |
markrad | 0:cdf462088d13 | 210 | |
markrad | 0:cdf462088d13 | 211 | unhexify( message_str, message_hex_string ); |
markrad | 0:cdf462088d13 | 212 | hash_len = unhexify( hash_result, hash_result_string ); |
markrad | 0:cdf462088d13 | 213 | unhexify( result_str, result_hex_str ); |
markrad | 0:cdf462088d13 | 214 | |
markrad | 0:cdf462088d13 | 215 | TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_NONE, hash_len, hash_result, result_str ) == correct ); |
markrad | 0:cdf462088d13 | 216 | |
markrad | 0:cdf462088d13 | 217 | /* For PKCS#1 v1.5, there is an alternative way to verify signatures */ |
markrad | 0:cdf462088d13 | 218 | if( padding_mode == MBEDTLS_RSA_PKCS_V15 ) |
markrad | 0:cdf462088d13 | 219 | { |
markrad | 0:cdf462088d13 | 220 | int ok; |
markrad | 0:cdf462088d13 | 221 | |
markrad | 0:cdf462088d13 | 222 | TEST_ASSERT( mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, |
markrad | 0:cdf462088d13 | 223 | NULL, NULL, MBEDTLS_RSA_PUBLIC, |
markrad | 0:cdf462088d13 | 224 | &olen, result_str, output, sizeof( output ) ) == 0 ); |
markrad | 0:cdf462088d13 | 225 | |
markrad | 0:cdf462088d13 | 226 | ok = olen == hash_len && memcmp( output, hash_result, olen ) == 0; |
markrad | 0:cdf462088d13 | 227 | if( correct == 0 ) |
markrad | 0:cdf462088d13 | 228 | TEST_ASSERT( ok == 1 ); |
markrad | 0:cdf462088d13 | 229 | else |
markrad | 0:cdf462088d13 | 230 | TEST_ASSERT( ok == 0 ); |
markrad | 0:cdf462088d13 | 231 | } |
markrad | 0:cdf462088d13 | 232 | |
markrad | 0:cdf462088d13 | 233 | exit: |
markrad | 0:cdf462088d13 | 234 | mbedtls_rsa_free( &ctx ); |
markrad | 0:cdf462088d13 | 235 | } |
markrad | 0:cdf462088d13 | 236 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 237 | |
markrad | 0:cdf462088d13 | 238 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 239 | void mbedtls_rsa_pkcs1_encrypt( char *message_hex_string, int padding_mode, int mod, |
markrad | 0:cdf462088d13 | 240 | int radix_N, char *input_N, int radix_E, char *input_E, |
markrad | 0:cdf462088d13 | 241 | char *result_hex_str, int result ) |
markrad | 0:cdf462088d13 | 242 | { |
markrad | 0:cdf462088d13 | 243 | unsigned char message_str[1000]; |
markrad | 0:cdf462088d13 | 244 | unsigned char output[1000]; |
markrad | 0:cdf462088d13 | 245 | unsigned char output_str[1000]; |
markrad | 0:cdf462088d13 | 246 | mbedtls_rsa_context ctx; |
markrad | 0:cdf462088d13 | 247 | size_t msg_len; |
markrad | 0:cdf462088d13 | 248 | rnd_pseudo_info rnd_info; |
markrad | 0:cdf462088d13 | 249 | |
markrad | 0:cdf462088d13 | 250 | memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) ); |
markrad | 0:cdf462088d13 | 251 | |
markrad | 0:cdf462088d13 | 252 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
markrad | 0:cdf462088d13 | 253 | memset( message_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 254 | memset( output, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 255 | memset( output_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 256 | |
markrad | 0:cdf462088d13 | 257 | ctx.len = mod / 8; |
markrad | 0:cdf462088d13 | 258 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 ); |
markrad | 0:cdf462088d13 | 259 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 ); |
markrad | 0:cdf462088d13 | 260 | |
markrad | 0:cdf462088d13 | 261 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); |
markrad | 0:cdf462088d13 | 262 | |
markrad | 0:cdf462088d13 | 263 | msg_len = unhexify( message_str, message_hex_string ); |
markrad | 0:cdf462088d13 | 264 | |
markrad | 0:cdf462088d13 | 265 | TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PUBLIC, msg_len, message_str, output ) == result ); |
markrad | 0:cdf462088d13 | 266 | if( result == 0 ) |
markrad | 0:cdf462088d13 | 267 | { |
markrad | 0:cdf462088d13 | 268 | hexify( output_str, output, ctx.len ); |
markrad | 0:cdf462088d13 | 269 | |
markrad | 0:cdf462088d13 | 270 | TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 ); |
markrad | 0:cdf462088d13 | 271 | } |
markrad | 0:cdf462088d13 | 272 | |
markrad | 0:cdf462088d13 | 273 | exit: |
markrad | 0:cdf462088d13 | 274 | mbedtls_rsa_free( &ctx ); |
markrad | 0:cdf462088d13 | 275 | } |
markrad | 0:cdf462088d13 | 276 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 277 | |
markrad | 0:cdf462088d13 | 278 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 279 | void rsa_pkcs1_encrypt_bad_rng( char *message_hex_string, int padding_mode, |
markrad | 0:cdf462088d13 | 280 | int mod, int radix_N, char *input_N, |
markrad | 0:cdf462088d13 | 281 | int radix_E, char *input_E, |
markrad | 0:cdf462088d13 | 282 | char *result_hex_str, int result ) |
markrad | 0:cdf462088d13 | 283 | { |
markrad | 0:cdf462088d13 | 284 | unsigned char message_str[1000]; |
markrad | 0:cdf462088d13 | 285 | unsigned char output[1000]; |
markrad | 0:cdf462088d13 | 286 | unsigned char output_str[1000]; |
markrad | 0:cdf462088d13 | 287 | mbedtls_rsa_context ctx; |
markrad | 0:cdf462088d13 | 288 | size_t msg_len; |
markrad | 0:cdf462088d13 | 289 | |
markrad | 0:cdf462088d13 | 290 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
markrad | 0:cdf462088d13 | 291 | memset( message_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 292 | memset( output, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 293 | memset( output_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 294 | |
markrad | 0:cdf462088d13 | 295 | ctx.len = mod / 8; |
markrad | 0:cdf462088d13 | 296 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 ); |
markrad | 0:cdf462088d13 | 297 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 ); |
markrad | 0:cdf462088d13 | 298 | |
markrad | 0:cdf462088d13 | 299 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); |
markrad | 0:cdf462088d13 | 300 | |
markrad | 0:cdf462088d13 | 301 | msg_len = unhexify( message_str, message_hex_string ); |
markrad | 0:cdf462088d13 | 302 | |
markrad | 0:cdf462088d13 | 303 | TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL, MBEDTLS_RSA_PUBLIC, msg_len, message_str, output ) == result ); |
markrad | 0:cdf462088d13 | 304 | if( result == 0 ) |
markrad | 0:cdf462088d13 | 305 | { |
markrad | 0:cdf462088d13 | 306 | hexify( output_str, output, ctx.len ); |
markrad | 0:cdf462088d13 | 307 | |
markrad | 0:cdf462088d13 | 308 | TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 ); |
markrad | 0:cdf462088d13 | 309 | } |
markrad | 0:cdf462088d13 | 310 | |
markrad | 0:cdf462088d13 | 311 | exit: |
markrad | 0:cdf462088d13 | 312 | mbedtls_rsa_free( &ctx ); |
markrad | 0:cdf462088d13 | 313 | } |
markrad | 0:cdf462088d13 | 314 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 315 | |
markrad | 0:cdf462088d13 | 316 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 317 | void mbedtls_rsa_pkcs1_decrypt( char *message_hex_string, int padding_mode, int mod, |
markrad | 0:cdf462088d13 | 318 | int radix_P, char *input_P, int radix_Q, char *input_Q, |
markrad | 0:cdf462088d13 | 319 | int radix_N, char *input_N, int radix_E, char *input_E, |
markrad | 0:cdf462088d13 | 320 | int max_output, char *result_hex_str, int result ) |
markrad | 0:cdf462088d13 | 321 | { |
markrad | 0:cdf462088d13 | 322 | unsigned char message_str[1000]; |
markrad | 0:cdf462088d13 | 323 | unsigned char output[1000]; |
markrad | 0:cdf462088d13 | 324 | unsigned char output_str[1000]; |
markrad | 0:cdf462088d13 | 325 | mbedtls_rsa_context ctx; |
markrad | 0:cdf462088d13 | 326 | mbedtls_mpi P1, Q1, H, G; |
markrad | 0:cdf462088d13 | 327 | size_t output_len; |
markrad | 0:cdf462088d13 | 328 | rnd_pseudo_info rnd_info; |
markrad | 0:cdf462088d13 | 329 | |
markrad | 0:cdf462088d13 | 330 | mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G ); |
markrad | 0:cdf462088d13 | 331 | mbedtls_rsa_init( &ctx, padding_mode, 0 ); |
markrad | 0:cdf462088d13 | 332 | |
markrad | 0:cdf462088d13 | 333 | memset( message_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 334 | memset( output, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 335 | memset( output_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 336 | memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) ); |
markrad | 0:cdf462088d13 | 337 | |
markrad | 0:cdf462088d13 | 338 | ctx.len = mod / 8; |
markrad | 0:cdf462088d13 | 339 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 ); |
markrad | 0:cdf462088d13 | 340 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 ); |
markrad | 0:cdf462088d13 | 341 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 ); |
markrad | 0:cdf462088d13 | 342 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 ); |
markrad | 0:cdf462088d13 | 343 | |
markrad | 0:cdf462088d13 | 344 | TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 ); |
markrad | 0:cdf462088d13 | 345 | TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 ); |
markrad | 0:cdf462088d13 | 346 | TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 ); |
markrad | 0:cdf462088d13 | 347 | TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H ) == 0 ); |
markrad | 0:cdf462088d13 | 348 | TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 ); |
markrad | 0:cdf462088d13 | 349 | TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 ); |
markrad | 0:cdf462088d13 | 350 | TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 ); |
markrad | 0:cdf462088d13 | 351 | TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 ); |
markrad | 0:cdf462088d13 | 352 | |
markrad | 0:cdf462088d13 | 353 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); |
markrad | 0:cdf462088d13 | 354 | |
markrad | 0:cdf462088d13 | 355 | unhexify( message_str, message_hex_string ); |
markrad | 0:cdf462088d13 | 356 | output_len = 0; |
markrad | 0:cdf462088d13 | 357 | |
markrad | 0:cdf462088d13 | 358 | TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, &output_len, message_str, output, max_output ) == result ); |
markrad | 0:cdf462088d13 | 359 | if( result == 0 ) |
markrad | 0:cdf462088d13 | 360 | { |
markrad | 0:cdf462088d13 | 361 | hexify( output_str, output, ctx.len ); |
markrad | 0:cdf462088d13 | 362 | |
markrad | 0:cdf462088d13 | 363 | TEST_ASSERT( strncasecmp( (char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 ); |
markrad | 0:cdf462088d13 | 364 | } |
markrad | 0:cdf462088d13 | 365 | |
markrad | 0:cdf462088d13 | 366 | exit: |
markrad | 0:cdf462088d13 | 367 | mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G ); |
markrad | 0:cdf462088d13 | 368 | mbedtls_rsa_free( &ctx ); |
markrad | 0:cdf462088d13 | 369 | } |
markrad | 0:cdf462088d13 | 370 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 371 | |
markrad | 0:cdf462088d13 | 372 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 373 | void mbedtls_rsa_public( char *message_hex_string, int mod, int radix_N, char *input_N, |
markrad | 0:cdf462088d13 | 374 | int radix_E, char *input_E, char *result_hex_str, int result ) |
markrad | 0:cdf462088d13 | 375 | { |
markrad | 0:cdf462088d13 | 376 | unsigned char message_str[1000]; |
markrad | 0:cdf462088d13 | 377 | unsigned char output[1000]; |
markrad | 0:cdf462088d13 | 378 | unsigned char output_str[1000]; |
markrad | 0:cdf462088d13 | 379 | mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */ |
markrad | 0:cdf462088d13 | 380 | |
markrad | 0:cdf462088d13 | 381 | mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 ); |
markrad | 0:cdf462088d13 | 382 | mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 ); |
markrad | 0:cdf462088d13 | 383 | memset( message_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 384 | memset( output, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 385 | memset( output_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 386 | |
markrad | 0:cdf462088d13 | 387 | ctx.len = mod / 8; |
markrad | 0:cdf462088d13 | 388 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 ); |
markrad | 0:cdf462088d13 | 389 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 ); |
markrad | 0:cdf462088d13 | 390 | |
markrad | 0:cdf462088d13 | 391 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); |
markrad | 0:cdf462088d13 | 392 | |
markrad | 0:cdf462088d13 | 393 | unhexify( message_str, message_hex_string ); |
markrad | 0:cdf462088d13 | 394 | |
markrad | 0:cdf462088d13 | 395 | TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str, output ) == result ); |
markrad | 0:cdf462088d13 | 396 | if( result == 0 ) |
markrad | 0:cdf462088d13 | 397 | { |
markrad | 0:cdf462088d13 | 398 | hexify( output_str, output, ctx.len ); |
markrad | 0:cdf462088d13 | 399 | |
markrad | 0:cdf462088d13 | 400 | TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 ); |
markrad | 0:cdf462088d13 | 401 | } |
markrad | 0:cdf462088d13 | 402 | |
markrad | 0:cdf462088d13 | 403 | /* And now with the copy */ |
markrad | 0:cdf462088d13 | 404 | TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 ); |
markrad | 0:cdf462088d13 | 405 | /* clear the original to be sure */ |
markrad | 0:cdf462088d13 | 406 | mbedtls_rsa_free( &ctx ); |
markrad | 0:cdf462088d13 | 407 | |
markrad | 0:cdf462088d13 | 408 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 ); |
markrad | 0:cdf462088d13 | 409 | |
markrad | 0:cdf462088d13 | 410 | memset( output, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 411 | memset( output_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 412 | TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str, output ) == result ); |
markrad | 0:cdf462088d13 | 413 | if( result == 0 ) |
markrad | 0:cdf462088d13 | 414 | { |
markrad | 0:cdf462088d13 | 415 | hexify( output_str, output, ctx2.len ); |
markrad | 0:cdf462088d13 | 416 | |
markrad | 0:cdf462088d13 | 417 | TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 ); |
markrad | 0:cdf462088d13 | 418 | } |
markrad | 0:cdf462088d13 | 419 | |
markrad | 0:cdf462088d13 | 420 | exit: |
markrad | 0:cdf462088d13 | 421 | mbedtls_rsa_free( &ctx ); |
markrad | 0:cdf462088d13 | 422 | mbedtls_rsa_free( &ctx2 ); |
markrad | 0:cdf462088d13 | 423 | } |
markrad | 0:cdf462088d13 | 424 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 425 | |
markrad | 0:cdf462088d13 | 426 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 427 | void mbedtls_rsa_private( char *message_hex_string, int mod, int radix_P, char *input_P, |
markrad | 0:cdf462088d13 | 428 | int radix_Q, char *input_Q, int radix_N, char *input_N, |
markrad | 0:cdf462088d13 | 429 | int radix_E, char *input_E, char *result_hex_str, int result ) |
markrad | 0:cdf462088d13 | 430 | { |
markrad | 0:cdf462088d13 | 431 | unsigned char message_str[1000]; |
markrad | 0:cdf462088d13 | 432 | unsigned char output[1000]; |
markrad | 0:cdf462088d13 | 433 | unsigned char output_str[1000]; |
markrad | 0:cdf462088d13 | 434 | mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */ |
markrad | 0:cdf462088d13 | 435 | mbedtls_mpi P1, Q1, H, G; |
markrad | 0:cdf462088d13 | 436 | rnd_pseudo_info rnd_info; |
markrad | 0:cdf462088d13 | 437 | int i; |
markrad | 0:cdf462088d13 | 438 | |
markrad | 0:cdf462088d13 | 439 | mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G ); |
markrad | 0:cdf462088d13 | 440 | mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 ); |
markrad | 0:cdf462088d13 | 441 | mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 ); |
markrad | 0:cdf462088d13 | 442 | |
markrad | 0:cdf462088d13 | 443 | memset( message_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 444 | memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) ); |
markrad | 0:cdf462088d13 | 445 | |
markrad | 0:cdf462088d13 | 446 | ctx.len = mod / 8; |
markrad | 0:cdf462088d13 | 447 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 ); |
markrad | 0:cdf462088d13 | 448 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 ); |
markrad | 0:cdf462088d13 | 449 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 ); |
markrad | 0:cdf462088d13 | 450 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 ); |
markrad | 0:cdf462088d13 | 451 | |
markrad | 0:cdf462088d13 | 452 | TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 ); |
markrad | 0:cdf462088d13 | 453 | TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 ); |
markrad | 0:cdf462088d13 | 454 | TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 ); |
markrad | 0:cdf462088d13 | 455 | TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H ) == 0 ); |
markrad | 0:cdf462088d13 | 456 | TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 ); |
markrad | 0:cdf462088d13 | 457 | TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 ); |
markrad | 0:cdf462088d13 | 458 | TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 ); |
markrad | 0:cdf462088d13 | 459 | TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 ); |
markrad | 0:cdf462088d13 | 460 | |
markrad | 0:cdf462088d13 | 461 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); |
markrad | 0:cdf462088d13 | 462 | |
markrad | 0:cdf462088d13 | 463 | unhexify( message_str, message_hex_string ); |
markrad | 0:cdf462088d13 | 464 | |
markrad | 0:cdf462088d13 | 465 | /* repeat three times to test updating of blinding values */ |
markrad | 0:cdf462088d13 | 466 | for( i = 0; i < 3; i++ ) |
markrad | 0:cdf462088d13 | 467 | { |
markrad | 0:cdf462088d13 | 468 | memset( output, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 469 | memset( output_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 470 | TEST_ASSERT( mbedtls_rsa_private( &ctx, rnd_pseudo_rand, &rnd_info, |
markrad | 0:cdf462088d13 | 471 | message_str, output ) == result ); |
markrad | 0:cdf462088d13 | 472 | if( result == 0 ) |
markrad | 0:cdf462088d13 | 473 | { |
markrad | 0:cdf462088d13 | 474 | hexify( output_str, output, ctx.len ); |
markrad | 0:cdf462088d13 | 475 | |
markrad | 0:cdf462088d13 | 476 | TEST_ASSERT( strcasecmp( (char *) output_str, |
markrad | 0:cdf462088d13 | 477 | result_hex_str ) == 0 ); |
markrad | 0:cdf462088d13 | 478 | } |
markrad | 0:cdf462088d13 | 479 | } |
markrad | 0:cdf462088d13 | 480 | |
markrad | 0:cdf462088d13 | 481 | /* And now one more time with the copy */ |
markrad | 0:cdf462088d13 | 482 | TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 ); |
markrad | 0:cdf462088d13 | 483 | /* clear the original to be sure */ |
markrad | 0:cdf462088d13 | 484 | mbedtls_rsa_free( &ctx ); |
markrad | 0:cdf462088d13 | 485 | |
markrad | 0:cdf462088d13 | 486 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 ); |
markrad | 0:cdf462088d13 | 487 | |
markrad | 0:cdf462088d13 | 488 | memset( output, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 489 | memset( output_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 490 | TEST_ASSERT( mbedtls_rsa_private( &ctx2, rnd_pseudo_rand, &rnd_info, |
markrad | 0:cdf462088d13 | 491 | message_str, output ) == result ); |
markrad | 0:cdf462088d13 | 492 | if( result == 0 ) |
markrad | 0:cdf462088d13 | 493 | { |
markrad | 0:cdf462088d13 | 494 | hexify( output_str, output, ctx2.len ); |
markrad | 0:cdf462088d13 | 495 | |
markrad | 0:cdf462088d13 | 496 | TEST_ASSERT( strcasecmp( (char *) output_str, |
markrad | 0:cdf462088d13 | 497 | result_hex_str ) == 0 ); |
markrad | 0:cdf462088d13 | 498 | } |
markrad | 0:cdf462088d13 | 499 | |
markrad | 0:cdf462088d13 | 500 | exit: |
markrad | 0:cdf462088d13 | 501 | mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G ); |
markrad | 0:cdf462088d13 | 502 | mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 ); |
markrad | 0:cdf462088d13 | 503 | } |
markrad | 0:cdf462088d13 | 504 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 505 | |
markrad | 0:cdf462088d13 | 506 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 507 | void rsa_check_privkey_null() |
markrad | 0:cdf462088d13 | 508 | { |
markrad | 0:cdf462088d13 | 509 | mbedtls_rsa_context ctx; |
markrad | 0:cdf462088d13 | 510 | memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) ); |
markrad | 0:cdf462088d13 | 511 | |
markrad | 0:cdf462088d13 | 512 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
markrad | 0:cdf462088d13 | 513 | } |
markrad | 0:cdf462088d13 | 514 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 515 | |
markrad | 0:cdf462088d13 | 516 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 517 | void mbedtls_rsa_check_pubkey( int radix_N, char *input_N, int radix_E, char *input_E, |
markrad | 0:cdf462088d13 | 518 | int result ) |
markrad | 0:cdf462088d13 | 519 | { |
markrad | 0:cdf462088d13 | 520 | mbedtls_rsa_context ctx; |
markrad | 0:cdf462088d13 | 521 | |
markrad | 0:cdf462088d13 | 522 | mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 ); |
markrad | 0:cdf462088d13 | 523 | |
markrad | 0:cdf462088d13 | 524 | if( strlen( input_N ) ) |
markrad | 0:cdf462088d13 | 525 | { |
markrad | 0:cdf462088d13 | 526 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 ); |
markrad | 0:cdf462088d13 | 527 | } |
markrad | 0:cdf462088d13 | 528 | if( strlen( input_E ) ) |
markrad | 0:cdf462088d13 | 529 | { |
markrad | 0:cdf462088d13 | 530 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 ); |
markrad | 0:cdf462088d13 | 531 | } |
markrad | 0:cdf462088d13 | 532 | |
markrad | 0:cdf462088d13 | 533 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result ); |
markrad | 0:cdf462088d13 | 534 | |
markrad | 0:cdf462088d13 | 535 | exit: |
markrad | 0:cdf462088d13 | 536 | mbedtls_rsa_free( &ctx ); |
markrad | 0:cdf462088d13 | 537 | } |
markrad | 0:cdf462088d13 | 538 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 539 | |
markrad | 0:cdf462088d13 | 540 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 541 | void mbedtls_rsa_check_privkey( int mod, int radix_P, char *input_P, int radix_Q, |
markrad | 0:cdf462088d13 | 542 | char *input_Q, int radix_N, char *input_N, |
markrad | 0:cdf462088d13 | 543 | int radix_E, char *input_E, int radix_D, char *input_D, |
markrad | 0:cdf462088d13 | 544 | int radix_DP, char *input_DP, int radix_DQ, |
markrad | 0:cdf462088d13 | 545 | char *input_DQ, int radix_QP, char *input_QP, |
markrad | 0:cdf462088d13 | 546 | int result ) |
markrad | 0:cdf462088d13 | 547 | { |
markrad | 0:cdf462088d13 | 548 | mbedtls_rsa_context ctx; |
markrad | 0:cdf462088d13 | 549 | |
markrad | 0:cdf462088d13 | 550 | mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 ); |
markrad | 0:cdf462088d13 | 551 | |
markrad | 0:cdf462088d13 | 552 | ctx.len = mod / 8; |
markrad | 0:cdf462088d13 | 553 | if( strlen( input_P ) ) |
markrad | 0:cdf462088d13 | 554 | { |
markrad | 0:cdf462088d13 | 555 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 ); |
markrad | 0:cdf462088d13 | 556 | } |
markrad | 0:cdf462088d13 | 557 | if( strlen( input_Q ) ) |
markrad | 0:cdf462088d13 | 558 | { |
markrad | 0:cdf462088d13 | 559 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 ); |
markrad | 0:cdf462088d13 | 560 | } |
markrad | 0:cdf462088d13 | 561 | if( strlen( input_N ) ) |
markrad | 0:cdf462088d13 | 562 | { |
markrad | 0:cdf462088d13 | 563 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 ); |
markrad | 0:cdf462088d13 | 564 | } |
markrad | 0:cdf462088d13 | 565 | if( strlen( input_E ) ) |
markrad | 0:cdf462088d13 | 566 | { |
markrad | 0:cdf462088d13 | 567 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 ); |
markrad | 0:cdf462088d13 | 568 | } |
markrad | 0:cdf462088d13 | 569 | if( strlen( input_D ) ) |
markrad | 0:cdf462088d13 | 570 | { |
markrad | 0:cdf462088d13 | 571 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 ); |
markrad | 0:cdf462088d13 | 572 | } |
markrad | 0:cdf462088d13 | 573 | if( strlen( input_DP ) ) |
markrad | 0:cdf462088d13 | 574 | { |
markrad | 0:cdf462088d13 | 575 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 ); |
markrad | 0:cdf462088d13 | 576 | } |
markrad | 0:cdf462088d13 | 577 | if( strlen( input_DQ ) ) |
markrad | 0:cdf462088d13 | 578 | { |
markrad | 0:cdf462088d13 | 579 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 ); |
markrad | 0:cdf462088d13 | 580 | } |
markrad | 0:cdf462088d13 | 581 | if( strlen( input_QP ) ) |
markrad | 0:cdf462088d13 | 582 | { |
markrad | 0:cdf462088d13 | 583 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 ); |
markrad | 0:cdf462088d13 | 584 | } |
markrad | 0:cdf462088d13 | 585 | |
markrad | 0:cdf462088d13 | 586 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result ); |
markrad | 0:cdf462088d13 | 587 | |
markrad | 0:cdf462088d13 | 588 | exit: |
markrad | 0:cdf462088d13 | 589 | mbedtls_rsa_free( &ctx ); |
markrad | 0:cdf462088d13 | 590 | } |
markrad | 0:cdf462088d13 | 591 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 592 | |
markrad | 0:cdf462088d13 | 593 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 594 | void rsa_check_pubpriv( int mod, int radix_Npub, char *input_Npub, |
markrad | 0:cdf462088d13 | 595 | int radix_Epub, char *input_Epub, |
markrad | 0:cdf462088d13 | 596 | int radix_P, char *input_P, int radix_Q, |
markrad | 0:cdf462088d13 | 597 | char *input_Q, int radix_N, char *input_N, |
markrad | 0:cdf462088d13 | 598 | int radix_E, char *input_E, int radix_D, char *input_D, |
markrad | 0:cdf462088d13 | 599 | int radix_DP, char *input_DP, int radix_DQ, |
markrad | 0:cdf462088d13 | 600 | char *input_DQ, int radix_QP, char *input_QP, |
markrad | 0:cdf462088d13 | 601 | int result ) |
markrad | 0:cdf462088d13 | 602 | { |
markrad | 0:cdf462088d13 | 603 | mbedtls_rsa_context pub, prv; |
markrad | 0:cdf462088d13 | 604 | |
markrad | 0:cdf462088d13 | 605 | mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 ); |
markrad | 0:cdf462088d13 | 606 | mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 ); |
markrad | 0:cdf462088d13 | 607 | |
markrad | 0:cdf462088d13 | 608 | pub.len = mod / 8; |
markrad | 0:cdf462088d13 | 609 | prv.len = mod / 8; |
markrad | 0:cdf462088d13 | 610 | |
markrad | 0:cdf462088d13 | 611 | if( strlen( input_Npub ) ) |
markrad | 0:cdf462088d13 | 612 | { |
markrad | 0:cdf462088d13 | 613 | TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 ); |
markrad | 0:cdf462088d13 | 614 | } |
markrad | 0:cdf462088d13 | 615 | if( strlen( input_Epub ) ) |
markrad | 0:cdf462088d13 | 616 | { |
markrad | 0:cdf462088d13 | 617 | TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 ); |
markrad | 0:cdf462088d13 | 618 | } |
markrad | 0:cdf462088d13 | 619 | |
markrad | 0:cdf462088d13 | 620 | if( strlen( input_P ) ) |
markrad | 0:cdf462088d13 | 621 | { |
markrad | 0:cdf462088d13 | 622 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 ); |
markrad | 0:cdf462088d13 | 623 | } |
markrad | 0:cdf462088d13 | 624 | if( strlen( input_Q ) ) |
markrad | 0:cdf462088d13 | 625 | { |
markrad | 0:cdf462088d13 | 626 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 ); |
markrad | 0:cdf462088d13 | 627 | } |
markrad | 0:cdf462088d13 | 628 | if( strlen( input_N ) ) |
markrad | 0:cdf462088d13 | 629 | { |
markrad | 0:cdf462088d13 | 630 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 ); |
markrad | 0:cdf462088d13 | 631 | } |
markrad | 0:cdf462088d13 | 632 | if( strlen( input_E ) ) |
markrad | 0:cdf462088d13 | 633 | { |
markrad | 0:cdf462088d13 | 634 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 ); |
markrad | 0:cdf462088d13 | 635 | } |
markrad | 0:cdf462088d13 | 636 | if( strlen( input_D ) ) |
markrad | 0:cdf462088d13 | 637 | { |
markrad | 0:cdf462088d13 | 638 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 ); |
markrad | 0:cdf462088d13 | 639 | } |
markrad | 0:cdf462088d13 | 640 | if( strlen( input_DP ) ) |
markrad | 0:cdf462088d13 | 641 | { |
markrad | 0:cdf462088d13 | 642 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 ); |
markrad | 0:cdf462088d13 | 643 | } |
markrad | 0:cdf462088d13 | 644 | if( strlen( input_DQ ) ) |
markrad | 0:cdf462088d13 | 645 | { |
markrad | 0:cdf462088d13 | 646 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 ); |
markrad | 0:cdf462088d13 | 647 | } |
markrad | 0:cdf462088d13 | 648 | if( strlen( input_QP ) ) |
markrad | 0:cdf462088d13 | 649 | { |
markrad | 0:cdf462088d13 | 650 | TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 ); |
markrad | 0:cdf462088d13 | 651 | } |
markrad | 0:cdf462088d13 | 652 | |
markrad | 0:cdf462088d13 | 653 | TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result ); |
markrad | 0:cdf462088d13 | 654 | |
markrad | 0:cdf462088d13 | 655 | exit: |
markrad | 0:cdf462088d13 | 656 | mbedtls_rsa_free( &pub ); |
markrad | 0:cdf462088d13 | 657 | mbedtls_rsa_free( &prv ); |
markrad | 0:cdf462088d13 | 658 | } |
markrad | 0:cdf462088d13 | 659 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 660 | |
markrad | 0:cdf462088d13 | 661 | /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */ |
markrad | 0:cdf462088d13 | 662 | void mbedtls_rsa_gen_key( int nrbits, int exponent, int result) |
markrad | 0:cdf462088d13 | 663 | { |
markrad | 0:cdf462088d13 | 664 | mbedtls_rsa_context ctx; |
markrad | 0:cdf462088d13 | 665 | mbedtls_entropy_context entropy; |
markrad | 0:cdf462088d13 | 666 | mbedtls_ctr_drbg_context ctr_drbg; |
markrad | 0:cdf462088d13 | 667 | const char *pers = "test_suite_rsa"; |
markrad | 0:cdf462088d13 | 668 | |
markrad | 0:cdf462088d13 | 669 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
markrad | 0:cdf462088d13 | 670 | |
markrad | 0:cdf462088d13 | 671 | mbedtls_entropy_init( &entropy ); |
markrad | 0:cdf462088d13 | 672 | TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, |
markrad | 0:cdf462088d13 | 673 | (const unsigned char *) pers, strlen( pers ) ) == 0 ); |
markrad | 0:cdf462088d13 | 674 | |
markrad | 0:cdf462088d13 | 675 | mbedtls_rsa_init( &ctx, 0, 0 ); |
markrad | 0:cdf462088d13 | 676 | |
markrad | 0:cdf462088d13 | 677 | TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result ); |
markrad | 0:cdf462088d13 | 678 | if( result == 0 ) |
markrad | 0:cdf462088d13 | 679 | { |
markrad | 0:cdf462088d13 | 680 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); |
markrad | 0:cdf462088d13 | 681 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 ); |
markrad | 0:cdf462088d13 | 682 | } |
markrad | 0:cdf462088d13 | 683 | |
markrad | 0:cdf462088d13 | 684 | exit: |
markrad | 0:cdf462088d13 | 685 | mbedtls_rsa_free( &ctx ); |
markrad | 0:cdf462088d13 | 686 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
markrad | 0:cdf462088d13 | 687 | mbedtls_entropy_free( &entropy ); |
markrad | 0:cdf462088d13 | 688 | } |
markrad | 0:cdf462088d13 | 689 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 690 | |
markrad | 0:cdf462088d13 | 691 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
markrad | 0:cdf462088d13 | 692 | void rsa_selftest() |
markrad | 0:cdf462088d13 | 693 | { |
markrad | 0:cdf462088d13 | 694 | TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 ); |
markrad | 0:cdf462088d13 | 695 | } |
markrad | 0:cdf462088d13 | 696 | /* END_CASE */ |