blob: 8ed7a7d5c47990ae311a83f3d27524a24705ae4b [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskine1838e822019-06-20 12:40:56 +02008#include "psa_crypto_helpers.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +03009
Gilles Peskinec744d992019-07-30 17:26:54 +020010/* Tests that require more than 128kB of RAM plus change have this symbol
11 * as a dependency. Currently we always define this symbol, so the tests
12 * are always executed. In the future we should make this conditional
13 * so that tests that require a lot of memory are skipped on constrained
14 * platforms. */
15#define HAVE_RAM_AVAILABLE_128k
16
Jaeden Amerof24c7f82018-06-27 17:20:43 +010017/** An invalid export length that will never be set by psa_export_key(). */
18static const size_t INVALID_EXPORT_LENGTH = ~0U;
19
Gilles Peskinef426e0f2019-02-25 17:42:03 +010020/* A hash algorithm that is known to be supported.
21 *
22 * This is used in some smoke tests.
23 */
24#if defined(MBEDTLS_MD2_C)
25#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
26#elif defined(MBEDTLS_MD4_C)
27#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
28#elif defined(MBEDTLS_MD5_C)
29#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
30/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
31 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
32 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
33 * implausible anyway. */
34#elif defined(MBEDTLS_SHA1_C)
35#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
36#elif defined(MBEDTLS_SHA256_C)
37#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
38#elif defined(MBEDTLS_SHA512_C)
39#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
40#elif defined(MBEDTLS_SHA3_C)
41#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
42#else
43#undef KNOWN_SUPPORTED_HASH_ALG
44#endif
45
46/* A block cipher that is known to be supported.
47 *
48 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
49 */
50#if defined(MBEDTLS_AES_C)
51#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
52#elif defined(MBEDTLS_ARIA_C)
53#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
54#elif defined(MBEDTLS_CAMELLIA_C)
55#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
56#undef KNOWN_SUPPORTED_BLOCK_CIPHER
57#endif
58
59/* A MAC mode that is known to be supported.
60 *
61 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
62 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
63 *
64 * This is used in some smoke tests.
65 */
66#if defined(KNOWN_SUPPORTED_HASH_ALG)
67#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
68#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
69#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
70#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
71#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
72#else
73#undef KNOWN_SUPPORTED_MAC_ALG
74#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
75#endif
76
77/* A cipher algorithm and key type that are known to be supported.
78 *
79 * This is used in some smoke tests.
80 */
81#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
82#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
83#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
84#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
85#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
86#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
87#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
88#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
89#else
90#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
91#endif
92#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
93#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
94#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
95#elif defined(MBEDTLS_RC4_C)
96#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
97#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
98#else
99#undef KNOWN_SUPPORTED_CIPHER_ALG
100#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
101#endif
102
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200103/** Test if a buffer contains a constant byte value.
104 *
105 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200106 *
107 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200108 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200109 * \param size Size of the buffer in bytes.
110 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200111 * \return 1 if the buffer is all-bits-zero.
112 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200113 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200114static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200115{
116 size_t i;
117 for( i = 0; i < size; i++ )
118 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200119 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200120 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200121 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200122 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200123}
Gilles Peskine818ca122018-06-20 18:16:48 +0200124
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200125/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
126static int asn1_write_10x( unsigned char **p,
127 unsigned char *start,
128 size_t bits,
129 unsigned char x )
130{
131 int ret;
132 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200133 if( bits == 0 )
134 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
135 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200136 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300137 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200138 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
139 *p -= len;
140 ( *p )[len-1] = x;
141 if( bits % 8 == 0 )
142 ( *p )[1] |= 1;
143 else
144 ( *p )[0] |= 1 << ( bits % 8 );
145 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
146 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
147 MBEDTLS_ASN1_INTEGER ) );
148 return( len );
149}
150
151static int construct_fake_rsa_key( unsigned char *buffer,
152 size_t buffer_size,
153 unsigned char **p,
154 size_t bits,
155 int keypair )
156{
157 size_t half_bits = ( bits + 1 ) / 2;
158 int ret;
159 int len = 0;
160 /* Construct something that looks like a DER encoding of
161 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
162 * RSAPrivateKey ::= SEQUENCE {
163 * version Version,
164 * modulus INTEGER, -- n
165 * publicExponent INTEGER, -- e
166 * privateExponent INTEGER, -- d
167 * prime1 INTEGER, -- p
168 * prime2 INTEGER, -- q
169 * exponent1 INTEGER, -- d mod (p-1)
170 * exponent2 INTEGER, -- d mod (q-1)
171 * coefficient INTEGER, -- (inverse of q) mod p
172 * otherPrimeInfos OtherPrimeInfos OPTIONAL
173 * }
174 * Or, for a public key, the same structure with only
175 * version, modulus and publicExponent.
176 */
177 *p = buffer + buffer_size;
178 if( keypair )
179 {
180 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
181 asn1_write_10x( p, buffer, half_bits, 1 ) );
182 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
183 asn1_write_10x( p, buffer, half_bits, 1 ) );
184 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
185 asn1_write_10x( p, buffer, half_bits, 1 ) );
186 MBEDTLS_ASN1_CHK_ADD( len, /* q */
187 asn1_write_10x( p, buffer, half_bits, 1 ) );
188 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
189 asn1_write_10x( p, buffer, half_bits, 3 ) );
190 MBEDTLS_ASN1_CHK_ADD( len, /* d */
191 asn1_write_10x( p, buffer, bits, 1 ) );
192 }
193 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
194 asn1_write_10x( p, buffer, 17, 1 ) );
195 MBEDTLS_ASN1_CHK_ADD( len, /* n */
196 asn1_write_10x( p, buffer, bits, 1 ) );
197 if( keypair )
198 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
199 mbedtls_asn1_write_int( p, buffer, 0 ) );
200 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
201 {
202 const unsigned char tag =
203 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
204 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
205 }
206 return( len );
207}
208
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100209int exercise_mac_setup( psa_key_type_t key_type,
210 const unsigned char *key_bytes,
211 size_t key_length,
212 psa_algorithm_t alg,
213 psa_mac_operation_t *operation,
214 psa_status_t *status )
215{
216 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200217 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100218
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200219 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
220 psa_set_key_algorithm( &attributes, alg );
221 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +0200222 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length,
223 &handle ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100224
225 *status = psa_mac_sign_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100226 /* Whether setup succeeded or failed, abort must succeed. */
227 PSA_ASSERT( psa_mac_abort( operation ) );
228 /* If setup failed, reproduce the failure, so that the caller can
229 * test the resulting state of the operation object. */
230 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100231 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100232 TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ),
233 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100234 }
235
236 psa_destroy_key( handle );
237 return( 1 );
238
239exit:
240 psa_destroy_key( handle );
241 return( 0 );
242}
243
244int exercise_cipher_setup( psa_key_type_t key_type,
245 const unsigned char *key_bytes,
246 size_t key_length,
247 psa_algorithm_t alg,
248 psa_cipher_operation_t *operation,
249 psa_status_t *status )
250{
251 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200252 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100253
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200254 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
255 psa_set_key_algorithm( &attributes, alg );
256 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +0200257 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length,
258 &handle ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100259
260 *status = psa_cipher_encrypt_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100261 /* Whether setup succeeded or failed, abort must succeed. */
262 PSA_ASSERT( psa_cipher_abort( operation ) );
263 /* If setup failed, reproduce the failure, so that the caller can
264 * test the resulting state of the operation object. */
265 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100266 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100267 TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ),
268 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100269 }
270
271 psa_destroy_key( handle );
272 return( 1 );
273
274exit:
275 psa_destroy_key( handle );
276 return( 0 );
277}
278
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100279static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200280 psa_key_usage_t usage,
281 psa_algorithm_t alg )
282{
Jaeden Amero769ce272019-01-04 11:48:03 +0000283 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200284 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200285 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200286 size_t mac_length = sizeof( mac );
287
288 if( usage & PSA_KEY_USAGE_SIGN )
289 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100290 PSA_ASSERT( psa_mac_sign_setup( &operation,
291 handle, alg ) );
292 PSA_ASSERT( psa_mac_update( &operation,
293 input, sizeof( input ) ) );
294 PSA_ASSERT( psa_mac_sign_finish( &operation,
295 mac, sizeof( mac ),
296 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200297 }
298
299 if( usage & PSA_KEY_USAGE_VERIFY )
300 {
301 psa_status_t verify_status =
302 ( usage & PSA_KEY_USAGE_SIGN ?
303 PSA_SUCCESS :
304 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100305 PSA_ASSERT( psa_mac_verify_setup( &operation,
306 handle, alg ) );
307 PSA_ASSERT( psa_mac_update( &operation,
308 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100309 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
310 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200311 }
312
313 return( 1 );
314
315exit:
316 psa_mac_abort( &operation );
317 return( 0 );
318}
319
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100320static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200321 psa_key_usage_t usage,
322 psa_algorithm_t alg )
323{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000324 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200325 unsigned char iv[16] = {0};
326 size_t iv_length = sizeof( iv );
327 const unsigned char plaintext[16] = "Hello, world...";
328 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
329 size_t ciphertext_length = sizeof( ciphertext );
330 unsigned char decrypted[sizeof( ciphertext )];
331 size_t part_length;
332
333 if( usage & PSA_KEY_USAGE_ENCRYPT )
334 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100335 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
336 handle, alg ) );
337 PSA_ASSERT( psa_cipher_generate_iv( &operation,
338 iv, sizeof( iv ),
339 &iv_length ) );
340 PSA_ASSERT( psa_cipher_update( &operation,
341 plaintext, sizeof( plaintext ),
342 ciphertext, sizeof( ciphertext ),
343 &ciphertext_length ) );
344 PSA_ASSERT( psa_cipher_finish( &operation,
345 ciphertext + ciphertext_length,
346 sizeof( ciphertext ) - ciphertext_length,
347 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200348 ciphertext_length += part_length;
349 }
350
351 if( usage & PSA_KEY_USAGE_DECRYPT )
352 {
353 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200354 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200355 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
356 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200357 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
358 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
359 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
360 * have this macro yet. */
361 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE(
362 psa_get_key_type( &attributes ) );
363 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200364 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100365 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
366 handle, alg ) );
367 PSA_ASSERT( psa_cipher_set_iv( &operation,
368 iv, iv_length ) );
369 PSA_ASSERT( psa_cipher_update( &operation,
370 ciphertext, ciphertext_length,
371 decrypted, sizeof( decrypted ),
372 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200373 status = psa_cipher_finish( &operation,
374 decrypted + part_length,
375 sizeof( decrypted ) - part_length,
376 &part_length );
377 /* For a stream cipher, all inputs are valid. For a block cipher,
378 * if the input is some aribtrary data rather than an actual
379 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200380 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200381 TEST_ASSERT( status == PSA_SUCCESS ||
382 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200383 else
384 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200385 }
386
387 return( 1 );
388
389exit:
390 psa_cipher_abort( &operation );
391 return( 0 );
392}
393
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100394static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200395 psa_key_usage_t usage,
396 psa_algorithm_t alg )
397{
398 unsigned char nonce[16] = {0};
399 size_t nonce_length = sizeof( nonce );
400 unsigned char plaintext[16] = "Hello, world...";
401 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
402 size_t ciphertext_length = sizeof( ciphertext );
403 size_t plaintext_length = sizeof( ciphertext );
404
405 if( usage & PSA_KEY_USAGE_ENCRYPT )
406 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100407 PSA_ASSERT( psa_aead_encrypt( handle, alg,
408 nonce, nonce_length,
409 NULL, 0,
410 plaintext, sizeof( plaintext ),
411 ciphertext, sizeof( ciphertext ),
412 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200413 }
414
415 if( usage & PSA_KEY_USAGE_DECRYPT )
416 {
417 psa_status_t verify_status =
418 ( usage & PSA_KEY_USAGE_ENCRYPT ?
419 PSA_SUCCESS :
420 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100421 TEST_EQUAL( psa_aead_decrypt( handle, alg,
422 nonce, nonce_length,
423 NULL, 0,
424 ciphertext, ciphertext_length,
425 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100426 &plaintext_length ),
427 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200428 }
429
430 return( 1 );
431
432exit:
433 return( 0 );
434}
435
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100436static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200437 psa_key_usage_t usage,
438 psa_algorithm_t alg )
439{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200440 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
441 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200442 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200443 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100444 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
445
446 /* If the policy allows signing with any hash, just pick one. */
447 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
448 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100449#if defined(KNOWN_SUPPORTED_HASH_ALG)
450 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
451 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100452#else
453 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100454 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100455#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100456 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200457
458 if( usage & PSA_KEY_USAGE_SIGN )
459 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200460 /* Some algorithms require the payload to have the size of
461 * the hash encoded in the algorithm. Use this input size
462 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200463 if( hash_alg != 0 )
464 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100465 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
466 payload, payload_length,
467 signature, sizeof( signature ),
468 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200469 }
470
471 if( usage & PSA_KEY_USAGE_VERIFY )
472 {
473 psa_status_t verify_status =
474 ( usage & PSA_KEY_USAGE_SIGN ?
475 PSA_SUCCESS :
476 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100477 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
478 payload, payload_length,
479 signature, signature_length ),
480 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200481 }
482
483 return( 1 );
484
485exit:
486 return( 0 );
487}
488
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100489static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200490 psa_key_usage_t usage,
491 psa_algorithm_t alg )
492{
493 unsigned char plaintext[256] = "Hello, world...";
494 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
495 size_t ciphertext_length = sizeof( ciphertext );
496 size_t plaintext_length = 16;
497
498 if( usage & PSA_KEY_USAGE_ENCRYPT )
499 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100500 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
501 plaintext, plaintext_length,
502 NULL, 0,
503 ciphertext, sizeof( ciphertext ),
504 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200505 }
506
507 if( usage & PSA_KEY_USAGE_DECRYPT )
508 {
509 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100510 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200511 ciphertext, ciphertext_length,
512 NULL, 0,
513 plaintext, sizeof( plaintext ),
514 &plaintext_length );
515 TEST_ASSERT( status == PSA_SUCCESS ||
516 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
517 ( status == PSA_ERROR_INVALID_ARGUMENT ||
518 status == PSA_ERROR_INVALID_PADDING ) ) );
519 }
520
521 return( 1 );
522
523exit:
524 return( 0 );
525}
Gilles Peskine02b75072018-07-01 22:31:34 +0200526
Janos Follathf2815ea2019-07-03 12:41:36 +0100527static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation,
528 psa_key_handle_t handle,
529 psa_algorithm_t alg,
530 unsigned char* input1, size_t input1_length,
531 unsigned char* input2, size_t input2_length,
532 size_t capacity )
533{
534 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
535 if( PSA_ALG_IS_HKDF( alg ) )
536 {
537 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
538 PSA_KEY_DERIVATION_INPUT_SALT,
539 input1, input1_length ) );
540 PSA_ASSERT( psa_key_derivation_input_key( operation,
541 PSA_KEY_DERIVATION_INPUT_SECRET,
542 handle ) );
543 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
544 PSA_KEY_DERIVATION_INPUT_INFO,
545 input2,
546 input2_length ) );
547 }
548 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
549 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
550 {
551 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
552 PSA_KEY_DERIVATION_INPUT_SEED,
553 input1, input1_length ) );
554 PSA_ASSERT( psa_key_derivation_input_key( operation,
555 PSA_KEY_DERIVATION_INPUT_SECRET,
556 handle ) );
557 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
558 PSA_KEY_DERIVATION_INPUT_LABEL,
559 input2, input2_length ) );
560 }
561 else
562 {
563 TEST_ASSERT( ! "Key derivation algorithm not supported" );
564 }
565
Gilles Peskinec744d992019-07-30 17:26:54 +0200566 if( capacity != SIZE_MAX )
567 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100568
569 return( 1 );
570
571exit:
572 return( 0 );
573}
574
575
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100576static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200577 psa_key_usage_t usage,
578 psa_algorithm_t alg )
579{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200580 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100581 unsigned char input1[] = "Input 1";
582 size_t input1_length = sizeof( input1 );
583 unsigned char input2[] = "Input 2";
584 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200585 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100586 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200587
588 if( usage & PSA_KEY_USAGE_DERIVE )
589 {
Janos Follathf2815ea2019-07-03 12:41:36 +0100590 if( !setup_key_derivation_wrap( &operation, handle, alg,
591 input1, input1_length,
592 input2, input2_length, capacity ) )
593 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200594
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200595 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200596 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100597 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200598 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200599 }
600
601 return( 1 );
602
603exit:
604 return( 0 );
605}
606
Gilles Peskinec7998b72018-11-07 18:45:02 +0100607/* We need two keys to exercise key agreement. Exercise the
608 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200609static psa_status_t key_agreement_with_self(
610 psa_key_derivation_operation_t *operation,
611 psa_key_handle_t handle )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100612{
613 psa_key_type_t private_key_type;
614 psa_key_type_t public_key_type;
615 size_t key_bits;
616 uint8_t *public_key = NULL;
617 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200618 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200619 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
620 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200621 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200622 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100623
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200624 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
625 private_key_type = psa_get_key_type( &attributes );
626 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200627 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100628 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
629 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100630 PSA_ASSERT( psa_export_public_key( handle,
631 public_key, public_key_length,
632 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100633
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200634 status = psa_key_derivation_key_agreement(
635 operation, PSA_KEY_DERIVATION_INPUT_SECRET, handle,
636 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100637exit:
638 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200639 psa_reset_key_attributes( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100640 return( status );
641}
642
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200643/* We need two keys to exercise key agreement. Exercise the
644 * private key against its own public key. */
645static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
646 psa_key_handle_t handle )
647{
648 psa_key_type_t private_key_type;
649 psa_key_type_t public_key_type;
650 size_t key_bits;
651 uint8_t *public_key = NULL;
652 size_t public_key_length;
653 uint8_t output[1024];
654 size_t output_length;
655 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200656 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
657 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200658 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200659 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200660
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200661 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
662 private_key_type = psa_get_key_type( &attributes );
663 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200664 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200665 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
666 ASSERT_ALLOC( public_key, public_key_length );
667 PSA_ASSERT( psa_export_public_key( handle,
668 public_key, public_key_length,
669 &public_key_length ) );
670
Gilles Peskinebe697d82019-05-16 18:00:41 +0200671 status = psa_raw_key_agreement( alg, handle,
672 public_key, public_key_length,
673 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200674exit:
675 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200676 psa_reset_key_attributes( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200677 return( status );
678}
679
680static int exercise_raw_key_agreement_key( psa_key_handle_t handle,
681 psa_key_usage_t usage,
682 psa_algorithm_t alg )
683{
684 int ok = 0;
685
686 if( usage & PSA_KEY_USAGE_DERIVE )
687 {
688 /* We need two keys to exercise key agreement. Exercise the
689 * private key against its own public key. */
690 PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) );
691 }
692 ok = 1;
693
694exit:
695 return( ok );
696}
697
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100698static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200699 psa_key_usage_t usage,
700 psa_algorithm_t alg )
701{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200702 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200703 unsigned char output[1];
704 int ok = 0;
705
706 if( usage & PSA_KEY_USAGE_DERIVE )
707 {
708 /* We need two keys to exercise key agreement. Exercise the
709 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200710 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
711 PSA_ASSERT( key_agreement_with_self( &operation, handle ) );
712 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200713 output,
714 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200715 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200716 }
717 ok = 1;
718
719exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200720 return( ok );
721}
722
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200723static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
724 size_t min_bits, size_t max_bits,
725 int must_be_odd )
726{
727 size_t len;
728 size_t actual_bits;
729 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100730 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100731 MBEDTLS_ASN1_INTEGER ),
732 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200733 /* Tolerate a slight departure from DER encoding:
734 * - 0 may be represented by an empty string or a 1-byte string.
735 * - The sign bit may be used as a value bit. */
736 if( ( len == 1 && ( *p )[0] == 0 ) ||
737 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
738 {
739 ++( *p );
740 --len;
741 }
742 if( min_bits == 0 && len == 0 )
743 return( 1 );
744 msb = ( *p )[0];
745 TEST_ASSERT( msb != 0 );
746 actual_bits = 8 * ( len - 1 );
747 while( msb != 0 )
748 {
749 msb >>= 1;
750 ++actual_bits;
751 }
752 TEST_ASSERT( actual_bits >= min_bits );
753 TEST_ASSERT( actual_bits <= max_bits );
754 if( must_be_odd )
755 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
756 *p += len;
757 return( 1 );
758exit:
759 return( 0 );
760}
761
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200762static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
763 uint8_t *exported, size_t exported_length )
764{
765 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100766 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200767 else
768 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200769
770#if defined(MBEDTLS_DES_C)
771 if( type == PSA_KEY_TYPE_DES )
772 {
773 /* Check the parity bits. */
774 unsigned i;
775 for( i = 0; i < bits / 8; i++ )
776 {
777 unsigned bit_count = 0;
778 unsigned m;
779 for( m = 1; m <= 0x100; m <<= 1 )
780 {
781 if( exported[i] & m )
782 ++bit_count;
783 }
784 TEST_ASSERT( bit_count % 2 != 0 );
785 }
786 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200787 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200788#endif
789
790#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200791 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200792 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200793 uint8_t *p = exported;
794 uint8_t *end = exported + exported_length;
795 size_t len;
796 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200797 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200798 * modulus INTEGER, -- n
799 * publicExponent INTEGER, -- e
800 * privateExponent INTEGER, -- d
801 * prime1 INTEGER, -- p
802 * prime2 INTEGER, -- q
803 * exponent1 INTEGER, -- d mod (p-1)
804 * exponent2 INTEGER, -- d mod (q-1)
805 * coefficient INTEGER, -- (inverse of q) mod p
806 * }
807 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100808 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
809 MBEDTLS_ASN1_SEQUENCE |
810 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
811 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200812 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
813 goto exit;
814 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
815 goto exit;
816 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
817 goto exit;
818 /* Require d to be at least half the size of n. */
819 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
820 goto exit;
821 /* Require p and q to be at most half the size of n, rounded up. */
822 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
823 goto exit;
824 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
825 goto exit;
826 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
827 goto exit;
828 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
829 goto exit;
830 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
831 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100832 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100833 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200834 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200835#endif /* MBEDTLS_RSA_C */
836
837#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200838 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200839 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100840 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100841 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100842 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200843 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200844#endif /* MBEDTLS_ECP_C */
845
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200846 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
847 {
848 uint8_t *p = exported;
849 uint8_t *end = exported + exported_length;
850 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200851#if defined(MBEDTLS_RSA_C)
852 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
853 {
854 /* RSAPublicKey ::= SEQUENCE {
855 * modulus INTEGER, -- n
856 * publicExponent INTEGER } -- e
857 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100858 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
859 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100860 MBEDTLS_ASN1_CONSTRUCTED ),
861 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100862 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200863 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
864 goto exit;
865 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
866 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100867 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200868 }
869 else
870#endif /* MBEDTLS_RSA_C */
871#if defined(MBEDTLS_ECP_C)
872 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
873 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000874 /* The representation of an ECC public key is:
875 * - The byte 0x04;
876 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
877 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
878 * - where m is the bit size associated with the curve.
Jaeden Amero6b196002019-01-10 10:23:21 +0000879 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100880 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
881 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200882 }
883 else
884#endif /* MBEDTLS_ECP_C */
885 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100886 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200887 mbedtls_snprintf( message, sizeof( message ),
888 "No sanity check for public key type=0x%08lx",
889 (unsigned long) type );
890 test_fail( message, __LINE__, __FILE__ );
891 return( 0 );
892 }
893 }
894 else
895
896 {
897 /* No sanity checks for other types */
898 }
899
900 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200901
902exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200903 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200904}
905
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100906static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200907 psa_key_usage_t usage )
908{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200909 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200910 uint8_t *exported = NULL;
911 size_t exported_size = 0;
912 size_t exported_length = 0;
913 int ok = 0;
914
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200915 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200916
917 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200918 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200919 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100920 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
921 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200922 ok = 1;
923 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +0200924 }
925
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200926 exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ),
927 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200928 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200929
Gilles Peskine8817f612018-12-18 00:18:46 +0100930 PSA_ASSERT( psa_export_key( handle,
931 exported, exported_size,
932 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200933 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
934 psa_get_key_bits( &attributes ),
935 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +0200936
937exit:
938 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200939 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +0200940 return( ok );
941}
942
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100943static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200944{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200945 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200946 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +0200947 uint8_t *exported = NULL;
948 size_t exported_size = 0;
949 size_t exported_length = 0;
950 int ok = 0;
951
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200952 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
953 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200954 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100955 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100956 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200957 return( 1 );
958 }
959
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200960 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200961 psa_get_key_type( &attributes ) );
962 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type,
963 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200964 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200965
Gilles Peskine8817f612018-12-18 00:18:46 +0100966 PSA_ASSERT( psa_export_public_key( handle,
967 exported, exported_size,
968 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200969 ok = exported_key_sanity_check( public_type,
970 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +0200971 exported, exported_length );
972
973exit:
974 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200975 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +0200976 return( ok );
977}
978
Gilles Peskinec9516fb2019-02-05 20:32:06 +0100979/** Do smoke tests on a key.
980 *
981 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
982 * sign/verify, or derivation) that is permitted according to \p usage.
983 * \p usage and \p alg should correspond to the expected policy on the
984 * key.
985 *
986 * Export the key if permitted by \p usage, and check that the output
987 * looks sensible. If \p usage forbids export, check that
988 * \p psa_export_key correctly rejects the attempt. If the key is
989 * asymmetric, also check \p psa_export_public_key.
990 *
991 * If the key fails the tests, this function calls the test framework's
992 * `test_fail` function and returns false. Otherwise this function returns
993 * true. Therefore it should be used as follows:
994 * ```
995 * if( ! exercise_key( ... ) ) goto exit;
996 * ```
997 *
998 * \param handle The key to exercise. It should be capable of performing
999 * \p alg.
1000 * \param usage The usage flags to assume.
1001 * \param alg The algorithm to exercise.
1002 *
1003 * \retval 0 The key failed the smoke tests.
1004 * \retval 1 The key passed the smoke tests.
1005 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001006static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +02001007 psa_key_usage_t usage,
1008 psa_algorithm_t alg )
1009{
1010 int ok;
1011 if( alg == 0 )
1012 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1013 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001014 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001015 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001016 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001017 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001018 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001019 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001020 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001021 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001022 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001023 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001024 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001025 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
1026 ok = exercise_raw_key_agreement_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001027 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001028 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001029 else
1030 {
1031 char message[40];
1032 mbedtls_snprintf( message, sizeof( message ),
1033 "No code to exercise alg=0x%08lx",
1034 (unsigned long) alg );
1035 test_fail( message, __LINE__, __FILE__ );
1036 ok = 0;
1037 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001038
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001039 ok = ok && exercise_export_key( handle, usage );
1040 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +02001041
Gilles Peskine02b75072018-07-01 22:31:34 +02001042 return( ok );
1043}
1044
Gilles Peskine10df3412018-10-25 22:35:43 +02001045static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1046 psa_algorithm_t alg )
1047{
1048 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1049 {
1050 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1051 PSA_KEY_USAGE_VERIFY :
1052 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
1053 }
1054 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1055 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1056 {
1057 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1058 PSA_KEY_USAGE_ENCRYPT :
1059 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1060 }
1061 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1062 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1063 {
1064 return( PSA_KEY_USAGE_DERIVE );
1065 }
1066 else
1067 {
1068 return( 0 );
1069 }
1070
1071}
Darryl Green0c6575a2018-11-07 16:05:30 +00001072
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001073static int test_operations_on_invalid_handle( psa_key_handle_t handle )
1074{
1075 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1076 uint8_t buffer[1];
1077 size_t length;
1078 int ok = 0;
1079
Gilles Peskinec87af662019-05-15 16:12:22 +02001080 psa_set_key_id( &attributes, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001081 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1082 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1083 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
1084 TEST_EQUAL( psa_get_key_attributes( handle, &attributes ),
1085 PSA_ERROR_INVALID_HANDLE );
1086 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001087 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001088 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1089 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1090 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1091 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1092
1093 TEST_EQUAL( psa_export_key( handle,
1094 buffer, sizeof( buffer ), &length ),
1095 PSA_ERROR_INVALID_HANDLE );
1096 TEST_EQUAL( psa_export_public_key( handle,
1097 buffer, sizeof( buffer ), &length ),
1098 PSA_ERROR_INVALID_HANDLE );
1099
1100 TEST_EQUAL( psa_close_key( handle ), PSA_ERROR_INVALID_HANDLE );
1101 TEST_EQUAL( psa_destroy_key( handle ), PSA_ERROR_INVALID_HANDLE );
1102
1103 ok = 1;
1104
1105exit:
1106 psa_reset_key_attributes( &attributes );
1107 return( ok );
1108}
1109
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001110/* An overapproximation of the amount of storage needed for a key of the
1111 * given type and with the given content. The API doesn't make it easy
1112 * to find a good value for the size. The current implementation doesn't
1113 * care about the value anyway. */
1114#define KEY_BITS_FROM_DATA( type, data ) \
1115 ( data )->len
1116
Darryl Green0c6575a2018-11-07 16:05:30 +00001117typedef enum {
1118 IMPORT_KEY = 0,
1119 GENERATE_KEY = 1,
1120 DERIVE_KEY = 2
1121} generate_method;
1122
Gilles Peskinee59236f2018-01-27 23:32:46 +01001123/* END_HEADER */
1124
1125/* BEGIN_DEPENDENCIES
1126 * depends_on:MBEDTLS_PSA_CRYPTO_C
1127 * END_DEPENDENCIES
1128 */
1129
1130/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001131void static_checks( )
1132{
1133 size_t max_truncated_mac_size =
1134 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1135
1136 /* Check that the length for a truncated MAC always fits in the algorithm
1137 * encoding. The shifted mask is the maximum truncated value. The
1138 * untruncated algorithm may be one byte larger. */
1139 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
1140}
1141/* END_CASE */
1142
1143/* BEGIN_CASE */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001144void attributes_set_get( int id_arg, int lifetime_arg,
1145 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001146 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001147{
Gilles Peskine4747d192019-04-17 15:05:45 +02001148 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001149 psa_key_id_t id = id_arg;
1150 psa_key_lifetime_t lifetime = lifetime_arg;
1151 psa_key_usage_t usage_flags = usage_flags_arg;
1152 psa_algorithm_t alg = alg_arg;
1153 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001154 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001155
1156 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1157 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1158 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1159 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1160 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001161 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001162
Gilles Peskinec87af662019-05-15 16:12:22 +02001163 psa_set_key_id( &attributes, id );
1164 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001165 psa_set_key_usage_flags( &attributes, usage_flags );
1166 psa_set_key_algorithm( &attributes, alg );
1167 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001168 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001169
1170 TEST_EQUAL( psa_get_key_id( &attributes ), id );
1171 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1172 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1173 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1174 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001175 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001176
1177 psa_reset_key_attributes( &attributes );
1178
1179 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1180 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1181 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1182 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1183 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001184 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001185}
1186/* END_CASE */
1187
1188/* BEGIN_CASE */
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001189void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg,
1190 int expected_id_arg, int expected_lifetime_arg )
1191{
1192 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1193 psa_key_id_t id1 = id1_arg;
1194 psa_key_lifetime_t lifetime = lifetime_arg;
1195 psa_key_id_t id2 = id2_arg;
1196 psa_key_id_t expected_id = expected_id_arg;
1197 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1198
1199 if( id1_arg != -1 )
1200 psa_set_key_id( &attributes, id1 );
1201 if( lifetime_arg != -1 )
1202 psa_set_key_lifetime( &attributes, lifetime );
1203 if( id2_arg != -1 )
1204 psa_set_key_id( &attributes, id2 );
1205
1206 TEST_EQUAL( psa_get_key_id( &attributes ), expected_id );
1207 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1208}
1209/* END_CASE */
1210
1211/* BEGIN_CASE */
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001212void import( data_t *data, int type_arg,
1213 int attr_bits_arg,
1214 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001215{
1216 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1217 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001218 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001219 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001220 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001221 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001222 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001223
Gilles Peskine8817f612018-12-18 00:18:46 +01001224 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001225
Gilles Peskine4747d192019-04-17 15:05:45 +02001226 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001227 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001228 status = psa_import_key( &attributes, data->x, data->len, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001229 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001230 if( status != PSA_SUCCESS )
1231 goto exit;
1232
1233 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1234 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001235 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001236 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001237
1238 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001239 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001240
1241exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001242 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001243 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001244 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001245}
1246/* END_CASE */
1247
1248/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001249void import_large_key( int type_arg, int byte_size_arg,
1250 int expected_status_arg )
1251{
1252 psa_key_type_t type = type_arg;
1253 size_t byte_size = byte_size_arg;
1254 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1255 psa_status_t expected_status = expected_status_arg;
1256 psa_key_handle_t handle = 0;
1257 psa_status_t status;
1258 uint8_t *buffer = NULL;
1259 size_t buffer_size = byte_size + 1;
1260 size_t n;
1261
1262 /* It would be better to skip the test than fail it if the allocation
1263 * fails, but the test framework doesn't support this yet. */
1264 ASSERT_ALLOC( buffer, buffer_size );
1265 memset( buffer, 'K', byte_size );
1266
1267 PSA_ASSERT( psa_crypto_init( ) );
1268
1269 /* Try importing the key */
1270 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1271 psa_set_key_type( &attributes, type );
1272 status = psa_import_key( &attributes, buffer, byte_size, &handle );
1273 TEST_EQUAL( status, expected_status );
1274
1275 if( status == PSA_SUCCESS )
1276 {
1277 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1278 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1279 TEST_EQUAL( psa_get_key_bits( &attributes ),
1280 PSA_BYTES_TO_BITS( byte_size ) );
1281 memset( buffer, 0, byte_size + 1 );
1282 PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) );
1283 for( n = 0; n < byte_size; n++ )
1284 TEST_EQUAL( buffer[n], 'K' );
1285 for( n = byte_size; n < buffer_size; n++ )
1286 TEST_EQUAL( buffer[n], 0 );
1287 }
1288
1289exit:
1290 psa_destroy_key( handle );
1291 PSA_DONE( );
1292 mbedtls_free( buffer );
1293}
1294/* END_CASE */
1295
1296/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001297void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1298{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001299 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001300 size_t bits = bits_arg;
1301 psa_status_t expected_status = expected_status_arg;
1302 psa_status_t status;
1303 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001304 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001305 size_t buffer_size = /* Slight overapproximations */
1306 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001307 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001308 unsigned char *p;
1309 int ret;
1310 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001311 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001312
Gilles Peskine8817f612018-12-18 00:18:46 +01001313 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001314 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001315
1316 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1317 bits, keypair ) ) >= 0 );
1318 length = ret;
1319
1320 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001321 psa_set_key_type( &attributes, type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001322 status = psa_import_key( &attributes, p, length, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001323 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001324
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001325 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001326 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001327
1328exit:
1329 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001330 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001331}
1332/* END_CASE */
1333
1334/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001335void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001336 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001337 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001338 int expected_bits,
1339 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001340 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001341 int canonical_input )
1342{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001343 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001344 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001345 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001346 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001347 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001348 unsigned char *exported = NULL;
1349 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001350 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001351 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001352 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001353 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001354 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001355
Moran Pekercb088e72018-07-17 17:36:59 +03001356 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001357 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001358 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001359 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001360 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001361
Gilles Peskine4747d192019-04-17 15:05:45 +02001362 psa_set_key_usage_flags( &attributes, usage_arg );
1363 psa_set_key_algorithm( &attributes, alg );
1364 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001365
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001366 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001367 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001368
1369 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001370 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1371 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1372 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001373
1374 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001375 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001376 exported, export_size,
1377 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001378 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001379
1380 /* The exported length must be set by psa_export_key() to a value between 0
1381 * and export_size. On errors, the exported length must be 0. */
1382 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1383 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1384 TEST_ASSERT( exported_length <= export_size );
1385
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001386 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001387 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001388 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001389 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001390 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001391 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001392 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001393
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001394 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001395 goto exit;
1396
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001397 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001398 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001399 else
1400 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001401 psa_key_handle_t handle2;
Gilles Peskine049c7532019-05-15 20:22:09 +02001402 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
1403 &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001404 PSA_ASSERT( psa_export_key( handle2,
1405 reexported,
1406 export_size,
1407 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001408 ASSERT_COMPARE( exported, exported_length,
1409 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001410 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001411 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001412 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001413
1414destroy:
1415 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001416 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001417 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001418
1419exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001420 mbedtls_free( exported );
1421 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001422 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001423 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001424}
1425/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001426
Moran Pekerf709f4a2018-06-06 17:26:04 +03001427/* BEGIN_CASE */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001428void invalid_handle( int handle )
Moran Peker28a38e62018-11-07 16:18:24 +02001429{
Gilles Peskine8817f612018-12-18 00:18:46 +01001430 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001431 test_operations_on_invalid_handle( handle );
Moran Peker28a38e62018-11-07 16:18:24 +02001432
1433exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001434 PSA_DONE( );
Moran Peker28a38e62018-11-07 16:18:24 +02001435}
1436/* END_CASE */
1437
1438/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001439void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001440 int type_arg,
1441 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001442 int export_size_delta,
1443 int expected_export_status_arg,
1444 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001445{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001446 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001447 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001448 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001449 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001450 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001451 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001452 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001453 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001454 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001455
Gilles Peskine8817f612018-12-18 00:18:46 +01001456 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001457
Gilles Peskine4747d192019-04-17 15:05:45 +02001458 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1459 psa_set_key_algorithm( &attributes, alg );
1460 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001461
1462 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001463 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001464
Gilles Peskine49c25912018-10-29 15:15:31 +01001465 /* Export the public key */
1466 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001467 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001468 exported, export_size,
1469 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001470 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001471 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001472 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001473 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001474 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001475 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1476 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001477 TEST_ASSERT( expected_public_key->len <=
1478 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001479 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1480 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001481 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001482
1483exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001484 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001485 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001486 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001487 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001488}
1489/* END_CASE */
1490
Gilles Peskine20035e32018-02-03 22:44:14 +01001491/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001492void import_and_exercise_key( data_t *data,
1493 int type_arg,
1494 int bits_arg,
1495 int alg_arg )
1496{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001497 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001498 psa_key_type_t type = type_arg;
1499 size_t bits = bits_arg;
1500 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001501 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001503 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001504
Gilles Peskine8817f612018-12-18 00:18:46 +01001505 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001506
Gilles Peskine4747d192019-04-17 15:05:45 +02001507 psa_set_key_usage_flags( &attributes, usage );
1508 psa_set_key_algorithm( &attributes, alg );
1509 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001510
1511 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001512 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001513
1514 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001515 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1516 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1517 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001518
1519 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001520 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001521 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001522
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001523 PSA_ASSERT( psa_destroy_key( handle ) );
1524 test_operations_on_invalid_handle( handle );
1525
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001526exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001527 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001528 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001529 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001530}
1531/* END_CASE */
1532
1533/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001534void key_policy( int usage_arg, int alg_arg )
1535{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001536 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001537 psa_algorithm_t alg = alg_arg;
1538 psa_key_usage_t usage = usage_arg;
1539 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1540 unsigned char key[32] = {0};
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001541 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001542
1543 memset( key, 0x2a, sizeof( key ) );
1544
Gilles Peskine8817f612018-12-18 00:18:46 +01001545 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001546
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001547 psa_set_key_usage_flags( &attributes, usage );
1548 psa_set_key_algorithm( &attributes, alg );
1549 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001550
Gilles Peskine73676cb2019-05-15 20:15:10 +02001551 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001552
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001553 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1554 TEST_EQUAL( psa_get_key_type( &attributes ), key_type );
1555 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage );
1556 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001557
1558exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001559 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001560 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001561 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001562}
1563/* END_CASE */
1564
1565/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001566void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001567{
1568 /* Test each valid way of initializing the object, except for `= {0}`, as
1569 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1570 * though it's OK by the C standard. We could test for this, but we'd need
1571 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001572 psa_key_attributes_t func = psa_key_attributes_init( );
1573 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1574 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001575
1576 memset( &zero, 0, sizeof( zero ) );
1577
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001578 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1579 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1580 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001581
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001582 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1583 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1584 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1585
1586 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1587 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1588 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1589
1590 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1591 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1592 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1593
1594 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1595 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1596 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001597}
1598/* END_CASE */
1599
1600/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001601void mac_key_policy( int policy_usage,
1602 int policy_alg,
1603 int key_type,
1604 data_t *key_data,
1605 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001606{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001607 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001608 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001609 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001610 psa_status_t status;
1611 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001612
Gilles Peskine8817f612018-12-18 00:18:46 +01001613 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001614
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001615 psa_set_key_usage_flags( &attributes, policy_usage );
1616 psa_set_key_algorithm( &attributes, policy_alg );
1617 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001618
Gilles Peskine049c7532019-05-15 20:22:09 +02001619 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1620 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001621
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001622 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001623 if( policy_alg == exercise_alg &&
1624 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001625 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001626 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001627 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001628 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001629
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001630 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001631 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001632 if( policy_alg == exercise_alg &&
1633 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001634 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001635 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001636 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001637
1638exit:
1639 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001640 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001641 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001642}
1643/* END_CASE */
1644
1645/* BEGIN_CASE */
1646void cipher_key_policy( int policy_usage,
1647 int policy_alg,
1648 int key_type,
1649 data_t *key_data,
1650 int exercise_alg )
1651{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001652 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001653 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001654 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001655 psa_status_t status;
1656
Gilles Peskine8817f612018-12-18 00:18:46 +01001657 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001658
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001659 psa_set_key_usage_flags( &attributes, policy_usage );
1660 psa_set_key_algorithm( &attributes, policy_alg );
1661 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001662
Gilles Peskine049c7532019-05-15 20:22:09 +02001663 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1664 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001665
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001666 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001667 if( policy_alg == exercise_alg &&
1668 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001669 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001670 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001671 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001672 psa_cipher_abort( &operation );
1673
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001674 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001675 if( policy_alg == exercise_alg &&
1676 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001677 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001678 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001679 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001680
1681exit:
1682 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001683 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001684 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001685}
1686/* END_CASE */
1687
1688/* BEGIN_CASE */
1689void aead_key_policy( int policy_usage,
1690 int policy_alg,
1691 int key_type,
1692 data_t *key_data,
1693 int nonce_length_arg,
1694 int tag_length_arg,
1695 int exercise_alg )
1696{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001697 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001698 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001699 psa_status_t status;
1700 unsigned char nonce[16] = {0};
1701 size_t nonce_length = nonce_length_arg;
1702 unsigned char tag[16];
1703 size_t tag_length = tag_length_arg;
1704 size_t output_length;
1705
1706 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1707 TEST_ASSERT( tag_length <= sizeof( tag ) );
1708
Gilles Peskine8817f612018-12-18 00:18:46 +01001709 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001710
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001711 psa_set_key_usage_flags( &attributes, policy_usage );
1712 psa_set_key_algorithm( &attributes, policy_alg );
1713 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001714
Gilles Peskine049c7532019-05-15 20:22:09 +02001715 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1716 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001717
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001718 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001719 nonce, nonce_length,
1720 NULL, 0,
1721 NULL, 0,
1722 tag, tag_length,
1723 &output_length );
1724 if( policy_alg == exercise_alg &&
1725 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001726 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001727 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001728 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001729
1730 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001731 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001732 nonce, nonce_length,
1733 NULL, 0,
1734 tag, tag_length,
1735 NULL, 0,
1736 &output_length );
1737 if( policy_alg == exercise_alg &&
1738 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001739 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001740 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001741 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001742
1743exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001744 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001745 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001746}
1747/* END_CASE */
1748
1749/* BEGIN_CASE */
1750void asymmetric_encryption_key_policy( int policy_usage,
1751 int policy_alg,
1752 int key_type,
1753 data_t *key_data,
1754 int exercise_alg )
1755{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001756 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001757 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001758 psa_status_t status;
1759 size_t key_bits;
1760 size_t buffer_length;
1761 unsigned char *buffer = NULL;
1762 size_t output_length;
1763
Gilles Peskine8817f612018-12-18 00:18:46 +01001764 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001765
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001766 psa_set_key_usage_flags( &attributes, policy_usage );
1767 psa_set_key_algorithm( &attributes, policy_alg );
1768 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001769
Gilles Peskine049c7532019-05-15 20:22:09 +02001770 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1771 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001772
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001773 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1774 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001775 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1776 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001777 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001778
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001779 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001780 NULL, 0,
1781 NULL, 0,
1782 buffer, buffer_length,
1783 &output_length );
1784 if( policy_alg == exercise_alg &&
1785 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001786 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001787 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001788 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001789
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001790 if( buffer_length != 0 )
1791 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001792 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001793 buffer, buffer_length,
1794 NULL, 0,
1795 buffer, buffer_length,
1796 &output_length );
1797 if( policy_alg == exercise_alg &&
1798 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001799 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001800 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001801 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001802
1803exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001804 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001805 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001806 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001807 mbedtls_free( buffer );
1808}
1809/* END_CASE */
1810
1811/* BEGIN_CASE */
1812void asymmetric_signature_key_policy( int policy_usage,
1813 int policy_alg,
1814 int key_type,
1815 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001816 int exercise_alg,
1817 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001818{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001819 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001820 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001821 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001822 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1823 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1824 * compatible with the policy and `payload_length_arg` is supposed to be
1825 * a valid input length to sign. If `payload_length_arg <= 0`,
1826 * `exercise_alg` is supposed to be forbidden by the policy. */
1827 int compatible_alg = payload_length_arg > 0;
1828 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001829 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1830 size_t signature_length;
1831
Gilles Peskine8817f612018-12-18 00:18:46 +01001832 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001833
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001834 psa_set_key_usage_flags( &attributes, policy_usage );
1835 psa_set_key_algorithm( &attributes, policy_alg );
1836 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001837
Gilles Peskine049c7532019-05-15 20:22:09 +02001838 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1839 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001840
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001841 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001842 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001843 signature, sizeof( signature ),
1844 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001845 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001846 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001847 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001848 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001849
1850 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001851 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001852 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001853 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001854 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001855 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001856 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001857 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001858
1859exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001860 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001861 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001862}
1863/* END_CASE */
1864
Janos Follathba3fab92019-06-11 14:50:16 +01001865/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001866void derive_key_policy( int policy_usage,
1867 int policy_alg,
1868 int key_type,
1869 data_t *key_data,
1870 int exercise_alg )
1871{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001872 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001873 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001874 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001875 psa_status_t status;
1876
Gilles Peskine8817f612018-12-18 00:18:46 +01001877 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001878
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001879 psa_set_key_usage_flags( &attributes, policy_usage );
1880 psa_set_key_algorithm( &attributes, policy_alg );
1881 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001882
Gilles Peskine049c7532019-05-15 20:22:09 +02001883 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1884 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001885
Janos Follathba3fab92019-06-11 14:50:16 +01001886 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1887
1888 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1889 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001890 {
Janos Follathba3fab92019-06-11 14:50:16 +01001891 PSA_ASSERT( psa_key_derivation_input_bytes(
1892 &operation,
1893 PSA_KEY_DERIVATION_INPUT_SEED,
1894 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001895 }
Janos Follathba3fab92019-06-11 14:50:16 +01001896
1897 status = psa_key_derivation_input_key( &operation,
1898 PSA_KEY_DERIVATION_INPUT_SECRET,
1899 handle );
1900
Gilles Peskineea0fb492018-07-12 17:17:20 +02001901 if( policy_alg == exercise_alg &&
1902 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001903 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001904 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001905 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001906
1907exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001908 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001909 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001910 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001911}
1912/* END_CASE */
1913
1914/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001915void agreement_key_policy( int policy_usage,
1916 int policy_alg,
1917 int key_type_arg,
1918 data_t *key_data,
1919 int exercise_alg )
1920{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001921 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001922 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001923 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001924 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001925 psa_status_t status;
1926
Gilles Peskine8817f612018-12-18 00:18:46 +01001927 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001928
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001929 psa_set_key_usage_flags( &attributes, policy_usage );
1930 psa_set_key_algorithm( &attributes, policy_alg );
1931 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001932
Gilles Peskine049c7532019-05-15 20:22:09 +02001933 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1934 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001935
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001936 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1937 status = key_agreement_with_self( &operation, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001938
Gilles Peskine01d718c2018-09-18 12:01:02 +02001939 if( policy_alg == exercise_alg &&
1940 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001941 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001942 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001943 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001944
1945exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001946 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001947 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001948 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001949}
1950/* END_CASE */
1951
1952/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001953void key_policy_alg2( int key_type_arg, data_t *key_data,
1954 int usage_arg, int alg_arg, int alg2_arg )
1955{
1956 psa_key_handle_t handle = 0;
1957 psa_key_type_t key_type = key_type_arg;
1958 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1959 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1960 psa_key_usage_t usage = usage_arg;
1961 psa_algorithm_t alg = alg_arg;
1962 psa_algorithm_t alg2 = alg2_arg;
1963
1964 PSA_ASSERT( psa_crypto_init( ) );
1965
1966 psa_set_key_usage_flags( &attributes, usage );
1967 psa_set_key_algorithm( &attributes, alg );
1968 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1969 psa_set_key_type( &attributes, key_type );
1970 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1971 &handle ) );
1972
1973 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1974 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1975 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1976 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1977
1978 if( ! exercise_key( handle, usage, alg ) )
1979 goto exit;
1980 if( ! exercise_key( handle, usage, alg2 ) )
1981 goto exit;
1982
1983exit:
1984 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001985 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001986}
1987/* END_CASE */
1988
1989/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001990void raw_agreement_key_policy( int policy_usage,
1991 int policy_alg,
1992 int key_type_arg,
1993 data_t *key_data,
1994 int exercise_alg )
1995{
1996 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001998 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001999 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002000 psa_status_t status;
2001
2002 PSA_ASSERT( psa_crypto_init( ) );
2003
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002004 psa_set_key_usage_flags( &attributes, policy_usage );
2005 psa_set_key_algorithm( &attributes, policy_alg );
2006 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002007
Gilles Peskine049c7532019-05-15 20:22:09 +02002008 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2009 &handle ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002010
2011 status = raw_key_agreement_with_self( exercise_alg, handle );
2012
2013 if( policy_alg == exercise_alg &&
2014 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
2015 PSA_ASSERT( status );
2016 else
2017 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2018
2019exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002020 psa_key_derivation_abort( &operation );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002021 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002022 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002023}
2024/* END_CASE */
2025
2026/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002027void copy_success( int source_usage_arg,
2028 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002029 int type_arg, data_t *material,
2030 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002031 int target_usage_arg,
2032 int target_alg_arg, int target_alg2_arg,
2033 int expected_usage_arg,
2034 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002035{
Gilles Peskineca25db92019-04-19 11:43:08 +02002036 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2037 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002038 psa_key_usage_t expected_usage = expected_usage_arg;
2039 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002040 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02002041 psa_key_handle_t source_handle = 0;
2042 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002043 uint8_t *export_buffer = NULL;
2044
Gilles Peskine57ab7212019-01-28 13:03:09 +01002045 PSA_ASSERT( psa_crypto_init( ) );
2046
Gilles Peskineca25db92019-04-19 11:43:08 +02002047 /* Prepare the source key. */
2048 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2049 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002050 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002051 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002052 PSA_ASSERT( psa_import_key( &source_attributes,
2053 material->x, material->len,
2054 &source_handle ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002055 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002056
Gilles Peskineca25db92019-04-19 11:43:08 +02002057 /* Prepare the target attributes. */
2058 if( copy_attributes )
2059 target_attributes = source_attributes;
2060 if( target_usage_arg != -1 )
2061 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2062 if( target_alg_arg != -1 )
2063 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002064 if( target_alg2_arg != -1 )
2065 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002066
2067 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02002068 PSA_ASSERT( psa_copy_key( source_handle,
2069 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002070
2071 /* Destroy the source to ensure that this doesn't affect the target. */
2072 PSA_ASSERT( psa_destroy_key( source_handle ) );
2073
2074 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02002075 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
2076 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2077 psa_get_key_type( &target_attributes ) );
2078 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2079 psa_get_key_bits( &target_attributes ) );
2080 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2081 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002082 TEST_EQUAL( expected_alg2,
2083 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002084 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2085 {
2086 size_t length;
2087 ASSERT_ALLOC( export_buffer, material->len );
2088 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2089 material->len, &length ) );
2090 ASSERT_COMPARE( material->x, material->len,
2091 export_buffer, length );
2092 }
2093 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2094 goto exit;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002095 if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) )
2096 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002097
2098 PSA_ASSERT( psa_close_key( target_handle ) );
2099
2100exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002101 psa_reset_key_attributes( &source_attributes );
2102 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002103 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002104 mbedtls_free( export_buffer );
2105}
2106/* END_CASE */
2107
2108/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002109void copy_fail( int source_usage_arg,
2110 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002111 int type_arg, data_t *material,
2112 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002113 int target_usage_arg,
2114 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002115 int expected_status_arg )
2116{
2117 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2118 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2119 psa_key_handle_t source_handle = 0;
2120 psa_key_handle_t target_handle = 0;
2121
2122 PSA_ASSERT( psa_crypto_init( ) );
2123
2124 /* Prepare the source key. */
2125 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2126 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002127 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002128 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002129 PSA_ASSERT( psa_import_key( &source_attributes,
2130 material->x, material->len,
2131 &source_handle ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002132
2133 /* Prepare the target attributes. */
2134 psa_set_key_type( &target_attributes, target_type_arg );
2135 psa_set_key_bits( &target_attributes, target_bits_arg );
2136 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2137 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002138 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002139
2140 /* Try to copy the key. */
2141 TEST_EQUAL( psa_copy_key( source_handle,
2142 &target_attributes, &target_handle ),
2143 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002144
2145 PSA_ASSERT( psa_destroy_key( source_handle ) );
2146
Gilles Peskine4a644642019-05-03 17:14:08 +02002147exit:
2148 psa_reset_key_attributes( &source_attributes );
2149 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002150 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002151}
2152/* END_CASE */
2153
2154/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002155void hash_operation_init( )
2156{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002157 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002158 /* Test each valid way of initializing the object, except for `= {0}`, as
2159 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2160 * though it's OK by the C standard. We could test for this, but we'd need
2161 * to supress the Clang warning for the test. */
2162 psa_hash_operation_t func = psa_hash_operation_init( );
2163 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2164 psa_hash_operation_t zero;
2165
2166 memset( &zero, 0, sizeof( zero ) );
2167
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002168 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002169 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2170 PSA_ERROR_BAD_STATE );
2171 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2172 PSA_ERROR_BAD_STATE );
2173 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2174 PSA_ERROR_BAD_STATE );
2175
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002176 /* A default hash operation should be abortable without error. */
2177 PSA_ASSERT( psa_hash_abort( &func ) );
2178 PSA_ASSERT( psa_hash_abort( &init ) );
2179 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002180}
2181/* END_CASE */
2182
2183/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002184void hash_setup( int alg_arg,
2185 int expected_status_arg )
2186{
2187 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002188 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002189 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002190 psa_status_t status;
2191
Gilles Peskine8817f612018-12-18 00:18:46 +01002192 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002193
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002194 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002195 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002196
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002197 /* Whether setup succeeded or failed, abort must succeed. */
2198 PSA_ASSERT( psa_hash_abort( &operation ) );
2199
2200 /* If setup failed, reproduce the failure, so as to
2201 * test the resulting state of the operation object. */
2202 if( status != PSA_SUCCESS )
2203 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2204
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002205 /* Now the operation object should be reusable. */
2206#if defined(KNOWN_SUPPORTED_HASH_ALG)
2207 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2208 PSA_ASSERT( psa_hash_abort( &operation ) );
2209#endif
2210
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002211exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002212 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002213}
2214/* END_CASE */
2215
2216/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002217void hash_bad_order( )
2218{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002219 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002220 unsigned char input[] = "";
2221 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002222 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002223 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2224 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2225 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002226 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002227 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002228 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002229
Gilles Peskine8817f612018-12-18 00:18:46 +01002230 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002231
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002232 /* Call setup twice in a row. */
2233 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2234 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2235 PSA_ERROR_BAD_STATE );
2236 PSA_ASSERT( psa_hash_abort( &operation ) );
2237
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002238 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002239 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002240 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002241 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002242
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002243 /* Call update after finish. */
2244 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2245 PSA_ASSERT( psa_hash_finish( &operation,
2246 hash, sizeof( hash ), &hash_len ) );
2247 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002248 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002249 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002250
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002251 /* Call verify without calling setup beforehand. */
2252 TEST_EQUAL( psa_hash_verify( &operation,
2253 valid_hash, sizeof( valid_hash ) ),
2254 PSA_ERROR_BAD_STATE );
2255 PSA_ASSERT( psa_hash_abort( &operation ) );
2256
2257 /* Call verify after finish. */
2258 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2259 PSA_ASSERT( psa_hash_finish( &operation,
2260 hash, sizeof( hash ), &hash_len ) );
2261 TEST_EQUAL( psa_hash_verify( &operation,
2262 valid_hash, sizeof( valid_hash ) ),
2263 PSA_ERROR_BAD_STATE );
2264 PSA_ASSERT( psa_hash_abort( &operation ) );
2265
2266 /* Call verify twice in a row. */
2267 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2268 PSA_ASSERT( psa_hash_verify( &operation,
2269 valid_hash, sizeof( valid_hash ) ) );
2270 TEST_EQUAL( psa_hash_verify( &operation,
2271 valid_hash, sizeof( valid_hash ) ),
2272 PSA_ERROR_BAD_STATE );
2273 PSA_ASSERT( psa_hash_abort( &operation ) );
2274
2275 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002276 TEST_EQUAL( psa_hash_finish( &operation,
2277 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002278 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002279 PSA_ASSERT( psa_hash_abort( &operation ) );
2280
2281 /* Call finish twice in a row. */
2282 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2283 PSA_ASSERT( psa_hash_finish( &operation,
2284 hash, sizeof( hash ), &hash_len ) );
2285 TEST_EQUAL( psa_hash_finish( &operation,
2286 hash, sizeof( hash ), &hash_len ),
2287 PSA_ERROR_BAD_STATE );
2288 PSA_ASSERT( psa_hash_abort( &operation ) );
2289
2290 /* Call finish after calling verify. */
2291 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2292 PSA_ASSERT( psa_hash_verify( &operation,
2293 valid_hash, sizeof( valid_hash ) ) );
2294 TEST_EQUAL( psa_hash_finish( &operation,
2295 hash, sizeof( hash ), &hash_len ),
2296 PSA_ERROR_BAD_STATE );
2297 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002298
2299exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002300 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002301}
2302/* END_CASE */
2303
itayzafrir27e69452018-11-01 14:26:34 +02002304/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2305void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002306{
2307 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002308 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2309 * appended to it */
2310 unsigned char hash[] = {
2311 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2312 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2313 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002314 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002315 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002316
Gilles Peskine8817f612018-12-18 00:18:46 +01002317 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002318
itayzafrir27e69452018-11-01 14:26:34 +02002319 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002320 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002321 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002322 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002323
itayzafrir27e69452018-11-01 14:26:34 +02002324 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002325 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002326 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002327 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002328
itayzafrir27e69452018-11-01 14:26:34 +02002329 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002330 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002331 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002332 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002333
itayzafrirec93d302018-10-18 18:01:10 +03002334exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002335 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002336}
2337/* END_CASE */
2338
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002339/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2340void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002341{
2342 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002343 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002344 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002345 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002346 size_t hash_len;
2347
Gilles Peskine8817f612018-12-18 00:18:46 +01002348 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002349
itayzafrir58028322018-10-25 10:22:01 +03002350 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002351 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002352 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002353 hash, expected_size - 1, &hash_len ),
2354 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002355
2356exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002357 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002358}
2359/* END_CASE */
2360
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002361/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2362void hash_clone_source_state( )
2363{
2364 psa_algorithm_t alg = PSA_ALG_SHA_256;
2365 unsigned char hash[PSA_HASH_MAX_SIZE];
2366 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2367 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2368 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2369 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2370 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2371 size_t hash_len;
2372
2373 PSA_ASSERT( psa_crypto_init( ) );
2374 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2375
2376 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2377 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2378 PSA_ASSERT( psa_hash_finish( &op_finished,
2379 hash, sizeof( hash ), &hash_len ) );
2380 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2381 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2382
2383 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2384 PSA_ERROR_BAD_STATE );
2385
2386 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2387 PSA_ASSERT( psa_hash_finish( &op_init,
2388 hash, sizeof( hash ), &hash_len ) );
2389 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2390 PSA_ASSERT( psa_hash_finish( &op_finished,
2391 hash, sizeof( hash ), &hash_len ) );
2392 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2393 PSA_ASSERT( psa_hash_finish( &op_aborted,
2394 hash, sizeof( hash ), &hash_len ) );
2395
2396exit:
2397 psa_hash_abort( &op_source );
2398 psa_hash_abort( &op_init );
2399 psa_hash_abort( &op_setup );
2400 psa_hash_abort( &op_finished );
2401 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002402 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002403}
2404/* END_CASE */
2405
2406/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2407void hash_clone_target_state( )
2408{
2409 psa_algorithm_t alg = PSA_ALG_SHA_256;
2410 unsigned char hash[PSA_HASH_MAX_SIZE];
2411 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2412 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2413 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2414 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2415 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2416 size_t hash_len;
2417
2418 PSA_ASSERT( psa_crypto_init( ) );
2419
2420 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2421 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2422 PSA_ASSERT( psa_hash_finish( &op_finished,
2423 hash, sizeof( hash ), &hash_len ) );
2424 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2425 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2426
2427 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2428 PSA_ASSERT( psa_hash_finish( &op_target,
2429 hash, sizeof( hash ), &hash_len ) );
2430
2431 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2432 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2433 PSA_ERROR_BAD_STATE );
2434 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2435 PSA_ERROR_BAD_STATE );
2436
2437exit:
2438 psa_hash_abort( &op_target );
2439 psa_hash_abort( &op_init );
2440 psa_hash_abort( &op_setup );
2441 psa_hash_abort( &op_finished );
2442 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002443 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002444}
2445/* END_CASE */
2446
itayzafrir58028322018-10-25 10:22:01 +03002447/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002448void mac_operation_init( )
2449{
Jaeden Amero252ef282019-02-15 14:05:35 +00002450 const uint8_t input[1] = { 0 };
2451
Jaeden Amero769ce272019-01-04 11:48:03 +00002452 /* Test each valid way of initializing the object, except for `= {0}`, as
2453 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2454 * though it's OK by the C standard. We could test for this, but we'd need
2455 * to supress the Clang warning for the test. */
2456 psa_mac_operation_t func = psa_mac_operation_init( );
2457 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2458 psa_mac_operation_t zero;
2459
2460 memset( &zero, 0, sizeof( zero ) );
2461
Jaeden Amero252ef282019-02-15 14:05:35 +00002462 /* A freshly-initialized MAC operation should not be usable. */
2463 TEST_EQUAL( psa_mac_update( &func,
2464 input, sizeof( input ) ),
2465 PSA_ERROR_BAD_STATE );
2466 TEST_EQUAL( psa_mac_update( &init,
2467 input, sizeof( input ) ),
2468 PSA_ERROR_BAD_STATE );
2469 TEST_EQUAL( psa_mac_update( &zero,
2470 input, sizeof( input ) ),
2471 PSA_ERROR_BAD_STATE );
2472
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002473 /* A default MAC operation should be abortable without error. */
2474 PSA_ASSERT( psa_mac_abort( &func ) );
2475 PSA_ASSERT( psa_mac_abort( &init ) );
2476 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002477}
2478/* END_CASE */
2479
2480/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002481void mac_setup( int key_type_arg,
2482 data_t *key,
2483 int alg_arg,
2484 int expected_status_arg )
2485{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002486 psa_key_type_t key_type = key_type_arg;
2487 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002488 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002489 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002490 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2491#if defined(KNOWN_SUPPORTED_MAC_ALG)
2492 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2493#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002494
Gilles Peskine8817f612018-12-18 00:18:46 +01002495 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002496
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002497 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2498 &operation, &status ) )
2499 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002500 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002501
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002502 /* The operation object should be reusable. */
2503#if defined(KNOWN_SUPPORTED_MAC_ALG)
2504 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2505 smoke_test_key_data,
2506 sizeof( smoke_test_key_data ),
2507 KNOWN_SUPPORTED_MAC_ALG,
2508 &operation, &status ) )
2509 goto exit;
2510 TEST_EQUAL( status, PSA_SUCCESS );
2511#endif
2512
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002513exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002514 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002515}
2516/* END_CASE */
2517
2518/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002519void mac_bad_order( )
2520{
2521 psa_key_handle_t handle = 0;
2522 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2523 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2524 const uint8_t key[] = {
2525 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2526 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2527 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002528 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002529 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2530 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2531 size_t sign_mac_length = 0;
2532 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2533 const uint8_t verify_mac[] = {
2534 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2535 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2536 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2537
2538 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002539 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
2540 psa_set_key_algorithm( &attributes, alg );
2541 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002542
Gilles Peskine73676cb2019-05-15 20:15:10 +02002543 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002544
Jaeden Amero252ef282019-02-15 14:05:35 +00002545 /* Call update without calling setup beforehand. */
2546 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2547 PSA_ERROR_BAD_STATE );
2548 PSA_ASSERT( psa_mac_abort( &operation ) );
2549
2550 /* Call sign finish without calling setup beforehand. */
2551 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2552 &sign_mac_length),
2553 PSA_ERROR_BAD_STATE );
2554 PSA_ASSERT( psa_mac_abort( &operation ) );
2555
2556 /* Call verify finish without calling setup beforehand. */
2557 TEST_EQUAL( psa_mac_verify_finish( &operation,
2558 verify_mac, sizeof( verify_mac ) ),
2559 PSA_ERROR_BAD_STATE );
2560 PSA_ASSERT( psa_mac_abort( &operation ) );
2561
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002562 /* Call setup twice in a row. */
2563 PSA_ASSERT( psa_mac_sign_setup( &operation,
2564 handle, alg ) );
2565 TEST_EQUAL( psa_mac_sign_setup( &operation,
2566 handle, alg ),
2567 PSA_ERROR_BAD_STATE );
2568 PSA_ASSERT( psa_mac_abort( &operation ) );
2569
Jaeden Amero252ef282019-02-15 14:05:35 +00002570 /* Call update after sign finish. */
2571 PSA_ASSERT( psa_mac_sign_setup( &operation,
2572 handle, alg ) );
2573 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2574 PSA_ASSERT( psa_mac_sign_finish( &operation,
2575 sign_mac, sizeof( sign_mac ),
2576 &sign_mac_length ) );
2577 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2578 PSA_ERROR_BAD_STATE );
2579 PSA_ASSERT( psa_mac_abort( &operation ) );
2580
2581 /* Call update after verify finish. */
2582 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002583 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002584 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2585 PSA_ASSERT( psa_mac_verify_finish( &operation,
2586 verify_mac, sizeof( verify_mac ) ) );
2587 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2588 PSA_ERROR_BAD_STATE );
2589 PSA_ASSERT( psa_mac_abort( &operation ) );
2590
2591 /* Call sign finish twice in a row. */
2592 PSA_ASSERT( psa_mac_sign_setup( &operation,
2593 handle, alg ) );
2594 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2595 PSA_ASSERT( psa_mac_sign_finish( &operation,
2596 sign_mac, sizeof( sign_mac ),
2597 &sign_mac_length ) );
2598 TEST_EQUAL( psa_mac_sign_finish( &operation,
2599 sign_mac, sizeof( sign_mac ),
2600 &sign_mac_length ),
2601 PSA_ERROR_BAD_STATE );
2602 PSA_ASSERT( psa_mac_abort( &operation ) );
2603
2604 /* Call verify finish twice in a row. */
2605 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002606 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002607 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2608 PSA_ASSERT( psa_mac_verify_finish( &operation,
2609 verify_mac, sizeof( verify_mac ) ) );
2610 TEST_EQUAL( psa_mac_verify_finish( &operation,
2611 verify_mac, sizeof( verify_mac ) ),
2612 PSA_ERROR_BAD_STATE );
2613 PSA_ASSERT( psa_mac_abort( &operation ) );
2614
2615 /* Setup sign but try verify. */
2616 PSA_ASSERT( psa_mac_sign_setup( &operation,
2617 handle, alg ) );
2618 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2619 TEST_EQUAL( psa_mac_verify_finish( &operation,
2620 verify_mac, sizeof( verify_mac ) ),
2621 PSA_ERROR_BAD_STATE );
2622 PSA_ASSERT( psa_mac_abort( &operation ) );
2623
2624 /* Setup verify but try sign. */
2625 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002626 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002627 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2628 TEST_EQUAL( psa_mac_sign_finish( &operation,
2629 sign_mac, sizeof( sign_mac ),
2630 &sign_mac_length ),
2631 PSA_ERROR_BAD_STATE );
2632 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002633
Gilles Peskine76b29a72019-05-28 14:08:50 +02002634 PSA_ASSERT( psa_destroy_key( handle ) );
2635
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002636exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002637 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002638}
2639/* END_CASE */
2640
2641/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002642void mac_sign( int key_type_arg,
2643 data_t *key,
2644 int alg_arg,
2645 data_t *input,
2646 data_t *expected_mac )
2647{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002648 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002649 psa_key_type_t key_type = key_type_arg;
2650 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002651 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002652 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002653 /* Leave a little extra room in the output buffer. At the end of the
2654 * test, we'll check that the implementation didn't overwrite onto
2655 * this extra room. */
2656 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2657 size_t mac_buffer_size =
2658 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2659 size_t mac_length = 0;
2660
2661 memset( actual_mac, '+', sizeof( actual_mac ) );
2662 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2663 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2664
Gilles Peskine8817f612018-12-18 00:18:46 +01002665 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002666
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002667 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
2668 psa_set_key_algorithm( &attributes, alg );
2669 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002670
Gilles Peskine73676cb2019-05-15 20:15:10 +02002671 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002672
2673 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002674 PSA_ASSERT( psa_mac_sign_setup( &operation,
2675 handle, alg ) );
2676 PSA_ASSERT( psa_mac_update( &operation,
2677 input->x, input->len ) );
2678 PSA_ASSERT( psa_mac_sign_finish( &operation,
2679 actual_mac, mac_buffer_size,
2680 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002681
2682 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002683 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2684 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002685
2686 /* Verify that the end of the buffer is untouched. */
2687 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2688 sizeof( actual_mac ) - mac_length ) );
2689
2690exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002691 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002692 PSA_DONE( );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002693}
2694/* END_CASE */
2695
2696/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002697void mac_verify( int key_type_arg,
2698 data_t *key,
2699 int alg_arg,
2700 data_t *input,
2701 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002702{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002703 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002704 psa_key_type_t key_type = key_type_arg;
2705 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002706 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002707 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002708
Gilles Peskine69c12672018-06-28 00:07:19 +02002709 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2710
Gilles Peskine8817f612018-12-18 00:18:46 +01002711 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002712
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002713 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
2714 psa_set_key_algorithm( &attributes, alg );
2715 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002716
Gilles Peskine73676cb2019-05-15 20:15:10 +02002717 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002718
Gilles Peskine8817f612018-12-18 00:18:46 +01002719 PSA_ASSERT( psa_mac_verify_setup( &operation,
2720 handle, alg ) );
2721 PSA_ASSERT( psa_destroy_key( handle ) );
2722 PSA_ASSERT( psa_mac_update( &operation,
2723 input->x, input->len ) );
2724 PSA_ASSERT( psa_mac_verify_finish( &operation,
2725 expected_mac->x,
2726 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002727
2728exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002729 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002730 PSA_DONE( );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002731}
2732/* END_CASE */
2733
2734/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002735void cipher_operation_init( )
2736{
Jaeden Ameroab439972019-02-15 14:12:05 +00002737 const uint8_t input[1] = { 0 };
2738 unsigned char output[1] = { 0 };
2739 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002740 /* Test each valid way of initializing the object, except for `= {0}`, as
2741 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2742 * though it's OK by the C standard. We could test for this, but we'd need
2743 * to supress the Clang warning for the test. */
2744 psa_cipher_operation_t func = psa_cipher_operation_init( );
2745 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2746 psa_cipher_operation_t zero;
2747
2748 memset( &zero, 0, sizeof( zero ) );
2749
Jaeden Ameroab439972019-02-15 14:12:05 +00002750 /* A freshly-initialized cipher operation should not be usable. */
2751 TEST_EQUAL( psa_cipher_update( &func,
2752 input, sizeof( input ),
2753 output, sizeof( output ),
2754 &output_length ),
2755 PSA_ERROR_BAD_STATE );
2756 TEST_EQUAL( psa_cipher_update( &init,
2757 input, sizeof( input ),
2758 output, sizeof( output ),
2759 &output_length ),
2760 PSA_ERROR_BAD_STATE );
2761 TEST_EQUAL( psa_cipher_update( &zero,
2762 input, sizeof( input ),
2763 output, sizeof( output ),
2764 &output_length ),
2765 PSA_ERROR_BAD_STATE );
2766
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002767 /* A default cipher operation should be abortable without error. */
2768 PSA_ASSERT( psa_cipher_abort( &func ) );
2769 PSA_ASSERT( psa_cipher_abort( &init ) );
2770 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002771}
2772/* END_CASE */
2773
2774/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002775void cipher_setup( int key_type_arg,
2776 data_t *key,
2777 int alg_arg,
2778 int expected_status_arg )
2779{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002780 psa_key_type_t key_type = key_type_arg;
2781 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002782 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002783 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002784 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002785#if defined(KNOWN_SUPPORTED_MAC_ALG)
2786 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2787#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002788
Gilles Peskine8817f612018-12-18 00:18:46 +01002789 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002790
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002791 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2792 &operation, &status ) )
2793 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002794 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002795
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002796 /* The operation object should be reusable. */
2797#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2798 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2799 smoke_test_key_data,
2800 sizeof( smoke_test_key_data ),
2801 KNOWN_SUPPORTED_CIPHER_ALG,
2802 &operation, &status ) )
2803 goto exit;
2804 TEST_EQUAL( status, PSA_SUCCESS );
2805#endif
2806
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002807exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002808 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002809}
2810/* END_CASE */
2811
2812/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002813void cipher_bad_order( )
2814{
2815 psa_key_handle_t handle = 0;
2816 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2817 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002818 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002819 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2820 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2821 const uint8_t key[] = {
2822 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2823 0xaa, 0xaa, 0xaa, 0xaa };
2824 const uint8_t text[] = {
2825 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2826 0xbb, 0xbb, 0xbb, 0xbb };
2827 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2828 size_t length = 0;
2829
2830 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002831 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2832 psa_set_key_algorithm( &attributes, alg );
2833 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02002834 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002835
2836
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002837 /* Call encrypt setup twice in a row. */
2838 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2839 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2840 PSA_ERROR_BAD_STATE );
2841 PSA_ASSERT( psa_cipher_abort( &operation ) );
2842
2843 /* Call decrypt setup twice in a row. */
2844 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2845 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2846 PSA_ERROR_BAD_STATE );
2847 PSA_ASSERT( psa_cipher_abort( &operation ) );
2848
Jaeden Ameroab439972019-02-15 14:12:05 +00002849 /* Generate an IV without calling setup beforehand. */
2850 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2851 buffer, sizeof( buffer ),
2852 &length ),
2853 PSA_ERROR_BAD_STATE );
2854 PSA_ASSERT( psa_cipher_abort( &operation ) );
2855
2856 /* Generate an IV twice in a row. */
2857 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2858 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2859 buffer, sizeof( buffer ),
2860 &length ) );
2861 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2862 buffer, sizeof( buffer ),
2863 &length ),
2864 PSA_ERROR_BAD_STATE );
2865 PSA_ASSERT( psa_cipher_abort( &operation ) );
2866
2867 /* Generate an IV after it's already set. */
2868 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2869 PSA_ASSERT( psa_cipher_set_iv( &operation,
2870 iv, sizeof( iv ) ) );
2871 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2872 buffer, sizeof( buffer ),
2873 &length ),
2874 PSA_ERROR_BAD_STATE );
2875 PSA_ASSERT( psa_cipher_abort( &operation ) );
2876
2877 /* Set an IV without calling setup beforehand. */
2878 TEST_EQUAL( psa_cipher_set_iv( &operation,
2879 iv, sizeof( iv ) ),
2880 PSA_ERROR_BAD_STATE );
2881 PSA_ASSERT( psa_cipher_abort( &operation ) );
2882
2883 /* Set an IV after it's already set. */
2884 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2885 PSA_ASSERT( psa_cipher_set_iv( &operation,
2886 iv, sizeof( iv ) ) );
2887 TEST_EQUAL( psa_cipher_set_iv( &operation,
2888 iv, sizeof( iv ) ),
2889 PSA_ERROR_BAD_STATE );
2890 PSA_ASSERT( psa_cipher_abort( &operation ) );
2891
2892 /* Set an IV after it's already generated. */
2893 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2894 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2895 buffer, sizeof( buffer ),
2896 &length ) );
2897 TEST_EQUAL( psa_cipher_set_iv( &operation,
2898 iv, sizeof( iv ) ),
2899 PSA_ERROR_BAD_STATE );
2900 PSA_ASSERT( psa_cipher_abort( &operation ) );
2901
2902 /* Call update without calling setup beforehand. */
2903 TEST_EQUAL( psa_cipher_update( &operation,
2904 text, sizeof( text ),
2905 buffer, sizeof( buffer ),
2906 &length ),
2907 PSA_ERROR_BAD_STATE );
2908 PSA_ASSERT( psa_cipher_abort( &operation ) );
2909
2910 /* Call update without an IV where an IV is required. */
2911 TEST_EQUAL( psa_cipher_update( &operation,
2912 text, sizeof( text ),
2913 buffer, sizeof( buffer ),
2914 &length ),
2915 PSA_ERROR_BAD_STATE );
2916 PSA_ASSERT( psa_cipher_abort( &operation ) );
2917
2918 /* Call update after finish. */
2919 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2920 PSA_ASSERT( psa_cipher_set_iv( &operation,
2921 iv, sizeof( iv ) ) );
2922 PSA_ASSERT( psa_cipher_finish( &operation,
2923 buffer, sizeof( buffer ), &length ) );
2924 TEST_EQUAL( psa_cipher_update( &operation,
2925 text, sizeof( text ),
2926 buffer, sizeof( buffer ),
2927 &length ),
2928 PSA_ERROR_BAD_STATE );
2929 PSA_ASSERT( psa_cipher_abort( &operation ) );
2930
2931 /* Call finish without calling setup beforehand. */
2932 TEST_EQUAL( psa_cipher_finish( &operation,
2933 buffer, sizeof( buffer ), &length ),
2934 PSA_ERROR_BAD_STATE );
2935 PSA_ASSERT( psa_cipher_abort( &operation ) );
2936
2937 /* Call finish without an IV where an IV is required. */
2938 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2939 /* Not calling update means we are encrypting an empty buffer, which is OK
2940 * for cipher modes with padding. */
2941 TEST_EQUAL( psa_cipher_finish( &operation,
2942 buffer, sizeof( buffer ), &length ),
2943 PSA_ERROR_BAD_STATE );
2944 PSA_ASSERT( psa_cipher_abort( &operation ) );
2945
2946 /* Call finish twice in a row. */
2947 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2948 PSA_ASSERT( psa_cipher_set_iv( &operation,
2949 iv, sizeof( iv ) ) );
2950 PSA_ASSERT( psa_cipher_finish( &operation,
2951 buffer, sizeof( buffer ), &length ) );
2952 TEST_EQUAL( psa_cipher_finish( &operation,
2953 buffer, sizeof( buffer ), &length ),
2954 PSA_ERROR_BAD_STATE );
2955 PSA_ASSERT( psa_cipher_abort( &operation ) );
2956
Gilles Peskine76b29a72019-05-28 14:08:50 +02002957 PSA_ASSERT( psa_destroy_key( handle ) );
2958
Jaeden Ameroab439972019-02-15 14:12:05 +00002959exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002960 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002961}
2962/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002963
Gilles Peskine50e586b2018-06-08 14:28:46 +02002964/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002965void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02002966 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002967 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002968 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002969{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002970 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002971 psa_status_t status;
2972 psa_key_type_t key_type = key_type_arg;
2973 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002974 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002975 unsigned char *output = NULL;
2976 size_t output_buffer_size = 0;
2977 size_t function_output_length = 0;
2978 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002979 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002980 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002981
Gilles Peskine8817f612018-12-18 00:18:46 +01002982 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002983
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002984 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2985 psa_set_key_algorithm( &attributes, alg );
2986 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002987
Gilles Peskine73676cb2019-05-15 20:15:10 +02002988 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002989
Gilles Peskine8817f612018-12-18 00:18:46 +01002990 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2991 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002992
Gilles Peskine423005e2019-05-06 15:22:57 +02002993 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002994 output_buffer_size = ( (size_t) input->len +
2995 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002996 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002997
Gilles Peskine8817f612018-12-18 00:18:46 +01002998 PSA_ASSERT( psa_cipher_update( &operation,
2999 input->x, input->len,
3000 output, output_buffer_size,
3001 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003002 total_output_length += function_output_length;
3003 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003004 output + total_output_length,
3005 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003006 &function_output_length );
3007 total_output_length += function_output_length;
3008
Gilles Peskinefe11b722018-12-18 00:24:04 +01003009 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003010 if( expected_status == PSA_SUCCESS )
3011 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003012 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003013 ASSERT_COMPARE( expected_output->x, expected_output->len,
3014 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003015 }
3016
3017exit:
3018 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003019 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003020 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003021}
3022/* END_CASE */
3023
3024/* BEGIN_CASE */
3025void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003026 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003027 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003028 int first_part_size_arg,
3029 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003030 data_t *expected_output )
3031{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003032 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003033 psa_key_type_t key_type = key_type_arg;
3034 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003035 size_t first_part_size = first_part_size_arg;
3036 size_t output1_length = output1_length_arg;
3037 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003038 unsigned char *output = NULL;
3039 size_t output_buffer_size = 0;
3040 size_t function_output_length = 0;
3041 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003042 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003043 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003044
Gilles Peskine8817f612018-12-18 00:18:46 +01003045 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003046
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003047 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3048 psa_set_key_algorithm( &attributes, alg );
3049 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003050
Gilles Peskine73676cb2019-05-15 20:15:10 +02003051 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003052
Gilles Peskine8817f612018-12-18 00:18:46 +01003053 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3054 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003055
Gilles Peskine423005e2019-05-06 15:22:57 +02003056 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003057 output_buffer_size = ( (size_t) input->len +
3058 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003059 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003060
Gilles Peskinee0866522019-02-19 19:44:00 +01003061 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003062 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3063 output, output_buffer_size,
3064 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003065 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003066 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003067 PSA_ASSERT( psa_cipher_update( &operation,
3068 input->x + first_part_size,
3069 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003070 output + total_output_length,
3071 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003072 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003073 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003074 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003075 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003076 output + total_output_length,
3077 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003078 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003079 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003080 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003081
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003082 ASSERT_COMPARE( expected_output->x, expected_output->len,
3083 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003084
3085exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003086 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003087 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003088 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003089}
3090/* END_CASE */
3091
3092/* BEGIN_CASE */
3093void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003094 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003095 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003096 int first_part_size_arg,
3097 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003098 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003099{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003100 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003101
3102 psa_key_type_t key_type = key_type_arg;
3103 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003104 size_t first_part_size = first_part_size_arg;
3105 size_t output1_length = output1_length_arg;
3106 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003107 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003108 size_t output_buffer_size = 0;
3109 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003110 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003111 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003112 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003113
Gilles Peskine8817f612018-12-18 00:18:46 +01003114 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003115
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003116 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3117 psa_set_key_algorithm( &attributes, alg );
3118 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003119
Gilles Peskine73676cb2019-05-15 20:15:10 +02003120 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003121
Gilles Peskine8817f612018-12-18 00:18:46 +01003122 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3123 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003124
Gilles Peskine423005e2019-05-06 15:22:57 +02003125 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003126
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003127 output_buffer_size = ( (size_t) input->len +
3128 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003129 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003130
Gilles Peskinee0866522019-02-19 19:44:00 +01003131 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003132 PSA_ASSERT( psa_cipher_update( &operation,
3133 input->x, first_part_size,
3134 output, output_buffer_size,
3135 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003136 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003137 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003138 PSA_ASSERT( psa_cipher_update( &operation,
3139 input->x + first_part_size,
3140 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003141 output + total_output_length,
3142 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003143 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003144 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003145 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003146 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003147 output + total_output_length,
3148 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003149 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003150 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003151 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003152
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003153 ASSERT_COMPARE( expected_output->x, expected_output->len,
3154 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003155
3156exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003157 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003158 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003159 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003160}
3161/* END_CASE */
3162
Gilles Peskine50e586b2018-06-08 14:28:46 +02003163/* BEGIN_CASE */
3164void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003165 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003166 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003167 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003168{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003169 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003170 psa_status_t status;
3171 psa_key_type_t key_type = key_type_arg;
3172 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003173 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003174 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003175 size_t output_buffer_size = 0;
3176 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003177 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003178 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003179 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003180
Gilles Peskine8817f612018-12-18 00:18:46 +01003181 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003182
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003183 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3184 psa_set_key_algorithm( &attributes, alg );
3185 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003186
Gilles Peskine73676cb2019-05-15 20:15:10 +02003187 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003188
Gilles Peskine8817f612018-12-18 00:18:46 +01003189 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3190 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003191
Gilles Peskine423005e2019-05-06 15:22:57 +02003192 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003193
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003194 output_buffer_size = ( (size_t) input->len +
3195 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003196 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003197
Gilles Peskine8817f612018-12-18 00:18:46 +01003198 PSA_ASSERT( psa_cipher_update( &operation,
3199 input->x, input->len,
3200 output, output_buffer_size,
3201 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003202 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003203 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003204 output + total_output_length,
3205 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003206 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003207 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003208 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003209
3210 if( expected_status == PSA_SUCCESS )
3211 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003212 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003213 ASSERT_COMPARE( expected_output->x, expected_output->len,
3214 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003215 }
3216
Gilles Peskine50e586b2018-06-08 14:28:46 +02003217exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003218 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003219 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003220 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003221}
3222/* END_CASE */
3223
Gilles Peskine50e586b2018-06-08 14:28:46 +02003224/* BEGIN_CASE */
3225void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003226 data_t *key,
3227 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003228{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003229 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003230 psa_key_type_t key_type = key_type_arg;
3231 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003232 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003233 size_t iv_size = 16;
3234 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003235 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003236 size_t output1_size = 0;
3237 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003238 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003239 size_t output2_size = 0;
3240 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003241 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003242 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3243 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003244 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003245
Gilles Peskine8817f612018-12-18 00:18:46 +01003246 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003247
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003248 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3249 psa_set_key_algorithm( &attributes, alg );
3250 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003251
Gilles Peskine73676cb2019-05-15 20:15:10 +02003252 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003253
Gilles Peskine8817f612018-12-18 00:18:46 +01003254 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3255 handle, alg ) );
3256 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3257 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003258
Gilles Peskine8817f612018-12-18 00:18:46 +01003259 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3260 iv, iv_size,
3261 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003262 output1_size = ( (size_t) input->len +
3263 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003264 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003265
Gilles Peskine8817f612018-12-18 00:18:46 +01003266 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3267 output1, output1_size,
3268 &output1_length ) );
3269 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003270 output1 + output1_length,
3271 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003272 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003273
Gilles Peskine048b7f02018-06-08 14:20:49 +02003274 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003275
Gilles Peskine8817f612018-12-18 00:18:46 +01003276 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003277
3278 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003279 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003280
Gilles Peskine8817f612018-12-18 00:18:46 +01003281 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3282 iv, iv_length ) );
3283 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3284 output2, output2_size,
3285 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003286 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003287 PSA_ASSERT( psa_cipher_finish( &operation2,
3288 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003289 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003290 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003291
Gilles Peskine048b7f02018-06-08 14:20:49 +02003292 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003293
Gilles Peskine8817f612018-12-18 00:18:46 +01003294 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003295
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003296 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003297
3298exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003299 mbedtls_free( output1 );
3300 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003301 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003302 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003303}
3304/* END_CASE */
3305
3306/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003307void cipher_verify_output_multipart( int alg_arg,
3308 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003309 data_t *key,
3310 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003311 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003312{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003313 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003314 psa_key_type_t key_type = key_type_arg;
3315 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003316 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003317 unsigned char iv[16] = {0};
3318 size_t iv_size = 16;
3319 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003320 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003321 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003322 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003323 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003324 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003325 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003326 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003327 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3328 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003329 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003330
Gilles Peskine8817f612018-12-18 00:18:46 +01003331 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003332
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003333 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3334 psa_set_key_algorithm( &attributes, alg );
3335 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003336
Gilles Peskine73676cb2019-05-15 20:15:10 +02003337 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003338
Gilles Peskine8817f612018-12-18 00:18:46 +01003339 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3340 handle, alg ) );
3341 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3342 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003343
Gilles Peskine8817f612018-12-18 00:18:46 +01003344 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3345 iv, iv_size,
3346 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003347 output1_buffer_size = ( (size_t) input->len +
3348 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003349 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003350
Gilles Peskinee0866522019-02-19 19:44:00 +01003351 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003352
Gilles Peskine8817f612018-12-18 00:18:46 +01003353 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3354 output1, output1_buffer_size,
3355 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003356 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003357
Gilles Peskine8817f612018-12-18 00:18:46 +01003358 PSA_ASSERT( psa_cipher_update( &operation1,
3359 input->x + first_part_size,
3360 input->len - first_part_size,
3361 output1, output1_buffer_size,
3362 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003363 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003364
Gilles Peskine8817f612018-12-18 00:18:46 +01003365 PSA_ASSERT( psa_cipher_finish( &operation1,
3366 output1 + output1_length,
3367 output1_buffer_size - output1_length,
3368 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003369 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003370
Gilles Peskine8817f612018-12-18 00:18:46 +01003371 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003372
Gilles Peskine048b7f02018-06-08 14:20:49 +02003373 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003374 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003375
Gilles Peskine8817f612018-12-18 00:18:46 +01003376 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3377 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003378
Gilles Peskine8817f612018-12-18 00:18:46 +01003379 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3380 output2, output2_buffer_size,
3381 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003382 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003383
Gilles Peskine8817f612018-12-18 00:18:46 +01003384 PSA_ASSERT( psa_cipher_update( &operation2,
3385 output1 + first_part_size,
3386 output1_length - first_part_size,
3387 output2, output2_buffer_size,
3388 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003389 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003390
Gilles Peskine8817f612018-12-18 00:18:46 +01003391 PSA_ASSERT( psa_cipher_finish( &operation2,
3392 output2 + output2_length,
3393 output2_buffer_size - output2_length,
3394 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003395 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003396
Gilles Peskine8817f612018-12-18 00:18:46 +01003397 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003398
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003399 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003400
3401exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003402 mbedtls_free( output1 );
3403 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003404 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003405 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003406}
3407/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003408
Gilles Peskine20035e32018-02-03 22:44:14 +01003409/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003410void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003411 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003412 data_t *nonce,
3413 data_t *additional_data,
3414 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003415 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003416{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003417 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003418 psa_key_type_t key_type = key_type_arg;
3419 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003420 unsigned char *output_data = NULL;
3421 size_t output_size = 0;
3422 size_t output_length = 0;
3423 unsigned char *output_data2 = NULL;
3424 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003425 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003426 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003427 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003428
Gilles Peskine4abf7412018-06-18 16:35:34 +02003429 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003430 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3431 * should be exact. */
3432 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3433 TEST_EQUAL( output_size,
3434 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003435 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003436
Gilles Peskine8817f612018-12-18 00:18:46 +01003437 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003438
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003439 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3440 psa_set_key_algorithm( &attributes, alg );
3441 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003442
Gilles Peskine049c7532019-05-15 20:22:09 +02003443 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3444 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003445
Gilles Peskinefe11b722018-12-18 00:24:04 +01003446 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3447 nonce->x, nonce->len,
3448 additional_data->x,
3449 additional_data->len,
3450 input_data->x, input_data->len,
3451 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003452 &output_length ),
3453 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003454
3455 if( PSA_SUCCESS == expected_result )
3456 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003457 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003458
Gilles Peskine003a4a92019-05-14 16:09:40 +02003459 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3460 * should be exact. */
3461 TEST_EQUAL( input_data->len,
3462 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3463
Gilles Peskinefe11b722018-12-18 00:24:04 +01003464 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3465 nonce->x, nonce->len,
3466 additional_data->x,
3467 additional_data->len,
3468 output_data, output_length,
3469 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003470 &output_length2 ),
3471 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003472
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003473 ASSERT_COMPARE( input_data->x, input_data->len,
3474 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003475 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003476
Gilles Peskinea1cac842018-06-11 19:33:02 +02003477exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003478 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003479 mbedtls_free( output_data );
3480 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003481 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003482}
3483/* END_CASE */
3484
3485/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003486void aead_encrypt( int key_type_arg, data_t *key_data,
3487 int alg_arg,
3488 data_t *nonce,
3489 data_t *additional_data,
3490 data_t *input_data,
3491 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003492{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003493 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003494 psa_key_type_t key_type = key_type_arg;
3495 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003496 unsigned char *output_data = NULL;
3497 size_t output_size = 0;
3498 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003499 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003500 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003501
Gilles Peskine4abf7412018-06-18 16:35:34 +02003502 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003503 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3504 * should be exact. */
3505 TEST_EQUAL( output_size,
3506 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003507 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003508
Gilles Peskine8817f612018-12-18 00:18:46 +01003509 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003510
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003511 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3512 psa_set_key_algorithm( &attributes, alg );
3513 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003514
Gilles Peskine049c7532019-05-15 20:22:09 +02003515 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3516 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003517
Gilles Peskine8817f612018-12-18 00:18:46 +01003518 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3519 nonce->x, nonce->len,
3520 additional_data->x, additional_data->len,
3521 input_data->x, input_data->len,
3522 output_data, output_size,
3523 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003524
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003525 ASSERT_COMPARE( expected_result->x, expected_result->len,
3526 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003527
Gilles Peskinea1cac842018-06-11 19:33:02 +02003528exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003529 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003530 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003531 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003532}
3533/* END_CASE */
3534
3535/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003536void aead_decrypt( int key_type_arg, data_t *key_data,
3537 int alg_arg,
3538 data_t *nonce,
3539 data_t *additional_data,
3540 data_t *input_data,
3541 data_t *expected_data,
3542 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003543{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003544 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003545 psa_key_type_t key_type = key_type_arg;
3546 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003547 unsigned char *output_data = NULL;
3548 size_t output_size = 0;
3549 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003550 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003551 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003552 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003553
Gilles Peskine003a4a92019-05-14 16:09:40 +02003554 output_size = input_data->len - tag_length;
3555 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3556 * should be exact. */
3557 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3558 TEST_EQUAL( output_size,
3559 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003560 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003561
Gilles Peskine8817f612018-12-18 00:18:46 +01003562 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003563
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003564 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3565 psa_set_key_algorithm( &attributes, alg );
3566 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003567
Gilles Peskine049c7532019-05-15 20:22:09 +02003568 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3569 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003570
Gilles Peskinefe11b722018-12-18 00:24:04 +01003571 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3572 nonce->x, nonce->len,
3573 additional_data->x,
3574 additional_data->len,
3575 input_data->x, input_data->len,
3576 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003577 &output_length ),
3578 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003579
Gilles Peskine2d277862018-06-18 15:41:12 +02003580 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003581 ASSERT_COMPARE( expected_data->x, expected_data->len,
3582 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003583
Gilles Peskinea1cac842018-06-11 19:33:02 +02003584exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003585 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003586 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003587 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003588}
3589/* END_CASE */
3590
3591/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003592void signature_size( int type_arg,
3593 int bits,
3594 int alg_arg,
3595 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003596{
3597 psa_key_type_t type = type_arg;
3598 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003599 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003600 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003601exit:
3602 ;
3603}
3604/* END_CASE */
3605
3606/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003607void sign_deterministic( int key_type_arg, data_t *key_data,
3608 int alg_arg, data_t *input_data,
3609 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003610{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003611 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003612 psa_key_type_t key_type = key_type_arg;
3613 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003614 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003615 unsigned char *signature = NULL;
3616 size_t signature_size;
3617 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003618 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003619
Gilles Peskine8817f612018-12-18 00:18:46 +01003620 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003621
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003622 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3623 psa_set_key_algorithm( &attributes, alg );
3624 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003625
Gilles Peskine049c7532019-05-15 20:22:09 +02003626 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3627 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003628 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3629 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003630
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003631 /* Allocate a buffer which has the size advertized by the
3632 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003633 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3634 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003635 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003636 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003637 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003638
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003639 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003640 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3641 input_data->x, input_data->len,
3642 signature, signature_size,
3643 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003644 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003645 ASSERT_COMPARE( output_data->x, output_data->len,
3646 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003647
3648exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003649 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003650 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003651 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003652 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003653}
3654/* END_CASE */
3655
3656/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003657void sign_fail( int key_type_arg, data_t *key_data,
3658 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003659 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003660{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003661 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003662 psa_key_type_t key_type = key_type_arg;
3663 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003664 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003665 psa_status_t actual_status;
3666 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003667 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003668 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003669 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003670
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003671 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003672
Gilles Peskine8817f612018-12-18 00:18:46 +01003673 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003674
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003675 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3676 psa_set_key_algorithm( &attributes, alg );
3677 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003678
Gilles Peskine049c7532019-05-15 20:22:09 +02003679 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3680 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003681
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003682 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003683 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003684 signature, signature_size,
3685 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003686 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003687 /* The value of *signature_length is unspecified on error, but
3688 * whatever it is, it should be less than signature_size, so that
3689 * if the caller tries to read *signature_length bytes without
3690 * checking the error code then they don't overflow a buffer. */
3691 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003692
3693exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003694 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003695 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003696 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003697 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003698}
3699/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003700
3701/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003702void sign_verify( int key_type_arg, data_t *key_data,
3703 int alg_arg, data_t *input_data )
3704{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003705 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003706 psa_key_type_t key_type = key_type_arg;
3707 psa_algorithm_t alg = alg_arg;
3708 size_t key_bits;
3709 unsigned char *signature = NULL;
3710 size_t signature_size;
3711 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003713
Gilles Peskine8817f612018-12-18 00:18:46 +01003714 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003715
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003716 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
3717 psa_set_key_algorithm( &attributes, alg );
3718 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003719
Gilles Peskine049c7532019-05-15 20:22:09 +02003720 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3721 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003722 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3723 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003724
3725 /* Allocate a buffer which has the size advertized by the
3726 * library. */
3727 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3728 key_bits, alg );
3729 TEST_ASSERT( signature_size != 0 );
3730 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003731 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003732
3733 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003734 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3735 input_data->x, input_data->len,
3736 signature, signature_size,
3737 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003738 /* Check that the signature length looks sensible. */
3739 TEST_ASSERT( signature_length <= signature_size );
3740 TEST_ASSERT( signature_length > 0 );
3741
3742 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003743 PSA_ASSERT( psa_asymmetric_verify(
3744 handle, alg,
3745 input_data->x, input_data->len,
3746 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003747
3748 if( input_data->len != 0 )
3749 {
3750 /* Flip a bit in the input and verify that the signature is now
3751 * detected as invalid. Flip a bit at the beginning, not at the end,
3752 * because ECDSA may ignore the last few bits of the input. */
3753 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003754 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3755 input_data->x, input_data->len,
3756 signature, signature_length ),
3757 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003758 }
3759
3760exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003761 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003762 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003763 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003764 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003765}
3766/* END_CASE */
3767
3768/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003769void asymmetric_verify( int key_type_arg, data_t *key_data,
3770 int alg_arg, data_t *hash_data,
3771 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003772{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003773 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003774 psa_key_type_t key_type = key_type_arg;
3775 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003776 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003777
Gilles Peskine69c12672018-06-28 00:07:19 +02003778 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3779
Gilles Peskine8817f612018-12-18 00:18:46 +01003780 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003781
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003782 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3783 psa_set_key_algorithm( &attributes, alg );
3784 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003785
Gilles Peskine049c7532019-05-15 20:22:09 +02003786 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3787 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03003788
Gilles Peskine8817f612018-12-18 00:18:46 +01003789 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3790 hash_data->x, hash_data->len,
3791 signature_data->x,
3792 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003793exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003794 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003795 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003796 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003797}
3798/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003799
3800/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003801void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3802 int alg_arg, data_t *hash_data,
3803 data_t *signature_data,
3804 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003805{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003806 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003807 psa_key_type_t key_type = key_type_arg;
3808 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003809 psa_status_t actual_status;
3810 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003811 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003812
Gilles Peskine8817f612018-12-18 00:18:46 +01003813 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003814
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003815 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3816 psa_set_key_algorithm( &attributes, alg );
3817 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003818
Gilles Peskine049c7532019-05-15 20:22:09 +02003819 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3820 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003821
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003822 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003823 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003824 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003825 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003826
Gilles Peskinefe11b722018-12-18 00:24:04 +01003827 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003828
3829exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003830 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003831 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003832 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003833}
3834/* END_CASE */
3835
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003836/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003837void asymmetric_encrypt( int key_type_arg,
3838 data_t *key_data,
3839 int alg_arg,
3840 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003841 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003842 int expected_output_length_arg,
3843 int expected_status_arg )
3844{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003845 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003846 psa_key_type_t key_type = key_type_arg;
3847 psa_algorithm_t alg = alg_arg;
3848 size_t expected_output_length = expected_output_length_arg;
3849 size_t key_bits;
3850 unsigned char *output = NULL;
3851 size_t output_size;
3852 size_t output_length = ~0;
3853 psa_status_t actual_status;
3854 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003855 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003856
Gilles Peskine8817f612018-12-18 00:18:46 +01003857 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003858
Gilles Peskine656896e2018-06-29 19:12:28 +02003859 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003860 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3861 psa_set_key_algorithm( &attributes, alg );
3862 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003863 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3864 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003865
3866 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003867 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3868 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02003869 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003870 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003871
3872 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003873 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003874 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003875 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003876 output, output_size,
3877 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003878 TEST_EQUAL( actual_status, expected_status );
3879 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003880
Gilles Peskine68428122018-06-30 18:42:41 +02003881 /* If the label is empty, the test framework puts a non-null pointer
3882 * in label->x. Test that a null pointer works as well. */
3883 if( label->len == 0 )
3884 {
3885 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003886 if( output_size != 0 )
3887 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003888 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003889 input_data->x, input_data->len,
3890 NULL, label->len,
3891 output, output_size,
3892 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003893 TEST_EQUAL( actual_status, expected_status );
3894 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003895 }
3896
Gilles Peskine656896e2018-06-29 19:12:28 +02003897exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003898 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003899 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003900 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003901 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003902}
3903/* END_CASE */
3904
3905/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003906void asymmetric_encrypt_decrypt( int key_type_arg,
3907 data_t *key_data,
3908 int alg_arg,
3909 data_t *input_data,
3910 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003911{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003912 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003913 psa_key_type_t key_type = key_type_arg;
3914 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003915 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003916 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003917 size_t output_size;
3918 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003919 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003920 size_t output2_size;
3921 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003922 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003923
Gilles Peskine8817f612018-12-18 00:18:46 +01003924 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003925
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003926 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3927 psa_set_key_algorithm( &attributes, alg );
3928 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003929
Gilles Peskine049c7532019-05-15 20:22:09 +02003930 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3931 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003932
3933 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003934 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3935 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003936 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003937 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003938 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003939 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003940
Gilles Peskineeebd7382018-06-08 18:11:54 +02003941 /* We test encryption by checking that encrypt-then-decrypt gives back
3942 * the original plaintext because of the non-optional random
3943 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003944 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3945 input_data->x, input_data->len,
3946 label->x, label->len,
3947 output, output_size,
3948 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003949 /* We don't know what ciphertext length to expect, but check that
3950 * it looks sensible. */
3951 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003952
Gilles Peskine8817f612018-12-18 00:18:46 +01003953 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3954 output, output_length,
3955 label->x, label->len,
3956 output2, output2_size,
3957 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003958 ASSERT_COMPARE( input_data->x, input_data->len,
3959 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003960
3961exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003962 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003963 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003964 mbedtls_free( output );
3965 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003966 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003967}
3968/* END_CASE */
3969
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003970/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003971void asymmetric_decrypt( int key_type_arg,
3972 data_t *key_data,
3973 int alg_arg,
3974 data_t *input_data,
3975 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003976 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003977{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003978 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003979 psa_key_type_t key_type = key_type_arg;
3980 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003981 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003982 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003983 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003984 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003985
Jaeden Amero412654a2019-02-06 12:57:46 +00003986 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003987 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003988
Gilles Peskine8817f612018-12-18 00:18:46 +01003989 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003990
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003991 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3992 psa_set_key_algorithm( &attributes, alg );
3993 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003994
Gilles Peskine049c7532019-05-15 20:22:09 +02003995 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3996 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003997
Gilles Peskine8817f612018-12-18 00:18:46 +01003998 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3999 input_data->x, input_data->len,
4000 label->x, label->len,
4001 output,
4002 output_size,
4003 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004004 ASSERT_COMPARE( expected_data->x, expected_data->len,
4005 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004006
Gilles Peskine68428122018-06-30 18:42:41 +02004007 /* If the label is empty, the test framework puts a non-null pointer
4008 * in label->x. Test that a null pointer works as well. */
4009 if( label->len == 0 )
4010 {
4011 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004012 if( output_size != 0 )
4013 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004014 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4015 input_data->x, input_data->len,
4016 NULL, label->len,
4017 output,
4018 output_size,
4019 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004020 ASSERT_COMPARE( expected_data->x, expected_data->len,
4021 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004022 }
4023
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004024exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004025 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004026 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004027 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004028 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004029}
4030/* END_CASE */
4031
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004032/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004033void asymmetric_decrypt_fail( int key_type_arg,
4034 data_t *key_data,
4035 int alg_arg,
4036 data_t *input_data,
4037 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004038 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004039 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004040{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004041 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004042 psa_key_type_t key_type = key_type_arg;
4043 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004044 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004045 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004046 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004047 psa_status_t actual_status;
4048 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004049 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004050
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004051 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004052
Gilles Peskine8817f612018-12-18 00:18:46 +01004053 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004054
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004055 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4056 psa_set_key_algorithm( &attributes, alg );
4057 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004058
Gilles Peskine049c7532019-05-15 20:22:09 +02004059 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4060 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004061
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004062 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004063 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004064 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004065 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004066 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004067 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004068 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004069
Gilles Peskine68428122018-06-30 18:42:41 +02004070 /* If the label is empty, the test framework puts a non-null pointer
4071 * in label->x. Test that a null pointer works as well. */
4072 if( label->len == 0 )
4073 {
4074 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004075 if( output_size != 0 )
4076 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004077 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004078 input_data->x, input_data->len,
4079 NULL, label->len,
4080 output, output_size,
4081 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004082 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004083 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004084 }
4085
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004086exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004087 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004088 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004089 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004090 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004091}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004092/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004093
4094/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004095void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004096{
4097 /* Test each valid way of initializing the object, except for `= {0}`, as
4098 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4099 * though it's OK by the C standard. We could test for this, but we'd need
4100 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004101 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004102 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4103 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4104 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004105
4106 memset( &zero, 0, sizeof( zero ) );
4107
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004108 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004109 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004110 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004111 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004112 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004113 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004114 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004115
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004116 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004117 PSA_ASSERT( psa_key_derivation_abort(&func) );
4118 PSA_ASSERT( psa_key_derivation_abort(&init) );
4119 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004120}
4121/* END_CASE */
4122
Janos Follath16de4a42019-06-13 16:32:24 +01004123/* BEGIN_CASE */
4124void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004125{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004126 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004127 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004128 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004129
Gilles Peskine8817f612018-12-18 00:18:46 +01004130 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004131
Janos Follath16de4a42019-06-13 16:32:24 +01004132 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004133 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004134
4135exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004136 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004137 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004138}
4139/* END_CASE */
4140
Janos Follathaf3c2a02019-06-12 12:34:34 +01004141/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004142void derive_set_capacity( int alg_arg, int capacity_arg,
4143 int expected_status_arg )
4144{
4145 psa_algorithm_t alg = alg_arg;
4146 size_t capacity = capacity_arg;
4147 psa_status_t expected_status = expected_status_arg;
4148 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4149
4150 PSA_ASSERT( psa_crypto_init( ) );
4151
4152 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4153
4154 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4155 expected_status );
4156
4157exit:
4158 psa_key_derivation_abort( &operation );
4159 PSA_DONE( );
4160}
4161/* END_CASE */
4162
4163/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004164void derive_input( int alg_arg,
4165 int key_type_arg,
4166 int step1_arg, data_t *input1,
4167 int step2_arg, data_t *input2,
4168 int step3_arg, data_t *input3,
4169 int expected_status_arg1,
4170 int expected_status_arg2,
4171 int expected_status_arg3 )
4172{
4173 psa_algorithm_t alg = alg_arg;
4174 size_t key_type = key_type_arg;
4175 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4176 psa_status_t expected_statuses[] = {expected_status_arg1,
4177 expected_status_arg2,
4178 expected_status_arg3};
4179 data_t *inputs[] = {input1, input2, input3};
4180 psa_key_handle_t handles[] = {0, 0, 0};
4181 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4182 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4183 size_t i;
4184
4185 PSA_ASSERT( psa_crypto_init( ) );
4186
4187 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4188 psa_set_key_algorithm( &attributes, alg );
4189 psa_set_key_type( &attributes, key_type );
4190
4191 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4192
4193 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4194 {
4195 switch( steps[i] )
4196 {
4197 case PSA_KEY_DERIVATION_INPUT_SECRET:
4198 PSA_ASSERT( psa_import_key( &attributes,
4199 inputs[i]->x, inputs[i]->len,
4200 &handles[i] ) );
4201 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4202 handles[i] ),
4203 expected_statuses[i] );
4204 break;
4205 default:
4206 TEST_EQUAL( psa_key_derivation_input_bytes(
4207 &operation, steps[i],
4208 inputs[i]->x, inputs[i]->len ),
4209 expected_statuses[i] );
4210 break;
4211 }
4212 }
4213
4214exit:
4215 psa_key_derivation_abort( &operation );
4216 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4217 psa_destroy_key( handles[i] );
4218 PSA_DONE( );
4219}
4220/* END_CASE */
4221
Janos Follathd958bb72019-07-03 15:02:16 +01004222/* BEGIN_CASE */
4223void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004224{
Janos Follathd958bb72019-07-03 15:02:16 +01004225 psa_algorithm_t alg = alg_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004226 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004227 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004228 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004229 unsigned char input1[] = "Input 1";
4230 size_t input1_length = sizeof( input1 );
4231 unsigned char input2[] = "Input 2";
4232 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004233 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004234 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004235 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4236 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4237 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004238 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004239
Gilles Peskine8817f612018-12-18 00:18:46 +01004240 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004241
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004242 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4243 psa_set_key_algorithm( &attributes, alg );
4244 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004245
Gilles Peskine73676cb2019-05-15 20:15:10 +02004246 PSA_ASSERT( psa_import_key( &attributes,
4247 key_data, sizeof( key_data ),
4248 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004249
4250 /* valid key derivation */
Janos Follathd958bb72019-07-03 15:02:16 +01004251 if( !setup_key_derivation_wrap( &operation, handle, alg,
4252 input1, input1_length,
4253 input2, input2_length,
4254 capacity ) )
4255 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004256
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004257 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004258 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004259 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004260
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004261 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004262
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004263 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004264 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004265
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004266exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004267 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004268 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004269 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004270}
4271/* END_CASE */
4272
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004273/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004274void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004275{
4276 uint8_t output_buffer[16];
4277 size_t buffer_size = 16;
4278 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004279 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004280
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004281 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4282 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004283 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004284
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004285 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004286 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004287
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004288 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004289
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004290 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4291 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004292 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004293
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004294 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004295 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004296
4297exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004298 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004299}
4300/* END_CASE */
4301
4302/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004303void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004304 int step1_arg, data_t *input1,
4305 int step2_arg, data_t *input2,
4306 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004307 int requested_capacity_arg,
4308 data_t *expected_output1,
4309 data_t *expected_output2 )
4310{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004311 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004312 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4313 data_t *inputs[] = {input1, input2, input3};
4314 psa_key_handle_t handles[] = {0, 0, 0};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004315 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004316 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004317 uint8_t *expected_outputs[2] =
4318 {expected_output1->x, expected_output2->x};
4319 size_t output_sizes[2] =
4320 {expected_output1->len, expected_output2->len};
4321 size_t output_buffer_size = 0;
4322 uint8_t *output_buffer = NULL;
4323 size_t expected_capacity;
4324 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004325 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004326 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004327 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004328
4329 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4330 {
4331 if( output_sizes[i] > output_buffer_size )
4332 output_buffer_size = output_sizes[i];
4333 if( output_sizes[i] == 0 )
4334 expected_outputs[i] = NULL;
4335 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004336 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004337 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004338
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004339 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4340 psa_set_key_algorithm( &attributes, alg );
4341 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004342
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004343 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004344 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4345 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4346 requested_capacity ) );
4347 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004348 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004349 switch( steps[i] )
4350 {
4351 case 0:
4352 break;
4353 case PSA_KEY_DERIVATION_INPUT_SECRET:
4354 PSA_ASSERT( psa_import_key( &attributes,
4355 inputs[i]->x, inputs[i]->len,
4356 &handles[i] ) );
4357 PSA_ASSERT( psa_key_derivation_input_key(
4358 &operation, steps[i],
4359 handles[i] ) );
4360 break;
4361 default:
4362 PSA_ASSERT( psa_key_derivation_input_bytes(
4363 &operation, steps[i],
4364 inputs[i]->x, inputs[i]->len ) );
4365 break;
4366 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004367 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004368
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004369 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004370 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004371 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004372 expected_capacity = requested_capacity;
4373
4374 /* Expansion phase. */
4375 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4376 {
4377 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004378 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004379 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004380 if( expected_capacity == 0 && output_sizes[i] == 0 )
4381 {
4382 /* Reading 0 bytes when 0 bytes are available can go either way. */
4383 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004384 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004385 continue;
4386 }
4387 else if( expected_capacity == 0 ||
4388 output_sizes[i] > expected_capacity )
4389 {
4390 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004391 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004392 expected_capacity = 0;
4393 continue;
4394 }
4395 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004396 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004397 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004398 ASSERT_COMPARE( output_buffer, output_sizes[i],
4399 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004400 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004401 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004402 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004403 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004404 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004405 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004406 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004407
4408exit:
4409 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004410 psa_key_derivation_abort( &operation );
Gilles Peskine1468da72019-05-29 17:35:49 +02004411 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4412 psa_destroy_key( handles[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004413 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004414}
4415/* END_CASE */
4416
4417/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004418void derive_full( int alg_arg,
4419 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004420 data_t *input1,
4421 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004422 int requested_capacity_arg )
4423{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004424 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004425 psa_algorithm_t alg = alg_arg;
4426 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004427 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004428 unsigned char output_buffer[16];
4429 size_t expected_capacity = requested_capacity;
4430 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004431 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004432
Gilles Peskine8817f612018-12-18 00:18:46 +01004433 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004434
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004435 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4436 psa_set_key_algorithm( &attributes, alg );
4437 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004438
Gilles Peskine049c7532019-05-15 20:22:09 +02004439 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4440 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004441
Janos Follathf2815ea2019-07-03 12:41:36 +01004442 if( !setup_key_derivation_wrap( &operation, handle, alg,
4443 input1->x, input1->len,
4444 input2->x, input2->len,
4445 requested_capacity ) )
4446 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004447
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004448 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004449 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004450 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004451
4452 /* Expansion phase. */
4453 while( current_capacity > 0 )
4454 {
4455 size_t read_size = sizeof( output_buffer );
4456 if( read_size > current_capacity )
4457 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004458 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004459 output_buffer,
4460 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004461 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004462 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004463 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004464 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004465 }
4466
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004467 /* Check that the operation refuses to go over capacity. */
4468 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004469 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004470
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004471 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004472
4473exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004474 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004475 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004476 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004477}
4478/* END_CASE */
4479
Janos Follathe60c9052019-07-03 13:51:30 +01004480/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004481void derive_key_exercise( int alg_arg,
4482 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004483 data_t *input1,
4484 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004485 int derived_type_arg,
4486 int derived_bits_arg,
4487 int derived_usage_arg,
4488 int derived_alg_arg )
4489{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004490 psa_key_handle_t base_handle = 0;
4491 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004492 psa_algorithm_t alg = alg_arg;
4493 psa_key_type_t derived_type = derived_type_arg;
4494 size_t derived_bits = derived_bits_arg;
4495 psa_key_usage_t derived_usage = derived_usage_arg;
4496 psa_algorithm_t derived_alg = derived_alg_arg;
4497 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004498 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004499 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004500 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004501
Gilles Peskine8817f612018-12-18 00:18:46 +01004502 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004503
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004504 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4505 psa_set_key_algorithm( &attributes, alg );
4506 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004507 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4508 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004509
4510 /* Derive a key. */
Janos Follathe60c9052019-07-03 13:51:30 +01004511 if ( setup_key_derivation_wrap( &operation, base_handle, alg,
4512 input1->x, input1->len,
4513 input2->x, input2->len, capacity ) )
4514 goto exit;
4515
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004516 psa_set_key_usage_flags( &attributes, derived_usage );
4517 psa_set_key_algorithm( &attributes, derived_alg );
4518 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004519 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004520 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004521 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004522
4523 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004524 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4525 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4526 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004527
4528 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004529 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004530 goto exit;
4531
4532exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004533 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004534 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004535 psa_destroy_key( base_handle );
4536 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004537 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004538}
4539/* END_CASE */
4540
Janos Follath42fd8882019-07-03 14:17:09 +01004541/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004542void derive_key_export( int alg_arg,
4543 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004544 data_t *input1,
4545 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004546 int bytes1_arg,
4547 int bytes2_arg )
4548{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004549 psa_key_handle_t base_handle = 0;
4550 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004551 psa_algorithm_t alg = alg_arg;
4552 size_t bytes1 = bytes1_arg;
4553 size_t bytes2 = bytes2_arg;
4554 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004555 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004556 uint8_t *output_buffer = NULL;
4557 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004558 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4559 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004560 size_t length;
4561
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004562 ASSERT_ALLOC( output_buffer, capacity );
4563 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004564 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004565
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004566 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4567 psa_set_key_algorithm( &base_attributes, alg );
4568 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004569 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4570 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004571
4572 /* Derive some material and output it. */
Janos Follath42fd8882019-07-03 14:17:09 +01004573 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4574 input1->x, input1->len,
4575 input2->x, input2->len, capacity ) )
4576 goto exit;
4577
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004578 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004579 output_buffer,
4580 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004581 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004582
4583 /* Derive the same output again, but this time store it in key objects. */
Janos Follath42fd8882019-07-03 14:17:09 +01004584 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4585 input1->x, input1->len,
4586 input2->x, input2->len, capacity ) )
4587 goto exit;
4588
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004589 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4590 psa_set_key_algorithm( &derived_attributes, 0 );
4591 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004592 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004593 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004594 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004595 PSA_ASSERT( psa_export_key( derived_handle,
4596 export_buffer, bytes1,
4597 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004598 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004599 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004600 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004601 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004602 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004603 PSA_ASSERT( psa_export_key( derived_handle,
4604 export_buffer + bytes1, bytes2,
4605 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004606 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004607
4608 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004609 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4610 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004611
4612exit:
4613 mbedtls_free( output_buffer );
4614 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004615 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004616 psa_destroy_key( base_handle );
4617 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004618 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004619}
4620/* END_CASE */
4621
4622/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02004623void derive_large_key( int alg_arg,
4624 data_t *key_data, data_t *input1, data_t *input2,
4625 int bits_arg,
4626 int expected_status_arg )
4627{
4628 psa_key_handle_t base_handle = 0;
4629 psa_key_handle_t derived_handle = 0;
4630 psa_algorithm_t alg = alg_arg;
4631 size_t bits = bits_arg;
4632 psa_status_t expected_status = expected_status_arg;
4633 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4634 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4635 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4636
4637 PSA_ASSERT( psa_crypto_init( ) );
4638
4639 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4640 psa_set_key_algorithm( &base_attributes, alg );
4641 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4642 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4643 &base_handle ) );
4644
4645 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4646 input1->x, input1->len,
4647 input2->x, input2->len, SIZE_MAX ) )
4648 goto exit;
4649
4650 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4651 psa_set_key_algorithm( &derived_attributes, 0 );
4652 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
4653 psa_set_key_bits( &derived_attributes, bits );
4654 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
4655 &derived_handle ),
4656 expected_status );
4657
4658exit:
4659 psa_key_derivation_abort( &operation );
4660 psa_destroy_key( base_handle );
4661 psa_destroy_key( derived_handle );
4662 PSA_DONE( );
4663}
4664/* END_CASE */
4665
4666/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004667void key_agreement_setup( int alg_arg,
4668 int our_key_type_arg, data_t *our_key_data,
4669 data_t *peer_key_data,
4670 int expected_status_arg )
4671{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004672 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004673 psa_algorithm_t alg = alg_arg;
4674 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004675 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004676 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004677 psa_status_t expected_status = expected_status_arg;
4678 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004679
Gilles Peskine8817f612018-12-18 00:18:46 +01004680 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004681
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004682 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4683 psa_set_key_algorithm( &attributes, alg );
4684 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004685 PSA_ASSERT( psa_import_key( &attributes,
4686 our_key_data->x, our_key_data->len,
4687 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004688
Gilles Peskine77f40d82019-04-11 21:27:06 +02004689 /* The tests currently include inputs that should fail at either step.
4690 * Test cases that fail at the setup step should be changed to call
4691 * key_derivation_setup instead, and this function should be renamed
4692 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004693 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004694 if( status == PSA_SUCCESS )
4695 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004696 TEST_EQUAL( psa_key_derivation_key_agreement(
4697 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4698 our_key,
4699 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004700 expected_status );
4701 }
4702 else
4703 {
4704 TEST_ASSERT( status == expected_status );
4705 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004706
4707exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004708 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004709 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004710 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004711}
4712/* END_CASE */
4713
4714/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004715void raw_key_agreement( int alg_arg,
4716 int our_key_type_arg, data_t *our_key_data,
4717 data_t *peer_key_data,
4718 data_t *expected_output )
4719{
4720 psa_key_handle_t our_key = 0;
4721 psa_algorithm_t alg = alg_arg;
4722 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004723 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004724 unsigned char *output = NULL;
4725 size_t output_length = ~0;
4726
4727 ASSERT_ALLOC( output, expected_output->len );
4728 PSA_ASSERT( psa_crypto_init( ) );
4729
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004730 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4731 psa_set_key_algorithm( &attributes, alg );
4732 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004733 PSA_ASSERT( psa_import_key( &attributes,
4734 our_key_data->x, our_key_data->len,
4735 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004736
Gilles Peskinebe697d82019-05-16 18:00:41 +02004737 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4738 peer_key_data->x, peer_key_data->len,
4739 output, expected_output->len,
4740 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004741 ASSERT_COMPARE( output, output_length,
4742 expected_output->x, expected_output->len );
4743
4744exit:
4745 mbedtls_free( output );
4746 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004747 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004748}
4749/* END_CASE */
4750
4751/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004752void key_agreement_capacity( int alg_arg,
4753 int our_key_type_arg, data_t *our_key_data,
4754 data_t *peer_key_data,
4755 int expected_capacity_arg )
4756{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004757 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004758 psa_algorithm_t alg = alg_arg;
4759 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004760 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004761 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004762 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004763 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004764
Gilles Peskine8817f612018-12-18 00:18:46 +01004765 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004766
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004767 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4768 psa_set_key_algorithm( &attributes, alg );
4769 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004770 PSA_ASSERT( psa_import_key( &attributes,
4771 our_key_data->x, our_key_data->len,
4772 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004773
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004774 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004775 PSA_ASSERT( psa_key_derivation_key_agreement(
4776 &operation,
4777 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4778 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004779 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4780 {
4781 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004782 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004783 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004784 NULL, 0 ) );
4785 }
Gilles Peskine59685592018-09-18 12:11:34 +02004786
Gilles Peskinebf491972018-10-25 22:36:12 +02004787 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004788 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004789 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004790 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004791
Gilles Peskinebf491972018-10-25 22:36:12 +02004792 /* Test the actual capacity by reading the output. */
4793 while( actual_capacity > sizeof( output ) )
4794 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004795 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004796 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004797 actual_capacity -= sizeof( output );
4798 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004799 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004800 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004801 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004802 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004803
Gilles Peskine59685592018-09-18 12:11:34 +02004804exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004805 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004806 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004807 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004808}
4809/* END_CASE */
4810
4811/* BEGIN_CASE */
4812void key_agreement_output( int alg_arg,
4813 int our_key_type_arg, data_t *our_key_data,
4814 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004815 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004816{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004817 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004818 psa_algorithm_t alg = alg_arg;
4819 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004820 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004821 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004822 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004823
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004824 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4825 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004826
Gilles Peskine8817f612018-12-18 00:18:46 +01004827 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004828
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004829 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4830 psa_set_key_algorithm( &attributes, alg );
4831 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004832 PSA_ASSERT( psa_import_key( &attributes,
4833 our_key_data->x, our_key_data->len,
4834 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004835
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004836 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004837 PSA_ASSERT( psa_key_derivation_key_agreement(
4838 &operation,
4839 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4840 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004841 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4842 {
4843 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004844 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004845 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004846 NULL, 0 ) );
4847 }
Gilles Peskine59685592018-09-18 12:11:34 +02004848
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004849 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004850 actual_output,
4851 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004852 ASSERT_COMPARE( actual_output, expected_output1->len,
4853 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004854 if( expected_output2->len != 0 )
4855 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004856 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004857 actual_output,
4858 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004859 ASSERT_COMPARE( actual_output, expected_output2->len,
4860 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004861 }
Gilles Peskine59685592018-09-18 12:11:34 +02004862
4863exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004864 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004865 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004866 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004867 mbedtls_free( actual_output );
4868}
4869/* END_CASE */
4870
4871/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004872void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004873{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004874 size_t bytes = bytes_arg;
4875 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004876 unsigned char *output = NULL;
4877 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004878 size_t i;
4879 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004880
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004881 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4882 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004883 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004884
Gilles Peskine8817f612018-12-18 00:18:46 +01004885 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004886
Gilles Peskinea50d7392018-06-21 10:22:13 +02004887 /* Run several times, to ensure that every output byte will be
4888 * nonzero at least once with overwhelming probability
4889 * (2^(-8*number_of_runs)). */
4890 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004891 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004892 if( bytes != 0 )
4893 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004894 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004895
4896 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004897 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4898 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004899
4900 for( i = 0; i < bytes; i++ )
4901 {
4902 if( output[i] != 0 )
4903 ++changed[i];
4904 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004905 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004906
4907 /* Check that every byte was changed to nonzero at least once. This
4908 * validates that psa_generate_random is overwriting every byte of
4909 * the output buffer. */
4910 for( i = 0; i < bytes; i++ )
4911 {
4912 TEST_ASSERT( changed[i] != 0 );
4913 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004914
4915exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004916 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004917 mbedtls_free( output );
4918 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004919}
4920/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004921
4922/* BEGIN_CASE */
4923void generate_key( int type_arg,
4924 int bits_arg,
4925 int usage_arg,
4926 int alg_arg,
4927 int expected_status_arg )
4928{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004929 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004930 psa_key_type_t type = type_arg;
4931 psa_key_usage_t usage = usage_arg;
4932 size_t bits = bits_arg;
4933 psa_algorithm_t alg = alg_arg;
4934 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004935 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004936 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004937
Gilles Peskine8817f612018-12-18 00:18:46 +01004938 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004939
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004940 psa_set_key_usage_flags( &attributes, usage );
4941 psa_set_key_algorithm( &attributes, alg );
4942 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004943 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004944
4945 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004946 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004947 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004948 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004949
4950 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004951 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
4952 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4953 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004954
Gilles Peskine818ca122018-06-20 18:16:48 +02004955 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004956 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004957 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004958
4959exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004960 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004961 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004962 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004963}
4964/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004965
Gilles Peskinee56e8782019-04-26 17:34:02 +02004966/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
4967void generate_key_rsa( int bits_arg,
4968 data_t *e_arg,
4969 int expected_status_arg )
4970{
4971 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004972 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02004973 size_t bits = bits_arg;
4974 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4975 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4976 psa_status_t expected_status = expected_status_arg;
4977 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4978 uint8_t *exported = NULL;
4979 size_t exported_size =
4980 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
4981 size_t exported_length = SIZE_MAX;
4982 uint8_t *e_read_buffer = NULL;
4983 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004984 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004985 size_t e_read_length = SIZE_MAX;
4986
4987 if( e_arg->len == 0 ||
4988 ( e_arg->len == 3 &&
4989 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4990 {
4991 is_default_public_exponent = 1;
4992 e_read_size = 0;
4993 }
4994 ASSERT_ALLOC( e_read_buffer, e_read_size );
4995 ASSERT_ALLOC( exported, exported_size );
4996
4997 PSA_ASSERT( psa_crypto_init( ) );
4998
4999 psa_set_key_usage_flags( &attributes, usage );
5000 psa_set_key_algorithm( &attributes, alg );
5001 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5002 e_arg->x, e_arg->len ) );
5003 psa_set_key_bits( &attributes, bits );
5004
5005 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005006 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005007 if( expected_status != PSA_SUCCESS )
5008 goto exit;
5009
5010 /* Test the key information */
5011 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5012 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5013 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5014 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5015 e_read_buffer, e_read_size,
5016 &e_read_length ) );
5017 if( is_default_public_exponent )
5018 TEST_EQUAL( e_read_length, 0 );
5019 else
5020 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5021
5022 /* Do something with the key according to its type and permitted usage. */
5023 if( ! exercise_key( handle, usage, alg ) )
5024 goto exit;
5025
5026 /* Export the key and check the public exponent. */
5027 PSA_ASSERT( psa_export_public_key( handle,
5028 exported, exported_size,
5029 &exported_length ) );
5030 {
5031 uint8_t *p = exported;
5032 uint8_t *end = exported + exported_length;
5033 size_t len;
5034 /* RSAPublicKey ::= SEQUENCE {
5035 * modulus INTEGER, -- n
5036 * publicExponent INTEGER } -- e
5037 */
5038 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005039 MBEDTLS_ASN1_SEQUENCE |
5040 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005041 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5042 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5043 MBEDTLS_ASN1_INTEGER ) );
5044 if( len >= 1 && p[0] == 0 )
5045 {
5046 ++p;
5047 --len;
5048 }
5049 if( e_arg->len == 0 )
5050 {
5051 TEST_EQUAL( len, 3 );
5052 TEST_EQUAL( p[0], 1 );
5053 TEST_EQUAL( p[1], 0 );
5054 TEST_EQUAL( p[2], 1 );
5055 }
5056 else
5057 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5058 }
5059
5060exit:
5061 psa_reset_key_attributes( &attributes );
5062 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005063 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005064 mbedtls_free( e_read_buffer );
5065 mbedtls_free( exported );
5066}
5067/* END_CASE */
5068
Darryl Greend49a4992018-06-18 17:27:26 +01005069/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005070void persistent_key_load_key_from_storage( data_t *data,
5071 int type_arg, int bits_arg,
5072 int usage_flags_arg, int alg_arg,
5073 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005074{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005075 psa_key_id_t key_id = 1;
5076 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005077 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005078 psa_key_handle_t base_key = 0;
5079 psa_key_type_t type = type_arg;
5080 size_t bits = bits_arg;
5081 psa_key_usage_t usage_flags = usage_flags_arg;
5082 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005083 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005084 unsigned char *first_export = NULL;
5085 unsigned char *second_export = NULL;
5086 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5087 size_t first_exported_length;
5088 size_t second_exported_length;
5089
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005090 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5091 {
5092 ASSERT_ALLOC( first_export, export_size );
5093 ASSERT_ALLOC( second_export, export_size );
5094 }
Darryl Greend49a4992018-06-18 17:27:26 +01005095
Gilles Peskine8817f612018-12-18 00:18:46 +01005096 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005097
Gilles Peskinec87af662019-05-15 16:12:22 +02005098 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005099 psa_set_key_usage_flags( &attributes, usage_flags );
5100 psa_set_key_algorithm( &attributes, alg );
5101 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005102 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005103
Darryl Green0c6575a2018-11-07 16:05:30 +00005104 switch( generation_method )
5105 {
5106 case IMPORT_KEY:
5107 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005108 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
5109 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005110 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005111
Darryl Green0c6575a2018-11-07 16:05:30 +00005112 case GENERATE_KEY:
5113 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005114 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005115 break;
5116
5117 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005118 {
5119 /* Create base key */
5120 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5121 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5122 psa_set_key_usage_flags( &base_attributes,
5123 PSA_KEY_USAGE_DERIVE );
5124 psa_set_key_algorithm( &base_attributes, derive_alg );
5125 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005126 PSA_ASSERT( psa_import_key( &base_attributes,
5127 data->x, data->len,
5128 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005129 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005130 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005131 PSA_ASSERT( psa_key_derivation_input_key(
5132 &operation,
5133 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005134 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005135 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005136 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005137 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5138 &operation,
5139 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005140 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005141 PSA_ASSERT( psa_destroy_key( base_key ) );
5142 base_key = 0;
5143 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005144 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005145 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005146 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005147
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005148 /* Export the key if permitted by the key policy. */
5149 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5150 {
5151 PSA_ASSERT( psa_export_key( handle,
5152 first_export, export_size,
5153 &first_exported_length ) );
5154 if( generation_method == IMPORT_KEY )
5155 ASSERT_COMPARE( data->x, data->len,
5156 first_export, first_exported_length );
5157 }
Darryl Greend49a4992018-06-18 17:27:26 +01005158
5159 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005160 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005161 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005162 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005163
Darryl Greend49a4992018-06-18 17:27:26 +01005164 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005165 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005166 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5167 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
5168 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5169 PSA_KEY_LIFETIME_PERSISTENT );
5170 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5171 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5172 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5173 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005174
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005175 /* Export the key again if permitted by the key policy. */
5176 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005177 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005178 PSA_ASSERT( psa_export_key( handle,
5179 second_export, export_size,
5180 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005181 ASSERT_COMPARE( first_export, first_exported_length,
5182 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005183 }
5184
5185 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005186 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005187 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005188
5189exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005190 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005191 mbedtls_free( first_export );
5192 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005193 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005194 psa_destroy_key( base_key );
5195 if( handle == 0 )
5196 {
5197 /* In case there was a test failure after creating the persistent key
5198 * but while it was not open, try to re-open the persistent key
5199 * to delete it. */
Gilles Peskine225010f2019-05-06 18:44:55 +02005200 psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005201 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005202 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005203 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005204}
5205/* END_CASE */