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