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