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