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