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