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