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