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