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 | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame^] | 3 | #include "mbedtls/asn1write.h" |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 4 | #include "psa/crypto.h" |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 5 | |
| 6 | #if(UINT32_MAX > SIZE_MAX) |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7 | #define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) ( ( x ) <= SIZE_MAX ) |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 8 | #else |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 9 | #define PSA_CRYPTO_TEST_SIZE_T_RANGE( x ) 1 |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 10 | #endif |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 11 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 12 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 13 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 14 | |
Gilles Peskine | d35a1cc | 2018-06-26 21:26:10 +0200 | [diff] [blame] | 15 | /** Test if a buffer is all-bits zero. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 16 | * |
| 17 | * \param buffer Pointer to the beginning of the buffer. |
| 18 | * \param size Size of the buffer in bytes. |
| 19 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 20 | * \return 1 if the buffer is all-bits-zero. |
| 21 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 22 | */ |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 23 | static int mem_is_zero( void *buffer, size_t size ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 24 | { |
| 25 | size_t i; |
| 26 | for( i = 0; i < size; i++ ) |
| 27 | { |
| 28 | if( ( (unsigned char *) buffer )[i] != 0 ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 29 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 30 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 31 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 32 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 33 | |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 34 | static int key_type_is_raw_bytes( psa_key_type_t type ) |
| 35 | { |
| 36 | psa_key_type_t category = type & PSA_KEY_TYPE_CATEGORY_MASK; |
| 37 | return( category == PSA_KEY_TYPE_RAW_DATA || |
| 38 | category == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ); |
| 39 | } |
| 40 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame^] | 41 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 42 | static int asn1_write_10x( unsigned char **p, |
| 43 | unsigned char *start, |
| 44 | size_t bits, |
| 45 | unsigned char x ) |
| 46 | { |
| 47 | int ret; |
| 48 | int len = bits / 8 + 1; |
| 49 | if( x >= 1 << bits ) |
| 50 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 51 | if( *p < start || *p - start < (ssize_t) len ) |
| 52 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 53 | *p -= len; |
| 54 | ( *p )[len-1] = x; |
| 55 | if( bits % 8 == 0 ) |
| 56 | ( *p )[1] |= 1; |
| 57 | else |
| 58 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 59 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 60 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 61 | MBEDTLS_ASN1_INTEGER ) ); |
| 62 | return( len ); |
| 63 | } |
| 64 | |
| 65 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 66 | size_t buffer_size, |
| 67 | unsigned char **p, |
| 68 | size_t bits, |
| 69 | int keypair ) |
| 70 | { |
| 71 | size_t half_bits = ( bits + 1 ) / 2; |
| 72 | int ret; |
| 73 | int len = 0; |
| 74 | /* Construct something that looks like a DER encoding of |
| 75 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 76 | * RSAPrivateKey ::= SEQUENCE { |
| 77 | * version Version, |
| 78 | * modulus INTEGER, -- n |
| 79 | * publicExponent INTEGER, -- e |
| 80 | * privateExponent INTEGER, -- d |
| 81 | * prime1 INTEGER, -- p |
| 82 | * prime2 INTEGER, -- q |
| 83 | * exponent1 INTEGER, -- d mod (p-1) |
| 84 | * exponent2 INTEGER, -- d mod (q-1) |
| 85 | * coefficient INTEGER, -- (inverse of q) mod p |
| 86 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 87 | * } |
| 88 | * Or, for a public key, the same structure with only |
| 89 | * version, modulus and publicExponent. |
| 90 | */ |
| 91 | *p = buffer + buffer_size; |
| 92 | if( keypair ) |
| 93 | { |
| 94 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 95 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 96 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 97 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 98 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 99 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 100 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 101 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 102 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 103 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 104 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 105 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 106 | } |
| 107 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 108 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 109 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 110 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 111 | if( keypair ) |
| 112 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 113 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 114 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 115 | { |
| 116 | const unsigned char tag = |
| 117 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 118 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 119 | } |
| 120 | return( len ); |
| 121 | } |
| 122 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 123 | static int exercise_mac_key( psa_key_slot_t key, |
| 124 | psa_key_usage_t usage, |
| 125 | psa_algorithm_t alg ) |
| 126 | { |
| 127 | psa_mac_operation_t operation; |
| 128 | const unsigned char input[] = "foo"; |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 129 | unsigned char mac[PSA_MAC_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 130 | size_t mac_length = sizeof( mac ); |
| 131 | |
| 132 | if( usage & PSA_KEY_USAGE_SIGN ) |
| 133 | { |
| 134 | TEST_ASSERT( psa_mac_start( &operation, key, alg ) == PSA_SUCCESS ); |
| 135 | TEST_ASSERT( psa_mac_update( &operation, |
| 136 | input, sizeof( input ) ) == PSA_SUCCESS ); |
| 137 | TEST_ASSERT( psa_mac_finish( &operation, |
| 138 | mac, sizeof( input ), |
| 139 | &mac_length ) == PSA_SUCCESS ); |
| 140 | } |
| 141 | |
| 142 | if( usage & PSA_KEY_USAGE_VERIFY ) |
| 143 | { |
| 144 | psa_status_t verify_status = |
| 145 | ( usage & PSA_KEY_USAGE_SIGN ? |
| 146 | PSA_SUCCESS : |
| 147 | PSA_ERROR_INVALID_SIGNATURE ); |
| 148 | TEST_ASSERT( psa_mac_start( &operation, key, alg ) == PSA_SUCCESS ); |
| 149 | TEST_ASSERT( psa_mac_update( &operation, |
| 150 | input, sizeof( input ) ) == PSA_SUCCESS ); |
| 151 | TEST_ASSERT( psa_mac_verify( &operation, mac, mac_length ) == verify_status ); |
| 152 | } |
| 153 | |
| 154 | return( 1 ); |
| 155 | |
| 156 | exit: |
| 157 | psa_mac_abort( &operation ); |
| 158 | return( 0 ); |
| 159 | } |
| 160 | |
| 161 | static int exercise_cipher_key( psa_key_slot_t key, |
| 162 | psa_key_usage_t usage, |
| 163 | psa_algorithm_t alg ) |
| 164 | { |
| 165 | psa_cipher_operation_t operation; |
| 166 | unsigned char iv[16] = {0}; |
| 167 | size_t iv_length = sizeof( iv ); |
| 168 | const unsigned char plaintext[16] = "Hello, world..."; |
| 169 | unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)"; |
| 170 | size_t ciphertext_length = sizeof( ciphertext ); |
| 171 | unsigned char decrypted[sizeof( ciphertext )]; |
| 172 | size_t part_length; |
| 173 | |
| 174 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 175 | { |
| 176 | TEST_ASSERT( psa_encrypt_setup( &operation, key, alg ) == PSA_SUCCESS ); |
| 177 | TEST_ASSERT( psa_encrypt_generate_iv( &operation, |
| 178 | iv, sizeof( iv ), |
| 179 | &iv_length ) == PSA_SUCCESS ); |
| 180 | TEST_ASSERT( psa_cipher_update( &operation, |
| 181 | plaintext, sizeof( plaintext ), |
| 182 | ciphertext, sizeof( ciphertext ), |
| 183 | &ciphertext_length ) == PSA_SUCCESS ); |
| 184 | TEST_ASSERT( psa_cipher_finish( &operation, |
| 185 | ciphertext + ciphertext_length, |
| 186 | sizeof( ciphertext ) - ciphertext_length, |
| 187 | &part_length ) == PSA_SUCCESS ); |
| 188 | ciphertext_length += part_length; |
| 189 | } |
| 190 | |
| 191 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 192 | { |
| 193 | psa_status_t status; |
Mohammad AboMokh | adb9b23 | 2018-06-28 01:52:54 -0700 | [diff] [blame] | 194 | psa_key_type_t type = PSA_KEY_TYPE_NONE; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 195 | if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) ) |
| 196 | { |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 197 | size_t bits; |
| 198 | TEST_ASSERT( psa_get_key_information( key, &type, &bits ) ); |
| 199 | iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type ); |
| 200 | } |
| 201 | TEST_ASSERT( psa_decrypt_setup( &operation, key, alg ) == PSA_SUCCESS ); |
| 202 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 203 | iv, iv_length ) == PSA_SUCCESS ); |
| 204 | TEST_ASSERT( psa_cipher_update( &operation, |
| 205 | ciphertext, ciphertext_length, |
| 206 | decrypted, sizeof( decrypted ), |
| 207 | &part_length ) == PSA_SUCCESS ); |
| 208 | status = psa_cipher_finish( &operation, |
| 209 | decrypted + part_length, |
| 210 | sizeof( decrypted ) - part_length, |
| 211 | &part_length ); |
| 212 | /* For a stream cipher, all inputs are valid. For a block cipher, |
| 213 | * if the input is some aribtrary data rather than an actual |
| 214 | ciphertext, a padding error is likely. */ |
Mohammad AboMokh | 65fa0b8 | 2018-06-28 02:14:00 -0700 | [diff] [blame] | 215 | if( ( usage & PSA_KEY_USAGE_ENCRYPT ) || |
Mohammad AboMokh | adb9b23 | 2018-06-28 01:52:54 -0700 | [diff] [blame] | 216 | PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 ) |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 217 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 218 | else |
| 219 | TEST_ASSERT( status == PSA_SUCCESS || |
| 220 | status == PSA_ERROR_INVALID_PADDING ); |
| 221 | } |
| 222 | |
| 223 | return( 1 ); |
| 224 | |
| 225 | exit: |
| 226 | psa_cipher_abort( &operation ); |
| 227 | return( 0 ); |
| 228 | } |
| 229 | |
| 230 | static int exercise_aead_key( psa_key_slot_t key, |
| 231 | psa_key_usage_t usage, |
| 232 | psa_algorithm_t alg ) |
| 233 | { |
| 234 | unsigned char nonce[16] = {0}; |
| 235 | size_t nonce_length = sizeof( nonce ); |
| 236 | unsigned char plaintext[16] = "Hello, world..."; |
| 237 | unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)"; |
| 238 | size_t ciphertext_length = sizeof( ciphertext ); |
| 239 | size_t plaintext_length = sizeof( ciphertext ); |
| 240 | |
| 241 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 242 | { |
| 243 | TEST_ASSERT( psa_aead_encrypt( key, alg, |
| 244 | nonce, nonce_length, |
| 245 | NULL, 0, |
| 246 | plaintext, sizeof( plaintext ), |
| 247 | ciphertext, sizeof( ciphertext ), |
| 248 | &ciphertext_length ) == PSA_SUCCESS ); |
| 249 | } |
| 250 | |
| 251 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 252 | { |
| 253 | psa_status_t verify_status = |
| 254 | ( usage & PSA_KEY_USAGE_ENCRYPT ? |
| 255 | PSA_SUCCESS : |
| 256 | PSA_ERROR_INVALID_SIGNATURE ); |
| 257 | TEST_ASSERT( psa_aead_decrypt( key, alg, |
| 258 | nonce, nonce_length, |
| 259 | NULL, 0, |
| 260 | ciphertext, ciphertext_length, |
| 261 | plaintext, sizeof( plaintext ), |
| 262 | &plaintext_length ) == verify_status ); |
| 263 | } |
| 264 | |
| 265 | return( 1 ); |
| 266 | |
| 267 | exit: |
| 268 | return( 0 ); |
| 269 | } |
| 270 | |
| 271 | static int exercise_signature_key( psa_key_slot_t key, |
| 272 | psa_key_usage_t usage, |
| 273 | psa_algorithm_t alg ) |
| 274 | { |
Gilles Peskine | ca45c35 | 2018-06-26 16:13:09 +0200 | [diff] [blame] | 275 | unsigned char payload[16] = {1}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 276 | size_t payload_length = sizeof( payload ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 277 | unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 278 | size_t signature_length = sizeof( signature ); |
| 279 | |
| 280 | if( usage & PSA_KEY_USAGE_SIGN ) |
| 281 | { |
| 282 | TEST_ASSERT( psa_asymmetric_sign( key, alg, |
| 283 | payload, payload_length, |
| 284 | NULL, 0, |
| 285 | signature, sizeof( signature ), |
| 286 | &signature_length ) == PSA_SUCCESS ); |
| 287 | } |
| 288 | |
| 289 | if( usage & PSA_KEY_USAGE_VERIFY ) |
| 290 | { |
| 291 | psa_status_t verify_status = |
| 292 | ( usage & PSA_KEY_USAGE_SIGN ? |
| 293 | PSA_SUCCESS : |
| 294 | PSA_ERROR_INVALID_SIGNATURE ); |
| 295 | TEST_ASSERT( psa_asymmetric_verify( key, alg, |
| 296 | payload, payload_length, |
| 297 | NULL, 0, |
| 298 | signature, signature_length ) == |
| 299 | verify_status ); |
| 300 | } |
| 301 | |
| 302 | return( 1 ); |
| 303 | |
| 304 | exit: |
| 305 | return( 0 ); |
| 306 | } |
| 307 | |
| 308 | static int exercise_asymmetric_encryption_key( psa_key_slot_t key, |
| 309 | psa_key_usage_t usage, |
| 310 | psa_algorithm_t alg ) |
| 311 | { |
| 312 | unsigned char plaintext[256] = "Hello, world..."; |
| 313 | unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)"; |
| 314 | size_t ciphertext_length = sizeof( ciphertext ); |
| 315 | size_t plaintext_length = 16; |
| 316 | |
| 317 | if( usage & PSA_KEY_USAGE_ENCRYPT ) |
| 318 | { |
| 319 | TEST_ASSERT( |
| 320 | psa_asymmetric_encrypt( key, alg, |
| 321 | plaintext, plaintext_length, |
| 322 | NULL, 0, |
| 323 | ciphertext, sizeof( ciphertext ), |
| 324 | &ciphertext_length ) == PSA_SUCCESS ); |
| 325 | } |
| 326 | |
| 327 | if( usage & PSA_KEY_USAGE_DECRYPT ) |
| 328 | { |
| 329 | psa_status_t status = |
| 330 | psa_asymmetric_decrypt( key, alg, |
| 331 | ciphertext, ciphertext_length, |
| 332 | NULL, 0, |
| 333 | plaintext, sizeof( plaintext ), |
| 334 | &plaintext_length ); |
| 335 | TEST_ASSERT( status == PSA_SUCCESS || |
| 336 | ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 && |
| 337 | ( status == PSA_ERROR_INVALID_ARGUMENT || |
| 338 | status == PSA_ERROR_INVALID_PADDING ) ) ); |
| 339 | } |
| 340 | |
| 341 | return( 1 ); |
| 342 | |
| 343 | exit: |
| 344 | return( 0 ); |
| 345 | } |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 346 | /* END_HEADER */ |
| 347 | |
| 348 | /* BEGIN_DEPENDENCIES |
| 349 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 350 | * END_DEPENDENCIES |
| 351 | */ |
| 352 | |
| 353 | /* BEGIN_CASE */ |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 354 | void init_deinit( ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 355 | { |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 356 | psa_status_t status; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 357 | int i; |
| 358 | for( i = 0; i <= 1; i++ ) |
| 359 | { |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 360 | status = psa_crypto_init( ); |
| 361 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 362 | status = psa_crypto_init( ); |
| 363 | TEST_ASSERT( status == PSA_SUCCESS ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 364 | mbedtls_psa_crypto_free( ); |
| 365 | } |
| 366 | } |
| 367 | /* END_CASE */ |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 368 | |
| 369 | /* BEGIN_CASE */ |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 370 | void import( data_t *data, int type, int expected_status_arg ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 371 | { |
| 372 | int slot = 1; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 373 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 374 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 375 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 376 | TEST_ASSERT( data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 377 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 378 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 379 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 380 | status = psa_import_key( slot, type, data->x, data->len ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 381 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 382 | if( status == PSA_SUCCESS ) |
| 383 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 384 | |
| 385 | exit: |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 386 | mbedtls_psa_crypto_free( ); |
| 387 | } |
| 388 | /* END_CASE */ |
| 389 | |
| 390 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame^] | 391 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 392 | { |
| 393 | int slot = 1; |
| 394 | size_t bits = bits_arg; |
| 395 | psa_status_t expected_status = expected_status_arg; |
| 396 | psa_status_t status; |
| 397 | psa_key_type_t type = |
| 398 | keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY; |
| 399 | size_t buffer_size = /* Slight overapproximations */ |
| 400 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
| 401 | unsigned char *buffer = mbedtls_calloc( 1, buffer_size ); |
| 402 | unsigned char *p; |
| 403 | int ret; |
| 404 | size_t length; |
| 405 | |
| 406 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 407 | TEST_ASSERT( buffer != NULL ); |
| 408 | |
| 409 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 410 | bits, keypair ) ) >= 0 ); |
| 411 | length = ret; |
| 412 | |
| 413 | /* Try importing the key */ |
| 414 | status = psa_import_key( slot, type, p, length ); |
| 415 | TEST_ASSERT( status == expected_status ); |
| 416 | if( status == PSA_SUCCESS ) |
| 417 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 418 | |
| 419 | exit: |
| 420 | mbedtls_free( buffer ); |
| 421 | mbedtls_psa_crypto_free( ); |
| 422 | } |
| 423 | /* END_CASE */ |
| 424 | |
| 425 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 426 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 427 | int type_arg, |
| 428 | int alg_arg, |
| 429 | int usage_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 430 | int expected_bits, |
| 431 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 432 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 433 | int canonical_input ) |
| 434 | { |
| 435 | int slot = 1; |
| 436 | int slot2 = slot + 1; |
| 437 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 438 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 439 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 440 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 441 | unsigned char *exported = NULL; |
| 442 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 443 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 444 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 445 | size_t reexported_length; |
| 446 | psa_key_type_t got_type; |
| 447 | size_t got_bits; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 448 | psa_key_policy_t policy; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 449 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 450 | TEST_ASSERT( data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 451 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) ); |
| 452 | export_size = (ssize_t) data->len + export_size_delta; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 453 | exported = mbedtls_calloc( 1, export_size ); |
| 454 | TEST_ASSERT( exported != NULL ); |
| 455 | if( ! canonical_input ) |
| 456 | { |
| 457 | reexported = mbedtls_calloc( 1, export_size ); |
| 458 | TEST_ASSERT( reexported != NULL ); |
| 459 | } |
| 460 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 461 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 462 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 463 | psa_key_policy_set_usage( &policy, usage_arg, alg ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 464 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 465 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 466 | /* Import the key */ |
| 467 | TEST_ASSERT( psa_import_key( slot, type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 468 | data->x, data->len ) == PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 469 | |
| 470 | /* Test the key information */ |
| 471 | TEST_ASSERT( psa_get_key_information( slot, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 472 | &got_type, |
| 473 | &got_bits ) == PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 474 | TEST_ASSERT( got_type == type ); |
| 475 | TEST_ASSERT( got_bits == (size_t) expected_bits ); |
| 476 | |
| 477 | /* Export the key */ |
| 478 | status = psa_export_key( slot, |
| 479 | exported, export_size, |
| 480 | &exported_length ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 481 | TEST_ASSERT( status == expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 482 | |
| 483 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 484 | * and export_size. On errors, the exported length must be 0. */ |
| 485 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 486 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 487 | TEST_ASSERT( exported_length <= export_size ); |
| 488 | |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 489 | TEST_ASSERT( mem_is_zero( exported + exported_length, |
| 490 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 491 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 492 | { |
| 493 | TEST_ASSERT( exported_length == 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 494 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 495 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 496 | |
| 497 | if( canonical_input ) |
| 498 | { |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 499 | TEST_ASSERT( exported_length == data->len ); |
| 500 | TEST_ASSERT( memcmp( exported, data->x, data->len ) == 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 501 | } |
| 502 | else |
| 503 | { |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 504 | TEST_ASSERT( psa_set_key_policy( slot2, &policy ) == PSA_SUCCESS ); |
| 505 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 506 | TEST_ASSERT( psa_import_key( slot2, type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 507 | exported, |
| 508 | export_size ) == PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 509 | TEST_ASSERT( psa_export_key( slot2, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 510 | reexported, |
| 511 | export_size, |
| 512 | &reexported_length ) == PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 513 | TEST_ASSERT( reexported_length == exported_length ); |
| 514 | TEST_ASSERT( memcmp( reexported, exported, |
| 515 | exported_length ) == 0 ); |
| 516 | } |
| 517 | |
| 518 | destroy: |
| 519 | /* Destroy the key */ |
| 520 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 521 | TEST_ASSERT( psa_get_key_information( |
| 522 | slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT ); |
| 523 | |
| 524 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 525 | mbedtls_free( exported ); |
| 526 | mbedtls_free( reexported ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 527 | mbedtls_psa_crypto_free( ); |
| 528 | } |
| 529 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 530 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 531 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 532 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 533 | int type_arg, |
| 534 | int alg_arg, |
| 535 | int expected_bits, |
| 536 | int public_key_expected_length, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 537 | int expected_export_status_arg ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 538 | { |
| 539 | int slot = 1; |
| 540 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 541 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 542 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 543 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 544 | unsigned char *exported = NULL; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 545 | size_t export_size; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 546 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 547 | psa_key_type_t got_type; |
| 548 | size_t got_bits; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 549 | psa_key_policy_t policy; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 550 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 551 | TEST_ASSERT( data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 552 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) ); |
| 553 | export_size = (ssize_t) data->len; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 554 | exported = mbedtls_calloc( 1, export_size ); |
| 555 | TEST_ASSERT( exported != NULL ); |
| 556 | |
| 557 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 558 | |
| 559 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 560 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 561 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 562 | |
| 563 | /* Import the key */ |
| 564 | TEST_ASSERT( psa_import_key( slot, type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 565 | data->x, data->len ) == PSA_SUCCESS ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 566 | |
| 567 | /* Test the key information */ |
| 568 | TEST_ASSERT( psa_get_key_information( slot, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 569 | &got_type, |
| 570 | &got_bits ) == PSA_SUCCESS ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 571 | TEST_ASSERT( got_type == type ); |
| 572 | TEST_ASSERT( got_bits == (size_t) expected_bits ); |
| 573 | |
| 574 | /* Export the key */ |
| 575 | status = psa_export_public_key( slot, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 576 | exported, export_size, |
| 577 | &exported_length ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 578 | TEST_ASSERT( status == expected_export_status ); |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 579 | TEST_ASSERT( exported_length == (size_t) public_key_expected_length ); |
| 580 | TEST_ASSERT( mem_is_zero( exported + exported_length, |
| 581 | export_size - exported_length ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 582 | if( status != PSA_SUCCESS ) |
| 583 | goto destroy; |
| 584 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 585 | destroy: |
| 586 | /* Destroy the key */ |
| 587 | TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS ); |
| 588 | TEST_ASSERT( psa_get_key_information( |
| 589 | slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT ); |
| 590 | |
| 591 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 592 | mbedtls_free( exported ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 593 | mbedtls_psa_crypto_free( ); |
| 594 | } |
| 595 | /* END_CASE */ |
| 596 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 597 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 598 | void import_and_exercise_key( data_t *data, |
| 599 | int type_arg, |
| 600 | int bits_arg, |
| 601 | int alg_arg ) |
| 602 | { |
| 603 | int slot = 1; |
| 604 | psa_key_type_t type = type_arg; |
| 605 | size_t bits = bits_arg; |
| 606 | psa_algorithm_t alg = alg_arg; |
| 607 | psa_key_usage_t usage = |
| 608 | ( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ? |
| 609 | ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 610 | PSA_KEY_USAGE_VERIFY : |
| 611 | PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ) : |
| 612 | PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) || |
| 613 | PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ? |
| 614 | ( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 615 | PSA_KEY_USAGE_ENCRYPT : |
| 616 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ) : |
| 617 | 0 ); |
| 618 | psa_key_policy_t policy; |
| 619 | psa_key_type_t got_type; |
| 620 | size_t got_bits; |
| 621 | psa_status_t status; |
| 622 | |
| 623 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 624 | |
| 625 | psa_key_policy_init( &policy ); |
| 626 | psa_key_policy_set_usage( &policy, usage, alg ); |
| 627 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 628 | |
| 629 | /* Import the key */ |
| 630 | status = psa_import_key( slot, type, data->x, data->len ); |
| 631 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 632 | |
| 633 | /* Test the key information */ |
| 634 | TEST_ASSERT( psa_get_key_information( slot, |
| 635 | &got_type, |
| 636 | &got_bits ) == PSA_SUCCESS ); |
| 637 | TEST_ASSERT( got_type == type ); |
| 638 | TEST_ASSERT( got_bits == bits ); |
| 639 | |
| 640 | /* Do something with the key according to its type and permitted usage. */ |
| 641 | if( PSA_ALG_IS_MAC( alg ) ) |
| 642 | exercise_mac_key( slot, usage, alg ); |
| 643 | else if( PSA_ALG_IS_CIPHER( alg ) ) |
| 644 | exercise_cipher_key( slot, usage, alg ); |
| 645 | else if( PSA_ALG_IS_AEAD( alg ) ) |
| 646 | exercise_aead_key( slot, usage, alg ); |
| 647 | else if( PSA_ALG_IS_SIGN( alg ) ) |
| 648 | exercise_signature_key( slot, usage, alg ); |
| 649 | else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
| 650 | exercise_asymmetric_encryption_key( slot, usage, alg ); |
| 651 | |
| 652 | exit: |
| 653 | psa_destroy_key( slot ); |
| 654 | mbedtls_psa_crypto_free( ); |
| 655 | } |
| 656 | /* END_CASE */ |
| 657 | |
| 658 | /* BEGIN_CASE */ |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 659 | void key_policy( int usage_arg, int alg_arg ) |
| 660 | { |
| 661 | int key_slot = 1; |
| 662 | psa_algorithm_t alg = alg_arg; |
| 663 | psa_key_usage_t usage = usage_arg; |
| 664 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 665 | unsigned char key[32] = {0}; |
| 666 | psa_key_policy_t policy_set; |
| 667 | psa_key_policy_t policy_get; |
| 668 | |
| 669 | memset( key, 0x2a, sizeof( key ) ); |
| 670 | |
| 671 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 672 | |
| 673 | psa_key_policy_init( &policy_set ); |
| 674 | psa_key_policy_init( &policy_get ); |
| 675 | |
| 676 | psa_key_policy_set_usage( &policy_set, usage, alg ); |
| 677 | |
| 678 | TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage ); |
| 679 | TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg ); |
| 680 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy_set ) == PSA_SUCCESS ); |
| 681 | |
| 682 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 683 | key, sizeof( key ) ) == PSA_SUCCESS ); |
| 684 | |
| 685 | TEST_ASSERT( psa_get_key_policy( key_slot, &policy_get ) == PSA_SUCCESS ); |
| 686 | |
| 687 | TEST_ASSERT( policy_get.usage == policy_set.usage ); |
| 688 | TEST_ASSERT( policy_get.alg == policy_set.alg ); |
| 689 | |
| 690 | exit: |
| 691 | psa_destroy_key( key_slot ); |
| 692 | mbedtls_psa_crypto_free( ); |
| 693 | } |
| 694 | /* END_CASE */ |
| 695 | |
| 696 | /* BEGIN_CASE */ |
| 697 | void key_policy_fail( int usage_arg, int alg_arg, int expected_status, |
| 698 | data_t *keypair ) |
| 699 | { |
| 700 | int key_slot = 1; |
| 701 | psa_algorithm_t alg = alg_arg; |
| 702 | psa_key_usage_t usage = usage_arg; |
| 703 | size_t signature_length = 0; |
| 704 | psa_key_policy_t policy; |
| 705 | int actual_status = PSA_SUCCESS; |
| 706 | |
| 707 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 708 | |
| 709 | psa_key_policy_init( &policy ); |
| 710 | psa_key_policy_set_usage( &policy, usage, alg ); |
| 711 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 712 | |
| 713 | if( usage & PSA_KEY_USAGE_EXPORT ) |
| 714 | { |
| 715 | TEST_ASSERT( keypair != NULL ); |
| 716 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) ); |
| 717 | TEST_ASSERT( psa_import_key( key_slot, |
| 718 | PSA_KEY_TYPE_RSA_KEYPAIR, |
| 719 | keypair->x, |
| 720 | keypair->len ) == PSA_SUCCESS ); |
| 721 | actual_status = psa_asymmetric_sign( key_slot, alg, |
| 722 | NULL, 0, |
| 723 | NULL, 0, |
| 724 | NULL, 0, &signature_length ); |
| 725 | } |
| 726 | |
| 727 | if( usage & PSA_KEY_USAGE_SIGN ) |
| 728 | { |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 729 | size_t data_length; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 730 | TEST_ASSERT( keypair != NULL ); |
| 731 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( keypair->len ) ); |
| 732 | TEST_ASSERT( psa_import_key( key_slot, |
| 733 | PSA_KEY_TYPE_RSA_KEYPAIR, |
| 734 | keypair->x, |
| 735 | keypair->len ) == PSA_SUCCESS ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 736 | actual_status = psa_export_key( key_slot, NULL, 0, &data_length ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | TEST_ASSERT( actual_status == expected_status ); |
| 740 | |
| 741 | exit: |
| 742 | psa_destroy_key( key_slot ); |
| 743 | mbedtls_psa_crypto_free( ); |
| 744 | } |
| 745 | /* END_CASE */ |
| 746 | |
| 747 | /* BEGIN_CASE */ |
| 748 | void key_lifetime( int lifetime_arg ) |
| 749 | { |
| 750 | int key_slot = 1; |
| 751 | psa_key_type_t key_type = PSA_ALG_CBC_BASE; |
| 752 | unsigned char key[32] = {0}; |
| 753 | psa_key_lifetime_t lifetime_set = lifetime_arg; |
| 754 | psa_key_lifetime_t lifetime_get; |
| 755 | |
| 756 | memset( key, 0x2a, sizeof( key ) ); |
| 757 | |
| 758 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 759 | |
| 760 | TEST_ASSERT( psa_set_key_lifetime( key_slot, |
| 761 | lifetime_set ) == PSA_SUCCESS ); |
| 762 | |
| 763 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 764 | key, sizeof( key ) ) == PSA_SUCCESS ); |
| 765 | |
| 766 | TEST_ASSERT( psa_get_key_lifetime( key_slot, |
| 767 | &lifetime_get ) == PSA_SUCCESS ); |
| 768 | |
| 769 | TEST_ASSERT( lifetime_get == lifetime_set ); |
| 770 | |
| 771 | exit: |
| 772 | psa_destroy_key( key_slot ); |
| 773 | mbedtls_psa_crypto_free( ); |
| 774 | } |
| 775 | /* END_CASE */ |
| 776 | |
| 777 | /* BEGIN_CASE */ |
| 778 | void key_lifetime_set_fail( int key_slot_arg, |
| 779 | int lifetime_arg, |
| 780 | int expected_status_arg ) |
| 781 | { |
| 782 | psa_key_slot_t key_slot = key_slot_arg; |
| 783 | psa_key_lifetime_t lifetime_set = lifetime_arg; |
| 784 | psa_status_t actual_status; |
| 785 | psa_status_t expected_status = expected_status_arg; |
| 786 | |
| 787 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 788 | |
| 789 | actual_status = psa_set_key_lifetime( key_slot, lifetime_set ); |
| 790 | |
| 791 | if( actual_status == PSA_SUCCESS ) |
| 792 | actual_status = psa_set_key_lifetime( key_slot, lifetime_set ); |
| 793 | |
| 794 | TEST_ASSERT( expected_status == actual_status ); |
| 795 | |
| 796 | exit: |
| 797 | psa_destroy_key( key_slot ); |
| 798 | mbedtls_psa_crypto_free( ); |
| 799 | } |
| 800 | /* END_CASE */ |
| 801 | |
| 802 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 803 | void hash_setup( int alg_arg, |
| 804 | int expected_status_arg ) |
| 805 | { |
| 806 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 807 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 808 | psa_hash_operation_t operation; |
| 809 | psa_status_t status; |
| 810 | |
| 811 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 812 | |
| 813 | status = psa_hash_start( &operation, alg ); |
| 814 | psa_hash_abort( &operation ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 815 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 816 | |
| 817 | exit: |
| 818 | mbedtls_psa_crypto_free( ); |
| 819 | } |
| 820 | /* END_CASE */ |
| 821 | |
| 822 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 823 | 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] | 824 | { |
| 825 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b3e6e5d | 2018-06-18 22:16:43 +0200 | [diff] [blame] | 826 | unsigned char actual_hash[PSA_HASH_MAX_SIZE]; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 827 | size_t actual_hash_length; |
| 828 | psa_hash_operation_t operation; |
| 829 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 830 | TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) ); |
| 831 | TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE ); |
| 832 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 833 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 834 | TEST_ASSERT( expected_hash != NULL ); |
| 835 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 836 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 837 | |
| 838 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 839 | |
| 840 | TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS ); |
| 841 | TEST_ASSERT( psa_hash_update( &operation, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 842 | input->x, input->len ) == PSA_SUCCESS ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 843 | TEST_ASSERT( psa_hash_finish( &operation, |
| 844 | actual_hash, sizeof( actual_hash ), |
| 845 | &actual_hash_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 846 | TEST_ASSERT( actual_hash_length == expected_hash->len ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 847 | TEST_ASSERT( memcmp( expected_hash->x, actual_hash, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 848 | expected_hash->len ) == 0 ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 849 | |
| 850 | exit: |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 851 | mbedtls_psa_crypto_free( ); |
| 852 | } |
| 853 | /* END_CASE */ |
| 854 | |
| 855 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 856 | 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] | 857 | { |
| 858 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 859 | psa_hash_operation_t operation; |
| 860 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 861 | TEST_ASSERT( expected_hash->len == PSA_HASH_SIZE( alg ) ); |
| 862 | TEST_ASSERT( expected_hash->len <= PSA_HASH_MAX_SIZE ); |
| 863 | |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 864 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 865 | TEST_ASSERT( expected_hash != NULL ); |
| 866 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 867 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_hash->len ) ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 868 | |
| 869 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 870 | |
| 871 | TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS ); |
| 872 | TEST_ASSERT( psa_hash_update( &operation, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 873 | input->x, |
| 874 | input->len ) == PSA_SUCCESS ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 875 | TEST_ASSERT( psa_hash_verify( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 876 | expected_hash->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 877 | expected_hash->len ) == PSA_SUCCESS ); |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 878 | |
| 879 | exit: |
Gilles Peskine | 9ef733f | 2018-02-07 21:05:37 +0100 | [diff] [blame] | 880 | mbedtls_psa_crypto_free( ); |
| 881 | } |
| 882 | /* END_CASE */ |
| 883 | |
| 884 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 885 | void mac_setup( int key_type_arg, |
| 886 | data_t *key, |
| 887 | int alg_arg, |
| 888 | int expected_status_arg ) |
| 889 | { |
| 890 | int key_slot = 1; |
| 891 | psa_key_type_t key_type = key_type_arg; |
| 892 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 893 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 894 | psa_mac_operation_t operation; |
| 895 | psa_key_policy_t policy; |
| 896 | psa_status_t status; |
| 897 | |
| 898 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 899 | |
| 900 | psa_key_policy_init( &policy ); |
| 901 | psa_key_policy_set_usage( &policy, |
| 902 | PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY, |
| 903 | alg ); |
| 904 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 905 | |
| 906 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 907 | key->x, key->len ) == PSA_SUCCESS ); |
| 908 | |
| 909 | status = psa_mac_start( &operation, key_slot, alg ); |
| 910 | psa_mac_abort( &operation ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 911 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 912 | |
| 913 | exit: |
| 914 | psa_destroy_key( key_slot ); |
| 915 | mbedtls_psa_crypto_free( ); |
| 916 | } |
| 917 | /* END_CASE */ |
| 918 | |
| 919 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 920 | void mac_verify( int key_type_arg, |
| 921 | data_t *key, |
| 922 | int alg_arg, |
| 923 | data_t *input, |
| 924 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 925 | { |
| 926 | int key_slot = 1; |
| 927 | psa_key_type_t key_type = key_type_arg; |
| 928 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 929 | psa_mac_operation_t operation; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 930 | psa_key_policy_t policy; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 931 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 932 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 933 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 934 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 935 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 936 | TEST_ASSERT( expected_mac != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 937 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 938 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 939 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 940 | |
| 941 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 942 | |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 943 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 944 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 945 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 946 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 947 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 948 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 949 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 950 | TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS ); |
| 951 | TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS ); |
| 952 | TEST_ASSERT( psa_mac_update( &operation, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 953 | input->x, input->len ) == PSA_SUCCESS ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 954 | TEST_ASSERT( psa_mac_verify( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 955 | expected_mac->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 956 | expected_mac->len ) == PSA_SUCCESS ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 957 | |
| 958 | exit: |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 959 | psa_destroy_key( key_slot ); |
| 960 | mbedtls_psa_crypto_free( ); |
| 961 | } |
| 962 | /* END_CASE */ |
| 963 | |
| 964 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 965 | void cipher_setup( int key_type_arg, |
| 966 | data_t *key, |
| 967 | int alg_arg, |
| 968 | int expected_status_arg ) |
| 969 | { |
| 970 | int key_slot = 1; |
| 971 | psa_key_type_t key_type = key_type_arg; |
| 972 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 973 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 974 | psa_cipher_operation_t operation; |
| 975 | psa_key_policy_t policy; |
| 976 | psa_status_t status; |
| 977 | |
| 978 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 979 | |
| 980 | psa_key_policy_init( &policy ); |
| 981 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg ); |
| 982 | TEST_ASSERT( psa_set_key_policy( key_slot, &policy ) == PSA_SUCCESS ); |
| 983 | |
| 984 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
| 985 | key->x, key->len ) == PSA_SUCCESS ); |
| 986 | |
| 987 | status = psa_encrypt_setup( &operation, key_slot, alg ); |
| 988 | psa_cipher_abort( &operation ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 989 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 990 | |
| 991 | exit: |
| 992 | psa_destroy_key( key_slot ); |
| 993 | mbedtls_psa_crypto_free( ); |
| 994 | } |
| 995 | /* END_CASE */ |
| 996 | |
| 997 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 998 | void cipher_encrypt( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 999 | data_t *key, |
| 1000 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1001 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1002 | { |
| 1003 | int key_slot = 1; |
| 1004 | psa_status_t status; |
| 1005 | psa_key_type_t key_type = key_type_arg; |
| 1006 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1007 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1008 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1009 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1010 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1011 | size_t output_buffer_size = 0; |
| 1012 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1013 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1014 | psa_cipher_operation_t operation; |
| 1015 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1016 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1017 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1018 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1019 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 1020 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 1021 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1022 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1023 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 1024 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1025 | |
| 1026 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1027 | |
| 1028 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1029 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1030 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1031 | TEST_ASSERT( psa_encrypt_setup( &operation, |
| 1032 | key_slot, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1033 | |
| 1034 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1035 | iv, iv_size ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1036 | output_buffer_size = input->len + operation.block_size; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1037 | output = mbedtls_calloc( 1, output_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1038 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1039 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1040 | TEST_ASSERT( psa_cipher_update( &operation, |
| 1041 | input->x, input->len, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1042 | output, output_buffer_size, |
| 1043 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1044 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1045 | status = psa_cipher_finish( &operation, |
| 1046 | output + function_output_length, |
| 1047 | output_buffer_size, |
| 1048 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1049 | total_output_length += function_output_length; |
| 1050 | |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1051 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1052 | if( expected_status == PSA_SUCCESS ) |
| 1053 | { |
| 1054 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1055 | TEST_ASSERT( total_output_length == expected_output->len ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1056 | TEST_ASSERT( memcmp( expected_output->x, output, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1057 | expected_output->len ) == 0 ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1058 | } |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1059 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1060 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1061 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1062 | psa_destroy_key( key_slot ); |
| 1063 | mbedtls_psa_crypto_free( ); |
| 1064 | } |
| 1065 | /* END_CASE */ |
| 1066 | |
| 1067 | /* BEGIN_CASE */ |
| 1068 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1069 | data_t *key, |
| 1070 | data_t *input, |
| 1071 | int first_part_size, |
| 1072 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1073 | { |
| 1074 | int key_slot = 1; |
| 1075 | psa_key_type_t key_type = key_type_arg; |
| 1076 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1077 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1078 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1079 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1080 | size_t output_buffer_size = 0; |
| 1081 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1082 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1083 | psa_cipher_operation_t operation; |
| 1084 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1085 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1086 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1087 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1088 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 1089 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 1090 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1091 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1092 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 1093 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1094 | |
| 1095 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1096 | |
| 1097 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1098 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1099 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1100 | TEST_ASSERT( psa_encrypt_setup( &operation, |
| 1101 | key_slot, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1102 | |
| 1103 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 1104 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1105 | output_buffer_size = input->len + operation.block_size; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1106 | output = mbedtls_calloc( 1, output_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1107 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1108 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1109 | TEST_ASSERT( (unsigned int) first_part_size < input->len ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1110 | TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1111 | output, output_buffer_size, |
| 1112 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1113 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1114 | TEST_ASSERT( psa_cipher_update( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1115 | input->x + first_part_size, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1116 | input->len - first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1117 | output, output_buffer_size, |
| 1118 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1119 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1120 | TEST_ASSERT( psa_cipher_finish( &operation, |
| 1121 | output + function_output_length, |
| 1122 | output_buffer_size, |
| 1123 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1124 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1125 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 1126 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1127 | TEST_ASSERT( total_output_length == expected_output->len ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1128 | TEST_ASSERT( memcmp( expected_output->x, output, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1129 | expected_output->len ) == 0 ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1130 | |
| 1131 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1132 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1133 | psa_destroy_key( key_slot ); |
| 1134 | mbedtls_psa_crypto_free( ); |
| 1135 | } |
| 1136 | /* END_CASE */ |
| 1137 | |
| 1138 | /* BEGIN_CASE */ |
| 1139 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1140 | data_t *key, |
| 1141 | data_t *input, |
| 1142 | int first_part_size, |
| 1143 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1144 | { |
| 1145 | int key_slot = 1; |
| 1146 | |
| 1147 | psa_key_type_t key_type = key_type_arg; |
| 1148 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1149 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1150 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1151 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1152 | size_t output_buffer_size = 0; |
| 1153 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1154 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1155 | psa_cipher_operation_t operation; |
| 1156 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1157 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1158 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1159 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1160 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 1161 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 1162 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1163 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1164 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 1165 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1166 | |
| 1167 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1168 | |
| 1169 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1170 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1171 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1172 | TEST_ASSERT( psa_decrypt_setup( &operation, |
| 1173 | key_slot, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1174 | |
| 1175 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
| 1176 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
| 1177 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1178 | output_buffer_size = input->len + operation.block_size; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1179 | output = mbedtls_calloc( 1, output_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1180 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1181 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1182 | TEST_ASSERT( (unsigned int) first_part_size < input->len ); |
| 1183 | TEST_ASSERT( psa_cipher_update( &operation, |
| 1184 | input->x, first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1185 | output, output_buffer_size, |
| 1186 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1187 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1188 | TEST_ASSERT( psa_cipher_update( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1189 | input->x + first_part_size, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1190 | input->len - first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1191 | output, output_buffer_size, |
| 1192 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1193 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1194 | TEST_ASSERT( psa_cipher_finish( &operation, |
| 1195 | output + function_output_length, |
| 1196 | output_buffer_size, |
| 1197 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1198 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1199 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 1200 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1201 | TEST_ASSERT( total_output_length == expected_output->len ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1202 | TEST_ASSERT( memcmp( expected_output->x, output, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1203 | expected_output->len ) == 0 ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1204 | |
| 1205 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1206 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1207 | psa_destroy_key( key_slot ); |
| 1208 | mbedtls_psa_crypto_free( ); |
| 1209 | } |
| 1210 | /* END_CASE */ |
| 1211 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1212 | /* BEGIN_CASE */ |
| 1213 | void cipher_decrypt( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1214 | data_t *key, |
| 1215 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1216 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1217 | { |
| 1218 | int key_slot = 1; |
| 1219 | psa_status_t status; |
| 1220 | psa_key_type_t key_type = key_type_arg; |
| 1221 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1222 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1223 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1224 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1225 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1226 | size_t output_buffer_size = 0; |
| 1227 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1228 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1229 | psa_cipher_operation_t operation; |
| 1230 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1231 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1232 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1233 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1234 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 1235 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 1236 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1237 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1238 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 1239 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1240 | |
| 1241 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1242 | |
| 1243 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1244 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1245 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1246 | TEST_ASSERT( psa_decrypt_setup( &operation, |
| 1247 | key_slot, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1248 | |
| 1249 | TEST_ASSERT( psa_encrypt_set_iv( &operation, |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 1250 | iv, iv_size ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1251 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1252 | output_buffer_size = input->len + operation.block_size; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1253 | output = mbedtls_calloc( 1, output_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1254 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1255 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1256 | TEST_ASSERT( psa_cipher_update( &operation, |
| 1257 | input->x, input->len, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1258 | output, output_buffer_size, |
| 1259 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1260 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1261 | status = psa_cipher_finish( &operation, |
| 1262 | output + function_output_length, |
| 1263 | output_buffer_size, |
| 1264 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 1265 | total_output_length += function_output_length; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1266 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1267 | |
| 1268 | if( expected_status == PSA_SUCCESS ) |
| 1269 | { |
| 1270 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1271 | TEST_ASSERT( total_output_length == expected_output->len ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1272 | TEST_ASSERT( memcmp( expected_output->x, output, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1273 | expected_output->len ) == 0 ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1274 | } |
| 1275 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1276 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1277 | mbedtls_free( output ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1278 | psa_destroy_key( key_slot ); |
| 1279 | mbedtls_psa_crypto_free( ); |
| 1280 | } |
| 1281 | /* END_CASE */ |
| 1282 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1283 | /* BEGIN_CASE */ |
| 1284 | void cipher_verify_output( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1285 | data_t *key, |
| 1286 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1287 | { |
| 1288 | int key_slot = 1; |
| 1289 | psa_key_type_t key_type = key_type_arg; |
| 1290 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 1291 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1292 | size_t iv_size = 16; |
| 1293 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1294 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1295 | size_t output1_size = 0; |
| 1296 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1297 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1298 | size_t output2_size = 0; |
| 1299 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1300 | size_t function_output_length = 0; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1301 | psa_cipher_operation_t operation1; |
| 1302 | psa_cipher_operation_t operation2; |
| 1303 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1304 | TEST_ASSERT( key != NULL ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1305 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1306 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 1307 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1308 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1309 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1310 | |
| 1311 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1312 | key->x, key->len ) == PSA_SUCCESS ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1313 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1314 | TEST_ASSERT( psa_encrypt_setup( &operation1, |
| 1315 | key_slot, alg ) == PSA_SUCCESS ); |
| 1316 | TEST_ASSERT( psa_decrypt_setup( &operation2, |
| 1317 | key_slot, alg ) == PSA_SUCCESS ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1318 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1319 | TEST_ASSERT( psa_encrypt_generate_iv( &operation1, |
| 1320 | iv, iv_size, |
| 1321 | &iv_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1322 | output1_size = input->len + operation1.block_size; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1323 | output1 = mbedtls_calloc( 1, output1_size ); |
| 1324 | TEST_ASSERT( output1 != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1325 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1326 | TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1327 | output1, output1_size, |
| 1328 | &output1_length ) == PSA_SUCCESS ); |
| 1329 | TEST_ASSERT( psa_cipher_finish( &operation1, |
| 1330 | output1 + output1_length, output1_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1331 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1332 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1333 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1334 | |
| 1335 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 1336 | |
| 1337 | output2_size = output1_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1338 | output2 = mbedtls_calloc( 1, output2_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1339 | TEST_ASSERT( output2 != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1340 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1341 | TEST_ASSERT( psa_encrypt_set_iv( &operation2, |
| 1342 | iv, iv_length ) == PSA_SUCCESS ); |
| 1343 | TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 1344 | output2, output2_size, |
| 1345 | &output2_length ) == PSA_SUCCESS ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1346 | function_output_length = 0; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1347 | TEST_ASSERT( psa_cipher_finish( &operation2, |
| 1348 | output2 + output2_length, |
| 1349 | output2_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1350 | &function_output_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1351 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1352 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1353 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1354 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 1355 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1356 | TEST_ASSERT( input->len == output2_length ); |
| 1357 | TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1358 | |
| 1359 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1360 | mbedtls_free( output1 ); |
| 1361 | mbedtls_free( output2 ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1362 | psa_destroy_key( key_slot ); |
| 1363 | mbedtls_psa_crypto_free( ); |
| 1364 | } |
| 1365 | /* END_CASE */ |
| 1366 | |
| 1367 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1368 | void cipher_verify_output_multipart( int alg_arg, |
| 1369 | int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1370 | data_t *key, |
| 1371 | data_t *input, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 1372 | int first_part_size ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1373 | { |
| 1374 | int key_slot = 1; |
| 1375 | psa_key_type_t key_type = key_type_arg; |
| 1376 | psa_algorithm_t alg = alg_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1377 | unsigned char iv[16] = {0}; |
| 1378 | size_t iv_size = 16; |
| 1379 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1380 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1381 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1382 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1383 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1384 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1385 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1386 | size_t function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1387 | psa_cipher_operation_t operation1; |
| 1388 | psa_cipher_operation_t operation2; |
| 1389 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1390 | TEST_ASSERT( key != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1391 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1392 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 1393 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1394 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1395 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1396 | |
| 1397 | TEST_ASSERT( psa_import_key( key_slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1398 | key->x, key->len ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1399 | |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1400 | TEST_ASSERT( psa_encrypt_setup( &operation1, |
| 1401 | key_slot, alg ) == PSA_SUCCESS ); |
| 1402 | TEST_ASSERT( psa_decrypt_setup( &operation2, |
| 1403 | key_slot, alg ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1404 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1405 | TEST_ASSERT( psa_encrypt_generate_iv( &operation1, |
| 1406 | iv, iv_size, |
| 1407 | &iv_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1408 | output1_buffer_size = input->len + operation1.block_size; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1409 | output1 = mbedtls_calloc( 1, output1_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1410 | TEST_ASSERT( output1 != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1411 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1412 | TEST_ASSERT( (unsigned int) first_part_size < input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1413 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1414 | TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1415 | output1, output1_buffer_size, |
| 1416 | &function_output_length ) == PSA_SUCCESS ); |
| 1417 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1418 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1419 | TEST_ASSERT( psa_cipher_update( &operation1, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1420 | input->x + first_part_size, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1421 | input->len - first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1422 | output1, output1_buffer_size, |
| 1423 | &function_output_length ) == PSA_SUCCESS ); |
| 1424 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1425 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1426 | TEST_ASSERT( psa_cipher_finish( &operation1, |
| 1427 | output1 + output1_length, |
| 1428 | output1_buffer_size - output1_length, |
| 1429 | &function_output_length ) == PSA_SUCCESS ); |
| 1430 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1431 | |
| 1432 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 1433 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1434 | output2_buffer_size = output1_length; |
| 1435 | output2 = mbedtls_calloc( 1, output2_buffer_size ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1436 | TEST_ASSERT( output2 != NULL ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1437 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1438 | TEST_ASSERT( psa_encrypt_set_iv( &operation2, |
| 1439 | iv, iv_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1440 | |
| 1441 | TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1442 | output2, output2_buffer_size, |
| 1443 | &function_output_length ) == PSA_SUCCESS ); |
| 1444 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1445 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1446 | TEST_ASSERT( psa_cipher_update( &operation2, |
| 1447 | output1 + first_part_size, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1448 | output1_length - first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1449 | output2, output2_buffer_size, |
| 1450 | &function_output_length ) == PSA_SUCCESS ); |
| 1451 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 1452 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1453 | TEST_ASSERT( psa_cipher_finish( &operation2, |
| 1454 | output2 + output2_length, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 1455 | output2_buffer_size - output2_length, |
| 1456 | &function_output_length ) == PSA_SUCCESS ); |
| 1457 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 1458 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1459 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 1460 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1461 | TEST_ASSERT( input->len == output2_length ); |
| 1462 | TEST_ASSERT( memcmp( input->x, output2, input->len ) == 0 ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1463 | |
| 1464 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1465 | mbedtls_free( output1 ); |
| 1466 | mbedtls_free( output2 ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 1467 | psa_destroy_key( key_slot ); |
| 1468 | mbedtls_psa_crypto_free( ); |
| 1469 | } |
| 1470 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 1471 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1472 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1473 | void aead_encrypt_decrypt( int key_type_arg, |
| 1474 | data_t * key_data, |
| 1475 | int alg_arg, |
| 1476 | data_t * input_data, |
| 1477 | data_t * nonce, |
| 1478 | data_t * additional_data, |
| 1479 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1480 | { |
| 1481 | int slot = 1; |
| 1482 | psa_key_type_t key_type = key_type_arg; |
| 1483 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1484 | unsigned char *output_data = NULL; |
| 1485 | size_t output_size = 0; |
| 1486 | size_t output_length = 0; |
| 1487 | unsigned char *output_data2 = NULL; |
| 1488 | size_t output_length2 = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1489 | size_t tag_length = 16; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1490 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1491 | psa_key_policy_t policy; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1492 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1493 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1494 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1495 | TEST_ASSERT( nonce != NULL ); |
| 1496 | TEST_ASSERT( additional_data != NULL ); |
| 1497 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1498 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1499 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 1500 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 1501 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1502 | output_size = input_data->len + tag_length; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1503 | output_data = mbedtls_calloc( 1, output_size ); |
| 1504 | TEST_ASSERT( output_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1505 | |
| 1506 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1507 | |
| 1508 | psa_key_policy_init( &policy ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1509 | psa_key_policy_set_usage( &policy, |
| 1510 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, |
| 1511 | alg ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1512 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1513 | |
| 1514 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1515 | key_data->x, key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1516 | |
| 1517 | TEST_ASSERT( psa_aead_encrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1518 | nonce->x, nonce->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1519 | additional_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1520 | additional_data->len, |
| 1521 | input_data->x, input_data->len, |
| 1522 | output_data, output_size, |
| 1523 | &output_length ) == expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1524 | |
| 1525 | if( PSA_SUCCESS == expected_result ) |
| 1526 | { |
| 1527 | output_data2 = mbedtls_calloc( 1, output_length ); |
| 1528 | TEST_ASSERT( output_data2 != NULL ); |
| 1529 | |
| 1530 | TEST_ASSERT( psa_aead_decrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1531 | nonce->x, nonce->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1532 | additional_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1533 | additional_data->len, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1534 | output_data, output_length, |
| 1535 | output_data2, output_length, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1536 | &output_length2 ) == expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1537 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1538 | TEST_ASSERT( memcmp( input_data->x, output_data2, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1539 | input_data->len ) == 0 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1540 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1541 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1542 | exit: |
| 1543 | psa_destroy_key( slot ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1544 | mbedtls_free( output_data ); |
| 1545 | mbedtls_free( output_data2 ); |
| 1546 | mbedtls_psa_crypto_free( ); |
| 1547 | } |
| 1548 | /* END_CASE */ |
| 1549 | |
| 1550 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1551 | void aead_encrypt( int key_type_arg, data_t * key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1552 | int alg_arg, data_t * input_data, |
| 1553 | data_t * additional_data, data_t * nonce, |
| 1554 | data_t * expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1555 | { |
| 1556 | int slot = 1; |
| 1557 | psa_key_type_t key_type = key_type_arg; |
| 1558 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1559 | unsigned char *output_data = NULL; |
| 1560 | size_t output_size = 0; |
| 1561 | size_t output_length = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1562 | size_t tag_length = 16; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1563 | psa_key_policy_t policy; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1564 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1565 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1566 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1567 | TEST_ASSERT( additional_data != NULL ); |
| 1568 | TEST_ASSERT( nonce != NULL ); |
| 1569 | TEST_ASSERT( expected_result != NULL ); |
| 1570 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1571 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1572 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 1573 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 1574 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) ); |
| 1575 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1576 | output_size = input_data->len + tag_length; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1577 | output_data = mbedtls_calloc( 1, output_size ); |
| 1578 | TEST_ASSERT( output_data != NULL ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1579 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1580 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1581 | |
| 1582 | psa_key_policy_init( &policy ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1583 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1584 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1585 | |
| 1586 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1587 | key_data->x, |
| 1588 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1589 | |
| 1590 | TEST_ASSERT( psa_aead_encrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1591 | nonce->x, nonce->len, |
| 1592 | additional_data->x, additional_data->len, |
| 1593 | input_data->x, input_data->len, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1594 | output_data, output_size, |
| 1595 | &output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1596 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1597 | TEST_ASSERT( memcmp( output_data, expected_result->x, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1598 | output_length ) == 0 ); |
| 1599 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1600 | exit: |
| 1601 | psa_destroy_key( slot ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1602 | mbedtls_free( output_data ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1603 | mbedtls_psa_crypto_free( ); |
| 1604 | } |
| 1605 | /* END_CASE */ |
| 1606 | |
| 1607 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1608 | void aead_decrypt( int key_type_arg, data_t * key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1609 | int alg_arg, data_t * input_data, |
| 1610 | data_t * additional_data, data_t * nonce, |
| 1611 | data_t * expected_data, int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1612 | { |
| 1613 | int slot = 1; |
| 1614 | psa_key_type_t key_type = key_type_arg; |
| 1615 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1616 | unsigned char *output_data = NULL; |
| 1617 | size_t output_size = 0; |
| 1618 | size_t output_length = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1619 | size_t tag_length = 16; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1620 | psa_key_policy_t policy; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1621 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1622 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1623 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1624 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1625 | TEST_ASSERT( additional_data != NULL ); |
| 1626 | TEST_ASSERT( nonce != NULL ); |
| 1627 | TEST_ASSERT( expected_data != NULL ); |
| 1628 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1629 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1630 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 1631 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 1632 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) ); |
| 1633 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1634 | output_size = input_data->len + tag_length; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1635 | output_data = mbedtls_calloc( 1, output_size ); |
| 1636 | TEST_ASSERT( output_data != NULL ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1637 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1638 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1639 | |
| 1640 | psa_key_policy_init( &policy ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1641 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1642 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1643 | |
| 1644 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1645 | key_data->x, |
| 1646 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1647 | |
| 1648 | TEST_ASSERT( psa_aead_decrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1649 | nonce->x, nonce->len, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1650 | additional_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1651 | additional_data->len, |
| 1652 | input_data->x, input_data->len, |
| 1653 | output_data, output_size, |
| 1654 | &output_length ) == expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1655 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1656 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1657 | { |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1658 | TEST_ASSERT( memcmp( output_data, expected_data->x, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1659 | output_length ) == 0 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1660 | } |
| 1661 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1662 | exit: |
| 1663 | psa_destroy_key( slot ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1664 | mbedtls_free( output_data ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 1665 | mbedtls_psa_crypto_free( ); |
| 1666 | } |
| 1667 | /* END_CASE */ |
| 1668 | |
| 1669 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1670 | void signature_size( int type_arg, |
| 1671 | int bits, |
| 1672 | int alg_arg, |
| 1673 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1674 | { |
| 1675 | psa_key_type_t type = type_arg; |
| 1676 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1677 | size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1678 | TEST_ASSERT( actual_size == (size_t) expected_size_arg ); |
| 1679 | exit: |
| 1680 | ; |
| 1681 | } |
| 1682 | /* END_CASE */ |
| 1683 | |
| 1684 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1685 | void sign_deterministic( int key_type_arg, data_t *key_data, |
| 1686 | int alg_arg, data_t *input_data, |
| 1687 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1688 | { |
| 1689 | int slot = 1; |
| 1690 | psa_key_type_t key_type = key_type_arg; |
| 1691 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1692 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1693 | unsigned char *signature = NULL; |
| 1694 | size_t signature_size; |
| 1695 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1696 | psa_key_policy_t policy; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1697 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1698 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1699 | TEST_ASSERT( input_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1700 | TEST_ASSERT( output_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1701 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1702 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1703 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1704 | |
| 1705 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1706 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1707 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1708 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1709 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1710 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1711 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1712 | key_data->x, |
| 1713 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1714 | TEST_ASSERT( psa_get_key_information( slot, |
| 1715 | NULL, |
| 1716 | &key_bits ) == PSA_SUCCESS ); |
| 1717 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 1718 | /* Allocate a buffer which has the size advertized by the |
| 1719 | * library. */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1720 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, |
| 1721 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1722 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 1723 | TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1724 | signature = mbedtls_calloc( 1, signature_size ); |
| 1725 | TEST_ASSERT( signature != NULL ); |
| 1726 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 1727 | /* Perform the signature. */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1728 | TEST_ASSERT( psa_asymmetric_sign( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1729 | input_data->x, input_data->len, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1730 | NULL, 0, |
| 1731 | signature, signature_size, |
| 1732 | &signature_length ) == PSA_SUCCESS ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 1733 | /* Verify that the signature is correct. */ |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1734 | TEST_ASSERT( signature_length == output_data->len ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1735 | TEST_ASSERT( memcmp( signature, output_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1736 | output_data->len ) == 0 ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1737 | |
| 1738 | exit: |
| 1739 | psa_destroy_key( slot ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 1740 | mbedtls_free( signature ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1741 | mbedtls_psa_crypto_free( ); |
| 1742 | } |
| 1743 | /* END_CASE */ |
| 1744 | |
| 1745 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1746 | void sign_fail( int key_type_arg, data_t *key_data, |
| 1747 | int alg_arg, data_t *input_data, |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 1748 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1749 | { |
| 1750 | int slot = 1; |
| 1751 | psa_key_type_t key_type = key_type_arg; |
| 1752 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 1753 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1754 | psa_status_t actual_status; |
| 1755 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 1756 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 1757 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1758 | psa_key_policy_t policy; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1759 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1760 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1761 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1762 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1763 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1764 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1765 | signature = mbedtls_calloc( 1, signature_size ); |
| 1766 | TEST_ASSERT( signature != NULL ); |
| 1767 | |
| 1768 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1769 | |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1770 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1771 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1772 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1773 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1774 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1775 | key_data->x, |
| 1776 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1777 | |
| 1778 | actual_status = psa_asymmetric_sign( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1779 | input_data->x, input_data->len, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1780 | NULL, 0, |
| 1781 | signature, signature_size, |
| 1782 | &signature_length ); |
| 1783 | TEST_ASSERT( actual_status == expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 1784 | /* The value of *signature_length is unspecified on error, but |
| 1785 | * whatever it is, it should be less than signature_size, so that |
| 1786 | * if the caller tries to read *signature_length bytes without |
| 1787 | * checking the error code then they don't overflow a buffer. */ |
| 1788 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1789 | |
| 1790 | exit: |
| 1791 | psa_destroy_key( slot ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1792 | mbedtls_free( signature ); |
| 1793 | mbedtls_psa_crypto_free( ); |
| 1794 | } |
| 1795 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 1796 | |
| 1797 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1798 | void asymmetric_verify( int key_type_arg, data_t *key_data, |
| 1799 | int alg_arg, data_t *hash_data, |
| 1800 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1801 | { |
| 1802 | int slot = 1; |
| 1803 | psa_key_type_t key_type = key_type_arg; |
| 1804 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1805 | psa_key_policy_t policy; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1806 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 1807 | TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
| 1808 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1809 | TEST_ASSERT( key_data != NULL ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1810 | TEST_ASSERT( hash_data != NULL ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1811 | TEST_ASSERT( signature_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1812 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1813 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) ); |
| 1814 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1815 | |
| 1816 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1817 | |
| 1818 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1819 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1820 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1821 | |
| 1822 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1823 | key_data->x, |
| 1824 | key_data->len ) == PSA_SUCCESS ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1825 | |
| 1826 | TEST_ASSERT( psa_asymmetric_verify( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1827 | hash_data->x, hash_data->len, |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1828 | NULL, 0, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1829 | signature_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1830 | signature_data->len ) == PSA_SUCCESS ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1831 | exit: |
| 1832 | psa_destroy_key( slot ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 1833 | mbedtls_psa_crypto_free( ); |
| 1834 | } |
| 1835 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1836 | |
| 1837 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1838 | void asymmetric_verify_fail( int key_type_arg, data_t *key_data, |
| 1839 | int alg_arg, data_t *hash_data, |
| 1840 | data_t *signature_data, |
| 1841 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1842 | { |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1843 | int slot = 1; |
| 1844 | psa_key_type_t key_type = key_type_arg; |
| 1845 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1846 | psa_status_t actual_status; |
| 1847 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1848 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1849 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1850 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1851 | TEST_ASSERT( hash_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1852 | TEST_ASSERT( signature_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1853 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1854 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) ); |
| 1855 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1856 | |
| 1857 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1858 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1859 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1860 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1861 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1862 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1863 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1864 | key_data->x, |
| 1865 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1866 | |
| 1867 | actual_status = psa_asymmetric_verify( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1868 | hash_data->x, hash_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1869 | NULL, 0, |
| 1870 | signature_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1871 | signature_data->len ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1872 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1873 | TEST_ASSERT( actual_status == expected_status ); |
| 1874 | |
| 1875 | exit: |
| 1876 | psa_destroy_key( slot ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1877 | mbedtls_psa_crypto_free( ); |
| 1878 | } |
| 1879 | /* END_CASE */ |
| 1880 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1881 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1882 | void asymmetric_encrypt_decrypt( int key_type_arg, data_t *key_data, |
| 1883 | int alg_arg, data_t *input_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1884 | { |
| 1885 | int slot = 1; |
| 1886 | psa_key_type_t key_type = key_type_arg; |
| 1887 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1888 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1889 | size_t output_size = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1890 | size_t output_length = 0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1891 | unsigned char *output2 = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1892 | size_t output2_size = 0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1893 | size_t output2_length = 0; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1894 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1895 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1896 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1897 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1898 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1899 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1900 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1901 | output_size = key_data->len; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1902 | output2_size = output_size; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1903 | output = mbedtls_calloc( 1, output_size ); |
| 1904 | TEST_ASSERT( output != NULL ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1905 | output2 = mbedtls_calloc( 1, output2_size ); |
| 1906 | TEST_ASSERT( output2 != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1907 | |
| 1908 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1909 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1910 | psa_key_policy_init( &policy ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1911 | psa_key_policy_set_usage( &policy, |
| 1912 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1913 | alg ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1914 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1915 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1916 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1917 | key_data->x, |
| 1918 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1919 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 1920 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 1921 | * the original plaintext because of the non-optional random |
| 1922 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1923 | TEST_ASSERT( psa_asymmetric_encrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1924 | input_data->x, input_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1925 | NULL, 0, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1926 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1927 | &output_length ) == PSA_SUCCESS ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 1928 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1929 | TEST_ASSERT( psa_asymmetric_decrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1930 | output, output_length, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1931 | NULL, 0, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1932 | output2, output2_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1933 | &output2_length ) == PSA_SUCCESS ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 1934 | TEST_ASSERT( memcmp( input_data->x, output2, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1935 | input_data->len ) == 0 ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1936 | |
| 1937 | exit: |
| 1938 | psa_destroy_key( slot ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1939 | mbedtls_free( output ); |
| 1940 | mbedtls_free( output2 ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1941 | mbedtls_psa_crypto_free( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1942 | } |
| 1943 | /* END_CASE */ |
| 1944 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1945 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1946 | void asymmetric_encrypt_fail( int key_type_arg, data_t *key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1947 | int alg_arg, data_t *input_data, |
| 1948 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1949 | { |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1950 | int slot = 1; |
| 1951 | psa_key_type_t key_type = key_type_arg; |
| 1952 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1953 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 1954 | size_t output_size = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1955 | size_t output_length = 0; |
| 1956 | psa_status_t actual_status; |
| 1957 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1958 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1959 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1960 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1961 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1962 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 1963 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 1964 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1965 | output_size = key_data->len; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1966 | output = mbedtls_calloc( 1, output_size ); |
| 1967 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 1968 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1969 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1970 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1971 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1972 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 1973 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 1974 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1975 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1976 | key_data->x, |
| 1977 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1978 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1979 | actual_status = psa_asymmetric_encrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1980 | input_data->x, input_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1981 | NULL, 0, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1982 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1983 | &output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1984 | TEST_ASSERT( actual_status == expected_status ); |
| 1985 | |
| 1986 | exit: |
| 1987 | psa_destroy_key( slot ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1988 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1989 | mbedtls_psa_crypto_free( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1990 | } |
| 1991 | /* END_CASE */ |
| 1992 | |
| 1993 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1994 | void asymmetric_decrypt( int key_type_arg, data_t *key_data, |
| 1995 | int alg_arg, data_t *input_data, |
| 1996 | data_t *expected_data, int expected_size ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 1997 | { |
| 1998 | int slot = 1; |
| 1999 | psa_key_type_t key_type = key_type_arg; |
| 2000 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2001 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 2002 | size_t output_size = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2003 | size_t output_length = 0; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 2004 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2005 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2006 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2007 | TEST_ASSERT( input_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2008 | TEST_ASSERT( expected_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2009 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 2010 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 2011 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) ); |
| 2012 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2013 | output_size = key_data->len; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2014 | output = mbedtls_calloc( 1, output_size ); |
| 2015 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 2016 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2017 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2018 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 2019 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2020 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 2021 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 2022 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2023 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2024 | key_data->x, |
| 2025 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2026 | |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 2027 | TEST_ASSERT( psa_asymmetric_decrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2028 | input_data->x, input_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2029 | NULL, 0, |
| 2030 | output, |
| 2031 | output_size, |
| 2032 | &output_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2033 | TEST_ASSERT( (size_t) expected_size == output_length ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2034 | TEST_ASSERT( memcmp( expected_data->x, output, output_length ) == 0 ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2035 | |
| 2036 | exit: |
| 2037 | psa_destroy_key( slot ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2038 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2039 | mbedtls_psa_crypto_free( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2040 | } |
| 2041 | /* END_CASE */ |
| 2042 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2043 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2044 | void asymmetric_decrypt_fail( int key_type_arg, data_t *key_data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2045 | int alg_arg, data_t *input_data, |
| 2046 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2047 | { |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2048 | int slot = 1; |
| 2049 | psa_key_type_t key_type = key_type_arg; |
| 2050 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2051 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 2052 | size_t output_size = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2053 | size_t output_length = 0; |
| 2054 | psa_status_t actual_status; |
| 2055 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 2056 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2057 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2058 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2059 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2060 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 2061 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 2062 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2063 | output_size = key_data->len; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2064 | output = mbedtls_calloc( 1, output_size ); |
| 2065 | TEST_ASSERT( output != NULL ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 2066 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2067 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2068 | |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 2069 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2070 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 2071 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 2072 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2073 | TEST_ASSERT( psa_import_key( slot, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2074 | key_data->x, |
| 2075 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2076 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2077 | actual_status = psa_asymmetric_decrypt( slot, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2078 | input_data->x, input_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2079 | NULL, 0, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2080 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2081 | &output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2082 | TEST_ASSERT( actual_status == expected_status ); |
| 2083 | |
| 2084 | exit: |
| 2085 | psa_destroy_key( slot ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2086 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 2087 | mbedtls_psa_crypto_free( ); |
| 2088 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 2089 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 2090 | |
| 2091 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 2092 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 2093 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 2094 | size_t bytes = bytes_arg; |
| 2095 | const unsigned char trail[] = "don't overwrite me"; |
| 2096 | unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) ); |
| 2097 | unsigned char *changed = mbedtls_calloc( 1, bytes ); |
| 2098 | size_t i; |
| 2099 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 2100 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 2101 | TEST_ASSERT( output != NULL ); |
| 2102 | TEST_ASSERT( changed != NULL ); |
| 2103 | memcpy( output + bytes, trail, sizeof( trail ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 2104 | |
| 2105 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2106 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 2107 | /* Run several times, to ensure that every output byte will be |
| 2108 | * nonzero at least once with overwhelming probability |
| 2109 | * (2^(-8*number_of_runs)). */ |
| 2110 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 2111 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 2112 | memset( output, 0, bytes ); |
| 2113 | TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS ); |
| 2114 | |
| 2115 | /* Check that no more than bytes have been overwritten */ |
| 2116 | TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 ); |
| 2117 | |
| 2118 | for( i = 0; i < bytes; i++ ) |
| 2119 | { |
| 2120 | if( output[i] != 0 ) |
| 2121 | ++changed[i]; |
| 2122 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 2123 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 2124 | |
| 2125 | /* Check that every byte was changed to nonzero at least once. This |
| 2126 | * validates that psa_generate_random is overwriting every byte of |
| 2127 | * the output buffer. */ |
| 2128 | for( i = 0; i < bytes; i++ ) |
| 2129 | { |
| 2130 | TEST_ASSERT( changed[i] != 0 ); |
| 2131 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 2132 | |
| 2133 | exit: |
| 2134 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 2135 | mbedtls_free( output ); |
| 2136 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 2137 | } |
| 2138 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 2139 | |
| 2140 | /* BEGIN_CASE */ |
| 2141 | void generate_key( int type_arg, |
| 2142 | int bits_arg, |
| 2143 | int usage_arg, |
| 2144 | int alg_arg, |
| 2145 | int expected_status_arg ) |
| 2146 | { |
| 2147 | int slot = 1; |
| 2148 | psa_key_type_t type = type_arg; |
| 2149 | psa_key_usage_t usage = usage_arg; |
| 2150 | size_t bits = bits_arg; |
| 2151 | psa_algorithm_t alg = alg_arg; |
| 2152 | psa_status_t expected_status = expected_status_arg; |
| 2153 | psa_key_type_t got_type; |
| 2154 | size_t got_bits; |
| 2155 | unsigned char exported[616] = {0}; /* enough for a 1024-bit RSA key */ |
| 2156 | size_t exported_length; |
| 2157 | psa_status_t expected_export_status = |
| 2158 | usage & PSA_KEY_USAGE_EXPORT ? PSA_SUCCESS : PSA_ERROR_NOT_PERMITTED; |
| 2159 | psa_status_t expected_info_status = |
| 2160 | expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT; |
| 2161 | psa_key_policy_t policy; |
| 2162 | |
| 2163 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2164 | |
| 2165 | psa_key_policy_init( &policy ); |
| 2166 | psa_key_policy_set_usage( &policy, usage, alg ); |
| 2167 | TEST_ASSERT( psa_set_key_policy( slot, &policy ) == PSA_SUCCESS ); |
| 2168 | |
| 2169 | /* Generate a key */ |
| 2170 | TEST_ASSERT( psa_generate_key( slot, type, bits, |
| 2171 | NULL, 0 ) == expected_status ); |
| 2172 | |
| 2173 | /* Test the key information */ |
| 2174 | TEST_ASSERT( psa_get_key_information( slot, |
| 2175 | &got_type, |
| 2176 | &got_bits ) == expected_info_status ); |
| 2177 | if( expected_info_status != PSA_SUCCESS ) |
| 2178 | goto exit; |
| 2179 | TEST_ASSERT( got_type == type ); |
| 2180 | TEST_ASSERT( got_bits == bits ); |
| 2181 | |
| 2182 | /* Export the key */ |
| 2183 | TEST_ASSERT( psa_export_key( slot, |
| 2184 | exported, sizeof( exported ), |
| 2185 | &exported_length ) == expected_export_status ); |
| 2186 | if( expected_export_status == PSA_SUCCESS ) |
| 2187 | { |
Gilles Peskine | 48c0ea1 | 2018-06-21 14:15:31 +0200 | [diff] [blame] | 2188 | if( key_type_is_raw_bytes( type ) ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 2189 | TEST_ASSERT( exported_length == ( bits + 7 ) / 8 ); |
| 2190 | #if defined(MBEDTLS_DES_C) |
| 2191 | if( type == PSA_KEY_TYPE_DES ) |
| 2192 | { |
| 2193 | /* Check the parity bits. */ |
| 2194 | unsigned i; |
| 2195 | for( i = 0; i < bits / 8; i++ ) |
| 2196 | { |
| 2197 | unsigned bit_count = 0; |
| 2198 | unsigned m; |
| 2199 | for( m = 1; m <= 0x100; m <<= 1 ) |
| 2200 | { |
| 2201 | if( exported[i] & m ) |
| 2202 | ++bit_count; |
| 2203 | } |
| 2204 | TEST_ASSERT( bit_count % 2 != 0 ); |
| 2205 | } |
| 2206 | } |
| 2207 | #endif |
| 2208 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
| 2209 | if( type == PSA_KEY_TYPE_RSA_KEYPAIR ) |
| 2210 | { |
| 2211 | /* Sanity check: does this look like the beginning of a PKCS#8 |
| 2212 | * RSA key pair? Assumes bits is a multiple of 8. */ |
| 2213 | size_t n_bytes = bits / 8 + 1; |
| 2214 | size_t n_encoded_bytes; |
| 2215 | unsigned char *n_end; |
| 2216 | TEST_ASSERT( exported_length >= 7 + ( n_bytes + 3 ) * 9 / 2 ); |
| 2217 | TEST_ASSERT( exported[0] == 0x30 ); |
| 2218 | TEST_ASSERT( exported[1] == 0x82 ); // assumes >=416-bit key |
| 2219 | TEST_ASSERT( exported[4] == 0x02 ); |
| 2220 | TEST_ASSERT( exported[5] == 0x01 ); |
| 2221 | TEST_ASSERT( exported[6] == 0x00 ); |
| 2222 | TEST_ASSERT( exported[7] == 0x02 ); |
| 2223 | n_encoded_bytes = exported[8]; |
| 2224 | n_end = exported + 9 + n_encoded_bytes; |
| 2225 | if( n_encoded_bytes & 0x80 ) |
| 2226 | { |
| 2227 | n_encoded_bytes = ( n_encoded_bytes & 0x7f ) << 7; |
| 2228 | n_encoded_bytes |= exported[9] & 0x7f; |
| 2229 | n_end += 1; |
| 2230 | } |
| 2231 | /* The encoding of n should start with a 0 byte since it should |
| 2232 | * have its high bit set. However Mbed TLS is not compliant and |
| 2233 | * generates an invalid, but widely tolerated, encoding of |
| 2234 | * positive INTEGERs with a bit size that is a multiple of 8 |
| 2235 | * with no leading 0 byte. Accept this here. */ |
| 2236 | TEST_ASSERT( n_bytes == n_encoded_bytes || |
| 2237 | n_bytes == n_encoded_bytes + 1 ); |
| 2238 | if( n_bytes == n_encoded_bytes ) |
| 2239 | TEST_ASSERT( exported[n_encoded_bytes <= 127 ? 9 : 10] == 0x00 ); |
| 2240 | /* Sanity check: e must be 3 */ |
| 2241 | TEST_ASSERT( n_end[0] == 0x02 ); |
| 2242 | TEST_ASSERT( n_end[1] == 0x03 ); |
| 2243 | TEST_ASSERT( n_end[2] == 0x01 ); |
| 2244 | TEST_ASSERT( n_end[3] == 0x00 ); |
| 2245 | TEST_ASSERT( n_end[4] == 0x01 ); |
| 2246 | TEST_ASSERT( n_end[5] == 0x02 ); |
| 2247 | } |
| 2248 | #endif /* MBEDTLS_RSA_C */ |
| 2249 | #if defined(MBEDTLS_ECP_C) |
| 2250 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
| 2251 | { |
| 2252 | /* Sanity check: does this look like the beginning of a PKCS#8 |
| 2253 | * elliptic curve key pair? */ |
| 2254 | TEST_ASSERT( exported_length >= bits * 3 / 8 + 10 ); |
| 2255 | TEST_ASSERT( exported[0] == 0x30 ); |
| 2256 | } |
| 2257 | #endif /* MBEDTLS_ECP_C */ |
| 2258 | } |
| 2259 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 2260 | /* Do something with the key according to its type and permitted usage. */ |
| 2261 | if( PSA_ALG_IS_MAC( alg ) ) |
| 2262 | exercise_mac_key( slot, usage, alg ); |
| 2263 | else if( PSA_ALG_IS_CIPHER( alg ) ) |
| 2264 | exercise_cipher_key( slot, usage, alg ); |
| 2265 | else if( PSA_ALG_IS_AEAD( alg ) ) |
| 2266 | exercise_aead_key( slot, usage, alg ); |
| 2267 | else if( PSA_ALG_IS_SIGN( alg ) ) |
| 2268 | exercise_signature_key( slot, usage, alg ); |
| 2269 | else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
| 2270 | exercise_asymmetric_encryption_key( slot, usage, alg ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 2271 | |
| 2272 | exit: |
| 2273 | psa_destroy_key( slot ); |
| 2274 | mbedtls_psa_crypto_free( ); |
| 2275 | } |
| 2276 | /* END_CASE */ |