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