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 */ |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 52 | void import_export( char *hex, |
| 53 | int type_arg, |
| 54 | int alg_arg, |
| 55 | int usage_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 56 | int expected_bits, |
| 57 | int export_size_delta, |
| 58 | int expected_export_status, |
| 59 | int canonical_input ) |
| 60 | { |
| 61 | int slot = 1; |
| 62 | int slot2 = slot + 1; |
| 63 | psa_key_type_t type = type_arg; |
| 64 | psa_status_t status; |
| 65 | unsigned char *data = NULL; |
| 66 | unsigned char *exported = NULL; |
| 67 | unsigned char *reexported = NULL; |
| 68 | size_t data_size; |
| 69 | size_t export_size; |
| 70 | size_t exported_length; |
| 71 | size_t reexported_length; |
| 72 | psa_key_type_t got_type; |
| 73 | size_t got_bits; |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 74 | psa_key_policy_t policy = {0}; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 75 | |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 76 | data = unhexify_alloc( hex, &data_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 77 | TEST_ASSERT( data != NULL ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 78 | export_size = (ssize_t) data_size + export_size_delta; |
| 79 | exported = mbedtls_calloc( 1, export_size ); |
| 80 | TEST_ASSERT( exported != NULL ); |
| 81 | if( ! canonical_input ) |
| 82 | { |
| 83 | reexported = mbedtls_calloc( 1, export_size ); |
| 84 | TEST_ASSERT( reexported != NULL ); |
| 85 | } |
| 86 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 87 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 88 | psa_key_policy_init( &policy ); |
| 89 | |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 90 | psa_key_policy_set_usage( &policy, usage_arg, alg_arg ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 91 | |
| 92 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 93 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 94 | /* Import the key */ |
| 95 | TEST_ASSERT( psa_import_key( slot, type, |
| 96 | data, data_size ) == PSA_SUCCESS ); |
| 97 | |
| 98 | /* Test the key information */ |
| 99 | TEST_ASSERT( psa_get_key_information( slot, |
| 100 | &got_type, &got_bits ) == |
| 101 | PSA_SUCCESS ); |
| 102 | TEST_ASSERT( got_type == type ); |
| 103 | TEST_ASSERT( got_bits == (size_t) expected_bits ); |
| 104 | |
| 105 | /* Export the key */ |
| 106 | status = psa_export_key( slot, |
| 107 | exported, export_size, |
| 108 | &exported_length ); |
| 109 | TEST_ASSERT( status == (psa_status_t) expected_export_status ); |
| 110 | if( status != PSA_SUCCESS ) |
| 111 | goto destroy; |
| 112 | |
| 113 | if( canonical_input ) |
| 114 | { |
| 115 | TEST_ASSERT( exported_length == data_size ); |
| 116 | TEST_ASSERT( memcmp( exported, data, data_size ) == 0 ); |
| 117 | } |
| 118 | else |
| 119 | { |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 120 | TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS ); |
| 121 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 122 | TEST_ASSERT( psa_import_key( slot2, type, |
| 123 | exported, export_size ) == |
| 124 | PSA_SUCCESS ); |
| 125 | TEST_ASSERT( psa_export_key( slot2, |
| 126 | reexported, export_size, |
| 127 | &reexported_length ) == |
| 128 | PSA_SUCCESS ); |
| 129 | TEST_ASSERT( reexported_length == exported_length ); |
| 130 | TEST_ASSERT( memcmp( reexported, exported, |
| 131 | exported_length ) == 0 ); |
| 132 | } |
| 133 | |
| 134 | destroy: |
| 135 | /* Destroy the key */ |
| 136 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 137 | TEST_ASSERT( psa_get_key_information( |
| 138 | slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT ); |
| 139 | |
| 140 | exit: |
| 141 | mbedtls_free( data ); |
| 142 | mbedtls_psa_crypto_free( ); |
| 143 | } |
| 144 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 145 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame^] | 146 | |
| 147 | /* BEGIN_CASE */ |
| 148 | void import_export_public_key( char *hex, |
| 149 | int type_arg, |
| 150 | int alg_arg, |
| 151 | int expected_bits, |
| 152 | int public_key_expected_length, |
| 153 | int expected_export_status ) |
| 154 | { |
| 155 | int slot = 1; |
| 156 | psa_key_type_t type = type_arg; |
| 157 | psa_status_t status; |
| 158 | unsigned char *data = NULL; |
| 159 | unsigned char *exported = NULL; |
| 160 | size_t data_size; |
| 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 | |
| 167 | data = unhexify_alloc( hex, &data_size ); |
| 168 | TEST_ASSERT( data != NULL ); |
| 169 | export_size = (ssize_t) data_size ; |
| 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, |
| 184 | data, data_size ) == PSA_SUCCESS ); |
| 185 | |
| 186 | /* Test the key information */ |
| 187 | TEST_ASSERT( psa_get_key_information( slot, |
| 188 | &got_type, &got_bits ) == PSA_SUCCESS ); |
| 189 | TEST_ASSERT( got_type == type ); |
| 190 | TEST_ASSERT( got_bits == (size_t) expected_bits ); |
| 191 | |
| 192 | /* Export the key */ |
| 193 | status = psa_export_public_key( slot, |
| 194 | exported, export_size, |
| 195 | &exported_length ); |
| 196 | TEST_ASSERT( status == (psa_status_t) expected_export_status ); |
| 197 | if( status != PSA_SUCCESS ) |
| 198 | goto destroy; |
| 199 | |
| 200 | TEST_ASSERT( exported_length == (size_t) public_key_expected_length ); |
| 201 | |
| 202 | destroy: |
| 203 | /* Destroy the key */ |
| 204 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 205 | TEST_ASSERT( psa_get_key_information( |
| 206 | slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT ); |
| 207 | |
| 208 | exit: |
| 209 | mbedtls_free( data ); |
| 210 | mbedtls_psa_crypto_free( ); |
| 211 | } |
| 212 | /* END_CASE */ |
| 213 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 214 | /* BEGIN_CASE */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 215 | void hash_finish( int alg_arg, char *input_hex, char *hash_hex ) |
| 216 | { |
| 217 | psa_algorithm_t alg = alg_arg; |
| 218 | unsigned char *input = NULL; |
| 219 | size_t input_size; |
| 220 | unsigned char expected_hash[MBEDTLS_MD_MAX_SIZE]; |
| 221 | size_t expected_hash_length; |
| 222 | unsigned char actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 223 | size_t actual_hash_length; |
| 224 | psa_hash_operation_t operation; |
| 225 | |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 226 | input = unhexify_alloc( input_hex, &input_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 227 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 228 | expected_hash_length = unhexify( expected_hash, hash_hex ); |
| 229 | |
| 230 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 231 | |
| 232 | TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS ); |
| 233 | TEST_ASSERT( psa_hash_update( &operation, |
| 234 | input, input_size ) == PSA_SUCCESS ); |
| 235 | TEST_ASSERT( psa_hash_finish( &operation, |
| 236 | actual_hash, sizeof( actual_hash ), |
| 237 | &actual_hash_length ) == PSA_SUCCESS ); |
| 238 | TEST_ASSERT( actual_hash_length == expected_hash_length ); |
| 239 | TEST_ASSERT( memcmp( expected_hash, actual_hash, |
| 240 | expected_hash_length ) == 0 ); |
| 241 | |
| 242 | exit: |
| 243 | mbedtls_free( input ); |
| 244 | mbedtls_psa_crypto_free( ); |
| 245 | } |
| 246 | /* END_CASE */ |
| 247 | |
| 248 | /* BEGIN_CASE */ |
| 249 | void hash_verify( int alg_arg, char *input_hex, char *hash_hex ) |
| 250 | { |
| 251 | psa_algorithm_t alg = alg_arg; |
| 252 | unsigned char *input = NULL; |
| 253 | size_t input_size; |
| 254 | unsigned char expected_hash[MBEDTLS_MD_MAX_SIZE]; |
| 255 | size_t expected_hash_length; |
| 256 | psa_hash_operation_t operation; |
| 257 | |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 258 | input = unhexify_alloc( input_hex, &input_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 259 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 260 | expected_hash_length = unhexify( expected_hash, hash_hex ); |
| 261 | |
| 262 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 263 | |
| 264 | TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS ); |
| 265 | TEST_ASSERT( psa_hash_update( &operation, |
| 266 | input, input_size ) == PSA_SUCCESS ); |
| 267 | TEST_ASSERT( psa_hash_verify( &operation, |
| 268 | expected_hash, |
| 269 | expected_hash_length ) == PSA_SUCCESS ); |
| 270 | |
| 271 | exit: |
| 272 | mbedtls_free( input ); |
| 273 | mbedtls_psa_crypto_free( ); |
| 274 | } |
| 275 | /* END_CASE */ |
| 276 | |
| 277 | /* BEGIN_CASE */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 278 | void mac_verify( int key_type_arg, char *key_hex, |
| 279 | int alg_arg, char *iv_hex, |
| 280 | char *input_hex, char *mac_hex ) |
| 281 | { |
| 282 | int key_slot = 1; |
| 283 | psa_key_type_t key_type = key_type_arg; |
| 284 | psa_algorithm_t alg = alg_arg; |
| 285 | unsigned char *key = NULL; |
| 286 | size_t key_size; |
| 287 | unsigned char *iv = NULL; |
| 288 | size_t iv_size; |
| 289 | unsigned char *input = NULL; |
| 290 | size_t input_size; |
| 291 | unsigned char *expected_mac = NULL; |
| 292 | size_t expected_mac_size; |
| 293 | psa_mac_operation_t operation; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 294 | psa_key_policy_t policy; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 295 | |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 296 | key = unhexify_alloc( key_hex, &key_size ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 297 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 298 | if( iv_hex[0] != 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 299 | { |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 300 | iv = unhexify_alloc( iv_hex, &iv_size ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 301 | TEST_ASSERT( iv != NULL ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 302 | } |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 303 | input = unhexify_alloc( input_hex, &input_size ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 304 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 305 | expected_mac = unhexify_alloc( mac_hex, &expected_mac_size ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 306 | TEST_ASSERT( expected_mac != NULL ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 307 | |
| 308 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 309 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 310 | psa_key_policy_init( &policy ); |
| 311 | |
| 312 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg_arg ); |
| 313 | |
| 314 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 315 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 316 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 317 | key, key_size ) == PSA_SUCCESS ); |
| 318 | // TODO: support IV |
| 319 | TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 320 | TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS ); |
| 321 | TEST_ASSERT( psa_mac_update( &operation, |
| 322 | input, input_size ) == PSA_SUCCESS ); |
| 323 | TEST_ASSERT( psa_mac_verify( &operation, |
| 324 | expected_mac, |
| 325 | expected_mac_size ) == PSA_SUCCESS ); |
| 326 | |
| 327 | exit: |
| 328 | mbedtls_free( key ); |
| 329 | mbedtls_free( iv ); |
| 330 | mbedtls_free( input ); |
| 331 | mbedtls_free( expected_mac ); |
| 332 | psa_destroy_key( key_slot ); |
| 333 | mbedtls_psa_crypto_free( ); |
| 334 | } |
| 335 | /* END_CASE */ |
| 336 | |
| 337 | /* BEGIN_CASE */ |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 338 | void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg ) |
| 339 | { |
| 340 | psa_key_type_t type = type_arg; |
| 341 | psa_algorithm_t alg = alg_arg; |
| 342 | size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg); |
| 343 | TEST_ASSERT( actual_size == (size_t) expected_size_arg ); |
| 344 | exit: |
| 345 | ; |
| 346 | } |
| 347 | /* END_CASE */ |
| 348 | |
| 349 | /* BEGIN_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 350 | void sign_deterministic( int key_type_arg, char *key_hex, |
| 351 | int alg_arg, char *input_hex, char *output_hex ) |
| 352 | { |
| 353 | int slot = 1; |
| 354 | psa_key_type_t key_type = key_type_arg; |
| 355 | psa_algorithm_t alg = alg_arg; |
| 356 | unsigned char *key_data = NULL; |
| 357 | size_t key_size; |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 358 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 359 | unsigned char *input_data = NULL; |
| 360 | size_t input_size; |
| 361 | unsigned char *output_data = NULL; |
| 362 | size_t output_size; |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 363 | unsigned char *signature = NULL; |
| 364 | size_t signature_size; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 365 | size_t signature_length = 0xdeadbeef; |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 366 | psa_key_policy_t policy = {0}; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 367 | |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 368 | key_data = unhexify_alloc( key_hex, &key_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 369 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 370 | input_data = unhexify_alloc( input_hex, &input_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 371 | TEST_ASSERT( input_data != NULL ); |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 372 | output_data = unhexify_alloc( output_hex, &output_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 373 | TEST_ASSERT( output_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 374 | |
| 375 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 376 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 377 | psa_key_policy_init( &policy ); |
| 378 | |
| 379 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg ); |
| 380 | |
| 381 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 382 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 383 | TEST_ASSERT( psa_import_key( slot, key_type, |
| 384 | key_data, key_size ) == PSA_SUCCESS ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 385 | TEST_ASSERT( psa_get_key_information( slot, |
| 386 | NULL, |
| 387 | &key_bits ) == PSA_SUCCESS ); |
| 388 | |
| 389 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, alg, key_bits ); |
| 390 | TEST_ASSERT( signature_size != 0 ); |
| 391 | signature = mbedtls_calloc( 1, signature_size ); |
| 392 | TEST_ASSERT( signature != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 393 | |
| 394 | TEST_ASSERT( psa_asymmetric_sign( slot, alg, |
| 395 | input_data, input_size, |
| 396 | NULL, 0, |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 397 | signature, signature_size, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 398 | &signature_length ) == PSA_SUCCESS ); |
| 399 | TEST_ASSERT( signature_length == output_size ); |
| 400 | TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 ); |
| 401 | |
| 402 | exit: |
| 403 | psa_destroy_key( slot ); |
| 404 | mbedtls_free( key_data ); |
| 405 | mbedtls_free( input_data ); |
| 406 | mbedtls_free( output_data ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 407 | mbedtls_free( signature ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 408 | mbedtls_psa_crypto_free( ); |
| 409 | } |
| 410 | /* END_CASE */ |
| 411 | |
| 412 | /* BEGIN_CASE */ |
| 413 | void sign_fail( int key_type_arg, char *key_hex, |
| 414 | int alg_arg, char *input_hex, |
| 415 | int signature_size, int expected_status_arg ) |
| 416 | { |
| 417 | int slot = 1; |
| 418 | psa_key_type_t key_type = key_type_arg; |
| 419 | psa_algorithm_t alg = alg_arg; |
| 420 | unsigned char *key_data = NULL; |
| 421 | size_t key_size; |
| 422 | unsigned char *input_data = NULL; |
| 423 | size_t input_size; |
| 424 | psa_status_t actual_status; |
| 425 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 426 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 427 | size_t signature_length = 0xdeadbeef; |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 428 | psa_key_policy_t policy = {0}; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 429 | |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 430 | key_data = unhexify_alloc( key_hex, &key_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 431 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 432 | input_data = unhexify_alloc( input_hex, &input_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 433 | TEST_ASSERT( input_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 434 | signature = mbedtls_calloc( 1, signature_size ); |
| 435 | TEST_ASSERT( signature != NULL ); |
| 436 | |
| 437 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 438 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 439 | psa_key_policy_init( &policy ); |
| 440 | |
| 441 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg ); |
| 442 | |
| 443 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 444 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 445 | TEST_ASSERT( psa_import_key( slot, key_type, |
| 446 | key_data, key_size ) == PSA_SUCCESS ); |
| 447 | |
| 448 | actual_status = psa_asymmetric_sign( slot, alg, |
| 449 | input_data, input_size, |
| 450 | NULL, 0, |
| 451 | signature, signature_size, |
| 452 | &signature_length ); |
| 453 | TEST_ASSERT( actual_status == expected_status ); |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 454 | TEST_ASSERT( signature_length == 0 ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 455 | |
| 456 | exit: |
| 457 | psa_destroy_key( slot ); |
| 458 | mbedtls_free( key_data ); |
| 459 | mbedtls_free( input_data ); |
| 460 | mbedtls_free( signature ); |
| 461 | mbedtls_psa_crypto_free( ); |
| 462 | } |
| 463 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 464 | |
| 465 | /* BEGIN_CASE */ |
| 466 | void key_policy( int usage_arg, int alg_arg ) |
| 467 | { |
| 468 | int key_slot = 1; |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 469 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 470 | unsigned char key[32] = {0}; |
| 471 | psa_key_policy_t policy_set = {0}; |
| 472 | psa_key_policy_t policy_get = {0}; |
| 473 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 474 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 475 | memset( key, 0x2a, sizeof( key ) ); |
| 476 | |
| 477 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 478 | |
| 479 | psa_key_policy_init(& policy_set ); |
| 480 | psa_key_policy_init(& policy_get ); |
| 481 | |
| 482 | psa_key_policy_set_usage( &policy_set, usage_arg, alg_arg ); |
| 483 | |
| 484 | TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == ( psa_key_usage_t )usage_arg ); |
| 485 | |
| 486 | TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set) == ( psa_algorithm_t )alg_arg ); |
| 487 | |
| 488 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS ); |
| 489 | |
| 490 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 491 | key, sizeof( key ) ) == PSA_SUCCESS ); |
| 492 | |
| 493 | TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS ); |
| 494 | |
| 495 | TEST_ASSERT( policy_get.usage == policy_set.usage ); |
| 496 | TEST_ASSERT( policy_get.alg == policy_set.alg ); |
| 497 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 498 | |
| 499 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 500 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 501 | exit: |
| 502 | psa_destroy_key( key_slot ); |
| 503 | mbedtls_psa_crypto_free( ); |
| 504 | } |
| 505 | /* END_CASE */ |
| 506 | |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 507 | /* BEGIN_CASE */ |
| 508 | void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex ) |
| 509 | { |
| 510 | int key_slot = 1; |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 511 | unsigned char* keypair = NULL; |
| 512 | size_t key_size = 0; |
| 513 | size_t signature_length = 0; |
| 514 | psa_key_policy_t policy = {0}; |
| 515 | int actual_status = PSA_SUCCESS; |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 516 | |
| 517 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 518 | |
| 519 | psa_key_policy_init( &policy ); |
| 520 | |
| 521 | psa_key_policy_set_usage( &policy, usage_arg, alg_arg ); |
| 522 | |
| 523 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 524 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 525 | if( usage_arg & PSA_KEY_USAGE_EXPORT ) |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 526 | { |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 527 | keypair = unhexify_alloc( key_hex, &key_size ); |
| 528 | TEST_ASSERT( keypair != NULL ); |
| 529 | TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR, |
| 530 | keypair, key_size ) == PSA_SUCCESS ); |
| 531 | actual_status = psa_asymmetric_sign( key_slot, |
| 532 | ( psa_algorithm_t )alg_arg, NULL, 0, NULL, 0, |
| 533 | NULL, 0, &signature_length ); |
| 534 | } |
| 535 | |
| 536 | if( usage_arg & PSA_KEY_USAGE_SIGN ) |
| 537 | { |
mohammad1603 | d926b88 | 2018-04-16 01:53:20 -0700 | [diff] [blame] | 538 | keypair = unhexify_alloc( key_hex, &key_size ); |
| 539 | TEST_ASSERT( keypair != NULL ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 540 | TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR, |
mohammad1603 | d926b88 | 2018-04-16 01:53:20 -0700 | [diff] [blame] | 541 | keypair, key_size ) == PSA_SUCCESS ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 542 | actual_status = psa_export_key( key_slot, NULL, 0, NULL ); |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | TEST_ASSERT( actual_status == expected_status ); |
| 546 | |
| 547 | exit: |
| 548 | psa_destroy_key( key_slot ); |
mohammad1603 | d926b88 | 2018-04-16 01:53:20 -0700 | [diff] [blame] | 549 | mbedtls_free( keypair ); |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 550 | mbedtls_psa_crypto_free( ); |
| 551 | } |
| 552 | /* END_CASE */ |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 553 | |
| 554 | /* BEGIN_CASE */ |
| 555 | void key_lifetime( int lifetime_arg ) |
| 556 | { |
| 557 | int key_slot = 1; |
| 558 | psa_key_type_t key_type = PSA_ALG_CBC_BASE; |
| 559 | unsigned char key[32] = {0}; |
| 560 | psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg; |
| 561 | psa_key_lifetime_t lifetime_get; |
| 562 | memset( key, 0x2a, sizeof( key ) ); |
| 563 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 564 | TEST_ASSERT( psa_set_key_lifetime( key_slot, |
| 565 | lifetime_set ) == PSA_SUCCESS ); |
| 566 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 567 | key, sizeof( key ) ) == PSA_SUCCESS ); |
| 568 | TEST_ASSERT( psa_get_key_lifetime( key_slot, |
| 569 | &lifetime_get ) == PSA_SUCCESS ); |
| 570 | TEST_ASSERT( lifetime_get == lifetime_set ); |
| 571 | exit: |
| 572 | psa_destroy_key( key_slot ); |
| 573 | mbedtls_psa_crypto_free( ); |
| 574 | } |
| 575 | /* END_CASE */ |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 576 | |
| 577 | /* BEGIN_CASE */ |
| 578 | void key_lifetime_set_fail( int key_slot_arg, int lifetime_arg, int expected_status_arg ) |
| 579 | { |
| 580 | int key_slot = 1; |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 581 | psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg; |
| 582 | psa_status_t actual_status; |
| 583 | psa_status_t expected_status = expected_status_arg; |
| 584 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 585 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 586 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 587 | actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set ); |
| 588 | |
| 589 | if( actual_status == PSA_SUCCESS ) |
| 590 | actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set ); |
| 591 | |
| 592 | TEST_ASSERT( expected_status == actual_status ); |
| 593 | |
| 594 | exit: |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 595 | psa_destroy_key( key_slot ); |
| 596 | mbedtls_psa_crypto_free( ); |
| 597 | } |
| 598 | /* END_CASE */ |