mbed TLS Build
tests/suites/test_suite_pkcs1_v15.function@1:1a219dea6cb5, 2019-06-04 (annotated)
- Committer:
- williequesada
- Date:
- Tue Jun 04 16:03:38 2019 +0000
- Revision:
- 1:1a219dea6cb5
- Parent:
- 0:cdf462088d13
compartir a Pablo
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/md.h" |
markrad | 0:cdf462088d13 | 4 | /* END_HEADER */ |
markrad | 0:cdf462088d13 | 5 | |
markrad | 0:cdf462088d13 | 6 | /* BEGIN_DEPENDENCIES |
markrad | 0:cdf462088d13 | 7 | * depends_on:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_SHA1_C |
markrad | 0:cdf462088d13 | 8 | * END_DEPENDENCIES |
markrad | 0:cdf462088d13 | 9 | */ |
markrad | 0:cdf462088d13 | 10 | |
markrad | 0:cdf462088d13 | 11 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 12 | void pkcs1_rsaes_v15_encrypt( int mod, int radix_N, char *input_N, int radix_E, |
markrad | 0:cdf462088d13 | 13 | char *input_E, int hash, |
markrad | 0:cdf462088d13 | 14 | char *message_hex_string, char *seed, |
markrad | 0:cdf462088d13 | 15 | char *result_hex_str, int result ) |
markrad | 0:cdf462088d13 | 16 | { |
markrad | 0:cdf462088d13 | 17 | unsigned char message_str[1000]; |
markrad | 0:cdf462088d13 | 18 | unsigned char output[1000]; |
markrad | 0:cdf462088d13 | 19 | unsigned char output_str[1000]; |
markrad | 0:cdf462088d13 | 20 | unsigned char rnd_buf[1000]; |
markrad | 0:cdf462088d13 | 21 | mbedtls_rsa_context ctx; |
markrad | 0:cdf462088d13 | 22 | size_t msg_len; |
markrad | 0:cdf462088d13 | 23 | rnd_buf_info info; |
markrad | 0:cdf462088d13 | 24 | |
markrad | 0:cdf462088d13 | 25 | info.length = unhexify( rnd_buf, seed ); |
markrad | 0:cdf462088d13 | 26 | info.buf = rnd_buf; |
markrad | 0:cdf462088d13 | 27 | |
markrad | 0:cdf462088d13 | 28 | mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, hash ); |
markrad | 0:cdf462088d13 | 29 | memset( message_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 30 | memset( output, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 31 | memset( output_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 32 | |
markrad | 0:cdf462088d13 | 33 | ctx.len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 ); |
markrad | 0:cdf462088d13 | 34 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 ); |
markrad | 0:cdf462088d13 | 35 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 ); |
markrad | 0:cdf462088d13 | 36 | |
markrad | 0:cdf462088d13 | 37 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); |
markrad | 0:cdf462088d13 | 38 | |
markrad | 0:cdf462088d13 | 39 | msg_len = unhexify( message_str, message_hex_string ); |
markrad | 0:cdf462088d13 | 40 | |
markrad | 0:cdf462088d13 | 41 | TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_buffer_rand, &info, MBEDTLS_RSA_PUBLIC, msg_len, message_str, output ) == result ); |
markrad | 0:cdf462088d13 | 42 | if( result == 0 ) |
markrad | 0:cdf462088d13 | 43 | { |
markrad | 0:cdf462088d13 | 44 | hexify( output_str, output, ctx.len ); |
markrad | 0:cdf462088d13 | 45 | |
markrad | 0:cdf462088d13 | 46 | TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 ); |
markrad | 0:cdf462088d13 | 47 | } |
markrad | 0:cdf462088d13 | 48 | |
markrad | 0:cdf462088d13 | 49 | exit: |
markrad | 0:cdf462088d13 | 50 | mbedtls_rsa_free( &ctx ); |
markrad | 0:cdf462088d13 | 51 | } |
markrad | 0:cdf462088d13 | 52 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 53 | |
markrad | 0:cdf462088d13 | 54 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 55 | void pkcs1_rsaes_v15_decrypt( int mod, int radix_P, char *input_P, |
markrad | 0:cdf462088d13 | 56 | int radix_Q, char *input_Q, int radix_N, |
markrad | 0:cdf462088d13 | 57 | char *input_N, int radix_E, char *input_E, |
markrad | 0:cdf462088d13 | 58 | int hash, char *result_hex_str, char *seed, |
markrad | 0:cdf462088d13 | 59 | char *message_hex_string, int result ) |
markrad | 0:cdf462088d13 | 60 | { |
markrad | 0:cdf462088d13 | 61 | unsigned char message_str[1000]; |
markrad | 0:cdf462088d13 | 62 | unsigned char output[1000]; |
markrad | 0:cdf462088d13 | 63 | unsigned char output_str[1000]; |
markrad | 0:cdf462088d13 | 64 | mbedtls_rsa_context ctx; |
markrad | 0:cdf462088d13 | 65 | mbedtls_mpi P1, Q1, H, G; |
markrad | 0:cdf462088d13 | 66 | size_t output_len; |
markrad | 0:cdf462088d13 | 67 | rnd_pseudo_info rnd_info; |
markrad | 0:cdf462088d13 | 68 | ((void) seed); |
markrad | 0:cdf462088d13 | 69 | |
markrad | 0:cdf462088d13 | 70 | mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G ); |
markrad | 0:cdf462088d13 | 71 | mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, hash ); |
markrad | 0:cdf462088d13 | 72 | |
markrad | 0:cdf462088d13 | 73 | memset( message_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 74 | memset( output, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 75 | memset( output_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 76 | memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) ); |
markrad | 0:cdf462088d13 | 77 | |
markrad | 0:cdf462088d13 | 78 | ctx.len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 ); |
markrad | 0:cdf462088d13 | 79 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 ); |
markrad | 0:cdf462088d13 | 80 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 ); |
markrad | 0:cdf462088d13 | 81 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 ); |
markrad | 0:cdf462088d13 | 82 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 ); |
markrad | 0:cdf462088d13 | 83 | |
markrad | 0:cdf462088d13 | 84 | TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &ctx.P, 1 ) == 0 ); |
markrad | 0:cdf462088d13 | 85 | TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 ); |
markrad | 0:cdf462088d13 | 86 | TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 ); |
markrad | 0:cdf462088d13 | 87 | TEST_ASSERT( mbedtls_mpi_gcd( &G, &ctx.E, &H ) == 0 ); |
markrad | 0:cdf462088d13 | 88 | TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 ); |
markrad | 0:cdf462088d13 | 89 | TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 ); |
markrad | 0:cdf462088d13 | 90 | TEST_ASSERT( mbedtls_mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 ); |
markrad | 0:cdf462088d13 | 91 | TEST_ASSERT( mbedtls_mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 ); |
markrad | 0:cdf462088d13 | 92 | |
markrad | 0:cdf462088d13 | 93 | TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 ); |
markrad | 0:cdf462088d13 | 94 | |
markrad | 0:cdf462088d13 | 95 | unhexify( message_str, message_hex_string ); |
markrad | 0:cdf462088d13 | 96 | |
markrad | 0:cdf462088d13 | 97 | TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, &output_len, message_str, output, 1000 ) == result ); |
markrad | 0:cdf462088d13 | 98 | if( result == 0 ) |
markrad | 0:cdf462088d13 | 99 | { |
markrad | 0:cdf462088d13 | 100 | hexify( output_str, output, ctx.len ); |
markrad | 0:cdf462088d13 | 101 | |
markrad | 0:cdf462088d13 | 102 | TEST_ASSERT( strncasecmp( (char *) output_str, result_hex_str, strlen( result_hex_str ) ) == 0 ); |
markrad | 0:cdf462088d13 | 103 | } |
markrad | 0:cdf462088d13 | 104 | |
markrad | 0:cdf462088d13 | 105 | exit: |
markrad | 0:cdf462088d13 | 106 | mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G ); |
markrad | 0:cdf462088d13 | 107 | mbedtls_rsa_free( &ctx ); |
markrad | 0:cdf462088d13 | 108 | } |
markrad | 0:cdf462088d13 | 109 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 110 | |
markrad | 0:cdf462088d13 | 111 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 112 | void pkcs1_rsassa_v15_sign( int mod, int radix_P, char *input_P, int radix_Q, |
markrad | 0:cdf462088d13 | 113 | char *input_Q, int radix_N, char *input_N, |
markrad | 0:cdf462088d13 | 114 | int radix_E, char *input_E, int digest, int hash, |
markrad | 0:cdf462088d13 | 115 | char *message_hex_string, char *salt, |
markrad | 0:cdf462088d13 | 116 | char *result_hex_str, int result ) |
markrad | 0:cdf462088d13 | 117 | { |
markrad | 0:cdf462088d13 | 118 | unsigned char message_str[1000]; |
markrad | 0:cdf462088d13 | 119 | unsigned char hash_result[1000]; |
markrad | 0:cdf462088d13 | 120 | unsigned char output[1000]; |
markrad | 0:cdf462088d13 | 121 | unsigned char output_str[1000]; |
markrad | 0:cdf462088d13 | 122 | unsigned char rnd_buf[1000]; |
markrad | 0:cdf462088d13 | 123 | mbedtls_rsa_context ctx; |
markrad | 0:cdf462088d13 | 124 | mbedtls_mpi P1, Q1, H, G; |
markrad | 0:cdf462088d13 | 125 | size_t msg_len; |
markrad | 0:cdf462088d13 | 126 | rnd_buf_info info; |
markrad | 0:cdf462088d13 | 127 | |
markrad | 0:cdf462088d13 | 128 | info.length = unhexify( rnd_buf, salt ); |
markrad | 0:cdf462088d13 | 129 | info.buf = rnd_buf; |
markrad | 0:cdf462088d13 | 130 | |
markrad | 0:cdf462088d13 | 131 | mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G ); |
markrad | 0:cdf462088d13 | 132 | mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, hash ); |
markrad | 0:cdf462088d13 | 133 | |
markrad | 0:cdf462088d13 | 134 | memset( message_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 135 | memset( hash_result, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 136 | memset( output, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 137 | memset( output_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 138 | |
markrad | 0:cdf462088d13 | 139 | ctx.len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 ); |
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 | msg_len = unhexify( message_str, message_hex_string ); |
markrad | 0:cdf462088d13 | 157 | |
markrad | 0:cdf462088d13 | 158 | if( mbedtls_md_info_from_type( digest ) != NULL ) |
markrad | 0:cdf462088d13 | 159 | TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 ); |
markrad | 0:cdf462088d13 | 160 | |
markrad | 0:cdf462088d13 | 161 | TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_buffer_rand, &info, MBEDTLS_RSA_PRIVATE, digest, 0, hash_result, output ) == result ); |
markrad | 0:cdf462088d13 | 162 | if( result == 0 ) |
markrad | 0:cdf462088d13 | 163 | { |
markrad | 0:cdf462088d13 | 164 | hexify( output_str, output, ctx.len); |
markrad | 0:cdf462088d13 | 165 | |
markrad | 0:cdf462088d13 | 166 | TEST_ASSERT( strcasecmp( (char *) output_str, result_hex_str ) == 0 ); |
markrad | 0:cdf462088d13 | 167 | } |
markrad | 0:cdf462088d13 | 168 | |
markrad | 0:cdf462088d13 | 169 | exit: |
markrad | 0:cdf462088d13 | 170 | mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G ); |
markrad | 0:cdf462088d13 | 171 | mbedtls_rsa_free( &ctx ); |
markrad | 0:cdf462088d13 | 172 | } |
markrad | 0:cdf462088d13 | 173 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 174 | |
markrad | 0:cdf462088d13 | 175 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 176 | void pkcs1_rsassa_v15_verify( int mod, int radix_N, char *input_N, int radix_E, |
markrad | 0:cdf462088d13 | 177 | char *input_E, int digest, int hash, |
markrad | 0:cdf462088d13 | 178 | char *message_hex_string, char *salt, |
markrad | 0:cdf462088d13 | 179 | char *result_hex_str, int result ) |
markrad | 0:cdf462088d13 | 180 | { |
markrad | 0:cdf462088d13 | 181 | unsigned char message_str[1000]; |
markrad | 0:cdf462088d13 | 182 | unsigned char hash_result[1000]; |
markrad | 0:cdf462088d13 | 183 | unsigned char result_str[1000]; |
markrad | 0:cdf462088d13 | 184 | mbedtls_rsa_context ctx; |
markrad | 0:cdf462088d13 | 185 | size_t msg_len; |
markrad | 0:cdf462088d13 | 186 | ((void) salt); |
markrad | 0:cdf462088d13 | 187 | |
markrad | 0:cdf462088d13 | 188 | mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, hash ); |
markrad | 0:cdf462088d13 | 189 | memset( message_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 190 | memset( hash_result, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 191 | memset( result_str, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 192 | |
markrad | 0:cdf462088d13 | 193 | ctx.len = mod / 8 + ( ( mod % 8 ) ? 1 : 0 ); |
markrad | 0:cdf462088d13 | 194 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 ); |
markrad | 0:cdf462088d13 | 195 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 ); |
markrad | 0:cdf462088d13 | 196 | |
markrad | 0:cdf462088d13 | 197 | TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 ); |
markrad | 0:cdf462088d13 | 198 | |
markrad | 0:cdf462088d13 | 199 | msg_len = unhexify( message_str, message_hex_string ); |
markrad | 0:cdf462088d13 | 200 | unhexify( result_str, result_hex_str ); |
markrad | 0:cdf462088d13 | 201 | |
markrad | 0:cdf462088d13 | 202 | if( mbedtls_md_info_from_type( digest ) != NULL ) |
markrad | 0:cdf462088d13 | 203 | TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 ); |
markrad | 0:cdf462088d13 | 204 | |
markrad | 0:cdf462088d13 | 205 | TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, digest, 0, hash_result, result_str ) == result ); |
markrad | 0:cdf462088d13 | 206 | |
markrad | 0:cdf462088d13 | 207 | exit: |
markrad | 0:cdf462088d13 | 208 | mbedtls_rsa_free( &ctx ); |
markrad | 0:cdf462088d13 | 209 | } |
markrad | 0:cdf462088d13 | 210 | /* END_CASE */ |