| 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 */ | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 5 | #include "mbedtls/asn1.h" | 
|  | 6 | #include "mbedtls/base64.h" | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 7 | #include "mbedtls/ecp.h" | 
|  | 8 | #include "mbedtls/rsa.h" | 
| Manuel Pégourié-Gonnard | 7c13d69 | 2014-11-12 00:01:34 +0100 | [diff] [blame] | 9 |  | 
| Andres Amaya Garcia | 7ea6727 | 2017-05-08 11:15:49 +0100 | [diff] [blame] | 10 | #include <limits.h> | 
| Andres Amaya Garcia | 6ff067d | 2017-06-09 14:26:59 +0100 | [diff] [blame] | 11 | #include <stdint.h> | 
| Andres AG | 5c79d25 | 2017-02-15 10:52:32 +0000 | [diff] [blame] | 12 |  | 
| Gilles Peskine | 33b1c69 | 2019-12-19 00:09:47 +0100 | [diff] [blame] | 13 | /* Needed only for test case data under #if defined(MBEDTLS_USE_PSA_CRYPTO), | 
|  | 14 | * but the test code generator requires test case data to be valid C code | 
|  | 15 | * unconditionally (https://github.com/ARMmbed/mbedtls/issues/2023). */ | 
|  | 16 | #include "psa/crypto.h" | 
|  | 17 |  | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 18 | #define RSA_KEY_SIZE 512 | 
|  | 19 | #define RSA_KEY_LEN   64 | 
|  | 20 |  | 
| Gilles Peskine | e48fe55 | 2019-11-05 16:42:13 +0100 | [diff] [blame] | 21 | /** Generate a key of the desired type. | 
|  | 22 | * | 
|  | 23 | * \param pk        The PK object to fill. It must have been initialized | 
|  | 24 | *                  with mbedtls_pk_setup(). | 
|  | 25 | * \param parameter - For RSA keys, the key size in bits. | 
|  | 26 | *                  - For EC keys, the curve (\c MBEDTLS_ECP_DP_xxx). | 
|  | 27 | * | 
|  | 28 | * \return          The status from the underlying type-specific key | 
|  | 29 | *                  generation function. | 
|  | 30 | * \return          -1 if the key type is not recognized. | 
|  | 31 | */ | 
|  | 32 | static int pk_genkey( mbedtls_pk_context *pk, int parameter ) | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 33 | { | 
| Paul Bakker | a532090 | 2013-12-19 17:29:52 +0100 | [diff] [blame] | 34 | ((void) pk); | 
| Gilles Peskine | e48fe55 | 2019-11-05 16:42:13 +0100 | [diff] [blame] | 35 | (void) parameter; | 
| Paul Bakker | a532090 | 2013-12-19 17:29:52 +0100 | [diff] [blame] | 36 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) | 
|  | 38 | if( mbedtls_pk_get_type( pk ) == MBEDTLS_PK_RSA ) | 
| Gilles Peskine | e48fe55 | 2019-11-05 16:42:13 +0100 | [diff] [blame] | 39 | return mbedtls_rsa_gen_key( mbedtls_pk_rsa( *pk ), | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 40 | mbedtls_test_rnd_std_rand, NULL, | 
| Gilles Peskine | e48fe55 | 2019-11-05 16:42:13 +0100 | [diff] [blame] | 41 | parameter, 3 ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 42 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | #if defined(MBEDTLS_ECP_C) | 
|  | 44 | if( mbedtls_pk_get_type( pk ) == MBEDTLS_PK_ECKEY || | 
|  | 45 | mbedtls_pk_get_type( pk ) == MBEDTLS_PK_ECKEY_DH || | 
|  | 46 | mbedtls_pk_get_type( pk ) == MBEDTLS_PK_ECDSA ) | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 47 | { | 
|  | 48 | int ret; | 
| Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 49 | if( ( ret = mbedtls_ecp_group_load( &mbedtls_pk_ec( *pk )->grp, | 
| Gilles Peskine | e48fe55 | 2019-11-05 16:42:13 +0100 | [diff] [blame] | 50 | parameter ) ) != 0 ) | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 51 | return( ret ); | 
|  | 52 |  | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 53 | return mbedtls_ecp_gen_keypair( &mbedtls_pk_ec( *pk )->grp, | 
|  | 54 | &mbedtls_pk_ec( *pk )->d, | 
|  | 55 | &mbedtls_pk_ec( *pk )->Q, | 
|  | 56 | mbedtls_test_rnd_std_rand, NULL ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 57 | } | 
|  | 58 | #endif | 
|  | 59 | return( -1 ); | 
|  | 60 | } | 
| 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 | #if defined(MBEDTLS_RSA_C) | 
|  | 63 | 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] | 64 | const unsigned char *input, unsigned char *output, | 
|  | 65 | size_t output_max_len ) | 
|  | 66 | { | 
| Hanno Becker | 6ac972d | 2017-09-07 10:57:48 +0100 | [diff] [blame] | 67 | return( mbedtls_rsa_pkcs1_decrypt( (mbedtls_rsa_context *) ctx, | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 68 | mbedtls_test_rnd_std_rand, NULL, mode, | 
|  | 69 | olen, input, output, output_max_len ) ); | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 70 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 71 | int mbedtls_rsa_sign_func( void *ctx, | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 72 | 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] | 73 | 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] | 74 | const unsigned char *hash, unsigned char *sig ) | 
|  | 75 | { | 
| Hanno Becker | a540068 | 2017-05-03 16:43:15 +0100 | [diff] [blame] | 76 | ((void) f_rng); | 
|  | 77 | ((void) p_rng); | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 78 | return( mbedtls_rsa_pkcs1_sign( (mbedtls_rsa_context *) ctx, | 
|  | 79 | mbedtls_test_rnd_std_rand, NULL, mode, | 
|  | 80 | md_alg, hashlen, hash, sig ) ); | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 81 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | size_t mbedtls_rsa_key_len_func( void *ctx ) | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 83 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 84 | return( ((const mbedtls_rsa_context *) ctx)->len ); | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 85 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 86 | #endif /* MBEDTLS_RSA_C */ | 
| Manuel Pégourié-Gonnard | 01a12c4 | 2018-10-31 10:28:01 +0100 | [diff] [blame] | 87 |  | 
|  | 88 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 89 |  | 
| Manuel Pégourié-Gonnard | 01a12c4 | 2018-10-31 10:28:01 +0100 | [diff] [blame] | 90 | /* | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 91 | * Generate a key using PSA and return the key identifier of that key, | 
| Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 92 | * or 0 if the key generation failed. | 
| Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 93 | * The key uses NIST P-256 and is usable for signing with SHA-256. | 
| Manuel Pégourié-Gonnard | 01a12c4 | 2018-10-31 10:28:01 +0100 | [diff] [blame] | 94 | */ | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 95 | mbedtls_svc_key_id_t pk_psa_genkey( void ) | 
| Manuel Pégourié-Gonnard | 01a12c4 | 2018-10-31 10:28:01 +0100 | [diff] [blame] | 96 | { | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 97 | mbedtls_svc_key_id_t key; | 
| Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 98 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 99 | const psa_key_type_t type = | 
| Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 100 | PSA_KEY_TYPE_ECC_KEY_PAIR( PSA_ECC_FAMILY_SECP_R1 ); | 
| Manuel Pégourié-Gonnard | 01a12c4 | 2018-10-31 10:28:01 +0100 | [diff] [blame] | 101 | const size_t bits = 256; | 
|  | 102 |  | 
| Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 103 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); | 
| Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 104 | psa_set_key_algorithm( &attributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256) ); | 
|  | 105 | psa_set_key_type( &attributes, type ); | 
|  | 106 | psa_set_key_bits( &attributes, bits ); | 
|  | 107 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); | 
| Manuel Pégourié-Gonnard | 01a12c4 | 2018-10-31 10:28:01 +0100 | [diff] [blame] | 108 |  | 
| Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 109 | exit: | 
| Manuel Pégourié-Gonnard | 01a12c4 | 2018-10-31 10:28:01 +0100 | [diff] [blame] | 110 | return( key ); | 
|  | 111 | } | 
|  | 112 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 113 | /* END_HEADER */ | 
|  | 114 |  | 
|  | 115 | /* BEGIN_DEPENDENCIES | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 116 | * depends_on:MBEDTLS_PK_C | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 117 | * END_DEPENDENCIES | 
|  | 118 | */ | 
|  | 119 |  | 
| Manuel Pégourié-Gonnard | 01a12c4 | 2018-10-31 10:28:01 +0100 | [diff] [blame] | 120 | /* BEGIN_CASE depends_on:MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_ECDSA_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED */ | 
| Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 121 | void pk_psa_utils(  ) | 
|  | 122 | { | 
| Manuel Pégourié-Gonnard | d97390e | 2018-10-31 11:14:36 +0100 | [diff] [blame] | 123 | mbedtls_pk_context pk, pk2; | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 124 | mbedtls_svc_key_id_t key; | 
| Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 125 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 126 |  | 
| Manuel Pégourié-Gonnard | 69baf70 | 2018-11-06 09:34:30 +0100 | [diff] [blame] | 127 | const char * const name = "Opaque"; | 
| Manuel Pégourié-Gonnard | 0184b3c | 2018-10-31 10:36:51 +0100 | [diff] [blame] | 128 | const size_t bitlen = 256; /* harcoded in genkey() */ | 
|  | 129 |  | 
| Manuel Pégourié-Gonnard | d97390e | 2018-10-31 11:14:36 +0100 | [diff] [blame] | 130 | mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE; | 
|  | 131 | unsigned char b1[1], b2[1]; | 
|  | 132 | size_t len; | 
|  | 133 | mbedtls_pk_debug_item dbg; | 
|  | 134 |  | 
| Gilles Peskine | 5386f6b | 2019-08-01 12:47:40 +0200 | [diff] [blame] | 135 | PSA_ASSERT( psa_crypto_init( ) ); | 
| Jaeden Amero | 3ea2687 | 2019-02-13 11:30:22 +0000 | [diff] [blame] | 136 |  | 
| Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 137 | mbedtls_pk_init( &pk ); | 
| Manuel Pégourié-Gonnard | d97390e | 2018-10-31 11:14:36 +0100 | [diff] [blame] | 138 | mbedtls_pk_init( &pk2 ); | 
| Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 139 |  | 
| Gilles Peskine | 614faa2 | 2019-06-07 15:39:07 +0200 | [diff] [blame] | 140 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); | 
| Gilles Peskine | 69c0ea2 | 2019-06-07 15:38:59 +0200 | [diff] [blame] | 141 |  | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 142 | TEST_ASSERT( mbedtls_pk_setup_opaque( &pk, MBEDTLS_SVC_KEY_ID_INIT ) == | 
| Manuel Pégourié-Gonnard | 920c063 | 2018-10-31 10:57:29 +0100 | [diff] [blame] | 143 | MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
|  | 144 |  | 
|  | 145 | mbedtls_pk_free( &pk ); | 
|  | 146 | mbedtls_pk_init( &pk ); | 
|  | 147 |  | 
| Manuel Pégourié-Gonnard | 01a12c4 | 2018-10-31 10:28:01 +0100 | [diff] [blame] | 148 | key = pk_psa_genkey(); | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 149 | if( mbedtls_svc_key_id_is_null( key ) ) | 
| Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 150 | goto exit; | 
| Manuel Pégourié-Gonnard | 01a12c4 | 2018-10-31 10:28:01 +0100 | [diff] [blame] | 151 |  | 
| Manuel Pégourié-Gonnard | 69baf70 | 2018-11-06 09:34:30 +0100 | [diff] [blame] | 152 | TEST_ASSERT( mbedtls_pk_setup_opaque( &pk, key ) == 0 ); | 
| Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 153 |  | 
| Manuel Pégourié-Gonnard | 69baf70 | 2018-11-06 09:34:30 +0100 | [diff] [blame] | 154 | TEST_ASSERT( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_OPAQUE ); | 
| Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 155 | TEST_ASSERT( strcmp( mbedtls_pk_get_name( &pk), name ) == 0 ); | 
|  | 156 |  | 
| Manuel Pégourié-Gonnard | 0184b3c | 2018-10-31 10:36:51 +0100 | [diff] [blame] | 157 | TEST_ASSERT( mbedtls_pk_get_bitlen( &pk ) == bitlen ); | 
|  | 158 | TEST_ASSERT( mbedtls_pk_get_len( &pk ) == bitlen / 8 ); | 
|  | 159 |  | 
| Manuel Pégourié-Gonnard | 920c063 | 2018-10-31 10:57:29 +0100 | [diff] [blame] | 160 | TEST_ASSERT( mbedtls_pk_can_do( &pk, MBEDTLS_PK_ECKEY ) == 1 ); | 
|  | 161 | TEST_ASSERT( mbedtls_pk_can_do( &pk, MBEDTLS_PK_ECDSA ) == 1 ); | 
|  | 162 | TEST_ASSERT( mbedtls_pk_can_do( &pk, MBEDTLS_PK_RSA ) == 0 ); | 
|  | 163 |  | 
| Manuel Pégourié-Gonnard | d97390e | 2018-10-31 11:14:36 +0100 | [diff] [blame] | 164 | /* unsupported operations: verify, decrypt, encrypt */ | 
|  | 165 | TEST_ASSERT( mbedtls_pk_verify( &pk, md_alg, | 
|  | 166 | b1, sizeof( b1), b2, sizeof( b2 ) ) | 
|  | 167 | == MBEDTLS_ERR_PK_TYPE_MISMATCH ); | 
|  | 168 | TEST_ASSERT( mbedtls_pk_decrypt( &pk, b1, sizeof( b1 ), | 
|  | 169 | b2, &len, sizeof( b2 ), | 
|  | 170 | NULL, NULL ) | 
|  | 171 | == MBEDTLS_ERR_PK_TYPE_MISMATCH ); | 
|  | 172 | TEST_ASSERT( mbedtls_pk_encrypt( &pk, b1, sizeof( b1 ), | 
|  | 173 | b2, &len, sizeof( b2 ), | 
|  | 174 | NULL, NULL ) | 
|  | 175 | == MBEDTLS_ERR_PK_TYPE_MISMATCH ); | 
|  | 176 |  | 
|  | 177 | /* unsupported functions: check_pair, debug */ | 
|  | 178 | TEST_ASSERT( mbedtls_pk_setup( &pk2, | 
|  | 179 | mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY ) ) == 0 ); | 
|  | 180 | TEST_ASSERT( mbedtls_pk_check_pair( &pk, &pk2 ) | 
|  | 181 | == MBEDTLS_ERR_PK_TYPE_MISMATCH ); | 
|  | 182 | TEST_ASSERT( mbedtls_pk_debug( &pk, &dbg ) | 
|  | 183 | == MBEDTLS_ERR_PK_TYPE_MISMATCH ); | 
|  | 184 |  | 
| Manuel Pégourié-Gonnard | 01a12c4 | 2018-10-31 10:28:01 +0100 | [diff] [blame] | 185 | /* test that freeing the context does not destroy the key */ | 
| Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 186 | mbedtls_pk_free( &pk ); | 
| Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 187 | TEST_ASSERT( PSA_SUCCESS == psa_get_key_attributes( key, &attributes ) ); | 
| Manuel Pégourié-Gonnard | 01a12c4 | 2018-10-31 10:28:01 +0100 | [diff] [blame] | 188 | TEST_ASSERT( PSA_SUCCESS == psa_destroy_key( key ) ); | 
|  | 189 |  | 
|  | 190 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 191 | /* | 
|  | 192 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 193 | * thus reset them as required. | 
|  | 194 | */ | 
|  | 195 | psa_reset_key_attributes( &attributes ); | 
|  | 196 |  | 
| Manuel Pégourié-Gonnard | 01a12c4 | 2018-10-31 10:28:01 +0100 | [diff] [blame] | 197 | mbedtls_pk_free( &pk ); /* redundant except upon error */ | 
| Manuel Pégourié-Gonnard | d97390e | 2018-10-31 11:14:36 +0100 | [diff] [blame] | 198 | mbedtls_pk_free( &pk2 ); | 
| Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 199 | USE_PSA_DONE( ); | 
| Manuel Pégourié-Gonnard | 7b5fe04 | 2018-10-31 09:57:45 +0100 | [diff] [blame] | 200 | } | 
|  | 201 | /* END_CASE */ | 
|  | 202 |  | 
|  | 203 |  | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 204 | /* BEGIN_CASE */ | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 205 | void valid_parameters( ) | 
|  | 206 | { | 
|  | 207 | mbedtls_pk_context pk; | 
|  | 208 | unsigned char buf[1]; | 
|  | 209 | size_t len; | 
|  | 210 | void *options = NULL; | 
|  | 211 |  | 
|  | 212 | mbedtls_pk_init( &pk ); | 
|  | 213 |  | 
|  | 214 | TEST_VALID_PARAM( mbedtls_pk_free( NULL ) ); | 
|  | 215 |  | 
|  | 216 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) | 
|  | 217 | TEST_VALID_PARAM( mbedtls_pk_restart_free( NULL ) ); | 
|  | 218 | #endif | 
|  | 219 |  | 
|  | 220 | TEST_ASSERT( mbedtls_pk_setup( &pk, NULL ) == | 
|  | 221 | MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
|  | 222 |  | 
|  | 223 | /* In informational functions, we accept NULL where a context pointer | 
|  | 224 | * is expected because that's what the library has done forever. | 
|  | 225 | * We do not document that NULL is accepted, so we may wish to change | 
|  | 226 | * the behavior in a future version. */ | 
|  | 227 | TEST_ASSERT( mbedtls_pk_get_bitlen( NULL ) == 0 ); | 
|  | 228 | TEST_ASSERT( mbedtls_pk_get_len( NULL ) == 0 ); | 
|  | 229 | TEST_ASSERT( mbedtls_pk_can_do( NULL, MBEDTLS_PK_NONE ) == 0 ); | 
|  | 230 |  | 
|  | 231 | TEST_ASSERT( mbedtls_pk_sign_restartable( &pk, | 
|  | 232 | MBEDTLS_MD_NONE, | 
|  | 233 | NULL, 0, | 
|  | 234 | buf, &len, | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 235 | mbedtls_test_rnd_std_rand, NULL, | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 236 | NULL ) == | 
|  | 237 | MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
|  | 238 |  | 
|  | 239 | TEST_ASSERT( mbedtls_pk_sign_restartable( &pk, | 
|  | 240 | MBEDTLS_MD_NONE, | 
|  | 241 | NULL, 0, | 
|  | 242 | buf, &len, | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 243 | mbedtls_test_rnd_std_rand, NULL, | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 244 | NULL ) == | 
|  | 245 | MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
|  | 246 |  | 
|  | 247 | TEST_ASSERT( mbedtls_pk_sign( &pk, | 
|  | 248 | MBEDTLS_MD_NONE, | 
|  | 249 | NULL, 0, | 
|  | 250 | buf, &len, | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 251 | mbedtls_test_rnd_std_rand, NULL ) == | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 252 | MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
|  | 253 |  | 
|  | 254 | TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, | 
|  | 255 | MBEDTLS_MD_NONE, | 
|  | 256 | NULL, 0, | 
|  | 257 | buf, sizeof( buf ), | 
|  | 258 | NULL ) == | 
|  | 259 | MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
|  | 260 |  | 
|  | 261 | TEST_ASSERT( mbedtls_pk_verify( &pk, | 
|  | 262 | MBEDTLS_MD_NONE, | 
|  | 263 | NULL, 0, | 
|  | 264 | buf, sizeof( buf ) ) == | 
|  | 265 | MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
|  | 266 |  | 
|  | 267 | TEST_ASSERT( mbedtls_pk_verify_ext( MBEDTLS_PK_NONE, options, | 
|  | 268 | &pk, | 
|  | 269 | MBEDTLS_MD_NONE, | 
|  | 270 | NULL, 0, | 
|  | 271 | buf, sizeof( buf ) ) == | 
|  | 272 | MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
|  | 273 |  | 
|  | 274 | TEST_ASSERT( mbedtls_pk_encrypt( &pk, | 
|  | 275 | NULL, 0, | 
|  | 276 | NULL, &len, 0, | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 277 | mbedtls_test_rnd_std_rand, NULL ) == | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 278 | MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
|  | 279 |  | 
|  | 280 | TEST_ASSERT( mbedtls_pk_decrypt( &pk, | 
|  | 281 | NULL, 0, | 
|  | 282 | NULL, &len, 0, | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 283 | mbedtls_test_rnd_std_rand, NULL ) == | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 284 | MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
|  | 285 |  | 
|  | 286 | #if defined(MBEDTLS_PK_PARSE_C) | 
|  | 287 | TEST_ASSERT( mbedtls_pk_parse_key( &pk, NULL, 0, NULL, 1 ) == | 
|  | 288 | MBEDTLS_ERR_PK_KEY_INVALID_FORMAT ); | 
|  | 289 |  | 
|  | 290 | TEST_ASSERT( mbedtls_pk_parse_public_key( &pk, NULL, 0 ) == | 
|  | 291 | MBEDTLS_ERR_PK_KEY_INVALID_FORMAT ); | 
|  | 292 | #endif /* MBEDTLS_PK_PARSE_C */ | 
|  | 293 | } | 
|  | 294 | /* END_CASE */ | 
|  | 295 |  | 
|  | 296 | /* BEGIN_CASE depends_on:MBEDTLS_PK_WRITE_C */ | 
|  | 297 | void valid_parameters_pkwrite( data_t *key_data ) | 
|  | 298 | { | 
|  | 299 | mbedtls_pk_context pk; | 
|  | 300 |  | 
|  | 301 | /* For the write tests to be effective, we need a valid key pair. */ | 
|  | 302 | mbedtls_pk_init( &pk ); | 
|  | 303 | TEST_ASSERT( mbedtls_pk_parse_key( &pk, | 
|  | 304 | key_data->x, key_data->len, | 
|  | 305 | NULL, 0 ) == 0 ); | 
|  | 306 |  | 
|  | 307 | TEST_ASSERT( mbedtls_pk_write_key_der( &pk, NULL, 0 ) == | 
|  | 308 | MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); | 
|  | 309 |  | 
|  | 310 | TEST_ASSERT( mbedtls_pk_write_pubkey_der( &pk, NULL, 0 ) == | 
|  | 311 | MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); | 
|  | 312 |  | 
|  | 313 | #if defined(MBEDTLS_PEM_WRITE_C) | 
|  | 314 | TEST_ASSERT( mbedtls_pk_write_key_pem( &pk, NULL, 0 ) == | 
|  | 315 | MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL ); | 
|  | 316 |  | 
|  | 317 | TEST_ASSERT( mbedtls_pk_write_pubkey_pem( &pk, NULL, 0 ) == | 
|  | 318 | MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL ); | 
|  | 319 | #endif /* MBEDTLS_PEM_WRITE_C */ | 
|  | 320 |  | 
|  | 321 | exit: | 
|  | 322 | mbedtls_pk_free( &pk ); | 
|  | 323 | } | 
|  | 324 | /* END_CASE */ | 
|  | 325 |  | 
|  | 326 | /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */ | 
|  | 327 | void invalid_parameters( ) | 
|  | 328 | { | 
|  | 329 | size_t len; | 
|  | 330 | unsigned char *null_buf = NULL; | 
|  | 331 | unsigned char buf[1]; | 
|  | 332 | unsigned char *p = buf; | 
|  | 333 | char str[1] = {0}; | 
|  | 334 | mbedtls_pk_context pk; | 
|  | 335 | mbedtls_md_type_t valid_md = MBEDTLS_MD_SHA256; | 
|  | 336 | void *options = buf; | 
|  | 337 |  | 
|  | 338 | (void) null_buf; | 
|  | 339 | (void) p; | 
|  | 340 | (void) str; | 
|  | 341 |  | 
|  | 342 | mbedtls_pk_init( &pk ); | 
|  | 343 |  | 
|  | 344 | TEST_INVALID_PARAM( mbedtls_pk_init( NULL ) ); | 
|  | 345 |  | 
|  | 346 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) | 
|  | 347 | TEST_INVALID_PARAM( mbedtls_pk_restart_init( NULL ) ); | 
|  | 348 | #endif | 
|  | 349 |  | 
|  | 350 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 351 | mbedtls_pk_setup( NULL, NULL ) ); | 
|  | 352 |  | 
|  | 353 | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) | 
|  | 354 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 355 | mbedtls_pk_setup_rsa_alt( NULL, buf, | 
|  | 356 | NULL, NULL, NULL ) ); | 
|  | 357 | #endif | 
|  | 358 |  | 
|  | 359 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 360 | mbedtls_pk_verify_restartable( NULL, | 
|  | 361 | MBEDTLS_MD_NONE, | 
|  | 362 | buf, sizeof( buf ), | 
|  | 363 | buf, sizeof( buf ), | 
|  | 364 | NULL ) ); | 
|  | 365 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 366 | mbedtls_pk_verify_restartable( &pk, | 
|  | 367 | MBEDTLS_MD_NONE, | 
|  | 368 | NULL, sizeof( buf ), | 
|  | 369 | buf, sizeof( buf ), | 
|  | 370 | NULL ) ); | 
|  | 371 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 372 | mbedtls_pk_verify_restartable( &pk, | 
|  | 373 | valid_md, | 
|  | 374 | NULL, 0, | 
|  | 375 | buf, sizeof( buf ), | 
|  | 376 | NULL ) ); | 
|  | 377 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 378 | mbedtls_pk_verify_restartable( &pk, | 
|  | 379 | MBEDTLS_MD_NONE, | 
|  | 380 | buf, sizeof( buf ), | 
|  | 381 | NULL, sizeof( buf ), | 
|  | 382 | NULL ) ); | 
|  | 383 |  | 
|  | 384 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 385 | mbedtls_pk_verify( NULL, | 
|  | 386 | MBEDTLS_MD_NONE, | 
|  | 387 | buf, sizeof( buf ), | 
|  | 388 | buf, sizeof( buf ) ) ); | 
|  | 389 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 390 | mbedtls_pk_verify( &pk, | 
|  | 391 | MBEDTLS_MD_NONE, | 
|  | 392 | NULL, sizeof( buf ), | 
|  | 393 | buf, sizeof( buf ) ) ); | 
|  | 394 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 395 | mbedtls_pk_verify( &pk, | 
|  | 396 | valid_md, | 
|  | 397 | NULL, 0, | 
|  | 398 | buf, sizeof( buf ) ) ); | 
|  | 399 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 400 | mbedtls_pk_verify( &pk, | 
|  | 401 | MBEDTLS_MD_NONE, | 
|  | 402 | buf, sizeof( buf ), | 
|  | 403 | NULL, sizeof( buf ) ) ); | 
|  | 404 |  | 
|  | 405 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 406 | mbedtls_pk_verify_ext( MBEDTLS_PK_NONE, options, | 
|  | 407 | NULL, | 
|  | 408 | MBEDTLS_MD_NONE, | 
|  | 409 | buf, sizeof( buf ), | 
|  | 410 | buf, sizeof( buf ) ) ); | 
|  | 411 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 412 | mbedtls_pk_verify_ext( MBEDTLS_PK_NONE, options, | 
|  | 413 | &pk, | 
|  | 414 | MBEDTLS_MD_NONE, | 
|  | 415 | NULL, sizeof( buf ), | 
|  | 416 | buf, sizeof( buf ) ) ); | 
|  | 417 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 418 | mbedtls_pk_verify_ext( MBEDTLS_PK_NONE, options, | 
|  | 419 | &pk, | 
|  | 420 | valid_md, | 
|  | 421 | NULL, 0, | 
|  | 422 | buf, sizeof( buf ) ) ); | 
|  | 423 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 424 | mbedtls_pk_verify_ext( MBEDTLS_PK_NONE, options, | 
|  | 425 | &pk, | 
|  | 426 | MBEDTLS_MD_NONE, | 
|  | 427 | buf, sizeof( buf ), | 
|  | 428 | NULL, sizeof( buf ) ) ); | 
|  | 429 |  | 
|  | 430 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 431 | mbedtls_pk_sign_restartable( NULL, MBEDTLS_MD_NONE, buf, sizeof( buf ), | 
|  | 432 | buf, &len, mbedtls_test_rnd_std_rand, | 
|  | 433 | NULL, NULL ) ); | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 434 |  | 
|  | 435 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 436 | mbedtls_pk_sign_restartable( &pk, MBEDTLS_MD_NONE, NULL, sizeof( buf ), | 
|  | 437 | buf, &len, mbedtls_test_rnd_std_rand, | 
|  | 438 | NULL, NULL ) ); | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 439 |  | 
|  | 440 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 441 | mbedtls_pk_sign_restartable( &pk, valid_md, NULL, 0, buf, &len, | 
|  | 442 | mbedtls_test_rnd_std_rand, NULL, NULL ) ); | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 443 |  | 
|  | 444 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 445 | mbedtls_pk_sign_restartable( &pk, MBEDTLS_MD_NONE, buf, sizeof( buf ), | 
|  | 446 | NULL, &len, mbedtls_test_rnd_std_rand, | 
|  | 447 | NULL, NULL ) ); | 
|  | 448 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 449 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 450 | mbedtls_pk_sign( NULL, MBEDTLS_MD_NONE, buf, sizeof( buf ), | 
|  | 451 | buf, &len, mbedtls_test_rnd_std_rand, NULL ) ); | 
|  | 452 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 453 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 454 | mbedtls_pk_sign( &pk, MBEDTLS_MD_NONE, NULL, sizeof( buf ), | 
|  | 455 | buf, &len, mbedtls_test_rnd_std_rand, NULL ) ); | 
|  | 456 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 457 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 458 | mbedtls_pk_sign( &pk, valid_md, NULL, 0, buf, &len, | 
|  | 459 | mbedtls_test_rnd_std_rand, NULL ) ); | 
|  | 460 |  | 
|  | 461 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 462 | mbedtls_pk_sign( &pk, MBEDTLS_MD_NONE, buf, sizeof( buf ), NULL, &len, | 
|  | 463 | mbedtls_test_rnd_std_rand, NULL ) ); | 
|  | 464 |  | 
|  | 465 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 466 | mbedtls_pk_decrypt( NULL, buf, sizeof( buf ), buf, &len, sizeof( buf ), | 
|  | 467 | mbedtls_test_rnd_std_rand, NULL ) ); | 
|  | 468 |  | 
|  | 469 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 470 | mbedtls_pk_decrypt( &pk, NULL, sizeof( buf ), buf, &len, sizeof( buf ), | 
|  | 471 | mbedtls_test_rnd_std_rand, NULL ) ); | 
|  | 472 |  | 
|  | 473 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 474 | mbedtls_pk_decrypt( &pk, buf, sizeof( buf ), NULL, &len, sizeof( buf ), | 
|  | 475 | mbedtls_test_rnd_std_rand, NULL ) ); | 
|  | 476 |  | 
|  | 477 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 478 | mbedtls_pk_decrypt( &pk, buf, sizeof( buf ), buf, NULL, sizeof( buf ), | 
|  | 479 | mbedtls_test_rnd_std_rand, NULL ) ); | 
|  | 480 |  | 
|  | 481 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 482 | mbedtls_pk_encrypt( NULL, buf, sizeof( buf ), buf, &len, sizeof( buf ), | 
|  | 483 | mbedtls_test_rnd_std_rand, NULL ) ); | 
|  | 484 |  | 
|  | 485 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 486 | mbedtls_pk_encrypt( &pk, NULL, sizeof( buf ), buf, &len, sizeof( buf ), | 
|  | 487 | mbedtls_test_rnd_std_rand, NULL ) ); | 
|  | 488 |  | 
|  | 489 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 490 | mbedtls_pk_encrypt( &pk, buf, sizeof( buf ), NULL, &len, sizeof( buf ), | 
|  | 491 | mbedtls_test_rnd_std_rand, NULL ) ); | 
|  | 492 |  | 
|  | 493 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 494 | mbedtls_pk_encrypt( &pk, buf, sizeof( buf ), buf, NULL, sizeof( buf ), | 
|  | 495 | mbedtls_test_rnd_std_rand, NULL ) ); | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 496 |  | 
|  | 497 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 498 | mbedtls_pk_check_pair( NULL, &pk ) ); | 
|  | 499 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 500 | mbedtls_pk_check_pair( &pk, NULL ) ); | 
|  | 501 |  | 
|  | 502 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 503 | mbedtls_pk_debug( NULL, NULL ) ); | 
|  | 504 |  | 
|  | 505 | #if defined(MBEDTLS_PK_PARSE_C) | 
|  | 506 | #if defined(MBEDTLS_FS_IO) | 
|  | 507 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 508 | mbedtls_pk_load_file( NULL, &p, &len ) ); | 
|  | 509 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 510 | mbedtls_pk_load_file( str, NULL, &len ) ); | 
|  | 511 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 512 | mbedtls_pk_load_file( str, &p, NULL ) ); | 
|  | 513 |  | 
|  | 514 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 515 | mbedtls_pk_parse_keyfile( NULL, str, NULL ) ); | 
|  | 516 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 517 | mbedtls_pk_parse_keyfile( &pk, NULL, NULL ) ); | 
|  | 518 |  | 
|  | 519 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 520 | mbedtls_pk_parse_public_keyfile( NULL, str ) ); | 
|  | 521 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 522 | mbedtls_pk_parse_public_keyfile( &pk, NULL ) ); | 
|  | 523 | #endif | 
|  | 524 |  | 
|  | 525 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 526 | mbedtls_pk_parse_subpubkey( NULL, buf, &pk ) ); | 
|  | 527 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 528 | mbedtls_pk_parse_subpubkey( &null_buf, buf, &pk ) ); | 
|  | 529 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 530 | mbedtls_pk_parse_subpubkey( &p, NULL, &pk ) ); | 
|  | 531 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 532 | mbedtls_pk_parse_subpubkey( &p, buf, NULL ) ); | 
|  | 533 |  | 
|  | 534 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 535 | mbedtls_pk_parse_key( NULL, | 
|  | 536 | buf, sizeof( buf ), | 
|  | 537 | buf, sizeof( buf ) ) ); | 
|  | 538 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 539 | mbedtls_pk_parse_key( &pk, | 
|  | 540 | NULL, sizeof( buf ), | 
|  | 541 | buf, sizeof( buf ) ) ); | 
|  | 542 |  | 
|  | 543 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 544 | mbedtls_pk_parse_public_key( NULL, | 
|  | 545 | buf, sizeof( buf ) ) ); | 
|  | 546 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 547 | mbedtls_pk_parse_public_key( &pk, | 
|  | 548 | NULL, sizeof( buf ) ) ); | 
|  | 549 | #endif /* MBEDTLS_PK_PARSE_C */ | 
|  | 550 |  | 
|  | 551 | #if defined(MBEDTLS_PK_WRITE_C) | 
|  | 552 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 553 | mbedtls_pk_write_pubkey( NULL, p, &pk ) ); | 
|  | 554 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 555 | mbedtls_pk_write_pubkey( &null_buf, p, &pk ) ); | 
|  | 556 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 557 | mbedtls_pk_write_pubkey( &p, NULL, &pk ) ); | 
|  | 558 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 559 | mbedtls_pk_write_pubkey( &p, p, NULL ) ); | 
|  | 560 |  | 
|  | 561 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 562 | mbedtls_pk_write_pubkey_der( NULL, | 
|  | 563 | buf, sizeof( buf ) ) ); | 
|  | 564 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 565 | mbedtls_pk_write_pubkey_der( &pk, | 
|  | 566 | NULL, sizeof( buf ) ) ); | 
|  | 567 |  | 
|  | 568 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 569 | mbedtls_pk_write_key_der( NULL, | 
|  | 570 | buf, sizeof( buf ) ) ); | 
|  | 571 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 572 | mbedtls_pk_write_key_der( &pk, | 
|  | 573 | NULL, sizeof( buf ) ) ); | 
|  | 574 |  | 
|  | 575 | #if defined(MBEDTLS_PEM_WRITE_C) | 
|  | 576 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 577 | mbedtls_pk_write_pubkey_pem( NULL, | 
|  | 578 | buf, sizeof( buf ) ) ); | 
|  | 579 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 580 | mbedtls_pk_write_pubkey_pem( &pk, | 
|  | 581 | NULL, sizeof( buf ) ) ); | 
|  | 582 |  | 
|  | 583 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 584 | mbedtls_pk_write_key_pem( NULL, | 
|  | 585 | buf, sizeof( buf ) ) ); | 
|  | 586 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_PK_BAD_INPUT_DATA, | 
|  | 587 | mbedtls_pk_write_key_pem( &pk, | 
|  | 588 | NULL, sizeof( buf ) ) ); | 
|  | 589 | #endif /* MBEDTLS_PEM_WRITE_C */ | 
|  | 590 |  | 
|  | 591 | #endif /* MBEDTLS_PK_WRITE_C */ | 
|  | 592 | } | 
|  | 593 | /* END_CASE */ | 
|  | 594 |  | 
|  | 595 | /* BEGIN_CASE */ | 
| Gilles Peskine | e48fe55 | 2019-11-05 16:42:13 +0100 | [diff] [blame] | 596 | void pk_utils( int type, int parameter, int bitlen, int len, char * name ) | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 597 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 598 | mbedtls_pk_context pk; | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 599 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 600 | mbedtls_pk_init( &pk ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 601 |  | 
| Manuel Pégourié-Gonnard | d9e6a3a | 2015-05-14 19:41:36 +0200 | [diff] [blame] | 602 | TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( type ) ) == 0 ); | 
| Gilles Peskine | e48fe55 | 2019-11-05 16:42:13 +0100 | [diff] [blame] | 603 | TEST_ASSERT( pk_genkey( &pk, parameter ) == 0 ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 604 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 605 | TEST_ASSERT( (int) mbedtls_pk_get_type( &pk ) == type ); | 
|  | 606 | TEST_ASSERT( mbedtls_pk_can_do( &pk, type ) ); | 
| Gilles Peskine | e48fe55 | 2019-11-05 16:42:13 +0100 | [diff] [blame] | 607 | TEST_ASSERT( mbedtls_pk_get_bitlen( &pk ) == (unsigned) bitlen ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 608 | TEST_ASSERT( mbedtls_pk_get_len( &pk ) == (unsigned) len ); | 
|  | 609 | TEST_ASSERT( strcmp( mbedtls_pk_get_name( &pk), name ) == 0 ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 610 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 611 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 612 | mbedtls_pk_free( &pk ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 613 | } | 
|  | 614 | /* END_CASE */ | 
|  | 615 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 616 | /* BEGIN_CASE depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_FS_IO */ | 
| Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 617 | 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] | 618 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 619 | mbedtls_pk_context pub, prv, alt; | 
| Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 620 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 621 | mbedtls_pk_init( &pub ); | 
|  | 622 | mbedtls_pk_init( &prv ); | 
|  | 623 | mbedtls_pk_init( &alt ); | 
| Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 624 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 625 | TEST_ASSERT( mbedtls_pk_parse_public_keyfile( &pub, pub_file ) == 0 ); | 
|  | 626 | 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] | 627 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 628 | TEST_ASSERT( mbedtls_pk_check_pair( &pub, &prv ) == ret ); | 
| Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 629 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 630 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_RSA_ALT_SUPPORT) | 
|  | 631 | if( mbedtls_pk_get_type( &prv ) == MBEDTLS_PK_RSA ) | 
| Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 632 | { | 
| Manuel Pégourié-Gonnard | d9e6a3a | 2015-05-14 19:41:36 +0200 | [diff] [blame] | 633 | TEST_ASSERT( mbedtls_pk_setup_rsa_alt( &alt, mbedtls_pk_rsa( prv ), | 
| Hanno Becker | 6ac972d | 2017-09-07 10:57:48 +0100 | [diff] [blame] | 634 | mbedtls_rsa_decrypt_func, mbedtls_rsa_sign_func, | 
|  | 635 | mbedtls_rsa_key_len_func ) == 0 ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 636 | TEST_ASSERT( mbedtls_pk_check_pair( &pub, &alt ) == ret ); | 
| Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 637 | } | 
| Manuel Pégourié-Gonnard | 7c13d69 | 2014-11-12 00:01:34 +0100 | [diff] [blame] | 638 | #endif | 
| Manuel Pégourié-Gonnard | a1efcb0 | 2014-11-08 17:08:08 +0100 | [diff] [blame] | 639 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 640 | mbedtls_pk_free( &pub ); | 
|  | 641 | mbedtls_pk_free( &prv ); | 
|  | 642 | mbedtls_pk_free( &alt ); | 
| Manuel Pégourié-Gonnard | 70bdadf | 2014-11-06 16:51:20 +0100 | [diff] [blame] | 643 | } | 
|  | 644 | /* END_CASE */ | 
|  | 645 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 646 | /* BEGIN_CASE depends_on:MBEDTLS_RSA_C */ | 
| Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 647 | void pk_rsa_verify_test_vec( data_t * message_str, int digest, int mod, | 
| Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 648 | int radix_N, char * input_N, int radix_E, | 
| Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 649 | char * input_E, data_t * result_str, | 
| Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 650 | int result ) | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 651 | { | 
| Ron Eldor | 58e3f69 | 2018-11-21 13:44:31 +0200 | [diff] [blame] | 652 | unsigned char hash_result[MBEDTLS_MD_MAX_SIZE]; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 653 | mbedtls_rsa_context *rsa; | 
|  | 654 | mbedtls_pk_context pk; | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 655 | mbedtls_pk_restart_ctx *rs_ctx = NULL; | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 656 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 657 | mbedtls_pk_restart_ctx ctx; | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 658 |  | 
|  | 659 | rs_ctx = &ctx; | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 660 | mbedtls_pk_restart_init( rs_ctx ); | 
| Manuel Pégourié-Gonnard | 4edb9cb | 2018-07-02 12:23:23 +0200 | [diff] [blame] | 661 | // this setting would ensure restart would happen if ECC was used | 
|  | 662 | mbedtls_ecp_set_max_ops( 1 ); | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 663 | #endif | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 664 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 665 | mbedtls_pk_init( &pk ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 666 |  | 
| Ron Eldor | 58e3f69 | 2018-11-21 13:44:31 +0200 | [diff] [blame] | 667 | memset( hash_result, 0x00, MBEDTLS_MD_MAX_SIZE ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 668 |  | 
| Manuel Pégourié-Gonnard | d9e6a3a | 2015-05-14 19:41:36 +0200 | [diff] [blame] | 669 | 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] | 670 | rsa = mbedtls_pk_rsa( pk ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 671 |  | 
| Paul Bakker | 42099c3 | 2014-01-27 11:45:49 +0100 | [diff] [blame] | 672 | rsa->len = mod / 8; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 673 | TEST_ASSERT( mbedtls_mpi_read_string( &rsa->N, radix_N, input_N ) == 0 ); | 
|  | 674 | 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] | 675 |  | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 676 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 677 | if( mbedtls_md_info_from_type( digest ) != NULL ) | 
| Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 678 | TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str->x, message_str->len, hash_result ) == 0 ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 679 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 680 | TEST_ASSERT( mbedtls_pk_verify( &pk, digest, hash_result, 0, | 
| Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 681 | result_str->x, mbedtls_pk_get_len( &pk ) ) == result ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 682 |  | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 683 | TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, digest, hash_result, 0, | 
| Manuel Pégourié-Gonnard | 125af94 | 2018-09-11 11:08:12 +0200 | [diff] [blame] | 684 | result_str->x, mbedtls_pk_get_len( &pk ), rs_ctx ) == result ); | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 685 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 686 | exit: | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 687 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 688 | mbedtls_pk_restart_free( rs_ctx ); | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 689 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 690 | mbedtls_pk_free( &pk ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 691 | } | 
|  | 692 | /* END_CASE */ | 
|  | 693 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 694 | /* BEGIN_CASE depends_on:MBEDTLS_RSA_C */ | 
| Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 695 | void pk_rsa_verify_ext_test_vec( data_t * message_str, int digest, | 
| Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 696 | int mod, int radix_N, char * input_N, | 
|  | 697 | int radix_E, char * input_E, | 
| Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 698 | data_t * result_str, int pk_type, | 
| Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 699 | int mgf1_hash_id, int salt_len, int result ) | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 700 | { | 
| Ron Eldor | 58e3f69 | 2018-11-21 13:44:31 +0200 | [diff] [blame] | 701 | unsigned char hash_result[MBEDTLS_MD_MAX_SIZE]; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 702 | mbedtls_rsa_context *rsa; | 
|  | 703 | mbedtls_pk_context pk; | 
|  | 704 | mbedtls_pk_rsassa_pss_options pss_opts; | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 705 | void *options; | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 706 | size_t hash_len; | 
|  | 707 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 708 | mbedtls_pk_init( &pk ); | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 709 |  | 
| Ron Eldor | 58e3f69 | 2018-11-21 13:44:31 +0200 | [diff] [blame] | 710 | memset( hash_result, 0x00, sizeof( hash_result ) ); | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 711 |  | 
| Manuel Pégourié-Gonnard | d9e6a3a | 2015-05-14 19:41:36 +0200 | [diff] [blame] | 712 | 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] | 713 | rsa = mbedtls_pk_rsa( pk ); | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 714 |  | 
|  | 715 | rsa->len = mod / 8; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 716 | TEST_ASSERT( mbedtls_mpi_read_string( &rsa->N, radix_N, input_N ) == 0 ); | 
|  | 717 | 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] | 718 |  | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 719 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 720 | if( digest != MBEDTLS_MD_NONE ) | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 721 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 722 | TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), | 
| Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 723 | message_str->x, message_str->len, hash_result ) == 0 ); | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 724 | hash_len = 0; | 
|  | 725 | } | 
|  | 726 | else | 
|  | 727 | { | 
| Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 728 | memcpy( hash_result, message_str->x, message_str->len ); | 
|  | 729 | hash_len = message_str->len; | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 730 | } | 
|  | 731 |  | 
|  | 732 | if( mgf1_hash_id < 0 ) | 
|  | 733 | { | 
|  | 734 | options = NULL; | 
|  | 735 | } | 
|  | 736 | else | 
|  | 737 | { | 
|  | 738 | options = &pss_opts; | 
|  | 739 |  | 
|  | 740 | pss_opts.mgf1_hash_id = mgf1_hash_id; | 
|  | 741 | pss_opts.expected_salt_len = salt_len; | 
|  | 742 | } | 
|  | 743 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 744 | TEST_ASSERT( mbedtls_pk_verify_ext( pk_type, options, &pk, | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 745 | digest, hash_result, hash_len, | 
| Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 746 | result_str->x, mbedtls_pk_get_len( &pk ) ) == result ); | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 747 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 748 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 749 | mbedtls_pk_free( &pk ); | 
| Manuel Pégourié-Gonnard | 20422e9 | 2014-06-05 13:41:44 +0200 | [diff] [blame] | 750 | } | 
|  | 751 | /* END_CASE */ | 
|  | 752 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 753 | /* BEGIN_CASE depends_on:MBEDTLS_ECDSA_C */ | 
| Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 754 | void pk_ec_test_vec( int type, int id, data_t * key, data_t * hash, | 
|  | 755 | data_t * sig, int ret ) | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 756 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 757 | mbedtls_pk_context pk; | 
|  | 758 | mbedtls_ecp_keypair *eckey; | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 759 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 760 | mbedtls_pk_init( &pk ); | 
| Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 761 | USE_PSA_INIT( ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 762 |  | 
| Manuel Pégourié-Gonnard | d9e6a3a | 2015-05-14 19:41:36 +0200 | [diff] [blame] | 763 | 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] | 764 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 765 | TEST_ASSERT( mbedtls_pk_can_do( &pk, MBEDTLS_PK_ECDSA ) ); | 
|  | 766 | eckey = mbedtls_pk_ec( pk ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 767 |  | 
| Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 768 | TEST_ASSERT( mbedtls_ecp_group_load( &eckey->grp, id ) == 0 ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 769 | TEST_ASSERT( mbedtls_ecp_point_read_binary( &eckey->grp, &eckey->Q, | 
| Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 770 | key->x, key->len ) == 0 ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 771 |  | 
| John Durkop | d4efa8d | 2020-09-08 05:58:28 -0700 | [diff] [blame] | 772 | // MBEDTLS_MD_NONE is used since it will be ignored. | 
|  | 773 | TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_NONE, | 
| Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 774 | hash->x, hash->len, sig->x, sig->len ) == ret ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 775 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 776 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 777 | mbedtls_pk_free( &pk ); | 
| Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 778 | USE_PSA_DONE( ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 779 | } | 
|  | 780 | /* END_CASE */ | 
|  | 781 |  | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 782 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_C:MBEDTLS_ECDSA_DETERMINISTIC */ | 
|  | 783 | void pk_sign_verify_restart( int pk_type, int grp_id, char *d_str, | 
|  | 784 | char *QX_str, char *QY_str, | 
| Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 785 | int md_alg, char *msg, data_t *sig_check, | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 786 | int max_ops, int min_restart, int max_restart ) | 
|  | 787 | { | 
|  | 788 | int ret, cnt_restart; | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 789 | mbedtls_pk_restart_ctx rs_ctx; | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 790 | mbedtls_pk_context prv, pub; | 
|  | 791 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; | 
|  | 792 | unsigned char sig[MBEDTLS_ECDSA_MAX_LEN]; | 
| Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 793 | size_t hlen, slen; | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 794 | const mbedtls_md_info_t *md_info; | 
|  | 795 |  | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 796 | mbedtls_pk_restart_init( &rs_ctx ); | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 797 | mbedtls_pk_init( &prv ); | 
|  | 798 | mbedtls_pk_init( &pub ); | 
|  | 799 | memset( hash, 0, sizeof( hash ) ); | 
|  | 800 | memset( sig, 0, sizeof( sig ) ); | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 801 |  | 
|  | 802 | TEST_ASSERT( mbedtls_pk_setup( &prv, mbedtls_pk_info_from_type( pk_type ) ) == 0 ); | 
|  | 803 | TEST_ASSERT( mbedtls_ecp_group_load( &mbedtls_pk_ec( prv )->grp, grp_id ) == 0 ); | 
|  | 804 | TEST_ASSERT( mbedtls_mpi_read_string( &mbedtls_pk_ec( prv )->d, 16, d_str ) == 0 ); | 
|  | 805 |  | 
|  | 806 | TEST_ASSERT( mbedtls_pk_setup( &pub, mbedtls_pk_info_from_type( pk_type ) ) == 0 ); | 
|  | 807 | TEST_ASSERT( mbedtls_ecp_group_load( &mbedtls_pk_ec( pub )->grp, grp_id ) == 0 ); | 
|  | 808 | TEST_ASSERT( mbedtls_ecp_point_read_string( &mbedtls_pk_ec( pub )->Q, 16, QX_str, QY_str ) == 0 ); | 
|  | 809 |  | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 810 | md_info = mbedtls_md_info_from_type( md_alg ); | 
|  | 811 | TEST_ASSERT( md_info != NULL ); | 
|  | 812 |  | 
|  | 813 | hlen = mbedtls_md_get_size( md_info ); | 
| Gilles Peskine | 84984ae | 2020-01-21 16:52:08 +0100 | [diff] [blame] | 814 | TEST_ASSERT( mbedtls_md( md_info, | 
|  | 815 | (const unsigned char *) msg, strlen( msg ), | 
|  | 816 | hash ) == 0 ); | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 817 |  | 
|  | 818 | mbedtls_ecp_set_max_ops( max_ops ); | 
|  | 819 |  | 
|  | 820 | slen = sizeof( sig ); | 
|  | 821 | cnt_restart = 0; | 
|  | 822 | do { | 
|  | 823 | ret = mbedtls_pk_sign_restartable( &prv, md_alg, hash, hlen, | 
|  | 824 | sig, &slen, NULL, NULL, &rs_ctx ); | 
|  | 825 | } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart ); | 
|  | 826 |  | 
|  | 827 | TEST_ASSERT( ret == 0 ); | 
| Ronald Cron | 9ed4073 | 2020-06-25 09:03:34 +0200 | [diff] [blame] | 828 | TEST_ASSERT( slen == sig_check->len ); | 
|  | 829 | TEST_ASSERT( memcmp( sig, sig_check->x, slen ) == 0 ); | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 830 |  | 
|  | 831 | TEST_ASSERT( cnt_restart >= min_restart ); | 
|  | 832 | TEST_ASSERT( cnt_restart <= max_restart ); | 
|  | 833 |  | 
|  | 834 | cnt_restart = 0; | 
|  | 835 | do { | 
|  | 836 | ret = mbedtls_pk_verify_restartable( &pub, md_alg, | 
|  | 837 | hash, hlen, sig, slen, &rs_ctx ); | 
|  | 838 | } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart ); | 
|  | 839 |  | 
|  | 840 | TEST_ASSERT( ret == 0 ); | 
|  | 841 | TEST_ASSERT( cnt_restart >= min_restart ); | 
|  | 842 | TEST_ASSERT( cnt_restart <= max_restart ); | 
|  | 843 |  | 
|  | 844 | hash[0]++; | 
|  | 845 | do { | 
|  | 846 | ret = mbedtls_pk_verify_restartable( &pub, md_alg, | 
|  | 847 | hash, hlen, sig, slen, &rs_ctx ); | 
|  | 848 | } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ); | 
|  | 849 | TEST_ASSERT( ret != 0 ); | 
|  | 850 | hash[0]--; | 
|  | 851 |  | 
|  | 852 | sig[0]++; | 
|  | 853 | do { | 
|  | 854 | ret = mbedtls_pk_verify_restartable( &pub, md_alg, | 
|  | 855 | hash, hlen, sig, slen, &rs_ctx ); | 
|  | 856 | } while( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ); | 
|  | 857 | TEST_ASSERT( ret != 0 ); | 
|  | 858 | sig[0]--; | 
|  | 859 |  | 
| Manuel Pégourié-Gonnard | 46ba7f3 | 2017-08-28 12:20:39 +0200 | [diff] [blame] | 860 | /* Do we leak memory when aborting? try verify then sign | 
|  | 861 | * This test only makes sense when we actually restart */ | 
|  | 862 | if( min_restart > 0 ) | 
|  | 863 | { | 
|  | 864 | ret = mbedtls_pk_verify_restartable( &pub, md_alg, | 
|  | 865 | hash, hlen, sig, slen, &rs_ctx ); | 
|  | 866 | TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ); | 
|  | 867 | mbedtls_pk_restart_free( &rs_ctx ); | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 868 |  | 
| Manuel Pégourié-Gonnard | 46ba7f3 | 2017-08-28 12:20:39 +0200 | [diff] [blame] | 869 | slen = sizeof( sig ); | 
|  | 870 | ret = mbedtls_pk_sign_restartable( &prv, md_alg, hash, hlen, | 
|  | 871 | sig, &slen, NULL, NULL, &rs_ctx ); | 
|  | 872 | TEST_ASSERT( ret == MBEDTLS_ERR_ECP_IN_PROGRESS ); | 
|  | 873 | } | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 874 |  | 
|  | 875 | exit: | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 876 | mbedtls_pk_restart_free( &rs_ctx ); | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 877 | mbedtls_pk_free( &prv ); | 
|  | 878 | mbedtls_pk_free( &pub ); | 
|  | 879 | } | 
|  | 880 | /* END_CASE */ | 
|  | 881 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 882 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ | 
| Gilles Peskine | e48fe55 | 2019-11-05 16:42:13 +0100 | [diff] [blame] | 883 | void pk_sign_verify( int type, int parameter, int sign_ret, int verify_ret ) | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 884 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 885 | mbedtls_pk_context pk; | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 886 | size_t sig_len; | 
| Gilles Peskine | eba088a | 2019-11-05 16:32:32 +0100 | [diff] [blame] | 887 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; | 
|  | 888 | unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE]; | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 889 | void *rs_ctx = NULL; | 
|  | 890 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 891 | mbedtls_pk_restart_ctx ctx; | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 892 |  | 
|  | 893 | rs_ctx = &ctx; | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 894 | mbedtls_pk_restart_init( rs_ctx ); | 
| Manuel Pégourié-Gonnard | 4edb9cb | 2018-07-02 12:23:23 +0200 | [diff] [blame] | 895 | /* This value is large enough that the operation will complete in one run. | 
|  | 896 | * See comments at the top of ecp_test_vect_restart in | 
|  | 897 | * test_suite_ecp.function for estimates of operation counts. */ | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 898 | mbedtls_ecp_set_max_ops( 42000 ); | 
|  | 899 | #endif | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 900 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 901 | mbedtls_pk_init( &pk ); | 
| Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 902 | USE_PSA_INIT( ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 903 |  | 
|  | 904 | memset( hash, 0x2a, sizeof hash ); | 
|  | 905 | memset( sig, 0, sizeof sig ); | 
|  | 906 |  | 
| Manuel Pégourié-Gonnard | d9e6a3a | 2015-05-14 19:41:36 +0200 | [diff] [blame] | 907 | TEST_ASSERT( mbedtls_pk_setup( &pk, mbedtls_pk_info_from_type( type ) ) == 0 ); | 
| Gilles Peskine | e48fe55 | 2019-11-05 16:42:13 +0100 | [diff] [blame] | 908 | TEST_ASSERT( pk_genkey( &pk, parameter ) == 0 ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 909 |  | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 910 | TEST_ASSERT( mbedtls_pk_sign_restartable( &pk, MBEDTLS_MD_SHA256, | 
|  | 911 | hash, sizeof hash, sig, &sig_len, | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 912 | mbedtls_test_rnd_std_rand, NULL, rs_ctx ) == sign_ret ); | 
| Gilles Peskine | eba088a | 2019-11-05 16:32:32 +0100 | [diff] [blame] | 913 | if( sign_ret == 0 ) | 
|  | 914 | TEST_ASSERT( sig_len <= MBEDTLS_PK_SIGNATURE_MAX_SIZE ); | 
|  | 915 | else | 
| Gilles Peskine | f85e4e6 | 2019-11-12 11:08:23 +0100 | [diff] [blame] | 916 | sig_len = MBEDTLS_PK_SIGNATURE_MAX_SIZE; | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 917 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 918 | TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 919 | hash, sizeof hash, sig, sig_len ) == verify_ret ); | 
|  | 920 |  | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 921 | if( verify_ret == 0 ) | 
|  | 922 | { | 
|  | 923 | hash[0]++; | 
|  | 924 | TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, | 
|  | 925 | hash, sizeof hash, sig, sig_len ) != 0 ); | 
|  | 926 | hash[0]--; | 
|  | 927 |  | 
|  | 928 | sig[0]++; | 
|  | 929 | TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, | 
|  | 930 | hash, sizeof hash, sig, sig_len ) != 0 ); | 
|  | 931 | sig[0]--; | 
|  | 932 | } | 
|  | 933 |  | 
|  | 934 | TEST_ASSERT( mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256, hash, sizeof hash, | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 935 | sig, &sig_len, | 
|  | 936 | mbedtls_test_rnd_std_rand, | 
|  | 937 | NULL ) == sign_ret ); | 
| Gilles Peskine | eba088a | 2019-11-05 16:32:32 +0100 | [diff] [blame] | 938 | if( sign_ret == 0 ) | 
|  | 939 | TEST_ASSERT( sig_len <= MBEDTLS_PK_SIGNATURE_MAX_SIZE ); | 
|  | 940 | else | 
| Gilles Peskine | f85e4e6 | 2019-11-12 11:08:23 +0100 | [diff] [blame] | 941 | sig_len = MBEDTLS_PK_SIGNATURE_MAX_SIZE; | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 942 |  | 
|  | 943 | TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, MBEDTLS_MD_SHA256, | 
|  | 944 | hash, sizeof hash, sig, sig_len, rs_ctx ) == verify_ret ); | 
|  | 945 |  | 
|  | 946 | if( verify_ret == 0 ) | 
|  | 947 | { | 
|  | 948 | hash[0]++; | 
|  | 949 | TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, MBEDTLS_MD_SHA256, | 
|  | 950 | hash, sizeof hash, sig, sig_len, rs_ctx ) != 0 ); | 
|  | 951 | hash[0]--; | 
|  | 952 |  | 
|  | 953 | sig[0]++; | 
|  | 954 | TEST_ASSERT( mbedtls_pk_verify_restartable( &pk, MBEDTLS_MD_SHA256, | 
|  | 955 | hash, sizeof hash, sig, sig_len, rs_ctx ) != 0 ); | 
|  | 956 | sig[0]--; | 
|  | 957 | } | 
|  | 958 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 959 | exit: | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 960 | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) | 
| Manuel Pégourié-Gonnard | 15d7df2 | 2017-08-17 14:33:31 +0200 | [diff] [blame] | 961 | mbedtls_pk_restart_free( rs_ctx ); | 
| Manuel Pégourié-Gonnard | c4ee9ac | 2017-05-08 10:09:36 +0200 | [diff] [blame] | 962 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 963 | mbedtls_pk_free( &pk ); | 
| Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 964 | USE_PSA_DONE( ); | 
| Manuel Pégourié-Gonnard | b0a467f | 2013-09-21 12:31:05 +0200 | [diff] [blame] | 965 | } | 
|  | 966 | /* END_CASE */ | 
| Manuel Pégourié-Gonnard | 67d4583 | 2013-10-17 12:34:16 +0200 | [diff] [blame] | 967 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 968 | /* BEGIN_CASE depends_on:MBEDTLS_RSA_C */ | 
| Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 969 | void pk_rsa_encrypt_test_vec( data_t * message, int mod, int radix_N, | 
| Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 970 | char * input_N, int radix_E, char * input_E, | 
| Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 971 | data_t * result, int ret ) | 
| Manuel Pégourié-Gonnard | 67d4583 | 2013-10-17 12:34:16 +0200 | [diff] [blame] | 972 | { | 
| Ron Eldor | 58e3f69 | 2018-11-21 13:44:31 +0200 | [diff] [blame] | 973 | unsigned char output[300]; | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 974 | mbedtls_test_rnd_pseudo_info rnd_info; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 975 | mbedtls_rsa_context *rsa; | 
|  | 976 | mbedtls_pk_context pk; | 
| Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 977 | size_t olen; | 
| Manuel Pégourié-Gonnard | 67d4583 | 2013-10-17 12:34:16 +0200 | [diff] [blame] | 978 |  | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 979 | memset( &rnd_info,  0, sizeof( mbedtls_test_rnd_pseudo_info ) ); | 
| Manuel Pégourié-Gonnard | 67d4583 | 2013-10-17 12:34:16 +0200 | [diff] [blame] | 980 | memset( output,     0, sizeof( output ) ); | 
| Manuel Pégourié-Gonnard | 67d4583 | 2013-10-17 12:34:16 +0200 | [diff] [blame] | 981 |  | 
| Manuel Pégourié-Gonnard | 67d4583 | 2013-10-17 12:34:16 +0200 | [diff] [blame] | 982 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 983 | mbedtls_pk_init( &pk ); | 
| Manuel Pégourié-Gonnard | d9e6a3a | 2015-05-14 19:41:36 +0200 | [diff] [blame] | 984 | 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] | 985 | rsa = mbedtls_pk_rsa( pk ); | 
| Manuel Pégourié-Gonnard | 67d4583 | 2013-10-17 12:34:16 +0200 | [diff] [blame] | 986 |  | 
|  | 987 | rsa->len = mod / 8; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 988 | TEST_ASSERT( mbedtls_mpi_read_string( &rsa->N, radix_N, input_N ) == 0 ); | 
|  | 989 | 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] | 990 |  | 
| Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 991 | TEST_ASSERT( mbedtls_pk_encrypt( &pk, message->x, message->len, | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 992 | output, &olen, sizeof( output ), | 
|  | 993 | mbedtls_test_rnd_pseudo_rand, &rnd_info ) == ret ); | 
| Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 994 | TEST_ASSERT( olen == result->len ); | 
|  | 995 | TEST_ASSERT( memcmp( output, result->x, olen ) == 0 ); | 
| Manuel Pégourié-Gonnard | 67d4583 | 2013-10-17 12:34:16 +0200 | [diff] [blame] | 996 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 997 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 998 | mbedtls_pk_free( &pk ); | 
| Manuel Pégourié-Gonnard | 67d4583 | 2013-10-17 12:34:16 +0200 | [diff] [blame] | 999 | } | 
|  | 1000 | /* END_CASE */ | 
|  | 1001 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1002 | /* BEGIN_CASE depends_on:MBEDTLS_RSA_C */ | 
| Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 1003 | void pk_rsa_decrypt_test_vec( data_t * cipher, int mod, int radix_P, | 
| Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1004 | char * input_P, int radix_Q, char * input_Q, | 
|  | 1005 | int radix_N, char * input_N, int radix_E, | 
| Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 1006 | char * input_E, data_t * clear, int ret ) | 
| Manuel Pégourié-Gonnard | 67d4583 | 2013-10-17 12:34:16 +0200 | [diff] [blame] | 1007 | { | 
| Ron Eldor | 58e3f69 | 2018-11-21 13:44:31 +0200 | [diff] [blame] | 1008 | unsigned char output[256]; | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 1009 | mbedtls_test_rnd_pseudo_info rnd_info; | 
| Hanno Becker | d71dc15 | 2017-08-23 06:32:42 +0100 | [diff] [blame] | 1010 | mbedtls_mpi N, P, Q, E; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1011 | mbedtls_rsa_context *rsa; | 
|  | 1012 | mbedtls_pk_context pk; | 
| Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1013 | size_t olen; | 
| Manuel Pégourié-Gonnard | 67d4583 | 2013-10-17 12:34:16 +0200 | [diff] [blame] | 1014 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1015 | mbedtls_pk_init( &pk ); | 
| Hanno Becker | d71dc15 | 2017-08-23 06:32:42 +0100 | [diff] [blame] | 1016 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); | 
|  | 1017 | mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E ); | 
| Manuel Pégourié-Gonnard | 67d4583 | 2013-10-17 12:34:16 +0200 | [diff] [blame] | 1018 |  | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 1019 | memset( &rnd_info,  0, sizeof( mbedtls_test_rnd_pseudo_info ) ); | 
| Manuel Pégourié-Gonnard | 67d4583 | 2013-10-17 12:34:16 +0200 | [diff] [blame] | 1020 |  | 
| Manuel Pégourié-Gonnard | 67d4583 | 2013-10-17 12:34:16 +0200 | [diff] [blame] | 1021 |  | 
|  | 1022 | /* init pk-rsa context */ | 
| Manuel Pégourié-Gonnard | d9e6a3a | 2015-05-14 19:41:36 +0200 | [diff] [blame] | 1023 | 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] | 1024 | rsa = mbedtls_pk_rsa( pk ); | 
| Manuel Pégourié-Gonnard | 67d4583 | 2013-10-17 12:34:16 +0200 | [diff] [blame] | 1025 |  | 
|  | 1026 | /* load public key */ | 
| Hanno Becker | d71dc15 | 2017-08-23 06:32:42 +0100 | [diff] [blame] | 1027 | TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 ); | 
|  | 1028 | TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 ); | 
| Manuel Pégourié-Gonnard | 67d4583 | 2013-10-17 12:34:16 +0200 | [diff] [blame] | 1029 |  | 
|  | 1030 | /* load private key */ | 
| Hanno Becker | d71dc15 | 2017-08-23 06:32:42 +0100 | [diff] [blame] | 1031 | TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 ); | 
|  | 1032 | TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 ); | 
|  | 1033 | TEST_ASSERT( mbedtls_rsa_import( rsa, &N, &P, &Q, NULL, &E ) == 0 ); | 
|  | 1034 | TEST_ASSERT( mbedtls_rsa_get_len( rsa ) == (size_t) ( mod / 8 ) ); | 
| Hanno Becker | 7f25f85 | 2017-10-10 16:56:22 +0100 | [diff] [blame] | 1035 | TEST_ASSERT( mbedtls_rsa_complete( rsa ) == 0 ); | 
| Manuel Pégourié-Gonnard | 67d4583 | 2013-10-17 12:34:16 +0200 | [diff] [blame] | 1036 |  | 
|  | 1037 | /* decryption test */ | 
|  | 1038 | memset( output, 0, sizeof( output ) ); | 
|  | 1039 | olen = 0; | 
| Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1040 | TEST_ASSERT( mbedtls_pk_decrypt( &pk, cipher->x, cipher->len, | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 1041 | output, &olen, sizeof( output ), | 
|  | 1042 | mbedtls_test_rnd_pseudo_rand, &rnd_info ) == ret ); | 
| Manuel Pégourié-Gonnard | 67d4583 | 2013-10-17 12:34:16 +0200 | [diff] [blame] | 1043 | if( ret == 0 ) | 
|  | 1044 | { | 
| Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1045 | TEST_ASSERT( olen == clear->len ); | 
|  | 1046 | TEST_ASSERT( memcmp( output, clear->x, olen ) == 0 ); | 
| Manuel Pégourié-Gonnard | 67d4583 | 2013-10-17 12:34:16 +0200 | [diff] [blame] | 1047 | } | 
|  | 1048 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1049 | exit: | 
| Hanno Becker | d71dc15 | 2017-08-23 06:32:42 +0100 | [diff] [blame] | 1050 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); | 
|  | 1051 | mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1052 | mbedtls_pk_free( &pk ); | 
| Manuel Pégourié-Gonnard | 67d4583 | 2013-10-17 12:34:16 +0200 | [diff] [blame] | 1053 | } | 
|  | 1054 | /* END_CASE */ | 
| Manuel Pégourié-Gonnard | 75c7882 | 2013-10-17 12:46:39 +0200 | [diff] [blame] | 1055 |  | 
|  | 1056 | /* BEGIN_CASE */ | 
|  | 1057 | void pk_ec_nocrypt( int type ) | 
|  | 1058 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1059 | mbedtls_pk_context pk; | 
| Manuel Pégourié-Gonnard | 75c7882 | 2013-10-17 12:46:39 +0200 | [diff] [blame] | 1060 | unsigned char output[100]; | 
|  | 1061 | unsigned char input[100]; | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 1062 | mbedtls_test_rnd_pseudo_info rnd_info; | 
| Manuel Pégourié-Gonnard | 75c7882 | 2013-10-17 12:46:39 +0200 | [diff] [blame] | 1063 | size_t olen = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1064 | int ret = MBEDTLS_ERR_PK_TYPE_MISMATCH; | 
| Manuel Pégourié-Gonnard | 75c7882 | 2013-10-17 12:46:39 +0200 | [diff] [blame] | 1065 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1066 | mbedtls_pk_init( &pk ); | 
| Manuel Pégourié-Gonnard | 75c7882 | 2013-10-17 12:46:39 +0200 | [diff] [blame] | 1067 |  | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 1068 | memset( &rnd_info,  0, sizeof( mbedtls_test_rnd_pseudo_info ) ); | 
| Manuel Pégourié-Gonnard | 75c7882 | 2013-10-17 12:46:39 +0200 | [diff] [blame] | 1069 | memset( output,     0, sizeof( output ) ); | 
|  | 1070 | memset( input,      0, sizeof( input ) ); | 
|  | 1071 |  | 
| Manuel Pégourié-Gonnard | d9e6a3a | 2015-05-14 19:41:36 +0200 | [diff] [blame] | 1072 | 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] | 1073 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1074 | TEST_ASSERT( mbedtls_pk_encrypt( &pk, input, sizeof( input ), | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 1075 | output, &olen, sizeof( output ), | 
|  | 1076 | mbedtls_test_rnd_pseudo_rand, &rnd_info ) == ret ); | 
| Manuel Pégourié-Gonnard | 75c7882 | 2013-10-17 12:46:39 +0200 | [diff] [blame] | 1077 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1078 | TEST_ASSERT( mbedtls_pk_decrypt( &pk, input, sizeof( input ), | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 1079 | output, &olen, sizeof( output ), | 
|  | 1080 | mbedtls_test_rnd_pseudo_rand, &rnd_info ) == ret ); | 
| Manuel Pégourié-Gonnard | 75c7882 | 2013-10-17 12:46:39 +0200 | [diff] [blame] | 1081 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1082 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1083 | mbedtls_pk_free( &pk ); | 
| Manuel Pégourié-Gonnard | 75c7882 | 2013-10-17 12:46:39 +0200 | [diff] [blame] | 1084 | } | 
|  | 1085 | /* END_CASE */ | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 1086 |  | 
| Andres Amaya Garcia | 7ea6727 | 2017-05-08 11:15:49 +0100 | [diff] [blame] | 1087 | /* BEGIN_CASE depends_on:MBEDTLS_RSA_C */ | 
| Andres AG | 5c79d25 | 2017-02-15 10:52:32 +0000 | [diff] [blame] | 1088 | void pk_rsa_overflow( ) | 
|  | 1089 | { | 
|  | 1090 | mbedtls_pk_context pk; | 
| itayzafrir | 693a1d9 | 2018-02-26 12:02:10 +0200 | [diff] [blame] | 1091 | size_t hash_len = SIZE_MAX, sig_len = SIZE_MAX; | 
|  | 1092 | unsigned char hash[50], sig[100]; | 
| Andres Amaya Garcia | 7ea6727 | 2017-05-08 11:15:49 +0100 | [diff] [blame] | 1093 |  | 
|  | 1094 | if( SIZE_MAX <= UINT_MAX ) | 
|  | 1095 | return; | 
| Andres AG | 5c79d25 | 2017-02-15 10:52:32 +0000 | [diff] [blame] | 1096 |  | 
| itayzafrir | 693a1d9 | 2018-02-26 12:02:10 +0200 | [diff] [blame] | 1097 | memset( hash, 0x2a, sizeof hash ); | 
|  | 1098 | memset( sig, 0, sizeof sig ); | 
| Andres AG | 5c79d25 | 2017-02-15 10:52:32 +0000 | [diff] [blame] | 1099 |  | 
|  | 1100 | mbedtls_pk_init( &pk ); | 
|  | 1101 |  | 
|  | 1102 | TEST_ASSERT( mbedtls_pk_setup( &pk, | 
|  | 1103 | mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 ); | 
|  | 1104 |  | 
|  | 1105 | #if defined(MBEDTLS_PKCS1_V21) | 
|  | 1106 | TEST_ASSERT( mbedtls_pk_verify_ext( MBEDTLS_PK_RSASSA_PSS, NULL, &pk, | 
| itayzafrir | 693a1d9 | 2018-02-26 12:02:10 +0200 | [diff] [blame] | 1107 | MBEDTLS_MD_NONE, hash, hash_len, sig, sig_len ) == | 
| Andres AG | 5c79d25 | 2017-02-15 10:52:32 +0000 | [diff] [blame] | 1108 | MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
|  | 1109 | #endif /* MBEDTLS_PKCS1_V21 */ | 
|  | 1110 |  | 
| itayzafrir | 693a1d9 | 2018-02-26 12:02:10 +0200 | [diff] [blame] | 1111 | TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_NONE, hash, hash_len, | 
|  | 1112 | sig, sig_len ) == MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
| Andres AG | 5c79d25 | 2017-02-15 10:52:32 +0000 | [diff] [blame] | 1113 |  | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 1114 | TEST_ASSERT( mbedtls_pk_sign( &pk, MBEDTLS_MD_NONE, hash, hash_len, sig, | 
|  | 1115 | &sig_len, mbedtls_test_rnd_std_rand, NULL ) | 
|  | 1116 | == MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
| Andres AG | 5c79d25 | 2017-02-15 10:52:32 +0000 | [diff] [blame] | 1117 |  | 
|  | 1118 | exit: | 
|  | 1119 | mbedtls_pk_free( &pk ); | 
|  | 1120 | } | 
|  | 1121 | /* END_CASE */ | 
|  | 1122 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1123 | /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_PK_RSA_ALT_SUPPORT */ | 
| Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1124 | void pk_rsa_alt(  ) | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 1125 | { | 
|  | 1126 | /* | 
|  | 1127 | * An rsa_alt context can only do private operations (decrypt, sign). | 
|  | 1128 | * Test it against the public operations (encrypt, verify) of a | 
|  | 1129 | * corresponding rsa context. | 
|  | 1130 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1131 | mbedtls_rsa_context raw; | 
|  | 1132 | mbedtls_pk_context rsa, alt; | 
|  | 1133 | mbedtls_pk_debug_item dbg_items[10]; | 
| Ron Eldor | 58e3f69 | 2018-11-21 13:44:31 +0200 | [diff] [blame] | 1134 | unsigned char hash[50], sig[64]; | 
|  | 1135 | unsigned char msg[50], ciph[64], test[50]; | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 1136 | size_t sig_len, ciph_len, test_len; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1137 | int ret = MBEDTLS_ERR_PK_TYPE_MISMATCH; | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 1138 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1139 | mbedtls_rsa_init( &raw, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE ); | 
|  | 1140 | mbedtls_pk_init( &rsa ); mbedtls_pk_init( &alt ); | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 1141 |  | 
|  | 1142 | memset( hash, 0x2a, sizeof hash ); | 
|  | 1143 | memset( sig, 0, sizeof sig ); | 
|  | 1144 | memset( msg, 0x2a, sizeof msg ); | 
|  | 1145 | memset( ciph, 0, sizeof ciph ); | 
|  | 1146 | memset( test, 0, sizeof test ); | 
|  | 1147 |  | 
|  | 1148 | /* Initiliaze PK RSA context with random key */ | 
| Manuel Pégourié-Gonnard | d9e6a3a | 2015-05-14 19:41:36 +0200 | [diff] [blame] | 1149 | TEST_ASSERT( mbedtls_pk_setup( &rsa, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1150 | mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == 0 ); | 
| Gilles Peskine | e48fe55 | 2019-11-05 16:42:13 +0100 | [diff] [blame] | 1151 | TEST_ASSERT( pk_genkey( &rsa, RSA_KEY_SIZE ) == 0 ); | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 1152 |  | 
|  | 1153 | /* Extract key to the raw rsa context */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1154 | 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] | 1155 |  | 
|  | 1156 | /* Initialize PK RSA_ALT context */ | 
| Manuel Pégourié-Gonnard | d9e6a3a | 2015-05-14 19:41:36 +0200 | [diff] [blame] | 1157 | TEST_ASSERT( mbedtls_pk_setup_rsa_alt( &alt, (void *) &raw, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1158 | 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] | 1159 |  | 
|  | 1160 | /* Test administrative functions */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1161 | TEST_ASSERT( mbedtls_pk_can_do( &alt, MBEDTLS_PK_RSA ) ); | 
| Manuel Pégourié-Gonnard | 097c7bb | 2015-06-18 16:43:38 +0200 | [diff] [blame] | 1162 | TEST_ASSERT( mbedtls_pk_get_bitlen( &alt ) == RSA_KEY_SIZE ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1163 | TEST_ASSERT( mbedtls_pk_get_len( &alt ) == RSA_KEY_LEN ); | 
|  | 1164 | TEST_ASSERT( mbedtls_pk_get_type( &alt ) == MBEDTLS_PK_RSA_ALT ); | 
|  | 1165 | 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] | 1166 |  | 
|  | 1167 | /* Test signature */ | 
| Andres Amaya Garcia | 7ea6727 | 2017-05-08 11:15:49 +0100 | [diff] [blame] | 1168 | #if SIZE_MAX > UINT_MAX | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 1169 | TEST_ASSERT( mbedtls_pk_sign( &alt, MBEDTLS_MD_NONE, hash, SIZE_MAX, sig, | 
|  | 1170 | &sig_len, mbedtls_test_rnd_std_rand, NULL ) | 
|  | 1171 | == MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
| Andres Amaya Garcia | 7ea6727 | 2017-05-08 11:15:49 +0100 | [diff] [blame] | 1172 | #endif /* SIZE_MAX > UINT_MAX */ | 
| Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 1173 | TEST_ASSERT( mbedtls_pk_sign( &alt, MBEDTLS_MD_NONE, hash, sizeof hash, sig, | 
|  | 1174 | &sig_len, mbedtls_test_rnd_std_rand, NULL ) | 
|  | 1175 | == 0 ); | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 1176 | TEST_ASSERT( sig_len == RSA_KEY_LEN ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1177 | TEST_ASSERT( mbedtls_pk_verify( &rsa, MBEDTLS_MD_NONE, | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 1178 | hash, sizeof hash, sig, sig_len ) == 0 ); | 
|  | 1179 |  | 
|  | 1180 | /* Test decrypt */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1181 | TEST_ASSERT( mbedtls_pk_encrypt( &rsa, msg, sizeof msg, | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 1182 | ciph, &ciph_len, sizeof ciph, | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 1183 | mbedtls_test_rnd_std_rand, NULL ) == 0 ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1184 | TEST_ASSERT( mbedtls_pk_decrypt( &alt, ciph, ciph_len, | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 1185 | test, &test_len, sizeof test, | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 1186 | mbedtls_test_rnd_std_rand, NULL ) == 0 ); | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 1187 | TEST_ASSERT( test_len == sizeof msg ); | 
|  | 1188 | TEST_ASSERT( memcmp( test, msg, test_len ) == 0 ); | 
|  | 1189 |  | 
|  | 1190 | /* Test forbidden operations */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1191 | TEST_ASSERT( mbedtls_pk_encrypt( &alt, msg, sizeof msg, | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 1192 | ciph, &ciph_len, sizeof ciph, | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 1193 | mbedtls_test_rnd_std_rand, NULL ) == ret ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1194 | TEST_ASSERT( mbedtls_pk_verify( &alt, MBEDTLS_MD_NONE, | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 1195 | hash, sizeof hash, sig, sig_len ) == ret ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1196 | TEST_ASSERT( mbedtls_pk_debug( &alt, dbg_items ) == ret ); | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 1197 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1198 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1199 | mbedtls_rsa_free( &raw ); | 
|  | 1200 | mbedtls_pk_free( &rsa ); mbedtls_pk_free( &alt ); | 
| Manuel Pégourié-Gonnard | 0148875 | 2014-04-03 22:09:18 +0200 | [diff] [blame] | 1201 | } | 
|  | 1202 | /* END_CASE */ | 
| Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 1203 |  | 
| Gilles Peskine | 33b1c69 | 2019-12-19 00:09:47 +0100 | [diff] [blame] | 1204 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C:MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_ECDSA_C */ | 
|  | 1205 | void pk_psa_sign( int grpid_arg, | 
|  | 1206 | int psa_curve_arg, int expected_bits_arg ) | 
| Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 1207 | { | 
| Gilles Peskine | 33b1c69 | 2019-12-19 00:09:47 +0100 | [diff] [blame] | 1208 | mbedtls_ecp_group_id grpid = grpid_arg; | 
| Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 1209 | mbedtls_pk_context pk; | 
| Gilles Peskine | 33b1c69 | 2019-12-19 00:09:47 +0100 | [diff] [blame] | 1210 | unsigned char hash[32]; | 
|  | 1211 | unsigned char sig[MBEDTLS_ECDSA_MAX_LEN]; | 
|  | 1212 | unsigned char pkey_legacy[200]; | 
|  | 1213 | unsigned char pkey_psa[200]; | 
| Hanno Becker | 56104ea | 2019-02-01 11:48:19 +0000 | [diff] [blame] | 1214 | unsigned char *pkey_legacy_start, *pkey_psa_start; | 
|  | 1215 | size_t sig_len, klen_legacy, klen_psa; | 
|  | 1216 | int ret; | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1217 | mbedtls_svc_key_id_t key_id; | 
| Gilles Peskine | 85f47c9 | 2019-12-18 20:44:15 +0100 | [diff] [blame] | 1218 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Gilles Peskine | 33b1c69 | 2019-12-19 00:09:47 +0100 | [diff] [blame] | 1219 | psa_key_type_t expected_type = PSA_KEY_TYPE_ECC_KEY_PAIR( psa_curve_arg ); | 
|  | 1220 | size_t expected_bits = expected_bits_arg; | 
| Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 1221 |  | 
|  | 1222 | /* | 
|  | 1223 | * This tests making signatures with a wrapped PSA key: | 
| Hanno Becker | 56104ea | 2019-02-01 11:48:19 +0000 | [diff] [blame] | 1224 | * - generate a fresh ECP legacy PK context | 
| Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 1225 | * - wrap it in a PK context and make a signature this way | 
|  | 1226 | * - extract the public key | 
|  | 1227 | * - parse it to a PK context and verify the signature this way | 
|  | 1228 | */ | 
|  | 1229 |  | 
| Gilles Peskine | 5386f6b | 2019-08-01 12:47:40 +0200 | [diff] [blame] | 1230 | PSA_ASSERT( psa_crypto_init( ) ); | 
|  | 1231 |  | 
| Hanno Becker | 56104ea | 2019-02-01 11:48:19 +0000 | [diff] [blame] | 1232 | /* Create legacy EC public/private key in PK context. */ | 
| Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 1233 | mbedtls_pk_init( &pk ); | 
| Hanno Becker | 56104ea | 2019-02-01 11:48:19 +0000 | [diff] [blame] | 1234 | TEST_ASSERT( mbedtls_pk_setup( &pk, | 
|  | 1235 | mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY ) ) == 0 ); | 
| Gilles Peskine | 85f47c9 | 2019-12-18 20:44:15 +0100 | [diff] [blame] | 1236 | TEST_ASSERT( mbedtls_ecp_gen_key( grpid, | 
| Hanno Becker | 56104ea | 2019-02-01 11:48:19 +0000 | [diff] [blame] | 1237 | (mbedtls_ecp_keypair*) pk.pk_ctx, | 
| Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 1238 | mbedtls_test_rnd_std_rand, NULL ) == 0 ); | 
| Hanno Becker | 56104ea | 2019-02-01 11:48:19 +0000 | [diff] [blame] | 1239 |  | 
|  | 1240 | /* Export underlying public key for re-importing in a legacy context. */ | 
|  | 1241 | ret = mbedtls_pk_write_pubkey_der( &pk, pkey_legacy, | 
|  | 1242 | sizeof( pkey_legacy ) ); | 
|  | 1243 | TEST_ASSERT( ret >= 0 ); | 
|  | 1244 | klen_legacy = (size_t) ret; | 
|  | 1245 | /* mbedtls_pk_write_pubkey_der() writes backwards in the data buffer. */ | 
|  | 1246 | pkey_legacy_start = pkey_legacy + sizeof( pkey_legacy ) - klen_legacy; | 
|  | 1247 |  | 
|  | 1248 | /* Turn PK context into an opaque one. */ | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1249 | TEST_ASSERT( mbedtls_pk_wrap_as_opaque( &pk, &key_id, | 
| Hanno Becker | 56104ea | 2019-02-01 11:48:19 +0000 | [diff] [blame] | 1250 | PSA_ALG_SHA_256 ) == 0 ); | 
| Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 1251 |  | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1252 | PSA_ASSERT( psa_get_key_attributes( key_id, &attributes ) ); | 
| Gilles Peskine | 33b1c69 | 2019-12-19 00:09:47 +0100 | [diff] [blame] | 1253 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_type ); | 
| Gilles Peskine | 85f47c9 | 2019-12-18 20:44:15 +0100 | [diff] [blame] | 1254 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); | 
|  | 1255 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), | 
|  | 1256 | PSA_KEY_LIFETIME_VOLATILE ); | 
|  | 1257 |  | 
| Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 1258 | memset( hash, 0x2a, sizeof hash ); | 
|  | 1259 | memset( sig, 0, sizeof sig ); | 
| Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 1260 |  | 
|  | 1261 | TEST_ASSERT( mbedtls_pk_sign( &pk, MBEDTLS_MD_SHA256, | 
|  | 1262 | hash, sizeof hash, sig, &sig_len, | 
|  | 1263 | NULL, NULL ) == 0 ); | 
|  | 1264 |  | 
| Hanno Becker | 56104ea | 2019-02-01 11:48:19 +0000 | [diff] [blame] | 1265 | /* Export underlying public key for re-importing in a psa context. */ | 
|  | 1266 | ret = mbedtls_pk_write_pubkey_der( &pk, pkey_psa, | 
|  | 1267 | sizeof( pkey_psa ) ); | 
|  | 1268 | TEST_ASSERT( ret >= 0 ); | 
|  | 1269 | klen_psa = (size_t) ret; | 
|  | 1270 | /* mbedtls_pk_write_pubkey_der() writes backwards in the data buffer. */ | 
|  | 1271 | pkey_psa_start = pkey_psa + sizeof( pkey_psa ) - klen_psa; | 
| Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 1272 |  | 
| Hanno Becker | 56104ea | 2019-02-01 11:48:19 +0000 | [diff] [blame] | 1273 | TEST_ASSERT( klen_psa == klen_legacy ); | 
|  | 1274 | TEST_ASSERT( memcmp( pkey_psa_start, pkey_legacy_start, klen_psa ) == 0 ); | 
|  | 1275 |  | 
|  | 1276 | mbedtls_pk_free( &pk ); | 
| Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1277 | TEST_ASSERT( PSA_SUCCESS == psa_destroy_key( key_id ) ); | 
| Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 1278 |  | 
|  | 1279 | mbedtls_pk_init( &pk ); | 
| Hanno Becker | 56104ea | 2019-02-01 11:48:19 +0000 | [diff] [blame] | 1280 | TEST_ASSERT( mbedtls_pk_parse_public_key( &pk, pkey_legacy_start, | 
|  | 1281 | klen_legacy ) == 0 ); | 
| Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 1282 | TEST_ASSERT( mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, | 
|  | 1283 | hash, sizeof hash, sig, sig_len ) == 0 ); | 
|  | 1284 |  | 
|  | 1285 | exit: | 
| Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1286 | /* | 
|  | 1287 | * Key attributes may have been returned by psa_get_key_attributes() | 
|  | 1288 | * thus reset them as required. | 
|  | 1289 | */ | 
|  | 1290 | psa_reset_key_attributes( &attributes ); | 
|  | 1291 |  | 
| Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 1292 | mbedtls_pk_free( &pk ); | 
| Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 1293 | USE_PSA_DONE( ); | 
| Manuel Pégourié-Gonnard | 3686771 | 2018-10-31 16:22:49 +0100 | [diff] [blame] | 1294 | } | 
|  | 1295 | /* END_CASE */ |