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