Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2 | #include <stdint.h> |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 3 | #include "psa/crypto.h" |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 4 | #include "mbedtls/md.h" |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 5 | |
| 6 | #if(UINT32_MAX > SIZE_MAX) |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7 | #define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX ) |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 8 | #else |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 9 | #define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1 |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 10 | #endif |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 11 | /* END_HEADER */ |
| 12 | |
| 13 | /* BEGIN_DEPENDENCIES |
| 14 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 15 | * END_DEPENDENCIES |
| 16 | */ |
| 17 | |
| 18 | /* BEGIN_CASE */ |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 19 | void init_deinit( ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 20 | { |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 21 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 22 | int i; |
| 23 | for( i = 0; i <= 1; i++ ) |
| 24 | { |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 25 | status = psa_crypto_init( ); |
| 26 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 27 | status = psa_crypto_init( ); |
| 28 | TEST_ASSERT( status == PSA_SUCCESS ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 29 | mbedtls_psa_crypto_free( ); |
| 30 | } |
| 31 | } |
| 32 | /* END_CASE */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 33 | |
| 34 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 35 | void import( data_t *data, int type, int expected_status ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 36 | { |
| 37 | int slot = 1; |
| 38 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 39 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 40 | TEST_ASSERT( data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 41 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 42 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 43 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 44 | status = psa_import_key( slot, type, data->x, (size_t) data->len ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 45 | TEST_ASSERT( status == (psa_status_t) expected_status ); |
| 46 | if( status == PSA_SUCCESS ) |
| 47 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 48 | |
| 49 | exit: |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 50 | mbedtls_psa_crypto_free( ); |
| 51 | } |
| 52 | /* END_CASE */ |
| 53 | |
| 54 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 55 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 56 | int type_arg, |
| 57 | int alg_arg, |
| 58 | int usage_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 59 | int expected_bits, |
| 60 | int export_size_delta, |
| 61 | int expected_export_status, |
| 62 | int canonical_input ) |
| 63 | { |
| 64 | int slot = 1; |
| 65 | int slot2 = slot + 1; |
| 66 | psa_key_type_t type = type_arg; |
| 67 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 68 | unsigned char *exported = NULL; |
| 69 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 70 | size_t export_size; |
| 71 | size_t exported_length; |
| 72 | size_t reexported_length; |
| 73 | psa_key_type_t got_type; |
| 74 | size_t got_bits; |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 75 | psa_key_policy_t policy = {0}; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 76 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 77 | TEST_ASSERT( data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 78 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) ); |
| 79 | export_size = (ssize_t) data->len + export_size_delta; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 80 | exported = mbedtls_calloc( 1, export_size ); |
| 81 | TEST_ASSERT( exported != NULL ); |
| 82 | if( ! canonical_input ) |
| 83 | { |
| 84 | reexported = mbedtls_calloc( 1, export_size ); |
| 85 | TEST_ASSERT( reexported != NULL ); |
| 86 | } |
| 87 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 88 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 89 | psa_key_policy_init( &policy ); |
| 90 | |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 91 | psa_key_policy_set_usage( &policy, usage_arg, alg_arg ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 92 | |
| 93 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 94 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 95 | /* Import the key */ |
| 96 | TEST_ASSERT( psa_import_key( slot, type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 97 | data->x, (size_t) data->len ) == PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 98 | |
| 99 | /* Test the key information */ |
| 100 | TEST_ASSERT( psa_get_key_information( slot, |
| 101 | &got_type, &got_bits ) == |
| 102 | PSA_SUCCESS ); |
| 103 | TEST_ASSERT( got_type == type ); |
| 104 | TEST_ASSERT( got_bits == (size_t) expected_bits ); |
| 105 | |
| 106 | /* Export the key */ |
| 107 | status = psa_export_key( slot, |
| 108 | exported, export_size, |
| 109 | &exported_length ); |
| 110 | TEST_ASSERT( status == (psa_status_t) expected_export_status ); |
| 111 | if( status != PSA_SUCCESS ) |
| 112 | goto destroy; |
| 113 | |
| 114 | if( canonical_input ) |
| 115 | { |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 116 | TEST_ASSERT( exported_length == (size_t) data->len ); |
| 117 | TEST_ASSERT( memcmp( exported, data->x, (size_t) data->len ) == 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 118 | } |
| 119 | else |
| 120 | { |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 121 | TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS ); |
| 122 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 123 | TEST_ASSERT( psa_import_key( slot2, type, |
| 124 | exported, export_size ) == |
| 125 | PSA_SUCCESS ); |
| 126 | TEST_ASSERT( psa_export_key( slot2, |
| 127 | reexported, export_size, |
| 128 | &reexported_length ) == |
| 129 | PSA_SUCCESS ); |
| 130 | TEST_ASSERT( reexported_length == exported_length ); |
| 131 | TEST_ASSERT( memcmp( reexported, exported, |
| 132 | exported_length ) == 0 ); |
| 133 | } |
| 134 | |
| 135 | destroy: |
| 136 | /* Destroy the key */ |
| 137 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 138 | TEST_ASSERT( psa_get_key_information( |
| 139 | slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT ); |
| 140 | |
| 141 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 142 | mbedtls_free( exported ); |
| 143 | mbedtls_free( reexported ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 144 | mbedtls_psa_crypto_free( ); |
| 145 | } |
| 146 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 147 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 148 | |
| 149 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 150 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 151 | int type_arg, |
| 152 | int alg_arg, |
| 153 | int expected_bits, |
| 154 | int public_key_expected_length, |
| 155 | int expected_export_status ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 156 | { |
| 157 | int slot = 1; |
| 158 | psa_key_type_t type = type_arg; |
| 159 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 160 | unsigned char *exported = NULL; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 161 | size_t export_size; |
| 162 | size_t exported_length; |
| 163 | psa_key_type_t got_type; |
| 164 | size_t got_bits; |
| 165 | psa_key_policy_t policy = {0}; |
| 166 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 167 | TEST_ASSERT( data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 168 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) ); |
| 169 | export_size = (ssize_t) data->len; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 170 | exported = mbedtls_calloc( 1, export_size ); |
| 171 | TEST_ASSERT( exported != NULL ); |
| 172 | |
| 173 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 174 | |
| 175 | psa_key_policy_init( &policy ); |
| 176 | |
| 177 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, |
| 178 | alg_arg ); |
| 179 | |
| 180 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 181 | |
| 182 | /* Import the key */ |
| 183 | TEST_ASSERT( psa_import_key( slot, type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 184 | data->x, (size_t) data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 185 | PSA_SUCCESS ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 186 | |
| 187 | /* Test the key information */ |
| 188 | TEST_ASSERT( psa_get_key_information( slot, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame^] | 189 | &got_type, |
| 190 | &got_bits ) == PSA_SUCCESS ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 191 | TEST_ASSERT( got_type == type ); |
| 192 | TEST_ASSERT( got_bits == (size_t) expected_bits ); |
| 193 | |
| 194 | /* Export the key */ |
| 195 | status = psa_export_public_key( slot, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 196 | exported, export_size, |
| 197 | &exported_length ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 198 | TEST_ASSERT( status == (psa_status_t) expected_export_status ); |
| 199 | if( status != PSA_SUCCESS ) |
| 200 | goto destroy; |
| 201 | |
| 202 | TEST_ASSERT( exported_length == (size_t) public_key_expected_length ); |
| 203 | |
| 204 | destroy: |
| 205 | /* Destroy the key */ |
| 206 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 207 | TEST_ASSERT( psa_get_key_information( |
| 208 | slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT ); |
| 209 | |
| 210 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 211 | mbedtls_free( exported ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 212 | mbedtls_psa_crypto_free( ); |
| 213 | } |
| 214 | /* END_CASE */ |
| 215 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 216 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 217 | void hash_finish( int alg_arg, data_t *input, data_t *expected_hash ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 218 | { |
| 219 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 220 | unsigned char actual_hash[MBEDTLS_MD_MAX_SIZE]; |
| 221 | size_t actual_hash_length; |
| 222 | psa_hash_operation_t operation; |
| 223 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 224 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 225 | TEST_ASSERT( expected_hash != NULL ); |
| 226 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 227 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 228 | |
| 229 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 230 | |
| 231 | TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS ); |
| 232 | TEST_ASSERT( psa_hash_update( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 233 | input->x, (size_t) input->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 234 | PSA_SUCCESS ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 235 | TEST_ASSERT( psa_hash_finish( &operation, |
| 236 | actual_hash, sizeof( actual_hash ), |
| 237 | &actual_hash_length ) == PSA_SUCCESS ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 238 | TEST_ASSERT( actual_hash_length == (size_t) expected_hash->len ); |
| 239 | TEST_ASSERT( memcmp( expected_hash->x, actual_hash, |
| 240 | (size_t) expected_hash->len ) == 0 ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 241 | |
| 242 | exit: |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 243 | mbedtls_psa_crypto_free( ); |
| 244 | } |
| 245 | /* END_CASE */ |
| 246 | |
| 247 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 248 | void hash_verify( int alg_arg, data_t *input, data_t *expected_hash ) |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 249 | { |
| 250 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 251 | psa_hash_operation_t operation; |
| 252 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 253 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 254 | TEST_ASSERT( expected_hash != NULL ); |
| 255 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 256 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 257 | |
| 258 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 259 | |
| 260 | TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS ); |
| 261 | TEST_ASSERT( psa_hash_update( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 262 | input->x, (size_t) input->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 263 | PSA_SUCCESS ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 264 | TEST_ASSERT( psa_hash_verify( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 265 | expected_hash->x, |
| 266 | (size_t) expected_hash->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 267 | PSA_SUCCESS ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 268 | |
| 269 | exit: |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 270 | mbedtls_psa_crypto_free( ); |
| 271 | } |
| 272 | /* END_CASE */ |
| 273 | |
| 274 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 275 | void mac_verify( int key_type_arg, data_t *key, |
| 276 | int alg_arg, data_t *iv, |
| 277 | data_t *input, data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 278 | { |
| 279 | int key_slot = 1; |
| 280 | psa_key_type_t key_type = key_type_arg; |
| 281 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 282 | psa_mac_operation_t operation; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 283 | psa_key_policy_t policy; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 284 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 285 | TEST_ASSERT( key != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 286 | TEST_ASSERT( iv != NULL ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 287 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 288 | TEST_ASSERT( expected_mac != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 289 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 290 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( iv->len ) ); |
| 291 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 292 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 293 | |
| 294 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 295 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 296 | psa_key_policy_init( &policy ); |
| 297 | |
| 298 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg_arg ); |
| 299 | |
| 300 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 301 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 302 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 303 | key->x, (size_t) key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 304 | // TODO: support IV |
| 305 | TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 306 | TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS ); |
| 307 | TEST_ASSERT( psa_mac_update( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 308 | input->x, (size_t) input->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 309 | PSA_SUCCESS ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 310 | TEST_ASSERT( psa_mac_verify( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 311 | expected_mac->x, |
| 312 | (size_t) expected_mac->len ) == PSA_SUCCESS ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 313 | |
| 314 | exit: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 315 | psa_destroy_key( key_slot ); |
| 316 | mbedtls_psa_crypto_free( ); |
| 317 | } |
| 318 | /* END_CASE */ |
| 319 | |
Moran Peker | 7f87850 | 2018-06-06 17:09:40 +0300 | [diff] [blame] | 320 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 321 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 322 | void cipher_encrypt( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 323 | data_t *key, |
| 324 | data_t *input, data_t *expected_output, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 325 | int expected_status ) |
| 326 | { |
| 327 | int key_slot = 1; |
| 328 | psa_status_t status; |
| 329 | psa_key_type_t key_type = key_type_arg; |
| 330 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 331 | unsigned char iv[16] = {0}; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 332 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 333 | size_t output_buffer_size = 0; |
| 334 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 335 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 336 | psa_cipher_operation_t operation; |
| 337 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 338 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 339 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 340 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 341 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 342 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 343 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 344 | |
| 345 | memset( iv, 0x2a, sizeof( iv ) ); |
| 346 | |
| 347 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 348 | |
| 349 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 350 | key->x, (size_t) key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 351 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame^] | 352 | TEST_ASSERT( psa_encrypt_setup( &operation, |
| 353 | key_slot, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 354 | |
| 355 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 356 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 357 | output_buffer_size = (size_t) input->len + operation.block_size; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 358 | output = mbedtls_calloc( 1, output_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 359 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 360 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 361 | TEST_ASSERT( psa_cipher_update( &operation, input->x, (size_t) input->len, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 362 | output, output_buffer_size, |
| 363 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 364 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 365 | status = psa_cipher_finish( &operation, |
| 366 | output + function_output_length, |
| 367 | output_buffer_size, |
| 368 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 369 | total_output_length += function_output_length; |
| 370 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 371 | TEST_ASSERT( status == (psa_status_t) expected_status ); |
| 372 | if( expected_status == PSA_SUCCESS ) |
| 373 | { |
| 374 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 375 | TEST_ASSERT( total_output_length == (size_t) expected_output->len ); |
| 376 | TEST_ASSERT( memcmp( expected_output->x, output, |
| 377 | (size_t) expected_output->len ) == 0 ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 378 | } |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 379 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 380 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 381 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 382 | psa_destroy_key( key_slot ); |
| 383 | mbedtls_psa_crypto_free( ); |
| 384 | } |
| 385 | /* END_CASE */ |
| 386 | |
| 387 | /* BEGIN_CASE */ |
| 388 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 389 | data_t *key, |
| 390 | data_t *input, |
| 391 | int first_part_size, |
| 392 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 393 | { |
| 394 | int key_slot = 1; |
| 395 | psa_key_type_t key_type = key_type_arg; |
| 396 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 397 | unsigned char iv[16] = {0}; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 398 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 399 | size_t output_buffer_size = 0; |
| 400 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 401 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 402 | psa_cipher_operation_t operation; |
| 403 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 404 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 405 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 406 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 407 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 408 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 409 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 410 | |
| 411 | memset( iv, 0x2a, sizeof( iv ) ); |
| 412 | |
| 413 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 414 | |
| 415 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 416 | key->x, (size_t) key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 417 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame^] | 418 | TEST_ASSERT( psa_encrypt_setup( &operation, |
| 419 | key_slot, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 420 | |
| 421 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 422 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 423 | output_buffer_size = (size_t) input->len + operation.block_size; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 424 | output = mbedtls_calloc( 1, output_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 425 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 426 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 427 | TEST_ASSERT( (unsigned int) first_part_size < (size_t) input->len ); |
| 428 | TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 429 | output, output_buffer_size, |
| 430 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 431 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 432 | TEST_ASSERT( psa_cipher_update( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 433 | input->x + first_part_size, |
| 434 | (size_t) input->len - first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 435 | output, output_buffer_size, |
| 436 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 437 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 438 | TEST_ASSERT( psa_cipher_finish( &operation, |
| 439 | output + function_output_length, |
| 440 | output_buffer_size, |
| 441 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 442 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 443 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 444 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 445 | TEST_ASSERT( total_output_length == (size_t) expected_output->len ); |
| 446 | TEST_ASSERT( memcmp( expected_output->x, output, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 447 | (size_t) expected_output->len ) == 0 ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 448 | |
| 449 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 450 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 451 | psa_destroy_key( key_slot ); |
| 452 | mbedtls_psa_crypto_free( ); |
| 453 | } |
| 454 | /* END_CASE */ |
| 455 | |
| 456 | /* BEGIN_CASE */ |
| 457 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 458 | data_t *key, |
| 459 | data_t *input, |
| 460 | int first_part_size, |
| 461 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 462 | { |
| 463 | int key_slot = 1; |
| 464 | |
| 465 | psa_key_type_t key_type = key_type_arg; |
| 466 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 467 | unsigned char iv[16] = {0}; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 468 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 469 | size_t output_buffer_size = 0; |
| 470 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 471 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 472 | psa_cipher_operation_t operation; |
| 473 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 474 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 475 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 476 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 477 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 478 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 479 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 480 | |
| 481 | memset( iv, 0x2a, sizeof( iv ) ); |
| 482 | |
| 483 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 484 | |
| 485 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 486 | key->x, (size_t) key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 487 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame^] | 488 | TEST_ASSERT( psa_decrypt_setup( &operation, |
| 489 | key_slot, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 490 | |
| 491 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 492 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
| 493 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 494 | output_buffer_size = (size_t) input->len + operation.block_size; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 495 | output = mbedtls_calloc( 1, output_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 496 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 497 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 498 | TEST_ASSERT( (unsigned int) first_part_size < (size_t) input->len ); |
| 499 | TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 500 | output, output_buffer_size, |
| 501 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 502 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 503 | TEST_ASSERT( psa_cipher_update( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 504 | input->x + first_part_size, |
| 505 | (size_t) input->len - first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 506 | output, output_buffer_size, |
| 507 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 508 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 509 | TEST_ASSERT( psa_cipher_finish( &operation, |
| 510 | output + function_output_length, |
| 511 | output_buffer_size, |
| 512 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 513 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 514 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 515 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 516 | TEST_ASSERT( total_output_length == (size_t) expected_output->len ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 517 | TEST_ASSERT( memcmp( expected_output->x, output, |
| 518 | (size_t) expected_output->len ) == 0 ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 519 | |
| 520 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 521 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 522 | psa_destroy_key( key_slot ); |
| 523 | mbedtls_psa_crypto_free( ); |
| 524 | } |
| 525 | /* END_CASE */ |
| 526 | |
| 527 | |
| 528 | /* BEGIN_CASE */ |
| 529 | void cipher_decrypt( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 530 | data_t *key, |
| 531 | data_t *input, data_t *expected_output, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 532 | int expected_status ) |
| 533 | { |
| 534 | int key_slot = 1; |
| 535 | psa_status_t status; |
| 536 | psa_key_type_t key_type = key_type_arg; |
| 537 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 538 | unsigned char iv[16] = {0}; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 539 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 540 | size_t output_buffer_size = 0; |
| 541 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 542 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 543 | psa_cipher_operation_t operation; |
| 544 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 545 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 546 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 547 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 548 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 549 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 550 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 551 | |
| 552 | memset( iv, 0x2a, sizeof( iv ) ); |
| 553 | |
| 554 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 555 | |
| 556 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 557 | key->x, (size_t) key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 558 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame^] | 559 | TEST_ASSERT( psa_decrypt_setup( &operation, |
| 560 | key_slot, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 561 | |
| 562 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 563 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
| 564 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 565 | output_buffer_size = (size_t) input->len + operation.block_size; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 566 | output = mbedtls_calloc( 1, output_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 567 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 568 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 569 | TEST_ASSERT( psa_cipher_update( &operation, input->x, (size_t) input->len, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 570 | output, output_buffer_size, |
| 571 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 572 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 573 | status = psa_cipher_finish( &operation, |
| 574 | output + function_output_length, |
| 575 | output_buffer_size, |
| 576 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 577 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 578 | TEST_ASSERT( status == (psa_status_t) expected_status ); |
| 579 | |
| 580 | if( expected_status == PSA_SUCCESS ) |
| 581 | { |
| 582 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 583 | TEST_ASSERT( total_output_length == (size_t) expected_output->len ); |
| 584 | TEST_ASSERT( memcmp( expected_output->x, output, |
| 585 | (size_t) expected_output->len ) == 0 ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | |
| 589 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 590 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 591 | psa_destroy_key( key_slot ); |
| 592 | mbedtls_psa_crypto_free( ); |
| 593 | } |
| 594 | /* END_CASE */ |
| 595 | |
| 596 | |
| 597 | /* BEGIN_CASE */ |
| 598 | void cipher_verify_output( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 599 | data_t *key, |
| 600 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 601 | { |
| 602 | int key_slot = 1; |
| 603 | psa_key_type_t key_type = key_type_arg; |
| 604 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 605 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 606 | size_t iv_size = 16; |
| 607 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 608 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 609 | size_t output1_size = 0; |
| 610 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 611 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 612 | size_t output2_size = 0; |
| 613 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 614 | size_t function_output_length = 0; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 615 | psa_cipher_operation_t operation1; |
| 616 | psa_cipher_operation_t operation2; |
| 617 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 618 | TEST_ASSERT( key != NULL ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 619 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 620 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 621 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 622 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 623 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 624 | |
| 625 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 626 | key->x, (size_t) key->len ) == PSA_SUCCESS ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 627 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame^] | 628 | TEST_ASSERT( psa_encrypt_setup( &operation1, |
| 629 | key_slot, alg ) == PSA_SUCCESS ); |
| 630 | TEST_ASSERT( psa_decrypt_setup( &operation2, |
| 631 | key_slot, alg ) == PSA_SUCCESS ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 632 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 633 | TEST_ASSERT( psa_encrypt_generate_iv( &operation1, |
| 634 | iv, iv_size, |
| 635 | &iv_length ) == PSA_SUCCESS ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 636 | output1_size = (size_t) input->len + operation1.block_size; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 637 | output1 = mbedtls_calloc( 1, output1_size ); |
| 638 | TEST_ASSERT( output1 != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 639 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 640 | TEST_ASSERT( psa_cipher_update( &operation1, input->x, (size_t) input->len, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 641 | output1, output1_size, |
| 642 | &output1_length ) == PSA_SUCCESS ); |
| 643 | TEST_ASSERT( psa_cipher_finish( &operation1, |
| 644 | output1 + output1_length, output1_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 645 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 646 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 647 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 648 | |
| 649 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 650 | |
| 651 | output2_size = output1_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 652 | output2 = mbedtls_calloc( 1, output2_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 653 | TEST_ASSERT( output2 != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 654 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 655 | TEST_ASSERT( psa_encrypt_set_iv( &operation2, |
| 656 | iv, iv_length ) == PSA_SUCCESS ); |
| 657 | TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 658 | output2, output2_size, |
| 659 | &output2_length ) == PSA_SUCCESS ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 660 | function_output_length = 0; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 661 | TEST_ASSERT( psa_cipher_finish( &operation2, |
| 662 | output2 + output2_length, |
| 663 | output2_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 664 | &function_output_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 665 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 666 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 667 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 668 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 669 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 670 | TEST_ASSERT( (size_t) input->len == output2_length ); |
| 671 | TEST_ASSERT( memcmp( input->x, output2, (size_t) input->len ) == 0 ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 672 | |
| 673 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 674 | mbedtls_free( output1 ); |
| 675 | mbedtls_free( output2 ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 676 | psa_destroy_key( key_slot ); |
| 677 | mbedtls_psa_crypto_free( ); |
| 678 | } |
| 679 | /* END_CASE */ |
| 680 | |
| 681 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 682 | void cipher_verify_output_multipart( int alg_arg, |
| 683 | int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 684 | data_t *key, |
| 685 | data_t *input, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 686 | int first_part_size ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 687 | { |
| 688 | int key_slot = 1; |
| 689 | psa_key_type_t key_type = key_type_arg; |
| 690 | psa_algorithm_t alg = alg_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 691 | unsigned char iv[16] = {0}; |
| 692 | size_t iv_size = 16; |
| 693 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 694 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 695 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 696 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 697 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 698 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 699 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 700 | size_t function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 701 | psa_cipher_operation_t operation1; |
| 702 | psa_cipher_operation_t operation2; |
| 703 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 704 | TEST_ASSERT( key != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 705 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 706 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 707 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 708 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 709 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 710 | |
| 711 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 712 | key->x, (size_t) key->len ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 713 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame^] | 714 | TEST_ASSERT( psa_encrypt_setup( &operation1, |
| 715 | key_slot, alg ) == PSA_SUCCESS ); |
| 716 | TEST_ASSERT( psa_decrypt_setup( &operation2, |
| 717 | key_slot, alg ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 718 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 719 | TEST_ASSERT( psa_encrypt_generate_iv( &operation1, |
| 720 | iv, iv_size, |
| 721 | &iv_length ) == PSA_SUCCESS ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 722 | output1_buffer_size = (size_t) input->len + operation1.block_size; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 723 | output1 = mbedtls_calloc( 1, output1_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 724 | TEST_ASSERT( output1 != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 725 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 726 | TEST_ASSERT( (unsigned int) first_part_size < (size_t) input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 727 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 728 | TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 729 | output1, output1_buffer_size, |
| 730 | &function_output_length ) == PSA_SUCCESS ); |
| 731 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 732 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 733 | TEST_ASSERT( psa_cipher_update( &operation1, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 734 | input->x + first_part_size, |
| 735 | (size_t) input->len - first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 736 | output1, output1_buffer_size, |
| 737 | &function_output_length ) == PSA_SUCCESS ); |
| 738 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 739 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 740 | TEST_ASSERT( psa_cipher_finish( &operation1, |
| 741 | output1 + output1_length, |
| 742 | output1_buffer_size - output1_length, |
| 743 | &function_output_length ) == PSA_SUCCESS ); |
| 744 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 745 | |
| 746 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 747 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 748 | output2_buffer_size = output1_length; |
| 749 | output2 = mbedtls_calloc( 1, output2_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 750 | TEST_ASSERT( output2 != NULL ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 751 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 752 | TEST_ASSERT( psa_encrypt_set_iv( &operation2, |
| 753 | iv, iv_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 754 | |
| 755 | TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 756 | output2, output2_buffer_size, |
| 757 | &function_output_length ) == PSA_SUCCESS ); |
| 758 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 759 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 760 | TEST_ASSERT( psa_cipher_update( &operation2, |
| 761 | output1 + first_part_size, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 762 | output1_length - first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 763 | output2, output2_buffer_size, |
| 764 | &function_output_length ) == PSA_SUCCESS ); |
| 765 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 766 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 767 | TEST_ASSERT( psa_cipher_finish( &operation2, |
| 768 | output2 + output2_length, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 769 | output2_buffer_size - output2_length, |
| 770 | &function_output_length ) == PSA_SUCCESS ); |
| 771 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 772 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 773 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 774 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 775 | TEST_ASSERT( (size_t) input->len == output2_length ); |
| 776 | TEST_ASSERT( memcmp( input->x, output2, (size_t) input->len ) == 0 ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 777 | |
| 778 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 779 | mbedtls_free( output1 ); |
| 780 | mbedtls_free( output2 ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 781 | psa_destroy_key( key_slot ); |
| 782 | mbedtls_psa_crypto_free( ); |
| 783 | } |
| 784 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 785 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 786 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame^] | 787 | void aead_encrypt_decrypt( int key_type_arg, |
| 788 | data_t * key_data, |
| 789 | int alg_arg, |
| 790 | data_t * input_data, |
| 791 | data_t * nonce, |
| 792 | data_t * additional_data, |
| 793 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 794 | { |
| 795 | int slot = 1; |
| 796 | psa_key_type_t key_type = key_type_arg; |
| 797 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 798 | unsigned char *output_data = NULL; |
| 799 | size_t output_size = 0; |
| 800 | size_t output_length = 0; |
| 801 | unsigned char *output_data2 = NULL; |
| 802 | size_t output_length2 = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 803 | size_t tag_length = 16; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 804 | psa_status_t expected_result = (psa_status_t) expected_result_arg; |
| 805 | psa_key_policy_t policy = {0}; |
| 806 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 807 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 808 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 809 | TEST_ASSERT( nonce != NULL ); |
| 810 | TEST_ASSERT( additional_data != NULL ); |
| 811 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 812 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 813 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 814 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 815 | |
| 816 | output_size = (size_t) input_data->len + tag_length; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 817 | output_data = mbedtls_calloc( 1, output_size ); |
| 818 | TEST_ASSERT( output_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 819 | |
| 820 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 821 | |
| 822 | psa_key_policy_init( &policy ); |
| 823 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame^] | 824 | psa_key_policy_set_usage( &policy, |
| 825 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, |
| 826 | alg ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 827 | |
| 828 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 829 | |
| 830 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 831 | key_data->x, (size_t) key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 832 | |
| 833 | TEST_ASSERT( psa_aead_encrypt( slot, alg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 834 | nonce->x, (size_t) nonce->len, |
| 835 | additional_data->x, |
| 836 | (size_t) additional_data->len, |
| 837 | input_data->x, (size_t) input_data->len, |
| 838 | output_data, |
| 839 | output_size, &output_length ) == |
| 840 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 841 | |
| 842 | if( PSA_SUCCESS == expected_result ) |
| 843 | { |
| 844 | output_data2 = mbedtls_calloc( 1, output_length ); |
| 845 | TEST_ASSERT( output_data2 != NULL ); |
| 846 | |
| 847 | TEST_ASSERT( psa_aead_decrypt( slot, alg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 848 | nonce->x, (size_t) nonce->len, |
| 849 | additional_data->x, |
| 850 | (size_t) additional_data->len, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame^] | 851 | output_data, output_length, |
| 852 | output_data2, output_length, |
| 853 | &output_length2 ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 854 | expected_result ); |
| 855 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 856 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 857 | TEST_ASSERT( memcmp( input_data->x, output_data2, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 858 | (size_t) input_data->len ) == 0 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 859 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 860 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 861 | |
| 862 | exit: |
| 863 | psa_destroy_key( slot ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 864 | mbedtls_free( output_data ); |
| 865 | mbedtls_free( output_data2 ); |
| 866 | mbedtls_psa_crypto_free( ); |
| 867 | } |
| 868 | /* END_CASE */ |
| 869 | |
| 870 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 871 | void aead_encrypt( int key_type_arg, data_t * key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 872 | int alg_arg, data_t * input_data, |
| 873 | data_t * additional_data, data_t * nonce, |
| 874 | data_t * expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 875 | { |
| 876 | int slot = 1; |
| 877 | psa_key_type_t key_type = key_type_arg; |
| 878 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 879 | unsigned char *output_data = NULL; |
| 880 | size_t output_size = 0; |
| 881 | size_t output_length = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 882 | size_t tag_length = 16; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 883 | psa_key_policy_t policy = {0}; |
| 884 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 885 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 886 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 887 | TEST_ASSERT( additional_data != NULL ); |
| 888 | TEST_ASSERT( nonce != NULL ); |
| 889 | TEST_ASSERT( expected_result != NULL ); |
| 890 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 891 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 892 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 893 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 894 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) ); |
| 895 | |
| 896 | output_size = (size_t) input_data->len + tag_length; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 897 | output_data = mbedtls_calloc( 1, output_size ); |
| 898 | TEST_ASSERT( output_data != NULL ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 899 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 900 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 901 | |
| 902 | psa_key_policy_init( &policy ); |
| 903 | |
| 904 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg ); |
| 905 | |
| 906 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 907 | |
| 908 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 909 | key_data->x, (size_t) key_data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 910 | PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 911 | |
| 912 | TEST_ASSERT( psa_aead_encrypt( slot, alg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 913 | nonce->x, (size_t) nonce->len, |
| 914 | additional_data->x, |
| 915 | (size_t) additional_data->len, |
| 916 | input_data->x, (size_t) input_data->len, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame^] | 917 | output_data, output_size, |
| 918 | &output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 919 | |
| 920 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 921 | TEST_ASSERT( memcmp( output_data, expected_result->x, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 922 | output_length ) == 0 ); |
| 923 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 924 | |
| 925 | exit: |
| 926 | psa_destroy_key( slot ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 927 | mbedtls_free( output_data ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 928 | mbedtls_psa_crypto_free( ); |
| 929 | } |
| 930 | /* END_CASE */ |
| 931 | |
| 932 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 933 | void aead_decrypt( int key_type_arg, data_t * key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 934 | int alg_arg, data_t * input_data, |
| 935 | data_t * additional_data, data_t * nonce, |
| 936 | data_t * expected_data, int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 937 | { |
| 938 | int slot = 1; |
| 939 | psa_key_type_t key_type = key_type_arg; |
| 940 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 941 | unsigned char *output_data = NULL; |
| 942 | size_t output_size = 0; |
| 943 | size_t output_length = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 944 | size_t tag_length = 16; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 945 | psa_key_policy_t policy = {0}; |
| 946 | psa_status_t expected_result = (psa_status_t) expected_result_arg; |
| 947 | |
| 948 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 949 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 950 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 951 | TEST_ASSERT( additional_data != NULL ); |
| 952 | TEST_ASSERT( nonce != NULL ); |
| 953 | TEST_ASSERT( expected_data != NULL ); |
| 954 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 955 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 956 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 957 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 958 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) ); |
| 959 | |
| 960 | output_size = (size_t) input_data->len + tag_length; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 961 | output_data = mbedtls_calloc( 1, output_size ); |
| 962 | TEST_ASSERT( output_data != NULL ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 963 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 964 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 965 | |
| 966 | psa_key_policy_init( &policy ); |
| 967 | |
| 968 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg ); |
| 969 | |
| 970 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 971 | |
| 972 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 973 | key_data->x, (size_t) key_data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 974 | PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 975 | |
| 976 | TEST_ASSERT( psa_aead_decrypt( slot, alg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 977 | nonce->x, (size_t) nonce->len, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame^] | 978 | additional_data->x, |
| 979 | (size_t) additional_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 980 | input_data->x, (size_t) input_data->len, |
| 981 | output_data, |
| 982 | output_size, &output_length ) == |
| 983 | expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 984 | |
| 985 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 986 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 987 | { |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 988 | TEST_ASSERT( memcmp( output_data, expected_data->x, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 989 | output_length ) == 0 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 990 | } |
| 991 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 992 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 993 | |
| 994 | exit: |
| 995 | psa_destroy_key( slot ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 996 | mbedtls_free( output_data ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 997 | mbedtls_psa_crypto_free( ); |
| 998 | } |
| 999 | /* END_CASE */ |
| 1000 | |
| 1001 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame^] | 1002 | void signature_size( int type_arg, |
| 1003 | int bits, |
| 1004 | int alg_arg, |
| 1005 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1006 | { |
| 1007 | psa_key_type_t type = type_arg; |
| 1008 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1009 | size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1010 | TEST_ASSERT( actual_size == (size_t) expected_size_arg ); |
| 1011 | exit: |
| 1012 | ; |
| 1013 | } |
| 1014 | /* END_CASE */ |
| 1015 | |
| 1016 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1017 | void sign_deterministic( int key_type_arg, data_t *key_data, |
| 1018 | int alg_arg, data_t *input_data, |
| 1019 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1020 | { |
| 1021 | int slot = 1; |
| 1022 | psa_key_type_t key_type = key_type_arg; |
| 1023 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1024 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1025 | unsigned char *signature = NULL; |
| 1026 | size_t signature_size; |
| 1027 | size_t signature_length = 0xdeadbeef; |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1028 | psa_key_policy_t policy = {0}; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1029 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1030 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1031 | TEST_ASSERT( input_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1032 | TEST_ASSERT( output_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1033 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1034 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1035 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1036 | |
| 1037 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1038 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1039 | psa_key_policy_init( &policy ); |
| 1040 | |
| 1041 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg ); |
| 1042 | |
| 1043 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1044 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1045 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1046 | key_data->x, (size_t) key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1047 | TEST_ASSERT( psa_get_key_information( slot, |
| 1048 | NULL, |
| 1049 | &key_bits ) == PSA_SUCCESS ); |
| 1050 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame^] | 1051 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, |
| 1052 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1053 | TEST_ASSERT( signature_size != 0 ); |
| 1054 | signature = mbedtls_calloc( 1, signature_size ); |
| 1055 | TEST_ASSERT( signature != NULL ); |
| 1056 | |
| 1057 | TEST_ASSERT( psa_asymmetric_sign( slot, alg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1058 | input_data->x, (size_t) input_data->len, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1059 | NULL, 0, |
| 1060 | signature, signature_size, |
| 1061 | &signature_length ) == PSA_SUCCESS ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1062 | TEST_ASSERT( signature_length == (size_t) output_data->len ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1063 | TEST_ASSERT( memcmp( signature, output_data->x, |
| 1064 | (size_t) output_data->len ) == 0 ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1065 | |
| 1066 | exit: |
| 1067 | psa_destroy_key( slot ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 1068 | mbedtls_free( signature ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1069 | mbedtls_psa_crypto_free( ); |
| 1070 | } |
| 1071 | /* END_CASE */ |
| 1072 | |
| 1073 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1074 | void sign_fail( int key_type_arg, data_t *key_data, |
| 1075 | int alg_arg, data_t *input_data, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1076 | int signature_size, int expected_status_arg ) |
| 1077 | { |
| 1078 | int slot = 1; |
| 1079 | psa_key_type_t key_type = key_type_arg; |
| 1080 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1081 | psa_status_t actual_status; |
| 1082 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 1083 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 1084 | size_t signature_length = 0xdeadbeef; |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1085 | psa_key_policy_t policy = {0}; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1086 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1087 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1088 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1089 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1090 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1091 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1092 | signature = mbedtls_calloc( 1, signature_size ); |
| 1093 | TEST_ASSERT( signature != NULL ); |
| 1094 | |
| 1095 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1096 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1097 | psa_key_policy_init( &policy ); |
| 1098 | |
| 1099 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg_arg ); |
| 1100 | |
| 1101 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1102 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1103 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1104 | key_data->x, (size_t) key_data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1105 | PSA_SUCCESS ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1106 | |
| 1107 | actual_status = psa_asymmetric_sign( slot, alg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1108 | input_data->x, |
| 1109 | (size_t) input_data->len, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1110 | NULL, 0, |
| 1111 | signature, signature_size, |
| 1112 | &signature_length ); |
| 1113 | TEST_ASSERT( actual_status == expected_status ); |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 1114 | TEST_ASSERT( signature_length == 0 ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1115 | |
| 1116 | exit: |
| 1117 | psa_destroy_key( slot ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1118 | mbedtls_free( signature ); |
| 1119 | mbedtls_psa_crypto_free( ); |
| 1120 | } |
| 1121 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1122 | |
| 1123 | /* BEGIN_CASE */ |
| 1124 | void key_policy( int usage_arg, int alg_arg ) |
| 1125 | { |
| 1126 | int key_slot = 1; |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 1127 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1128 | unsigned char key[32] = {0}; |
| 1129 | psa_key_policy_t policy_set = {0}; |
| 1130 | psa_key_policy_t policy_get = {0}; |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1131 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1132 | memset( key, 0x2a, sizeof( key ) ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1133 | |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1134 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1135 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1136 | psa_key_policy_init( &policy_set ); |
| 1137 | psa_key_policy_init( &policy_get ); |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1138 | |
| 1139 | psa_key_policy_set_usage( &policy_set, usage_arg, alg_arg ); |
| 1140 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame^] | 1141 | TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == |
| 1142 | (psa_key_usage_t) usage_arg ); |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1143 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame^] | 1144 | TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == |
| 1145 | (psa_algorithm_t) alg_arg ); |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1146 | |
| 1147 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS ); |
| 1148 | |
| 1149 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 1150 | key, sizeof( key ) ) == PSA_SUCCESS ); |
| 1151 | |
| 1152 | TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS ); |
| 1153 | |
| 1154 | TEST_ASSERT( policy_get.usage == policy_set.usage ); |
| 1155 | TEST_ASSERT( policy_get.alg == policy_set.alg ); |
| 1156 | |
| 1157 | exit: |
| 1158 | psa_destroy_key( key_slot ); |
| 1159 | mbedtls_psa_crypto_free( ); |
| 1160 | } |
| 1161 | /* END_CASE */ |
| 1162 | |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 1163 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1164 | void key_policy_fail( int usage_arg, int alg_arg, int expected_status, |
| 1165 | data_t *keypair ) |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 1166 | { |
| 1167 | int key_slot = 1; |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 1168 | size_t signature_length = 0; |
| 1169 | psa_key_policy_t policy = {0}; |
| 1170 | int actual_status = PSA_SUCCESS; |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1171 | |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 1172 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1173 | |
| 1174 | psa_key_policy_init( &policy ); |
| 1175 | |
| 1176 | psa_key_policy_set_usage( &policy, usage_arg, alg_arg ); |
| 1177 | |
| 1178 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 1179 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1180 | if( usage_arg & PSA_KEY_USAGE_EXPORT ) |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 1181 | { |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1182 | TEST_ASSERT( keypair != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1183 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1184 | TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1185 | keypair->x, (size_t) keypair->len ) == |
| 1186 | PSA_SUCCESS ); |
| 1187 | actual_status = psa_asymmetric_sign( key_slot, |
| 1188 | (psa_algorithm_t) alg_arg, |
| 1189 | NULL, 0, |
| 1190 | NULL, 0, |
| 1191 | NULL, 0, &signature_length ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1192 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1193 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1194 | if( usage_arg & PSA_KEY_USAGE_SIGN ) |
| 1195 | { |
mohammad1603 | d926b88 | 2018-04-16 01:53:20 -0700 | [diff] [blame] | 1196 | TEST_ASSERT( keypair != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1197 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1198 | TEST_ASSERT( psa_import_key( key_slot, PSA_KEY_TYPE_RSA_KEYPAIR, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1199 | keypair->x, (size_t) keypair->len ) == |
| 1200 | PSA_SUCCESS ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 1201 | actual_status = psa_export_key( key_slot, NULL, 0, NULL ); |
mohammad1603 | 4eed757 | 2018-03-28 05:14:59 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | TEST_ASSERT( actual_status == expected_status ); |
| 1205 | |
| 1206 | exit: |
| 1207 | psa_destroy_key( key_slot ); |
| 1208 | mbedtls_psa_crypto_free( ); |
| 1209 | } |
| 1210 | /* END_CASE */ |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 1211 | |
| 1212 | /* BEGIN_CASE */ |
| 1213 | void key_lifetime( int lifetime_arg ) |
| 1214 | { |
| 1215 | int key_slot = 1; |
| 1216 | psa_key_type_t key_type = PSA_ALG_CBC_BASE; |
| 1217 | unsigned char key[32] = {0}; |
| 1218 | psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg; |
| 1219 | psa_key_lifetime_t lifetime_get; |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 1220 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 1221 | memset( key, 0x2a, sizeof( key ) ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1222 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 1223 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 1224 | |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1225 | TEST_ASSERT( psa_set_key_lifetime( key_slot, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1226 | lifetime_set ) == PSA_SUCCESS ); |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 1227 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 1228 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 1229 | key, sizeof( key ) ) == PSA_SUCCESS ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1230 | |
| 1231 | TEST_ASSERT( psa_get_key_lifetime( key_slot, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1232 | &lifetime_get ) == PSA_SUCCESS ); |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 1233 | |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1234 | TEST_ASSERT( lifetime_get == lifetime_set ); |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 1235 | |
Gilles Peskine | a0655c3 | 2018-04-30 17:06:50 +0200 | [diff] [blame] | 1236 | exit: |
| 1237 | psa_destroy_key( key_slot ); |
| 1238 | mbedtls_psa_crypto_free( ); |
| 1239 | } |
| 1240 | /* END_CASE */ |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1241 | |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 1242 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1243 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame^] | 1244 | void key_lifetime_set_fail( int key_slot_arg, |
| 1245 | int lifetime_arg, |
| 1246 | int expected_status_arg ) |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1247 | { |
| 1248 | int key_slot = 1; |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1249 | psa_key_lifetime_t lifetime_set = (psa_key_lifetime_t) lifetime_arg; |
| 1250 | psa_status_t actual_status; |
| 1251 | psa_status_t expected_status = expected_status_arg; |
| 1252 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1253 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1254 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1255 | actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set ); |
| 1256 | |
| 1257 | if( actual_status == PSA_SUCCESS ) |
| 1258 | actual_status = psa_set_key_lifetime( key_slot_arg, lifetime_set ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1259 | |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1260 | TEST_ASSERT( expected_status == actual_status ); |
| 1261 | |
| 1262 | exit: |
mohammad1603 | 804cd71 | 2018-03-20 22:44:08 +0200 | [diff] [blame] | 1263 | psa_destroy_key( key_slot ); |
| 1264 | mbedtls_psa_crypto_free( ); |
| 1265 | } |
| 1266 | /* END_CASE */ |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1267 | |
| 1268 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1269 | void asymmetric_verify( int key_type_arg, data_t *key_data, |
| 1270 | int alg_arg, data_t *hash_data, |
| 1271 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1272 | { |
| 1273 | int slot = 1; |
| 1274 | psa_key_type_t key_type = key_type_arg; |
| 1275 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1276 | psa_key_policy_t policy = {0}; |
| 1277 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1278 | TEST_ASSERT( key_data != NULL ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1279 | TEST_ASSERT( hash_data != NULL ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1280 | TEST_ASSERT( signature_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1281 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1282 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) ); |
| 1283 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1284 | |
| 1285 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1286 | |
| 1287 | psa_key_policy_init( &policy ); |
| 1288 | |
| 1289 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg_arg ); |
| 1290 | |
| 1291 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1292 | |
| 1293 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1294 | key_data->x, (size_t) key_data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1295 | PSA_SUCCESS ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1296 | |
| 1297 | TEST_ASSERT( psa_asymmetric_verify( slot, alg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1298 | hash_data->x, (size_t) hash_data->len, |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1299 | NULL, 0, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1300 | signature_data->x, |
| 1301 | (size_t) signature_data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1302 | PSA_SUCCESS ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1303 | exit: |
| 1304 | psa_destroy_key( slot ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1305 | mbedtls_psa_crypto_free( ); |
| 1306 | } |
| 1307 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1308 | |
| 1309 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1310 | void asymmetric_verify_fail( int key_type_arg, data_t *key_data, |
| 1311 | int alg_arg, data_t *hash_data, |
| 1312 | data_t *signature_data, |
| 1313 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1314 | { |
| 1315 | |
| 1316 | int slot = 1; |
| 1317 | psa_key_type_t key_type = key_type_arg; |
| 1318 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1319 | psa_status_t actual_status; |
| 1320 | psa_status_t expected_status = expected_status_arg; |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1321 | psa_key_policy_t policy = {0}; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1322 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1323 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1324 | TEST_ASSERT( hash_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1325 | TEST_ASSERT( signature_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1326 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1327 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) ); |
| 1328 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1329 | |
| 1330 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1331 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1332 | psa_key_policy_init( &policy ); |
| 1333 | |
| 1334 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg_arg ); |
| 1335 | |
| 1336 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1337 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1338 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1339 | key_data->x, (size_t) key_data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1340 | PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1341 | |
| 1342 | actual_status = psa_asymmetric_verify( slot, alg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1343 | hash_data->x, (size_t) hash_data->len, |
| 1344 | NULL, 0, |
| 1345 | signature_data->x, |
| 1346 | (size_t) signature_data->len ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1347 | |
| 1348 | |
| 1349 | TEST_ASSERT( actual_status == expected_status ); |
| 1350 | |
| 1351 | exit: |
| 1352 | psa_destroy_key( slot ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1353 | mbedtls_psa_crypto_free( ); |
| 1354 | } |
| 1355 | /* END_CASE */ |
| 1356 | |
| 1357 | |
| 1358 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1359 | void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data, |
| 1360 | int alg_arg, data_t *input_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1361 | { |
| 1362 | int slot = 1; |
| 1363 | psa_key_type_t key_type = key_type_arg; |
| 1364 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1365 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1366 | size_t output_size = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1367 | size_t output_length = 0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1368 | unsigned char *output2 = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1369 | size_t output2_size = 0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1370 | size_t output2_length = 0; |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1371 | psa_key_policy_t policy = {0}; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1372 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1373 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1374 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1375 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1376 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1377 | |
| 1378 | output_size = (size_t) key_data->len; |
| 1379 | output2_size = output_size; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1380 | output = mbedtls_calloc( 1, output_size ); |
| 1381 | TEST_ASSERT( output != NULL ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1382 | output2 = mbedtls_calloc( 1, output2_size ); |
| 1383 | TEST_ASSERT( output2 != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1384 | |
| 1385 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1386 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1387 | psa_key_policy_init( &policy ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame^] | 1388 | psa_key_policy_set_usage( &policy, |
| 1389 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, |
| 1390 | alg_arg ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1391 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1392 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1393 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1394 | key_data->x, (size_t) key_data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1395 | PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1396 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 1397 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 1398 | * the original plaintext because of the non-optional random |
| 1399 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1400 | TEST_ASSERT( psa_asymmetric_encrypt( slot, alg, |
| 1401 | input_data->x, |
| 1402 | (size_t) input_data->len, |
| 1403 | NULL, 0, |
| 1404 | output, |
| 1405 | output_size, |
| 1406 | &output_length ) == PSA_SUCCESS ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1407 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1408 | TEST_ASSERT( psa_asymmetric_decrypt( slot, alg, |
| 1409 | output, |
| 1410 | output_length, |
| 1411 | NULL, 0, |
| 1412 | output2, |
| 1413 | output2_size, |
| 1414 | &output2_length ) == PSA_SUCCESS ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame^] | 1415 | TEST_ASSERT( memcmp( input_data->x, output2, |
| 1416 | (size_t) input_data->len ) == 0 ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1417 | |
| 1418 | exit: |
| 1419 | psa_destroy_key( slot ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1420 | mbedtls_free( output ); |
| 1421 | mbedtls_free( output2 ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1422 | mbedtls_psa_crypto_free( ); |
| 1423 | |
| 1424 | } |
| 1425 | /* END_CASE */ |
| 1426 | |
| 1427 | |
| 1428 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1429 | void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1430 | int alg_arg, data_t *input_data, |
| 1431 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1432 | { |
| 1433 | |
| 1434 | |
| 1435 | int slot = 1; |
| 1436 | psa_key_type_t key_type = key_type_arg; |
| 1437 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1438 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1439 | size_t output_size = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1440 | size_t output_length = 0; |
| 1441 | psa_status_t actual_status; |
| 1442 | psa_status_t expected_status = expected_status_arg; |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1443 | psa_key_policy_t policy = {0}; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1444 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1445 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1446 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1447 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1448 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1449 | |
| 1450 | output_size = (size_t) key_data->len; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1451 | output = mbedtls_calloc( 1, output_size ); |
| 1452 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1453 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1454 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1455 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1456 | psa_key_policy_init( &policy ); |
| 1457 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg_arg ); |
| 1458 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1459 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1460 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1461 | key_data->x, (size_t) key_data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1462 | PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1463 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1464 | actual_status = psa_asymmetric_encrypt( slot, alg, |
| 1465 | input_data->x, |
| 1466 | (size_t) input_data->len, |
| 1467 | NULL, 0, |
| 1468 | output, |
| 1469 | output_size, |
| 1470 | &output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1471 | TEST_ASSERT( actual_status == expected_status ); |
| 1472 | |
| 1473 | exit: |
| 1474 | psa_destroy_key( slot ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1475 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1476 | mbedtls_psa_crypto_free( ); |
| 1477 | |
| 1478 | } |
| 1479 | /* END_CASE */ |
| 1480 | |
| 1481 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1482 | void asymmetric_decrypt( int key_type_arg, data_t *key_data, |
| 1483 | int alg_arg, data_t *input_data, |
| 1484 | data_t *expected_data, int expected_size ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1485 | { |
| 1486 | int slot = 1; |
| 1487 | psa_key_type_t key_type = key_type_arg; |
| 1488 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1489 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1490 | size_t output_size = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1491 | size_t output_length = 0; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1492 | psa_key_policy_t policy = {0}; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1493 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1494 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1495 | TEST_ASSERT( input_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1496 | TEST_ASSERT( expected_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1497 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1498 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1499 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) ); |
| 1500 | |
| 1501 | output_size = (size_t) key_data->len; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1502 | output = mbedtls_calloc( 1, output_size ); |
| 1503 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1504 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1505 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1506 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1507 | psa_key_policy_init( &policy ); |
| 1508 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg_arg ); |
| 1509 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1510 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1511 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1512 | key_data->x, (size_t) key_data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1513 | PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1514 | |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1515 | TEST_ASSERT( psa_asymmetric_decrypt( slot, alg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1516 | input_data->x, |
| 1517 | (size_t) input_data->len, |
| 1518 | NULL, 0, |
| 1519 | output, |
| 1520 | output_size, |
| 1521 | &output_length ) == PSA_SUCCESS ); |
| 1522 | TEST_ASSERT( ( (size_t) expected_size ) == output_length ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame^] | 1523 | TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1524 | |
| 1525 | exit: |
| 1526 | psa_destroy_key( slot ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1527 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1528 | mbedtls_psa_crypto_free( ); |
| 1529 | |
| 1530 | |
| 1531 | } |
| 1532 | /* END_CASE */ |
| 1533 | |
| 1534 | |
| 1535 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1536 | void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1537 | int alg_arg, data_t *input_data, |
| 1538 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1539 | { |
| 1540 | |
| 1541 | int slot = 1; |
| 1542 | psa_key_type_t key_type = key_type_arg; |
| 1543 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1544 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1545 | size_t output_size = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1546 | size_t output_length = 0; |
| 1547 | psa_status_t actual_status; |
| 1548 | psa_status_t expected_status = expected_status_arg; |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1549 | psa_key_policy_t policy = {0}; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1550 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1551 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1552 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1553 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1554 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1555 | |
| 1556 | output_size = (size_t) key_data->len; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1557 | output = mbedtls_calloc( 1, output_size ); |
| 1558 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1559 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1560 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1561 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1562 | psa_key_policy_init( &policy ); |
| 1563 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg_arg ); |
| 1564 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1565 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1566 | TEST_ASSERT( psa_import_key( slot, key_type, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1567 | key_data->x, (size_t) key_data->len ) == |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1568 | PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1569 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1570 | actual_status = psa_asymmetric_decrypt( slot, alg, |
| 1571 | input_data->x, |
| 1572 | (size_t) input_data->len, |
| 1573 | NULL, 0, |
| 1574 | output, |
| 1575 | output_size, |
| 1576 | &output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1577 | TEST_ASSERT( actual_status == expected_status ); |
| 1578 | |
| 1579 | exit: |
| 1580 | psa_destroy_key( slot ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1581 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1582 | mbedtls_psa_crypto_free( ); |
| 1583 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1584 | /* END_CASE */ |