Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
| 2 | #include "psa/crypto.h" |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3 | |
| 4 | #include "mbedtls/md.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5 | /* END_HEADER */ |
| 6 | |
| 7 | /* BEGIN_DEPENDENCIES |
| 8 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 9 | * END_DEPENDENCIES |
| 10 | */ |
| 11 | |
| 12 | /* BEGIN_CASE */ |
| 13 | void init_deinit() |
| 14 | { |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 15 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 16 | int i; |
| 17 | for( i = 0; i <= 1; i++ ) |
| 18 | { |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 19 | status = psa_crypto_init( ); |
| 20 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 21 | status = psa_crypto_init( ); |
| 22 | TEST_ASSERT( status == PSA_SUCCESS ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 23 | mbedtls_psa_crypto_free( ); |
| 24 | } |
| 25 | } |
| 26 | /* END_CASE */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 27 | |
| 28 | /* BEGIN_CASE */ |
| 29 | void import( char *hex, int type, int expected_status ) |
| 30 | { |
| 31 | int slot = 1; |
| 32 | psa_status_t status; |
| 33 | unsigned char *data = NULL; |
| 34 | size_t data_size; |
| 35 | |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 36 | data = unhexify_alloc( hex, &data_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 37 | TEST_ASSERT( data != NULL ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 38 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 39 | |
| 40 | status = psa_import_key( slot, type, data, data_size ); |
| 41 | TEST_ASSERT( status == (psa_status_t) expected_status ); |
| 42 | if( status == PSA_SUCCESS ) |
| 43 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 44 | |
| 45 | exit: |
| 46 | mbedtls_free( data ); |
| 47 | mbedtls_psa_crypto_free( ); |
| 48 | } |
| 49 | /* END_CASE */ |
| 50 | |
| 51 | /* BEGIN_CASE */ |
| 52 | void import_export( char *hex, int type_arg, |
| 53 | int expected_bits, |
| 54 | int export_size_delta, |
| 55 | int expected_export_status, |
| 56 | int canonical_input ) |
| 57 | { |
| 58 | int slot = 1; |
| 59 | int slot2 = slot + 1; |
| 60 | psa_key_type_t type = type_arg; |
| 61 | psa_status_t status; |
| 62 | unsigned char *data = NULL; |
| 63 | unsigned char *exported = NULL; |
| 64 | unsigned char *reexported = NULL; |
| 65 | size_t data_size; |
| 66 | size_t export_size; |
| 67 | size_t exported_length; |
| 68 | size_t reexported_length; |
| 69 | psa_key_type_t got_type; |
| 70 | size_t got_bits; |
| 71 | |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 72 | data = unhexify_alloc( hex, &data_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 73 | TEST_ASSERT( data != NULL ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 74 | export_size = (ssize_t) data_size + export_size_delta; |
| 75 | exported = mbedtls_calloc( 1, export_size ); |
| 76 | TEST_ASSERT( exported != NULL ); |
| 77 | if( ! canonical_input ) |
| 78 | { |
| 79 | reexported = mbedtls_calloc( 1, export_size ); |
| 80 | TEST_ASSERT( reexported != NULL ); |
| 81 | } |
| 82 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 83 | |
| 84 | /* Import the key */ |
| 85 | TEST_ASSERT( psa_import_key( slot, type, |
| 86 | data, data_size ) == PSA_SUCCESS ); |
| 87 | |
| 88 | /* Test the key information */ |
| 89 | TEST_ASSERT( psa_get_key_information( slot, |
| 90 | &got_type, &got_bits ) == |
| 91 | PSA_SUCCESS ); |
| 92 | TEST_ASSERT( got_type == type ); |
| 93 | TEST_ASSERT( got_bits == (size_t) expected_bits ); |
| 94 | |
| 95 | /* Export the key */ |
| 96 | status = psa_export_key( slot, |
| 97 | exported, export_size, |
| 98 | &exported_length ); |
| 99 | TEST_ASSERT( status == (psa_status_t) expected_export_status ); |
| 100 | if( status != PSA_SUCCESS ) |
| 101 | goto destroy; |
| 102 | |
| 103 | if( canonical_input ) |
| 104 | { |
| 105 | TEST_ASSERT( exported_length == data_size ); |
| 106 | TEST_ASSERT( memcmp( exported, data, data_size ) == 0 ); |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | TEST_ASSERT( psa_import_key( slot2, type, |
| 111 | exported, export_size ) == |
| 112 | PSA_SUCCESS ); |
| 113 | TEST_ASSERT( psa_export_key( slot2, |
| 114 | reexported, export_size, |
| 115 | &reexported_length ) == |
| 116 | PSA_SUCCESS ); |
| 117 | TEST_ASSERT( reexported_length == exported_length ); |
| 118 | TEST_ASSERT( memcmp( reexported, exported, |
| 119 | exported_length ) == 0 ); |
| 120 | } |
| 121 | |
| 122 | destroy: |
| 123 | /* Destroy the key */ |
| 124 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 125 | TEST_ASSERT( psa_get_key_information( |
| 126 | slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT ); |
| 127 | |
| 128 | exit: |
| 129 | mbedtls_free( data ); |
| 130 | mbedtls_psa_crypto_free( ); |
| 131 | } |
| 132 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 133 | |
| 134 | /* BEGIN_CASE */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 135 | void hash_finish( int alg_arg, char *input_hex, char *hash_hex ) |
| 136 | { |
| 137 | psa_algorithm_t alg = alg_arg; |
| 138 | unsigned char *input = NULL; |
| 139 | size_t input_size; |
| 140 | unsigned char expected_hash[MBEDTLS_MD_MAX_SIZE]; |
| 141 | size_t expected_hash_length; |
| 142 | unsigned char actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 143 | size_t actual_hash_length; |
| 144 | psa_hash_operation_t operation; |
| 145 | |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 146 | input = unhexify_alloc( input_hex, &input_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 147 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 148 | expected_hash_length = unhexify( expected_hash, hash_hex ); |
| 149 | |
| 150 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 151 | |
| 152 | TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS ); |
| 153 | TEST_ASSERT( psa_hash_update( &operation, |
| 154 | input, input_size ) == PSA_SUCCESS ); |
| 155 | TEST_ASSERT( psa_hash_finish( &operation, |
| 156 | actual_hash, sizeof( actual_hash ), |
| 157 | &actual_hash_length ) == PSA_SUCCESS ); |
| 158 | TEST_ASSERT( actual_hash_length == expected_hash_length ); |
| 159 | TEST_ASSERT( memcmp( expected_hash, actual_hash, |
| 160 | expected_hash_length ) == 0 ); |
| 161 | |
| 162 | exit: |
| 163 | mbedtls_free( input ); |
| 164 | mbedtls_psa_crypto_free( ); |
| 165 | } |
| 166 | /* END_CASE */ |
| 167 | |
| 168 | /* BEGIN_CASE */ |
| 169 | void hash_verify( int alg_arg, char *input_hex, char *hash_hex ) |
| 170 | { |
| 171 | psa_algorithm_t alg = alg_arg; |
| 172 | unsigned char *input = NULL; |
| 173 | size_t input_size; |
| 174 | unsigned char expected_hash[MBEDTLS_MD_MAX_SIZE]; |
| 175 | size_t expected_hash_length; |
| 176 | psa_hash_operation_t operation; |
| 177 | |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 178 | input = unhexify_alloc( input_hex, &input_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 179 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 180 | expected_hash_length = unhexify( expected_hash, hash_hex ); |
| 181 | |
| 182 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 183 | |
| 184 | TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS ); |
| 185 | TEST_ASSERT( psa_hash_update( &operation, |
| 186 | input, input_size ) == PSA_SUCCESS ); |
| 187 | TEST_ASSERT( psa_hash_verify( &operation, |
| 188 | expected_hash, |
| 189 | expected_hash_length ) == PSA_SUCCESS ); |
| 190 | |
| 191 | exit: |
| 192 | mbedtls_free( input ); |
| 193 | mbedtls_psa_crypto_free( ); |
| 194 | } |
| 195 | /* END_CASE */ |
| 196 | |
| 197 | /* BEGIN_CASE */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 198 | void mac_verify( int key_type_arg, char *key_hex, |
| 199 | int alg_arg, char *iv_hex, |
| 200 | char *input_hex, char *mac_hex ) |
| 201 | { |
| 202 | int key_slot = 1; |
| 203 | psa_key_type_t key_type = key_type_arg; |
| 204 | psa_algorithm_t alg = alg_arg; |
| 205 | unsigned char *key = NULL; |
| 206 | size_t key_size; |
| 207 | unsigned char *iv = NULL; |
| 208 | size_t iv_size; |
| 209 | unsigned char *input = NULL; |
| 210 | size_t input_size; |
| 211 | unsigned char *expected_mac = NULL; |
| 212 | size_t expected_mac_size; |
| 213 | psa_mac_operation_t operation; |
| 214 | |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 215 | key = unhexify_alloc( key_hex, &key_size ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 216 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 217 | if( iv_hex[0] != 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 218 | { |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 219 | iv = unhexify_alloc( iv_hex, &iv_size ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 220 | TEST_ASSERT( iv != NULL ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 221 | } |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 222 | input = unhexify_alloc( input_hex, &input_size ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 223 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 224 | expected_mac = unhexify_alloc( mac_hex, &expected_mac_size ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 225 | TEST_ASSERT( expected_mac != NULL ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 226 | |
| 227 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 228 | |
| 229 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 230 | key, key_size ) == PSA_SUCCESS ); |
| 231 | // TODO: support IV |
| 232 | TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 233 | TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS ); |
| 234 | TEST_ASSERT( psa_mac_update( &operation, |
| 235 | input, input_size ) == PSA_SUCCESS ); |
| 236 | TEST_ASSERT( psa_mac_verify( &operation, |
| 237 | expected_mac, |
| 238 | expected_mac_size ) == PSA_SUCCESS ); |
| 239 | |
| 240 | exit: |
| 241 | mbedtls_free( key ); |
| 242 | mbedtls_free( iv ); |
| 243 | mbedtls_free( input ); |
| 244 | mbedtls_free( expected_mac ); |
| 245 | psa_destroy_key( key_slot ); |
| 246 | mbedtls_psa_crypto_free( ); |
| 247 | } |
| 248 | /* END_CASE */ |
| 249 | |
| 250 | /* BEGIN_CASE */ |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 251 | void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg ) |
| 252 | { |
| 253 | psa_key_type_t type = type_arg; |
| 254 | psa_algorithm_t alg = alg_arg; |
| 255 | size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg); |
| 256 | TEST_ASSERT( actual_size == (size_t) expected_size_arg ); |
| 257 | exit: |
| 258 | ; |
| 259 | } |
| 260 | /* END_CASE */ |
| 261 | |
| 262 | /* BEGIN_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 263 | void sign_deterministic( int key_type_arg, char *key_hex, |
| 264 | int alg_arg, char *input_hex, char *output_hex ) |
| 265 | { |
| 266 | int slot = 1; |
| 267 | psa_key_type_t key_type = key_type_arg; |
| 268 | psa_algorithm_t alg = alg_arg; |
| 269 | unsigned char *key_data = NULL; |
| 270 | size_t key_size; |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 271 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 272 | unsigned char *input_data = NULL; |
| 273 | size_t input_size; |
| 274 | unsigned char *output_data = NULL; |
| 275 | size_t output_size; |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 276 | unsigned char *signature = NULL; |
| 277 | size_t signature_size; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 278 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 279 | |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 280 | key_data = unhexify_alloc( key_hex, &key_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 281 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 282 | input_data = unhexify_alloc( input_hex, &input_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 283 | TEST_ASSERT( input_data != NULL ); |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 284 | output_data = unhexify_alloc( output_hex, &output_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 285 | TEST_ASSERT( output_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 286 | |
| 287 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 288 | |
| 289 | TEST_ASSERT( psa_import_key( slot, key_type, |
| 290 | key_data, key_size ) == PSA_SUCCESS ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 291 | TEST_ASSERT( psa_get_key_information( slot, |
| 292 | NULL, |
| 293 | &key_bits ) == PSA_SUCCESS ); |
| 294 | |
| 295 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, alg, key_bits ); |
| 296 | TEST_ASSERT( signature_size != 0 ); |
| 297 | signature = mbedtls_calloc( 1, signature_size ); |
| 298 | TEST_ASSERT( signature != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 299 | |
| 300 | TEST_ASSERT( psa_asymmetric_sign( slot, alg, |
| 301 | input_data, input_size, |
| 302 | NULL, 0, |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 303 | signature, signature_size, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 304 | &signature_length ) == PSA_SUCCESS ); |
| 305 | TEST_ASSERT( signature_length == output_size ); |
| 306 | TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 ); |
| 307 | |
| 308 | exit: |
| 309 | psa_destroy_key( slot ); |
| 310 | mbedtls_free( key_data ); |
| 311 | mbedtls_free( input_data ); |
| 312 | mbedtls_free( output_data ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 313 | mbedtls_free( signature ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 314 | mbedtls_psa_crypto_free( ); |
| 315 | } |
| 316 | /* END_CASE */ |
| 317 | |
| 318 | /* BEGIN_CASE */ |
| 319 | void sign_fail( int key_type_arg, char *key_hex, |
| 320 | int alg_arg, char *input_hex, |
| 321 | int signature_size, int expected_status_arg ) |
| 322 | { |
| 323 | int slot = 1; |
| 324 | psa_key_type_t key_type = key_type_arg; |
| 325 | psa_algorithm_t alg = alg_arg; |
| 326 | unsigned char *key_data = NULL; |
| 327 | size_t key_size; |
| 328 | unsigned char *input_data = NULL; |
| 329 | size_t input_size; |
| 330 | psa_status_t actual_status; |
| 331 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 332 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 333 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 334 | |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 335 | key_data = unhexify_alloc( key_hex, &key_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 336 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 337 | input_data = unhexify_alloc( input_hex, &input_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 338 | TEST_ASSERT( input_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 339 | signature = mbedtls_calloc( 1, signature_size ); |
| 340 | TEST_ASSERT( signature != NULL ); |
| 341 | |
| 342 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 343 | |
| 344 | TEST_ASSERT( psa_import_key( slot, key_type, |
| 345 | key_data, key_size ) == PSA_SUCCESS ); |
| 346 | |
| 347 | actual_status = psa_asymmetric_sign( slot, alg, |
| 348 | input_data, input_size, |
| 349 | NULL, 0, |
| 350 | signature, signature_size, |
| 351 | &signature_length ); |
| 352 | TEST_ASSERT( actual_status == expected_status ); |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 353 | TEST_ASSERT( signature_length == 0 ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 354 | |
| 355 | exit: |
| 356 | psa_destroy_key( slot ); |
| 357 | mbedtls_free( key_data ); |
| 358 | mbedtls_free( input_data ); |
| 359 | mbedtls_free( signature ); |
| 360 | mbedtls_psa_crypto_free( ); |
| 361 | } |
| 362 | /* END_CASE */ |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame^] | 363 | |
| 364 | /* BEGIN_CASE */ |
| 365 | void key_lifetime( int lifetime_arg ) |
| 366 | { |
| 367 | int key_slot = 1; |
| 368 | psa_key_type_t key_type = PSA_ALG_CBC_BASE; |
| 369 | unsigned char key[32] = {0}; |
| 370 | psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg; |
| 371 | psa_key_lifetime_t lifetime_get; |
| 372 | |
| 373 | TEST_ASSERT( key != NULL ); |
| 374 | |
| 375 | memset( key, 0x2a, sizeof( key ) ); |
| 376 | |
| 377 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 378 | |
| 379 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 380 | key, sizeof( key ) ) == PSA_SUCCESS ); |
| 381 | |
| 382 | TEST_ASSERT( psa_set_key_lifetime( key_slot, lifetime_set )); |
| 383 | |
| 384 | TEST_ASSERT( psa_get_key_lifetime( key_slot, &lifetime_get )); |
| 385 | |
| 386 | TEST_ASSERT( lifetime_get == lifetime_set ); |
| 387 | |
| 388 | exit: |
| 389 | mbedtls_free( key ); |
| 390 | psa_destroy_key( key_slot ); |
| 391 | mbedtls_psa_crypto_free( ); |
| 392 | } |
| 393 | /* END_CASE */ |
| 394 | |
| 395 | |
| 396 | /* BEGIN_CASE */ |
| 397 | void key_lifetime_set_fail( int key_slot_arg, int lifetime_arg, int expected_status_arg ) |
| 398 | { |
| 399 | int key_slot = 1; |
| 400 | psa_key_type_t key_type = PSA_ALG_CBC_BASE; |
| 401 | unsigned char key[32] = {0}; |
| 402 | psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg; |
| 403 | psa_status_t actual_status; |
| 404 | psa_status_t expected_status = expected_status_arg; |
| 405 | |
| 406 | TEST_ASSERT( key != NULL ); |
| 407 | |
| 408 | memset( key, 0x2a, sizeof( key ) ); |
| 409 | |
| 410 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 411 | |
| 412 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 413 | key, sizeof( key ) ) == PSA_SUCCESS ); |
| 414 | |
| 415 | actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set ); |
| 416 | |
| 417 | if( actual_status == PSA_SUCCESS ) |
| 418 | actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set ); |
| 419 | |
| 420 | TEST_ASSERT( expected_status == actual_status ); |
| 421 | |
| 422 | exit: |
| 423 | mbedtls_free( key ); |
| 424 | psa_destroy_key( key_slot ); |
| 425 | mbedtls_psa_crypto_free( ); |
| 426 | } |
| 427 | /* END_CASE */ |