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 | |
| 267 | /* BEGIN_CASE */ |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 268 | void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg ) |
| 269 | { |
| 270 | psa_key_type_t type = type_arg; |
| 271 | psa_algorithm_t alg = alg_arg; |
| 272 | size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg); |
| 273 | TEST_ASSERT( actual_size == (size_t) expected_size_arg ); |
| 274 | exit: |
| 275 | ; |
| 276 | } |
| 277 | /* END_CASE */ |
| 278 | |
| 279 | /* BEGIN_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 280 | void sign_deterministic( int key_type_arg, char *key_hex, |
| 281 | int alg_arg, char *input_hex, char *output_hex ) |
| 282 | { |
| 283 | int slot = 1; |
| 284 | psa_key_type_t key_type = key_type_arg; |
| 285 | psa_algorithm_t alg = alg_arg; |
| 286 | unsigned char *key_data = NULL; |
| 287 | size_t key_size; |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 288 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 289 | unsigned char *input_data = NULL; |
| 290 | size_t input_size; |
| 291 | unsigned char *output_data = NULL; |
| 292 | size_t output_size; |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 293 | unsigned char *signature = NULL; |
| 294 | size_t signature_size; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 295 | size_t signature_length = 0xdeadbeef; |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 296 | psa_key_policy_t policy = {0}; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 297 | |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 298 | key_data = unhexify_alloc( key_hex, &key_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 299 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 300 | input_data = unhexify_alloc( input_hex, &input_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 301 | TEST_ASSERT( input_data != NULL ); |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 302 | output_data = unhexify_alloc( output_hex, &output_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 303 | TEST_ASSERT( output_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 304 | |
| 305 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 306 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 307 | psa_key_policy_init( &policy ); |
| 308 | |
| 309 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg ); |
| 310 | |
| 311 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 312 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 313 | TEST_ASSERT( psa_import_key( slot, key_type, |
| 314 | key_data, key_size ) == PSA_SUCCESS ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 315 | TEST_ASSERT( psa_get_key_information( slot, |
| 316 | NULL, |
| 317 | &key_bits ) == PSA_SUCCESS ); |
| 318 | |
| 319 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, alg, key_bits ); |
| 320 | TEST_ASSERT( signature_size != 0 ); |
| 321 | signature = mbedtls_calloc( 1, signature_size ); |
| 322 | TEST_ASSERT( signature != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 323 | |
| 324 | TEST_ASSERT( psa_asymmetric_sign( slot, alg, |
| 325 | input_data, input_size, |
| 326 | NULL, 0, |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 327 | signature, signature_size, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 328 | &signature_length ) == PSA_SUCCESS ); |
| 329 | TEST_ASSERT( signature_length == output_size ); |
| 330 | TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 ); |
| 331 | |
| 332 | exit: |
| 333 | psa_destroy_key( slot ); |
| 334 | mbedtls_free( key_data ); |
| 335 | mbedtls_free( input_data ); |
| 336 | mbedtls_free( output_data ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 337 | mbedtls_free( signature ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 338 | mbedtls_psa_crypto_free( ); |
| 339 | } |
| 340 | /* END_CASE */ |
| 341 | |
| 342 | /* BEGIN_CASE */ |
| 343 | void sign_fail( int key_type_arg, char *key_hex, |
| 344 | int alg_arg, char *input_hex, |
| 345 | int signature_size, int expected_status_arg ) |
| 346 | { |
| 347 | int slot = 1; |
| 348 | psa_key_type_t key_type = key_type_arg; |
| 349 | psa_algorithm_t alg = alg_arg; |
| 350 | unsigned char *key_data = NULL; |
| 351 | size_t key_size; |
| 352 | unsigned char *input_data = NULL; |
| 353 | size_t input_size; |
| 354 | psa_status_t actual_status; |
| 355 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 356 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 357 | size_t signature_length = 0xdeadbeef; |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 358 | psa_key_policy_t policy = {0}; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 359 | |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 360 | key_data = unhexify_alloc( key_hex, &key_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 361 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 362 | input_data = unhexify_alloc( input_hex, &input_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 363 | TEST_ASSERT( input_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 364 | signature = mbedtls_calloc( 1, signature_size ); |
| 365 | TEST_ASSERT( signature != NULL ); |
| 366 | |
| 367 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 368 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 369 | psa_key_policy_init( &policy ); |
| 370 | |
| 371 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg ); |
| 372 | |
| 373 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 374 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 375 | TEST_ASSERT( psa_import_key( slot, key_type, |
| 376 | key_data, key_size ) == PSA_SUCCESS ); |
| 377 | |
| 378 | actual_status = psa_asymmetric_sign( slot, alg, |
| 379 | input_data, input_size, |
| 380 | NULL, 0, |
| 381 | signature, signature_size, |
| 382 | &signature_length ); |
| 383 | TEST_ASSERT( actual_status == expected_status ); |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 384 | TEST_ASSERT( signature_length == 0 ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 385 | |
| 386 | exit: |
| 387 | psa_destroy_key( slot ); |
| 388 | mbedtls_free( key_data ); |
| 389 | mbedtls_free( input_data ); |
| 390 | mbedtls_free( signature ); |
| 391 | mbedtls_psa_crypto_free( ); |
| 392 | } |
| 393 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 394 | |
| 395 | /* BEGIN_CASE */ |
| 396 | void key_policy( int usage_arg, int alg_arg ) |
| 397 | { |
| 398 | int key_slot = 1; |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 399 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 400 | unsigned char key[32] = {0}; |
| 401 | psa_key_policy_t policy_set = {0}; |
| 402 | psa_key_policy_t policy_get = {0}; |
| 403 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 404 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 405 | memset( key, 0x2a, sizeof( key ) ); |
| 406 | |
| 407 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 408 | |
| 409 | psa_key_policy_init(& policy_set ); |
| 410 | psa_key_policy_init(& policy_get ); |
| 411 | |
| 412 | psa_key_policy_set_usage( &policy_set, usage_arg, alg_arg ); |
| 413 | |
| 414 | TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == ( psa_key_usage_t )usage_arg ); |
| 415 | |
| 416 | TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set) == ( psa_algorithm_t )alg_arg ); |
| 417 | |
| 418 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS ); |
| 419 | |
| 420 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 421 | key, sizeof( key ) ) == PSA_SUCCESS ); |
| 422 | |
| 423 | TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS ); |
| 424 | |
| 425 | TEST_ASSERT( policy_get.usage == policy_set.usage ); |
| 426 | TEST_ASSERT( policy_get.alg == policy_set.alg ); |
| 427 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 428 | |
| 429 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 430 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 431 | exit: |
| 432 | psa_destroy_key( key_slot ); |
| 433 | mbedtls_psa_crypto_free( ); |
| 434 | } |
| 435 | /* END_CASE */ |
| 436 | |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 437 | /* BEGIN_CASE */ |
| 438 | void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex ) |
| 439 | { |
| 440 | int key_slot = 1; |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 441 | unsigned char* keypair = NULL; |
| 442 | size_t key_size = 0; |
| 443 | size_t signature_length = 0; |
| 444 | psa_key_policy_t policy = {0}; |
| 445 | int actual_status = PSA_SUCCESS; |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 446 | |
| 447 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 448 | |
| 449 | psa_key_policy_init( &policy ); |
| 450 | |
| 451 | psa_key_policy_set_usage( &policy, usage_arg, alg_arg ); |
| 452 | |
| 453 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 454 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 455 | if( usage_arg & PSA_KEY_USAGE_EXPORT ) |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 456 | { |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 457 | keypair = unhexify_alloc( key_hex, &key_size ); |
| 458 | TEST_ASSERT( keypair != NULL ); |
| 459 | TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR, |
| 460 | keypair, key_size ) == PSA_SUCCESS ); |
| 461 | actual_status = psa_asymmetric_sign( key_slot, |
| 462 | ( psa_algorithm_t )alg_arg, NULL, 0, NULL, 0, |
| 463 | NULL, 0, &signature_length ); |
| 464 | } |
| 465 | |
| 466 | if( usage_arg & PSA_KEY_USAGE_SIGN ) |
| 467 | { |
mohammad1603 | d926b88 | 2018-04-16 01:53:20 -0700 | [diff] [blame] | 468 | keypair = unhexify_alloc( key_hex, &key_size ); |
| 469 | TEST_ASSERT( keypair != NULL ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 470 | TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR, |
mohammad1603 | d926b88 | 2018-04-16 01:53:20 -0700 | [diff] [blame] | 471 | keypair, key_size ) == PSA_SUCCESS ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 472 | actual_status = psa_export_key( key_slot, NULL, 0, NULL ); |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | TEST_ASSERT( actual_status == expected_status ); |
| 476 | |
| 477 | exit: |
| 478 | psa_destroy_key( key_slot ); |
mohammad1603 | d926b88 | 2018-04-16 01:53:20 -0700 | [diff] [blame] | 479 | mbedtls_free( keypair ); |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 480 | mbedtls_psa_crypto_free( ); |
| 481 | } |
| 482 | /* END_CASE */ |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 483 | |
| 484 | /* BEGIN_CASE */ |
| 485 | void key_lifetime( int lifetime_arg ) |
| 486 | { |
| 487 | int key_slot = 1; |
| 488 | psa_key_type_t key_type = PSA_ALG_CBC_BASE; |
| 489 | unsigned char key[32] = {0}; |
| 490 | psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg; |
| 491 | psa_key_lifetime_t lifetime_get; |
| 492 | memset( key, 0x2a, sizeof( key ) ); |
| 493 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 494 | TEST_ASSERT( psa_set_key_lifetime( key_slot, |
| 495 | lifetime_set ) == PSA_SUCCESS ); |
| 496 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 497 | key, sizeof( key ) ) == PSA_SUCCESS ); |
| 498 | TEST_ASSERT( psa_get_key_lifetime( key_slot, |
| 499 | &lifetime_get ) == PSA_SUCCESS ); |
| 500 | TEST_ASSERT( lifetime_get == lifetime_set ); |
| 501 | exit: |
| 502 | psa_destroy_key( key_slot ); |
| 503 | mbedtls_psa_crypto_free( ); |
| 504 | } |
| 505 | /* END_CASE */ |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 506 | |
| 507 | /* BEGIN_CASE */ |
| 508 | void key_lifetime_set_fail( int key_slot_arg, int lifetime_arg, int expected_status_arg ) |
| 509 | { |
| 510 | int key_slot = 1; |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 511 | psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg; |
| 512 | psa_status_t actual_status; |
| 513 | psa_status_t expected_status = expected_status_arg; |
| 514 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 515 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 516 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 517 | actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set ); |
| 518 | |
| 519 | if( actual_status == PSA_SUCCESS ) |
| 520 | actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set ); |
| 521 | |
| 522 | TEST_ASSERT( expected_status == actual_status ); |
| 523 | |
| 524 | exit: |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 525 | psa_destroy_key( key_slot ); |
| 526 | mbedtls_psa_crypto_free( ); |
| 527 | } |
| 528 | /* END_CASE */ |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 529 | |
Moran Peker | 96cc00a | 2018-05-31 14:03:56 +0300 | [diff] [blame] | 530 | /* BEGIN_CASE */ |
Gilles Peskine | 691dfb3 | 2018-06-06 15:18:02 +0200 | [diff] [blame^] | 531 | void cipher_test_encrypt( int alg_arg, int key_type_arg, |
| 532 | char *key_hex, |
| 533 | char *input_hex, char *output_hex ) |
Moran Peker | 96cc00a | 2018-05-31 14:03:56 +0300 | [diff] [blame] | 534 | { |
| 535 | int key_slot = 1; |
| 536 | psa_key_type_t key_type = key_type_arg; |
| 537 | psa_algorithm_t alg = alg_arg; |
| 538 | unsigned char *key = NULL; |
| 539 | size_t key_size; |
| 540 | unsigned char iv[16] = {0}; |
| 541 | unsigned char *input = NULL; |
| 542 | size_t input_size = 0; |
| 543 | unsigned char *output; |
| 544 | unsigned char *expected_output; |
| 545 | size_t output_size = 0; |
| 546 | size_t output_length = 0; |
| 547 | psa_cipher_operation_t operation; |
| 548 | |
| 549 | |
| 550 | key = unhexify_alloc( key_hex, &key_size ); |
| 551 | TEST_ASSERT( key != NULL ); |
| 552 | |
| 553 | input = unhexify_alloc( input_hex, &input_size ); |
| 554 | TEST_ASSERT( input != NULL ); |
| 555 | |
| 556 | expected_output = unhexify_alloc( output_hex, &output_size ); |
| 557 | TEST_ASSERT( expected_output != NULL ); |
| 558 | |
| 559 | memset( iv, 0x2a, sizeof( iv ) ); |
Gilles Peskine | 691dfb3 | 2018-06-06 15:18:02 +0200 | [diff] [blame^] | 560 | |
Moran Peker | 96cc00a | 2018-05-31 14:03:56 +0300 | [diff] [blame] | 561 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 562 | |
| 563 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 564 | key, key_size ) == PSA_SUCCESS ); |
| 565 | |
| 566 | TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 567 | |
| 568 | TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, |
| 569 | sizeof( iv ) ) == PSA_SUCCESS ); |
| 570 | |
| 571 | output = mbedtls_calloc(0, output_size); |
| 572 | |
| 573 | TEST_ASSERT( psa_cipher_update( &operation, input, input_size, |
Gilles Peskine | 691dfb3 | 2018-06-06 15:18:02 +0200 | [diff] [blame^] | 574 | output, output_size, |
Moran Peker | 96cc00a | 2018-05-31 14:03:56 +0300 | [diff] [blame] | 575 | &output_length) == PSA_SUCCESS ); |
| 576 | TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, |
| 577 | output_size, &output_length) == PSA_SUCCESS ); |
| 578 | |
| 579 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 580 | |
| 581 | TEST_ASSERT( input_size == output_size ); |
| 582 | TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 ); |
| 583 | |
| 584 | exit: |
| 585 | mbedtls_free( key ); |
| 586 | mbedtls_free( input ); |
| 587 | psa_destroy_key( key_slot ); |
| 588 | mbedtls_psa_crypto_free( ); |
| 589 | } |
| 590 | /* END_CASE */ |
mohammad1603 | b152d4d | 2018-04-11 23:51:20 -0700 | [diff] [blame] | 591 | |
| 592 | /* BEGIN_CASE */ |
Gilles Peskine | 691dfb3 | 2018-06-06 15:18:02 +0200 | [diff] [blame^] | 593 | void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg, |
Moran Peker | 7691fb7 | 2018-05-31 16:15:31 +0300 | [diff] [blame] | 594 | char *key_hex, |
| 595 | char *input_hex, |
| 596 | int first_part_size, char *output_hex ) |
| 597 | { |
| 598 | int key_slot = 1; |
| 599 | psa_key_type_t key_type = key_type_arg; |
| 600 | psa_algorithm_t alg = alg_arg; |
| 601 | unsigned char *key = NULL; |
| 602 | size_t key_size; |
| 603 | unsigned char iv[16] = {0}; |
| 604 | unsigned char *input = NULL; |
| 605 | size_t input_size = 0; |
| 606 | unsigned char *output; |
| 607 | unsigned char *expected_output; |
| 608 | size_t output_size = 0; |
| 609 | size_t output_length = 0; |
| 610 | psa_cipher_operation_t operation; |
| 611 | |
| 612 | |
| 613 | key = unhexify_alloc( key_hex, &key_size ); |
| 614 | TEST_ASSERT( key != NULL ); |
| 615 | |
| 616 | input = unhexify_alloc( input_hex, &input_size ); |
| 617 | TEST_ASSERT( input != NULL ); |
| 618 | |
| 619 | expected_output = unhexify_alloc( output_hex, &output_size ); |
| 620 | TEST_ASSERT( expected_output != NULL ); |
| 621 | |
| 622 | memset( iv, 0x2a, sizeof( iv ) ); |
Gilles Peskine | 691dfb3 | 2018-06-06 15:18:02 +0200 | [diff] [blame^] | 623 | |
Moran Peker | 7691fb7 | 2018-05-31 16:15:31 +0300 | [diff] [blame] | 624 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 625 | |
| 626 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 627 | key, key_size ) == PSA_SUCCESS ); |
| 628 | |
| 629 | TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 630 | |
| 631 | TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, |
Gilles Peskine | 691dfb3 | 2018-06-06 15:18:02 +0200 | [diff] [blame^] | 632 | sizeof( iv ) ) == PSA_SUCCESS ); |
Moran Peker | 7691fb7 | 2018-05-31 16:15:31 +0300 | [diff] [blame] | 633 | |
| 634 | output = mbedtls_calloc(0, output_size); |
| 635 | |
| 636 | TEST_ASSERT( (unsigned int)first_part_size < input_size ); |
| 637 | TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, |
Gilles Peskine | 691dfb3 | 2018-06-06 15:18:02 +0200 | [diff] [blame^] | 638 | output, output_size, |
Moran Peker | 7691fb7 | 2018-05-31 16:15:31 +0300 | [diff] [blame] | 639 | &output_length) == PSA_SUCCESS ); |
Gilles Peskine | 691dfb3 | 2018-06-06 15:18:02 +0200 | [diff] [blame^] | 640 | TEST_ASSERT( psa_cipher_update( &operation, input + first_part_size, |
| 641 | input_size - first_part_size, |
| 642 | output, output_size, |
Moran Peker | 7691fb7 | 2018-05-31 16:15:31 +0300 | [diff] [blame] | 643 | &output_length) == PSA_SUCCESS ); |
| 644 | TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, |
| 645 | output_size, &output_length) == PSA_SUCCESS ); |
| 646 | |
| 647 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 648 | |
| 649 | TEST_ASSERT( input_size == output_size ); |
| 650 | TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 ); |
| 651 | |
| 652 | exit: |
| 653 | mbedtls_free( key ); |
| 654 | mbedtls_free( input ); |
| 655 | psa_destroy_key( key_slot ); |
| 656 | mbedtls_psa_crypto_free( ); |
| 657 | } |
| 658 | /* END_CASE */ |
| 659 | |
| 660 | /* BEGIN_CASE */ |
Gilles Peskine | 691dfb3 | 2018-06-06 15:18:02 +0200 | [diff] [blame^] | 661 | void cipher_test_decrypt( int alg_arg, int key_type_arg, |
mohammad1603 | b152d4d | 2018-04-11 23:51:20 -0700 | [diff] [blame] | 662 | char *key_hex, |
| 663 | char *input_hex, char *output_hex ) |
| 664 | { |
| 665 | int key_slot = 1; |
| 666 | psa_key_type_t key_type = key_type_arg; |
| 667 | psa_algorithm_t alg = alg_arg; |
| 668 | unsigned char *key = NULL; |
| 669 | size_t key_size; |
| 670 | unsigned char iv[16] = {0}; |
| 671 | unsigned char *input = NULL; |
| 672 | size_t input_size = 0; |
| 673 | unsigned char *output; |
| 674 | unsigned char *expected_output; |
| 675 | size_t output_size = 0; |
| 676 | size_t output_length = 0; |
| 677 | psa_cipher_operation_t operation; |
| 678 | |
| 679 | |
| 680 | key = unhexify_alloc( key_hex, &key_size ); |
| 681 | TEST_ASSERT( key != NULL ); |
| 682 | |
| 683 | input = unhexify_alloc( input_hex, &input_size ); |
| 684 | TEST_ASSERT( input != NULL ); |
| 685 | |
| 686 | expected_output = unhexify_alloc( output_hex, &output_size ); |
| 687 | TEST_ASSERT( expected_output != NULL ); |
| 688 | |
| 689 | memset( iv, 0x2a, sizeof( iv ) ); |
Gilles Peskine | 691dfb3 | 2018-06-06 15:18:02 +0200 | [diff] [blame^] | 690 | |
mohammad1603 | b152d4d | 2018-04-11 23:51:20 -0700 | [diff] [blame] | 691 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 692 | |
| 693 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 694 | key, key_size ) == PSA_SUCCESS ); |
| 695 | |
| 696 | TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 697 | |
| 698 | TEST_ASSERT( psa_encrypt_set_iv( &operation, iv, |
| 699 | sizeof( iv ) ) == PSA_SUCCESS ); |
| 700 | |
Moran Peker | f55e804 | 2018-05-29 16:28:28 +0300 | [diff] [blame] | 701 | output = mbedtls_calloc(0, output_size); |
mohammad1603 | b152d4d | 2018-04-11 23:51:20 -0700 | [diff] [blame] | 702 | |
| 703 | TEST_ASSERT( psa_cipher_update( &operation, input, input_size, |
Gilles Peskine | 691dfb3 | 2018-06-06 15:18:02 +0200 | [diff] [blame^] | 704 | output, output_size, |
mohammad1603 | b152d4d | 2018-04-11 23:51:20 -0700 | [diff] [blame] | 705 | &output_length) == PSA_SUCCESS ); |
Moran Peker | f55e804 | 2018-05-29 16:28:28 +0300 | [diff] [blame] | 706 | TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, |
Moran Peker | 0071b87 | 2018-04-22 20:16:58 +0300 | [diff] [blame] | 707 | output_size, &output_length) == PSA_SUCCESS ); |
mohammad1603 | b152d4d | 2018-04-11 23:51:20 -0700 | [diff] [blame] | 708 | |
| 709 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 710 | |
| 711 | TEST_ASSERT( input_size == output_size ); |
| 712 | TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 ); |
| 713 | |
| 714 | exit: |
| 715 | mbedtls_free( key ); |
| 716 | mbedtls_free( input ); |
| 717 | psa_destroy_key( key_slot ); |
| 718 | mbedtls_psa_crypto_free( ); |
| 719 | } |
| 720 | /* END_CASE */ |
| 721 | |
| 722 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 723 | /* BEGIN_CASE */ |
Gilles Peskine | 691dfb3 | 2018-06-06 15:18:02 +0200 | [diff] [blame^] | 724 | void cipher_test_verify_output( int alg_arg, int key_type_arg, |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 725 | char *key_hex, |
| 726 | char *input_hex ) |
| 727 | { |
| 728 | int key_slot = 1; |
| 729 | psa_key_type_t key_type = key_type_arg; |
| 730 | psa_algorithm_t alg = alg_arg; |
| 731 | unsigned char *key = NULL; |
| 732 | size_t key_size; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 733 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 734 | size_t iv_size = 16; |
| 735 | size_t iv_length = 0; |
| 736 | unsigned char *input = NULL; |
| 737 | size_t input_size = 0; |
Moran Peker | 3205a65 | 2018-03-20 17:09:59 +0200 | [diff] [blame] | 738 | unsigned char *output1; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 739 | size_t output1_size = 0; |
| 740 | size_t output1_length = 0; |
Moran Peker | 3205a65 | 2018-03-20 17:09:59 +0200 | [diff] [blame] | 741 | unsigned char *output2; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 742 | size_t output2_size = 0; |
| 743 | size_t output2_length = 0; |
| 744 | size_t tmp_output_length = 0; |
| 745 | psa_cipher_operation_t operation1; |
| 746 | psa_cipher_operation_t operation2; |
| 747 | |
| 748 | key = unhexify_alloc( key_hex, &key_size ); |
| 749 | TEST_ASSERT( key != NULL ); |
| 750 | |
| 751 | input = unhexify_alloc( input_hex, &input_size ); |
| 752 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 691dfb3 | 2018-06-06 15:18:02 +0200 | [diff] [blame^] | 753 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 754 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 755 | |
| 756 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 757 | key, key_size ) == PSA_SUCCESS ); |
| 758 | |
Moran Peker | f55e804 | 2018-05-29 16:28:28 +0300 | [diff] [blame] | 759 | TEST_ASSERT( psa_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS ); |
| 760 | TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 761 | |
mohammad1603 | 8481e74 | 2018-03-18 13:57:31 +0200 | [diff] [blame] | 762 | TEST_ASSERT( psa_encrypt_generate_iv( &operation1, iv, |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 763 | iv_size, &iv_length) == PSA_SUCCESS ); |
Moran Peker | f55e804 | 2018-05-29 16:28:28 +0300 | [diff] [blame] | 764 | output1_size = input_size; |
| 765 | output1 = mbedtls_calloc(0, output1_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 766 | TEST_ASSERT( psa_cipher_update( &operation1, input, input_size, |
Gilles Peskine | 691dfb3 | 2018-06-06 15:18:02 +0200 | [diff] [blame^] | 767 | output1, output1_size, |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 768 | &output1_length) == PSA_SUCCESS ); |
Moran Peker | f55e804 | 2018-05-29 16:28:28 +0300 | [diff] [blame] | 769 | TEST_ASSERT( psa_cipher_finish( &operation1, output1 + output1_length, |
Gilles Peskine | 691dfb3 | 2018-06-06 15:18:02 +0200 | [diff] [blame^] | 770 | output1_size, &tmp_output_length) == PSA_SUCCESS ); |
| 771 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 772 | output1_length += tmp_output_length; |
| 773 | |
| 774 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 775 | |
Moran Peker | f55e804 | 2018-05-29 16:28:28 +0300 | [diff] [blame] | 776 | output2_size = output1_length; |
| 777 | output2 = mbedtls_calloc(0, output2_size); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 778 | |
Moran Peker | f55e804 | 2018-05-29 16:28:28 +0300 | [diff] [blame] | 779 | TEST_ASSERT( psa_encrypt_set_iv( &operation2, iv, |
Gilles Peskine | 691dfb3 | 2018-06-06 15:18:02 +0200 | [diff] [blame^] | 780 | iv_length) == PSA_SUCCESS ); |
| 781 | TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 782 | output2, output2_size, &output2_length) == PSA_SUCCESS ); |
| 783 | tmp_output_length = 0; |
Moran Peker | f55e804 | 2018-05-29 16:28:28 +0300 | [diff] [blame] | 784 | TEST_ASSERT( psa_cipher_finish( &operation2, output2 + output2_length, |
Gilles Peskine | 691dfb3 | 2018-06-06 15:18:02 +0200 | [diff] [blame^] | 785 | output2_size, &tmp_output_length) == PSA_SUCCESS ); |
Moran Peker | f55e804 | 2018-05-29 16:28:28 +0300 | [diff] [blame] | 786 | |
| 787 | output2_length += tmp_output_length; |
Gilles Peskine | 691dfb3 | 2018-06-06 15:18:02 +0200 | [diff] [blame^] | 788 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 789 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 790 | |
| 791 | TEST_ASSERT( input_size == output1_length ); |
| 792 | TEST_ASSERT( output1_length == output2_length ); |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 793 | TEST_ASSERT( memcmp( input, output2, input_size ) == 0 ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 794 | |
| 795 | exit: |
| 796 | mbedtls_free( key ); |
| 797 | mbedtls_free( input ); |
| 798 | psa_destroy_key( key_slot ); |
| 799 | mbedtls_psa_crypto_free( ); |
| 800 | } |
| 801 | /* END_CASE */ |