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