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