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