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