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