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 | fc411f1 | 2018-10-25 22:34:48 +0200 | [diff] [blame] | 14 | #define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) ) |
| 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 | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 131 | static int exercise_mac_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 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, |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 143 | handle, 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, |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 158 | handle, 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 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 173 | static int exercise_cipher_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 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, |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 189 | handle, alg ) == PSA_SUCCESS ); |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 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; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 211 | TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) ); |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 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, |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 215 | handle, alg ) == PSA_SUCCESS ); |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 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 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 244 | static int exercise_aead_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 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 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 257 | TEST_ASSERT( psa_aead_encrypt( handle, alg, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 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 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 271 | TEST_ASSERT( psa_aead_decrypt( handle, alg, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 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 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 285 | static int exercise_signature_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 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 | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 302 | TEST_ASSERT( psa_asymmetric_sign( handle, alg, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 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 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 314 | TEST_ASSERT( psa_asymmetric_verify( handle, alg, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 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 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 326 | static int exercise_asymmetric_encryption_key( psa_key_handle_t handle, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 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( |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 338 | psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 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 = |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 348 | psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 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 | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 365 | static int exercise_key_derivation_key( psa_key_handle_t handle, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 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, |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 379 | handle, alg, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 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 | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 395 | /* We need two keys to exercise key agreement. Exercise the |
| 396 | * private key against its own public key. */ |
| 397 | static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator, |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 398 | psa_key_handle_t handle, |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 399 | psa_algorithm_t alg ) |
| 400 | { |
| 401 | psa_key_type_t private_key_type; |
| 402 | psa_key_type_t public_key_type; |
| 403 | size_t key_bits; |
| 404 | uint8_t *public_key = NULL; |
| 405 | size_t public_key_length; |
| 406 | /* Return UNKNOWN_ERROR if something other than the final call to |
| 407 | * psa_key_agreement fails. This isn't fully satisfactory, but it's |
| 408 | * good enough: callers will report it as a failed test anyway. */ |
| 409 | psa_status_t status = PSA_ERROR_UNKNOWN_ERROR; |
| 410 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 411 | TEST_ASSERT( psa_get_key_information( handle, |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 412 | &private_key_type, |
| 413 | &key_bits ) == PSA_SUCCESS ); |
| 414 | public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type ); |
| 415 | public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits ); |
| 416 | ASSERT_ALLOC( public_key, public_key_length ); |
| 417 | TEST_ASSERT( public_key != NULL ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 418 | TEST_ASSERT( psa_export_public_key( handle, |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 419 | public_key, public_key_length, |
| 420 | &public_key_length ) == PSA_SUCCESS ); |
| 421 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 422 | status = psa_key_agreement( generator, handle, |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 423 | public_key, public_key_length, |
| 424 | alg ); |
| 425 | exit: |
| 426 | mbedtls_free( public_key ); |
| 427 | return( status ); |
| 428 | } |
| 429 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 430 | static int exercise_key_agreement_key( psa_key_handle_t handle, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 431 | psa_key_usage_t usage, |
| 432 | psa_algorithm_t alg ) |
| 433 | { |
| 434 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 435 | unsigned char output[1]; |
| 436 | int ok = 0; |
| 437 | |
| 438 | if( usage & PSA_KEY_USAGE_DERIVE ) |
| 439 | { |
| 440 | /* We need two keys to exercise key agreement. Exercise the |
| 441 | * private key against its own public key. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 442 | TEST_ASSERT( key_agreement_with_self( &generator, handle, alg ) == |
Gilles Peskine | c7998b7 | 2018-11-07 18:45:02 +0100 | [diff] [blame] | 443 | PSA_SUCCESS ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 444 | TEST_ASSERT( psa_generator_read( &generator, |
| 445 | output, |
| 446 | sizeof( output ) ) == PSA_SUCCESS ); |
| 447 | TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS ); |
| 448 | } |
| 449 | ok = 1; |
| 450 | |
| 451 | exit: |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 452 | return( ok ); |
| 453 | } |
| 454 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 455 | static int is_oid_of_key_type( psa_key_type_t type, |
| 456 | const uint8_t *oid, size_t oid_length ) |
| 457 | { |
Gilles Peskine | ae3d2a2 | 2018-08-13 14:14:22 +0200 | [diff] [blame] | 458 | const uint8_t *expected_oid = NULL; |
| 459 | size_t expected_oid_length = 0; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 460 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | ae3d2a2 | 2018-08-13 14:14:22 +0200 | [diff] [blame] | 461 | if( PSA_KEY_TYPE_IS_RSA( type ) ) |
| 462 | { |
| 463 | expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA; |
| 464 | expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1; |
| 465 | } |
| 466 | else |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 467 | #endif /* MBEDTLS_RSA_C */ |
| 468 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | ae3d2a2 | 2018-08-13 14:14:22 +0200 | [diff] [blame] | 469 | if( PSA_KEY_TYPE_IS_ECC( type ) ) |
| 470 | { |
| 471 | expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED; |
| 472 | expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1; |
| 473 | } |
| 474 | else |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 475 | #endif /* MBEDTLS_ECP_C */ |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 476 | { |
| 477 | char message[40]; |
| 478 | mbedtls_snprintf( message, sizeof( message ), |
| 479 | "OID not known for key type=0x%08lx", |
| 480 | (unsigned long) type ); |
| 481 | test_fail( message, __LINE__, __FILE__ ); |
| 482 | return( 0 ); |
| 483 | } |
| 484 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 485 | ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 486 | return( 1 ); |
| 487 | |
| 488 | exit: |
| 489 | return( 0 ); |
| 490 | } |
| 491 | |
| 492 | static int asn1_skip_integer( unsigned char **p, const unsigned char *end, |
| 493 | size_t min_bits, size_t max_bits, |
| 494 | int must_be_odd ) |
| 495 | { |
| 496 | size_t len; |
| 497 | size_t actual_bits; |
| 498 | unsigned char msb; |
| 499 | TEST_ASSERT( mbedtls_asn1_get_tag( p, end, &len, |
| 500 | MBEDTLS_ASN1_INTEGER ) == 0 ); |
| 501 | /* Tolerate a slight departure from DER encoding: |
| 502 | * - 0 may be represented by an empty string or a 1-byte string. |
| 503 | * - The sign bit may be used as a value bit. */ |
| 504 | if( ( len == 1 && ( *p )[0] == 0 ) || |
| 505 | ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) ) |
| 506 | { |
| 507 | ++( *p ); |
| 508 | --len; |
| 509 | } |
| 510 | if( min_bits == 0 && len == 0 ) |
| 511 | return( 1 ); |
| 512 | msb = ( *p )[0]; |
| 513 | TEST_ASSERT( msb != 0 ); |
| 514 | actual_bits = 8 * ( len - 1 ); |
| 515 | while( msb != 0 ) |
| 516 | { |
| 517 | msb >>= 1; |
| 518 | ++actual_bits; |
| 519 | } |
| 520 | TEST_ASSERT( actual_bits >= min_bits ); |
| 521 | TEST_ASSERT( actual_bits <= max_bits ); |
| 522 | if( must_be_odd ) |
| 523 | TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 ); |
| 524 | *p += len; |
| 525 | return( 1 ); |
| 526 | exit: |
| 527 | return( 0 ); |
| 528 | } |
| 529 | |
| 530 | static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end, |
| 531 | size_t *len, |
| 532 | unsigned char n, unsigned char tag ) |
| 533 | { |
| 534 | int ret; |
| 535 | ret = mbedtls_asn1_get_tag( p, end, len, |
| 536 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 537 | MBEDTLS_ASN1_CONSTRUCTED | ( n ) ); |
| 538 | if( ret != 0 ) |
| 539 | return( ret ); |
| 540 | end = *p + *len; |
| 541 | ret = mbedtls_asn1_get_tag( p, end, len, tag ); |
| 542 | if( ret != 0 ) |
| 543 | return( ret ); |
| 544 | if( *p + *len != end ) |
| 545 | return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
| 546 | return( 0 ); |
| 547 | } |
| 548 | |
| 549 | static int exported_key_sanity_check( psa_key_type_t type, size_t bits, |
| 550 | uint8_t *exported, size_t exported_length ) |
| 551 | { |
| 552 | if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 553 | TEST_ASSERT( exported_length == ( bits + 7 ) / 8 ); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 554 | else |
| 555 | TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 556 | |
| 557 | #if defined(MBEDTLS_DES_C) |
| 558 | if( type == PSA_KEY_TYPE_DES ) |
| 559 | { |
| 560 | /* Check the parity bits. */ |
| 561 | unsigned i; |
| 562 | for( i = 0; i < bits / 8; i++ ) |
| 563 | { |
| 564 | unsigned bit_count = 0; |
| 565 | unsigned m; |
| 566 | for( m = 1; m <= 0x100; m <<= 1 ) |
| 567 | { |
| 568 | if( exported[i] & m ) |
| 569 | ++bit_count; |
| 570 | } |
| 571 | TEST_ASSERT( bit_count % 2 != 0 ); |
| 572 | } |
| 573 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 574 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 575 | #endif |
| 576 | |
| 577 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) |
| 578 | if( type == PSA_KEY_TYPE_RSA_KEYPAIR ) |
| 579 | { |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 580 | uint8_t *p = exported; |
| 581 | uint8_t *end = exported + exported_length; |
| 582 | size_t len; |
| 583 | /* RSAPrivateKey ::= SEQUENCE { |
Gilles Peskine | dea46cf | 2018-08-21 16:12:54 +0200 | [diff] [blame] | 584 | * version INTEGER, -- must be 0 |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 585 | * modulus INTEGER, -- n |
| 586 | * publicExponent INTEGER, -- e |
| 587 | * privateExponent INTEGER, -- d |
| 588 | * prime1 INTEGER, -- p |
| 589 | * prime2 INTEGER, -- q |
| 590 | * exponent1 INTEGER, -- d mod (p-1) |
| 591 | * exponent2 INTEGER, -- d mod (q-1) |
| 592 | * coefficient INTEGER, -- (inverse of q) mod p |
| 593 | * } |
| 594 | */ |
| 595 | TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len, |
| 596 | MBEDTLS_ASN1_SEQUENCE | |
| 597 | MBEDTLS_ASN1_CONSTRUCTED ) == 0 ); |
| 598 | TEST_ASSERT( p + len == end ); |
| 599 | if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) ) |
| 600 | goto exit; |
| 601 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 602 | goto exit; |
| 603 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 604 | goto exit; |
| 605 | /* Require d to be at least half the size of n. */ |
| 606 | if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) ) |
| 607 | goto exit; |
| 608 | /* Require p and q to be at most half the size of n, rounded up. */ |
| 609 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 610 | goto exit; |
| 611 | if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) ) |
| 612 | goto exit; |
| 613 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 614 | goto exit; |
| 615 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 616 | goto exit; |
| 617 | if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) ) |
| 618 | goto exit; |
| 619 | TEST_ASSERT( p == end ); |
| 620 | } |
| 621 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 622 | #endif /* MBEDTLS_RSA_C */ |
| 623 | |
| 624 | #if defined(MBEDTLS_ECP_C) |
| 625 | if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) ) |
| 626 | { |
Gilles Peskine | 5b802a3 | 2018-10-29 19:21:41 +0100 | [diff] [blame] | 627 | /* Just the secret value */ |
| 628 | TEST_ASSERT( exported_length == PSA_BITS_TO_BYTES( bits ) ); |
| 629 | } |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 630 | else |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 631 | #endif /* MBEDTLS_ECP_C */ |
| 632 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 633 | if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
| 634 | { |
| 635 | uint8_t *p = exported; |
| 636 | uint8_t *end = exported + exported_length; |
| 637 | size_t len; |
| 638 | mbedtls_asn1_buf alg; |
| 639 | mbedtls_asn1_buf params; |
| 640 | mbedtls_asn1_bitstring bitstring; |
| 641 | /* SubjectPublicKeyInfo ::= SEQUENCE { |
| 642 | * algorithm AlgorithmIdentifier, |
| 643 | * subjectPublicKey BIT STRING } |
| 644 | * AlgorithmIdentifier ::= SEQUENCE { |
| 645 | * algorithm OBJECT IDENTIFIER, |
| 646 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
| 647 | */ |
| 648 | TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len, |
| 649 | MBEDTLS_ASN1_SEQUENCE | |
| 650 | MBEDTLS_ASN1_CONSTRUCTED ) == 0 ); |
| 651 | TEST_ASSERT( p + len == end ); |
| 652 | TEST_ASSERT( mbedtls_asn1_get_alg( &p, end, &alg, ¶ms ) == 0 ); |
| 653 | if( ! is_oid_of_key_type( type, alg.p, alg.len ) ) |
| 654 | goto exit; |
| 655 | TEST_ASSERT( mbedtls_asn1_get_bitstring( &p, end, &bitstring ) == 0 ); |
| 656 | TEST_ASSERT( p == end ); |
| 657 | p = bitstring.p; |
| 658 | #if defined(MBEDTLS_RSA_C) |
| 659 | if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ) |
| 660 | { |
| 661 | /* RSAPublicKey ::= SEQUENCE { |
| 662 | * modulus INTEGER, -- n |
| 663 | * publicExponent INTEGER } -- e |
| 664 | */ |
| 665 | TEST_ASSERT( bitstring.unused_bits == 0 ); |
| 666 | TEST_ASSERT( mbedtls_asn1_get_tag( &p, end, &len, |
| 667 | MBEDTLS_ASN1_SEQUENCE | |
| 668 | MBEDTLS_ASN1_CONSTRUCTED ) == 0 ); |
| 669 | TEST_ASSERT( p + len == end ); |
| 670 | if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) ) |
| 671 | goto exit; |
| 672 | if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) ) |
| 673 | goto exit; |
| 674 | TEST_ASSERT( p == end ); |
| 675 | } |
| 676 | else |
| 677 | #endif /* MBEDTLS_RSA_C */ |
| 678 | #if defined(MBEDTLS_ECP_C) |
| 679 | if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) ) |
| 680 | { |
| 681 | /* ECPoint ::= ... |
Gilles Peskine | c6290c0 | 2018-08-13 17:24:59 +0200 | [diff] [blame] | 682 | * -- first 8 bits: 0x04 (uncompressed representation); |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 683 | * -- then x_P as an n-bit string, big endian; |
| 684 | * -- then y_P as a n-bit string, big endian, |
| 685 | * -- where n is the order of the curve. |
| 686 | */ |
| 687 | TEST_ASSERT( bitstring.unused_bits == 0 ); |
| 688 | TEST_ASSERT( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ) == end ); |
| 689 | TEST_ASSERT( p[0] == 4 ); |
| 690 | } |
| 691 | else |
| 692 | #endif /* MBEDTLS_ECP_C */ |
| 693 | { |
Jaeden Amero | 594a330 | 2018-10-26 17:07:22 +0100 | [diff] [blame] | 694 | char message[47]; |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 695 | mbedtls_snprintf( message, sizeof( message ), |
| 696 | "No sanity check for public key type=0x%08lx", |
| 697 | (unsigned long) type ); |
| 698 | test_fail( message, __LINE__, __FILE__ ); |
| 699 | return( 0 ); |
| 700 | } |
| 701 | } |
| 702 | else |
| 703 | |
| 704 | { |
| 705 | /* No sanity checks for other types */ |
| 706 | } |
| 707 | |
| 708 | return( 1 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 709 | |
| 710 | exit: |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 711 | return( 0 ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 712 | } |
| 713 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 714 | static int exercise_export_key( psa_key_handle_t handle, |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 715 | psa_key_usage_t usage ) |
| 716 | { |
| 717 | psa_key_type_t type; |
| 718 | size_t bits; |
| 719 | uint8_t *exported = NULL; |
| 720 | size_t exported_size = 0; |
| 721 | size_t exported_length = 0; |
| 722 | int ok = 0; |
| 723 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 724 | TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) == PSA_SUCCESS ); |
Gilles Peskine | acec7b6f | 2018-09-13 20:34:11 +0200 | [diff] [blame] | 725 | |
| 726 | if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 && |
| 727 | ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 728 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 729 | TEST_ASSERT( psa_export_key( handle, NULL, 0, &exported_length ) == |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 730 | PSA_ERROR_NOT_PERMITTED ); |
| 731 | return( 1 ); |
| 732 | } |
| 733 | |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 734 | exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 735 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 736 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 737 | TEST_ASSERT( psa_export_key( handle, |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 738 | exported, exported_size, |
| 739 | &exported_length ) == PSA_SUCCESS ); |
| 740 | ok = exported_key_sanity_check( type, bits, exported, exported_length ); |
| 741 | |
| 742 | exit: |
| 743 | mbedtls_free( exported ); |
| 744 | return( ok ); |
| 745 | } |
| 746 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 747 | static int exercise_export_public_key( psa_key_handle_t handle ) |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 748 | { |
| 749 | psa_key_type_t type; |
| 750 | psa_key_type_t public_type; |
| 751 | size_t bits; |
| 752 | uint8_t *exported = NULL; |
| 753 | size_t exported_size = 0; |
| 754 | size_t exported_length = 0; |
| 755 | int ok = 0; |
| 756 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 757 | TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) == PSA_SUCCESS ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 758 | if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) ) |
| 759 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 760 | TEST_ASSERT( psa_export_public_key( handle, |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 761 | NULL, 0, &exported_length ) == |
| 762 | PSA_ERROR_INVALID_ARGUMENT ); |
| 763 | return( 1 ); |
| 764 | } |
| 765 | |
| 766 | public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type ); |
| 767 | exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 768 | ASSERT_ALLOC( exported, exported_size ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 769 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 770 | TEST_ASSERT( psa_export_public_key( handle, |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 771 | exported, exported_size, |
| 772 | &exported_length ) == PSA_SUCCESS ); |
| 773 | ok = exported_key_sanity_check( public_type, bits, |
| 774 | exported, exported_length ); |
| 775 | |
| 776 | exit: |
| 777 | mbedtls_free( exported ); |
| 778 | return( ok ); |
| 779 | } |
| 780 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 781 | static int exercise_key( psa_key_handle_t handle, |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 782 | psa_key_usage_t usage, |
| 783 | psa_algorithm_t alg ) |
| 784 | { |
| 785 | int ok; |
| 786 | if( alg == 0 ) |
| 787 | ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */ |
| 788 | else if( PSA_ALG_IS_MAC( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 789 | ok = exercise_mac_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 790 | else if( PSA_ALG_IS_CIPHER( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 791 | ok = exercise_cipher_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 792 | else if( PSA_ALG_IS_AEAD( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 793 | ok = exercise_aead_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 794 | else if( PSA_ALG_IS_SIGN( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 795 | ok = exercise_signature_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 796 | else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 797 | ok = exercise_asymmetric_encryption_key( handle, usage, alg ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 798 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 799 | ok = exercise_key_derivation_key( handle, usage, alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 800 | else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 801 | ok = exercise_key_agreement_key( handle, usage, alg ); |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 802 | else |
| 803 | { |
| 804 | char message[40]; |
| 805 | mbedtls_snprintf( message, sizeof( message ), |
| 806 | "No code to exercise alg=0x%08lx", |
| 807 | (unsigned long) alg ); |
| 808 | test_fail( message, __LINE__, __FILE__ ); |
| 809 | ok = 0; |
| 810 | } |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 811 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 812 | ok = ok && exercise_export_key( handle, usage ); |
| 813 | ok = ok && exercise_export_public_key( handle ); |
Gilles Peskine | d14664a | 2018-08-10 19:07:32 +0200 | [diff] [blame] | 814 | |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 815 | return( ok ); |
| 816 | } |
| 817 | |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 818 | static psa_key_usage_t usage_to_exercise( psa_key_type_t type, |
| 819 | psa_algorithm_t alg ) |
| 820 | { |
| 821 | if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) ) |
| 822 | { |
| 823 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 824 | PSA_KEY_USAGE_VERIFY : |
| 825 | PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY ); |
| 826 | } |
| 827 | else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) || |
| 828 | PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) ) |
| 829 | { |
| 830 | return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ? |
| 831 | PSA_KEY_USAGE_ENCRYPT : |
| 832 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 833 | } |
| 834 | else if( PSA_ALG_IS_KEY_DERIVATION( alg ) || |
| 835 | PSA_ALG_IS_KEY_AGREEMENT( alg ) ) |
| 836 | { |
| 837 | return( PSA_KEY_USAGE_DERIVE ); |
| 838 | } |
| 839 | else |
| 840 | { |
| 841 | return( 0 ); |
| 842 | } |
| 843 | |
| 844 | } |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 845 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 846 | /* An overapproximation of the amount of storage needed for a key of the |
| 847 | * given type and with the given content. The API doesn't make it easy |
| 848 | * to find a good value for the size. The current implementation doesn't |
| 849 | * care about the value anyway. */ |
| 850 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 851 | ( data )->len |
| 852 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 853 | typedef enum { |
| 854 | IMPORT_KEY = 0, |
| 855 | GENERATE_KEY = 1, |
| 856 | DERIVE_KEY = 2 |
| 857 | } generate_method; |
| 858 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 859 | /* END_HEADER */ |
| 860 | |
| 861 | /* BEGIN_DEPENDENCIES |
| 862 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 863 | * END_DEPENDENCIES |
| 864 | */ |
| 865 | |
| 866 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 867 | void static_checks( ) |
| 868 | { |
| 869 | size_t max_truncated_mac_size = |
| 870 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 871 | |
| 872 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 873 | * encoding. The shifted mask is the maximum truncated value. The |
| 874 | * untruncated algorithm may be one byte larger. */ |
| 875 | TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size ); |
| 876 | } |
| 877 | /* END_CASE */ |
| 878 | |
| 879 | /* BEGIN_CASE */ |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 880 | void import( data_t *data, int type, int expected_status_arg ) |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 881 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 882 | psa_key_handle_t handle = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 883 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 884 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 885 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 886 | TEST_ASSERT( data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 887 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 888 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 889 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 890 | TEST_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ), |
| 891 | &handle ) == PSA_SUCCESS ); |
| 892 | status = psa_import_key( handle, 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 ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 895 | TEST_ASSERT( psa_destroy_key( handle ) == PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 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 | a426168 | 2018-12-03 11:34:01 +0100 | [diff] [blame] | 903 | void import_twice( int alg_arg, int usage_arg, |
| 904 | int type1_arg, data_t *data1, |
| 905 | int expected_import1_status_arg, |
| 906 | int type2_arg, data_t *data2, |
| 907 | int expected_import2_status_arg ) |
| 908 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 909 | psa_key_handle_t handle = 0; |
Gilles Peskine | a426168 | 2018-12-03 11:34:01 +0100 | [diff] [blame] | 910 | psa_algorithm_t alg = alg_arg; |
| 911 | psa_key_usage_t usage = usage_arg; |
| 912 | psa_key_type_t type1 = type1_arg; |
| 913 | psa_status_t expected_import1_status = expected_import1_status_arg; |
| 914 | psa_key_type_t type2 = type2_arg; |
| 915 | psa_status_t expected_import2_status = expected_import2_status_arg; |
| 916 | psa_key_policy_t policy; |
| 917 | psa_status_t status; |
| 918 | |
| 919 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 920 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 921 | TEST_ASSERT( psa_allocate_key( type1, |
| 922 | MAX( KEY_BITS_FROM_DATA( type1, data1 ), |
| 923 | KEY_BITS_FROM_DATA( type2, data2 ) ), |
| 924 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | a426168 | 2018-12-03 11:34:01 +0100 | [diff] [blame] | 925 | psa_key_policy_init( &policy ); |
| 926 | psa_key_policy_set_usage( &policy, usage, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 927 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Gilles Peskine | a426168 | 2018-12-03 11:34:01 +0100 | [diff] [blame] | 928 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 929 | status = psa_import_key( handle, type1, data1->x, data1->len ); |
Gilles Peskine | a426168 | 2018-12-03 11:34:01 +0100 | [diff] [blame] | 930 | TEST_ASSERT( status == expected_import1_status ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 931 | status = psa_import_key( handle, type2, data2->x, data2->len ); |
Gilles Peskine | a426168 | 2018-12-03 11:34:01 +0100 | [diff] [blame] | 932 | TEST_ASSERT( status == expected_import2_status ); |
| 933 | |
| 934 | if( expected_import1_status == PSA_SUCCESS || |
| 935 | expected_import2_status == PSA_SUCCESS ) |
| 936 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 937 | TEST_ASSERT( exercise_key( handle, usage, alg ) ); |
Gilles Peskine | a426168 | 2018-12-03 11:34:01 +0100 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | exit: |
| 941 | mbedtls_psa_crypto_free( ); |
| 942 | } |
| 943 | /* END_CASE */ |
| 944 | |
| 945 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 946 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 947 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 948 | psa_key_handle_t handle = 0; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 949 | size_t bits = bits_arg; |
| 950 | psa_status_t expected_status = expected_status_arg; |
| 951 | psa_status_t status; |
| 952 | psa_key_type_t type = |
| 953 | keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY; |
| 954 | size_t buffer_size = /* Slight overapproximations */ |
| 955 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 956 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 957 | unsigned char *p; |
| 958 | int ret; |
| 959 | size_t length; |
| 960 | |
| 961 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 962 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 963 | |
| 964 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 965 | bits, keypair ) ) >= 0 ); |
| 966 | length = ret; |
| 967 | |
| 968 | /* Try importing the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 969 | TEST_ASSERT( psa_allocate_key( type, bits, &handle ) == PSA_SUCCESS ); |
| 970 | status = psa_import_key( handle, type, p, length ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 971 | TEST_ASSERT( status == expected_status ); |
| 972 | if( status == PSA_SUCCESS ) |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 973 | TEST_ASSERT( psa_destroy_key( handle ) == PSA_SUCCESS ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 974 | |
| 975 | exit: |
| 976 | mbedtls_free( buffer ); |
| 977 | mbedtls_psa_crypto_free( ); |
| 978 | } |
| 979 | /* END_CASE */ |
| 980 | |
| 981 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 982 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 983 | int type_arg, |
| 984 | int alg_arg, |
| 985 | int usage_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 986 | int expected_bits, |
| 987 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 988 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 989 | int canonical_input ) |
| 990 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 991 | psa_key_handle_t handle = 0; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 992 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 993 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 994 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 995 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 996 | unsigned char *exported = NULL; |
| 997 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 998 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 999 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1000 | size_t reexported_length; |
| 1001 | psa_key_type_t got_type; |
| 1002 | size_t got_bits; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1003 | psa_key_policy_t policy; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1004 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1005 | TEST_ASSERT( data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1006 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( data->len ) ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1007 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1008 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1009 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1010 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1011 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1012 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1013 | TEST_ASSERT( psa_allocate_key( type, expected_bits, &handle ) == |
| 1014 | PSA_SUCCESS ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1015 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1016 | psa_key_policy_set_usage( &policy, usage_arg, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1017 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
| 1018 | |
| 1019 | TEST_ASSERT( psa_get_key_information( |
| 1020 | handle, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1021 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1022 | /* Import the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1023 | TEST_ASSERT( psa_import_key( handle, type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1024 | data->x, data->len ) == PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1025 | |
| 1026 | /* Test the key information */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1027 | TEST_ASSERT( psa_get_key_information( handle, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1028 | &got_type, |
| 1029 | &got_bits ) == PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1030 | TEST_ASSERT( got_type == type ); |
| 1031 | TEST_ASSERT( got_bits == (size_t) expected_bits ); |
| 1032 | |
| 1033 | /* Export the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1034 | status = psa_export_key( handle, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1035 | exported, export_size, |
| 1036 | &exported_length ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1037 | TEST_ASSERT( status == expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1038 | |
| 1039 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1040 | * and export_size. On errors, the exported length must be 0. */ |
| 1041 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1042 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 1043 | TEST_ASSERT( exported_length <= export_size ); |
| 1044 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1045 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1046 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1047 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1048 | { |
| 1049 | TEST_ASSERT( exported_length == 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1050 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1051 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1052 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1053 | if( ! exercise_export_key( handle, usage_arg ) ) |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1054 | goto exit; |
| 1055 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1056 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1057 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1058 | else |
| 1059 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1060 | psa_key_handle_t handle2; |
| 1061 | TEST_ASSERT( psa_allocate_key( type, expected_bits, &handle2 ) == |
| 1062 | PSA_SUCCESS ); |
| 1063 | TEST_ASSERT( psa_set_key_policy( handle2, &policy ) == PSA_SUCCESS ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1064 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1065 | TEST_ASSERT( psa_import_key( handle2, type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1066 | exported, |
Gilles Peskine | b67f308 | 2018-08-07 15:33:10 +0200 | [diff] [blame] | 1067 | exported_length ) == PSA_SUCCESS ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1068 | TEST_ASSERT( psa_export_key( handle2, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1069 | reexported, |
| 1070 | export_size, |
| 1071 | &reexported_length ) == PSA_SUCCESS ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1072 | ASSERT_COMPARE( exported, exported_length, |
| 1073 | reexported, reexported_length ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1074 | TEST_ASSERT( psa_close_key( handle2 ) == PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1075 | } |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1076 | TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1077 | |
| 1078 | destroy: |
| 1079 | /* Destroy the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1080 | TEST_ASSERT( psa_destroy_key( handle ) == PSA_SUCCESS ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1081 | TEST_ASSERT( psa_get_key_information( |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1082 | handle, NULL, NULL ) == PSA_ERROR_INVALID_HANDLE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1083 | |
| 1084 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1085 | mbedtls_free( exported ); |
| 1086 | mbedtls_free( reexported ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1087 | mbedtls_psa_crypto_free( ); |
| 1088 | } |
| 1089 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1090 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1091 | /* BEGIN_CASE */ |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1092 | void import_key_nonempty_slot( ) |
| 1093 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1094 | psa_key_handle_t handle = 0; |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1095 | psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA; |
| 1096 | psa_status_t status; |
| 1097 | const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 }; |
| 1098 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1099 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1100 | TEST_ASSERT( psa_allocate_key( type, PSA_BYTES_TO_BITS( sizeof( data ) ), |
| 1101 | &handle ) == PSA_SUCCESS ); |
| 1102 | |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1103 | /* Import the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1104 | TEST_ASSERT( psa_import_key( handle, type, |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1105 | data, sizeof( data ) ) == PSA_SUCCESS ); |
| 1106 | |
| 1107 | /* Import the key again */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1108 | status = psa_import_key( handle, type, data, sizeof( data ) ); |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1109 | TEST_ASSERT( status == PSA_ERROR_OCCUPIED_SLOT ); |
| 1110 | |
| 1111 | exit: |
| 1112 | mbedtls_psa_crypto_free( ); |
| 1113 | } |
| 1114 | /* END_CASE */ |
| 1115 | |
| 1116 | /* BEGIN_CASE */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1117 | void export_invalid_handle( int handle, int expected_export_status_arg ) |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1118 | { |
| 1119 | psa_status_t status; |
| 1120 | unsigned char *exported = NULL; |
| 1121 | size_t export_size = 0; |
| 1122 | size_t exported_length = INVALID_EXPORT_LENGTH; |
| 1123 | psa_status_t expected_export_status = expected_export_status_arg; |
| 1124 | |
| 1125 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1126 | |
| 1127 | /* Export the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1128 | status = psa_export_key( (psa_key_handle_t) handle, |
Moran Peker | 28a38e6 | 2018-11-07 16:18:24 +0200 | [diff] [blame] | 1129 | exported, export_size, |
| 1130 | &exported_length ); |
| 1131 | TEST_ASSERT( status == expected_export_status ); |
| 1132 | |
| 1133 | exit: |
| 1134 | mbedtls_psa_crypto_free( ); |
| 1135 | } |
| 1136 | /* END_CASE */ |
| 1137 | |
| 1138 | /* BEGIN_CASE */ |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1139 | void export_with_no_key_activity( ) |
| 1140 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1141 | psa_key_handle_t handle = 0; |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1142 | psa_algorithm_t alg = PSA_ALG_CTR; |
| 1143 | psa_status_t status; |
| 1144 | psa_key_policy_t policy; |
| 1145 | unsigned char *exported = NULL; |
| 1146 | size_t export_size = 0; |
| 1147 | size_t exported_length = INVALID_EXPORT_LENGTH; |
| 1148 | |
| 1149 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1150 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1151 | TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0, |
| 1152 | &handle ) == PSA_SUCCESS ); |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1153 | psa_key_policy_init( &policy ); |
| 1154 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1155 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1156 | |
| 1157 | /* Export the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1158 | status = psa_export_key( handle, |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1159 | exported, export_size, |
| 1160 | &exported_length ); |
| 1161 | TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT ); |
| 1162 | |
| 1163 | exit: |
| 1164 | mbedtls_psa_crypto_free( ); |
| 1165 | } |
| 1166 | /* END_CASE */ |
| 1167 | |
| 1168 | /* BEGIN_CASE */ |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1169 | void cipher_with_no_key_activity( ) |
| 1170 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1171 | psa_key_handle_t handle = 0; |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1172 | psa_status_t status; |
| 1173 | psa_key_policy_t policy; |
| 1174 | psa_cipher_operation_t operation; |
| 1175 | int exercise_alg = PSA_ALG_CTR; |
| 1176 | |
| 1177 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1178 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1179 | TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, 0, |
| 1180 | &handle ) == PSA_SUCCESS ); |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1181 | psa_key_policy_init( &policy ); |
| 1182 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1183 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1184 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1185 | status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg ); |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1186 | TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT ); |
| 1187 | |
| 1188 | exit: |
| 1189 | psa_cipher_abort( &operation ); |
| 1190 | mbedtls_psa_crypto_free( ); |
| 1191 | } |
| 1192 | /* END_CASE */ |
| 1193 | |
| 1194 | /* BEGIN_CASE */ |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1195 | void export_after_import_failure( data_t *data, int type_arg, |
| 1196 | int expected_import_status_arg ) |
| 1197 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1198 | psa_key_handle_t handle = 0; |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1199 | psa_key_type_t type = type_arg; |
| 1200 | psa_status_t status; |
| 1201 | unsigned char *exported = NULL; |
| 1202 | size_t export_size = 0; |
| 1203 | psa_status_t expected_import_status = expected_import_status_arg; |
| 1204 | size_t exported_length = INVALID_EXPORT_LENGTH; |
| 1205 | |
| 1206 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1207 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1208 | TEST_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ), |
| 1209 | &handle ) == PSA_SUCCESS ); |
| 1210 | |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1211 | /* Import the key - expect failure */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1212 | status = psa_import_key( handle, type, |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1213 | data->x, data->len ); |
| 1214 | TEST_ASSERT( status == expected_import_status ); |
| 1215 | |
| 1216 | /* Export the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1217 | status = psa_export_key( handle, |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1218 | exported, export_size, |
| 1219 | &exported_length ); |
| 1220 | TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT ); |
| 1221 | |
| 1222 | exit: |
| 1223 | mbedtls_psa_crypto_free( ); |
| 1224 | } |
| 1225 | /* END_CASE */ |
| 1226 | |
| 1227 | /* BEGIN_CASE */ |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1228 | void cipher_after_import_failure( data_t *data, int type_arg, |
| 1229 | int expected_import_status_arg ) |
| 1230 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1231 | psa_key_handle_t handle = 0; |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1232 | psa_cipher_operation_t operation; |
| 1233 | psa_key_type_t type = type_arg; |
| 1234 | psa_status_t status; |
| 1235 | psa_status_t expected_import_status = expected_import_status_arg; |
| 1236 | int exercise_alg = PSA_ALG_CTR; |
| 1237 | |
| 1238 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1239 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1240 | TEST_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ), |
| 1241 | &handle ) == PSA_SUCCESS ); |
| 1242 | |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1243 | /* Import the key - expect failure */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1244 | status = psa_import_key( handle, type, |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1245 | data->x, data->len ); |
| 1246 | TEST_ASSERT( status == expected_import_status ); |
| 1247 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1248 | status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg ); |
Moran Peker | ce50007 | 2018-11-07 16:20:07 +0200 | [diff] [blame] | 1249 | TEST_ASSERT( status == PSA_ERROR_EMPTY_SLOT ); |
| 1250 | |
| 1251 | exit: |
| 1252 | psa_cipher_abort( &operation ); |
| 1253 | mbedtls_psa_crypto_free( ); |
| 1254 | } |
| 1255 | /* END_CASE */ |
| 1256 | |
| 1257 | /* BEGIN_CASE */ |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1258 | void export_after_destroy_key( data_t *data, int type_arg ) |
| 1259 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1260 | psa_key_handle_t handle = 0; |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1261 | psa_key_type_t type = type_arg; |
| 1262 | psa_status_t status; |
| 1263 | psa_key_policy_t policy; |
| 1264 | psa_algorithm_t alg = PSA_ALG_CTR; |
| 1265 | unsigned char *exported = NULL; |
| 1266 | size_t export_size = 0; |
| 1267 | size_t exported_length = INVALID_EXPORT_LENGTH; |
| 1268 | |
| 1269 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1270 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1271 | TEST_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ), |
| 1272 | &handle ) == PSA_SUCCESS ); |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1273 | psa_key_policy_init( &policy ); |
| 1274 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1275 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1276 | export_size = (ptrdiff_t) data->len; |
| 1277 | ASSERT_ALLOC( exported, export_size ); |
| 1278 | |
| 1279 | /* Import the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1280 | TEST_ASSERT( psa_import_key( handle, type, |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1281 | data->x, data->len ) == PSA_SUCCESS ); |
| 1282 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1283 | TEST_ASSERT( psa_export_key( handle, exported, export_size, |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1284 | &exported_length ) == PSA_SUCCESS ); |
| 1285 | |
| 1286 | /* Destroy the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1287 | TEST_ASSERT( psa_destroy_key( handle ) == PSA_SUCCESS ); |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1288 | |
| 1289 | /* Export the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1290 | status = psa_export_key( handle, exported, export_size, |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1291 | &exported_length ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1292 | TEST_ASSERT( status == PSA_ERROR_INVALID_HANDLE ); |
Moran Peker | 3455009 | 2018-11-07 16:19:34 +0200 | [diff] [blame] | 1293 | |
| 1294 | exit: |
| 1295 | mbedtls_free( exported ); |
| 1296 | mbedtls_psa_crypto_free( ); |
| 1297 | } |
| 1298 | /* END_CASE */ |
| 1299 | |
| 1300 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1301 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1302 | int type_arg, |
| 1303 | int alg_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1304 | int export_size_delta, |
| 1305 | int expected_export_status_arg, |
| 1306 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1307 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1308 | psa_key_handle_t handle = 0; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1309 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1310 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1311 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1312 | psa_status_t status; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1313 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1314 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1315 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 1316 | psa_key_policy_t policy; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1317 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1318 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1319 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1320 | TEST_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ), |
| 1321 | &handle ) == PSA_SUCCESS ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1322 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1323 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1324 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1325 | |
| 1326 | /* Import the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1327 | TEST_ASSERT( psa_import_key( handle, type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1328 | data->x, data->len ) == PSA_SUCCESS ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1329 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1330 | /* Export the public key */ |
| 1331 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1332 | status = psa_export_public_key( handle, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1333 | exported, export_size, |
| 1334 | &exported_length ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1335 | TEST_ASSERT( status == expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1336 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1337 | { |
| 1338 | psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type ); |
| 1339 | size_t bits; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1340 | TEST_ASSERT( psa_get_key_information( handle, NULL, &bits ) == |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1341 | PSA_SUCCESS ); |
| 1342 | TEST_ASSERT( expected_public_key->len <= |
| 1343 | PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1344 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1345 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1346 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1347 | |
| 1348 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1349 | mbedtls_free( exported ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1350 | psa_destroy_key( handle ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1351 | mbedtls_psa_crypto_free( ); |
| 1352 | } |
| 1353 | /* END_CASE */ |
| 1354 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1355 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1356 | void import_and_exercise_key( data_t *data, |
| 1357 | int type_arg, |
| 1358 | int bits_arg, |
| 1359 | int alg_arg ) |
| 1360 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1361 | psa_key_handle_t handle = 0; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1362 | psa_key_type_t type = type_arg; |
| 1363 | size_t bits = bits_arg; |
| 1364 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 10df341 | 2018-10-25 22:35:43 +0200 | [diff] [blame] | 1365 | psa_key_usage_t usage = usage_to_exercise( type, alg ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1366 | psa_key_policy_t policy; |
| 1367 | psa_key_type_t got_type; |
| 1368 | size_t got_bits; |
| 1369 | psa_status_t status; |
| 1370 | |
| 1371 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1372 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1373 | TEST_ASSERT( psa_allocate_key( type, KEY_BITS_FROM_DATA( type, data ), |
| 1374 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1375 | psa_key_policy_init( &policy ); |
| 1376 | psa_key_policy_set_usage( &policy, usage, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1377 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1378 | |
| 1379 | /* Import the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1380 | status = psa_import_key( handle, type, data->x, data->len ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1381 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 1382 | |
| 1383 | /* Test the key information */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1384 | TEST_ASSERT( psa_get_key_information( handle, |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1385 | &got_type, |
| 1386 | &got_bits ) == PSA_SUCCESS ); |
| 1387 | TEST_ASSERT( got_type == type ); |
| 1388 | TEST_ASSERT( got_bits == bits ); |
| 1389 | |
| 1390 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1391 | if( ! exercise_key( handle, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1392 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1393 | |
| 1394 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1395 | psa_destroy_key( handle ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1396 | mbedtls_psa_crypto_free( ); |
| 1397 | } |
| 1398 | /* END_CASE */ |
| 1399 | |
| 1400 | /* BEGIN_CASE */ |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1401 | void key_policy( int usage_arg, int alg_arg ) |
| 1402 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1403 | psa_key_handle_t handle = 0; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1404 | psa_algorithm_t alg = alg_arg; |
| 1405 | psa_key_usage_t usage = usage_arg; |
| 1406 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 1407 | unsigned char key[32] = {0}; |
| 1408 | psa_key_policy_t policy_set; |
| 1409 | psa_key_policy_t policy_get; |
| 1410 | |
| 1411 | memset( key, 0x2a, sizeof( key ) ); |
| 1412 | |
| 1413 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1414 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1415 | TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( sizeof( key ) ), |
| 1416 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1417 | psa_key_policy_init( &policy_set ); |
| 1418 | psa_key_policy_init( &policy_get ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1419 | psa_key_policy_set_usage( &policy_set, usage, alg ); |
| 1420 | |
| 1421 | TEST_ASSERT( psa_key_policy_get_usage( &policy_set ) == usage ); |
| 1422 | TEST_ASSERT( psa_key_policy_get_algorithm( &policy_set ) == alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1423 | TEST_ASSERT( psa_set_key_policy( handle, &policy_set ) == PSA_SUCCESS ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1424 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1425 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1426 | key, sizeof( key ) ) == PSA_SUCCESS ); |
| 1427 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1428 | TEST_ASSERT( psa_get_key_policy( handle, &policy_get ) == PSA_SUCCESS ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1429 | |
| 1430 | TEST_ASSERT( policy_get.usage == policy_set.usage ); |
| 1431 | TEST_ASSERT( policy_get.alg == policy_set.alg ); |
| 1432 | |
| 1433 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1434 | psa_destroy_key( handle ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1435 | mbedtls_psa_crypto_free( ); |
| 1436 | } |
| 1437 | /* END_CASE */ |
| 1438 | |
| 1439 | /* BEGIN_CASE */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1440 | void mac_key_policy( int policy_usage, |
| 1441 | int policy_alg, |
| 1442 | int key_type, |
| 1443 | data_t *key_data, |
| 1444 | int exercise_alg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1445 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1446 | psa_key_handle_t handle = 0; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1447 | psa_key_policy_t policy; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1448 | psa_mac_operation_t operation; |
| 1449 | psa_status_t status; |
| 1450 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1451 | |
| 1452 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1453 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1454 | TEST_ASSERT( psa_allocate_key( key_type, |
| 1455 | KEY_BITS_FROM_DATA( key_type, key_data ), |
| 1456 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1457 | psa_key_policy_init( &policy ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1458 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1459 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1460 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1461 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1462 | key_data->x, key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1463 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1464 | status = psa_mac_sign_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1465 | if( policy_alg == exercise_alg && |
| 1466 | ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 ) |
| 1467 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 1468 | else |
| 1469 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1470 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1471 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1472 | memset( mac, 0, sizeof( mac ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1473 | status = psa_mac_verify_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1474 | if( policy_alg == exercise_alg && |
| 1475 | ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 ) |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 1476 | TEST_ASSERT( status == PSA_SUCCESS ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1477 | else |
| 1478 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1479 | |
| 1480 | exit: |
| 1481 | psa_mac_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1482 | psa_destroy_key( handle ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1483 | mbedtls_psa_crypto_free( ); |
| 1484 | } |
| 1485 | /* END_CASE */ |
| 1486 | |
| 1487 | /* BEGIN_CASE */ |
| 1488 | void cipher_key_policy( int policy_usage, |
| 1489 | int policy_alg, |
| 1490 | int key_type, |
| 1491 | data_t *key_data, |
| 1492 | int exercise_alg ) |
| 1493 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1494 | psa_key_handle_t handle = 0; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1495 | psa_key_policy_t policy; |
| 1496 | psa_cipher_operation_t operation; |
| 1497 | psa_status_t status; |
| 1498 | |
| 1499 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1500 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1501 | TEST_ASSERT( psa_allocate_key( key_type, |
| 1502 | KEY_BITS_FROM_DATA( key_type, key_data ), |
| 1503 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1504 | psa_key_policy_init( &policy ); |
| 1505 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1506 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1507 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1508 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1509 | key_data->x, key_data->len ) == PSA_SUCCESS ); |
| 1510 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1511 | status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1512 | if( policy_alg == exercise_alg && |
| 1513 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1514 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 1515 | else |
| 1516 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1517 | psa_cipher_abort( &operation ); |
| 1518 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1519 | status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1520 | if( policy_alg == exercise_alg && |
| 1521 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
| 1522 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 1523 | else |
| 1524 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1525 | |
| 1526 | exit: |
| 1527 | psa_cipher_abort( &operation ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1528 | psa_destroy_key( handle ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1529 | mbedtls_psa_crypto_free( ); |
| 1530 | } |
| 1531 | /* END_CASE */ |
| 1532 | |
| 1533 | /* BEGIN_CASE */ |
| 1534 | void aead_key_policy( int policy_usage, |
| 1535 | int policy_alg, |
| 1536 | int key_type, |
| 1537 | data_t *key_data, |
| 1538 | int nonce_length_arg, |
| 1539 | int tag_length_arg, |
| 1540 | int exercise_alg ) |
| 1541 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1542 | psa_key_handle_t handle = 0; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1543 | psa_key_policy_t policy; |
| 1544 | psa_status_t status; |
| 1545 | unsigned char nonce[16] = {0}; |
| 1546 | size_t nonce_length = nonce_length_arg; |
| 1547 | unsigned char tag[16]; |
| 1548 | size_t tag_length = tag_length_arg; |
| 1549 | size_t output_length; |
| 1550 | |
| 1551 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 1552 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 1553 | |
| 1554 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1555 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1556 | TEST_ASSERT( psa_allocate_key( key_type, |
| 1557 | KEY_BITS_FROM_DATA( key_type, key_data ), |
| 1558 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1559 | psa_key_policy_init( &policy ); |
| 1560 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1561 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1562 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1563 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1564 | key_data->x, key_data->len ) == PSA_SUCCESS ); |
| 1565 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1566 | status = psa_aead_encrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1567 | nonce, nonce_length, |
| 1568 | NULL, 0, |
| 1569 | NULL, 0, |
| 1570 | tag, tag_length, |
| 1571 | &output_length ); |
| 1572 | if( policy_alg == exercise_alg && |
| 1573 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1574 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 1575 | else |
| 1576 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1577 | |
| 1578 | memset( tag, 0, sizeof( tag ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1579 | status = psa_aead_decrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1580 | nonce, nonce_length, |
| 1581 | NULL, 0, |
| 1582 | tag, tag_length, |
| 1583 | NULL, 0, |
| 1584 | &output_length ); |
| 1585 | if( policy_alg == exercise_alg && |
| 1586 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
| 1587 | TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE ); |
| 1588 | else |
| 1589 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1590 | |
| 1591 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1592 | psa_destroy_key( handle ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1593 | mbedtls_psa_crypto_free( ); |
| 1594 | } |
| 1595 | /* END_CASE */ |
| 1596 | |
| 1597 | /* BEGIN_CASE */ |
| 1598 | void asymmetric_encryption_key_policy( int policy_usage, |
| 1599 | int policy_alg, |
| 1600 | int key_type, |
| 1601 | data_t *key_data, |
| 1602 | int exercise_alg ) |
| 1603 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1604 | psa_key_handle_t handle = 0; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1605 | psa_key_policy_t policy; |
| 1606 | psa_status_t status; |
| 1607 | size_t key_bits; |
| 1608 | size_t buffer_length; |
| 1609 | unsigned char *buffer = NULL; |
| 1610 | size_t output_length; |
| 1611 | |
| 1612 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1613 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1614 | TEST_ASSERT( psa_allocate_key( key_type, |
| 1615 | KEY_BITS_FROM_DATA( key_type, key_data ), |
| 1616 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1617 | psa_key_policy_init( &policy ); |
| 1618 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1619 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1620 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1621 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1622 | key_data->x, key_data->len ) == PSA_SUCCESS ); |
| 1623 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1624 | TEST_ASSERT( psa_get_key_information( handle, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1625 | NULL, |
| 1626 | &key_bits ) == PSA_SUCCESS ); |
| 1627 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 1628 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1629 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1630 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1631 | status = psa_asymmetric_encrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1632 | NULL, 0, |
| 1633 | NULL, 0, |
| 1634 | buffer, buffer_length, |
| 1635 | &output_length ); |
| 1636 | if( policy_alg == exercise_alg && |
| 1637 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1638 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 1639 | else |
| 1640 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1641 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 1642 | if( buffer_length != 0 ) |
| 1643 | memset( buffer, 0, buffer_length ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1644 | status = psa_asymmetric_decrypt( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1645 | buffer, buffer_length, |
| 1646 | NULL, 0, |
| 1647 | buffer, buffer_length, |
| 1648 | &output_length ); |
| 1649 | if( policy_alg == exercise_alg && |
| 1650 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
| 1651 | TEST_ASSERT( status == PSA_ERROR_INVALID_PADDING ); |
| 1652 | else |
| 1653 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1654 | |
| 1655 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1656 | psa_destroy_key( handle ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1657 | mbedtls_psa_crypto_free( ); |
| 1658 | mbedtls_free( buffer ); |
| 1659 | } |
| 1660 | /* END_CASE */ |
| 1661 | |
| 1662 | /* BEGIN_CASE */ |
| 1663 | void asymmetric_signature_key_policy( int policy_usage, |
| 1664 | int policy_alg, |
| 1665 | int key_type, |
| 1666 | data_t *key_data, |
| 1667 | int exercise_alg ) |
| 1668 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1669 | psa_key_handle_t handle = 0; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1670 | psa_key_policy_t policy; |
| 1671 | psa_status_t status; |
| 1672 | unsigned char payload[16] = {1}; |
| 1673 | size_t payload_length = sizeof( payload ); |
| 1674 | unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0}; |
| 1675 | size_t signature_length; |
| 1676 | |
| 1677 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1678 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1679 | TEST_ASSERT( psa_allocate_key( key_type, |
| 1680 | KEY_BITS_FROM_DATA( key_type, key_data ), |
| 1681 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1682 | psa_key_policy_init( &policy ); |
| 1683 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1684 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1685 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1686 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1687 | key_data->x, key_data->len ) == PSA_SUCCESS ); |
| 1688 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1689 | status = psa_asymmetric_sign( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1690 | payload, payload_length, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1691 | signature, sizeof( signature ), |
| 1692 | &signature_length ); |
| 1693 | if( policy_alg == exercise_alg && |
| 1694 | ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 ) |
| 1695 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 1696 | else |
| 1697 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1698 | |
| 1699 | memset( signature, 0, sizeof( signature ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1700 | status = psa_asymmetric_verify( handle, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1701 | payload, payload_length, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1702 | signature, sizeof( signature ) ); |
| 1703 | if( policy_alg == exercise_alg && |
| 1704 | ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 ) |
| 1705 | TEST_ASSERT( status == PSA_ERROR_INVALID_SIGNATURE ); |
| 1706 | else |
| 1707 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1708 | |
| 1709 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1710 | psa_destroy_key( handle ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1711 | mbedtls_psa_crypto_free( ); |
| 1712 | } |
| 1713 | /* END_CASE */ |
| 1714 | |
| 1715 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1716 | void derive_key_policy( int policy_usage, |
| 1717 | int policy_alg, |
| 1718 | int key_type, |
| 1719 | data_t *key_data, |
| 1720 | int exercise_alg ) |
| 1721 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1722 | psa_key_handle_t handle = 0; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1723 | psa_key_policy_t policy; |
| 1724 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 1725 | psa_status_t status; |
| 1726 | |
| 1727 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1728 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1729 | TEST_ASSERT( psa_allocate_key( key_type, |
| 1730 | KEY_BITS_FROM_DATA( key_type, key_data ), |
| 1731 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1732 | psa_key_policy_init( &policy ); |
| 1733 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1734 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1735 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1736 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1737 | key_data->x, key_data->len ) == PSA_SUCCESS ); |
| 1738 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1739 | status = psa_key_derivation( &generator, handle, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1740 | exercise_alg, |
| 1741 | NULL, 0, |
| 1742 | NULL, 0, |
| 1743 | 1 ); |
| 1744 | if( policy_alg == exercise_alg && |
| 1745 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
| 1746 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 1747 | else |
| 1748 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1749 | |
| 1750 | exit: |
| 1751 | psa_generator_abort( &generator ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1752 | psa_destroy_key( handle ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1753 | mbedtls_psa_crypto_free( ); |
| 1754 | } |
| 1755 | /* END_CASE */ |
| 1756 | |
| 1757 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1758 | void agreement_key_policy( int policy_usage, |
| 1759 | int policy_alg, |
| 1760 | int key_type_arg, |
| 1761 | data_t *key_data, |
| 1762 | int exercise_alg ) |
| 1763 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1764 | psa_key_handle_t handle = 0; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1765 | psa_key_policy_t policy; |
| 1766 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1767 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 1768 | psa_status_t status; |
| 1769 | |
| 1770 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1771 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1772 | TEST_ASSERT( psa_allocate_key( key_type, |
| 1773 | KEY_BITS_FROM_DATA( key_type, key_data ), |
| 1774 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1775 | psa_key_policy_init( &policy ); |
| 1776 | psa_key_policy_set_usage( &policy, policy_usage, policy_alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1777 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1778 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1779 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1780 | key_data->x, key_data->len ) == PSA_SUCCESS ); |
| 1781 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1782 | status = key_agreement_with_self( &generator, handle, exercise_alg ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1783 | |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1784 | if( policy_alg == exercise_alg && |
| 1785 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
| 1786 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 1787 | else |
| 1788 | TEST_ASSERT( status == PSA_ERROR_NOT_PERMITTED ); |
| 1789 | |
| 1790 | exit: |
| 1791 | psa_generator_abort( &generator ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1792 | psa_destroy_key( handle ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1793 | mbedtls_psa_crypto_free( ); |
| 1794 | } |
| 1795 | /* END_CASE */ |
| 1796 | |
| 1797 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1798 | void hash_setup( int alg_arg, |
| 1799 | int expected_status_arg ) |
| 1800 | { |
| 1801 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1802 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1803 | psa_hash_operation_t operation; |
| 1804 | psa_status_t status; |
| 1805 | |
| 1806 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1807 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1808 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1809 | psa_hash_abort( &operation ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1810 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1811 | |
| 1812 | exit: |
| 1813 | mbedtls_psa_crypto_free( ); |
| 1814 | } |
| 1815 | /* END_CASE */ |
| 1816 | |
| 1817 | /* BEGIN_CASE */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1818 | void hash_bad_order( ) |
| 1819 | { |
| 1820 | unsigned char input[] = ""; |
| 1821 | /* SHA-256 hash of an empty string */ |
| 1822 | unsigned char hash[] = { |
| 1823 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1824 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 1825 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
| 1826 | size_t hash_len; |
| 1827 | psa_hash_operation_t operation; |
| 1828 | |
| 1829 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1830 | |
| 1831 | /* psa_hash_update without calling psa_hash_setup beforehand */ |
| 1832 | memset( &operation, 0, sizeof( operation ) ); |
| 1833 | TEST_ASSERT( psa_hash_update( &operation, |
| 1834 | input, sizeof( input ) ) == |
| 1835 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1836 | |
| 1837 | /* psa_hash_verify without calling psa_hash_setup beforehand */ |
| 1838 | memset( &operation, 0, sizeof( operation ) ); |
| 1839 | TEST_ASSERT( psa_hash_verify( &operation, |
| 1840 | hash, sizeof( hash ) ) == |
| 1841 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1842 | |
| 1843 | /* psa_hash_finish without calling psa_hash_setup beforehand */ |
| 1844 | memset( &operation, 0, sizeof( operation ) ); |
| 1845 | TEST_ASSERT( psa_hash_finish( &operation, |
| 1846 | hash, sizeof( hash ), &hash_len ) == |
| 1847 | PSA_ERROR_INVALID_ARGUMENT ); |
| 1848 | |
| 1849 | exit: |
| 1850 | mbedtls_psa_crypto_free( ); |
| 1851 | } |
| 1852 | /* END_CASE */ |
| 1853 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1854 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 1855 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1856 | { |
| 1857 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1858 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 1859 | * appended to it */ |
| 1860 | unsigned char hash[] = { |
| 1861 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1862 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 1863 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1864 | size_t expected_size = PSA_HASH_SIZE( alg ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1865 | psa_hash_operation_t operation; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1866 | |
| 1867 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1868 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1869 | /* psa_hash_verify with a smaller hash than expected */ |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1870 | TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS ); |
| 1871 | TEST_ASSERT( psa_hash_verify( &operation, |
| 1872 | hash, expected_size - 1 ) == |
| 1873 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1874 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1875 | /* psa_hash_verify with a non-matching hash */ |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1876 | TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1877 | TEST_ASSERT( psa_hash_verify( &operation, |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1878 | hash + 1, expected_size ) == |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1879 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1880 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1881 | /* psa_hash_verify with a hash longer than expected */ |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 1882 | TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 1883 | TEST_ASSERT( psa_hash_verify( &operation, |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 1884 | hash, sizeof( hash ) ) == |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 1885 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1886 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 1887 | exit: |
| 1888 | mbedtls_psa_crypto_free( ); |
| 1889 | } |
| 1890 | /* END_CASE */ |
| 1891 | |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1892 | /* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */ |
| 1893 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1894 | { |
| 1895 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 1896 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1897 | size_t expected_size = PSA_HASH_SIZE( alg ); |
| 1898 | psa_hash_operation_t operation; |
| 1899 | size_t hash_len; |
| 1900 | |
| 1901 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1902 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 1903 | /* psa_hash_finish with a smaller hash buffer than expected */ |
| 1904 | TEST_ASSERT( psa_hash_setup( &operation, alg ) == PSA_SUCCESS ); |
| 1905 | TEST_ASSERT( psa_hash_finish( &operation, |
| 1906 | hash, expected_size - 1, |
| 1907 | &hash_len ) == PSA_ERROR_BUFFER_TOO_SMALL ); |
| 1908 | |
| 1909 | exit: |
| 1910 | mbedtls_psa_crypto_free( ); |
| 1911 | } |
| 1912 | /* END_CASE */ |
| 1913 | |
| 1914 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1915 | void mac_setup( int key_type_arg, |
| 1916 | data_t *key, |
| 1917 | int alg_arg, |
| 1918 | int expected_status_arg ) |
| 1919 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1920 | psa_key_handle_t handle = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1921 | psa_key_type_t key_type = key_type_arg; |
| 1922 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1923 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1924 | psa_mac_operation_t operation; |
| 1925 | psa_key_policy_t policy; |
| 1926 | psa_status_t status; |
| 1927 | |
| 1928 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1929 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1930 | TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ), |
| 1931 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1932 | psa_key_policy_init( &policy ); |
| 1933 | psa_key_policy_set_usage( &policy, |
| 1934 | PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY, |
| 1935 | alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1936 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1937 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1938 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1939 | key->x, key->len ) == PSA_SUCCESS ); |
| 1940 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1941 | status = psa_mac_sign_setup( &operation, handle, alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1942 | psa_mac_abort( &operation ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1943 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1944 | |
| 1945 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1946 | psa_destroy_key( handle ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1947 | mbedtls_psa_crypto_free( ); |
| 1948 | } |
| 1949 | /* END_CASE */ |
| 1950 | |
| 1951 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1952 | void mac_sign( int key_type_arg, |
| 1953 | data_t *key, |
| 1954 | int alg_arg, |
| 1955 | data_t *input, |
| 1956 | data_t *expected_mac ) |
| 1957 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1958 | psa_key_handle_t handle = 0; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1959 | psa_key_type_t key_type = key_type_arg; |
| 1960 | psa_algorithm_t alg = alg_arg; |
| 1961 | psa_mac_operation_t operation; |
| 1962 | psa_key_policy_t policy; |
| 1963 | /* Leave a little extra room in the output buffer. At the end of the |
| 1964 | * test, we'll check that the implementation didn't overwrite onto |
| 1965 | * this extra room. */ |
| 1966 | uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10]; |
| 1967 | size_t mac_buffer_size = |
| 1968 | PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg ); |
| 1969 | size_t mac_length = 0; |
| 1970 | |
| 1971 | memset( actual_mac, '+', sizeof( actual_mac ) ); |
| 1972 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
| 1973 | TEST_ASSERT( expected_mac->len <= mac_buffer_size ); |
| 1974 | |
| 1975 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 1976 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1977 | TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ), |
| 1978 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1979 | psa_key_policy_init( &policy ); |
| 1980 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1981 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1982 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1983 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1984 | key->x, key->len ) == PSA_SUCCESS ); |
| 1985 | |
| 1986 | /* Calculate the MAC. */ |
| 1987 | TEST_ASSERT( psa_mac_sign_setup( &operation, |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 1988 | handle, alg ) == PSA_SUCCESS ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1989 | TEST_ASSERT( psa_mac_update( &operation, |
| 1990 | input->x, input->len ) == PSA_SUCCESS ); |
| 1991 | TEST_ASSERT( psa_mac_sign_finish( &operation, |
| 1992 | actual_mac, mac_buffer_size, |
| 1993 | &mac_length ) == PSA_SUCCESS ); |
| 1994 | |
| 1995 | /* Compare with the expected value. */ |
| 1996 | TEST_ASSERT( mac_length == expected_mac->len ); |
| 1997 | TEST_ASSERT( memcmp( actual_mac, expected_mac->x, mac_length ) == 0 ); |
| 1998 | |
| 1999 | /* Verify that the end of the buffer is untouched. */ |
| 2000 | TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+', |
| 2001 | sizeof( actual_mac ) - mac_length ) ); |
| 2002 | |
| 2003 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2004 | psa_destroy_key( handle ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2005 | mbedtls_psa_crypto_free( ); |
| 2006 | } |
| 2007 | /* END_CASE */ |
| 2008 | |
| 2009 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2010 | void mac_verify( int key_type_arg, |
| 2011 | data_t *key, |
| 2012 | int alg_arg, |
| 2013 | data_t *input, |
| 2014 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2015 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2016 | psa_key_handle_t handle = 0; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2017 | psa_key_type_t key_type = key_type_arg; |
| 2018 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2019 | psa_mac_operation_t operation; |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2020 | psa_key_policy_t policy; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2021 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2022 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 2023 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2024 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2025 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2026 | TEST_ASSERT( expected_mac != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2027 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2028 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 2029 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2030 | |
| 2031 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2032 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2033 | TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ), |
| 2034 | &handle ) == PSA_SUCCESS ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2035 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2036 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2037 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2038 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2039 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2040 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2041 | |
Gilles Peskine | 89167cb | 2018-07-08 20:12:23 +0200 | [diff] [blame] | 2042 | TEST_ASSERT( psa_mac_verify_setup( &operation, |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2043 | handle, alg ) == PSA_SUCCESS ); |
| 2044 | TEST_ASSERT( psa_destroy_key( handle ) == PSA_SUCCESS ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2045 | TEST_ASSERT( psa_mac_update( &operation, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2046 | input->x, input->len ) == PSA_SUCCESS ); |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 2047 | TEST_ASSERT( psa_mac_verify_finish( &operation, |
| 2048 | expected_mac->x, |
| 2049 | expected_mac->len ) == PSA_SUCCESS ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2050 | |
| 2051 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2052 | psa_destroy_key( handle ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2053 | mbedtls_psa_crypto_free( ); |
| 2054 | } |
| 2055 | /* END_CASE */ |
| 2056 | |
| 2057 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2058 | void cipher_setup( int key_type_arg, |
| 2059 | data_t *key, |
| 2060 | int alg_arg, |
| 2061 | int expected_status_arg ) |
| 2062 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2063 | psa_key_handle_t handle = 0; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2064 | psa_key_type_t key_type = key_type_arg; |
| 2065 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2066 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2067 | psa_cipher_operation_t operation; |
| 2068 | psa_key_policy_t policy; |
| 2069 | psa_status_t status; |
| 2070 | |
| 2071 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2072 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2073 | TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ), |
| 2074 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2075 | psa_key_policy_init( &policy ); |
| 2076 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2077 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2078 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2079 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2080 | key->x, key->len ) == PSA_SUCCESS ); |
| 2081 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2082 | status = psa_cipher_encrypt_setup( &operation, handle, alg ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2083 | psa_cipher_abort( &operation ); |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2084 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2085 | |
| 2086 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2087 | psa_destroy_key( handle ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2088 | mbedtls_psa_crypto_free( ); |
| 2089 | } |
| 2090 | /* END_CASE */ |
| 2091 | |
| 2092 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2093 | void cipher_encrypt( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2094 | data_t *key, |
| 2095 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2096 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2097 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2098 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2099 | psa_status_t status; |
| 2100 | psa_key_type_t key_type = key_type_arg; |
| 2101 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2102 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2103 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2104 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2105 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2106 | size_t output_buffer_size = 0; |
| 2107 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2108 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2109 | psa_cipher_operation_t operation; |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2110 | psa_key_policy_t policy; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2111 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2112 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2113 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2114 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2115 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 2116 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 2117 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2118 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2119 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 2120 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2121 | |
| 2122 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2123 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2124 | TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ), |
| 2125 | &handle ) == PSA_SUCCESS ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2126 | psa_key_policy_init( &policy ); |
| 2127 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2128 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2129 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2130 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2131 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2132 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2133 | TEST_ASSERT( psa_cipher_encrypt_setup( &operation, |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2134 | handle, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2135 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2136 | TEST_ASSERT( psa_cipher_set_iv( &operation, |
| 2137 | iv, iv_size ) == PSA_SUCCESS ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame^] | 2138 | output_buffer_size = ( (size_t) input->len + |
| 2139 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2140 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2141 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2142 | TEST_ASSERT( psa_cipher_update( &operation, |
| 2143 | input->x, input->len, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2144 | output, output_buffer_size, |
| 2145 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2146 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2147 | status = psa_cipher_finish( &operation, |
| 2148 | output + function_output_length, |
| 2149 | output_buffer_size, |
| 2150 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2151 | total_output_length += function_output_length; |
| 2152 | |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2153 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2154 | if( expected_status == PSA_SUCCESS ) |
| 2155 | { |
| 2156 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2157 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2158 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2159 | } |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2160 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2161 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2162 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2163 | psa_destroy_key( handle ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2164 | mbedtls_psa_crypto_free( ); |
| 2165 | } |
| 2166 | /* END_CASE */ |
| 2167 | |
| 2168 | /* BEGIN_CASE */ |
| 2169 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2170 | data_t *key, |
| 2171 | data_t *input, |
| 2172 | int first_part_size, |
| 2173 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2174 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2175 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2176 | psa_key_type_t key_type = key_type_arg; |
| 2177 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2178 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2179 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2180 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2181 | size_t output_buffer_size = 0; |
| 2182 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2183 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2184 | psa_cipher_operation_t operation; |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2185 | psa_key_policy_t policy; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2186 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2187 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2188 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2189 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2190 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 2191 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 2192 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2193 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2194 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 2195 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2196 | |
| 2197 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2198 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2199 | TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ), |
| 2200 | &handle ) == PSA_SUCCESS ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2201 | psa_key_policy_init( &policy ); |
| 2202 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2203 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2204 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2205 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2206 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2207 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2208 | TEST_ASSERT( psa_cipher_encrypt_setup( &operation, |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2209 | handle, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2210 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2211 | TEST_ASSERT( psa_cipher_set_iv( &operation, |
| 2212 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame^] | 2213 | output_buffer_size = ( (size_t) input->len + |
| 2214 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2215 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2216 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2217 | TEST_ASSERT( (unsigned int) first_part_size < input->len ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2218 | TEST_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2219 | output, output_buffer_size, |
| 2220 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2221 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2222 | TEST_ASSERT( psa_cipher_update( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2223 | input->x + first_part_size, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2224 | input->len - first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2225 | output, output_buffer_size, |
| 2226 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2227 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2228 | TEST_ASSERT( psa_cipher_finish( &operation, |
| 2229 | output + function_output_length, |
| 2230 | output_buffer_size, |
| 2231 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2232 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2233 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 2234 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2235 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2236 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2237 | |
| 2238 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2239 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2240 | psa_destroy_key( handle ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2241 | mbedtls_psa_crypto_free( ); |
| 2242 | } |
| 2243 | /* END_CASE */ |
| 2244 | |
| 2245 | /* BEGIN_CASE */ |
| 2246 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2247 | data_t *key, |
| 2248 | data_t *input, |
| 2249 | int first_part_size, |
| 2250 | data_t *expected_output ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2251 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2252 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2253 | |
| 2254 | psa_key_type_t key_type = key_type_arg; |
| 2255 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2256 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2257 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2258 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2259 | size_t output_buffer_size = 0; |
| 2260 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2261 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2262 | psa_cipher_operation_t operation; |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2263 | psa_key_policy_t policy; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2264 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2265 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2266 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2267 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2268 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 2269 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 2270 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2271 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2272 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 2273 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2274 | |
| 2275 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2276 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2277 | TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ), |
| 2278 | &handle ) == PSA_SUCCESS ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2279 | psa_key_policy_init( &policy ); |
| 2280 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2281 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2282 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2283 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2284 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2285 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2286 | TEST_ASSERT( psa_cipher_decrypt_setup( &operation, |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2287 | handle, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2288 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2289 | TEST_ASSERT( psa_cipher_set_iv( &operation, |
| 2290 | iv, sizeof( iv ) ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2291 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame^] | 2292 | output_buffer_size = ( (size_t) input->len + |
| 2293 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2294 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2295 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2296 | TEST_ASSERT( (unsigned int) first_part_size < input->len ); |
| 2297 | TEST_ASSERT( psa_cipher_update( &operation, |
| 2298 | input->x, first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2299 | output, output_buffer_size, |
| 2300 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2301 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2302 | TEST_ASSERT( psa_cipher_update( &operation, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2303 | input->x + first_part_size, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2304 | input->len - first_part_size, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2305 | output, output_buffer_size, |
| 2306 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2307 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2308 | TEST_ASSERT( psa_cipher_finish( &operation, |
| 2309 | output + function_output_length, |
| 2310 | output_buffer_size, |
| 2311 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2312 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2313 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
| 2314 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2315 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2316 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2317 | |
| 2318 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2319 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2320 | psa_destroy_key( handle ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2321 | mbedtls_psa_crypto_free( ); |
| 2322 | } |
| 2323 | /* END_CASE */ |
| 2324 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2325 | /* BEGIN_CASE */ |
| 2326 | void cipher_decrypt( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2327 | data_t *key, |
| 2328 | data_t *input, data_t *expected_output, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2329 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2330 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2331 | psa_key_handle_t handle = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2332 | psa_status_t status; |
| 2333 | psa_key_type_t key_type = key_type_arg; |
| 2334 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2335 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2336 | unsigned char iv[16] = {0}; |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2337 | size_t iv_size; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2338 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2339 | size_t output_buffer_size = 0; |
| 2340 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2341 | size_t total_output_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2342 | psa_cipher_operation_t operation; |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2343 | psa_key_policy_t policy; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2344 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2345 | TEST_ASSERT( key != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2346 | TEST_ASSERT( input != NULL ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2347 | TEST_ASSERT( expected_output != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2348 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 2349 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
| 2350 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_output->len ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2351 | |
Gilles Peskine | 9ad29e2 | 2018-06-21 09:40:04 +0200 | [diff] [blame] | 2352 | iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ); |
| 2353 | memset( iv, 0x2a, iv_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2354 | |
| 2355 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2356 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2357 | TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ), |
| 2358 | &handle ) == PSA_SUCCESS ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2359 | psa_key_policy_init( &policy ); |
| 2360 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2361 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2362 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2363 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2364 | key->x, key->len ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2365 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2366 | TEST_ASSERT( psa_cipher_decrypt_setup( &operation, |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2367 | handle, alg ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2368 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2369 | TEST_ASSERT( psa_cipher_set_iv( &operation, |
| 2370 | iv, iv_size ) == PSA_SUCCESS ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2371 | |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame^] | 2372 | output_buffer_size = ( (size_t) input->len + |
| 2373 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2374 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2375 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2376 | TEST_ASSERT( psa_cipher_update( &operation, |
| 2377 | input->x, input->len, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2378 | output, output_buffer_size, |
| 2379 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2380 | total_output_length += function_output_length; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2381 | status = psa_cipher_finish( &operation, |
| 2382 | output + function_output_length, |
| 2383 | output_buffer_size, |
| 2384 | &function_output_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 2385 | total_output_length += function_output_length; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2386 | TEST_ASSERT( status == expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2387 | |
| 2388 | if( expected_status == PSA_SUCCESS ) |
| 2389 | { |
| 2390 | TEST_ASSERT( psa_cipher_abort( &operation ) == PSA_SUCCESS ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2391 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2392 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2393 | } |
| 2394 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2395 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2396 | mbedtls_free( output ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2397 | psa_destroy_key( handle ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2398 | mbedtls_psa_crypto_free( ); |
| 2399 | } |
| 2400 | /* END_CASE */ |
| 2401 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2402 | /* BEGIN_CASE */ |
| 2403 | void cipher_verify_output( int alg_arg, int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2404 | data_t *key, |
| 2405 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2406 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2407 | psa_key_handle_t handle = 0; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2408 | psa_key_type_t key_type = key_type_arg; |
| 2409 | psa_algorithm_t alg = alg_arg; |
mohammad1603 | e6b67a1 | 2018-03-12 10:38:49 -0700 | [diff] [blame] | 2410 | unsigned char iv[16] = {0}; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2411 | size_t iv_size = 16; |
| 2412 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2413 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2414 | size_t output1_size = 0; |
| 2415 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2416 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2417 | size_t output2_size = 0; |
| 2418 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2419 | size_t function_output_length = 0; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2420 | psa_cipher_operation_t operation1; |
| 2421 | psa_cipher_operation_t operation2; |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2422 | psa_key_policy_t policy; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2423 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2424 | TEST_ASSERT( key != NULL ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2425 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2426 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 2427 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2428 | |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2429 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2430 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2431 | TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ), |
| 2432 | &handle ) == PSA_SUCCESS ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2433 | psa_key_policy_init( &policy ); |
| 2434 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2435 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2436 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2437 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2438 | key->x, key->len ) == PSA_SUCCESS ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2439 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2440 | TEST_ASSERT( psa_cipher_encrypt_setup( &operation1, |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2441 | handle, alg ) == PSA_SUCCESS ); |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2442 | TEST_ASSERT( psa_cipher_decrypt_setup( &operation2, |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2443 | handle, alg ) == PSA_SUCCESS ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2444 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2445 | TEST_ASSERT( psa_cipher_generate_iv( &operation1, |
| 2446 | iv, iv_size, |
| 2447 | &iv_length ) == PSA_SUCCESS ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame^] | 2448 | output1_size = ( (size_t) input->len + |
| 2449 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2450 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2451 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2452 | TEST_ASSERT( psa_cipher_update( &operation1, input->x, input->len, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2453 | output1, output1_size, |
| 2454 | &output1_length ) == PSA_SUCCESS ); |
| 2455 | TEST_ASSERT( psa_cipher_finish( &operation1, |
| 2456 | output1 + output1_length, output1_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2457 | &function_output_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2458 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2459 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2460 | |
| 2461 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 2462 | |
| 2463 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2464 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2465 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2466 | TEST_ASSERT( psa_cipher_set_iv( &operation2, |
| 2467 | iv, iv_length ) == PSA_SUCCESS ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2468 | TEST_ASSERT( psa_cipher_update( &operation2, output1, output1_length, |
| 2469 | output2, output2_size, |
| 2470 | &output2_length ) == PSA_SUCCESS ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2471 | function_output_length = 0; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2472 | TEST_ASSERT( psa_cipher_finish( &operation2, |
| 2473 | output2 + output2_length, |
| 2474 | output2_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2475 | &function_output_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2476 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2477 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2478 | |
Janos Follath | 25c4fa8 | 2018-07-06 16:23:25 +0100 | [diff] [blame] | 2479 | TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2480 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2481 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2482 | |
| 2483 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2484 | mbedtls_free( output1 ); |
| 2485 | mbedtls_free( output2 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2486 | psa_destroy_key( handle ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2487 | mbedtls_psa_crypto_free( ); |
| 2488 | } |
| 2489 | /* END_CASE */ |
| 2490 | |
| 2491 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2492 | void cipher_verify_output_multipart( int alg_arg, |
| 2493 | int key_type_arg, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2494 | data_t *key, |
| 2495 | data_t *input, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2496 | int first_part_size ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2497 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2498 | psa_key_handle_t handle = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2499 | psa_key_type_t key_type = key_type_arg; |
| 2500 | psa_algorithm_t alg = alg_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2501 | unsigned char iv[16] = {0}; |
| 2502 | size_t iv_size = 16; |
| 2503 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2504 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2505 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2506 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2507 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2508 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2509 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2510 | size_t function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2511 | psa_cipher_operation_t operation1; |
| 2512 | psa_cipher_operation_t operation2; |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2513 | psa_key_policy_t policy; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2514 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2515 | TEST_ASSERT( key != NULL ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2516 | TEST_ASSERT( input != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2517 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key->len ) ); |
| 2518 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input->len ) ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2519 | |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2520 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2521 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2522 | TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key->len ), |
| 2523 | &handle ) == PSA_SUCCESS ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2524 | psa_key_policy_init( &policy ); |
| 2525 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2526 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 2527 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2528 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2529 | key->x, key->len ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2530 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2531 | TEST_ASSERT( psa_cipher_encrypt_setup( &operation1, |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2532 | handle, alg ) == PSA_SUCCESS ); |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2533 | TEST_ASSERT( psa_cipher_decrypt_setup( &operation2, |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2534 | handle, alg ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2535 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2536 | TEST_ASSERT( psa_cipher_generate_iv( &operation1, |
| 2537 | iv, iv_size, |
| 2538 | &iv_length ) == PSA_SUCCESS ); |
Gilles Peskine | 9d8eea7 | 2018-12-17 23:34:57 +0100 | [diff] [blame^] | 2539 | output1_buffer_size = ( (size_t) input->len + |
| 2540 | PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2541 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2542 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2543 | TEST_ASSERT( (unsigned int) first_part_size < input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2544 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2545 | TEST_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2546 | output1, output1_buffer_size, |
| 2547 | &function_output_length ) == PSA_SUCCESS ); |
| 2548 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2549 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2550 | TEST_ASSERT( psa_cipher_update( &operation1, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2551 | input->x + first_part_size, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2552 | input->len - first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2553 | output1, output1_buffer_size, |
| 2554 | &function_output_length ) == PSA_SUCCESS ); |
| 2555 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2556 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2557 | TEST_ASSERT( psa_cipher_finish( &operation1, |
| 2558 | output1 + output1_length, |
| 2559 | output1_buffer_size - output1_length, |
| 2560 | &function_output_length ) == PSA_SUCCESS ); |
| 2561 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2562 | |
| 2563 | TEST_ASSERT( psa_cipher_abort( &operation1 ) == PSA_SUCCESS ); |
| 2564 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2565 | output2_buffer_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2566 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2567 | |
Gilles Peskine | fe11951 | 2018-07-08 21:39:34 +0200 | [diff] [blame] | 2568 | TEST_ASSERT( psa_cipher_set_iv( &operation2, |
| 2569 | iv, iv_length ) == PSA_SUCCESS ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2570 | |
| 2571 | TEST_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2572 | output2, output2_buffer_size, |
| 2573 | &function_output_length ) == PSA_SUCCESS ); |
| 2574 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2575 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2576 | TEST_ASSERT( psa_cipher_update( &operation2, |
| 2577 | output1 + first_part_size, |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2578 | output1_length - first_part_size, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2579 | output2, output2_buffer_size, |
| 2580 | &function_output_length ) == PSA_SUCCESS ); |
| 2581 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 2582 | |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2583 | TEST_ASSERT( psa_cipher_finish( &operation2, |
| 2584 | output2 + output2_length, |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 2585 | output2_buffer_size - output2_length, |
| 2586 | &function_output_length ) == PSA_SUCCESS ); |
| 2587 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 2588 | |
Janos Follath | 25c4fa8 | 2018-07-06 16:23:25 +0100 | [diff] [blame] | 2589 | TEST_ASSERT( psa_cipher_abort( &operation2 ) == PSA_SUCCESS ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2590 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2591 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2592 | |
| 2593 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2594 | mbedtls_free( output1 ); |
| 2595 | mbedtls_free( output2 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2596 | psa_destroy_key( handle ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 2597 | mbedtls_psa_crypto_free( ); |
| 2598 | } |
| 2599 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 2600 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2601 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 2602 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2603 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 2604 | data_t *nonce, |
| 2605 | data_t *additional_data, |
| 2606 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2607 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2608 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2609 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2610 | psa_key_type_t key_type = key_type_arg; |
| 2611 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2612 | unsigned char *output_data = NULL; |
| 2613 | size_t output_size = 0; |
| 2614 | size_t output_length = 0; |
| 2615 | unsigned char *output_data2 = NULL; |
| 2616 | size_t output_length2 = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2617 | size_t tag_length = 16; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2618 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 2619 | psa_key_policy_t policy; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2620 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2621 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2622 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2623 | TEST_ASSERT( nonce != NULL ); |
| 2624 | TEST_ASSERT( additional_data != NULL ); |
| 2625 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 2626 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 2627 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 2628 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 2629 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2630 | output_size = input_data->len + tag_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2631 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2632 | |
| 2633 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2634 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2635 | TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ), |
| 2636 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2637 | psa_key_policy_init( &policy ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2638 | psa_key_policy_set_usage( &policy, |
| 2639 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, |
| 2640 | alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2641 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2642 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2643 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2644 | key_data->x, key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2645 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2646 | TEST_ASSERT( psa_aead_encrypt( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2647 | nonce->x, nonce->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2648 | additional_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2649 | additional_data->len, |
| 2650 | input_data->x, input_data->len, |
| 2651 | output_data, output_size, |
| 2652 | &output_length ) == expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2653 | |
| 2654 | if( PSA_SUCCESS == expected_result ) |
| 2655 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2656 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2657 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2658 | TEST_ASSERT( psa_aead_decrypt( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2659 | nonce->x, nonce->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2660 | additional_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2661 | additional_data->len, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2662 | output_data, output_length, |
| 2663 | output_data2, output_length, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2664 | &output_length2 ) == expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2665 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2666 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 2667 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2668 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2669 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2670 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2671 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2672 | mbedtls_free( output_data ); |
| 2673 | mbedtls_free( output_data2 ); |
| 2674 | mbedtls_psa_crypto_free( ); |
| 2675 | } |
| 2676 | /* END_CASE */ |
| 2677 | |
| 2678 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 2679 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 2680 | int alg_arg, |
| 2681 | data_t *nonce, |
| 2682 | data_t *additional_data, |
| 2683 | data_t *input_data, |
| 2684 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2685 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2686 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2687 | psa_key_type_t key_type = key_type_arg; |
| 2688 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2689 | unsigned char *output_data = NULL; |
| 2690 | size_t output_size = 0; |
| 2691 | size_t output_length = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2692 | size_t tag_length = 16; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 2693 | psa_key_policy_t policy; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2694 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2695 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2696 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2697 | TEST_ASSERT( additional_data != NULL ); |
| 2698 | TEST_ASSERT( nonce != NULL ); |
| 2699 | TEST_ASSERT( expected_result != NULL ); |
| 2700 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 2701 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 2702 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 2703 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 2704 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_result->len ) ); |
| 2705 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2706 | output_size = input_data->len + tag_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2707 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2708 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2709 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2710 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2711 | TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ), |
| 2712 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2713 | psa_key_policy_init( &policy ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2714 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2715 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2716 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2717 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2718 | key_data->x, |
| 2719 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2720 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2721 | TEST_ASSERT( psa_aead_encrypt( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2722 | nonce->x, nonce->len, |
| 2723 | additional_data->x, additional_data->len, |
| 2724 | input_data->x, input_data->len, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2725 | output_data, output_size, |
| 2726 | &output_length ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2727 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2728 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 2729 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2730 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2731 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2732 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2733 | mbedtls_free( output_data ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2734 | mbedtls_psa_crypto_free( ); |
| 2735 | } |
| 2736 | /* END_CASE */ |
| 2737 | |
| 2738 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 2739 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 2740 | int alg_arg, |
| 2741 | data_t *nonce, |
| 2742 | data_t *additional_data, |
| 2743 | data_t *input_data, |
| 2744 | data_t *expected_data, |
| 2745 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2746 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2747 | psa_key_handle_t handle = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2748 | psa_key_type_t key_type = key_type_arg; |
| 2749 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2750 | unsigned char *output_data = NULL; |
| 2751 | size_t output_size = 0; |
| 2752 | size_t output_length = 0; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2753 | size_t tag_length = 16; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 2754 | psa_key_policy_t policy; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2755 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2756 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2757 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2758 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2759 | TEST_ASSERT( additional_data != NULL ); |
| 2760 | TEST_ASSERT( nonce != NULL ); |
| 2761 | TEST_ASSERT( expected_data != NULL ); |
| 2762 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 2763 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 2764 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( additional_data->len ) ); |
| 2765 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( nonce->len ) ); |
| 2766 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) ); |
| 2767 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2768 | output_size = input_data->len + tag_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2769 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2770 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2771 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2772 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2773 | TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ), |
| 2774 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2775 | psa_key_policy_init( &policy ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2776 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2777 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2778 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2779 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2780 | key_data->x, |
| 2781 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2782 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2783 | TEST_ASSERT( psa_aead_decrypt( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2784 | nonce->x, nonce->len, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2785 | additional_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2786 | additional_data->len, |
| 2787 | input_data->x, input_data->len, |
| 2788 | output_data, output_size, |
| 2789 | &output_length ) == expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2790 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2791 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2792 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 2793 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2794 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2795 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2796 | psa_destroy_key( handle ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2797 | mbedtls_free( output_data ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 2798 | mbedtls_psa_crypto_free( ); |
| 2799 | } |
| 2800 | /* END_CASE */ |
| 2801 | |
| 2802 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2803 | void signature_size( int type_arg, |
| 2804 | int bits, |
| 2805 | int alg_arg, |
| 2806 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2807 | { |
| 2808 | psa_key_type_t type = type_arg; |
| 2809 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 2810 | size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2811 | TEST_ASSERT( actual_size == (size_t) expected_size_arg ); |
| 2812 | exit: |
| 2813 | ; |
| 2814 | } |
| 2815 | /* END_CASE */ |
| 2816 | |
| 2817 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2818 | void sign_deterministic( int key_type_arg, data_t *key_data, |
| 2819 | int alg_arg, data_t *input_data, |
| 2820 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2821 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2822 | psa_key_handle_t handle = 0; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 2823 | psa_key_type_t key_type = key_type_arg; |
| 2824 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2825 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2826 | unsigned char *signature = NULL; |
| 2827 | size_t signature_size; |
| 2828 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 2829 | psa_key_policy_t policy; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2830 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2831 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2832 | TEST_ASSERT( input_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2833 | TEST_ASSERT( output_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2834 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 2835 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 2836 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( output_data->len ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2837 | |
| 2838 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2839 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2840 | TEST_ASSERT( psa_allocate_key( key_type, |
| 2841 | KEY_BITS_FROM_DATA( key_type, key_data ), |
| 2842 | &handle ) == PSA_SUCCESS ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 2843 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2844 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2845 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 2846 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2847 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2848 | key_data->x, |
| 2849 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2850 | TEST_ASSERT( psa_get_key_information( handle, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2851 | NULL, |
| 2852 | &key_bits ) == PSA_SUCCESS ); |
| 2853 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 2854 | /* Allocate a buffer which has the size advertized by the |
| 2855 | * library. */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 2856 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, |
| 2857 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2858 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2859 | TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2860 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2861 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 2862 | /* Perform the signature. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2863 | TEST_ASSERT( psa_asymmetric_sign( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2864 | input_data->x, input_data->len, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2865 | signature, signature_size, |
| 2866 | &signature_length ) == PSA_SUCCESS ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 2867 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 2868 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 2869 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2870 | |
| 2871 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2872 | psa_destroy_key( handle ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 2873 | mbedtls_free( signature ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2874 | mbedtls_psa_crypto_free( ); |
| 2875 | } |
| 2876 | /* END_CASE */ |
| 2877 | |
| 2878 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2879 | void sign_fail( int key_type_arg, data_t *key_data, |
| 2880 | int alg_arg, data_t *input_data, |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 2881 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2882 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2883 | psa_key_handle_t handle = 0; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2884 | psa_key_type_t key_type = key_type_arg; |
| 2885 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 2886 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2887 | psa_status_t actual_status; |
| 2888 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 2889 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 2890 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 2891 | psa_key_policy_t policy; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2892 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2893 | TEST_ASSERT( key_data != NULL ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2894 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2895 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 2896 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 2897 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2898 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2899 | |
| 2900 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2901 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2902 | TEST_ASSERT( psa_allocate_key( key_type, |
| 2903 | KEY_BITS_FROM_DATA( key_type, key_data ), |
| 2904 | &handle ) == PSA_SUCCESS ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 2905 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2906 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2907 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 2908 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2909 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2910 | key_data->x, |
| 2911 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2912 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2913 | actual_status = psa_asymmetric_sign( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 2914 | input_data->x, input_data->len, |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2915 | signature, signature_size, |
| 2916 | &signature_length ); |
| 2917 | TEST_ASSERT( actual_status == expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 2918 | /* The value of *signature_length is unspecified on error, but |
| 2919 | * whatever it is, it should be less than signature_size, so that |
| 2920 | * if the caller tries to read *signature_length bytes without |
| 2921 | * checking the error code then they don't overflow a buffer. */ |
| 2922 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2923 | |
| 2924 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2925 | psa_destroy_key( handle ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 2926 | mbedtls_free( signature ); |
| 2927 | mbedtls_psa_crypto_free( ); |
| 2928 | } |
| 2929 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 2930 | |
| 2931 | /* BEGIN_CASE */ |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 2932 | void sign_verify( int key_type_arg, data_t *key_data, |
| 2933 | int alg_arg, data_t *input_data ) |
| 2934 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2935 | psa_key_handle_t handle = 0; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 2936 | psa_key_type_t key_type = key_type_arg; |
| 2937 | psa_algorithm_t alg = alg_arg; |
| 2938 | size_t key_bits; |
| 2939 | unsigned char *signature = NULL; |
| 2940 | size_t signature_size; |
| 2941 | size_t signature_length = 0xdeadbeef; |
| 2942 | psa_key_policy_t policy; |
| 2943 | |
| 2944 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 2945 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2946 | TEST_ASSERT( psa_allocate_key( key_type, |
| 2947 | KEY_BITS_FROM_DATA( key_type, key_data ), |
| 2948 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 2949 | psa_key_policy_init( &policy ); |
| 2950 | psa_key_policy_set_usage( &policy, |
| 2951 | PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY, |
| 2952 | alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2953 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 2954 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2955 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 2956 | key_data->x, |
| 2957 | key_data->len ) == PSA_SUCCESS ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2958 | TEST_ASSERT( psa_get_key_information( handle, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 2959 | NULL, |
| 2960 | &key_bits ) == PSA_SUCCESS ); |
| 2961 | |
| 2962 | /* Allocate a buffer which has the size advertized by the |
| 2963 | * library. */ |
| 2964 | signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, |
| 2965 | key_bits, alg ); |
| 2966 | TEST_ASSERT( signature_size != 0 ); |
| 2967 | TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2968 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 2969 | |
| 2970 | /* Perform the signature. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2971 | TEST_ASSERT( psa_asymmetric_sign( handle, alg, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 2972 | input_data->x, input_data->len, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 2973 | signature, signature_size, |
| 2974 | &signature_length ) == PSA_SUCCESS ); |
| 2975 | /* Check that the signature length looks sensible. */ |
| 2976 | TEST_ASSERT( signature_length <= signature_size ); |
| 2977 | TEST_ASSERT( signature_length > 0 ); |
| 2978 | |
| 2979 | /* Use the library to verify that the signature is correct. */ |
| 2980 | TEST_ASSERT( psa_asymmetric_verify( |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2981 | handle, alg, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 2982 | input_data->x, input_data->len, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 2983 | signature, signature_length ) == PSA_SUCCESS ); |
| 2984 | |
| 2985 | if( input_data->len != 0 ) |
| 2986 | { |
| 2987 | /* Flip a bit in the input and verify that the signature is now |
| 2988 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 2989 | * because ECDSA may ignore the last few bits of the input. */ |
| 2990 | input_data->x[0] ^= 1; |
| 2991 | TEST_ASSERT( psa_asymmetric_verify( |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2992 | handle, alg, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 2993 | input_data->x, input_data->len, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 2994 | signature, |
| 2995 | signature_length ) == PSA_ERROR_INVALID_SIGNATURE ); |
| 2996 | } |
| 2997 | |
| 2998 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 2999 | psa_destroy_key( handle ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 3000 | mbedtls_free( signature ); |
| 3001 | mbedtls_psa_crypto_free( ); |
| 3002 | } |
| 3003 | /* END_CASE */ |
| 3004 | |
| 3005 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3006 | void asymmetric_verify( int key_type_arg, data_t *key_data, |
| 3007 | int alg_arg, data_t *hash_data, |
| 3008 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3009 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3010 | psa_key_handle_t handle = 0; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3011 | psa_key_type_t key_type = key_type_arg; |
| 3012 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 3013 | psa_key_policy_t policy; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3014 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3015 | TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE ); |
| 3016 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3017 | TEST_ASSERT( key_data != NULL ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3018 | TEST_ASSERT( hash_data != NULL ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3019 | TEST_ASSERT( signature_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3020 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 3021 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) ); |
| 3022 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3023 | |
| 3024 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3025 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3026 | TEST_ASSERT( psa_allocate_key( key_type, |
| 3027 | KEY_BITS_FROM_DATA( key_type, key_data ), |
| 3028 | &handle ) == PSA_SUCCESS ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3029 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3030 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3031 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3032 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3033 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3034 | key_data->x, |
| 3035 | key_data->len ) == PSA_SUCCESS ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3036 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3037 | TEST_ASSERT( psa_asymmetric_verify( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3038 | hash_data->x, hash_data->len, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3039 | signature_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3040 | signature_data->len ) == PSA_SUCCESS ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3041 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3042 | psa_destroy_key( handle ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 3043 | mbedtls_psa_crypto_free( ); |
| 3044 | } |
| 3045 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3046 | |
| 3047 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3048 | void asymmetric_verify_fail( int key_type_arg, data_t *key_data, |
| 3049 | int alg_arg, data_t *hash_data, |
| 3050 | data_t *signature_data, |
| 3051 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3052 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3053 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3054 | psa_key_type_t key_type = key_type_arg; |
| 3055 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3056 | psa_status_t actual_status; |
| 3057 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 3058 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3059 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3060 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3061 | TEST_ASSERT( hash_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3062 | TEST_ASSERT( signature_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3063 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 3064 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( hash_data->len ) ); |
| 3065 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( signature_data->len ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3066 | |
| 3067 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3068 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3069 | TEST_ASSERT( psa_allocate_key( key_type, |
| 3070 | KEY_BITS_FROM_DATA( key_type, key_data ), |
| 3071 | &handle ) == PSA_SUCCESS ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3072 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3073 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3074 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3075 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3076 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3077 | key_data->x, |
| 3078 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3079 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3080 | actual_status = psa_asymmetric_verify( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3081 | hash_data->x, hash_data->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3082 | signature_data->x, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3083 | signature_data->len ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3084 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3085 | TEST_ASSERT( actual_status == expected_status ); |
| 3086 | |
| 3087 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3088 | psa_destroy_key( handle ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3089 | mbedtls_psa_crypto_free( ); |
| 3090 | } |
| 3091 | /* END_CASE */ |
| 3092 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3093 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3094 | void asymmetric_encrypt( int key_type_arg, |
| 3095 | data_t *key_data, |
| 3096 | int alg_arg, |
| 3097 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3098 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3099 | int expected_output_length_arg, |
| 3100 | int expected_status_arg ) |
| 3101 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3102 | psa_key_handle_t handle = 0; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3103 | psa_key_type_t key_type = key_type_arg; |
| 3104 | psa_algorithm_t alg = alg_arg; |
| 3105 | size_t expected_output_length = expected_output_length_arg; |
| 3106 | size_t key_bits; |
| 3107 | unsigned char *output = NULL; |
| 3108 | size_t output_size; |
| 3109 | size_t output_length = ~0; |
| 3110 | psa_status_t actual_status; |
| 3111 | psa_status_t expected_status = expected_status_arg; |
| 3112 | psa_key_policy_t policy; |
| 3113 | |
| 3114 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3115 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3116 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3117 | /* Import the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3118 | TEST_ASSERT( psa_allocate_key( key_type, |
| 3119 | KEY_BITS_FROM_DATA( key_type, key_data ), |
| 3120 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3121 | psa_key_policy_init( &policy ); |
| 3122 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3123 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
| 3124 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3125 | key_data->x, |
| 3126 | key_data->len ) == PSA_SUCCESS ); |
| 3127 | |
| 3128 | /* Determine the maximum output length */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3129 | TEST_ASSERT( psa_get_key_information( handle, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3130 | NULL, |
| 3131 | &key_bits ) == PSA_SUCCESS ); |
| 3132 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3133 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3134 | |
| 3135 | /* Encrypt the input */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3136 | actual_status = psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3137 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3138 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3139 | output, output_size, |
| 3140 | &output_length ); |
| 3141 | TEST_ASSERT( actual_status == expected_status ); |
| 3142 | TEST_ASSERT( output_length == expected_output_length ); |
| 3143 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3144 | /* If the label is empty, the test framework puts a non-null pointer |
| 3145 | * in label->x. Test that a null pointer works as well. */ |
| 3146 | if( label->len == 0 ) |
| 3147 | { |
| 3148 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 3149 | if( output_size != 0 ) |
| 3150 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3151 | actual_status = psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3152 | input_data->x, input_data->len, |
| 3153 | NULL, label->len, |
| 3154 | output, output_size, |
| 3155 | &output_length ); |
| 3156 | TEST_ASSERT( actual_status == expected_status ); |
| 3157 | TEST_ASSERT( output_length == expected_output_length ); |
| 3158 | } |
| 3159 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3160 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3161 | psa_destroy_key( handle ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 3162 | mbedtls_free( output ); |
| 3163 | mbedtls_psa_crypto_free( ); |
| 3164 | } |
| 3165 | /* END_CASE */ |
| 3166 | |
| 3167 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3168 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 3169 | data_t *key_data, |
| 3170 | int alg_arg, |
| 3171 | data_t *input_data, |
| 3172 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3173 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3174 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3175 | psa_key_type_t key_type = key_type_arg; |
| 3176 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3177 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3178 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3179 | size_t output_size; |
| 3180 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 3181 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3182 | size_t output2_size; |
| 3183 | size_t output2_length = ~0; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 3184 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3185 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3186 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3187 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3188 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 3189 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 3190 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3191 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3192 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3193 | TEST_ASSERT( psa_allocate_key( key_type, |
| 3194 | KEY_BITS_FROM_DATA( key_type, key_data ), |
| 3195 | &handle ) == PSA_SUCCESS ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3196 | psa_key_policy_init( &policy ); |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3197 | psa_key_policy_set_usage( &policy, |
| 3198 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3199 | alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3200 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3201 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3202 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3203 | key_data->x, |
| 3204 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3205 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3206 | |
| 3207 | /* Determine the maximum ciphertext length */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3208 | TEST_ASSERT( psa_get_key_information( handle, |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3209 | NULL, |
| 3210 | &key_bits ) == PSA_SUCCESS ); |
| 3211 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3212 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3213 | output2_size = input_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3214 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3215 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 3216 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 3217 | * the original plaintext because of the non-optional random |
| 3218 | * part of encryption process which prevents using fixed vectors. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3219 | TEST_ASSERT( psa_asymmetric_encrypt( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3220 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3221 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3222 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3223 | &output_length ) == PSA_SUCCESS ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3224 | /* We don't know what ciphertext length to expect, but check that |
| 3225 | * it looks sensible. */ |
| 3226 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 3227 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3228 | TEST_ASSERT( psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3229 | output, output_length, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3230 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3231 | output2, output2_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3232 | &output2_length ) == PSA_SUCCESS ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3233 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3234 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3235 | |
| 3236 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3237 | psa_destroy_key( handle ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3238 | mbedtls_free( output ); |
| 3239 | mbedtls_free( output2 ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3240 | mbedtls_psa_crypto_free( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3241 | } |
| 3242 | /* END_CASE */ |
| 3243 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3244 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3245 | void asymmetric_decrypt( int key_type_arg, |
| 3246 | data_t *key_data, |
| 3247 | int alg_arg, |
| 3248 | data_t *input_data, |
| 3249 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 3250 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3251 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3252 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3253 | psa_key_type_t key_type = key_type_arg; |
| 3254 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3255 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 3256 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3257 | size_t output_length = ~0; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 3258 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3259 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3260 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3261 | TEST_ASSERT( input_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3262 | TEST_ASSERT( expected_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3263 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 3264 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 3265 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( expected_data->len ) ); |
| 3266 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3267 | output_size = key_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3268 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3269 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3270 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3271 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3272 | TEST_ASSERT( psa_allocate_key( key_type, |
| 3273 | KEY_BITS_FROM_DATA( key_type, key_data ), |
| 3274 | &handle ) == PSA_SUCCESS ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3275 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3276 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3277 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3278 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3279 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3280 | key_data->x, |
| 3281 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3282 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3283 | TEST_ASSERT( psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3284 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3285 | label->x, label->len, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3286 | output, |
| 3287 | output_size, |
| 3288 | &output_length ) == PSA_SUCCESS ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3289 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3290 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3291 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3292 | /* If the label is empty, the test framework puts a non-null pointer |
| 3293 | * in label->x. Test that a null pointer works as well. */ |
| 3294 | if( label->len == 0 ) |
| 3295 | { |
| 3296 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 3297 | if( output_size != 0 ) |
| 3298 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3299 | TEST_ASSERT( psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3300 | input_data->x, input_data->len, |
| 3301 | NULL, label->len, |
| 3302 | output, |
| 3303 | output_size, |
| 3304 | &output_length ) == PSA_SUCCESS ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3305 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3306 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3307 | } |
| 3308 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3309 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3310 | psa_destroy_key( handle ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3311 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3312 | mbedtls_psa_crypto_free( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3313 | } |
| 3314 | /* END_CASE */ |
| 3315 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3316 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3317 | void asymmetric_decrypt_fail( int key_type_arg, |
| 3318 | data_t *key_data, |
| 3319 | int alg_arg, |
| 3320 | data_t *input_data, |
| 3321 | data_t *label, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3322 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3323 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3324 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3325 | psa_key_type_t key_type = key_type_arg; |
| 3326 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3327 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 3328 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3329 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3330 | psa_status_t actual_status; |
| 3331 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | dec7261 | 2018-06-18 18:12:37 +0200 | [diff] [blame] | 3332 | psa_key_policy_t policy; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3333 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3334 | TEST_ASSERT( key_data != NULL ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3335 | TEST_ASSERT( input_data != NULL ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3336 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( key_data->len ) ); |
| 3337 | TEST_ASSERT( PSA_CRYPTO_TEST_SIZE_T_RANGE( input_data->len ) ); |
| 3338 | |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3339 | output_size = key_data->len; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3340 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3341 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3342 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3343 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3344 | TEST_ASSERT( psa_allocate_key( key_type, |
| 3345 | KEY_BITS_FROM_DATA( key_type, key_data ), |
| 3346 | &handle ) == PSA_SUCCESS ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3347 | psa_key_policy_init( &policy ); |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3348 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3349 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 3350 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3351 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3352 | key_data->x, |
| 3353 | key_data->len ) == PSA_SUCCESS ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3354 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3355 | actual_status = psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3356 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3357 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3358 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3359 | &output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3360 | TEST_ASSERT( actual_status == expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3361 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3362 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3363 | /* If the label is empty, the test framework puts a non-null pointer |
| 3364 | * in label->x. Test that a null pointer works as well. */ |
| 3365 | if( label->len == 0 ) |
| 3366 | { |
| 3367 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 3368 | if( output_size != 0 ) |
| 3369 | memset( output, 0, output_size ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3370 | actual_status = psa_asymmetric_decrypt( handle, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3371 | input_data->x, input_data->len, |
| 3372 | NULL, label->len, |
| 3373 | output, output_size, |
| 3374 | &output_length ); |
| 3375 | TEST_ASSERT( actual_status == expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 3376 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 3377 | } |
| 3378 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3379 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3380 | psa_destroy_key( handle ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3381 | mbedtls_free( output ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 3382 | mbedtls_psa_crypto_free( ); |
| 3383 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 3384 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 3385 | |
| 3386 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3387 | void derive_setup( int key_type_arg, |
| 3388 | data_t *key_data, |
| 3389 | int alg_arg, |
| 3390 | data_t *salt, |
| 3391 | data_t *label, |
| 3392 | int requested_capacity_arg, |
| 3393 | int expected_status_arg ) |
| 3394 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3395 | psa_key_handle_t handle = 0; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3396 | size_t key_type = key_type_arg; |
| 3397 | psa_algorithm_t alg = alg_arg; |
| 3398 | size_t requested_capacity = requested_capacity_arg; |
| 3399 | psa_status_t expected_status = expected_status_arg; |
| 3400 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 3401 | psa_key_policy_t policy; |
| 3402 | |
| 3403 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3404 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3405 | TEST_ASSERT( psa_allocate_key( key_type, PSA_BYTES_TO_BITS( key_data->len ), |
| 3406 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3407 | psa_key_policy_init( &policy ); |
| 3408 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3409 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3410 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3411 | TEST_ASSERT( psa_import_key( handle, key_type, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3412 | key_data->x, |
| 3413 | key_data->len ) == PSA_SUCCESS ); |
| 3414 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3415 | TEST_ASSERT( psa_key_derivation( &generator, handle, alg, |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3416 | salt->x, salt->len, |
| 3417 | label->x, label->len, |
| 3418 | requested_capacity ) == expected_status ); |
| 3419 | |
| 3420 | exit: |
| 3421 | psa_generator_abort( &generator ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3422 | psa_destroy_key( handle ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 3423 | mbedtls_psa_crypto_free( ); |
| 3424 | } |
| 3425 | /* END_CASE */ |
| 3426 | |
| 3427 | /* BEGIN_CASE */ |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 3428 | void test_derive_invalid_generator_state( ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 3429 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3430 | psa_key_handle_t handle = 0; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 3431 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3432 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 3433 | psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3434 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 3435 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 3436 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 3437 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 3438 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3439 | psa_key_policy_t policy; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 3440 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3441 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3442 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3443 | TEST_ASSERT( psa_allocate_key( key_type, |
| 3444 | PSA_BYTES_TO_BITS( sizeof( key_data ) ), |
| 3445 | &handle ) == PSA_SUCCESS ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3446 | psa_key_policy_init( &policy ); |
| 3447 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3448 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3449 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3450 | TEST_ASSERT( psa_import_key( handle, key_type, |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 3451 | key_data, |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 3452 | sizeof( key_data ) ) == PSA_SUCCESS ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3453 | |
| 3454 | /* valid key derivation */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3455 | TEST_ASSERT( psa_key_derivation( &generator, handle, alg, |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3456 | NULL, 0, |
| 3457 | NULL, 0, |
| 3458 | capacity ) == PSA_SUCCESS ); |
| 3459 | |
| 3460 | /* state of generator shouldn't allow additional generation */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3461 | TEST_ASSERT( psa_key_derivation( &generator, handle, alg, |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3462 | NULL, 0, |
| 3463 | NULL, 0, |
| 3464 | capacity ) == PSA_ERROR_BAD_STATE ); |
| 3465 | |
| 3466 | TEST_ASSERT( psa_generator_read( &generator, buffer, capacity ) |
| 3467 | == PSA_SUCCESS ); |
| 3468 | |
| 3469 | TEST_ASSERT( psa_generator_read( &generator, buffer, capacity ) |
| 3470 | == PSA_ERROR_INSUFFICIENT_CAPACITY ); |
| 3471 | |
| 3472 | |
| 3473 | exit: |
| 3474 | psa_generator_abort( &generator ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3475 | psa_destroy_key( handle ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3476 | mbedtls_psa_crypto_free( ); |
| 3477 | } |
| 3478 | /* END_CASE */ |
| 3479 | |
| 3480 | |
| 3481 | /* BEGIN_CASE */ |
| 3482 | void test_derive_invalid_generator_tests( ) |
| 3483 | { |
| 3484 | uint8_t output_buffer[16]; |
| 3485 | size_t buffer_size = 16; |
| 3486 | size_t capacity = 0; |
| 3487 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 3488 | |
Nir Sonnenschein | 5078930 | 2018-10-31 12:16:38 +0200 | [diff] [blame] | 3489 | TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size ) |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 3490 | == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183 |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3491 | |
| 3492 | TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity ) |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 3493 | == PSA_SUCCESS ); // should be PSA_ERROR_BAD_STATE:#183 |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3494 | |
Nir Sonnenschein | 5078930 | 2018-10-31 12:16:38 +0200 | [diff] [blame] | 3495 | TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3496 | |
Nir Sonnenschein | 5078930 | 2018-10-31 12:16:38 +0200 | [diff] [blame] | 3497 | TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size ) |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 3498 | == PSA_ERROR_INSUFFICIENT_CAPACITY ); // should be PSA_ERROR_BAD_STATE:#183 |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3499 | |
Nir Sonnenschein | 5078930 | 2018-10-31 12:16:38 +0200 | [diff] [blame] | 3500 | TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity ) |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 3501 | == PSA_SUCCESS );// should be PSA_ERROR_BAD_STATE:#183 |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 3502 | |
| 3503 | exit: |
| 3504 | psa_generator_abort( &generator ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 3505 | } |
| 3506 | /* END_CASE */ |
| 3507 | |
| 3508 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 3509 | void derive_output( int alg_arg, |
| 3510 | data_t *key_data, |
| 3511 | data_t *salt, |
| 3512 | data_t *label, |
| 3513 | int requested_capacity_arg, |
| 3514 | data_t *expected_output1, |
| 3515 | data_t *expected_output2 ) |
| 3516 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3517 | psa_key_handle_t handle = 0; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 3518 | psa_algorithm_t alg = alg_arg; |
| 3519 | size_t requested_capacity = requested_capacity_arg; |
| 3520 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 3521 | uint8_t *expected_outputs[2] = |
| 3522 | {expected_output1->x, expected_output2->x}; |
| 3523 | size_t output_sizes[2] = |
| 3524 | {expected_output1->len, expected_output2->len}; |
| 3525 | size_t output_buffer_size = 0; |
| 3526 | uint8_t *output_buffer = NULL; |
| 3527 | size_t expected_capacity; |
| 3528 | size_t current_capacity; |
| 3529 | psa_key_policy_t policy; |
| 3530 | psa_status_t status; |
| 3531 | unsigned i; |
| 3532 | |
| 3533 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 3534 | { |
| 3535 | if( output_sizes[i] > output_buffer_size ) |
| 3536 | output_buffer_size = output_sizes[i]; |
| 3537 | if( output_sizes[i] == 0 ) |
| 3538 | expected_outputs[i] = NULL; |
| 3539 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3540 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 3541 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3542 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3543 | TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE, |
| 3544 | PSA_BYTES_TO_BITS( key_data->len ), |
| 3545 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 3546 | psa_key_policy_init( &policy ); |
| 3547 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3548 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 3549 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3550 | TEST_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 3551 | key_data->x, |
| 3552 | key_data->len ) == PSA_SUCCESS ); |
| 3553 | |
| 3554 | /* Extraction phase. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3555 | TEST_ASSERT( psa_key_derivation( &generator, handle, alg, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 3556 | salt->x, salt->len, |
| 3557 | label->x, label->len, |
| 3558 | requested_capacity ) == PSA_SUCCESS ); |
| 3559 | TEST_ASSERT( psa_get_generator_capacity( &generator, |
| 3560 | ¤t_capacity ) == |
| 3561 | PSA_SUCCESS ); |
| 3562 | TEST_ASSERT( current_capacity == requested_capacity ); |
| 3563 | expected_capacity = requested_capacity; |
| 3564 | |
| 3565 | /* Expansion phase. */ |
| 3566 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 3567 | { |
| 3568 | /* Read some bytes. */ |
| 3569 | status = psa_generator_read( &generator, |
| 3570 | output_buffer, output_sizes[i] ); |
| 3571 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 3572 | { |
| 3573 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 3574 | TEST_ASSERT( status == PSA_SUCCESS || |
| 3575 | status == PSA_ERROR_INSUFFICIENT_CAPACITY ); |
| 3576 | continue; |
| 3577 | } |
| 3578 | else if( expected_capacity == 0 || |
| 3579 | output_sizes[i] > expected_capacity ) |
| 3580 | { |
| 3581 | /* Capacity exceeded. */ |
| 3582 | TEST_ASSERT( status == PSA_ERROR_INSUFFICIENT_CAPACITY ); |
| 3583 | expected_capacity = 0; |
| 3584 | continue; |
| 3585 | } |
| 3586 | /* Success. Check the read data. */ |
| 3587 | TEST_ASSERT( status == PSA_SUCCESS ); |
| 3588 | if( output_sizes[i] != 0 ) |
| 3589 | TEST_ASSERT( memcmp( output_buffer, expected_outputs[i], |
| 3590 | output_sizes[i] ) == 0 ); |
| 3591 | /* Check the generator status. */ |
| 3592 | expected_capacity -= output_sizes[i]; |
| 3593 | TEST_ASSERT( psa_get_generator_capacity( &generator, |
| 3594 | ¤t_capacity ) == |
| 3595 | PSA_SUCCESS ); |
| 3596 | TEST_ASSERT( expected_capacity == current_capacity ); |
| 3597 | } |
| 3598 | TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS ); |
| 3599 | |
| 3600 | exit: |
| 3601 | mbedtls_free( output_buffer ); |
| 3602 | psa_generator_abort( &generator ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3603 | psa_destroy_key( handle ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 3604 | mbedtls_psa_crypto_free( ); |
| 3605 | } |
| 3606 | /* END_CASE */ |
| 3607 | |
| 3608 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3609 | void derive_full( int alg_arg, |
| 3610 | data_t *key_data, |
| 3611 | data_t *salt, |
| 3612 | data_t *label, |
| 3613 | int requested_capacity_arg ) |
| 3614 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3615 | psa_key_handle_t handle = 0; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3616 | psa_algorithm_t alg = alg_arg; |
| 3617 | size_t requested_capacity = requested_capacity_arg; |
| 3618 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 3619 | unsigned char output_buffer[16]; |
| 3620 | size_t expected_capacity = requested_capacity; |
| 3621 | size_t current_capacity; |
| 3622 | psa_key_policy_t policy; |
| 3623 | |
| 3624 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3625 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3626 | TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE, |
| 3627 | PSA_BYTES_TO_BITS( key_data->len ), |
| 3628 | &handle ) == PSA_SUCCESS ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3629 | psa_key_policy_init( &policy ); |
| 3630 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3631 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3632 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3633 | TEST_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3634 | key_data->x, |
| 3635 | key_data->len ) == PSA_SUCCESS ); |
| 3636 | |
| 3637 | /* Extraction phase. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3638 | TEST_ASSERT( psa_key_derivation( &generator, handle, alg, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3639 | salt->x, salt->len, |
| 3640 | label->x, label->len, |
| 3641 | requested_capacity ) == PSA_SUCCESS ); |
| 3642 | TEST_ASSERT( psa_get_generator_capacity( &generator, |
| 3643 | ¤t_capacity ) == |
| 3644 | PSA_SUCCESS ); |
| 3645 | TEST_ASSERT( current_capacity == expected_capacity ); |
| 3646 | |
| 3647 | /* Expansion phase. */ |
| 3648 | while( current_capacity > 0 ) |
| 3649 | { |
| 3650 | size_t read_size = sizeof( output_buffer ); |
| 3651 | if( read_size > current_capacity ) |
| 3652 | read_size = current_capacity; |
| 3653 | TEST_ASSERT( psa_generator_read( &generator, |
| 3654 | output_buffer, |
| 3655 | read_size ) == PSA_SUCCESS ); |
| 3656 | expected_capacity -= read_size; |
| 3657 | TEST_ASSERT( psa_get_generator_capacity( &generator, |
| 3658 | ¤t_capacity ) == |
| 3659 | PSA_SUCCESS ); |
| 3660 | TEST_ASSERT( current_capacity == expected_capacity ); |
| 3661 | } |
| 3662 | |
| 3663 | /* Check that the generator refuses to go over capacity. */ |
| 3664 | TEST_ASSERT( psa_generator_read( &generator, |
| 3665 | output_buffer, |
| 3666 | 1 ) == PSA_ERROR_INSUFFICIENT_CAPACITY ); |
| 3667 | |
| 3668 | TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS ); |
| 3669 | |
| 3670 | exit: |
| 3671 | psa_generator_abort( &generator ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3672 | psa_destroy_key( handle ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 3673 | mbedtls_psa_crypto_free( ); |
| 3674 | } |
| 3675 | /* END_CASE */ |
| 3676 | |
| 3677 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3678 | void derive_key_exercise( int alg_arg, |
| 3679 | data_t *key_data, |
| 3680 | data_t *salt, |
| 3681 | data_t *label, |
| 3682 | int derived_type_arg, |
| 3683 | int derived_bits_arg, |
| 3684 | int derived_usage_arg, |
| 3685 | int derived_alg_arg ) |
| 3686 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3687 | psa_key_handle_t base_handle = 0; |
| 3688 | psa_key_handle_t derived_handle = 0; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3689 | psa_algorithm_t alg = alg_arg; |
| 3690 | psa_key_type_t derived_type = derived_type_arg; |
| 3691 | size_t derived_bits = derived_bits_arg; |
| 3692 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 3693 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 3694 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
| 3695 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 3696 | psa_key_policy_t policy; |
| 3697 | psa_key_type_t got_type; |
| 3698 | size_t got_bits; |
| 3699 | |
| 3700 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3701 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3702 | TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE, |
| 3703 | PSA_BYTES_TO_BITS( key_data->len ), |
| 3704 | &base_handle ) == PSA_SUCCESS ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3705 | psa_key_policy_init( &policy ); |
| 3706 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3707 | TEST_ASSERT( psa_set_key_policy( base_handle, &policy ) == PSA_SUCCESS ); |
| 3708 | TEST_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3709 | key_data->x, |
| 3710 | key_data->len ) == PSA_SUCCESS ); |
| 3711 | |
| 3712 | /* Derive a key. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3713 | TEST_ASSERT( psa_key_derivation( &generator, base_handle, alg, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3714 | salt->x, salt->len, |
| 3715 | label->x, label->len, |
| 3716 | capacity ) == PSA_SUCCESS ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3717 | TEST_ASSERT( psa_allocate_key( derived_type, derived_bits, |
| 3718 | &derived_handle ) == PSA_SUCCESS ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3719 | psa_key_policy_set_usage( &policy, derived_usage, derived_alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3720 | TEST_ASSERT( psa_set_key_policy( derived_handle, &policy ) == PSA_SUCCESS ); |
| 3721 | TEST_ASSERT( psa_generator_import_key( derived_handle, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3722 | derived_type, |
| 3723 | derived_bits, |
| 3724 | &generator ) == PSA_SUCCESS ); |
| 3725 | |
| 3726 | /* Test the key information */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3727 | TEST_ASSERT( psa_get_key_information( derived_handle, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3728 | &got_type, |
| 3729 | &got_bits ) == PSA_SUCCESS ); |
| 3730 | TEST_ASSERT( got_type == derived_type ); |
| 3731 | TEST_ASSERT( got_bits == derived_bits ); |
| 3732 | |
| 3733 | /* Exercise the derived key. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3734 | if( ! exercise_key( derived_handle, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3735 | goto exit; |
| 3736 | |
| 3737 | exit: |
| 3738 | psa_generator_abort( &generator ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3739 | psa_destroy_key( base_handle ); |
| 3740 | psa_destroy_key( derived_handle ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3741 | mbedtls_psa_crypto_free( ); |
| 3742 | } |
| 3743 | /* END_CASE */ |
| 3744 | |
| 3745 | /* BEGIN_CASE */ |
| 3746 | void derive_key_export( int alg_arg, |
| 3747 | data_t *key_data, |
| 3748 | data_t *salt, |
| 3749 | data_t *label, |
| 3750 | int bytes1_arg, |
| 3751 | int bytes2_arg ) |
| 3752 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3753 | psa_key_handle_t base_handle = 0; |
| 3754 | psa_key_handle_t derived_handle = 0; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3755 | psa_algorithm_t alg = alg_arg; |
| 3756 | size_t bytes1 = bytes1_arg; |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3757 | size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3758 | size_t bytes2 = bytes2_arg; |
| 3759 | size_t capacity = bytes1 + bytes2; |
| 3760 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3761 | uint8_t *output_buffer = NULL; |
| 3762 | uint8_t *export_buffer = NULL; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3763 | psa_key_policy_t policy; |
| 3764 | size_t length; |
| 3765 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3766 | ASSERT_ALLOC( output_buffer, capacity ); |
| 3767 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3768 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3769 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3770 | TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE, |
| 3771 | PSA_BYTES_TO_BITS( key_data->len ), |
| 3772 | &base_handle ) == PSA_SUCCESS ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3773 | psa_key_policy_init( &policy ); |
| 3774 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3775 | TEST_ASSERT( psa_set_key_policy( base_handle, &policy ) == PSA_SUCCESS ); |
| 3776 | TEST_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3777 | key_data->x, |
| 3778 | key_data->len ) == PSA_SUCCESS ); |
| 3779 | |
| 3780 | /* Derive some material and output it. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3781 | TEST_ASSERT( psa_key_derivation( &generator, base_handle, alg, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3782 | salt->x, salt->len, |
| 3783 | label->x, label->len, |
| 3784 | capacity ) == PSA_SUCCESS ); |
| 3785 | TEST_ASSERT( psa_generator_read( &generator, |
| 3786 | output_buffer, |
| 3787 | capacity ) == PSA_SUCCESS ); |
| 3788 | TEST_ASSERT( psa_generator_abort( &generator ) == PSA_SUCCESS ); |
| 3789 | |
| 3790 | /* Derive the same output again, but this time store it in key objects. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3791 | TEST_ASSERT( psa_key_derivation( &generator, base_handle, alg, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3792 | salt->x, salt->len, |
| 3793 | label->x, label->len, |
| 3794 | capacity ) == PSA_SUCCESS ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3795 | TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, derived_bits, |
| 3796 | &derived_handle ) == PSA_SUCCESS ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3797 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3798 | TEST_ASSERT( psa_set_key_policy( derived_handle, &policy ) == PSA_SUCCESS ); |
| 3799 | TEST_ASSERT( psa_generator_import_key( derived_handle, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3800 | PSA_KEY_TYPE_RAW_DATA, |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3801 | derived_bits, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3802 | &generator ) == PSA_SUCCESS ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3803 | TEST_ASSERT( psa_export_key( derived_handle, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3804 | export_buffer, bytes1, |
| 3805 | &length ) == PSA_SUCCESS ); |
| 3806 | TEST_ASSERT( length == bytes1 ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3807 | TEST_ASSERT( psa_destroy_key( derived_handle ) == PSA_SUCCESS ); |
| 3808 | TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_RAW_DATA, |
| 3809 | PSA_BYTES_TO_BITS( bytes2 ), |
| 3810 | &derived_handle ) == PSA_SUCCESS ); |
| 3811 | TEST_ASSERT( psa_set_key_policy( derived_handle, &policy ) == PSA_SUCCESS ); |
| 3812 | TEST_ASSERT( psa_generator_import_key( derived_handle, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3813 | PSA_KEY_TYPE_RAW_DATA, |
| 3814 | PSA_BYTES_TO_BITS( bytes2 ), |
| 3815 | &generator ) == PSA_SUCCESS ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3816 | TEST_ASSERT( psa_export_key( derived_handle, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3817 | export_buffer + bytes1, bytes2, |
| 3818 | &length ) == PSA_SUCCESS ); |
| 3819 | TEST_ASSERT( length == bytes2 ); |
| 3820 | |
| 3821 | /* Compare the outputs from the two runs. */ |
| 3822 | TEST_ASSERT( memcmp( output_buffer, export_buffer, capacity ) == 0 ); |
| 3823 | |
| 3824 | exit: |
| 3825 | mbedtls_free( output_buffer ); |
| 3826 | mbedtls_free( export_buffer ); |
| 3827 | psa_generator_abort( &generator ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3828 | psa_destroy_key( base_handle ); |
| 3829 | psa_destroy_key( derived_handle ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 3830 | mbedtls_psa_crypto_free( ); |
| 3831 | } |
| 3832 | /* END_CASE */ |
| 3833 | |
| 3834 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 3835 | void key_agreement_setup( int alg_arg, |
| 3836 | int our_key_type_arg, data_t *our_key_data, |
| 3837 | data_t *peer_key_data, |
| 3838 | int expected_status_arg ) |
| 3839 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3840 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 3841 | psa_algorithm_t alg = alg_arg; |
| 3842 | psa_key_type_t our_key_type = our_key_type_arg; |
| 3843 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 3844 | psa_key_policy_t policy; |
| 3845 | |
| 3846 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3847 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3848 | TEST_ASSERT( psa_allocate_key( our_key_type, |
| 3849 | KEY_BITS_FROM_DATA( our_key_type, |
| 3850 | our_key_data ), |
| 3851 | &our_key ) == PSA_SUCCESS ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 3852 | psa_key_policy_init( &policy ); |
| 3853 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
| 3854 | TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS ); |
| 3855 | TEST_ASSERT( psa_import_key( our_key, our_key_type, |
| 3856 | our_key_data->x, |
| 3857 | our_key_data->len ) == PSA_SUCCESS ); |
| 3858 | |
| 3859 | TEST_ASSERT( psa_key_agreement( &generator, |
| 3860 | our_key, |
| 3861 | peer_key_data->x, peer_key_data->len, |
| 3862 | alg ) == expected_status_arg ); |
| 3863 | |
| 3864 | exit: |
| 3865 | psa_generator_abort( &generator ); |
| 3866 | psa_destroy_key( our_key ); |
| 3867 | mbedtls_psa_crypto_free( ); |
| 3868 | } |
| 3869 | /* END_CASE */ |
| 3870 | |
| 3871 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 3872 | void key_agreement_capacity( int alg_arg, |
| 3873 | int our_key_type_arg, data_t *our_key_data, |
| 3874 | data_t *peer_key_data, |
| 3875 | int expected_capacity_arg ) |
| 3876 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3877 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 3878 | psa_algorithm_t alg = alg_arg; |
| 3879 | psa_key_type_t our_key_type = our_key_type_arg; |
| 3880 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 3881 | psa_key_policy_t policy; |
| 3882 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 3883 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 3884 | |
| 3885 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3886 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3887 | TEST_ASSERT( psa_allocate_key( our_key_type, |
| 3888 | KEY_BITS_FROM_DATA( our_key_type, |
| 3889 | our_key_data ), |
| 3890 | &our_key ) == PSA_SUCCESS ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 3891 | psa_key_policy_init( &policy ); |
| 3892 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
| 3893 | TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS ); |
| 3894 | TEST_ASSERT( psa_import_key( our_key, our_key_type, |
| 3895 | our_key_data->x, |
| 3896 | our_key_data->len ) == PSA_SUCCESS ); |
| 3897 | |
| 3898 | TEST_ASSERT( psa_key_agreement( &generator, |
| 3899 | our_key, |
| 3900 | peer_key_data->x, peer_key_data->len, |
| 3901 | alg ) == PSA_SUCCESS ); |
| 3902 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 3903 | /* Test the advertized capacity. */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 3904 | TEST_ASSERT( psa_get_generator_capacity( |
| 3905 | &generator, &actual_capacity ) == PSA_SUCCESS ); |
| 3906 | TEST_ASSERT( actual_capacity == (size_t) expected_capacity_arg ); |
| 3907 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 3908 | /* Test the actual capacity by reading the output. */ |
| 3909 | while( actual_capacity > sizeof( output ) ) |
| 3910 | { |
| 3911 | TEST_ASSERT( psa_generator_read( &generator, |
| 3912 | output, sizeof( output ) ) == |
| 3913 | PSA_SUCCESS ); |
| 3914 | actual_capacity -= sizeof( output ); |
| 3915 | } |
| 3916 | TEST_ASSERT( psa_generator_read( &generator, |
| 3917 | output, actual_capacity ) == |
| 3918 | PSA_SUCCESS ); |
| 3919 | TEST_ASSERT( psa_generator_read( &generator, output, 1 ) == |
| 3920 | PSA_ERROR_INSUFFICIENT_CAPACITY ); |
| 3921 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 3922 | exit: |
| 3923 | psa_generator_abort( &generator ); |
| 3924 | psa_destroy_key( our_key ); |
| 3925 | mbedtls_psa_crypto_free( ); |
| 3926 | } |
| 3927 | /* END_CASE */ |
| 3928 | |
| 3929 | /* BEGIN_CASE */ |
| 3930 | void key_agreement_output( int alg_arg, |
| 3931 | int our_key_type_arg, data_t *our_key_data, |
| 3932 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 3933 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 3934 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3935 | psa_key_handle_t our_key = 0; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 3936 | psa_algorithm_t alg = alg_arg; |
| 3937 | psa_key_type_t our_key_type = our_key_type_arg; |
| 3938 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 3939 | psa_key_policy_t policy; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 3940 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 3941 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 3942 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 3943 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 3944 | |
| 3945 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 3946 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 3947 | TEST_ASSERT( psa_allocate_key( our_key_type, |
| 3948 | KEY_BITS_FROM_DATA( our_key_type, |
| 3949 | our_key_data ), |
| 3950 | &our_key ) == PSA_SUCCESS ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 3951 | psa_key_policy_init( &policy ); |
| 3952 | psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg ); |
| 3953 | TEST_ASSERT( psa_set_key_policy( our_key, &policy ) == PSA_SUCCESS ); |
| 3954 | TEST_ASSERT( psa_import_key( our_key, our_key_type, |
| 3955 | our_key_data->x, |
| 3956 | our_key_data->len ) == PSA_SUCCESS ); |
| 3957 | |
| 3958 | TEST_ASSERT( psa_key_agreement( &generator, |
| 3959 | our_key, |
| 3960 | peer_key_data->x, peer_key_data->len, |
| 3961 | alg ) == PSA_SUCCESS ); |
| 3962 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 3963 | TEST_ASSERT( |
| 3964 | psa_generator_read( &generator, |
| 3965 | actual_output, |
| 3966 | expected_output1->len ) == PSA_SUCCESS ); |
| 3967 | TEST_ASSERT( memcmp( actual_output, expected_output1->x, |
| 3968 | expected_output1->len ) == 0 ); |
| 3969 | if( expected_output2->len != 0 ) |
| 3970 | { |
| 3971 | TEST_ASSERT( |
| 3972 | psa_generator_read( &generator, |
| 3973 | actual_output, |
| 3974 | expected_output2->len ) == PSA_SUCCESS ); |
| 3975 | TEST_ASSERT( memcmp( actual_output, expected_output2->x, |
| 3976 | expected_output2->len ) == 0 ); |
| 3977 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 3978 | |
| 3979 | exit: |
| 3980 | psa_generator_abort( &generator ); |
| 3981 | psa_destroy_key( our_key ); |
| 3982 | mbedtls_psa_crypto_free( ); |
| 3983 | mbedtls_free( actual_output ); |
| 3984 | } |
| 3985 | /* END_CASE */ |
| 3986 | |
| 3987 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 3988 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 3989 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 3990 | size_t bytes = bytes_arg; |
| 3991 | const unsigned char trail[] = "don't overwrite me"; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3992 | unsigned char *output = NULL; |
| 3993 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 3994 | size_t i; |
| 3995 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 3996 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3997 | ASSERT_ALLOC( output, bytes + sizeof( trail ) ); |
| 3998 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 3999 | memcpy( output + bytes, trail, sizeof( trail ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4000 | |
| 4001 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 4002 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4003 | /* Run several times, to ensure that every output byte will be |
| 4004 | * nonzero at least once with overwhelming probability |
| 4005 | * (2^(-8*number_of_runs)). */ |
| 4006 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4007 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 4008 | if( bytes != 0 ) |
| 4009 | memset( output, 0, bytes ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4010 | TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS ); |
| 4011 | |
| 4012 | /* Check that no more than bytes have been overwritten */ |
| 4013 | TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 ); |
| 4014 | |
| 4015 | for( i = 0; i < bytes; i++ ) |
| 4016 | { |
| 4017 | if( output[i] != 0 ) |
| 4018 | ++changed[i]; |
| 4019 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4020 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4021 | |
| 4022 | /* Check that every byte was changed to nonzero at least once. This |
| 4023 | * validates that psa_generate_random is overwriting every byte of |
| 4024 | * the output buffer. */ |
| 4025 | for( i = 0; i < bytes; i++ ) |
| 4026 | { |
| 4027 | TEST_ASSERT( changed[i] != 0 ); |
| 4028 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4029 | |
| 4030 | exit: |
| 4031 | mbedtls_psa_crypto_free( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 4032 | mbedtls_free( output ); |
| 4033 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 4034 | } |
| 4035 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4036 | |
| 4037 | /* BEGIN_CASE */ |
| 4038 | void generate_key( int type_arg, |
| 4039 | int bits_arg, |
| 4040 | int usage_arg, |
| 4041 | int alg_arg, |
| 4042 | int expected_status_arg ) |
| 4043 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4044 | psa_key_handle_t handle = 0; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4045 | psa_key_type_t type = type_arg; |
| 4046 | psa_key_usage_t usage = usage_arg; |
| 4047 | size_t bits = bits_arg; |
| 4048 | psa_algorithm_t alg = alg_arg; |
| 4049 | psa_status_t expected_status = expected_status_arg; |
| 4050 | psa_key_type_t got_type; |
| 4051 | size_t got_bits; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4052 | psa_status_t expected_info_status = |
| 4053 | expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_EMPTY_SLOT; |
| 4054 | psa_key_policy_t policy; |
| 4055 | |
| 4056 | TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS ); |
| 4057 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4058 | TEST_ASSERT( psa_allocate_key( type, bits, &handle ) == PSA_SUCCESS ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4059 | psa_key_policy_init( &policy ); |
| 4060 | psa_key_policy_set_usage( &policy, usage, alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4061 | TEST_ASSERT( psa_set_key_policy( handle, &policy ) == PSA_SUCCESS ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4062 | |
| 4063 | /* Generate a key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4064 | TEST_ASSERT( psa_generate_key( handle, type, bits, |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4065 | NULL, 0 ) == expected_status ); |
| 4066 | |
| 4067 | /* Test the key information */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4068 | TEST_ASSERT( psa_get_key_information( handle, |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4069 | &got_type, |
| 4070 | &got_bits ) == expected_info_status ); |
| 4071 | if( expected_info_status != PSA_SUCCESS ) |
| 4072 | goto exit; |
| 4073 | TEST_ASSERT( got_type == type ); |
| 4074 | TEST_ASSERT( got_bits == bits ); |
| 4075 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 4076 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4077 | if( ! exercise_key( handle, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 4078 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4079 | |
| 4080 | exit: |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4081 | psa_destroy_key( handle ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 4082 | mbedtls_psa_crypto_free( ); |
| 4083 | } |
| 4084 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 4085 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4086 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
| 4087 | void persistent_key_load_key_from_storage( data_t *data, int type_arg, |
| 4088 | int bits, int usage_arg, |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4089 | int alg_arg, int generation_method, |
| 4090 | int export_status ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4091 | { |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4092 | psa_key_handle_t handle = 0; |
| 4093 | psa_key_handle_t base_key; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4094 | psa_key_type_t type = (psa_key_type_t) type_arg; |
| 4095 | psa_key_type_t type_get; |
| 4096 | size_t bits_get; |
| 4097 | psa_key_policy_t policy_set; |
| 4098 | psa_key_policy_t policy_get; |
| 4099 | psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg; |
| 4100 | psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4101 | psa_key_policy_t base_policy_set; |
| 4102 | psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256); |
| 4103 | psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4104 | unsigned char *first_export = NULL; |
| 4105 | unsigned char *second_export = NULL; |
| 4106 | size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits ); |
| 4107 | size_t first_exported_length; |
| 4108 | size_t second_exported_length; |
| 4109 | |
| 4110 | ASSERT_ALLOC( first_export, export_size ); |
| 4111 | ASSERT_ALLOC( second_export, export_size ); |
| 4112 | |
| 4113 | TEST_ASSERT( psa_crypto_init() == PSA_SUCCESS ); |
| 4114 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4115 | TEST_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1, |
| 4116 | type, bits, |
| 4117 | &handle ) == PSA_SUCCESS ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4118 | psa_key_policy_init( &policy_set ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4119 | psa_key_policy_set_usage( &policy_set, policy_usage, |
| 4120 | policy_alg ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4121 | TEST_ASSERT( psa_set_key_policy( handle, &policy_set ) == PSA_SUCCESS ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4122 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4123 | switch( generation_method ) |
| 4124 | { |
| 4125 | case IMPORT_KEY: |
| 4126 | /* Import the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4127 | TEST_ASSERT( psa_import_key( handle, type, |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4128 | data->x, data->len ) == PSA_SUCCESS ); |
| 4129 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4130 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4131 | case GENERATE_KEY: |
| 4132 | /* Generate a key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4133 | TEST_ASSERT( psa_generate_key( handle, type, bits, |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4134 | NULL, 0 ) == PSA_SUCCESS ); |
| 4135 | break; |
| 4136 | |
| 4137 | case DERIVE_KEY: |
| 4138 | /* Create base key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4139 | TEST_ASSERT( psa_allocate_key( PSA_KEY_TYPE_DERIVE, |
| 4140 | PSA_BYTES_TO_BITS( data->len ), |
| 4141 | &base_key ) == PSA_SUCCESS ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4142 | psa_key_policy_init( &base_policy_set ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4143 | psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE, |
| 4144 | base_policy_alg ); |
| 4145 | TEST_ASSERT( psa_set_key_policy( |
| 4146 | base_key, &base_policy_set ) == PSA_SUCCESS ); |
| 4147 | TEST_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE, |
| 4148 | data->x, data->len ) == PSA_SUCCESS ); |
| 4149 | /* Derive a key. */ |
| 4150 | TEST_ASSERT( psa_key_derivation( &generator, base_key, |
| 4151 | base_policy_alg, |
| 4152 | NULL, 0, NULL, 0, |
| 4153 | export_size ) == PSA_SUCCESS ); |
| 4154 | TEST_ASSERT( psa_generator_import_key( |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4155 | handle, PSA_KEY_TYPE_RAW_DATA, |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4156 | bits, &generator ) == PSA_SUCCESS ); |
| 4157 | break; |
| 4158 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4159 | |
| 4160 | /* Export the key */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4161 | TEST_ASSERT( psa_export_key( handle, first_export, export_size, |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4162 | &first_exported_length ) == export_status ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4163 | |
| 4164 | /* Shutdown and restart */ |
| 4165 | mbedtls_psa_crypto_free(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4166 | TEST_ASSERT( psa_crypto_init() == PSA_SUCCESS ); |
| 4167 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4168 | /* Check key slot still contains key data */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4169 | TEST_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1, |
| 4170 | &handle ) == PSA_SUCCESS ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4171 | TEST_ASSERT( psa_get_key_information( |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4172 | handle, &type_get, &bits_get ) == PSA_SUCCESS ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4173 | TEST_ASSERT( type_get == type ); |
| 4174 | TEST_ASSERT( bits_get == (size_t) bits ); |
| 4175 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4176 | TEST_ASSERT( psa_get_key_policy( handle, &policy_get ) == PSA_SUCCESS ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4177 | TEST_ASSERT( psa_key_policy_get_usage( |
| 4178 | &policy_get ) == policy_usage ); |
| 4179 | TEST_ASSERT( psa_key_policy_get_algorithm( |
| 4180 | &policy_get ) == policy_alg ); |
| 4181 | |
| 4182 | /* Export the key again */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4183 | TEST_ASSERT( psa_export_key( handle, second_export, export_size, |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4184 | &second_exported_length ) == export_status ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4185 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4186 | if( export_status == PSA_SUCCESS ) |
| 4187 | { |
| 4188 | ASSERT_COMPARE( first_export, first_exported_length, |
| 4189 | second_export, second_exported_length ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4190 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4191 | switch( generation_method ) |
| 4192 | { |
| 4193 | case IMPORT_KEY: |
| 4194 | ASSERT_COMPARE( data->x, data->len, |
| 4195 | first_export, first_exported_length ); |
| 4196 | break; |
| 4197 | default: |
| 4198 | break; |
| 4199 | } |
| 4200 | } |
| 4201 | |
| 4202 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4203 | if( ! exercise_key( handle, policy_usage, policy_alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 4204 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4205 | |
| 4206 | exit: |
| 4207 | mbedtls_free( first_export ); |
| 4208 | mbedtls_free( second_export ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 4209 | psa_destroy_key( handle ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 4210 | mbedtls_psa_crypto_free(); |
| 4211 | } |
| 4212 | /* END_CASE */ |