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