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