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