mbed TLS Build

Dependents:   Slave-prot-prod

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?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /* BEGIN_HEADER */
markrad 0:cdf462088d13 2 #include "mbedtls/pk.h"
markrad 0:cdf462088d13 3
markrad 0:cdf462088d13 4 /* For error codes */
markrad 0:cdf462088d13 5 #include "mbedtls/ecp.h"
markrad 0:cdf462088d13 6 #include "mbedtls/rsa.h"
markrad 0:cdf462088d13 7
markrad 0:cdf462088d13 8 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len );
markrad 0:cdf462088d13 9
markrad 0:cdf462088d13 10 #define RSA_KEY_SIZE 512
markrad 0:cdf462088d13 11 #define RSA_KEY_LEN 64
markrad 0:cdf462088d13 12
markrad 0:cdf462088d13 13 static int pk_genkey( mbedtls_pk_context *pk )
markrad 0:cdf462088d13 14 {
markrad 0:cdf462088d13 15 ((void) pk);
markrad 0:cdf462088d13 16
markrad 0:cdf462088d13 17 #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
markrad 0:cdf462088d13 18 if( mbedtls_pk_get_type( pk ) == MBEDTLS_PK_RSA )
markrad 0:cdf462088d13 19 return mbedtls_rsa_gen_key( mbedtls_pk_rsa( *pk ), rnd_std_rand, NULL, RSA_KEY_SIZE, 3 );
markrad 0:cdf462088d13 20 #endif
markrad 0:cdf462088d13 21 #if defined(MBEDTLS_ECP_C)
markrad 0:cdf462088d13 22 if( mbedtls_pk_get_type( pk ) == MBEDTLS_PK_ECKEY ||
markrad 0:cdf462088d13 23 mbedtls_pk_get_type( pk ) == MBEDTLS_PK_ECKEY_DH ||
markrad 0:cdf462088d13 24 mbedtls_pk_get_type( pk ) == MBEDTLS_PK_ECDSA )
markrad 0:cdf462088d13 25 {
markrad 0:cdf462088d13 26 int ret;
markrad 0:cdf462088d13 27 if( ( ret = mbedtls_ecp_group_load( &mbedtls_pk_ec( *pk )->grp,
markrad 0:cdf462088d13 28 MBEDTLS_ECP_DP_SECP192R1 ) ) != 0 )
markrad 0:cdf462088d13 29 return( ret );
markrad 0:cdf462088d13 30
markrad 0:cdf462088d13 31 return mbedtls_ecp_gen_keypair( &mbedtls_pk_ec( *pk )->grp, &mbedtls_pk_ec( *pk )->d,
markrad 0:cdf462088d13 32 &mbedtls_pk_ec( *pk )->Q, rnd_std_rand, NULL );
markrad 0:cdf462088d13 33 }
markrad 0:cdf462088d13 34 #endif
markrad 0:cdf462088d13 35 return( -1 );
markrad 0:cdf462088d13 36 }
markrad 0:cdf462088d13 37
markrad 0:cdf462088d13 38 #if defined(MBEDTLS_RSA_C)
markrad 0:cdf462088d13 39 int mbedtls_rsa_decrypt_func( void *ctx, int mode, size_t *olen,
markrad 0:cdf462088d13 40 const unsigned char *input, unsigned char *output,
markrad 0:cdf462088d13 41 size_t output_max_len )
markrad 0:cdf462088d13 42 {
markrad 0:cdf462088d13 43 return( mbedtls_rsa_pkcs1_decrypt( (mbedtls_rsa_context *) ctx, NULL, NULL, mode, olen,
markrad 0:cdf462088d13 44 input, output, output_max_len ) );
markrad 0:cdf462088d13 45 }
markrad 0:cdf462088d13 46 int mbedtls_rsa_sign_func( void *ctx,
markrad 0:cdf462088d13 47 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
markrad 0:cdf462088d13 48 int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
markrad 0:cdf462088d13 49 const unsigned char *hash, unsigned char *sig )
markrad 0:cdf462088d13 50 {
markrad 0:cdf462088d13 51 return( mbedtls_rsa_pkcs1_sign( (mbedtls_rsa_context *) ctx, f_rng, p_rng, mode,
markrad 0:cdf462088d13 52 md_alg, hashlen, hash, sig ) );
markrad 0:cdf462088d13 53 }
markrad 0:cdf462088d13 54 size_t mbedtls_rsa_key_len_func( void *ctx )
markrad 0:cdf462088d13 55 {
markrad 0:cdf462088d13 56 return( ((const mbedtls_rsa_context *) ctx)->len );
markrad 0:cdf462088d13 57 }
markrad 0:cdf462088d13 58 #endif /* MBEDTLS_RSA_C */
markrad 0:cdf462088d13 59 /* END_HEADER */
markrad 0:cdf462088d13 60
markrad 0:cdf462088d13 61 /* BEGIN_DEPENDENCIES
markrad 0:cdf462088d13 62 * depends_on:MBEDTLS_PK_C
markrad 0:cdf462088d13 63 * END_DEPENDENCIES
markrad 0:cdf462088d13 64 */
markrad 0:cdf462088d13 65
markrad 0:cdf462088d13 66 /* BEGIN_CASE */
markrad 0:cdf462088d13 67 void pk_utils( int type, int size, int len, char *name )
markrad 0:cdf462088d13 68 {
markrad 0:cdf462088d13 69 mbedtls_pk_context pk;
markrad 0:cdf462088d13 70
markrad 0:cdf462088d13 71 mbedtls_pk_init( &pk );
markrad 0:cdf462088d13 72
markrad 0:cdf462088d13 73 TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( type ) ) == 0 );
markrad 0:cdf462088d13 74 TEST_ASSERT( pk_genkey( &pk ) == 0 );
markrad 0:cdf462088d13 75
markrad 0:cdf462088d13 76 TEST_ASSERT( (int) mbedtls_pk_get_type( &pk ) == type );
markrad 0:cdf462088d13 77 TEST_ASSERT( mbedtls_pk_can_do( &pk, type ) );
markrad 0:cdf462088d13 78 TEST_ASSERT( mbedtls_pk_get_bitlen( &pk ) == (unsigned) size );
markrad 0:cdf462088d13 79 TEST_ASSERT( mbedtls_pk_get_len( &pk ) == (unsigned) len );
markrad 0:cdf462088d13 80 TEST_ASSERT( strcmp( mbedtls_pk_get_name( &pk), name ) == 0 );
markrad 0:cdf462088d13 81
markrad 0:cdf462088d13 82 exit:
markrad 0:cdf462088d13 83 mbedtls_pk_free( &pk );
markrad 0:cdf462088d13 84 }
markrad 0:cdf462088d13 85 /* END_CASE */
markrad 0:cdf462088d13 86
markrad 0:cdf462088d13 87 /* BEGIN_CASE depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_FS_IO */
markrad 0:cdf462088d13 88 void mbedtls_pk_check_pair( char *pub_file, char *prv_file, int ret )
markrad 0:cdf462088d13 89 {
markrad 0:cdf462088d13 90 mbedtls_pk_context pub, prv, alt;
markrad 0:cdf462088d13 91
markrad 0:cdf462088d13 92 mbedtls_pk_init( &pub );
markrad 0:cdf462088d13 93 mbedtls_pk_init( &prv );
markrad 0:cdf462088d13 94 mbedtls_pk_init( &alt );
markrad 0:cdf462088d13 95
markrad 0:cdf462088d13 96 TEST_ASSERT( mbedtls_pk_parse_public_keyfile( &pub, pub_file ) == 0 );
markrad 0:cdf462088d13 97 TEST_ASSERT( mbedtls_pk_parse_keyfile( &prv, prv_file, NULL ) == 0 );
markrad 0:cdf462088d13 98
markrad 0:cdf462088d13 99 TEST_ASSERT( mbedtls_pk_check_pair( &pub, &prv ) == ret );
markrad 0:cdf462088d13 100
markrad 0:cdf462088d13 101 #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
markrad 0:cdf462088d13 102 if( mbedtls_pk_get_type( &prv ) == MBEDTLS_PK_RSA )
markrad 0:cdf462088d13 103 {
markrad 0:cdf462088d13 104 TEST_ASSERT( mbedtls_pk_setup_rsa_alt( &alt, mbedtls_pk_rsa( prv ),
markrad 0:cdf462088d13 105 mbedtls_rsa_decrypt_func, mbedtls_rsa_sign_func, mbedtls_rsa_key_len_func ) == 0 );
markrad 0:cdf462088d13 106 TEST_ASSERT( mbedtls_pk_check_pair( &pub, &alt ) == ret );
markrad 0:cdf462088d13 107 }
markrad 0:cdf462088d13 108 #endif
markrad 0:cdf462088d13 109
markrad 0:cdf462088d13 110 mbedtls_pk_free( &pub );
markrad 0:cdf462088d13 111 mbedtls_pk_free( &prv );
markrad 0:cdf462088d13 112 mbedtls_pk_free( &alt );
markrad 0:cdf462088d13 113 }
markrad 0:cdf462088d13 114 /* END_CASE */
markrad 0:cdf462088d13 115
markrad 0:cdf462088d13 116 /* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
markrad 0:cdf462088d13 117 void pk_rsa_verify_test_vec( char *message_hex_string, int digest,
markrad 0:cdf462088d13 118 int mod, int radix_N, char *input_N, int radix_E,
markrad 0:cdf462088d13 119 char *input_E, char *result_hex_str, int result )
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 result_str[1000];
markrad 0:cdf462088d13 124 mbedtls_rsa_context *rsa;
markrad 0:cdf462088d13 125 mbedtls_pk_context pk;
markrad 0:cdf462088d13 126 int msg_len;
markrad 0:cdf462088d13 127
markrad 0:cdf462088d13 128 mbedtls_pk_init( &pk );
markrad 0:cdf462088d13 129
markrad 0:cdf462088d13 130 memset( message_str, 0x00, 1000 );
markrad 0:cdf462088d13 131 memset( hash_result, 0x00, 1000 );
markrad 0:cdf462088d13 132 memset( result_str, 0x00, 1000 );
markrad 0:cdf462088d13 133
markrad 0:cdf462088d13 134 TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
markrad 0:cdf462088d13 135 rsa = mbedtls_pk_rsa( pk );
markrad 0:cdf462088d13 136
markrad 0:cdf462088d13 137 rsa->len = mod / 8;
markrad 0:cdf462088d13 138 TEST_ASSERT( mbedtls_mpi_read_string( &rsa->N, radix_N, input_N ) == 0 );
markrad 0:cdf462088d13 139 TEST_ASSERT( mbedtls_mpi_read_string( &rsa->E, radix_E, input_E ) == 0 );
markrad 0:cdf462088d13 140
markrad 0:cdf462088d13 141 msg_len = unhexify( message_str, message_hex_string );
markrad 0:cdf462088d13 142 unhexify( result_str, result_hex_str );
markrad 0:cdf462088d13 143
markrad 0:cdf462088d13 144 if( mbedtls_md_info_from_type( digest ) != NULL )
markrad 0:cdf462088d13 145 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str, msg_len, hash_result ) == 0 );
markrad 0:cdf462088d13 146
markrad 0:cdf462088d13 147 TEST_ASSERT( mbedtls_pk_verify( &pk, digest, hash_result, 0,
markrad 0:cdf462088d13 148 result_str, mbedtls_pk_get_len( &pk ) ) == result );
markrad 0:cdf462088d13 149
markrad 0:cdf462088d13 150 exit:
markrad 0:cdf462088d13 151 mbedtls_pk_free( &pk );
markrad 0:cdf462088d13 152 }
markrad 0:cdf462088d13 153 /* END_CASE */
markrad 0:cdf462088d13 154
markrad 0:cdf462088d13 155 /* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
markrad 0:cdf462088d13 156 void pk_rsa_verify_ext_test_vec( char *message_hex_string, int digest,
markrad 0:cdf462088d13 157 int mod, int radix_N, char *input_N, int radix_E,
markrad 0:cdf462088d13 158 char *input_E, char *result_hex_str,
markrad 0:cdf462088d13 159 int pk_type, int mgf1_hash_id, int salt_len,
markrad 0:cdf462088d13 160 int result )
markrad 0:cdf462088d13 161 {
markrad 0:cdf462088d13 162 unsigned char message_str[1000];
markrad 0:cdf462088d13 163 unsigned char hash_result[1000];
markrad 0:cdf462088d13 164 unsigned char result_str[1000];
markrad 0:cdf462088d13 165 mbedtls_rsa_context *rsa;
markrad 0:cdf462088d13 166 mbedtls_pk_context pk;
markrad 0:cdf462088d13 167 mbedtls_pk_rsassa_pss_options pss_opts;
markrad 0:cdf462088d13 168 void *options;
markrad 0:cdf462088d13 169 int msg_len;
markrad 0:cdf462088d13 170 size_t hash_len;
markrad 0:cdf462088d13 171
markrad 0:cdf462088d13 172 mbedtls_pk_init( &pk );
markrad 0:cdf462088d13 173
markrad 0:cdf462088d13 174 memset( message_str, 0x00, 1000 );
markrad 0:cdf462088d13 175 memset( hash_result, 0x00, 1000 );
markrad 0:cdf462088d13 176 memset( result_str, 0x00, 1000 );
markrad 0:cdf462088d13 177
markrad 0:cdf462088d13 178 TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
markrad 0:cdf462088d13 179 rsa = mbedtls_pk_rsa( pk );
markrad 0:cdf462088d13 180
markrad 0:cdf462088d13 181 rsa->len = mod / 8;
markrad 0:cdf462088d13 182 TEST_ASSERT( mbedtls_mpi_read_string( &rsa->N, radix_N, input_N ) == 0 );
markrad 0:cdf462088d13 183 TEST_ASSERT( mbedtls_mpi_read_string( &rsa->E, radix_E, input_E ) == 0 );
markrad 0:cdf462088d13 184
markrad 0:cdf462088d13 185 msg_len = unhexify( message_str, message_hex_string );
markrad 0:cdf462088d13 186 unhexify( result_str, result_hex_str );
markrad 0:cdf462088d13 187
markrad 0:cdf462088d13 188 if( digest != MBEDTLS_MD_NONE )
markrad 0:cdf462088d13 189 {
markrad 0:cdf462088d13 190 TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ),
markrad 0:cdf462088d13 191 message_str, msg_len, hash_result ) == 0 );
markrad 0:cdf462088d13 192 hash_len = 0;
markrad 0:cdf462088d13 193 }
markrad 0:cdf462088d13 194 else
markrad 0:cdf462088d13 195 {
markrad 0:cdf462088d13 196 memcpy( hash_result, message_str, msg_len );
markrad 0:cdf462088d13 197 hash_len = msg_len;
markrad 0:cdf462088d13 198 }
markrad 0:cdf462088d13 199
markrad 0:cdf462088d13 200 if( mgf1_hash_id < 0 )
markrad 0:cdf462088d13 201 {
markrad 0:cdf462088d13 202 options = NULL;
markrad 0:cdf462088d13 203 }
markrad 0:cdf462088d13 204 else
markrad 0:cdf462088d13 205 {
markrad 0:cdf462088d13 206 options = &pss_opts;
markrad 0:cdf462088d13 207
markrad 0:cdf462088d13 208 pss_opts.mgf1_hash_id = mgf1_hash_id;
markrad 0:cdf462088d13 209 pss_opts.expected_salt_len = salt_len;
markrad 0:cdf462088d13 210 }
markrad 0:cdf462088d13 211
markrad 0:cdf462088d13 212 TEST_ASSERT( mbedtls_pk_verify_ext( pk_type, options, &pk,
markrad 0:cdf462088d13 213 digest, hash_result, hash_len,
markrad 0:cdf462088d13 214 result_str, mbedtls_pk_get_len( &pk ) ) == result );
markrad 0:cdf462088d13 215
markrad 0:cdf462088d13 216 exit:
markrad 0:cdf462088d13 217 mbedtls_pk_free( &pk );
markrad 0:cdf462088d13 218 }
markrad 0:cdf462088d13 219 /* END_CASE */
markrad 0:cdf462088d13 220
markrad 0:cdf462088d13 221 /* BEGIN_CASE depends_on:MBEDTLS_ECDSA_C */
markrad 0:cdf462088d13 222 void pk_ec_test_vec( int type, int id, char *key_str,
markrad 0:cdf462088d13 223 char *hash_str, char * sig_str, int ret )
markrad 0:cdf462088d13 224 {
markrad 0:cdf462088d13 225 mbedtls_pk_context pk;
markrad 0:cdf462088d13 226 mbedtls_ecp_keypair *eckey;
markrad 0:cdf462088d13 227 unsigned char hash[100], sig[500], key[500];
markrad 0:cdf462088d13 228 size_t hash_len, sig_len, key_len;
markrad 0:cdf462088d13 229
markrad 0:cdf462088d13 230 mbedtls_pk_init( &pk );
markrad 0:cdf462088d13 231
markrad 0:cdf462088d13 232 memset( hash, 0, sizeof( hash ) ); hash_len = unhexify(hash, hash_str);
markrad 0:cdf462088d13 233 memset( sig, 0, sizeof( sig ) ); sig_len = unhexify(sig, sig_str);
markrad 0:cdf462088d13 234 memset( key, 0, sizeof( key ) ); key_len = unhexify(key, key_str);
markrad 0:cdf462088d13 235
markrad 0:cdf462088d13 236 TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( type ) ) == 0 );
markrad 0:cdf462088d13 237
markrad 0:cdf462088d13 238 TEST_ASSERT( mbedtls_pk_can_do( &pk, MBEDTLS_PK_ECDSA ) );
markrad 0:cdf462088d13 239 eckey = mbedtls_pk_ec( pk );
markrad 0:cdf462088d13 240
markrad 0:cdf462088d13 241 TEST_ASSERT( mbedtls_ecp_group_load( &eckey->grp, id ) == 0 );
markrad 0:cdf462088d13 242 TEST_ASSERT( mbedtls_ecp_point_read_binary( &eckey->grp, &eckey->Q,
markrad 0:cdf462088d13 243 key, key_len ) == 0 );
markrad 0:cdf462088d13 244
markrad 0:cdf462088d13 245 TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_NONE,
markrad 0:cdf462088d13 246 hash, hash_len, sig, sig_len ) == ret );
markrad 0:cdf462088d13 247
markrad 0:cdf462088d13 248 exit:
markrad 0:cdf462088d13 249 mbedtls_pk_free( &pk );
markrad 0:cdf462088d13 250 }
markrad 0:cdf462088d13 251 /* END_CASE */
markrad 0:cdf462088d13 252
markrad 0:cdf462088d13 253 /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
markrad 0:cdf462088d13 254 void pk_sign_verify( int type, int sign_ret, int verify_ret )
markrad 0:cdf462088d13 255 {
markrad 0:cdf462088d13 256 mbedtls_pk_context pk;
markrad 0:cdf462088d13 257 unsigned char hash[50], sig[5000];
markrad 0:cdf462088d13 258 size_t sig_len;
markrad 0:cdf462088d13 259
markrad 0:cdf462088d13 260 mbedtls_pk_init( &pk );
markrad 0:cdf462088d13 261
markrad 0:cdf462088d13 262 memset( hash, 0x2a, sizeof hash );
markrad 0:cdf462088d13 263 memset( sig, 0, sizeof sig );
markrad 0:cdf462088d13 264
markrad 0:cdf462088d13 265 TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( type ) ) == 0 );
markrad 0:cdf462088d13 266 TEST_ASSERT( pk_genkey( &pk ) == 0 );
markrad 0:cdf462088d13 267
markrad 0:cdf462088d13 268 TEST_ASSERT( mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256, hash, sizeof hash,
markrad 0:cdf462088d13 269 sig, &sig_len, rnd_std_rand, NULL ) == sign_ret );
markrad 0:cdf462088d13 270
markrad 0:cdf462088d13 271 TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256,
markrad 0:cdf462088d13 272 hash, sizeof hash, sig, sig_len ) == verify_ret );
markrad 0:cdf462088d13 273
markrad 0:cdf462088d13 274 exit:
markrad 0:cdf462088d13 275 mbedtls_pk_free( &pk );
markrad 0:cdf462088d13 276 }
markrad 0:cdf462088d13 277 /* END_CASE */
markrad 0:cdf462088d13 278
markrad 0:cdf462088d13 279 /* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
markrad 0:cdf462088d13 280 void pk_rsa_encrypt_test_vec( char *message_hex, int mod,
markrad 0:cdf462088d13 281 int radix_N, char *input_N,
markrad 0:cdf462088d13 282 int radix_E, char *input_E,
markrad 0:cdf462088d13 283 char *result_hex, int ret )
markrad 0:cdf462088d13 284 {
markrad 0:cdf462088d13 285 unsigned char message[1000];
markrad 0:cdf462088d13 286 unsigned char output[1000];
markrad 0:cdf462088d13 287 unsigned char result[1000];
markrad 0:cdf462088d13 288 size_t msg_len, olen, res_len;
markrad 0:cdf462088d13 289 rnd_pseudo_info rnd_info;
markrad 0:cdf462088d13 290 mbedtls_rsa_context *rsa;
markrad 0:cdf462088d13 291 mbedtls_pk_context pk;
markrad 0:cdf462088d13 292
markrad 0:cdf462088d13 293 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
markrad 0:cdf462088d13 294 memset( message, 0, sizeof( message ) );
markrad 0:cdf462088d13 295 memset( output, 0, sizeof( output ) );
markrad 0:cdf462088d13 296 memset( result, 0, sizeof( result ) );
markrad 0:cdf462088d13 297
markrad 0:cdf462088d13 298 msg_len = unhexify( message, message_hex );
markrad 0:cdf462088d13 299 res_len = unhexify( result, result_hex );
markrad 0:cdf462088d13 300
markrad 0:cdf462088d13 301 mbedtls_pk_init( &pk );
markrad 0:cdf462088d13 302 TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
markrad 0:cdf462088d13 303 rsa = mbedtls_pk_rsa( pk );
markrad 0:cdf462088d13 304
markrad 0:cdf462088d13 305 rsa->len = mod / 8;
markrad 0:cdf462088d13 306 TEST_ASSERT( mbedtls_mpi_read_string( &rsa->N, radix_N, input_N ) == 0 );
markrad 0:cdf462088d13 307 TEST_ASSERT( mbedtls_mpi_read_string( &rsa->E, radix_E, input_E ) == 0 );
markrad 0:cdf462088d13 308
markrad 0:cdf462088d13 309 TEST_ASSERT( mbedtls_pk_encrypt( &pk, message, msg_len,
markrad 0:cdf462088d13 310 output, &olen, sizeof( output ),
markrad 0:cdf462088d13 311 rnd_pseudo_rand, &rnd_info ) == ret );
markrad 0:cdf462088d13 312 TEST_ASSERT( olen == res_len );
markrad 0:cdf462088d13 313 TEST_ASSERT( memcmp( output, result, olen ) == 0 );
markrad 0:cdf462088d13 314
markrad 0:cdf462088d13 315 exit:
markrad 0:cdf462088d13 316 mbedtls_pk_free( &pk );
markrad 0:cdf462088d13 317 }
markrad 0:cdf462088d13 318 /* END_CASE */
markrad 0:cdf462088d13 319
markrad 0:cdf462088d13 320 /* BEGIN_CASE depends_on:MBEDTLS_RSA_C */
markrad 0:cdf462088d13 321 void pk_rsa_decrypt_test_vec( char *cipher_hex, int mod,
markrad 0:cdf462088d13 322 int radix_P, char *input_P,
markrad 0:cdf462088d13 323 int radix_Q, char *input_Q,
markrad 0:cdf462088d13 324 int radix_N, char *input_N,
markrad 0:cdf462088d13 325 int radix_E, char *input_E,
markrad 0:cdf462088d13 326 char *clear_hex, int ret )
markrad 0:cdf462088d13 327 {
markrad 0:cdf462088d13 328 unsigned char clear[1000];
markrad 0:cdf462088d13 329 unsigned char output[1000];
markrad 0:cdf462088d13 330 unsigned char cipher[1000];
markrad 0:cdf462088d13 331 size_t clear_len, olen, cipher_len;
markrad 0:cdf462088d13 332 rnd_pseudo_info rnd_info;
markrad 0:cdf462088d13 333 mbedtls_mpi P1, Q1, H, G;
markrad 0:cdf462088d13 334 mbedtls_rsa_context *rsa;
markrad 0:cdf462088d13 335 mbedtls_pk_context pk;
markrad 0:cdf462088d13 336
markrad 0:cdf462088d13 337 mbedtls_pk_init( &pk );
markrad 0:cdf462088d13 338 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
markrad 0:cdf462088d13 339
markrad 0:cdf462088d13 340 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
markrad 0:cdf462088d13 341 memset( clear, 0, sizeof( clear ) );
markrad 0:cdf462088d13 342 memset( cipher, 0, sizeof( cipher ) );
markrad 0:cdf462088d13 343
markrad 0:cdf462088d13 344 clear_len = unhexify( clear, clear_hex );
markrad 0:cdf462088d13 345 cipher_len = unhexify( cipher, cipher_hex );
markrad 0:cdf462088d13 346
markrad 0:cdf462088d13 347 /* init pk-rsa context */
markrad 0:cdf462088d13 348 TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
markrad 0:cdf462088d13 349 rsa = mbedtls_pk_rsa( pk );
markrad 0:cdf462088d13 350
markrad 0:cdf462088d13 351 /* load public key */
markrad 0:cdf462088d13 352 rsa->len = mod / 8;
markrad 0:cdf462088d13 353 TEST_ASSERT( mbedtls_mpi_read_string( &rsa->N, radix_N, input_N ) == 0 );
markrad 0:cdf462088d13 354 TEST_ASSERT( mbedtls_mpi_read_string( &rsa->E, radix_E, input_E ) == 0 );
markrad 0:cdf462088d13 355
markrad 0:cdf462088d13 356 /* load private key */
markrad 0:cdf462088d13 357 TEST_ASSERT( mbedtls_mpi_read_string( &rsa->P, radix_P, input_P ) == 0 );
markrad 0:cdf462088d13 358 TEST_ASSERT( mbedtls_mpi_read_string( &rsa->Q, radix_Q, input_Q ) == 0 );
markrad 0:cdf462088d13 359 TEST_ASSERT( mbedtls_mpi_sub_int( &P1, &rsa->P, 1 ) == 0 );
markrad 0:cdf462088d13 360 TEST_ASSERT( mbedtls_mpi_sub_int( &Q1, &rsa->Q, 1 ) == 0 );
markrad 0:cdf462088d13 361 TEST_ASSERT( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
markrad 0:cdf462088d13 362 TEST_ASSERT( mbedtls_mpi_gcd( &G, &rsa->E, &H ) == 0 );
markrad 0:cdf462088d13 363 TEST_ASSERT( mbedtls_mpi_inv_mod( &rsa->D , &rsa->E, &H ) == 0 );
markrad 0:cdf462088d13 364 TEST_ASSERT( mbedtls_mpi_mod_mpi( &rsa->DP, &rsa->D, &P1 ) == 0 );
markrad 0:cdf462088d13 365 TEST_ASSERT( mbedtls_mpi_mod_mpi( &rsa->DQ, &rsa->D, &Q1 ) == 0 );
markrad 0:cdf462088d13 366 TEST_ASSERT( mbedtls_mpi_inv_mod( &rsa->QP, &rsa->Q, &rsa->P ) == 0 );
markrad 0:cdf462088d13 367
markrad 0:cdf462088d13 368 /* decryption test */
markrad 0:cdf462088d13 369 memset( output, 0, sizeof( output ) );
markrad 0:cdf462088d13 370 olen = 0;
markrad 0:cdf462088d13 371 TEST_ASSERT( mbedtls_pk_decrypt( &pk, cipher, cipher_len,
markrad 0:cdf462088d13 372 output, &olen, sizeof( output ),
markrad 0:cdf462088d13 373 rnd_pseudo_rand, &rnd_info ) == ret );
markrad 0:cdf462088d13 374 if( ret == 0 )
markrad 0:cdf462088d13 375 {
markrad 0:cdf462088d13 376 TEST_ASSERT( olen == clear_len );
markrad 0:cdf462088d13 377 TEST_ASSERT( memcmp( output, clear, olen ) == 0 );
markrad 0:cdf462088d13 378 }
markrad 0:cdf462088d13 379
markrad 0:cdf462088d13 380 exit:
markrad 0:cdf462088d13 381 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
markrad 0:cdf462088d13 382 mbedtls_pk_free( &pk );
markrad 0:cdf462088d13 383 }
markrad 0:cdf462088d13 384 /* END_CASE */
markrad 0:cdf462088d13 385
markrad 0:cdf462088d13 386 /* BEGIN_CASE */
markrad 0:cdf462088d13 387 void pk_ec_nocrypt( int type )
markrad 0:cdf462088d13 388 {
markrad 0:cdf462088d13 389 mbedtls_pk_context pk;
markrad 0:cdf462088d13 390 unsigned char output[100];
markrad 0:cdf462088d13 391 unsigned char input[100];
markrad 0:cdf462088d13 392 rnd_pseudo_info rnd_info;
markrad 0:cdf462088d13 393 size_t olen = 0;
markrad 0:cdf462088d13 394 int ret = MBEDTLS_ERR_PK_TYPE_MISMATCH;
markrad 0:cdf462088d13 395
markrad 0:cdf462088d13 396 mbedtls_pk_init( &pk );
markrad 0:cdf462088d13 397
markrad 0:cdf462088d13 398 memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
markrad 0:cdf462088d13 399 memset( output, 0, sizeof( output ) );
markrad 0:cdf462088d13 400 memset( input, 0, sizeof( input ) );
markrad 0:cdf462088d13 401
markrad 0:cdf462088d13 402 TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( type ) ) == 0 );
markrad 0:cdf462088d13 403
markrad 0:cdf462088d13 404 TEST_ASSERT( mbedtls_pk_encrypt( &pk, input, sizeof( input ),
markrad 0:cdf462088d13 405 output, &olen, sizeof( output ),
markrad 0:cdf462088d13 406 rnd_pseudo_rand, &rnd_info ) == ret );
markrad 0:cdf462088d13 407
markrad 0:cdf462088d13 408 TEST_ASSERT( mbedtls_pk_decrypt( &pk, input, sizeof( input ),
markrad 0:cdf462088d13 409 output, &olen, sizeof( output ),
markrad 0:cdf462088d13 410 rnd_pseudo_rand, &rnd_info ) == ret );
markrad 0:cdf462088d13 411
markrad 0:cdf462088d13 412 exit:
markrad 0:cdf462088d13 413 mbedtls_pk_free( &pk );
markrad 0:cdf462088d13 414 }
markrad 0:cdf462088d13 415 /* END_CASE */
markrad 0:cdf462088d13 416
markrad 0:cdf462088d13 417 /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_PK_RSA_ALT_SUPPORT */
markrad 0:cdf462088d13 418 void pk_rsa_alt( )
markrad 0:cdf462088d13 419 {
markrad 0:cdf462088d13 420 /*
markrad 0:cdf462088d13 421 * An rsa_alt context can only do private operations (decrypt, sign).
markrad 0:cdf462088d13 422 * Test it against the public operations (encrypt, verify) of a
markrad 0:cdf462088d13 423 * corresponding rsa context.
markrad 0:cdf462088d13 424 */
markrad 0:cdf462088d13 425 mbedtls_rsa_context raw;
markrad 0:cdf462088d13 426 mbedtls_pk_context rsa, alt;
markrad 0:cdf462088d13 427 mbedtls_pk_debug_item dbg_items[10];
markrad 0:cdf462088d13 428 unsigned char hash[50], sig[1000];
markrad 0:cdf462088d13 429 unsigned char msg[50], ciph[1000], test[1000];
markrad 0:cdf462088d13 430 size_t sig_len, ciph_len, test_len;
markrad 0:cdf462088d13 431 int ret = MBEDTLS_ERR_PK_TYPE_MISMATCH;
markrad 0:cdf462088d13 432
markrad 0:cdf462088d13 433 mbedtls_rsa_init( &raw, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
markrad 0:cdf462088d13 434 mbedtls_pk_init( &rsa ); mbedtls_pk_init( &alt );
markrad 0:cdf462088d13 435
markrad 0:cdf462088d13 436 memset( hash, 0x2a, sizeof hash );
markrad 0:cdf462088d13 437 memset( sig, 0, sizeof sig );
markrad 0:cdf462088d13 438 memset( msg, 0x2a, sizeof msg );
markrad 0:cdf462088d13 439 memset( ciph, 0, sizeof ciph );
markrad 0:cdf462088d13 440 memset( test, 0, sizeof test );
markrad 0:cdf462088d13 441
markrad 0:cdf462088d13 442 /* Initiliaze PK RSA context with random key */
markrad 0:cdf462088d13 443 TEST_ASSERT( mbedtls_pk_setup( &rsa,
markrad 0:cdf462088d13 444 mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 );
markrad 0:cdf462088d13 445 TEST_ASSERT( pk_genkey( &rsa ) == 0 );
markrad 0:cdf462088d13 446
markrad 0:cdf462088d13 447 /* Extract key to the raw rsa context */
markrad 0:cdf462088d13 448 TEST_ASSERT( mbedtls_rsa_copy( &raw, mbedtls_pk_rsa( rsa ) ) == 0 );
markrad 0:cdf462088d13 449
markrad 0:cdf462088d13 450 /* Initialize PK RSA_ALT context */
markrad 0:cdf462088d13 451 TEST_ASSERT( mbedtls_pk_setup_rsa_alt( &alt, (void *) &raw,
markrad 0:cdf462088d13 452 mbedtls_rsa_decrypt_func, mbedtls_rsa_sign_func, mbedtls_rsa_key_len_func ) == 0 );
markrad 0:cdf462088d13 453
markrad 0:cdf462088d13 454 /* Test administrative functions */
markrad 0:cdf462088d13 455 TEST_ASSERT( mbedtls_pk_can_do( &alt, MBEDTLS_PK_RSA ) );
markrad 0:cdf462088d13 456 TEST_ASSERT( mbedtls_pk_get_bitlen( &alt ) == RSA_KEY_SIZE );
markrad 0:cdf462088d13 457 TEST_ASSERT( mbedtls_pk_get_len( &alt ) == RSA_KEY_LEN );
markrad 0:cdf462088d13 458 TEST_ASSERT( mbedtls_pk_get_type( &alt ) == MBEDTLS_PK_RSA_ALT );
markrad 0:cdf462088d13 459 TEST_ASSERT( strcmp( mbedtls_pk_get_name( &alt ), "RSA-alt" ) == 0 );
markrad 0:cdf462088d13 460
markrad 0:cdf462088d13 461 /* Test signature */
markrad 0:cdf462088d13 462 TEST_ASSERT( mbedtls_pk_sign( &alt, MBEDTLS_MD_NONE, hash, sizeof hash,
markrad 0:cdf462088d13 463 sig, &sig_len, rnd_std_rand, NULL ) == 0 );
markrad 0:cdf462088d13 464 TEST_ASSERT( sig_len == RSA_KEY_LEN );
markrad 0:cdf462088d13 465 TEST_ASSERT( mbedtls_pk_verify( &rsa, MBEDTLS_MD_NONE,
markrad 0:cdf462088d13 466 hash, sizeof hash, sig, sig_len ) == 0 );
markrad 0:cdf462088d13 467
markrad 0:cdf462088d13 468 /* Test decrypt */
markrad 0:cdf462088d13 469 TEST_ASSERT( mbedtls_pk_encrypt( &rsa, msg, sizeof msg,
markrad 0:cdf462088d13 470 ciph, &ciph_len, sizeof ciph,
markrad 0:cdf462088d13 471 rnd_std_rand, NULL ) == 0 );
markrad 0:cdf462088d13 472 TEST_ASSERT( mbedtls_pk_decrypt( &alt, ciph, ciph_len,
markrad 0:cdf462088d13 473 test, &test_len, sizeof test,
markrad 0:cdf462088d13 474 rnd_std_rand, NULL ) == 0 );
markrad 0:cdf462088d13 475 TEST_ASSERT( test_len == sizeof msg );
markrad 0:cdf462088d13 476 TEST_ASSERT( memcmp( test, msg, test_len ) == 0 );
markrad 0:cdf462088d13 477
markrad 0:cdf462088d13 478 /* Test forbidden operations */
markrad 0:cdf462088d13 479 TEST_ASSERT( mbedtls_pk_encrypt( &alt, msg, sizeof msg,
markrad 0:cdf462088d13 480 ciph, &ciph_len, sizeof ciph,
markrad 0:cdf462088d13 481 rnd_std_rand, NULL ) == ret );
markrad 0:cdf462088d13 482 TEST_ASSERT( mbedtls_pk_verify( &alt, MBEDTLS_MD_NONE,
markrad 0:cdf462088d13 483 hash, sizeof hash, sig, sig_len ) == ret );
markrad 0:cdf462088d13 484 TEST_ASSERT( mbedtls_pk_debug( &alt, dbg_items ) == ret );
markrad 0:cdf462088d13 485
markrad 0:cdf462088d13 486 exit:
markrad 0:cdf462088d13 487 mbedtls_rsa_free( &raw );
markrad 0:cdf462088d13 488 mbedtls_pk_free( &rsa ); mbedtls_pk_free( &alt );
markrad 0:cdf462088d13 489 }
markrad 0:cdf462088d13 490 /* END_CASE */