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 | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 269 | void cipher_test_encrypt( int alg_arg, int key_type_arg, |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 270 | char *key_hex, |
| 271 | char *input_hex, char *output_hex, |
| 272 | int expected_status ) |
Moran Peker | 96cc00a | 2018-05-31 14:03:56 +0300 | [diff] [blame] | 273 | { |
| 274 | int key_slot = 1; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 275 | psa_status_t status; |
Moran Peker | 96cc00a | 2018-05-31 14:03:56 +0300 | [diff] [blame] | 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; |
Moran Peker | 9e3aa62 | 2018-06-07 12:08:47 +0300 | [diff] [blame] | 285 | size_t output_size, max_output_size = 0; |
Moran Peker | 96cc00a | 2018-05-31 14:03:56 +0300 | [diff] [blame] | 286 | size_t output_length = 0; |
| 287 | psa_cipher_operation_t operation; |
| 288 | |
| 289 | |
| 290 | key = unhexify_alloc( key_hex, &key_size ); |
| 291 | TEST_ASSERT( key != NULL ); |
| 292 | |
| 293 | input = unhexify_alloc( input_hex, &input_size ); |
| 294 | TEST_ASSERT( input != NULL ); |
| 295 | |
| 296 | expected_output = unhexify_alloc( output_hex, &output_size ); |
| 297 | TEST_ASSERT( expected_output != NULL ); |
| 298 | |
| 299 | memset( iv, 0x2a, sizeof( iv ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 300 | |
Moran Peker | 96cc00a | 2018-05-31 14:03:56 +0300 | [diff] [blame] | 301 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 302 | |
| 303 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 304 | key, key_size ) == PSA_SUCCESS ); |
| 305 | |
| 306 | TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 307 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 308 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 309 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
Moran Peker | 9e3aa62 | 2018-06-07 12:08:47 +0300 | [diff] [blame] | 310 | max_output_size = input_size + operation.block_size; |
| 311 | output = mbedtls_calloc( 1, max_output_size ); |
Moran Peker | 96cc00a | 2018-05-31 14:03:56 +0300 | [diff] [blame] | 312 | |
| 313 | TEST_ASSERT( psa_cipher_update( &operation, input, input_size, |
Moran Peker | 9e3aa62 | 2018-06-07 12:08:47 +0300 | [diff] [blame] | 314 | output, max_output_size, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 315 | &output_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 316 | status = psa_cipher_finish( &operation, output + output_length, |
Moran Peker | 9e3aa62 | 2018-06-07 12:08:47 +0300 | [diff] [blame] | 317 | max_output_size, &output_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 318 | TEST_ASSERT( status == (psa_status_t) expected_status ); |
| 319 | if( expected_status == PSA_SUCCESS ) |
| 320 | { |
| 321 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 322 | TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 ); |
| 323 | } |
Moran Peker | 96cc00a | 2018-05-31 14:03:56 +0300 | [diff] [blame] | 324 | exit: |
| 325 | mbedtls_free( key ); |
| 326 | mbedtls_free( input ); |
| 327 | psa_destroy_key( key_slot ); |
| 328 | mbedtls_psa_crypto_free( ); |
| 329 | } |
| 330 | /* END_CASE */ |
mohammad1603 | b152d4d | 2018-04-11 23:51:20 -0700 | [diff] [blame] | 331 | |
| 332 | /* BEGIN_CASE */ |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 333 | void cipher_test_encrypt_multipart( int alg_arg, int key_type_arg, |
| 334 | char *key_hex, |
| 335 | char *input_hex, |
| 336 | int first_part_size, char *output_hex ) |
Moran Peker | 7691fb7 | 2018-05-31 16:15:31 +0300 | [diff] [blame] | 337 | { |
| 338 | int key_slot = 1; |
| 339 | psa_key_type_t key_type = key_type_arg; |
| 340 | psa_algorithm_t alg = alg_arg; |
| 341 | unsigned char *key = NULL; |
| 342 | size_t key_size; |
| 343 | unsigned char iv[16] = {0}; |
| 344 | unsigned char *input = NULL; |
| 345 | size_t input_size = 0; |
| 346 | unsigned char *output; |
| 347 | unsigned char *expected_output; |
Moran Peker | 9e3aa62 | 2018-06-07 12:08:47 +0300 | [diff] [blame] | 348 | size_t output_size, max_output_size = 0; |
Moran Peker | 7691fb7 | 2018-05-31 16:15:31 +0300 | [diff] [blame] | 349 | size_t output_length = 0; |
| 350 | psa_cipher_operation_t operation; |
| 351 | |
| 352 | |
| 353 | key = unhexify_alloc( key_hex, &key_size ); |
| 354 | TEST_ASSERT( key != NULL ); |
| 355 | |
| 356 | input = unhexify_alloc( input_hex, &input_size ); |
| 357 | TEST_ASSERT( input != NULL ); |
| 358 | |
| 359 | expected_output = unhexify_alloc( output_hex, &output_size ); |
| 360 | TEST_ASSERT( expected_output != NULL ); |
| 361 | |
| 362 | memset( iv, 0x2a, sizeof( iv ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 363 | |
Moran Peker | 7691fb7 | 2018-05-31 16:15:31 +0300 | [diff] [blame] | 364 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 365 | |
| 366 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 367 | key, key_size ) == PSA_SUCCESS ); |
| 368 | |
| 369 | TEST_ASSERT( psa_encrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 370 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 371 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 372 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
Moran Peker | 9e3aa62 | 2018-06-07 12:08:47 +0300 | [diff] [blame] | 373 | max_output_size = input_size + operation.block_size; |
| 374 | output = mbedtls_calloc( 1, max_output_size ); |
Moran Peker | 7691fb7 | 2018-05-31 16:15:31 +0300 | [diff] [blame] | 375 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 376 | TEST_ASSERT( (unsigned int) first_part_size < input_size ); |
Moran Peker | 7691fb7 | 2018-05-31 16:15:31 +0300 | [diff] [blame] | 377 | TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, |
Moran Peker | 9e3aa62 | 2018-06-07 12:08:47 +0300 | [diff] [blame] | 378 | output, max_output_size, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 379 | &output_length ) == PSA_SUCCESS ); |
| 380 | TEST_ASSERT( psa_cipher_update( &operation, |
| 381 | input + first_part_size, |
| 382 | input_size - first_part_size, |
Moran Peker | 9e3aa62 | 2018-06-07 12:08:47 +0300 | [diff] [blame] | 383 | output, max_output_size, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 384 | &output_length ) == PSA_SUCCESS ); |
Moran Peker | 7691fb7 | 2018-05-31 16:15:31 +0300 | [diff] [blame] | 385 | TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, |
Moran Peker | 9e3aa62 | 2018-06-07 12:08:47 +0300 | [diff] [blame] | 386 | max_output_size, &output_length ) == PSA_SUCCESS ); |
Moran Peker | 7691fb7 | 2018-05-31 16:15:31 +0300 | [diff] [blame] | 387 | |
| 388 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 389 | |
| 390 | TEST_ASSERT( input_size == output_size ); |
| 391 | TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 ); |
| 392 | |
| 393 | exit: |
| 394 | mbedtls_free( key ); |
| 395 | mbedtls_free( input ); |
| 396 | psa_destroy_key( key_slot ); |
| 397 | mbedtls_psa_crypto_free( ); |
| 398 | } |
| 399 | /* END_CASE */ |
| 400 | |
| 401 | /* BEGIN_CASE */ |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 402 | void cipher_test_decrypt_multipart( int alg_arg, int key_type_arg, |
| 403 | char *key_hex, |
| 404 | char *input_hex, |
| 405 | int first_part_size, char *output_hex ) |
mohammad1603 | b152d4d | 2018-04-11 23:51:20 -0700 | [diff] [blame] | 406 | { |
| 407 | int key_slot = 1; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 408 | |
mohammad1603 | b152d4d | 2018-04-11 23:51:20 -0700 | [diff] [blame] | 409 | psa_key_type_t key_type = key_type_arg; |
| 410 | psa_algorithm_t alg = alg_arg; |
| 411 | unsigned char *key = NULL; |
| 412 | size_t key_size; |
| 413 | unsigned char iv[16] = {0}; |
| 414 | unsigned char *input = NULL; |
| 415 | size_t input_size = 0; |
| 416 | unsigned char *output; |
| 417 | unsigned char *expected_output; |
Moran Peker | 9e3aa62 | 2018-06-07 12:08:47 +0300 | [diff] [blame] | 418 | size_t output_size, max_output_size = 0; |
mohammad1603 | b152d4d | 2018-04-11 23:51:20 -0700 | [diff] [blame] | 419 | size_t output_length = 0; |
| 420 | psa_cipher_operation_t operation; |
| 421 | |
| 422 | |
| 423 | key = unhexify_alloc( key_hex, &key_size ); |
| 424 | TEST_ASSERT( key != NULL ); |
| 425 | |
| 426 | input = unhexify_alloc( input_hex, &input_size ); |
| 427 | TEST_ASSERT( input != NULL ); |
| 428 | |
| 429 | expected_output = unhexify_alloc( output_hex, &output_size ); |
| 430 | TEST_ASSERT( expected_output != NULL ); |
| 431 | |
| 432 | memset( iv, 0x2a, sizeof( iv ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 433 | |
mohammad1603 | b152d4d | 2018-04-11 23:51:20 -0700 | [diff] [blame] | 434 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 435 | |
| 436 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 437 | key, key_size ) == PSA_SUCCESS ); |
| 438 | |
| 439 | TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 440 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 441 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 442 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
mohammad1603 | b152d4d | 2018-04-11 23:51:20 -0700 | [diff] [blame] | 443 | |
Moran Peker | a9c3a65 | 2018-06-07 18:08:58 +0300 | [diff] [blame] | 444 | max_output_size = input_size + operation.block_size; |
Moran Peker | 9e3aa62 | 2018-06-07 12:08:47 +0300 | [diff] [blame] | 445 | output = mbedtls_calloc( 1, max_output_size ); |
mohammad1603 | b152d4d | 2018-04-11 23:51:20 -0700 | [diff] [blame] | 446 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 447 | TEST_ASSERT( (unsigned int) first_part_size < input_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 448 | TEST_ASSERT( psa_cipher_update( &operation, input, first_part_size, |
Moran Peker | 9e3aa62 | 2018-06-07 12:08:47 +0300 | [diff] [blame] | 449 | output, max_output_size, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 450 | &output_length ) == PSA_SUCCESS ); |
| 451 | TEST_ASSERT( psa_cipher_update( &operation, |
| 452 | input + first_part_size, |
| 453 | input_size - first_part_size, |
Moran Peker | 9e3aa62 | 2018-06-07 12:08:47 +0300 | [diff] [blame] | 454 | output, max_output_size, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 455 | &output_length ) == PSA_SUCCESS ); |
Moran Peker | f55e804 | 2018-05-29 16:28:28 +0300 | [diff] [blame] | 456 | TEST_ASSERT( psa_cipher_finish( &operation, output + output_length, |
Moran Peker | 9e3aa62 | 2018-06-07 12:08:47 +0300 | [diff] [blame] | 457 | max_output_size, &output_length ) == PSA_SUCCESS ); |
mohammad1603 | b152d4d | 2018-04-11 23:51:20 -0700 | [diff] [blame] | 458 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 459 | |
| 460 | TEST_ASSERT( input_size == output_size ); |
| 461 | TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 ); |
| 462 | |
| 463 | exit: |
| 464 | mbedtls_free( key ); |
| 465 | mbedtls_free( input ); |
| 466 | psa_destroy_key( key_slot ); |
| 467 | mbedtls_psa_crypto_free( ); |
| 468 | } |
| 469 | /* END_CASE */ |
| 470 | |
| 471 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 472 | /* BEGIN_CASE */ |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 473 | void cipher_test_decrypt( int alg_arg, int key_type_arg, |
| 474 | char *key_hex, |
| 475 | char *input_hex, char *output_hex, |
| 476 | int expected_status ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 477 | { |
| 478 | int key_slot = 1; |
| 479 | psa_status_t status; |
| 480 | psa_key_type_t key_type = key_type_arg; |
| 481 | psa_algorithm_t alg = alg_arg; |
| 482 | unsigned char *key = NULL; |
| 483 | size_t key_size; |
| 484 | unsigned char iv[16] = {0}; |
| 485 | unsigned char *input = NULL; |
| 486 | size_t input_size = 0; |
| 487 | unsigned char *output; |
| 488 | unsigned char *expected_output; |
Moran Peker | 9e3aa62 | 2018-06-07 12:08:47 +0300 | [diff] [blame] | 489 | size_t output_size, max_output_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 490 | size_t output_length = 0; |
| 491 | psa_cipher_operation_t operation; |
| 492 | |
| 493 | |
| 494 | key = unhexify_alloc( key_hex, &key_size ); |
| 495 | TEST_ASSERT( key != NULL ); |
| 496 | |
| 497 | input = unhexify_alloc( input_hex, &input_size ); |
| 498 | TEST_ASSERT( input != NULL ); |
| 499 | |
| 500 | expected_output = unhexify_alloc( output_hex, &output_size ); |
| 501 | TEST_ASSERT( expected_output != NULL ); |
| 502 | |
| 503 | memset( iv, 0x2a, sizeof( iv ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 504 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 505 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 506 | |
| 507 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 508 | key, key_size ) == PSA_SUCCESS ); |
| 509 | |
| 510 | TEST_ASSERT( psa_decrypt_setup( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 511 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 512 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 513 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 514 | |
Moran Peker | 9e3aa62 | 2018-06-07 12:08:47 +0300 | [diff] [blame] | 515 | max_output_size = input_size + operation.block_size; |
Moran Peker | a9c3a65 | 2018-06-07 18:08:58 +0300 | [diff] [blame] | 516 | output = mbedtls_calloc( 1, max_output_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 517 | |
| 518 | TEST_ASSERT( psa_cipher_update( &operation, input, input_size, |
Moran Peker | 9e3aa62 | 2018-06-07 12:08:47 +0300 | [diff] [blame] | 519 | output, max_output_size, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 520 | &output_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 521 | status = psa_cipher_finish( &operation, output + output_length, |
Moran Peker | 9e3aa62 | 2018-06-07 12:08:47 +0300 | [diff] [blame] | 522 | max_output_size, &output_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 523 | TEST_ASSERT( status == (psa_status_t) expected_status ); |
| 524 | |
| 525 | if( expected_status == PSA_SUCCESS ) |
| 526 | { |
| 527 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 528 | TEST_ASSERT( memcmp( expected_output, output, output_size ) == 0 ); |
| 529 | } |
| 530 | |
| 531 | |
| 532 | exit: |
| 533 | mbedtls_free( key ); |
| 534 | mbedtls_free( input ); |
| 535 | psa_destroy_key( key_slot ); |
| 536 | mbedtls_psa_crypto_free( ); |
| 537 | } |
| 538 | /* END_CASE */ |
| 539 | |
| 540 | |
| 541 | /* BEGIN_CASE */ |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 542 | void cipher_test_verify_output( int alg_arg, int key_type_arg, |
| 543 | char *key_hex, |
| 544 | char *input_hex ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 545 | { |
| 546 | int key_slot = 1; |
| 547 | psa_key_type_t key_type = key_type_arg; |
| 548 | psa_algorithm_t alg = alg_arg; |
| 549 | unsigned char *key = NULL; |
| 550 | size_t key_size; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 551 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 552 | size_t iv_size = 16; |
| 553 | size_t iv_length = 0; |
| 554 | unsigned char *input = NULL; |
| 555 | size_t input_size = 0; |
Moran Peker | 3205a65 | 2018-03-20 17:09:59 +0200 | [diff] [blame] | 556 | unsigned char *output1; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 557 | size_t output1_size = 0; |
| 558 | size_t output1_length = 0; |
Moran Peker | 3205a65 | 2018-03-20 17:09:59 +0200 | [diff] [blame] | 559 | unsigned char *output2; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 560 | size_t output2_size = 0; |
| 561 | size_t output2_length = 0; |
| 562 | size_t tmp_output_length = 0; |
| 563 | psa_cipher_operation_t operation1; |
| 564 | psa_cipher_operation_t operation2; |
| 565 | |
| 566 | key = unhexify_alloc( key_hex, &key_size ); |
| 567 | TEST_ASSERT( key != NULL ); |
| 568 | |
| 569 | input = unhexify_alloc( input_hex, &input_size ); |
| 570 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 571 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 572 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 573 | |
| 574 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 575 | key, key_size ) == PSA_SUCCESS ); |
| 576 | |
Moran Peker | f55e804 | 2018-05-29 16:28:28 +0300 | [diff] [blame] | 577 | TEST_ASSERT( psa_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS ); |
| 578 | TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 579 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 580 | TEST_ASSERT( psa_encrypt_generate_iv( &operation1, |
| 581 | iv, iv_size, |
| 582 | &iv_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 583 | output1_size = input_size + operation1.block_size; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 584 | output1 = mbedtls_calloc( 1, output1_size ); |
| 585 | TEST_ASSERT( output1 != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 586 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 587 | TEST_ASSERT( psa_cipher_update( &operation1, input, input_size, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 588 | output1, output1_size, |
| 589 | &output1_length ) == PSA_SUCCESS ); |
| 590 | TEST_ASSERT( psa_cipher_finish( &operation1, |
| 591 | output1 + output1_length, output1_size, |
| 592 | &tmp_output_length ) == PSA_SUCCESS ); |
| 593 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 594 | output1_length += tmp_output_length; |
| 595 | |
| 596 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 597 | |
| 598 | output2_size = output1_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 599 | output2 = mbedtls_calloc( 1, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 600 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 601 | TEST_ASSERT( psa_encrypt_set_iv( &operation2, |
| 602 | iv, iv_length ) == PSA_SUCCESS ); |
| 603 | TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 604 | output2, output2_size, |
| 605 | &output2_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 606 | tmp_output_length = 0; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 607 | TEST_ASSERT( psa_cipher_finish( &operation2, |
| 608 | output2 + output2_length, |
| 609 | output2_size, |
| 610 | &tmp_output_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 611 | |
| 612 | output2_length += tmp_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 613 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 614 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 615 | |
| 616 | TEST_ASSERT( input_size == output2_length ); |
| 617 | TEST_ASSERT( memcmp( input, output2, input_size ) == 0 ); |
| 618 | |
| 619 | exit: |
| 620 | mbedtls_free( key ); |
| 621 | mbedtls_free( input ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 622 | psa_destroy_key( key_slot ); |
| 623 | mbedtls_psa_crypto_free( ); |
| 624 | } |
| 625 | /* END_CASE */ |
| 626 | |
| 627 | /* BEGIN_CASE */ |
| 628 | void cipher_test_verify_output_multpart( int alg_arg, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 629 | int key_type_arg, |
| 630 | char *key_hex, |
| 631 | char *input_hex, |
| 632 | int first_part_size ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 633 | { |
| 634 | int key_slot = 1; |
| 635 | psa_key_type_t key_type = key_type_arg; |
| 636 | psa_algorithm_t alg = alg_arg; |
| 637 | unsigned char *key = NULL; |
| 638 | size_t key_size; |
| 639 | unsigned char iv[16] = {0}; |
| 640 | size_t iv_size = 16; |
| 641 | size_t iv_length = 0; |
| 642 | unsigned char *input = NULL; |
| 643 | size_t input_size = 0; |
| 644 | unsigned char *output1; |
| 645 | size_t output1_size = 0; |
| 646 | size_t output1_length = 0; |
| 647 | unsigned char *output2; |
| 648 | size_t output2_size = 0; |
| 649 | size_t output2_length = 0; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 650 | size_t tmp_output_length, temp = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 651 | psa_cipher_operation_t operation1; |
| 652 | psa_cipher_operation_t operation2; |
| 653 | |
| 654 | key = unhexify_alloc( key_hex, &key_size ); |
| 655 | TEST_ASSERT( key != NULL ); |
| 656 | |
| 657 | input = unhexify_alloc( input_hex, &input_size ); |
| 658 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 659 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 660 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 661 | |
| 662 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 663 | key, key_size ) == PSA_SUCCESS ); |
| 664 | |
| 665 | TEST_ASSERT( psa_encrypt_setup( &operation1, key_slot, alg ) == PSA_SUCCESS ); |
| 666 | TEST_ASSERT( psa_decrypt_setup( &operation2, key_slot, alg ) == PSA_SUCCESS ); |
| 667 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 668 | TEST_ASSERT( psa_encrypt_generate_iv( &operation1, |
| 669 | iv, iv_size, |
| 670 | &iv_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 671 | output1_size = input_size + operation1.block_size; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 672 | output1 = mbedtls_calloc( 1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 673 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 674 | TEST_ASSERT( (unsigned int) first_part_size < input_size ); |
| 675 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 676 | TEST_ASSERT( psa_cipher_update( &operation1, input, first_part_size, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 677 | output1, output1_size, |
| 678 | &output1_length ) == PSA_SUCCESS ); |
| 679 | temp = output1_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 680 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 681 | TEST_ASSERT( psa_cipher_update( &operation1, |
| 682 | input + first_part_size, |
| 683 | input_size - first_part_size, |
| 684 | output1, output1_size, |
| 685 | &output1_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 686 | output1_length += temp; |
| 687 | |
| 688 | TEST_ASSERT( psa_cipher_finish( &operation1, output1 + output1_length, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 689 | output1_size - output1_length, |
| 690 | &tmp_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | 691dfb3 | 2018-06-06 15:18:02 +0200 | [diff] [blame] | 691 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 692 | output1_length += tmp_output_length; |
| 693 | |
| 694 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 695 | |
Moran Peker | f55e804 | 2018-05-29 16:28:28 +0300 | [diff] [blame] | 696 | output2_size = output1_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 697 | output2 = mbedtls_calloc( 1, output2_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 698 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 699 | TEST_ASSERT( psa_encrypt_set_iv( &operation2, |
| 700 | iv, iv_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 701 | |
| 702 | TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 703 | output2, output2_size, |
| 704 | &output2_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 705 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 706 | temp = output2_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 707 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 708 | TEST_ASSERT( psa_cipher_update( &operation2, output1 + first_part_size, |
| 709 | output1_length - first_part_size, |
| 710 | output2, output2_size, |
| 711 | &output2_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 712 | |
| 713 | output2_length += temp; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 714 | tmp_output_length = 0; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 715 | TEST_ASSERT( psa_cipher_finish( &operation2, |
| 716 | output2 + output2_length, |
| 717 | output2_size - output2_length, |
| 718 | &tmp_output_length ) == PSA_SUCCESS ); |
Moran Peker | f55e804 | 2018-05-29 16:28:28 +0300 | [diff] [blame] | 719 | |
| 720 | output2_length += tmp_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 721 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 722 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 723 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 724 | TEST_ASSERT( input_size == output2_length ); |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 725 | TEST_ASSERT( memcmp( input, output2, input_size ) == 0 ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 726 | |
| 727 | exit: |
| 728 | mbedtls_free( key ); |
| 729 | mbedtls_free( input ); |
| 730 | psa_destroy_key( key_slot ); |
| 731 | mbedtls_psa_crypto_free( ); |
| 732 | } |
| 733 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 734 | |
| 735 | /* BEGIN_CASE */ |
| 736 | void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg ) |
| 737 | { |
| 738 | psa_key_type_t type = type_arg; |
| 739 | psa_algorithm_t alg = alg_arg; |
| 740 | size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg); |
| 741 | TEST_ASSERT( actual_size == (size_t) expected_size_arg ); |
| 742 | exit: |
| 743 | ; |
| 744 | } |
| 745 | /* END_CASE */ |
| 746 | |
| 747 | /* BEGIN_CASE */ |
| 748 | void sign_deterministic( int key_type_arg, char *key_hex, |
| 749 | int alg_arg, char *input_hex, char *output_hex ) |
| 750 | { |
| 751 | int slot = 1; |
| 752 | psa_key_type_t key_type = key_type_arg; |
| 753 | psa_algorithm_t alg = alg_arg; |
| 754 | unsigned char *key_data = NULL; |
| 755 | size_t key_size; |
| 756 | size_t key_bits; |
| 757 | unsigned char *input_data = NULL; |
| 758 | size_t input_size; |
| 759 | unsigned char *output_data = NULL; |
| 760 | size_t output_size; |
| 761 | unsigned char *signature = NULL; |
| 762 | size_t signature_size; |
| 763 | size_t signature_length = 0xdeadbeef; |
| 764 | psa_key_policy_t policy = {0}; |
| 765 | |
| 766 | key_data = unhexify_alloc( key_hex, &key_size ); |
| 767 | TEST_ASSERT( key_data != NULL ); |
| 768 | input_data = unhexify_alloc( input_hex, &input_size ); |
| 769 | TEST_ASSERT( input_data != NULL ); |
| 770 | output_data = unhexify_alloc( output_hex, &output_size ); |
| 771 | TEST_ASSERT( output_data != NULL ); |
| 772 | |
| 773 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 774 | |
| 775 | psa_key_policy_init( &policy ); |
| 776 | |
| 777 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg ); |
| 778 | |
| 779 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 780 | |
| 781 | TEST_ASSERT( psa_import_key( slot, key_type, |
| 782 | key_data, key_size ) == PSA_SUCCESS ); |
| 783 | TEST_ASSERT( psa_get_key_information( slot, |
| 784 | NULL, |
| 785 | &key_bits ) == PSA_SUCCESS ); |
| 786 | |
| 787 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, alg, key_bits ); |
| 788 | TEST_ASSERT( signature_size != 0 ); |
| 789 | signature = mbedtls_calloc( 1, signature_size ); |
| 790 | TEST_ASSERT( signature != NULL ); |
| 791 | |
| 792 | TEST_ASSERT( psa_asymmetric_sign( slot, alg, |
| 793 | input_data, input_size, |
| 794 | NULL, 0, |
| 795 | signature, signature_size, |
| 796 | &signature_length ) == PSA_SUCCESS ); |
| 797 | TEST_ASSERT( signature_length == output_size ); |
| 798 | TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 ); |
| 799 | |
| 800 | exit: |
| 801 | psa_destroy_key( slot ); |
| 802 | mbedtls_free( key_data ); |
| 803 | mbedtls_free( input_data ); |
| 804 | mbedtls_free( output_data ); |
| 805 | mbedtls_free( signature ); |
| 806 | mbedtls_psa_crypto_free( ); |
| 807 | } |
| 808 | /* END_CASE */ |
| 809 | |
| 810 | /* BEGIN_CASE */ |
| 811 | void sign_fail( int key_type_arg, char *key_hex, |
| 812 | int alg_arg, char *input_hex, |
| 813 | int signature_size, int expected_status_arg ) |
| 814 | { |
| 815 | int slot = 1; |
| 816 | psa_key_type_t key_type = key_type_arg; |
| 817 | psa_algorithm_t alg = alg_arg; |
| 818 | unsigned char *key_data = NULL; |
| 819 | size_t key_size; |
| 820 | unsigned char *input_data = NULL; |
| 821 | size_t input_size; |
| 822 | psa_status_t actual_status; |
| 823 | psa_status_t expected_status = expected_status_arg; |
| 824 | unsigned char *signature = NULL; |
| 825 | size_t signature_length = 0xdeadbeef; |
| 826 | psa_key_policy_t policy = {0}; |
| 827 | |
| 828 | key_data = unhexify_alloc( key_hex, &key_size ); |
| 829 | TEST_ASSERT( key_data != NULL ); |
| 830 | input_data = unhexify_alloc( input_hex, &input_size ); |
| 831 | TEST_ASSERT( input_data != NULL ); |
| 832 | signature = mbedtls_calloc( 1, signature_size ); |
| 833 | TEST_ASSERT( signature != NULL ); |
| 834 | |
| 835 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 836 | |
| 837 | psa_key_policy_init( &policy ); |
| 838 | |
| 839 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg ); |
| 840 | |
| 841 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 842 | |
| 843 | TEST_ASSERT( psa_import_key( slot, key_type, |
| 844 | key_data, key_size ) == PSA_SUCCESS ); |
| 845 | |
| 846 | actual_status = psa_asymmetric_sign( slot, alg, |
| 847 | input_data, input_size, |
| 848 | NULL, 0, |
| 849 | signature, signature_size, |
| 850 | &signature_length ); |
| 851 | TEST_ASSERT( actual_status == expected_status ); |
| 852 | TEST_ASSERT( signature_length == 0 ); |
| 853 | |
| 854 | exit: |
| 855 | psa_destroy_key( slot ); |
| 856 | mbedtls_free( key_data ); |
| 857 | mbedtls_free( input_data ); |
| 858 | mbedtls_free( signature ); |
| 859 | mbedtls_psa_crypto_free( ); |
| 860 | } |
| 861 | /* END_CASE */ |
| 862 | |
| 863 | /* BEGIN_CASE */ |
| 864 | void key_policy( int usage_arg, int alg_arg ) |
| 865 | { |
| 866 | int key_slot = 1; |
| 867 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 868 | unsigned char key[32] = {0}; |
| 869 | psa_key_policy_t policy_set = {0}; |
| 870 | psa_key_policy_t policy_get = {0}; |
| 871 | |
| 872 | memset( key, 0x2a, sizeof( key ) ); |
| 873 | |
| 874 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 875 | |
| 876 | psa_key_policy_init(& policy_set ); |
| 877 | psa_key_policy_init(& policy_get ); |
| 878 | |
| 879 | psa_key_policy_set_usage( &policy_set, usage_arg, alg_arg ); |
| 880 | |
| 881 | TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == ( psa_key_usage_t )usage_arg ); |
| 882 | |
| 883 | TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set) == ( psa_algorithm_t )alg_arg ); |
| 884 | |
| 885 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS ); |
| 886 | |
| 887 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 888 | key, sizeof( key ) ) == PSA_SUCCESS ); |
| 889 | |
| 890 | TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS ); |
| 891 | |
| 892 | TEST_ASSERT( policy_get.usage == policy_set.usage ); |
| 893 | TEST_ASSERT( policy_get.alg == policy_set.alg ); |
| 894 | |
| 895 | exit: |
| 896 | psa_destroy_key( key_slot ); |
| 897 | mbedtls_psa_crypto_free( ); |
| 898 | } |
| 899 | /* END_CASE */ |
| 900 | |
| 901 | /* BEGIN_CASE */ |
| 902 | void key_policy_fail( int usage_arg, int alg_arg, int expected_status, char *key_hex ) |
| 903 | { |
| 904 | int key_slot = 1; |
| 905 | unsigned char* keypair = NULL; |
| 906 | size_t key_size = 0; |
| 907 | size_t signature_length = 0; |
| 908 | psa_key_policy_t policy = {0}; |
| 909 | int actual_status = PSA_SUCCESS; |
| 910 | |
| 911 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 912 | |
| 913 | psa_key_policy_init( &policy ); |
| 914 | |
| 915 | psa_key_policy_set_usage( &policy, usage_arg, alg_arg ); |
| 916 | |
| 917 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 918 | |
| 919 | if( usage_arg & PSA_KEY_USAGE_EXPORT ) |
| 920 | { |
| 921 | keypair = unhexify_alloc( key_hex, &key_size ); |
| 922 | TEST_ASSERT( keypair != NULL ); |
| 923 | TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR, |
| 924 | keypair, key_size ) == PSA_SUCCESS ); |
| 925 | actual_status = psa_asymmetric_sign( key_slot, |
| 926 | ( psa_algorithm_t )alg_arg, NULL, 0, NULL, 0, |
| 927 | NULL, 0, &signature_length ); |
| 928 | } |
| 929 | |
| 930 | if( usage_arg & PSA_KEY_USAGE_SIGN ) |
| 931 | { |
| 932 | keypair = unhexify_alloc( key_hex, &key_size ); |
| 933 | TEST_ASSERT( keypair != NULL ); |
| 934 | TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR, |
| 935 | keypair, key_size ) == PSA_SUCCESS ); |
| 936 | actual_status = psa_export_key( key_slot, NULL, 0, NULL ); |
| 937 | } |
| 938 | |
| 939 | TEST_ASSERT( actual_status == expected_status ); |
| 940 | |
| 941 | exit: |
| 942 | psa_destroy_key( key_slot ); |
| 943 | mbedtls_free( keypair ); |
| 944 | mbedtls_psa_crypto_free( ); |
| 945 | } |
| 946 | /* END_CASE */ |
| 947 | |
| 948 | /* BEGIN_CASE */ |
| 949 | void key_lifetime( int lifetime_arg ) |
| 950 | { |
| 951 | int key_slot = 1; |
| 952 | psa_key_type_t key_type = PSA_ALG_CBC_BASE; |
| 953 | unsigned char key[32] = {0}; |
| 954 | psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg; |
| 955 | psa_key_lifetime_t lifetime_get; |
| 956 | |
| 957 | memset( key, 0x2a, sizeof( key ) ); |
| 958 | |
| 959 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 960 | |
| 961 | TEST_ASSERT( psa_set_key_lifetime( key_slot, |
| 962 | lifetime_set ) == PSA_SUCCESS ); |
| 963 | |
| 964 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 965 | key, sizeof( key ) ) == PSA_SUCCESS ); |
| 966 | |
| 967 | TEST_ASSERT( psa_get_key_lifetime( key_slot, |
| 968 | &lifetime_get ) == PSA_SUCCESS ); |
| 969 | |
| 970 | TEST_ASSERT( lifetime_get == lifetime_set ); |
| 971 | |
| 972 | exit: |
| 973 | psa_destroy_key( key_slot ); |
| 974 | mbedtls_psa_crypto_free( ); |
| 975 | } |
| 976 | /* END_CASE */ |
| 977 | |
| 978 | |
| 979 | /* BEGIN_CASE */ |
| 980 | void key_lifetime_set_fail( int key_slot_arg, int lifetime_arg, int expected_status_arg ) |
| 981 | { |
| 982 | int key_slot = 1; |
| 983 | psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg; |
| 984 | psa_status_t actual_status; |
| 985 | psa_status_t expected_status = expected_status_arg; |
| 986 | |
| 987 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 988 | |
| 989 | actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set ); |
| 990 | |
| 991 | if( actual_status == PSA_SUCCESS ) |
| 992 | actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set ); |
| 993 | |
| 994 | TEST_ASSERT( expected_status == actual_status ); |
| 995 | |
| 996 | exit: |
| 997 | psa_destroy_key( key_slot ); |
| 998 | mbedtls_psa_crypto_free( ); |
| 999 | } |
| 1000 | /* END_CASE */ |