Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2 | #include <stdint.h> |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3 | #include "psa/crypto.h" |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 4 | #include "mbedtls/md.h" |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 5 | |
| 6 | #if(UINT32_MAX > SIZE_MAX) |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 7 | #define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX ) |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 8 | #else |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 9 | #define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1 |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 10 | #endif |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 11 | /* END_HEADER */ |
| 12 | |
| 13 | /* BEGIN_DEPENDENCIES |
| 14 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 15 | * END_DEPENDENCIES |
| 16 | */ |
| 17 | |
| 18 | /* BEGIN_CASE */ |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 19 | void init_deinit( ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 20 | { |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 21 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 22 | int i; |
| 23 | for( i = 0; i <= 1; i++ ) |
| 24 | { |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 25 | status = psa_crypto_init( ); |
| 26 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 27 | status = psa_crypto_init( ); |
| 28 | TEST_ASSERT( status == PSA_SUCCESS ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 29 | mbedtls_psa_crypto_free( ); |
| 30 | } |
| 31 | } |
| 32 | /* END_CASE */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 33 | |
| 34 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 35 | void import( data_t *data, int type, int expected_status ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 36 | { |
| 37 | int slot = 1; |
| 38 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 39 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 40 | TEST_ASSERT( data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 41 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 42 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 43 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 44 | status = psa_import_key( slot, type, data->x, (size_t) data->len ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 45 | TEST_ASSERT( status == (psa_status_t) expected_status ); |
| 46 | if( status == PSA_SUCCESS ) |
| 47 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 48 | |
| 49 | exit: |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 50 | mbedtls_psa_crypto_free( ); |
| 51 | } |
| 52 | /* END_CASE */ |
| 53 | |
| 54 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 55 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 56 | int type_arg, |
| 57 | int alg_arg, |
| 58 | int usage_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 59 | int expected_bits, |
| 60 | int export_size_delta, |
| 61 | int expected_export_status, |
| 62 | int canonical_input ) |
| 63 | { |
| 64 | int slot = 1; |
| 65 | int slot2 = slot + 1; |
| 66 | psa_key_type_t type = type_arg; |
| 67 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 68 | unsigned char *exported = NULL; |
| 69 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 70 | size_t export_size; |
| 71 | size_t exported_length; |
| 72 | size_t reexported_length; |
| 73 | psa_key_type_t got_type; |
| 74 | size_t got_bits; |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 75 | psa_key_policy_t policy = {0}; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 76 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 77 | TEST_ASSERT( data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 78 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) ); |
| 79 | export_size = (ssize_t) data->len + export_size_delta; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 80 | exported = mbedtls_calloc( 1, export_size ); |
| 81 | TEST_ASSERT( exported != NULL ); |
| 82 | if( ! canonical_input ) |
| 83 | { |
| 84 | reexported = mbedtls_calloc( 1, export_size ); |
| 85 | TEST_ASSERT( reexported != NULL ); |
| 86 | } |
| 87 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 88 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 89 | psa_key_policy_init( &policy ); |
| 90 | |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 91 | psa_key_policy_set_usage( &policy, usage_arg, alg_arg ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 92 | |
| 93 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 94 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 95 | /* Import the key */ |
| 96 | TEST_ASSERT( psa_import_key( slot, type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 97 | data->x, (size_t) data->len ) == PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 98 | |
| 99 | /* Test the key information */ |
| 100 | TEST_ASSERT( psa_get_key_information( slot, |
| 101 | &got_type, &got_bits ) == |
| 102 | PSA_SUCCESS ); |
| 103 | TEST_ASSERT( got_type == type ); |
| 104 | TEST_ASSERT( got_bits == (size_t) expected_bits ); |
| 105 | |
| 106 | /* Export the key */ |
| 107 | status = psa_export_key( slot, |
| 108 | exported, export_size, |
| 109 | &exported_length ); |
| 110 | TEST_ASSERT( status == (psa_status_t) expected_export_status ); |
| 111 | if( status != PSA_SUCCESS ) |
| 112 | goto destroy; |
| 113 | |
| 114 | if( canonical_input ) |
| 115 | { |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 116 | TEST_ASSERT( exported_length == (size_t) data->len ); |
| 117 | TEST_ASSERT( memcmp( exported, data->x, (size_t) data->len ) == 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 118 | } |
| 119 | else |
| 120 | { |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 121 | TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS ); |
| 122 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 123 | TEST_ASSERT( psa_import_key( slot2, type, |
| 124 | exported, export_size ) == |
| 125 | PSA_SUCCESS ); |
| 126 | TEST_ASSERT( psa_export_key( slot2, |
| 127 | reexported, export_size, |
| 128 | &reexported_length ) == |
| 129 | PSA_SUCCESS ); |
| 130 | TEST_ASSERT( reexported_length == exported_length ); |
| 131 | TEST_ASSERT( memcmp( reexported, exported, |
| 132 | exported_length ) == 0 ); |
| 133 | } |
| 134 | |
| 135 | destroy: |
| 136 | /* Destroy the key */ |
| 137 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 138 | TEST_ASSERT( psa_get_key_information( |
| 139 | slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT ); |
| 140 | |
| 141 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 142 | mbedtls_free( exported ); |
| 143 | mbedtls_free( reexported ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 144 | mbedtls_psa_crypto_free( ); |
| 145 | } |
| 146 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 147 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 148 | |
| 149 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 150 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 151 | int type_arg, |
| 152 | int alg_arg, |
| 153 | int expected_bits, |
| 154 | int public_key_expected_length, |
| 155 | int expected_export_status ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 156 | { |
| 157 | int slot = 1; |
| 158 | psa_key_type_t type = type_arg; |
| 159 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 160 | unsigned char *exported = NULL; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 161 | size_t export_size; |
| 162 | size_t exported_length; |
| 163 | psa_key_type_t got_type; |
| 164 | size_t got_bits; |
| 165 | psa_key_policy_t policy = {0}; |
| 166 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 167 | TEST_ASSERT( data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 168 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) ); |
| 169 | export_size = (ssize_t) data->len; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 170 | exported = mbedtls_calloc( 1, export_size ); |
| 171 | TEST_ASSERT( exported != NULL ); |
| 172 | |
| 173 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 174 | |
| 175 | psa_key_policy_init( &policy ); |
| 176 | |
| 177 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, |
| 178 | alg_arg ); |
| 179 | |
| 180 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 181 | |
| 182 | /* Import the key */ |
| 183 | TEST_ASSERT( psa_import_key( slot, type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 184 | data->x, (size_t) data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 185 | PSA_SUCCESS ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 186 | |
| 187 | /* Test the key information */ |
| 188 | TEST_ASSERT( psa_get_key_information( slot, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 189 | &got_type, &got_bits ) == PSA_SUCCESS ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 190 | TEST_ASSERT( got_type == type ); |
| 191 | TEST_ASSERT( got_bits == (size_t) expected_bits ); |
| 192 | |
| 193 | /* Export the key */ |
| 194 | status = psa_export_public_key( slot, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 195 | exported, export_size, |
| 196 | &exported_length ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 197 | TEST_ASSERT( status == (psa_status_t) expected_export_status ); |
| 198 | if( status != PSA_SUCCESS ) |
| 199 | goto destroy; |
| 200 | |
| 201 | TEST_ASSERT( exported_length == (size_t) public_key_expected_length ); |
| 202 | |
| 203 | destroy: |
| 204 | /* Destroy the key */ |
| 205 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 206 | TEST_ASSERT( psa_get_key_information( |
| 207 | slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT ); |
| 208 | |
| 209 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 210 | mbedtls_free( exported ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 211 | mbedtls_psa_crypto_free( ); |
| 212 | } |
| 213 | /* END_CASE */ |
| 214 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 215 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 216 | void hash_finish( int alg_arg, data_t *input, data_t *expected_hash ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 217 | { |
| 218 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 219 | unsigned char actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 220 | size_t actual_hash_length; |
| 221 | psa_hash_operation_t operation; |
| 222 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 223 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 224 | TEST_ASSERT( expected_hash != NULL ); |
| 225 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 226 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 227 | |
| 228 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 229 | |
| 230 | TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS ); |
| 231 | TEST_ASSERT( psa_hash_update( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 232 | input->x, (size_t) input->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 233 | PSA_SUCCESS ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 234 | TEST_ASSERT( psa_hash_finish( &operation, |
| 235 | actual_hash, sizeof( actual_hash ), |
| 236 | &actual_hash_length ) == PSA_SUCCESS ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 237 | TEST_ASSERT( actual_hash_length == (size_t) expected_hash->len ); |
| 238 | TEST_ASSERT( memcmp( expected_hash->x, actual_hash, |
| 239 | (size_t) expected_hash->len ) == 0 ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 240 | |
| 241 | exit: |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 242 | mbedtls_psa_crypto_free( ); |
| 243 | } |
| 244 | /* END_CASE */ |
| 245 | |
| 246 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 247 | void hash_verify( int alg_arg, data_t *input, data_t *expected_hash ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 248 | { |
| 249 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 250 | psa_hash_operation_t operation; |
| 251 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 252 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 253 | TEST_ASSERT( expected_hash != NULL ); |
| 254 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 255 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 256 | |
| 257 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 258 | |
| 259 | TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS ); |
| 260 | TEST_ASSERT( psa_hash_update( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 261 | input->x, (size_t) input->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 262 | PSA_SUCCESS ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 263 | TEST_ASSERT( psa_hash_verify( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 264 | expected_hash->x, |
| 265 | (size_t) expected_hash->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 266 | PSA_SUCCESS ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 267 | |
| 268 | exit: |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 269 | mbedtls_psa_crypto_free( ); |
| 270 | } |
| 271 | /* END_CASE */ |
| 272 | |
| 273 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 274 | void mac_verify( int key_type_arg, data_t *key, |
| 275 | int alg_arg, data_t *iv, |
| 276 | data_t *input, data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 277 | { |
| 278 | int key_slot = 1; |
| 279 | psa_key_type_t key_type = key_type_arg; |
| 280 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 281 | psa_mac_operation_t operation; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 282 | psa_key_policy_t policy; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 283 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 284 | TEST_ASSERT( key != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 285 | TEST_ASSERT( iv != NULL ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 286 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 287 | TEST_ASSERT( expected_mac != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 288 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 289 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( iv->len ) ); |
| 290 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 291 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 292 | |
| 293 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 294 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 295 | psa_key_policy_init( &policy ); |
| 296 | |
| 297 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg_arg ); |
| 298 | |
| 299 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 300 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 301 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 302 | key->x, (size_t) key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 303 | // TODO: support IV |
| 304 | TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 305 | TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS ); |
| 306 | TEST_ASSERT( psa_mac_update( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 307 | input->x, (size_t) input->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 308 | PSA_SUCCESS ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 309 | TEST_ASSERT( psa_mac_verify( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 310 | expected_mac->x, |
| 311 | (size_t) expected_mac->len ) == PSA_SUCCESS ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 312 | |
| 313 | exit: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 314 | psa_destroy_key( key_slot ); |
| 315 | mbedtls_psa_crypto_free( ); |
| 316 | } |
| 317 | /* END_CASE */ |
| 318 | |
Moran Peker | 7f87850 | 2018-06-06 17:09:40 +0300 | [diff] [blame] | 319 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 320 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 321 | void cipher_encrypt( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 322 | data_t *key, |
| 323 | data_t *input, data_t *expected_output, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 324 | int expected_status ) |
| 325 | { |
| 326 | int key_slot = 1; |
| 327 | psa_status_t status; |
| 328 | psa_key_type_t key_type = key_type_arg; |
| 329 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 330 | unsigned char iv[16] = {0}; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 331 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 332 | size_t output_buffer_size = 0; |
| 333 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 334 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 335 | psa_cipher_operation_t operation; |
| 336 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 337 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 338 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 339 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 340 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 341 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 342 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 343 | |
| 344 | memset( iv, 0x2a, sizeof( iv ) ); |
| 345 | |
| 346 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 347 | |
| 348 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 349 | key->x, (size_t) key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 350 | |
| 351 | TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 352 | |
| 353 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 354 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 355 | output_buffer_size = (size_t) input->len + operation.block_size; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 356 | output = mbedtls_calloc( 1, output_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 357 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 358 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 359 | TEST_ASSERT( psa_cipher_update( &operation, input->x, (size_t) input->len, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 360 | output, output_buffer_size, |
| 361 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 362 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 363 | status = psa_cipher_finish( &operation, |
| 364 | output + function_output_length, |
| 365 | output_buffer_size, |
| 366 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 367 | total_output_length += function_output_length; |
| 368 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 369 | TEST_ASSERT( status == (psa_status_t) expected_status ); |
| 370 | if( expected_status == PSA_SUCCESS ) |
| 371 | { |
| 372 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 373 | TEST_ASSERT( total_output_length == (size_t) expected_output->len ); |
| 374 | TEST_ASSERT( memcmp( expected_output->x, output, |
| 375 | (size_t) expected_output->len ) == 0 ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 376 | } |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 377 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 378 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 379 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 380 | psa_destroy_key( key_slot ); |
| 381 | mbedtls_psa_crypto_free( ); |
| 382 | } |
| 383 | /* END_CASE */ |
| 384 | |
| 385 | /* BEGIN_CASE */ |
| 386 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 387 | data_t *key, |
| 388 | data_t *input, |
| 389 | int first_part_size, |
| 390 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 391 | { |
| 392 | int key_slot = 1; |
| 393 | psa_key_type_t key_type = key_type_arg; |
| 394 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 395 | unsigned char iv[16] = {0}; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 396 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 397 | size_t output_buffer_size = 0; |
| 398 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 399 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 400 | psa_cipher_operation_t operation; |
| 401 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 402 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 403 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 404 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 405 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 406 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 407 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 408 | |
| 409 | memset( iv, 0x2a, sizeof( iv ) ); |
| 410 | |
| 411 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 412 | |
| 413 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 414 | key->x, (size_t) key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 415 | |
| 416 | TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 417 | |
| 418 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 419 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 420 | output_buffer_size = (size_t) input->len + operation.block_size; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 421 | output = mbedtls_calloc( 1, output_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 422 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 423 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 424 | TEST_ASSERT( (unsigned int) first_part_size < (size_t) input->len ); |
| 425 | TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 426 | output, output_buffer_size, |
| 427 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 428 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 429 | TEST_ASSERT( psa_cipher_update( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 430 | input->x + first_part_size, |
| 431 | (size_t) input->len - first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 432 | output, output_buffer_size, |
| 433 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 434 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 435 | TEST_ASSERT( psa_cipher_finish( &operation, |
| 436 | output + function_output_length, |
| 437 | output_buffer_size, |
| 438 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 439 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 440 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 441 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 442 | TEST_ASSERT( total_output_length == (size_t) expected_output->len ); |
| 443 | TEST_ASSERT( memcmp( expected_output->x, output, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 444 | (size_t) expected_output->len ) == 0 ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 445 | |
| 446 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 447 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 448 | psa_destroy_key( key_slot ); |
| 449 | mbedtls_psa_crypto_free( ); |
| 450 | } |
| 451 | /* END_CASE */ |
| 452 | |
| 453 | /* BEGIN_CASE */ |
| 454 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 455 | data_t *key, |
| 456 | data_t *input, |
| 457 | int first_part_size, |
| 458 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 459 | { |
| 460 | int key_slot = 1; |
| 461 | |
| 462 | psa_key_type_t key_type = key_type_arg; |
| 463 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 464 | unsigned char iv[16] = {0}; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 465 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 466 | size_t output_buffer_size = 0; |
| 467 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 468 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 469 | psa_cipher_operation_t operation; |
| 470 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 471 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 472 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 473 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 474 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 475 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 476 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 477 | |
| 478 | memset( iv, 0x2a, sizeof( iv ) ); |
| 479 | |
| 480 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 481 | |
| 482 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 483 | key->x, (size_t) key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 484 | |
| 485 | TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 486 | |
| 487 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 488 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
| 489 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 490 | output_buffer_size = (size_t) input->len + operation.block_size; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 491 | output = mbedtls_calloc( 1, output_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 492 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 493 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 494 | TEST_ASSERT( (unsigned int) first_part_size < (size_t) input->len ); |
| 495 | TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 496 | output, output_buffer_size, |
| 497 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 498 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 499 | TEST_ASSERT( psa_cipher_update( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 500 | input->x + first_part_size, |
| 501 | (size_t) input->len - first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 502 | output, output_buffer_size, |
| 503 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 504 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 505 | TEST_ASSERT( psa_cipher_finish( &operation, |
| 506 | output + function_output_length, |
| 507 | output_buffer_size, |
| 508 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 509 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 510 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 511 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 512 | TEST_ASSERT( total_output_length == (size_t) expected_output->len ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 513 | TEST_ASSERT( memcmp( expected_output->x, output, |
| 514 | (size_t) expected_output->len ) == 0 ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 515 | |
| 516 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 517 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 518 | psa_destroy_key( key_slot ); |
| 519 | mbedtls_psa_crypto_free( ); |
| 520 | } |
| 521 | /* END_CASE */ |
| 522 | |
| 523 | |
| 524 | /* BEGIN_CASE */ |
| 525 | void cipher_decrypt( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 526 | data_t *key, |
| 527 | data_t *input, data_t *expected_output, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 528 | int expected_status ) |
| 529 | { |
| 530 | int key_slot = 1; |
| 531 | psa_status_t status; |
| 532 | psa_key_type_t key_type = key_type_arg; |
| 533 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 534 | unsigned char iv[16] = {0}; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 535 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 536 | size_t output_buffer_size = 0; |
| 537 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 538 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 539 | psa_cipher_operation_t operation; |
| 540 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 541 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 542 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 543 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 544 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 545 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 546 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 547 | |
| 548 | memset( iv, 0x2a, sizeof( iv ) ); |
| 549 | |
| 550 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 551 | |
| 552 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 553 | key->x, (size_t) key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 554 | |
| 555 | TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 556 | |
| 557 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 558 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
| 559 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 560 | output_buffer_size = (size_t) input->len + operation.block_size; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 561 | output = mbedtls_calloc( 1, output_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 562 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 563 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 564 | TEST_ASSERT( psa_cipher_update( &operation, input->x, (size_t) input->len, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 565 | output, output_buffer_size, |
| 566 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 567 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 568 | status = psa_cipher_finish( &operation, |
| 569 | output + function_output_length, |
| 570 | output_buffer_size, |
| 571 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 572 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 573 | TEST_ASSERT( status == (psa_status_t) expected_status ); |
| 574 | |
| 575 | if( expected_status == PSA_SUCCESS ) |
| 576 | { |
| 577 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 578 | TEST_ASSERT( total_output_length == (size_t) expected_output->len ); |
| 579 | TEST_ASSERT( memcmp( expected_output->x, output, |
| 580 | (size_t) expected_output->len ) == 0 ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | |
| 584 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 585 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 586 | psa_destroy_key( key_slot ); |
| 587 | mbedtls_psa_crypto_free( ); |
| 588 | } |
| 589 | /* END_CASE */ |
| 590 | |
| 591 | |
| 592 | /* BEGIN_CASE */ |
| 593 | void cipher_verify_output( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 594 | data_t *key, |
| 595 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 596 | { |
| 597 | int key_slot = 1; |
| 598 | psa_key_type_t key_type = key_type_arg; |
| 599 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 600 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 601 | size_t iv_size = 16; |
| 602 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 603 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 604 | size_t output1_size = 0; |
| 605 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 606 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 607 | size_t output2_size = 0; |
| 608 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 609 | size_t function_output_length = 0; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 610 | psa_cipher_operation_t operation1; |
| 611 | psa_cipher_operation_t operation2; |
| 612 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 613 | TEST_ASSERT( key != NULL ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 614 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 615 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 616 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 617 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 618 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 619 | |
| 620 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 621 | key->x, (size_t) key->len ) == PSA_SUCCESS ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 622 | |
Moran Peker | f55e804 | 2018-05-29 16:28:28 +0300 | [diff] [blame] | 623 | TEST_ASSERT( psa_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS ); |
| 624 | TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 625 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 626 | TEST_ASSERT( psa_encrypt_generate_iv( &operation1, |
| 627 | iv, iv_size, |
| 628 | &iv_length ) == PSA_SUCCESS ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 629 | output1_size = (size_t) input->len + operation1.block_size; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 630 | output1 = mbedtls_calloc( 1, output1_size ); |
| 631 | TEST_ASSERT( output1 != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 632 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 633 | TEST_ASSERT( psa_cipher_update( &operation1, input->x, (size_t) input->len, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 634 | output1, output1_size, |
| 635 | &output1_length ) == PSA_SUCCESS ); |
| 636 | TEST_ASSERT( psa_cipher_finish( &operation1, |
| 637 | output1 + output1_length, output1_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 638 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 639 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 640 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 641 | |
| 642 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 643 | |
| 644 | output2_size = output1_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 645 | output2 = mbedtls_calloc( 1, output2_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 646 | TEST_ASSERT( output2 != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 647 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 648 | TEST_ASSERT( psa_encrypt_set_iv( &operation2, |
| 649 | iv, iv_length ) == PSA_SUCCESS ); |
| 650 | TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 651 | output2, output2_size, |
| 652 | &output2_length ) == PSA_SUCCESS ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 653 | function_output_length = 0; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 654 | TEST_ASSERT( psa_cipher_finish( &operation2, |
| 655 | output2 + output2_length, |
| 656 | output2_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 657 | &function_output_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 658 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 659 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 660 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 661 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 662 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 663 | TEST_ASSERT( (size_t) input->len == output2_length ); |
| 664 | TEST_ASSERT( memcmp( input->x, output2, (size_t) input->len ) == 0 ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 665 | |
| 666 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 667 | mbedtls_free( output1 ); |
| 668 | mbedtls_free( output2 ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 669 | psa_destroy_key( key_slot ); |
| 670 | mbedtls_psa_crypto_free( ); |
| 671 | } |
| 672 | /* END_CASE */ |
| 673 | |
| 674 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 675 | void cipher_verify_output_multipart( int alg_arg, |
| 676 | int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 677 | data_t *key, |
| 678 | data_t *input, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 679 | int first_part_size ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 680 | { |
| 681 | int key_slot = 1; |
| 682 | psa_key_type_t key_type = key_type_arg; |
| 683 | psa_algorithm_t alg = alg_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 684 | unsigned char iv[16] = {0}; |
| 685 | size_t iv_size = 16; |
| 686 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 687 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 688 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 689 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 690 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 691 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 692 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 693 | size_t function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 694 | psa_cipher_operation_t operation1; |
| 695 | psa_cipher_operation_t operation2; |
| 696 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 697 | TEST_ASSERT( key != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 698 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 699 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 700 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 701 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 702 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 703 | |
| 704 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 705 | key->x, (size_t) key->len ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 706 | |
| 707 | TEST_ASSERT( psa_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS ); |
| 708 | TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS ); |
| 709 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 710 | TEST_ASSERT( psa_encrypt_generate_iv( &operation1, |
| 711 | iv, iv_size, |
| 712 | &iv_length ) == PSA_SUCCESS ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 713 | output1_buffer_size = (size_t) input->len + operation1.block_size; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 714 | output1 = mbedtls_calloc( 1, output1_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 715 | TEST_ASSERT( output1 != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 716 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 717 | TEST_ASSERT( (unsigned int) first_part_size < (size_t) input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 718 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 719 | TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 720 | output1, output1_buffer_size, |
| 721 | &function_output_length ) == PSA_SUCCESS ); |
| 722 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 723 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 724 | TEST_ASSERT( psa_cipher_update( &operation1, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 725 | input->x + first_part_size, |
| 726 | (size_t) input->len - first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 727 | output1, output1_buffer_size, |
| 728 | &function_output_length ) == PSA_SUCCESS ); |
| 729 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 730 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 731 | TEST_ASSERT( psa_cipher_finish( &operation1, |
| 732 | output1 + output1_length, |
| 733 | output1_buffer_size - output1_length, |
| 734 | &function_output_length ) == PSA_SUCCESS ); |
| 735 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 736 | |
| 737 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 738 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 739 | output2_buffer_size = output1_length; |
| 740 | output2 = mbedtls_calloc( 1, output2_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 741 | TEST_ASSERT( output2 != NULL ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 742 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 743 | TEST_ASSERT( psa_encrypt_set_iv( &operation2, |
| 744 | iv, iv_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 745 | |
| 746 | TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 747 | output2, output2_buffer_size, |
| 748 | &function_output_length ) == PSA_SUCCESS ); |
| 749 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 750 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 751 | TEST_ASSERT( psa_cipher_update( &operation2, |
| 752 | output1 + first_part_size, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 753 | output1_length - first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 754 | output2, output2_buffer_size, |
| 755 | &function_output_length ) == PSA_SUCCESS ); |
| 756 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 757 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 758 | TEST_ASSERT( psa_cipher_finish( &operation2, |
| 759 | output2 + output2_length, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 760 | output2_buffer_size - output2_length, |
| 761 | &function_output_length ) == PSA_SUCCESS ); |
| 762 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 763 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 764 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 765 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 766 | TEST_ASSERT( (size_t) input->len == output2_length ); |
| 767 | TEST_ASSERT( memcmp( input->x, output2, (size_t) input->len ) == 0 ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 768 | |
| 769 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 770 | mbedtls_free( output1 ); |
| 771 | mbedtls_free( output2 ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 772 | psa_destroy_key( key_slot ); |
| 773 | mbedtls_psa_crypto_free( ); |
| 774 | } |
| 775 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 776 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 777 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 778 | void aead_encrypt_decrypt( int key_type_arg, data_t * key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 779 | int alg_arg, data_t * input_data, data_t * nonce, |
| 780 | data_t * additional_data, int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 781 | { |
| 782 | int slot = 1; |
| 783 | psa_key_type_t key_type = key_type_arg; |
| 784 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 785 | unsigned char *output_data = NULL; |
| 786 | size_t output_size = 0; |
| 787 | size_t output_length = 0; |
| 788 | unsigned char *output_data2 = NULL; |
| 789 | size_t output_length2 = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 790 | size_t tag_length = 16; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 791 | psa_status_t expected_result = (psa_status_t) expected_result_arg; |
| 792 | psa_key_policy_t policy = {0}; |
| 793 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 794 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 795 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 796 | TEST_ASSERT( nonce != NULL ); |
| 797 | TEST_ASSERT( additional_data != NULL ); |
| 798 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 799 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 800 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 801 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 802 | |
| 803 | output_size = (size_t) input_data->len + tag_length; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 804 | output_data = mbedtls_calloc( 1, output_size ); |
| 805 | TEST_ASSERT( output_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 806 | |
| 807 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 808 | |
| 809 | psa_key_policy_init( &policy ); |
| 810 | |
| 811 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT , alg ); |
| 812 | |
| 813 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 814 | |
| 815 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 816 | key_data->x, (size_t) key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 817 | |
| 818 | TEST_ASSERT( psa_aead_encrypt( slot, alg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 819 | nonce->x, (size_t) nonce->len, |
| 820 | additional_data->x, |
| 821 | (size_t) additional_data->len, |
| 822 | input_data->x, (size_t) input_data->len, |
| 823 | output_data, |
| 824 | output_size, &output_length ) == |
| 825 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 826 | |
| 827 | if( PSA_SUCCESS == expected_result ) |
| 828 | { |
| 829 | output_data2 = mbedtls_calloc( 1, output_length ); |
| 830 | TEST_ASSERT( output_data2 != NULL ); |
| 831 | |
| 832 | TEST_ASSERT( psa_aead_decrypt( slot, alg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 833 | nonce->x, (size_t) nonce->len, |
| 834 | additional_data->x, |
| 835 | (size_t) additional_data->len, |
| 836 | output_data, output_length, output_data2, |
| 837 | output_length, &output_length2 ) == |
| 838 | expected_result ); |
| 839 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 840 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 841 | TEST_ASSERT( memcmp( input_data->x, output_data2, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 842 | (size_t) input_data->len ) == 0 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 843 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 844 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 845 | |
| 846 | exit: |
| 847 | psa_destroy_key( slot ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 848 | mbedtls_free( output_data ); |
| 849 | mbedtls_free( output_data2 ); |
| 850 | mbedtls_psa_crypto_free( ); |
| 851 | } |
| 852 | /* END_CASE */ |
| 853 | |
| 854 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 855 | void aead_encrypt( int key_type_arg, data_t * key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 856 | int alg_arg, data_t * input_data, |
| 857 | data_t * additional_data, data_t * nonce, |
| 858 | data_t * expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 859 | { |
| 860 | int slot = 1; |
| 861 | psa_key_type_t key_type = key_type_arg; |
| 862 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 863 | unsigned char *output_data = NULL; |
| 864 | size_t output_size = 0; |
| 865 | size_t output_length = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 866 | size_t tag_length = 16; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 867 | psa_key_policy_t policy = {0}; |
| 868 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 869 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 870 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 871 | TEST_ASSERT( additional_data != NULL ); |
| 872 | TEST_ASSERT( nonce != NULL ); |
| 873 | TEST_ASSERT( expected_result != NULL ); |
| 874 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 875 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 876 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 877 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 878 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) ); |
| 879 | |
| 880 | output_size = (size_t) input_data->len + tag_length; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 881 | output_data = mbedtls_calloc( 1, output_size ); |
| 882 | TEST_ASSERT( output_data != NULL ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 883 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 884 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 885 | |
| 886 | psa_key_policy_init( &policy ); |
| 887 | |
| 888 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg ); |
| 889 | |
| 890 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 891 | |
| 892 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 893 | key_data->x, (size_t) key_data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 894 | PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 895 | |
| 896 | TEST_ASSERT( psa_aead_encrypt( slot, alg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 897 | nonce->x, (size_t) nonce->len, |
| 898 | additional_data->x, |
| 899 | (size_t) additional_data->len, |
| 900 | input_data->x, (size_t) input_data->len, |
| 901 | output_data, |
| 902 | output_size, &output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 903 | |
| 904 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 905 | TEST_ASSERT( memcmp( output_data, expected_result->x, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 906 | output_length ) == 0 ); |
| 907 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 908 | |
| 909 | exit: |
| 910 | psa_destroy_key( slot ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 911 | mbedtls_free( output_data ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 912 | mbedtls_psa_crypto_free( ); |
| 913 | } |
| 914 | /* END_CASE */ |
| 915 | |
| 916 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 917 | void aead_decrypt( int key_type_arg, data_t * key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 918 | int alg_arg, data_t * input_data, |
| 919 | data_t * additional_data, data_t * nonce, |
| 920 | data_t * expected_data, int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 921 | { |
| 922 | int slot = 1; |
| 923 | psa_key_type_t key_type = key_type_arg; |
| 924 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 925 | unsigned char *output_data = NULL; |
| 926 | size_t output_size = 0; |
| 927 | size_t output_length = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 928 | size_t tag_length = 16; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 929 | psa_key_policy_t policy = {0}; |
| 930 | psa_status_t expected_result = (psa_status_t) expected_result_arg; |
| 931 | |
| 932 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 933 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 934 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 935 | TEST_ASSERT( additional_data != NULL ); |
| 936 | TEST_ASSERT( nonce != NULL ); |
| 937 | TEST_ASSERT( expected_data != NULL ); |
| 938 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 939 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 940 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 941 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 942 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) ); |
| 943 | |
| 944 | output_size = (size_t) input_data->len + tag_length; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 945 | output_data = mbedtls_calloc( 1, output_size ); |
| 946 | TEST_ASSERT( output_data != NULL ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 947 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 948 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 949 | |
| 950 | psa_key_policy_init( &policy ); |
| 951 | |
| 952 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg ); |
| 953 | |
| 954 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 955 | |
| 956 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 957 | key_data->x, (size_t) key_data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 958 | PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 959 | |
| 960 | TEST_ASSERT( psa_aead_decrypt( slot, alg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 961 | nonce->x, (size_t) nonce->len, |
| 962 | additional_data->x, (size_t) additional_data->len, |
| 963 | input_data->x, (size_t) input_data->len, |
| 964 | output_data, |
| 965 | output_size, &output_length ) == |
| 966 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 967 | |
| 968 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 969 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 970 | { |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 971 | TEST_ASSERT( memcmp( output_data, expected_data->x, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 972 | output_length ) == 0 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 973 | } |
| 974 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 975 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 976 | |
| 977 | exit: |
| 978 | psa_destroy_key( slot ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 979 | mbedtls_free( output_data ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 980 | mbedtls_psa_crypto_free( ); |
| 981 | } |
| 982 | /* END_CASE */ |
| 983 | |
| 984 | /* BEGIN_CASE */ |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 985 | void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg ) |
| 986 | { |
| 987 | psa_key_type_t type = type_arg; |
| 988 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 989 | size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 990 | TEST_ASSERT( actual_size == (size_t) expected_size_arg ); |
| 991 | exit: |
| 992 | ; |
| 993 | } |
| 994 | /* END_CASE */ |
| 995 | |
| 996 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 997 | void sign_deterministic( int key_type_arg, data_t *key_data, |
| 998 | int alg_arg, data_t *input_data, |
| 999 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1000 | { |
| 1001 | int slot = 1; |
| 1002 | psa_key_type_t key_type = key_type_arg; |
| 1003 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1004 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1005 | unsigned char *signature = NULL; |
| 1006 | size_t signature_size; |
| 1007 | size_t signature_length = 0xdeadbeef; |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1008 | psa_key_policy_t policy = {0}; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1009 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1010 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1011 | TEST_ASSERT( input_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1012 | TEST_ASSERT( output_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1013 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1014 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1015 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1016 | |
| 1017 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1018 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1019 | psa_key_policy_init( &policy ); |
| 1020 | |
| 1021 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg ); |
| 1022 | |
| 1023 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1024 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1025 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1026 | key_data->x, (size_t) key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1027 | TEST_ASSERT( psa_get_key_information( slot, |
| 1028 | NULL, |
| 1029 | &key_bits ) == PSA_SUCCESS ); |
| 1030 | |
itayzafrir | 27fbaf7 | 2018-06-12 17:09:28 +0300 | [diff] [blame] | 1031 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1032 | TEST_ASSERT( signature_size != 0 ); |
| 1033 | signature = mbedtls_calloc( 1, signature_size ); |
| 1034 | TEST_ASSERT( signature != NULL ); |
| 1035 | |
| 1036 | TEST_ASSERT( psa_asymmetric_sign( slot, alg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1037 | input_data->x, (size_t) input_data->len, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1038 | NULL, 0, |
| 1039 | signature, signature_size, |
| 1040 | &signature_length ) == PSA_SUCCESS ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1041 | TEST_ASSERT( signature_length == (size_t) output_data->len ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1042 | TEST_ASSERT( memcmp( signature, output_data->x, |
| 1043 | (size_t) output_data->len ) == 0 ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1044 | |
| 1045 | exit: |
| 1046 | psa_destroy_key( slot ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 1047 | mbedtls_free( signature ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1048 | mbedtls_psa_crypto_free( ); |
| 1049 | } |
| 1050 | /* END_CASE */ |
| 1051 | |
| 1052 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1053 | void sign_fail( int key_type_arg, data_t *key_data, |
| 1054 | int alg_arg, data_t *input_data, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1055 | int signature_size, int expected_status_arg ) |
| 1056 | { |
| 1057 | int slot = 1; |
| 1058 | psa_key_type_t key_type = key_type_arg; |
| 1059 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1060 | psa_status_t actual_status; |
| 1061 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 1062 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 1063 | size_t signature_length = 0xdeadbeef; |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1064 | psa_key_policy_t policy = {0}; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1065 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1066 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1067 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1068 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1069 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1070 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1071 | signature = mbedtls_calloc( 1, signature_size ); |
| 1072 | TEST_ASSERT( signature != NULL ); |
| 1073 | |
| 1074 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1075 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1076 | psa_key_policy_init( &policy ); |
| 1077 | |
| 1078 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg ); |
| 1079 | |
| 1080 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1081 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1082 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1083 | key_data->x, (size_t) key_data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1084 | PSA_SUCCESS ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1085 | |
| 1086 | actual_status = psa_asymmetric_sign( slot, alg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1087 | input_data->x, |
| 1088 | (size_t) input_data->len, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1089 | NULL, 0, |
| 1090 | signature, signature_size, |
| 1091 | &signature_length ); |
| 1092 | TEST_ASSERT( actual_status == expected_status ); |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 1093 | TEST_ASSERT( signature_length == 0 ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1094 | |
| 1095 | exit: |
| 1096 | psa_destroy_key( slot ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1097 | mbedtls_free( signature ); |
| 1098 | mbedtls_psa_crypto_free( ); |
| 1099 | } |
| 1100 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1101 | |
| 1102 | /* BEGIN_CASE */ |
| 1103 | void key_policy( int usage_arg, int alg_arg ) |
| 1104 | { |
| 1105 | int key_slot = 1; |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 1106 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1107 | unsigned char key[32] = {0}; |
| 1108 | psa_key_policy_t policy_set = {0}; |
| 1109 | psa_key_policy_t policy_get = {0}; |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1110 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1111 | memset( key, 0x2a, sizeof( key ) ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1112 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1113 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1114 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1115 | psa_key_policy_init( &policy_set ); |
| 1116 | psa_key_policy_init( &policy_get ); |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1117 | |
| 1118 | psa_key_policy_set_usage( &policy_set, usage_arg, alg_arg ); |
| 1119 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1120 | TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == (psa_key_usage_t) usage_arg ); |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1121 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1122 | TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == (psa_algorithm_t) alg_arg ); |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1123 | |
| 1124 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS ); |
| 1125 | |
| 1126 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 1127 | key, sizeof( key ) ) == PSA_SUCCESS ); |
| 1128 | |
| 1129 | TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS ); |
| 1130 | |
| 1131 | TEST_ASSERT( policy_get.usage == policy_set.usage ); |
| 1132 | TEST_ASSERT( policy_get.alg == policy_set.alg ); |
| 1133 | |
| 1134 | exit: |
| 1135 | psa_destroy_key( key_slot ); |
| 1136 | mbedtls_psa_crypto_free( ); |
| 1137 | } |
| 1138 | /* END_CASE */ |
| 1139 | |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 1140 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1141 | void key_policy_fail( int usage_arg, int alg_arg, int expected_status, |
| 1142 | data_t *keypair ) |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 1143 | { |
| 1144 | int key_slot = 1; |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 1145 | size_t signature_length = 0; |
| 1146 | psa_key_policy_t policy = {0}; |
| 1147 | int actual_status = PSA_SUCCESS; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1148 | |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 1149 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1150 | |
| 1151 | psa_key_policy_init( &policy ); |
| 1152 | |
| 1153 | psa_key_policy_set_usage( &policy, usage_arg, alg_arg ); |
| 1154 | |
| 1155 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 1156 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1157 | if( usage_arg & PSA_KEY_USAGE_EXPORT ) |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 1158 | { |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1159 | TEST_ASSERT( keypair != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1160 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1161 | TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1162 | keypair->x, (size_t) keypair->len ) == |
| 1163 | PSA_SUCCESS ); |
| 1164 | actual_status = psa_asymmetric_sign( key_slot, |
| 1165 | (psa_algorithm_t) alg_arg, |
| 1166 | NULL, 0, |
| 1167 | NULL, 0, |
| 1168 | NULL, 0, &signature_length ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1169 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1170 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1171 | if( usage_arg & PSA_KEY_USAGE_SIGN ) |
| 1172 | { |
mohammad1603 | d926b88 | 2018-04-16 01:53:20 -0700 | [diff] [blame] | 1173 | TEST_ASSERT( keypair != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1174 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1175 | TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1176 | keypair->x, (size_t) keypair->len ) == |
| 1177 | PSA_SUCCESS ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1178 | actual_status = psa_export_key( key_slot, NULL, 0, NULL ); |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | TEST_ASSERT( actual_status == expected_status ); |
| 1182 | |
| 1183 | exit: |
| 1184 | psa_destroy_key( key_slot ); |
| 1185 | mbedtls_psa_crypto_free( ); |
| 1186 | } |
| 1187 | /* END_CASE */ |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 1188 | |
| 1189 | /* BEGIN_CASE */ |
| 1190 | void key_lifetime( int lifetime_arg ) |
| 1191 | { |
| 1192 | int key_slot = 1; |
| 1193 | psa_key_type_t key_type = PSA_ALG_CBC_BASE; |
| 1194 | unsigned char key[32] = {0}; |
| 1195 | psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg; |
| 1196 | psa_key_lifetime_t lifetime_get; |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 1197 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 1198 | memset( key, 0x2a, sizeof( key ) ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1199 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 1200 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 1201 | |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1202 | TEST_ASSERT( psa_set_key_lifetime( key_slot, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1203 | lifetime_set ) == PSA_SUCCESS ); |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 1204 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 1205 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 1206 | key, sizeof( key ) ) == PSA_SUCCESS ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1207 | |
| 1208 | TEST_ASSERT( psa_get_key_lifetime( key_slot, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1209 | &lifetime_get ) == PSA_SUCCESS ); |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 1210 | |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1211 | TEST_ASSERT( lifetime_get == lifetime_set ); |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 1212 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 1213 | exit: |
| 1214 | psa_destroy_key( key_slot ); |
| 1215 | mbedtls_psa_crypto_free( ); |
| 1216 | } |
| 1217 | /* END_CASE */ |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1218 | |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 1219 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1220 | /* BEGIN_CASE */ |
| 1221 | void key_lifetime_set_fail( int key_slot_arg, int lifetime_arg, int expected_status_arg ) |
| 1222 | { |
| 1223 | int key_slot = 1; |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1224 | psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg; |
| 1225 | psa_status_t actual_status; |
| 1226 | psa_status_t expected_status = expected_status_arg; |
| 1227 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1228 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1229 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1230 | actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set ); |
| 1231 | |
| 1232 | if( actual_status == PSA_SUCCESS ) |
| 1233 | actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1234 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1235 | TEST_ASSERT( expected_status == actual_status ); |
| 1236 | |
| 1237 | exit: |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1238 | psa_destroy_key( key_slot ); |
| 1239 | mbedtls_psa_crypto_free( ); |
| 1240 | } |
| 1241 | /* END_CASE */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1242 | |
| 1243 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1244 | void asymmetric_verify( int key_type_arg, data_t *key_data, |
| 1245 | int alg_arg, data_t *hash_data, |
| 1246 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1247 | { |
| 1248 | int slot = 1; |
| 1249 | psa_key_type_t key_type = key_type_arg; |
| 1250 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1251 | psa_key_policy_t policy = {0}; |
| 1252 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1253 | TEST_ASSERT( key_data != NULL ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1254 | TEST_ASSERT( hash_data != NULL ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1255 | TEST_ASSERT( signature_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1256 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1257 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) ); |
| 1258 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1259 | |
| 1260 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1261 | |
| 1262 | psa_key_policy_init( &policy ); |
| 1263 | |
| 1264 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg_arg ); |
| 1265 | |
| 1266 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1267 | |
| 1268 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1269 | key_data->x, (size_t) key_data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1270 | PSA_SUCCESS ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1271 | |
| 1272 | TEST_ASSERT( psa_asymmetric_verify( slot, alg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1273 | hash_data->x, (size_t) hash_data->len, |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1274 | NULL, 0, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1275 | signature_data->x, |
| 1276 | (size_t) signature_data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1277 | PSA_SUCCESS ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1278 | exit: |
| 1279 | psa_destroy_key( slot ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1280 | mbedtls_psa_crypto_free( ); |
| 1281 | } |
| 1282 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1283 | |
| 1284 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1285 | void asymmetric_verify_fail( int key_type_arg, data_t *key_data, |
| 1286 | int alg_arg, data_t *hash_data, |
| 1287 | data_t *signature_data, |
| 1288 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1289 | { |
| 1290 | |
| 1291 | int slot = 1; |
| 1292 | psa_key_type_t key_type = key_type_arg; |
| 1293 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1294 | psa_status_t actual_status; |
| 1295 | psa_status_t expected_status = expected_status_arg; |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1296 | psa_key_policy_t policy = {0}; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1297 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1298 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1299 | TEST_ASSERT( hash_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1300 | TEST_ASSERT( signature_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1301 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1302 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) ); |
| 1303 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1304 | |
| 1305 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1306 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1307 | psa_key_policy_init( &policy ); |
| 1308 | |
| 1309 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg_arg ); |
| 1310 | |
| 1311 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1312 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1313 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1314 | key_data->x, (size_t) key_data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1315 | PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1316 | |
| 1317 | actual_status = psa_asymmetric_verify( slot, alg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1318 | hash_data->x, (size_t) hash_data->len, |
| 1319 | NULL, 0, |
| 1320 | signature_data->x, |
| 1321 | (size_t) signature_data->len ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1322 | |
| 1323 | |
| 1324 | TEST_ASSERT( actual_status == expected_status ); |
| 1325 | |
| 1326 | exit: |
| 1327 | psa_destroy_key( slot ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1328 | mbedtls_psa_crypto_free( ); |
| 1329 | } |
| 1330 | /* END_CASE */ |
| 1331 | |
| 1332 | |
| 1333 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1334 | void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data, |
| 1335 | int alg_arg, data_t *input_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1336 | { |
| 1337 | int slot = 1; |
| 1338 | psa_key_type_t key_type = key_type_arg; |
| 1339 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1340 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1341 | size_t output_size = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1342 | size_t output_length = 0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1343 | unsigned char *output2 = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1344 | size_t output2_size = 0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1345 | size_t output2_length = 0; |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1346 | psa_key_policy_t policy = {0}; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1347 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1348 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1349 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1350 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1351 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1352 | |
| 1353 | output_size = (size_t) key_data->len; |
| 1354 | output2_size = output_size; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1355 | output = mbedtls_calloc( 1, output_size ); |
| 1356 | TEST_ASSERT( output != NULL ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1357 | output2 = mbedtls_calloc( 1, output2_size ); |
| 1358 | TEST_ASSERT( output2 != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1359 | |
| 1360 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1361 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1362 | psa_key_policy_init( &policy ); |
| 1363 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg_arg ); |
| 1364 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1365 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1366 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1367 | key_data->x, (size_t) key_data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1368 | PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1369 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 1370 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 1371 | * the original plaintext because of the non-optional random |
| 1372 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1373 | TEST_ASSERT( psa_asymmetric_encrypt( slot, alg, |
| 1374 | input_data->x, |
| 1375 | (size_t) input_data->len, |
| 1376 | NULL, 0, |
| 1377 | output, |
| 1378 | output_size, |
| 1379 | &output_length ) == PSA_SUCCESS ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1380 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1381 | TEST_ASSERT( psa_asymmetric_decrypt( slot, alg, |
| 1382 | output, |
| 1383 | output_length, |
| 1384 | NULL, 0, |
| 1385 | output2, |
| 1386 | output2_size, |
| 1387 | &output2_length ) == PSA_SUCCESS ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1388 | TEST_ASSERT( memcmp( input_data->x, output2, (size_t) input_data->len ) |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1389 | == 0 ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1390 | |
| 1391 | exit: |
| 1392 | psa_destroy_key( slot ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1393 | mbedtls_free( output ); |
| 1394 | mbedtls_free( output2 ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1395 | mbedtls_psa_crypto_free( ); |
| 1396 | |
| 1397 | } |
| 1398 | /* END_CASE */ |
| 1399 | |
| 1400 | |
| 1401 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1402 | void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1403 | int alg_arg, data_t *input_data, |
| 1404 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1405 | { |
| 1406 | |
| 1407 | |
| 1408 | int slot = 1; |
| 1409 | psa_key_type_t key_type = key_type_arg; |
| 1410 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1411 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1412 | size_t output_size = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1413 | size_t output_length = 0; |
| 1414 | psa_status_t actual_status; |
| 1415 | psa_status_t expected_status = expected_status_arg; |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1416 | psa_key_policy_t policy = {0}; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1417 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1418 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1419 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1420 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1421 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1422 | |
| 1423 | output_size = (size_t) key_data->len; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1424 | output = mbedtls_calloc( 1, output_size ); |
| 1425 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1426 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1427 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1428 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1429 | psa_key_policy_init( &policy ); |
| 1430 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg_arg ); |
| 1431 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1432 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1433 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1434 | key_data->x, (size_t) key_data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1435 | PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1436 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1437 | actual_status = psa_asymmetric_encrypt( slot, alg, |
| 1438 | input_data->x, |
| 1439 | (size_t) input_data->len, |
| 1440 | NULL, 0, |
| 1441 | output, |
| 1442 | output_size, |
| 1443 | &output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1444 | TEST_ASSERT( actual_status == expected_status ); |
| 1445 | |
| 1446 | exit: |
| 1447 | psa_destroy_key( slot ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1448 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1449 | mbedtls_psa_crypto_free( ); |
| 1450 | |
| 1451 | } |
| 1452 | /* END_CASE */ |
| 1453 | |
| 1454 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1455 | void asymmetric_decrypt( int key_type_arg, data_t *key_data, |
| 1456 | int alg_arg, data_t *input_data, |
| 1457 | data_t *expected_data, int expected_size ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1458 | { |
| 1459 | int slot = 1; |
| 1460 | psa_key_type_t key_type = key_type_arg; |
| 1461 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1462 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1463 | size_t output_size = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1464 | size_t output_length = 0; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1465 | psa_key_policy_t policy = {0}; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1466 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1467 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1468 | TEST_ASSERT( input_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1469 | TEST_ASSERT( expected_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1470 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1471 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1472 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) ); |
| 1473 | |
| 1474 | output_size = (size_t) key_data->len; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1475 | output = mbedtls_calloc( 1, output_size ); |
| 1476 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1477 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1478 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1479 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1480 | psa_key_policy_init( &policy ); |
| 1481 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg_arg ); |
| 1482 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1483 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1484 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1485 | key_data->x, (size_t) key_data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1486 | PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1487 | |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1488 | TEST_ASSERT( psa_asymmetric_decrypt( slot, alg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1489 | input_data->x, |
| 1490 | (size_t) input_data->len, |
| 1491 | NULL, 0, |
| 1492 | output, |
| 1493 | output_size, |
| 1494 | &output_length ) == PSA_SUCCESS ); |
| 1495 | TEST_ASSERT( ( (size_t) expected_size ) == output_length ); |
| 1496 | TEST_ASSERT( memcmp( expected_data->x, output, ( output_length ) ) == 0 ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1497 | |
| 1498 | exit: |
| 1499 | psa_destroy_key( slot ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1500 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1501 | mbedtls_psa_crypto_free( ); |
| 1502 | |
| 1503 | |
| 1504 | } |
| 1505 | /* END_CASE */ |
| 1506 | |
| 1507 | |
| 1508 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1509 | void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1510 | int alg_arg, data_t *input_data, |
| 1511 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1512 | { |
| 1513 | |
| 1514 | int slot = 1; |
| 1515 | psa_key_type_t key_type = key_type_arg; |
| 1516 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1517 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1518 | size_t output_size = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1519 | size_t output_length = 0; |
| 1520 | psa_status_t actual_status; |
| 1521 | psa_status_t expected_status = expected_status_arg; |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1522 | psa_key_policy_t policy = {0}; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1523 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1524 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1525 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1526 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1527 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1528 | |
| 1529 | output_size = (size_t) key_data->len; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1530 | output = mbedtls_calloc( 1, output_size ); |
| 1531 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1532 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1533 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1534 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1535 | psa_key_policy_init( &policy ); |
| 1536 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg_arg ); |
| 1537 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1538 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1539 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1540 | key_data->x, (size_t) key_data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1541 | PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1542 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1543 | actual_status = psa_asymmetric_decrypt( slot, alg, |
| 1544 | input_data->x, |
| 1545 | (size_t) input_data->len, |
| 1546 | NULL, 0, |
| 1547 | output, |
| 1548 | output_size, |
| 1549 | &output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1550 | TEST_ASSERT( actual_status == expected_status ); |
| 1551 | |
| 1552 | exit: |
| 1553 | psa_destroy_key( slot ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame^] | 1554 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1555 | mbedtls_psa_crypto_free( ); |
| 1556 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1557 | /* END_CASE */ |