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