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; |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 71 | psa_key_policy_t policy = {0}; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 72 | |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 73 | data = unhexify_alloc( hex, &data_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 74 | TEST_ASSERT( data != NULL ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 75 | export_size = (ssize_t) data_size + export_size_delta; |
| 76 | exported = mbedtls_calloc( 1, export_size ); |
| 77 | TEST_ASSERT( exported != NULL ); |
| 78 | if( ! canonical_input ) |
| 79 | { |
| 80 | reexported = mbedtls_calloc( 1, export_size ); |
| 81 | TEST_ASSERT( reexported != NULL ); |
| 82 | } |
| 83 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 84 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 85 | psa_key_policy_init( &policy ); |
| 86 | |
| 87 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, |
| 88 | PSA_ALG_VENDOR_FLAG ); |
| 89 | |
| 90 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 91 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 92 | /* Import the key */ |
| 93 | TEST_ASSERT( psa_import_key( slot, type, |
| 94 | data, data_size ) == PSA_SUCCESS ); |
| 95 | |
| 96 | /* Test the key information */ |
| 97 | TEST_ASSERT( psa_get_key_information( slot, |
| 98 | &got_type, &got_bits ) == |
| 99 | PSA_SUCCESS ); |
| 100 | TEST_ASSERT( got_type == type ); |
| 101 | TEST_ASSERT( got_bits == (size_t) expected_bits ); |
| 102 | |
| 103 | /* Export the key */ |
| 104 | status = psa_export_key( slot, |
| 105 | exported, export_size, |
| 106 | &exported_length ); |
| 107 | TEST_ASSERT( status == (psa_status_t) expected_export_status ); |
| 108 | if( status != PSA_SUCCESS ) |
| 109 | goto destroy; |
| 110 | |
| 111 | if( canonical_input ) |
| 112 | { |
| 113 | TEST_ASSERT( exported_length == data_size ); |
| 114 | TEST_ASSERT( memcmp( exported, data, data_size ) == 0 ); |
| 115 | } |
| 116 | else |
| 117 | { |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 118 | TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS ); |
| 119 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 120 | TEST_ASSERT( psa_import_key( slot2, type, |
| 121 | exported, export_size ) == |
| 122 | PSA_SUCCESS ); |
| 123 | TEST_ASSERT( psa_export_key( slot2, |
| 124 | reexported, export_size, |
| 125 | &reexported_length ) == |
| 126 | PSA_SUCCESS ); |
| 127 | TEST_ASSERT( reexported_length == exported_length ); |
| 128 | TEST_ASSERT( memcmp( reexported, exported, |
| 129 | exported_length ) == 0 ); |
| 130 | } |
| 131 | |
| 132 | destroy: |
| 133 | /* Destroy the key */ |
| 134 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 135 | TEST_ASSERT( psa_get_key_information( |
| 136 | slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT ); |
| 137 | |
| 138 | exit: |
| 139 | mbedtls_free( data ); |
| 140 | mbedtls_psa_crypto_free( ); |
| 141 | } |
| 142 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 143 | |
| 144 | /* BEGIN_CASE */ |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 145 | void hash_finish( int alg_arg, char *input_hex, char *hash_hex ) |
| 146 | { |
| 147 | psa_algorithm_t alg = alg_arg; |
| 148 | unsigned char *input = NULL; |
| 149 | size_t input_size; |
| 150 | unsigned char expected_hash[MBEDTLS_MD_MAX_SIZE]; |
| 151 | size_t expected_hash_length; |
| 152 | unsigned char actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 153 | size_t actual_hash_length; |
| 154 | psa_hash_operation_t operation; |
| 155 | |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 156 | input = unhexify_alloc( input_hex, &input_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 157 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 158 | expected_hash_length = unhexify( expected_hash, hash_hex ); |
| 159 | |
| 160 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 161 | |
| 162 | TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS ); |
| 163 | TEST_ASSERT( psa_hash_update( &operation, |
| 164 | input, input_size ) == PSA_SUCCESS ); |
| 165 | TEST_ASSERT( psa_hash_finish( &operation, |
| 166 | actual_hash, sizeof( actual_hash ), |
| 167 | &actual_hash_length ) == PSA_SUCCESS ); |
| 168 | TEST_ASSERT( actual_hash_length == expected_hash_length ); |
| 169 | TEST_ASSERT( memcmp( expected_hash, actual_hash, |
| 170 | expected_hash_length ) == 0 ); |
| 171 | |
| 172 | exit: |
| 173 | mbedtls_free( input ); |
| 174 | mbedtls_psa_crypto_free( ); |
| 175 | } |
| 176 | /* END_CASE */ |
| 177 | |
| 178 | /* BEGIN_CASE */ |
| 179 | void hash_verify( int alg_arg, char *input_hex, char *hash_hex ) |
| 180 | { |
| 181 | psa_algorithm_t alg = alg_arg; |
| 182 | unsigned char *input = NULL; |
| 183 | size_t input_size; |
| 184 | unsigned char expected_hash[MBEDTLS_MD_MAX_SIZE]; |
| 185 | size_t expected_hash_length; |
| 186 | psa_hash_operation_t operation; |
| 187 | |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 188 | input = unhexify_alloc( input_hex, &input_size ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 189 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 190 | expected_hash_length = unhexify( expected_hash, hash_hex ); |
| 191 | |
| 192 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 193 | |
| 194 | TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS ); |
| 195 | TEST_ASSERT( psa_hash_update( &operation, |
| 196 | input, input_size ) == PSA_SUCCESS ); |
| 197 | TEST_ASSERT( psa_hash_verify( &operation, |
| 198 | expected_hash, |
| 199 | expected_hash_length ) == PSA_SUCCESS ); |
| 200 | |
| 201 | exit: |
| 202 | mbedtls_free( input ); |
| 203 | mbedtls_psa_crypto_free( ); |
| 204 | } |
| 205 | /* END_CASE */ |
| 206 | |
| 207 | /* BEGIN_CASE */ |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 208 | void mac_verify( int key_type_arg, char *key_hex, |
| 209 | int alg_arg, char *iv_hex, |
| 210 | char *input_hex, char *mac_hex ) |
| 211 | { |
| 212 | int key_slot = 1; |
| 213 | psa_key_type_t key_type = key_type_arg; |
| 214 | psa_algorithm_t alg = alg_arg; |
| 215 | unsigned char *key = NULL; |
| 216 | size_t key_size; |
| 217 | unsigned char *iv = NULL; |
| 218 | size_t iv_size; |
| 219 | unsigned char *input = NULL; |
| 220 | size_t input_size; |
| 221 | unsigned char *expected_mac = NULL; |
| 222 | size_t expected_mac_size; |
| 223 | psa_mac_operation_t operation; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 224 | psa_key_policy_t policy; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 225 | |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 226 | key = unhexify_alloc( key_hex, &key_size ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 227 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 228 | if( iv_hex[0] != 0 ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 229 | { |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 230 | iv = unhexify_alloc( iv_hex, &iv_size ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 231 | TEST_ASSERT( iv != NULL ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 232 | } |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 233 | input = unhexify_alloc( input_hex, &input_size ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 234 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 235 | expected_mac = unhexify_alloc( mac_hex, &expected_mac_size ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 236 | TEST_ASSERT( expected_mac != NULL ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 237 | |
| 238 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 239 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 240 | psa_key_policy_init( &policy ); |
| 241 | |
| 242 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg_arg ); |
| 243 | |
| 244 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 245 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 246 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 247 | key, key_size ) == PSA_SUCCESS ); |
| 248 | // TODO: support IV |
| 249 | TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 250 | TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS ); |
| 251 | TEST_ASSERT( psa_mac_update( &operation, |
| 252 | input, input_size ) == PSA_SUCCESS ); |
| 253 | TEST_ASSERT( psa_mac_verify( &operation, |
| 254 | expected_mac, |
| 255 | expected_mac_size ) == PSA_SUCCESS ); |
| 256 | |
| 257 | exit: |
| 258 | mbedtls_free( key ); |
| 259 | mbedtls_free( iv ); |
| 260 | mbedtls_free( input ); |
| 261 | mbedtls_free( expected_mac ); |
| 262 | psa_destroy_key( key_slot ); |
| 263 | mbedtls_psa_crypto_free( ); |
| 264 | } |
| 265 | /* END_CASE */ |
| 266 | |
Moran Peker | 7f87850 | 2018-06-06 17:09:40 +0300 | [diff] [blame] | 267 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 268 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame^] | 269 | void cipher_encrypt( int alg_arg, int key_type_arg, |
| 270 | char *key_hex, |
| 271 | char *input_hex, char *output_hex, |
| 272 | int expected_status ) |
| 273 | { |
| 274 | int key_slot = 1; |
| 275 | psa_status_t status; |
| 276 | psa_key_type_t key_type = key_type_arg; |
| 277 | psa_algorithm_t alg = alg_arg; |
| 278 | unsigned char *key = NULL; |
| 279 | size_t key_size; |
| 280 | unsigned char iv[16] = {0}; |
| 281 | unsigned char *input = NULL; |
| 282 | size_t input_size = 0; |
| 283 | unsigned char *output; |
| 284 | unsigned char *expected_output; |
| 285 | size_t expected_output_size; |
| 286 | size_t output_buffer_size = 0; |
| 287 | size_t function_output_length = 0; |
| 288 | psa_cipher_operation_t operation; |
| 289 | |
| 290 | |
| 291 | key = unhexify_alloc( key_hex, &key_size ); |
| 292 | TEST_ASSERT( key != NULL ); |
| 293 | |
| 294 | input = unhexify_alloc( input_hex, &input_size ); |
| 295 | TEST_ASSERT( input != NULL ); |
| 296 | |
| 297 | expected_output = unhexify_alloc( output_hex, &expected_output_size ); |
| 298 | TEST_ASSERT( expected_output != NULL ); |
| 299 | |
| 300 | memset( iv, 0x2a, sizeof( iv ) ); |
| 301 | |
| 302 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 303 | |
| 304 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 305 | key, key_size ) == PSA_SUCCESS ); |
| 306 | |
| 307 | TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 308 | |
| 309 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 310 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
| 311 | output_buffer_size = input_size + operation.block_size; |
| 312 | output = mbedtls_calloc( 1, output_buffer_size ); |
| 313 | |
| 314 | TEST_ASSERT( psa_cipher_update( &operation, input, input_size, |
| 315 | output, output_buffer_size, |
| 316 | &function_output_length ) == PSA_SUCCESS ); |
| 317 | status = psa_cipher_finish( &operation, |
| 318 | output + function_output_length, |
| 319 | output_buffer_size, |
| 320 | &function_output_length ); |
| 321 | TEST_ASSERT( status == (psa_status_t) expected_status ); |
| 322 | if( expected_status == PSA_SUCCESS ) |
| 323 | { |
| 324 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 325 | TEST_ASSERT( memcmp( expected_output, output, |
| 326 | expected_output_size ) == 0 ); |
| 327 | } |
| 328 | exit: |
| 329 | mbedtls_free( key ); |
| 330 | mbedtls_free( input ); |
| 331 | psa_destroy_key( key_slot ); |
| 332 | mbedtls_psa_crypto_free( ); |
| 333 | } |
| 334 | /* END_CASE */ |
| 335 | |
| 336 | /* BEGIN_CASE */ |
| 337 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
| 338 | char *key_hex, |
| 339 | char *input_hex, |
| 340 | int first_part_size, char *output_hex ) |
| 341 | { |
| 342 | int key_slot = 1; |
| 343 | psa_key_type_t key_type = key_type_arg; |
| 344 | psa_algorithm_t alg = alg_arg; |
| 345 | unsigned char *key = NULL; |
| 346 | size_t key_size; |
| 347 | unsigned char iv[16] = {0}; |
| 348 | unsigned char *input = NULL; |
| 349 | size_t input_size = 0; |
| 350 | unsigned char *output; |
| 351 | unsigned char *expected_output; |
| 352 | size_t expected_output_size; |
| 353 | size_t output_buffer_size = 0; |
| 354 | size_t function_output_length = 0; |
| 355 | psa_cipher_operation_t operation; |
| 356 | |
| 357 | |
| 358 | key = unhexify_alloc( key_hex, &key_size ); |
| 359 | TEST_ASSERT( key != NULL ); |
| 360 | |
| 361 | input = unhexify_alloc( input_hex, &input_size ); |
| 362 | TEST_ASSERT( input != NULL ); |
| 363 | |
| 364 | expected_output = unhexify_alloc( output_hex, &expected_output_size ); |
| 365 | TEST_ASSERT( expected_output != NULL ); |
| 366 | |
| 367 | memset( iv, 0x2a, sizeof( iv ) ); |
| 368 | |
| 369 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 370 | |
| 371 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 372 | key, key_size ) == PSA_SUCCESS ); |
| 373 | |
| 374 | TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 375 | |
| 376 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 377 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
| 378 | output_buffer_size = input_size + operation.block_size; |
| 379 | output = mbedtls_calloc( 1, output_buffer_size ); |
| 380 | |
| 381 | TEST_ASSERT( (unsigned int) first_part_size < input_size ); |
| 382 | TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, |
| 383 | output, output_buffer_size, |
| 384 | &function_output_length ) == PSA_SUCCESS ); |
| 385 | TEST_ASSERT( psa_cipher_update( &operation, |
| 386 | input + first_part_size, |
| 387 | input_size - first_part_size, |
| 388 | output, output_buffer_size, |
| 389 | &function_output_length ) == PSA_SUCCESS ); |
| 390 | TEST_ASSERT( psa_cipher_finish( &operation, |
| 391 | output + function_output_length, |
| 392 | output_buffer_size, |
| 393 | &function_output_length ) == PSA_SUCCESS ); |
| 394 | |
| 395 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 396 | |
| 397 | TEST_ASSERT( input_size == expected_output_size ); |
| 398 | TEST_ASSERT( memcmp( expected_output, output, expected_output_size ) == 0 ); |
| 399 | |
| 400 | exit: |
| 401 | mbedtls_free( key ); |
| 402 | mbedtls_free( input ); |
| 403 | psa_destroy_key( key_slot ); |
| 404 | mbedtls_psa_crypto_free( ); |
| 405 | } |
| 406 | /* END_CASE */ |
| 407 | |
| 408 | /* BEGIN_CASE */ |
| 409 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
| 410 | char *key_hex, |
| 411 | char *input_hex, |
| 412 | int first_part_size, char *output_hex ) |
| 413 | { |
| 414 | int key_slot = 1; |
| 415 | |
| 416 | psa_key_type_t key_type = key_type_arg; |
| 417 | psa_algorithm_t alg = alg_arg; |
| 418 | unsigned char *key = NULL; |
| 419 | size_t key_size; |
| 420 | unsigned char iv[16] = {0}; |
| 421 | unsigned char *input = NULL; |
| 422 | size_t input_size = 0; |
| 423 | unsigned char *output; |
| 424 | unsigned char *expected_output; |
| 425 | size_t expected_output_size; |
| 426 | size_t output_buffer_size = 0; |
| 427 | size_t function_output_length = 0; |
| 428 | psa_cipher_operation_t operation; |
| 429 | |
| 430 | |
| 431 | key = unhexify_alloc( key_hex, &key_size ); |
| 432 | TEST_ASSERT( key != NULL ); |
| 433 | |
| 434 | input = unhexify_alloc( input_hex, &input_size ); |
| 435 | TEST_ASSERT( input != NULL ); |
| 436 | |
| 437 | expected_output = unhexify_alloc( output_hex, &expected_output_size ); |
| 438 | TEST_ASSERT( expected_output != NULL ); |
| 439 | |
| 440 | memset( iv, 0x2a, sizeof( iv ) ); |
| 441 | |
| 442 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 443 | |
| 444 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 445 | key, key_size ) == PSA_SUCCESS ); |
| 446 | |
| 447 | TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 448 | |
| 449 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 450 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
| 451 | |
| 452 | output_buffer_size = input_size + operation.block_size; |
| 453 | output = mbedtls_calloc( 1, output_buffer_size ); |
| 454 | |
| 455 | TEST_ASSERT( (unsigned int) first_part_size < input_size ); |
| 456 | TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, |
| 457 | output, output_buffer_size, |
| 458 | &function_output_length ) == PSA_SUCCESS ); |
| 459 | TEST_ASSERT( psa_cipher_update( &operation, |
| 460 | input + first_part_size, |
| 461 | input_size - first_part_size, |
| 462 | output, output_buffer_size, |
| 463 | &function_output_length ) == PSA_SUCCESS ); |
| 464 | TEST_ASSERT( psa_cipher_finish( &operation, |
| 465 | output + function_output_length, |
| 466 | output_buffer_size, |
| 467 | &function_output_length ) == PSA_SUCCESS ); |
| 468 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 469 | |
| 470 | TEST_ASSERT( input_size == expected_output_size ); |
| 471 | TEST_ASSERT( memcmp( expected_output, output, expected_output_size ) == 0 ); |
| 472 | |
| 473 | exit: |
| 474 | mbedtls_free( key ); |
| 475 | mbedtls_free( input ); |
| 476 | psa_destroy_key( key_slot ); |
| 477 | mbedtls_psa_crypto_free( ); |
| 478 | } |
| 479 | /* END_CASE */ |
| 480 | |
| 481 | |
| 482 | /* BEGIN_CASE */ |
| 483 | void cipher_decrypt( int alg_arg, int key_type_arg, |
| 484 | char *key_hex, |
| 485 | char *input_hex, char *output_hex, |
| 486 | int expected_status ) |
| 487 | { |
| 488 | int key_slot = 1; |
| 489 | psa_status_t status; |
| 490 | psa_key_type_t key_type = key_type_arg; |
| 491 | psa_algorithm_t alg = alg_arg; |
| 492 | unsigned char *key = NULL; |
| 493 | size_t key_size; |
| 494 | unsigned char iv[16] = {0}; |
| 495 | unsigned char *input = NULL; |
| 496 | size_t input_size = 0; |
| 497 | unsigned char *output; |
| 498 | unsigned char *expected_output; |
| 499 | size_t expected_output_size; |
| 500 | size_t output_buffer_size = 0; |
| 501 | size_t function_output_length = 0; |
| 502 | psa_cipher_operation_t operation; |
| 503 | |
| 504 | |
| 505 | key = unhexify_alloc( key_hex, &key_size ); |
| 506 | TEST_ASSERT( key != NULL ); |
| 507 | |
| 508 | input = unhexify_alloc( input_hex, &input_size ); |
| 509 | TEST_ASSERT( input != NULL ); |
| 510 | |
| 511 | expected_output = unhexify_alloc( output_hex, &expected_output_size ); |
| 512 | TEST_ASSERT( expected_output != NULL ); |
| 513 | |
| 514 | memset( iv, 0x2a, sizeof( iv ) ); |
| 515 | |
| 516 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 517 | |
| 518 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 519 | key, key_size ) == PSA_SUCCESS ); |
| 520 | |
| 521 | TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 522 | |
| 523 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 524 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
| 525 | |
| 526 | output_buffer_size = input_size + operation.block_size; |
| 527 | output = mbedtls_calloc( 1, output_buffer_size ); |
| 528 | |
| 529 | TEST_ASSERT( psa_cipher_update( &operation, input, input_size, |
| 530 | output, output_buffer_size, |
| 531 | &function_output_length ) == PSA_SUCCESS ); |
| 532 | status = psa_cipher_finish( &operation, |
| 533 | output + function_output_length, |
| 534 | output_buffer_size, |
| 535 | &function_output_length ); |
| 536 | TEST_ASSERT( status == (psa_status_t) expected_status ); |
| 537 | |
| 538 | if( expected_status == PSA_SUCCESS ) |
| 539 | { |
| 540 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 541 | TEST_ASSERT( memcmp( expected_output, output, |
| 542 | expected_output_size ) == 0 ); |
| 543 | } |
| 544 | |
| 545 | |
| 546 | exit: |
| 547 | mbedtls_free( key ); |
| 548 | mbedtls_free( input ); |
| 549 | psa_destroy_key( key_slot ); |
| 550 | mbedtls_psa_crypto_free( ); |
| 551 | } |
| 552 | /* END_CASE */ |
| 553 | |
| 554 | |
| 555 | /* BEGIN_CASE */ |
| 556 | void cipher_verify_output( int alg_arg, int key_type_arg, |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 557 | char *key_hex, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame^] | 558 | char *input_hex ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 559 | { |
| 560 | int key_slot = 1; |
| 561 | psa_key_type_t key_type = key_type_arg; |
| 562 | psa_algorithm_t alg = alg_arg; |
| 563 | unsigned char *key = NULL; |
| 564 | size_t key_size; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 565 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 566 | size_t iv_size = 16; |
| 567 | size_t iv_length = 0; |
| 568 | unsigned char *input = NULL; |
| 569 | size_t input_size = 0; |
Moran Peker | 3205a65 | 2018-03-20 17:09:59 +0200 | [diff] [blame] | 570 | unsigned char *output1; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 571 | size_t output1_size = 0; |
| 572 | size_t output1_length = 0; |
Moran Peker | 3205a65 | 2018-03-20 17:09:59 +0200 | [diff] [blame] | 573 | unsigned char *output2; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 574 | size_t output2_size = 0; |
| 575 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 576 | size_t function_output_length = 0; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 577 | psa_cipher_operation_t operation1; |
| 578 | psa_cipher_operation_t operation2; |
| 579 | |
| 580 | key = unhexify_alloc( key_hex, &key_size ); |
| 581 | TEST_ASSERT( key != NULL ); |
| 582 | |
| 583 | input = unhexify_alloc( input_hex, &input_size ); |
| 584 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 585 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 586 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 587 | |
| 588 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 589 | key, key_size ) == PSA_SUCCESS ); |
| 590 | |
Moran Peker | f55e804 | 2018-05-29 16:28:28 +0300 | [diff] [blame] | 591 | TEST_ASSERT( psa_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS ); |
| 592 | TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 593 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 594 | TEST_ASSERT( psa_encrypt_generate_iv( &operation1, |
| 595 | iv, iv_size, |
| 596 | &iv_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 597 | output1_size = input_size + operation1.block_size; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 598 | output1 = mbedtls_calloc( 1, output1_size ); |
| 599 | TEST_ASSERT( output1 != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 600 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 601 | TEST_ASSERT( psa_cipher_update( &operation1, input, input_size, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 602 | output1, output1_size, |
| 603 | &output1_length ) == PSA_SUCCESS ); |
| 604 | TEST_ASSERT( psa_cipher_finish( &operation1, |
| 605 | output1 + output1_length, output1_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 606 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 607 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 608 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 609 | |
| 610 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 611 | |
| 612 | output2_size = output1_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 613 | output2 = mbedtls_calloc( 1, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 614 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 615 | TEST_ASSERT( psa_encrypt_set_iv( &operation2, |
| 616 | iv, iv_length ) == PSA_SUCCESS ); |
| 617 | TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 618 | output2, output2_size, |
| 619 | &output2_length ) == PSA_SUCCESS ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 620 | function_output_length = 0; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 621 | TEST_ASSERT( psa_cipher_finish( &operation2, |
| 622 | output2 + output2_length, |
| 623 | output2_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 624 | &function_output_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 625 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 626 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 627 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 628 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 629 | |
| 630 | TEST_ASSERT( input_size == output2_length ); |
| 631 | TEST_ASSERT( memcmp( input, output2, input_size ) == 0 ); |
| 632 | |
| 633 | exit: |
| 634 | mbedtls_free( key ); |
| 635 | mbedtls_free( input ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 636 | psa_destroy_key( key_slot ); |
| 637 | mbedtls_psa_crypto_free( ); |
| 638 | } |
| 639 | /* END_CASE */ |
| 640 | |
| 641 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame^] | 642 | void cipher_verify_output_multipart( int alg_arg, |
| 643 | int key_type_arg, |
| 644 | char *key_hex, |
| 645 | char *input_hex, |
| 646 | int first_part_size ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 647 | { |
| 648 | int key_slot = 1; |
| 649 | psa_key_type_t key_type = key_type_arg; |
| 650 | psa_algorithm_t alg = alg_arg; |
| 651 | unsigned char *key = NULL; |
| 652 | size_t key_size; |
| 653 | unsigned char iv[16] = {0}; |
| 654 | size_t iv_size = 16; |
| 655 | size_t iv_length = 0; |
| 656 | unsigned char *input = NULL; |
| 657 | size_t input_size = 0; |
| 658 | unsigned char *output1; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 659 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 660 | size_t output1_length = 0; |
| 661 | unsigned char *output2; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 662 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 663 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 664 | size_t function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 665 | psa_cipher_operation_t operation1; |
| 666 | psa_cipher_operation_t operation2; |
| 667 | |
| 668 | key = unhexify_alloc( key_hex, &key_size ); |
| 669 | TEST_ASSERT( key != NULL ); |
| 670 | |
| 671 | input = unhexify_alloc( input_hex, &input_size ); |
| 672 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 673 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 674 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 675 | |
| 676 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 677 | key, key_size ) == PSA_SUCCESS ); |
| 678 | |
| 679 | TEST_ASSERT( psa_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS ); |
| 680 | TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS ); |
| 681 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 682 | TEST_ASSERT( psa_encrypt_generate_iv( &operation1, |
| 683 | iv, iv_size, |
| 684 | &iv_length ) == PSA_SUCCESS ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 685 | output1_buffer_size = input_size + operation1.block_size; |
| 686 | output1 = mbedtls_calloc( 1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 687 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 688 | TEST_ASSERT( (unsigned int) first_part_size < input_size ); |
| 689 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 690 | TEST_ASSERT( psa_cipher_update( &operation1, input, first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 691 | output1, output1_buffer_size, |
| 692 | &function_output_length ) == PSA_SUCCESS ); |
| 693 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 694 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 695 | TEST_ASSERT( psa_cipher_update( &operation1, |
| 696 | input + first_part_size, |
| 697 | input_size - first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 698 | output1, output1_buffer_size, |
| 699 | &function_output_length ) == PSA_SUCCESS ); |
| 700 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 701 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 702 | TEST_ASSERT( psa_cipher_finish( &operation1, |
| 703 | output1 + output1_length, |
| 704 | output1_buffer_size - output1_length, |
| 705 | &function_output_length ) == PSA_SUCCESS ); |
| 706 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 707 | |
| 708 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 709 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 710 | output2_buffer_size = output1_length; |
| 711 | output2 = mbedtls_calloc( 1, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 712 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 713 | TEST_ASSERT( psa_encrypt_set_iv( &operation2, |
| 714 | iv, iv_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 715 | |
| 716 | TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 717 | output2, output2_buffer_size, |
| 718 | &function_output_length ) == PSA_SUCCESS ); |
| 719 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 720 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 721 | TEST_ASSERT( psa_cipher_update( &operation2, |
| 722 | output1 + first_part_size, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 723 | output1_length - first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 724 | output2, output2_buffer_size, |
| 725 | &function_output_length ) == PSA_SUCCESS ); |
| 726 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 727 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 728 | TEST_ASSERT( psa_cipher_finish( &operation2, |
| 729 | output2 + output2_length, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 730 | output2_buffer_size - output2_length, |
| 731 | &function_output_length ) == PSA_SUCCESS ); |
| 732 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 733 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 734 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 735 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 736 | TEST_ASSERT( input_size == output2_length ); |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 737 | TEST_ASSERT( memcmp( input, output2, input_size ) == 0 ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 738 | |
| 739 | exit: |
| 740 | mbedtls_free( key ); |
| 741 | mbedtls_free( input ); |
| 742 | psa_destroy_key( key_slot ); |
| 743 | mbedtls_psa_crypto_free( ); |
| 744 | } |
| 745 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 746 | |
| 747 | /* BEGIN_CASE */ |
| 748 | void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg ) |
| 749 | { |
| 750 | psa_key_type_t type = type_arg; |
| 751 | psa_algorithm_t alg = alg_arg; |
| 752 | size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg); |
| 753 | TEST_ASSERT( actual_size == (size_t) expected_size_arg ); |
| 754 | exit: |
| 755 | ; |
| 756 | } |
| 757 | /* END_CASE */ |
| 758 | |
| 759 | /* BEGIN_CASE */ |
| 760 | void sign_deterministic( int key_type_arg, char *key_hex, |
| 761 | int alg_arg, char *input_hex, char *output_hex ) |
| 762 | { |
| 763 | int slot = 1; |
| 764 | psa_key_type_t key_type = key_type_arg; |
| 765 | psa_algorithm_t alg = alg_arg; |
| 766 | unsigned char *key_data = NULL; |
| 767 | size_t key_size; |
| 768 | size_t key_bits; |
| 769 | unsigned char *input_data = NULL; |
| 770 | size_t input_size; |
| 771 | unsigned char *output_data = NULL; |
| 772 | size_t output_size; |
| 773 | unsigned char *signature = NULL; |
| 774 | size_t signature_size; |
| 775 | size_t signature_length = 0xdeadbeef; |
| 776 | psa_key_policy_t policy = {0}; |
| 777 | |
| 778 | key_data = unhexify_alloc( key_hex, &key_size ); |
| 779 | TEST_ASSERT( key_data != NULL ); |
| 780 | input_data = unhexify_alloc( input_hex, &input_size ); |
| 781 | TEST_ASSERT( input_data != NULL ); |
| 782 | output_data = unhexify_alloc( output_hex, &output_size ); |
| 783 | TEST_ASSERT( output_data != NULL ); |
| 784 | |
| 785 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 786 | |
| 787 | psa_key_policy_init( &policy ); |
| 788 | |
| 789 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg ); |
| 790 | |
| 791 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 792 | |
| 793 | TEST_ASSERT( psa_import_key( slot, key_type, |
| 794 | key_data, key_size ) == PSA_SUCCESS ); |
| 795 | TEST_ASSERT( psa_get_key_information( slot, |
| 796 | NULL, |
| 797 | &key_bits ) == PSA_SUCCESS ); |
| 798 | |
| 799 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, alg, key_bits ); |
| 800 | TEST_ASSERT( signature_size != 0 ); |
| 801 | signature = mbedtls_calloc( 1, signature_size ); |
| 802 | TEST_ASSERT( signature != NULL ); |
| 803 | |
| 804 | TEST_ASSERT( psa_asymmetric_sign( slot, alg, |
| 805 | input_data, input_size, |
| 806 | NULL, 0, |
| 807 | signature, signature_size, |
| 808 | &signature_length ) == PSA_SUCCESS ); |
| 809 | TEST_ASSERT( signature_length == output_size ); |
| 810 | TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 ); |
| 811 | |
| 812 | exit: |
| 813 | psa_destroy_key( slot ); |
| 814 | mbedtls_free( key_data ); |
| 815 | mbedtls_free( input_data ); |
| 816 | mbedtls_free( output_data ); |
| 817 | mbedtls_free( signature ); |
| 818 | mbedtls_psa_crypto_free( ); |
| 819 | } |
| 820 | /* END_CASE */ |
| 821 | |
| 822 | /* BEGIN_CASE */ |
| 823 | void sign_fail( int key_type_arg, char *key_hex, |
| 824 | int alg_arg, char *input_hex, |
| 825 | int signature_size, int expected_status_arg ) |
| 826 | { |
| 827 | int slot = 1; |
| 828 | psa_key_type_t key_type = key_type_arg; |
| 829 | psa_algorithm_t alg = alg_arg; |
| 830 | unsigned char *key_data = NULL; |
| 831 | size_t key_size; |
| 832 | unsigned char *input_data = NULL; |
| 833 | size_t input_size; |
| 834 | psa_status_t actual_status; |
| 835 | psa_status_t expected_status = expected_status_arg; |
| 836 | unsigned char *signature = NULL; |
| 837 | size_t signature_length = 0xdeadbeef; |
| 838 | psa_key_policy_t policy = {0}; |
| 839 | |
| 840 | key_data = unhexify_alloc( key_hex, &key_size ); |
| 841 | TEST_ASSERT( key_data != NULL ); |
| 842 | input_data = unhexify_alloc( input_hex, &input_size ); |
| 843 | TEST_ASSERT( input_data != NULL ); |
| 844 | signature = mbedtls_calloc( 1, signature_size ); |
| 845 | TEST_ASSERT( signature != NULL ); |
| 846 | |
| 847 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 848 | |
| 849 | psa_key_policy_init( &policy ); |
| 850 | |
| 851 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg ); |
| 852 | |
| 853 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 854 | |
| 855 | TEST_ASSERT( psa_import_key( slot, key_type, |
| 856 | key_data, key_size ) == PSA_SUCCESS ); |
| 857 | |
| 858 | actual_status = psa_asymmetric_sign( slot, alg, |
| 859 | input_data, input_size, |
| 860 | NULL, 0, |
| 861 | signature, signature_size, |
| 862 | &signature_length ); |
| 863 | TEST_ASSERT( actual_status == expected_status ); |
| 864 | TEST_ASSERT( signature_length == 0 ); |
| 865 | |
| 866 | exit: |
| 867 | psa_destroy_key( slot ); |
| 868 | mbedtls_free( key_data ); |
| 869 | mbedtls_free( input_data ); |
| 870 | mbedtls_free( signature ); |
| 871 | mbedtls_psa_crypto_free( ); |
| 872 | } |
| 873 | /* END_CASE */ |
| 874 | |
| 875 | /* BEGIN_CASE */ |
| 876 | void key_policy( int usage_arg, int alg_arg ) |
| 877 | { |
| 878 | int key_slot = 1; |
| 879 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 880 | unsigned char key[32] = {0}; |
| 881 | psa_key_policy_t policy_set = {0}; |
| 882 | psa_key_policy_t policy_get = {0}; |
| 883 | |
| 884 | memset( key, 0x2a, sizeof( key ) ); |
| 885 | |
| 886 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 887 | |
| 888 | psa_key_policy_init(& policy_set ); |
| 889 | psa_key_policy_init(& policy_get ); |
| 890 | |
| 891 | psa_key_policy_set_usage( &policy_set, usage_arg, alg_arg ); |
| 892 | |
| 893 | TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == ( psa_key_usage_t )usage_arg ); |
| 894 | |
| 895 | TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set) == ( psa_algorithm_t )alg_arg ); |
| 896 | |
| 897 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS ); |
| 898 | |
| 899 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 900 | key, sizeof( key ) ) == PSA_SUCCESS ); |
| 901 | |
| 902 | TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS ); |
| 903 | |
| 904 | TEST_ASSERT( policy_get.usage == policy_set.usage ); |
| 905 | TEST_ASSERT( policy_get.alg == policy_set.alg ); |
| 906 | |
| 907 | exit: |
| 908 | psa_destroy_key( key_slot ); |
| 909 | mbedtls_psa_crypto_free( ); |
| 910 | } |
| 911 | /* END_CASE */ |
| 912 | |
| 913 | /* BEGIN_CASE */ |
| 914 | void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex ) |
| 915 | { |
| 916 | int key_slot = 1; |
| 917 | unsigned char* keypair = NULL; |
| 918 | size_t key_size = 0; |
| 919 | size_t signature_length = 0; |
| 920 | psa_key_policy_t policy = {0}; |
| 921 | int actual_status = PSA_SUCCESS; |
| 922 | |
| 923 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 924 | |
| 925 | psa_key_policy_init( &policy ); |
| 926 | |
| 927 | psa_key_policy_set_usage( &policy, usage_arg, alg_arg ); |
| 928 | |
| 929 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 930 | |
| 931 | if( usage_arg & PSA_KEY_USAGE_EXPORT ) |
| 932 | { |
| 933 | keypair = unhexify_alloc( key_hex, &key_size ); |
| 934 | TEST_ASSERT( keypair != NULL ); |
| 935 | TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR, |
| 936 | keypair, key_size ) == PSA_SUCCESS ); |
| 937 | actual_status = psa_asymmetric_sign( key_slot, |
| 938 | ( psa_algorithm_t )alg_arg, NULL, 0, NULL, 0, |
| 939 | NULL, 0, &signature_length ); |
| 940 | } |
| 941 | |
| 942 | if( usage_arg & PSA_KEY_USAGE_SIGN ) |
| 943 | { |
| 944 | keypair = unhexify_alloc( key_hex, &key_size ); |
| 945 | TEST_ASSERT( keypair != NULL ); |
| 946 | TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR, |
| 947 | keypair, key_size ) == PSA_SUCCESS ); |
| 948 | actual_status = psa_export_key( key_slot, NULL, 0, NULL ); |
| 949 | } |
| 950 | |
| 951 | TEST_ASSERT( actual_status == expected_status ); |
| 952 | |
| 953 | exit: |
| 954 | psa_destroy_key( key_slot ); |
| 955 | mbedtls_free( keypair ); |
| 956 | mbedtls_psa_crypto_free( ); |
| 957 | } |
| 958 | /* END_CASE */ |
| 959 | |
| 960 | /* BEGIN_CASE */ |
| 961 | void key_lifetime( int lifetime_arg ) |
| 962 | { |
| 963 | int key_slot = 1; |
| 964 | psa_key_type_t key_type = PSA_ALG_CBC_BASE; |
| 965 | unsigned char key[32] = {0}; |
| 966 | psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg; |
| 967 | psa_key_lifetime_t lifetime_get; |
| 968 | |
| 969 | memset( key, 0x2a, sizeof( key ) ); |
| 970 | |
| 971 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 972 | |
| 973 | TEST_ASSERT( psa_set_key_lifetime( key_slot, |
| 974 | lifetime_set ) == PSA_SUCCESS ); |
| 975 | |
| 976 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 977 | key, sizeof( key ) ) == PSA_SUCCESS ); |
| 978 | |
| 979 | TEST_ASSERT( psa_get_key_lifetime( key_slot, |
| 980 | &lifetime_get ) == PSA_SUCCESS ); |
| 981 | |
| 982 | TEST_ASSERT( lifetime_get == lifetime_set ); |
| 983 | |
| 984 | exit: |
| 985 | psa_destroy_key( key_slot ); |
| 986 | mbedtls_psa_crypto_free( ); |
| 987 | } |
| 988 | /* END_CASE */ |
| 989 | |
| 990 | |
| 991 | /* BEGIN_CASE */ |
| 992 | void key_lifetime_set_fail( int key_slot_arg, int lifetime_arg, int expected_status_arg ) |
| 993 | { |
| 994 | int key_slot = 1; |
| 995 | psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg; |
| 996 | psa_status_t actual_status; |
| 997 | psa_status_t expected_status = expected_status_arg; |
| 998 | |
| 999 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1000 | |
| 1001 | actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set ); |
| 1002 | |
| 1003 | if( actual_status == PSA_SUCCESS ) |
| 1004 | actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set ); |
| 1005 | |
| 1006 | TEST_ASSERT( expected_status == actual_status ); |
| 1007 | |
| 1008 | exit: |
| 1009 | psa_destroy_key( key_slot ); |
| 1010 | mbedtls_psa_crypto_free( ); |
| 1011 | } |
| 1012 | /* END_CASE */ |