blob: 3e698f56868f455c69c56a4a98301300d8b97bd7 [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 Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Gilles Peskine1838e822019-06-20 12:40:56 +020012#include "psa_crypto_helpers.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Gilles Peskinec744d992019-07-30 17:26:54 +020014/* Tests that require more than 128kB of RAM plus change have this symbol
15 * as a dependency. Currently we always define this symbol, so the tests
16 * are always executed. In the future we should make this conditional
17 * so that tests that require a lot of memory are skipped on constrained
18 * platforms. */
Gilles Peskine49232e82019-08-07 11:01:30 +020019#define HAVE_RAM_AVAILABLE_128K
Gilles Peskinec744d992019-07-30 17:26:54 +020020
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020021#include "psa/crypto.h"
22
Jaeden Amerof24c7f82018-06-27 17:20:43 +010023/** An invalid export length that will never be set by psa_export_key(). */
24static const size_t INVALID_EXPORT_LENGTH = ~0U;
25
Gilles Peskinef426e0f2019-02-25 17:42:03 +010026/* A hash algorithm that is known to be supported.
27 *
28 * This is used in some smoke tests.
29 */
30#if defined(MBEDTLS_MD2_C)
31#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
32#elif defined(MBEDTLS_MD4_C)
33#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
34#elif defined(MBEDTLS_MD5_C)
35#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
36/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
37 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
38 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
39 * implausible anyway. */
40#elif defined(MBEDTLS_SHA1_C)
41#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
42#elif defined(MBEDTLS_SHA256_C)
43#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
44#elif defined(MBEDTLS_SHA512_C)
45#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
46#elif defined(MBEDTLS_SHA3_C)
47#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
48#else
49#undef KNOWN_SUPPORTED_HASH_ALG
50#endif
51
52/* A block cipher that is known to be supported.
53 *
54 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
55 */
56#if defined(MBEDTLS_AES_C)
57#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
58#elif defined(MBEDTLS_ARIA_C)
59#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
60#elif defined(MBEDTLS_CAMELLIA_C)
61#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
62#undef KNOWN_SUPPORTED_BLOCK_CIPHER
63#endif
64
65/* A MAC mode that is known to be supported.
66 *
67 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
68 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
69 *
70 * This is used in some smoke tests.
71 */
72#if defined(KNOWN_SUPPORTED_HASH_ALG)
73#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
74#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
75#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
76#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
77#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
78#else
79#undef KNOWN_SUPPORTED_MAC_ALG
80#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
81#endif
82
83/* A cipher algorithm and key type that are known to be supported.
84 *
85 * This is used in some smoke tests.
86 */
87#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
88#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
89#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
90#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
91#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
92#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
93#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
94#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
95#else
96#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
97#endif
98#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
99#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
100#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
101#elif defined(MBEDTLS_RC4_C)
102#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
103#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
104#else
105#undef KNOWN_SUPPORTED_CIPHER_ALG
106#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
107#endif
108
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200109/** Test if a buffer contains a constant byte value.
110 *
111 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200112 *
113 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200114 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200115 * \param size Size of the buffer in bytes.
116 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200117 * \return 1 if the buffer is all-bits-zero.
118 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200119 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200120static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200121{
122 size_t i;
123 for( i = 0; i < size; i++ )
124 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200125 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200126 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200127 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200128 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200129}
Gilles Peskine818ca122018-06-20 18:16:48 +0200130
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200131/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
132static int asn1_write_10x( unsigned char **p,
133 unsigned char *start,
134 size_t bits,
135 unsigned char x )
136{
137 int ret;
138 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200139 if( bits == 0 )
140 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
141 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200142 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300143 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200144 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
145 *p -= len;
146 ( *p )[len-1] = x;
147 if( bits % 8 == 0 )
148 ( *p )[1] |= 1;
149 else
150 ( *p )[0] |= 1 << ( bits % 8 );
151 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
152 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
153 MBEDTLS_ASN1_INTEGER ) );
154 return( len );
155}
156
157static int construct_fake_rsa_key( unsigned char *buffer,
158 size_t buffer_size,
159 unsigned char **p,
160 size_t bits,
161 int keypair )
162{
163 size_t half_bits = ( bits + 1 ) / 2;
164 int ret;
165 int len = 0;
166 /* Construct something that looks like a DER encoding of
167 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
168 * RSAPrivateKey ::= SEQUENCE {
169 * version Version,
170 * modulus INTEGER, -- n
171 * publicExponent INTEGER, -- e
172 * privateExponent INTEGER, -- d
173 * prime1 INTEGER, -- p
174 * prime2 INTEGER, -- q
175 * exponent1 INTEGER, -- d mod (p-1)
176 * exponent2 INTEGER, -- d mod (q-1)
177 * coefficient INTEGER, -- (inverse of q) mod p
178 * otherPrimeInfos OtherPrimeInfos OPTIONAL
179 * }
180 * Or, for a public key, the same structure with only
181 * version, modulus and publicExponent.
182 */
183 *p = buffer + buffer_size;
184 if( keypair )
185 {
186 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
187 asn1_write_10x( p, buffer, half_bits, 1 ) );
188 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
189 asn1_write_10x( p, buffer, half_bits, 1 ) );
190 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
191 asn1_write_10x( p, buffer, half_bits, 1 ) );
192 MBEDTLS_ASN1_CHK_ADD( len, /* q */
193 asn1_write_10x( p, buffer, half_bits, 1 ) );
194 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
195 asn1_write_10x( p, buffer, half_bits, 3 ) );
196 MBEDTLS_ASN1_CHK_ADD( len, /* d */
197 asn1_write_10x( p, buffer, bits, 1 ) );
198 }
199 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
200 asn1_write_10x( p, buffer, 17, 1 ) );
201 MBEDTLS_ASN1_CHK_ADD( len, /* n */
202 asn1_write_10x( p, buffer, bits, 1 ) );
203 if( keypair )
204 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
205 mbedtls_asn1_write_int( p, buffer, 0 ) );
206 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
207 {
208 const unsigned char tag =
209 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
210 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
211 }
212 return( len );
213}
214
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100215int exercise_mac_setup( psa_key_type_t key_type,
216 const unsigned char *key_bytes,
217 size_t key_length,
218 psa_algorithm_t alg,
219 psa_mac_operation_t *operation,
220 psa_status_t *status )
221{
222 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200223 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100224
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200225 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
226 psa_set_key_algorithm( &attributes, alg );
227 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +0200228 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length,
229 &handle ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100230
231 *status = psa_mac_sign_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100232 /* Whether setup succeeded or failed, abort must succeed. */
233 PSA_ASSERT( psa_mac_abort( operation ) );
234 /* If setup failed, reproduce the failure, so that the caller can
235 * test the resulting state of the operation object. */
236 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100237 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100238 TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ),
239 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100240 }
241
242 psa_destroy_key( handle );
243 return( 1 );
244
245exit:
246 psa_destroy_key( handle );
247 return( 0 );
248}
249
250int exercise_cipher_setup( psa_key_type_t key_type,
251 const unsigned char *key_bytes,
252 size_t key_length,
253 psa_algorithm_t alg,
254 psa_cipher_operation_t *operation,
255 psa_status_t *status )
256{
257 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200258 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100259
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200260 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
261 psa_set_key_algorithm( &attributes, alg );
262 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +0200263 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length,
264 &handle ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100265
266 *status = psa_cipher_encrypt_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100267 /* Whether setup succeeded or failed, abort must succeed. */
268 PSA_ASSERT( psa_cipher_abort( operation ) );
269 /* If setup failed, reproduce the failure, so that the caller can
270 * test the resulting state of the operation object. */
271 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100272 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100273 TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ),
274 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100275 }
276
277 psa_destroy_key( handle );
278 return( 1 );
279
280exit:
281 psa_destroy_key( handle );
282 return( 0 );
283}
284
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100285static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200286 psa_key_usage_t usage,
287 psa_algorithm_t alg )
288{
Jaeden Amero769ce272019-01-04 11:48:03 +0000289 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200290 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200291 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200292 size_t mac_length = sizeof( mac );
293
294 if( usage & PSA_KEY_USAGE_SIGN )
295 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100296 PSA_ASSERT( psa_mac_sign_setup( &operation,
297 handle, alg ) );
298 PSA_ASSERT( psa_mac_update( &operation,
299 input, sizeof( input ) ) );
300 PSA_ASSERT( psa_mac_sign_finish( &operation,
301 mac, sizeof( mac ),
302 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200303 }
304
305 if( usage & PSA_KEY_USAGE_VERIFY )
306 {
307 psa_status_t verify_status =
308 ( usage & PSA_KEY_USAGE_SIGN ?
309 PSA_SUCCESS :
310 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100311 PSA_ASSERT( psa_mac_verify_setup( &operation,
312 handle, alg ) );
313 PSA_ASSERT( psa_mac_update( &operation,
314 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100315 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
316 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200317 }
318
319 return( 1 );
320
321exit:
322 psa_mac_abort( &operation );
323 return( 0 );
324}
325
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100326static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200327 psa_key_usage_t usage,
328 psa_algorithm_t alg )
329{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000330 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200331 unsigned char iv[16] = {0};
332 size_t iv_length = sizeof( iv );
333 const unsigned char plaintext[16] = "Hello, world...";
334 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
335 size_t ciphertext_length = sizeof( ciphertext );
336 unsigned char decrypted[sizeof( ciphertext )];
337 size_t part_length;
338
339 if( usage & PSA_KEY_USAGE_ENCRYPT )
340 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100341 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
342 handle, alg ) );
343 PSA_ASSERT( psa_cipher_generate_iv( &operation,
344 iv, sizeof( iv ),
345 &iv_length ) );
346 PSA_ASSERT( psa_cipher_update( &operation,
347 plaintext, sizeof( plaintext ),
348 ciphertext, sizeof( ciphertext ),
349 &ciphertext_length ) );
350 PSA_ASSERT( psa_cipher_finish( &operation,
351 ciphertext + ciphertext_length,
352 sizeof( ciphertext ) - ciphertext_length,
353 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200354 ciphertext_length += part_length;
355 }
356
357 if( usage & PSA_KEY_USAGE_DECRYPT )
358 {
359 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200360 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200361 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
362 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200363 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
364 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
365 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
366 * have this macro yet. */
367 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE(
368 psa_get_key_type( &attributes ) );
369 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200370 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100371 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
372 handle, alg ) );
373 PSA_ASSERT( psa_cipher_set_iv( &operation,
374 iv, iv_length ) );
375 PSA_ASSERT( psa_cipher_update( &operation,
376 ciphertext, ciphertext_length,
377 decrypted, sizeof( decrypted ),
378 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200379 status = psa_cipher_finish( &operation,
380 decrypted + part_length,
381 sizeof( decrypted ) - part_length,
382 &part_length );
383 /* For a stream cipher, all inputs are valid. For a block cipher,
384 * if the input is some aribtrary data rather than an actual
385 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200386 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200387 TEST_ASSERT( status == PSA_SUCCESS ||
388 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200389 else
390 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200391 }
392
393 return( 1 );
394
395exit:
396 psa_cipher_abort( &operation );
397 return( 0 );
398}
399
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100400static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200401 psa_key_usage_t usage,
402 psa_algorithm_t alg )
403{
404 unsigned char nonce[16] = {0};
405 size_t nonce_length = sizeof( nonce );
406 unsigned char plaintext[16] = "Hello, world...";
407 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
408 size_t ciphertext_length = sizeof( ciphertext );
409 size_t plaintext_length = sizeof( ciphertext );
410
411 if( usage & PSA_KEY_USAGE_ENCRYPT )
412 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100413 PSA_ASSERT( psa_aead_encrypt( handle, alg,
414 nonce, nonce_length,
415 NULL, 0,
416 plaintext, sizeof( plaintext ),
417 ciphertext, sizeof( ciphertext ),
418 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200419 }
420
421 if( usage & PSA_KEY_USAGE_DECRYPT )
422 {
423 psa_status_t verify_status =
424 ( usage & PSA_KEY_USAGE_ENCRYPT ?
425 PSA_SUCCESS :
426 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100427 TEST_EQUAL( psa_aead_decrypt( handle, alg,
428 nonce, nonce_length,
429 NULL, 0,
430 ciphertext, ciphertext_length,
431 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100432 &plaintext_length ),
433 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200434 }
435
436 return( 1 );
437
438exit:
439 return( 0 );
440}
441
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100442static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200443 psa_key_usage_t usage,
444 psa_algorithm_t alg )
445{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200446 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
447 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200448 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200449 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100450 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
451
452 /* If the policy allows signing with any hash, just pick one. */
453 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
454 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100455#if defined(KNOWN_SUPPORTED_HASH_ALG)
456 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
457 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100458#else
459 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100460 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100461#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100462 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200463
464 if( usage & PSA_KEY_USAGE_SIGN )
465 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200466 /* Some algorithms require the payload to have the size of
467 * the hash encoded in the algorithm. Use this input size
468 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200469 if( hash_alg != 0 )
470 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100471 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
472 payload, payload_length,
473 signature, sizeof( signature ),
474 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200475 }
476
477 if( usage & PSA_KEY_USAGE_VERIFY )
478 {
479 psa_status_t verify_status =
480 ( usage & PSA_KEY_USAGE_SIGN ?
481 PSA_SUCCESS :
482 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100483 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
484 payload, payload_length,
485 signature, signature_length ),
486 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200487 }
488
489 return( 1 );
490
491exit:
492 return( 0 );
493}
494
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100495static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200496 psa_key_usage_t usage,
497 psa_algorithm_t alg )
498{
499 unsigned char plaintext[256] = "Hello, world...";
500 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
501 size_t ciphertext_length = sizeof( ciphertext );
502 size_t plaintext_length = 16;
503
504 if( usage & PSA_KEY_USAGE_ENCRYPT )
505 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100506 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
507 plaintext, plaintext_length,
508 NULL, 0,
509 ciphertext, sizeof( ciphertext ),
510 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200511 }
512
513 if( usage & PSA_KEY_USAGE_DECRYPT )
514 {
515 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100516 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200517 ciphertext, ciphertext_length,
518 NULL, 0,
519 plaintext, sizeof( plaintext ),
520 &plaintext_length );
521 TEST_ASSERT( status == PSA_SUCCESS ||
522 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
523 ( status == PSA_ERROR_INVALID_ARGUMENT ||
524 status == PSA_ERROR_INVALID_PADDING ) ) );
525 }
526
527 return( 1 );
528
529exit:
530 return( 0 );
531}
Gilles Peskine02b75072018-07-01 22:31:34 +0200532
Janos Follathf2815ea2019-07-03 12:41:36 +0100533static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation,
534 psa_key_handle_t handle,
535 psa_algorithm_t alg,
536 unsigned char* input1, size_t input1_length,
537 unsigned char* input2, size_t input2_length,
538 size_t capacity )
539{
540 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
541 if( PSA_ALG_IS_HKDF( alg ) )
542 {
543 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
544 PSA_KEY_DERIVATION_INPUT_SALT,
545 input1, input1_length ) );
546 PSA_ASSERT( psa_key_derivation_input_key( operation,
547 PSA_KEY_DERIVATION_INPUT_SECRET,
548 handle ) );
549 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
550 PSA_KEY_DERIVATION_INPUT_INFO,
551 input2,
552 input2_length ) );
553 }
554 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
555 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
556 {
557 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
558 PSA_KEY_DERIVATION_INPUT_SEED,
559 input1, input1_length ) );
560 PSA_ASSERT( psa_key_derivation_input_key( operation,
561 PSA_KEY_DERIVATION_INPUT_SECRET,
562 handle ) );
563 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
564 PSA_KEY_DERIVATION_INPUT_LABEL,
565 input2, input2_length ) );
566 }
567 else
568 {
569 TEST_ASSERT( ! "Key derivation algorithm not supported" );
570 }
571
Gilles Peskinec744d992019-07-30 17:26:54 +0200572 if( capacity != SIZE_MAX )
573 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100574
575 return( 1 );
576
577exit:
578 return( 0 );
579}
580
581
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100582static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200583 psa_key_usage_t usage,
584 psa_algorithm_t alg )
585{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200586 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100587 unsigned char input1[] = "Input 1";
588 size_t input1_length = sizeof( input1 );
589 unsigned char input2[] = "Input 2";
590 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200591 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100592 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200593
594 if( usage & PSA_KEY_USAGE_DERIVE )
595 {
Janos Follathf2815ea2019-07-03 12:41:36 +0100596 if( !setup_key_derivation_wrap( &operation, handle, alg,
597 input1, input1_length,
598 input2, input2_length, capacity ) )
599 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200600
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200601 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200602 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100603 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200604 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200605 }
606
607 return( 1 );
608
609exit:
610 return( 0 );
611}
612
Gilles Peskinec7998b72018-11-07 18:45:02 +0100613/* We need two keys to exercise key agreement. Exercise the
614 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200615static psa_status_t key_agreement_with_self(
616 psa_key_derivation_operation_t *operation,
617 psa_key_handle_t handle )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100618{
619 psa_key_type_t private_key_type;
620 psa_key_type_t public_key_type;
621 size_t key_bits;
622 uint8_t *public_key = NULL;
623 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200624 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200625 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
626 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200627 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200628 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100629
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200630 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
631 private_key_type = psa_get_key_type( &attributes );
632 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200633 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100634 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
635 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100636 PSA_ASSERT( psa_export_public_key( handle,
637 public_key, public_key_length,
638 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100639
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200640 status = psa_key_derivation_key_agreement(
641 operation, PSA_KEY_DERIVATION_INPUT_SECRET, handle,
642 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100643exit:
644 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200645 psa_reset_key_attributes( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100646 return( status );
647}
648
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200649/* We need two keys to exercise key agreement. Exercise the
650 * private key against its own public key. */
651static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
652 psa_key_handle_t handle )
653{
654 psa_key_type_t private_key_type;
655 psa_key_type_t public_key_type;
656 size_t key_bits;
657 uint8_t *public_key = NULL;
658 size_t public_key_length;
659 uint8_t output[1024];
660 size_t output_length;
661 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200662 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
663 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200664 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200665 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200666
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200667 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
668 private_key_type = psa_get_key_type( &attributes );
669 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200670 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200671 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
672 ASSERT_ALLOC( public_key, public_key_length );
673 PSA_ASSERT( psa_export_public_key( handle,
674 public_key, public_key_length,
675 &public_key_length ) );
676
Gilles Peskinebe697d82019-05-16 18:00:41 +0200677 status = psa_raw_key_agreement( alg, handle,
678 public_key, public_key_length,
679 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200680exit:
681 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200682 psa_reset_key_attributes( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200683 return( status );
684}
685
686static int exercise_raw_key_agreement_key( psa_key_handle_t handle,
687 psa_key_usage_t usage,
688 psa_algorithm_t alg )
689{
690 int ok = 0;
691
692 if( usage & PSA_KEY_USAGE_DERIVE )
693 {
694 /* We need two keys to exercise key agreement. Exercise the
695 * private key against its own public key. */
696 PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) );
697 }
698 ok = 1;
699
700exit:
701 return( ok );
702}
703
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100704static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200705 psa_key_usage_t usage,
706 psa_algorithm_t alg )
707{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200708 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200709 unsigned char output[1];
710 int ok = 0;
711
712 if( usage & PSA_KEY_USAGE_DERIVE )
713 {
714 /* We need two keys to exercise key agreement. Exercise the
715 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200716 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
717 PSA_ASSERT( key_agreement_with_self( &operation, handle ) );
718 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200719 output,
720 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200721 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200722 }
723 ok = 1;
724
725exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200726 return( ok );
727}
728
Jaeden Amerof7dca862019-06-27 17:31:33 +0100729int asn1_skip_integer( unsigned char **p, const unsigned char *end,
730 size_t min_bits, size_t max_bits,
731 int must_be_odd )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200732{
733 size_t len;
734 size_t actual_bits;
735 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100736 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100737 MBEDTLS_ASN1_INTEGER ),
738 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200739 /* Tolerate a slight departure from DER encoding:
740 * - 0 may be represented by an empty string or a 1-byte string.
741 * - The sign bit may be used as a value bit. */
742 if( ( len == 1 && ( *p )[0] == 0 ) ||
743 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
744 {
745 ++( *p );
746 --len;
747 }
748 if( min_bits == 0 && len == 0 )
749 return( 1 );
750 msb = ( *p )[0];
751 TEST_ASSERT( msb != 0 );
752 actual_bits = 8 * ( len - 1 );
753 while( msb != 0 )
754 {
755 msb >>= 1;
756 ++actual_bits;
757 }
758 TEST_ASSERT( actual_bits >= min_bits );
759 TEST_ASSERT( actual_bits <= max_bits );
760 if( must_be_odd )
761 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
762 *p += len;
763 return( 1 );
764exit:
765 return( 0 );
766}
767
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200768static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
769 uint8_t *exported, size_t exported_length )
770{
771 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100772 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200773 else
774 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200775
776#if defined(MBEDTLS_DES_C)
777 if( type == PSA_KEY_TYPE_DES )
778 {
779 /* Check the parity bits. */
780 unsigned i;
781 for( i = 0; i < bits / 8; i++ )
782 {
783 unsigned bit_count = 0;
784 unsigned m;
785 for( m = 1; m <= 0x100; m <<= 1 )
786 {
787 if( exported[i] & m )
788 ++bit_count;
789 }
790 TEST_ASSERT( bit_count % 2 != 0 );
791 }
792 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200793 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200794#endif
795
796#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200797 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200798 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200799 uint8_t *p = exported;
800 uint8_t *end = exported + exported_length;
801 size_t len;
802 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200803 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200804 * modulus INTEGER, -- n
805 * publicExponent INTEGER, -- e
806 * privateExponent INTEGER, -- d
807 * prime1 INTEGER, -- p
808 * prime2 INTEGER, -- q
809 * exponent1 INTEGER, -- d mod (p-1)
810 * exponent2 INTEGER, -- d mod (q-1)
811 * coefficient INTEGER, -- (inverse of q) mod p
812 * }
813 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100814 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
815 MBEDTLS_ASN1_SEQUENCE |
816 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
817 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200818 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
819 goto exit;
820 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
821 goto exit;
822 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
823 goto exit;
824 /* Require d to be at least half the size of n. */
825 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
826 goto exit;
827 /* Require p and q to be at most half the size of n, rounded up. */
828 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
829 goto exit;
830 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
831 goto exit;
832 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
833 goto exit;
834 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
835 goto exit;
836 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
837 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100838 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100839 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200840 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200841#endif /* MBEDTLS_RSA_C */
842
843#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200844 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200845 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100846 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100847 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100848 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200849 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200850#endif /* MBEDTLS_ECP_C */
851
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200852 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
853 {
854 uint8_t *p = exported;
855 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200856#if defined(MBEDTLS_RSA_C)
857 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
858 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100859 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200860 /* RSAPublicKey ::= SEQUENCE {
861 * modulus INTEGER, -- n
862 * publicExponent INTEGER } -- e
863 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100864 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
865 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100866 MBEDTLS_ASN1_CONSTRUCTED ),
867 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100868 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200869 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
870 goto exit;
871 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
872 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100873 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200874 }
875 else
876#endif /* MBEDTLS_RSA_C */
877#if defined(MBEDTLS_ECP_C)
878 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
879 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000880 /* The representation of an ECC public key is:
881 * - The byte 0x04;
882 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
883 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
884 * - where m is the bit size associated with the curve.
Jaeden Amero6b196002019-01-10 10:23:21 +0000885 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100886 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
887 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200888 }
889 else
890#endif /* MBEDTLS_ECP_C */
891 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100892 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200893 mbedtls_snprintf( message, sizeof( message ),
894 "No sanity check for public key type=0x%08lx",
895 (unsigned long) type );
896 test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +0200897 (void) p;
898 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200899 return( 0 );
900 }
901 }
902 else
903
904 {
905 /* No sanity checks for other types */
906 }
907
908 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200909
910exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200911 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200912}
913
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100914static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200915 psa_key_usage_t usage )
916{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200917 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200918 uint8_t *exported = NULL;
919 size_t exported_size = 0;
920 size_t exported_length = 0;
921 int ok = 0;
922
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200923 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200924
925 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200926 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200927 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100928 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
929 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200930 ok = 1;
931 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +0200932 }
933
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200934 exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ),
935 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200936 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200937
Gilles Peskine8817f612018-12-18 00:18:46 +0100938 PSA_ASSERT( psa_export_key( handle,
939 exported, exported_size,
940 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200941 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
942 psa_get_key_bits( &attributes ),
943 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +0200944
945exit:
946 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200947 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +0200948 return( ok );
949}
950
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100951static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200952{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200953 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200954 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +0200955 uint8_t *exported = NULL;
956 size_t exported_size = 0;
957 size_t exported_length = 0;
958 int ok = 0;
959
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200960 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
961 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200962 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100963 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100964 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200965 return( 1 );
966 }
967
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200968 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200969 psa_get_key_type( &attributes ) );
970 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type,
971 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200972 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200973
Gilles Peskine8817f612018-12-18 00:18:46 +0100974 PSA_ASSERT( psa_export_public_key( handle,
975 exported, exported_size,
976 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200977 ok = exported_key_sanity_check( public_type,
978 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +0200979 exported, exported_length );
980
981exit:
982 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200983 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +0200984 return( ok );
985}
986
Gilles Peskinec9516fb2019-02-05 20:32:06 +0100987/** Do smoke tests on a key.
988 *
989 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
990 * sign/verify, or derivation) that is permitted according to \p usage.
991 * \p usage and \p alg should correspond to the expected policy on the
992 * key.
993 *
994 * Export the key if permitted by \p usage, and check that the output
995 * looks sensible. If \p usage forbids export, check that
996 * \p psa_export_key correctly rejects the attempt. If the key is
997 * asymmetric, also check \p psa_export_public_key.
998 *
999 * If the key fails the tests, this function calls the test framework's
1000 * `test_fail` function and returns false. Otherwise this function returns
1001 * true. Therefore it should be used as follows:
1002 * ```
1003 * if( ! exercise_key( ... ) ) goto exit;
1004 * ```
1005 *
1006 * \param handle The key to exercise. It should be capable of performing
1007 * \p alg.
1008 * \param usage The usage flags to assume.
1009 * \param alg The algorithm to exercise.
1010 *
1011 * \retval 0 The key failed the smoke tests.
1012 * \retval 1 The key passed the smoke tests.
1013 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001014static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +02001015 psa_key_usage_t usage,
1016 psa_algorithm_t alg )
1017{
1018 int ok;
1019 if( alg == 0 )
1020 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1021 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001022 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001023 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001024 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001025 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001026 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001027 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001028 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001029 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001030 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001031 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001032 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001033 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
1034 ok = exercise_raw_key_agreement_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001035 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001036 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001037 else
1038 {
1039 char message[40];
1040 mbedtls_snprintf( message, sizeof( message ),
1041 "No code to exercise alg=0x%08lx",
1042 (unsigned long) alg );
1043 test_fail( message, __LINE__, __FILE__ );
1044 ok = 0;
1045 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001046
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001047 ok = ok && exercise_export_key( handle, usage );
1048 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +02001049
Gilles Peskine02b75072018-07-01 22:31:34 +02001050 return( ok );
1051}
1052
Gilles Peskine10df3412018-10-25 22:35:43 +02001053static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1054 psa_algorithm_t alg )
1055{
1056 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1057 {
1058 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1059 PSA_KEY_USAGE_VERIFY :
1060 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
1061 }
1062 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1063 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1064 {
1065 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1066 PSA_KEY_USAGE_ENCRYPT :
1067 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1068 }
1069 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1070 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1071 {
1072 return( PSA_KEY_USAGE_DERIVE );
1073 }
1074 else
1075 {
1076 return( 0 );
1077 }
1078
1079}
Darryl Green0c6575a2018-11-07 16:05:30 +00001080
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001081static int test_operations_on_invalid_handle( psa_key_handle_t handle )
1082{
1083 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1084 uint8_t buffer[1];
1085 size_t length;
1086 int ok = 0;
1087
Gilles Peskinec87af662019-05-15 16:12:22 +02001088 psa_set_key_id( &attributes, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001089 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1090 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1091 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
1092 TEST_EQUAL( psa_get_key_attributes( handle, &attributes ),
1093 PSA_ERROR_INVALID_HANDLE );
1094 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001095 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001096 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1097 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1098 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1099 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1100
1101 TEST_EQUAL( psa_export_key( handle,
1102 buffer, sizeof( buffer ), &length ),
1103 PSA_ERROR_INVALID_HANDLE );
1104 TEST_EQUAL( psa_export_public_key( handle,
1105 buffer, sizeof( buffer ), &length ),
1106 PSA_ERROR_INVALID_HANDLE );
1107
1108 TEST_EQUAL( psa_close_key( handle ), PSA_ERROR_INVALID_HANDLE );
1109 TEST_EQUAL( psa_destroy_key( handle ), PSA_ERROR_INVALID_HANDLE );
1110
1111 ok = 1;
1112
1113exit:
1114 psa_reset_key_attributes( &attributes );
1115 return( ok );
1116}
1117
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001118/* Assert that a key isn't reported as having a slot number. */
1119#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1120#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1121 do \
1122 { \
1123 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1124 TEST_EQUAL( psa_get_key_slot_number( \
1125 attributes, \
1126 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1127 PSA_ERROR_INVALID_ARGUMENT ); \
1128 } \
1129 while( 0 )
1130#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1131#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1132 ( (void) 0 )
1133#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1134
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001135/* An overapproximation of the amount of storage needed for a key of the
1136 * given type and with the given content. The API doesn't make it easy
1137 * to find a good value for the size. The current implementation doesn't
1138 * care about the value anyway. */
1139#define KEY_BITS_FROM_DATA( type, data ) \
1140 ( data )->len
1141
Darryl Green0c6575a2018-11-07 16:05:30 +00001142typedef enum {
1143 IMPORT_KEY = 0,
1144 GENERATE_KEY = 1,
1145 DERIVE_KEY = 2
1146} generate_method;
1147
Gilles Peskinee59236f2018-01-27 23:32:46 +01001148/* END_HEADER */
1149
1150/* BEGIN_DEPENDENCIES
1151 * depends_on:MBEDTLS_PSA_CRYPTO_C
1152 * END_DEPENDENCIES
1153 */
1154
1155/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001156void static_checks( )
1157{
1158 size_t max_truncated_mac_size =
1159 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1160
1161 /* Check that the length for a truncated MAC always fits in the algorithm
1162 * encoding. The shifted mask is the maximum truncated value. The
1163 * untruncated algorithm may be one byte larger. */
1164 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
1165}
1166/* END_CASE */
1167
1168/* BEGIN_CASE */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001169void attributes_set_get( int id_arg, int lifetime_arg,
1170 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001171 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001172{
Gilles Peskine4747d192019-04-17 15:05:45 +02001173 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001174 psa_key_id_t id = id_arg;
1175 psa_key_lifetime_t lifetime = lifetime_arg;
1176 psa_key_usage_t usage_flags = usage_flags_arg;
1177 psa_algorithm_t alg = alg_arg;
1178 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001179 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001180
1181 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1182 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1183 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1184 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1185 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001186 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001187
Gilles Peskinec87af662019-05-15 16:12:22 +02001188 psa_set_key_id( &attributes, id );
1189 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001190 psa_set_key_usage_flags( &attributes, usage_flags );
1191 psa_set_key_algorithm( &attributes, alg );
1192 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001193 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001194
1195 TEST_EQUAL( psa_get_key_id( &attributes ), id );
1196 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1197 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1198 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1199 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001200 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001201
1202 psa_reset_key_attributes( &attributes );
1203
1204 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1205 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1206 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1207 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1208 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001209 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001210}
1211/* END_CASE */
1212
1213/* BEGIN_CASE */
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001214void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg,
1215 int expected_id_arg, int expected_lifetime_arg )
1216{
1217 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1218 psa_key_id_t id1 = id1_arg;
1219 psa_key_lifetime_t lifetime = lifetime_arg;
1220 psa_key_id_t id2 = id2_arg;
1221 psa_key_id_t expected_id = expected_id_arg;
1222 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1223
1224 if( id1_arg != -1 )
1225 psa_set_key_id( &attributes, id1 );
1226 if( lifetime_arg != -1 )
1227 psa_set_key_lifetime( &attributes, lifetime );
1228 if( id2_arg != -1 )
1229 psa_set_key_id( &attributes, id2 );
1230
1231 TEST_EQUAL( psa_get_key_id( &attributes ), expected_id );
1232 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1233}
1234/* END_CASE */
1235
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001236/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1237void slot_number_attribute( )
1238{
1239 psa_key_slot_number_t slot_number = 0xdeadbeef;
1240 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1241
1242 /* Initially, there is no slot number. */
1243 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1244 PSA_ERROR_INVALID_ARGUMENT );
1245
1246 /* Test setting a slot number. */
1247 psa_set_key_slot_number( &attributes, 0 );
1248 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1249 TEST_EQUAL( slot_number, 0 );
1250
1251 /* Test changing the slot number. */
1252 psa_set_key_slot_number( &attributes, 42 );
1253 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1254 TEST_EQUAL( slot_number, 42 );
1255
1256 /* Test clearing the slot number. */
1257 psa_clear_key_slot_number( &attributes );
1258 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1259 PSA_ERROR_INVALID_ARGUMENT );
1260
1261 /* Clearing again should have no effect. */
1262 psa_clear_key_slot_number( &attributes );
1263 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1264 PSA_ERROR_INVALID_ARGUMENT );
1265
1266 /* Test that reset clears the slot number. */
1267 psa_set_key_slot_number( &attributes, 42 );
1268 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1269 TEST_EQUAL( slot_number, 42 );
1270 psa_reset_key_attributes( &attributes );
1271 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1272 PSA_ERROR_INVALID_ARGUMENT );
1273}
1274/* END_CASE */
1275
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001276/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001277void import_with_policy( int type_arg,
1278 int usage_arg, int alg_arg,
1279 int expected_status_arg )
1280{
1281 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1282 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1283 psa_key_handle_t handle = 0;
1284 psa_key_type_t type = type_arg;
1285 psa_key_usage_t usage = usage_arg;
1286 psa_algorithm_t alg = alg_arg;
1287 psa_status_t expected_status = expected_status_arg;
1288 const uint8_t key_material[16] = {0};
1289 psa_status_t status;
1290
1291 PSA_ASSERT( psa_crypto_init( ) );
1292
1293 psa_set_key_type( &attributes, type );
1294 psa_set_key_usage_flags( &attributes, usage );
1295 psa_set_key_algorithm( &attributes, alg );
1296
1297 status = psa_import_key( &attributes,
1298 key_material, sizeof( key_material ),
1299 &handle );
1300 TEST_EQUAL( status, expected_status );
1301 if( status != PSA_SUCCESS )
1302 goto exit;
1303
1304 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1305 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1306 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1307 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001308 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001309
1310 PSA_ASSERT( psa_destroy_key( handle ) );
1311 test_operations_on_invalid_handle( handle );
1312
1313exit:
1314 psa_destroy_key( handle );
1315 psa_reset_key_attributes( &got_attributes );
1316 PSA_DONE( );
1317}
1318/* END_CASE */
1319
1320/* BEGIN_CASE */
1321void import_with_data( data_t *data, int type_arg,
1322 int attr_bits_arg,
1323 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001324{
1325 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1326 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001327 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001328 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001329 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001330 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001331 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001332
Gilles Peskine8817f612018-12-18 00:18:46 +01001333 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001334
Gilles Peskine4747d192019-04-17 15:05:45 +02001335 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001336 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001337
Gilles Peskine73676cb2019-05-15 20:15:10 +02001338 status = psa_import_key( &attributes, data->x, data->len, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001339 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001340 if( status != PSA_SUCCESS )
1341 goto exit;
1342
1343 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1344 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001345 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001346 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001347 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001348
1349 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001350 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001351
1352exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001353 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001354 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001355 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001356}
1357/* END_CASE */
1358
1359/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001360void import_large_key( int type_arg, int byte_size_arg,
1361 int expected_status_arg )
1362{
1363 psa_key_type_t type = type_arg;
1364 size_t byte_size = byte_size_arg;
1365 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1366 psa_status_t expected_status = expected_status_arg;
1367 psa_key_handle_t handle = 0;
1368 psa_status_t status;
1369 uint8_t *buffer = NULL;
1370 size_t buffer_size = byte_size + 1;
1371 size_t n;
1372
1373 /* It would be better to skip the test than fail it if the allocation
1374 * fails, but the test framework doesn't support this yet. */
1375 ASSERT_ALLOC( buffer, buffer_size );
1376 memset( buffer, 'K', byte_size );
1377
1378 PSA_ASSERT( psa_crypto_init( ) );
1379
1380 /* Try importing the key */
1381 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1382 psa_set_key_type( &attributes, type );
1383 status = psa_import_key( &attributes, buffer, byte_size, &handle );
1384 TEST_EQUAL( status, expected_status );
1385
1386 if( status == PSA_SUCCESS )
1387 {
1388 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1389 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1390 TEST_EQUAL( psa_get_key_bits( &attributes ),
1391 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001392 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001393 memset( buffer, 0, byte_size + 1 );
1394 PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) );
1395 for( n = 0; n < byte_size; n++ )
1396 TEST_EQUAL( buffer[n], 'K' );
1397 for( n = byte_size; n < buffer_size; n++ )
1398 TEST_EQUAL( buffer[n], 0 );
1399 }
1400
1401exit:
1402 psa_destroy_key( handle );
1403 PSA_DONE( );
1404 mbedtls_free( buffer );
1405}
1406/* END_CASE */
1407
1408/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001409void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1410{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001411 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001412 size_t bits = bits_arg;
1413 psa_status_t expected_status = expected_status_arg;
1414 psa_status_t status;
1415 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001416 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001417 size_t buffer_size = /* Slight overapproximations */
1418 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001419 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001420 unsigned char *p;
1421 int ret;
1422 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001423 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001424
Gilles Peskine8817f612018-12-18 00:18:46 +01001425 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001426 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001427
1428 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1429 bits, keypair ) ) >= 0 );
1430 length = ret;
1431
1432 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001433 psa_set_key_type( &attributes, type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001434 status = psa_import_key( &attributes, p, length, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001435 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001436
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001437 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001438 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001439
1440exit:
1441 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001442 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001443}
1444/* END_CASE */
1445
1446/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001447void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001448 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001449 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001450 int expected_bits,
1451 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001452 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001453 int canonical_input )
1454{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001455 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001456 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001457 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001458 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001459 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001460 unsigned char *exported = NULL;
1461 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001462 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001463 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001464 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001465 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001466 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001467
Moran Pekercb088e72018-07-17 17:36:59 +03001468 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001469 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001470 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001471 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001472 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001473
Gilles Peskine4747d192019-04-17 15:05:45 +02001474 psa_set_key_usage_flags( &attributes, usage_arg );
1475 psa_set_key_algorithm( &attributes, alg );
1476 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001477
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001478 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001479 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001480
1481 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001482 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1483 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1484 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001485 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001486
1487 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001488 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001489 exported, export_size,
1490 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001491 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001492
1493 /* The exported length must be set by psa_export_key() to a value between 0
1494 * and export_size. On errors, the exported length must be 0. */
1495 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1496 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1497 TEST_ASSERT( exported_length <= export_size );
1498
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001499 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001500 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001501 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001502 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001503 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001504 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001505 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001506
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001507 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001508 goto exit;
1509
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001510 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001511 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001512 else
1513 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001514 psa_key_handle_t handle2;
Gilles Peskine049c7532019-05-15 20:22:09 +02001515 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
1516 &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001517 PSA_ASSERT( psa_export_key( handle2,
1518 reexported,
1519 export_size,
1520 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001521 ASSERT_COMPARE( exported, exported_length,
1522 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001523 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001524 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001525 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001526
1527destroy:
1528 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001529 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001530 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001531
1532exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001533 mbedtls_free( exported );
1534 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001535 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001536 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001537}
1538/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001539
Moran Pekerf709f4a2018-06-06 17:26:04 +03001540/* BEGIN_CASE */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001541void invalid_handle( int handle )
Moran Peker28a38e62018-11-07 16:18:24 +02001542{
Gilles Peskine8817f612018-12-18 00:18:46 +01001543 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001544 test_operations_on_invalid_handle( handle );
Moran Peker28a38e62018-11-07 16:18:24 +02001545
1546exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001547 PSA_DONE( );
Moran Peker28a38e62018-11-07 16:18:24 +02001548}
1549/* END_CASE */
1550
1551/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001552void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001553 int type_arg,
1554 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001555 int export_size_delta,
1556 int expected_export_status_arg,
1557 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001558{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001559 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001560 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001561 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001562 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001563 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001564 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001565 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001566 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001567 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001568
Gilles Peskine8817f612018-12-18 00:18:46 +01001569 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001570
Gilles Peskine4747d192019-04-17 15:05:45 +02001571 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1572 psa_set_key_algorithm( &attributes, alg );
1573 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001574
1575 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001576 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001577
Gilles Peskine49c25912018-10-29 15:15:31 +01001578 /* Export the public key */
1579 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001580 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001581 exported, export_size,
1582 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001583 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001584 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001585 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001586 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001587 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001588 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1589 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001590 TEST_ASSERT( expected_public_key->len <=
1591 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001592 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1593 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001594 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001595
1596exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001597 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001598 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001599 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001600 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001601}
1602/* END_CASE */
1603
Gilles Peskine20035e32018-02-03 22:44:14 +01001604/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001605void import_and_exercise_key( data_t *data,
1606 int type_arg,
1607 int bits_arg,
1608 int alg_arg )
1609{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001610 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001611 psa_key_type_t type = type_arg;
1612 size_t bits = bits_arg;
1613 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001614 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001615 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001616 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001617
Gilles Peskine8817f612018-12-18 00:18:46 +01001618 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001619
Gilles Peskine4747d192019-04-17 15:05:45 +02001620 psa_set_key_usage_flags( &attributes, usage );
1621 psa_set_key_algorithm( &attributes, alg );
1622 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001623
1624 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001625 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001626
1627 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001628 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1629 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1630 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001631
1632 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001633 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001634 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001635
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001636 PSA_ASSERT( psa_destroy_key( handle ) );
1637 test_operations_on_invalid_handle( handle );
1638
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001639exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001640 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001641 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001642 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001643}
1644/* END_CASE */
1645
1646/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001647void key_policy( int usage_arg, int alg_arg )
1648{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001649 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001650 psa_algorithm_t alg = alg_arg;
1651 psa_key_usage_t usage = usage_arg;
1652 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1653 unsigned char key[32] = {0};
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001654 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001655
1656 memset( key, 0x2a, sizeof( key ) );
1657
Gilles Peskine8817f612018-12-18 00:18:46 +01001658 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001659
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001660 psa_set_key_usage_flags( &attributes, usage );
1661 psa_set_key_algorithm( &attributes, alg );
1662 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001663
Gilles Peskine73676cb2019-05-15 20:15:10 +02001664 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001665
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001666 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1667 TEST_EQUAL( psa_get_key_type( &attributes ), key_type );
1668 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage );
1669 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001670
1671exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001672 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001673 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001674 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001675}
1676/* END_CASE */
1677
1678/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001679void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001680{
1681 /* Test each valid way of initializing the object, except for `= {0}`, as
1682 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1683 * though it's OK by the C standard. We could test for this, but we'd need
1684 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001685 psa_key_attributes_t func = psa_key_attributes_init( );
1686 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1687 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001688
1689 memset( &zero, 0, sizeof( zero ) );
1690
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001691 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1692 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1693 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001694
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001695 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1696 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1697 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1698
1699 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1700 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1701 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1702
1703 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1704 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1705 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1706
1707 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1708 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1709 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001710}
1711/* END_CASE */
1712
1713/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001714void mac_key_policy( int policy_usage,
1715 int policy_alg,
1716 int key_type,
1717 data_t *key_data,
1718 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001719{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001720 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001721 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001722 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001723 psa_status_t status;
1724 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001725
Gilles Peskine8817f612018-12-18 00:18:46 +01001726 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001727
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001728 psa_set_key_usage_flags( &attributes, policy_usage );
1729 psa_set_key_algorithm( &attributes, policy_alg );
1730 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001731
Gilles Peskine049c7532019-05-15 20:22:09 +02001732 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1733 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001734
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001735 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001736 if( policy_alg == exercise_alg &&
1737 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001738 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001739 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001740 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001741 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001742
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001743 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001744 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001745 if( policy_alg == exercise_alg &&
1746 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001747 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001748 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001749 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001750
1751exit:
1752 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001753 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001754 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001755}
1756/* END_CASE */
1757
1758/* BEGIN_CASE */
1759void cipher_key_policy( int policy_usage,
1760 int policy_alg,
1761 int key_type,
1762 data_t *key_data,
1763 int exercise_alg )
1764{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001765 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001766 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001767 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001768 psa_status_t status;
1769
Gilles Peskine8817f612018-12-18 00:18:46 +01001770 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001771
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001772 psa_set_key_usage_flags( &attributes, policy_usage );
1773 psa_set_key_algorithm( &attributes, policy_alg );
1774 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001775
Gilles Peskine049c7532019-05-15 20:22:09 +02001776 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1777 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001778
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001779 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001780 if( policy_alg == exercise_alg &&
1781 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001782 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001783 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001784 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001785 psa_cipher_abort( &operation );
1786
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001787 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001788 if( policy_alg == exercise_alg &&
1789 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001790 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001791 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001792 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001793
1794exit:
1795 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001796 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001797 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001798}
1799/* END_CASE */
1800
1801/* BEGIN_CASE */
1802void aead_key_policy( int policy_usage,
1803 int policy_alg,
1804 int key_type,
1805 data_t *key_data,
1806 int nonce_length_arg,
1807 int tag_length_arg,
1808 int exercise_alg )
1809{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001810 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001811 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001812 psa_status_t status;
1813 unsigned char nonce[16] = {0};
1814 size_t nonce_length = nonce_length_arg;
1815 unsigned char tag[16];
1816 size_t tag_length = tag_length_arg;
1817 size_t output_length;
1818
1819 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1820 TEST_ASSERT( tag_length <= sizeof( tag ) );
1821
Gilles Peskine8817f612018-12-18 00:18:46 +01001822 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001823
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001824 psa_set_key_usage_flags( &attributes, policy_usage );
1825 psa_set_key_algorithm( &attributes, policy_alg );
1826 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001827
Gilles Peskine049c7532019-05-15 20:22:09 +02001828 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1829 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001830
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001831 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001832 nonce, nonce_length,
1833 NULL, 0,
1834 NULL, 0,
1835 tag, tag_length,
1836 &output_length );
1837 if( policy_alg == exercise_alg &&
1838 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001839 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001840 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001841 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001842
1843 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001844 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001845 nonce, nonce_length,
1846 NULL, 0,
1847 tag, tag_length,
1848 NULL, 0,
1849 &output_length );
1850 if( policy_alg == exercise_alg &&
1851 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001852 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001853 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001854 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001855
1856exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001857 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001858 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001859}
1860/* END_CASE */
1861
1862/* BEGIN_CASE */
1863void asymmetric_encryption_key_policy( int policy_usage,
1864 int policy_alg,
1865 int key_type,
1866 data_t *key_data,
1867 int exercise_alg )
1868{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001869 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001870 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001871 psa_status_t status;
1872 size_t key_bits;
1873 size_t buffer_length;
1874 unsigned char *buffer = NULL;
1875 size_t output_length;
1876
Gilles Peskine8817f612018-12-18 00:18:46 +01001877 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +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 Peskine76f5c7b2018-07-06 16:53:09 +02001882
Gilles Peskine049c7532019-05-15 20:22:09 +02001883 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1884 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001885
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001886 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1887 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001888 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1889 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001890 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001891
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001892 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001893 NULL, 0,
1894 NULL, 0,
1895 buffer, buffer_length,
1896 &output_length );
1897 if( policy_alg == exercise_alg &&
1898 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001899 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001900 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001901 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001902
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001903 if( buffer_length != 0 )
1904 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001905 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001906 buffer, buffer_length,
1907 NULL, 0,
1908 buffer, buffer_length,
1909 &output_length );
1910 if( policy_alg == exercise_alg &&
1911 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001912 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001913 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001914 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001915
1916exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001917 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001918 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001919 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001920 mbedtls_free( buffer );
1921}
1922/* END_CASE */
1923
1924/* BEGIN_CASE */
1925void asymmetric_signature_key_policy( int policy_usage,
1926 int policy_alg,
1927 int key_type,
1928 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001929 int exercise_alg,
1930 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001931{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001932 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001933 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001934 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001935 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1936 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1937 * compatible with the policy and `payload_length_arg` is supposed to be
1938 * a valid input length to sign. If `payload_length_arg <= 0`,
1939 * `exercise_alg` is supposed to be forbidden by the policy. */
1940 int compatible_alg = payload_length_arg > 0;
1941 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001942 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1943 size_t signature_length;
1944
Gilles Peskine8817f612018-12-18 00:18:46 +01001945 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001946
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001947 psa_set_key_usage_flags( &attributes, policy_usage );
1948 psa_set_key_algorithm( &attributes, policy_alg );
1949 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001950
Gilles Peskine049c7532019-05-15 20:22:09 +02001951 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1952 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001953
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001954 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001955 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001956 signature, sizeof( signature ),
1957 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001958 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001959 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001960 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001961 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001962
1963 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001964 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001965 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001966 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001967 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001968 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001969 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001970 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001971
1972exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001973 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001974 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001975}
1976/* END_CASE */
1977
Janos Follathba3fab92019-06-11 14:50:16 +01001978/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001979void derive_key_policy( int policy_usage,
1980 int policy_alg,
1981 int key_type,
1982 data_t *key_data,
1983 int exercise_alg )
1984{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001985 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001986 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001987 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001988 psa_status_t status;
1989
Gilles Peskine8817f612018-12-18 00:18:46 +01001990 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001991
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001992 psa_set_key_usage_flags( &attributes, policy_usage );
1993 psa_set_key_algorithm( &attributes, policy_alg );
1994 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001995
Gilles Peskine049c7532019-05-15 20:22:09 +02001996 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1997 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001998
Janos Follathba3fab92019-06-11 14:50:16 +01001999 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2000
2001 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2002 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002003 {
Janos Follathba3fab92019-06-11 14:50:16 +01002004 PSA_ASSERT( psa_key_derivation_input_bytes(
2005 &operation,
2006 PSA_KEY_DERIVATION_INPUT_SEED,
2007 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002008 }
Janos Follathba3fab92019-06-11 14:50:16 +01002009
2010 status = psa_key_derivation_input_key( &operation,
2011 PSA_KEY_DERIVATION_INPUT_SECRET,
2012 handle );
2013
Gilles Peskineea0fb492018-07-12 17:17:20 +02002014 if( policy_alg == exercise_alg &&
2015 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002016 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002017 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002018 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002019
2020exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002021 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002022 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002023 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002024}
2025/* END_CASE */
2026
2027/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002028void agreement_key_policy( int policy_usage,
2029 int policy_alg,
2030 int key_type_arg,
2031 data_t *key_data,
2032 int exercise_alg )
2033{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002034 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002035 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002036 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002037 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002038 psa_status_t status;
2039
Gilles Peskine8817f612018-12-18 00:18:46 +01002040 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002041
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002042 psa_set_key_usage_flags( &attributes, policy_usage );
2043 psa_set_key_algorithm( &attributes, policy_alg );
2044 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002045
Gilles Peskine049c7532019-05-15 20:22:09 +02002046 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2047 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002048
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002049 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2050 status = key_agreement_with_self( &operation, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002051
Gilles Peskine01d718c2018-09-18 12:01:02 +02002052 if( policy_alg == exercise_alg &&
2053 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002054 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002055 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002056 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002057
2058exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002059 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002060 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002061 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002062}
2063/* END_CASE */
2064
2065/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002066void key_policy_alg2( int key_type_arg, data_t *key_data,
2067 int usage_arg, int alg_arg, int alg2_arg )
2068{
2069 psa_key_handle_t handle = 0;
2070 psa_key_type_t key_type = key_type_arg;
2071 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2072 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2073 psa_key_usage_t usage = usage_arg;
2074 psa_algorithm_t alg = alg_arg;
2075 psa_algorithm_t alg2 = alg2_arg;
2076
2077 PSA_ASSERT( psa_crypto_init( ) );
2078
2079 psa_set_key_usage_flags( &attributes, usage );
2080 psa_set_key_algorithm( &attributes, alg );
2081 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2082 psa_set_key_type( &attributes, key_type );
2083 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2084 &handle ) );
2085
2086 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
2087 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2088 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2089 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2090
2091 if( ! exercise_key( handle, usage, alg ) )
2092 goto exit;
2093 if( ! exercise_key( handle, usage, alg2 ) )
2094 goto exit;
2095
2096exit:
2097 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002098 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002099}
2100/* END_CASE */
2101
2102/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002103void raw_agreement_key_policy( int policy_usage,
2104 int policy_alg,
2105 int key_type_arg,
2106 data_t *key_data,
2107 int exercise_alg )
2108{
2109 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002110 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002111 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002112 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002113 psa_status_t status;
2114
2115 PSA_ASSERT( psa_crypto_init( ) );
2116
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002117 psa_set_key_usage_flags( &attributes, policy_usage );
2118 psa_set_key_algorithm( &attributes, policy_alg );
2119 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002120
Gilles Peskine049c7532019-05-15 20:22:09 +02002121 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2122 &handle ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002123
2124 status = raw_key_agreement_with_self( exercise_alg, handle );
2125
2126 if( policy_alg == exercise_alg &&
2127 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
2128 PSA_ASSERT( status );
2129 else
2130 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2131
2132exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002133 psa_key_derivation_abort( &operation );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002134 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002135 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002136}
2137/* END_CASE */
2138
2139/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002140void copy_success( int source_usage_arg,
2141 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002142 int type_arg, data_t *material,
2143 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002144 int target_usage_arg,
2145 int target_alg_arg, int target_alg2_arg,
2146 int expected_usage_arg,
2147 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002148{
Gilles Peskineca25db92019-04-19 11:43:08 +02002149 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2150 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002151 psa_key_usage_t expected_usage = expected_usage_arg;
2152 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002153 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02002154 psa_key_handle_t source_handle = 0;
2155 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002156 uint8_t *export_buffer = NULL;
2157
Gilles Peskine57ab7212019-01-28 13:03:09 +01002158 PSA_ASSERT( psa_crypto_init( ) );
2159
Gilles Peskineca25db92019-04-19 11:43:08 +02002160 /* Prepare the source key. */
2161 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2162 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002163 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002164 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002165 PSA_ASSERT( psa_import_key( &source_attributes,
2166 material->x, material->len,
2167 &source_handle ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002168 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002169
Gilles Peskineca25db92019-04-19 11:43:08 +02002170 /* Prepare the target attributes. */
2171 if( copy_attributes )
2172 target_attributes = source_attributes;
2173 if( target_usage_arg != -1 )
2174 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2175 if( target_alg_arg != -1 )
2176 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002177 if( target_alg2_arg != -1 )
2178 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002179
2180 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02002181 PSA_ASSERT( psa_copy_key( source_handle,
2182 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002183
2184 /* Destroy the source to ensure that this doesn't affect the target. */
2185 PSA_ASSERT( psa_destroy_key( source_handle ) );
2186
2187 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02002188 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
2189 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2190 psa_get_key_type( &target_attributes ) );
2191 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2192 psa_get_key_bits( &target_attributes ) );
2193 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2194 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002195 TEST_EQUAL( expected_alg2,
2196 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002197 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2198 {
2199 size_t length;
2200 ASSERT_ALLOC( export_buffer, material->len );
2201 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2202 material->len, &length ) );
2203 ASSERT_COMPARE( material->x, material->len,
2204 export_buffer, length );
2205 }
2206 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2207 goto exit;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002208 if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) )
2209 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002210
2211 PSA_ASSERT( psa_close_key( target_handle ) );
2212
2213exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002214 psa_reset_key_attributes( &source_attributes );
2215 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002216 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002217 mbedtls_free( export_buffer );
2218}
2219/* END_CASE */
2220
2221/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002222void copy_fail( int source_usage_arg,
2223 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002224 int type_arg, data_t *material,
2225 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002226 int target_usage_arg,
2227 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002228 int expected_status_arg )
2229{
2230 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2231 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2232 psa_key_handle_t source_handle = 0;
2233 psa_key_handle_t target_handle = 0;
2234
2235 PSA_ASSERT( psa_crypto_init( ) );
2236
2237 /* Prepare the source key. */
2238 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2239 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002240 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002241 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002242 PSA_ASSERT( psa_import_key( &source_attributes,
2243 material->x, material->len,
2244 &source_handle ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002245
2246 /* Prepare the target attributes. */
2247 psa_set_key_type( &target_attributes, target_type_arg );
2248 psa_set_key_bits( &target_attributes, target_bits_arg );
2249 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2250 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002251 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002252
2253 /* Try to copy the key. */
2254 TEST_EQUAL( psa_copy_key( source_handle,
2255 &target_attributes, &target_handle ),
2256 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002257
2258 PSA_ASSERT( psa_destroy_key( source_handle ) );
2259
Gilles Peskine4a644642019-05-03 17:14:08 +02002260exit:
2261 psa_reset_key_attributes( &source_attributes );
2262 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002263 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002264}
2265/* END_CASE */
2266
2267/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002268void hash_operation_init( )
2269{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002270 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002271 /* Test each valid way of initializing the object, except for `= {0}`, as
2272 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2273 * though it's OK by the C standard. We could test for this, but we'd need
2274 * to supress the Clang warning for the test. */
2275 psa_hash_operation_t func = psa_hash_operation_init( );
2276 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2277 psa_hash_operation_t zero;
2278
2279 memset( &zero, 0, sizeof( zero ) );
2280
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002281 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002282 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2283 PSA_ERROR_BAD_STATE );
2284 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2285 PSA_ERROR_BAD_STATE );
2286 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2287 PSA_ERROR_BAD_STATE );
2288
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002289 /* A default hash operation should be abortable without error. */
2290 PSA_ASSERT( psa_hash_abort( &func ) );
2291 PSA_ASSERT( psa_hash_abort( &init ) );
2292 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002293}
2294/* END_CASE */
2295
2296/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002297void hash_setup( int alg_arg,
2298 int expected_status_arg )
2299{
2300 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002301 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002302 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002303 psa_status_t status;
2304
Gilles Peskine8817f612018-12-18 00:18:46 +01002305 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002306
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002307 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002308 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002309
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002310 /* Whether setup succeeded or failed, abort must succeed. */
2311 PSA_ASSERT( psa_hash_abort( &operation ) );
2312
2313 /* If setup failed, reproduce the failure, so as to
2314 * test the resulting state of the operation object. */
2315 if( status != PSA_SUCCESS )
2316 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2317
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002318 /* Now the operation object should be reusable. */
2319#if defined(KNOWN_SUPPORTED_HASH_ALG)
2320 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2321 PSA_ASSERT( psa_hash_abort( &operation ) );
2322#endif
2323
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002324exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002325 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002326}
2327/* END_CASE */
2328
2329/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002330void hash_bad_order( )
2331{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002332 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002333 unsigned char input[] = "";
2334 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002335 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002336 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2337 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2338 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002339 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002340 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002341 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002342
Gilles Peskine8817f612018-12-18 00:18:46 +01002343 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002344
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002345 /* Call setup twice in a row. */
2346 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2347 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2348 PSA_ERROR_BAD_STATE );
2349 PSA_ASSERT( psa_hash_abort( &operation ) );
2350
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002351 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002352 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002353 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002354 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002355
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002356 /* Call update after finish. */
2357 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2358 PSA_ASSERT( psa_hash_finish( &operation,
2359 hash, sizeof( hash ), &hash_len ) );
2360 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002361 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002362 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002363
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002364 /* Call verify without calling setup beforehand. */
2365 TEST_EQUAL( psa_hash_verify( &operation,
2366 valid_hash, sizeof( valid_hash ) ),
2367 PSA_ERROR_BAD_STATE );
2368 PSA_ASSERT( psa_hash_abort( &operation ) );
2369
2370 /* Call verify after finish. */
2371 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2372 PSA_ASSERT( psa_hash_finish( &operation,
2373 hash, sizeof( hash ), &hash_len ) );
2374 TEST_EQUAL( psa_hash_verify( &operation,
2375 valid_hash, sizeof( valid_hash ) ),
2376 PSA_ERROR_BAD_STATE );
2377 PSA_ASSERT( psa_hash_abort( &operation ) );
2378
2379 /* Call verify twice in a row. */
2380 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2381 PSA_ASSERT( psa_hash_verify( &operation,
2382 valid_hash, sizeof( valid_hash ) ) );
2383 TEST_EQUAL( psa_hash_verify( &operation,
2384 valid_hash, sizeof( valid_hash ) ),
2385 PSA_ERROR_BAD_STATE );
2386 PSA_ASSERT( psa_hash_abort( &operation ) );
2387
2388 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002389 TEST_EQUAL( psa_hash_finish( &operation,
2390 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002391 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002392 PSA_ASSERT( psa_hash_abort( &operation ) );
2393
2394 /* Call finish twice in a row. */
2395 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2396 PSA_ASSERT( psa_hash_finish( &operation,
2397 hash, sizeof( hash ), &hash_len ) );
2398 TEST_EQUAL( psa_hash_finish( &operation,
2399 hash, sizeof( hash ), &hash_len ),
2400 PSA_ERROR_BAD_STATE );
2401 PSA_ASSERT( psa_hash_abort( &operation ) );
2402
2403 /* Call finish after calling verify. */
2404 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2405 PSA_ASSERT( psa_hash_verify( &operation,
2406 valid_hash, sizeof( valid_hash ) ) );
2407 TEST_EQUAL( psa_hash_finish( &operation,
2408 hash, sizeof( hash ), &hash_len ),
2409 PSA_ERROR_BAD_STATE );
2410 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002411
2412exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002413 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002414}
2415/* END_CASE */
2416
itayzafrir27e69452018-11-01 14:26:34 +02002417/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2418void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002419{
2420 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002421 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2422 * appended to it */
2423 unsigned char hash[] = {
2424 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2425 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2426 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002427 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002428 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002429
Gilles Peskine8817f612018-12-18 00:18:46 +01002430 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002431
itayzafrir27e69452018-11-01 14:26:34 +02002432 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002433 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002434 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002435 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002436
itayzafrir27e69452018-11-01 14:26:34 +02002437 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002438 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002439 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002440 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002441
itayzafrir27e69452018-11-01 14:26:34 +02002442 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002443 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002444 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002445 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002446
itayzafrirec93d302018-10-18 18:01:10 +03002447exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002448 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002449}
2450/* END_CASE */
2451
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002452/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2453void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002454{
2455 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002456 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002457 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002458 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002459 size_t hash_len;
2460
Gilles Peskine8817f612018-12-18 00:18:46 +01002461 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002462
itayzafrir58028322018-10-25 10:22:01 +03002463 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002464 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002465 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002466 hash, expected_size - 1, &hash_len ),
2467 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002468
2469exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002470 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002471}
2472/* END_CASE */
2473
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002474/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2475void hash_clone_source_state( )
2476{
2477 psa_algorithm_t alg = PSA_ALG_SHA_256;
2478 unsigned char hash[PSA_HASH_MAX_SIZE];
2479 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2480 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2481 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2482 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2483 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2484 size_t hash_len;
2485
2486 PSA_ASSERT( psa_crypto_init( ) );
2487 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2488
2489 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2490 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2491 PSA_ASSERT( psa_hash_finish( &op_finished,
2492 hash, sizeof( hash ), &hash_len ) );
2493 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2494 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2495
2496 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2497 PSA_ERROR_BAD_STATE );
2498
2499 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2500 PSA_ASSERT( psa_hash_finish( &op_init,
2501 hash, sizeof( hash ), &hash_len ) );
2502 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2503 PSA_ASSERT( psa_hash_finish( &op_finished,
2504 hash, sizeof( hash ), &hash_len ) );
2505 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2506 PSA_ASSERT( psa_hash_finish( &op_aborted,
2507 hash, sizeof( hash ), &hash_len ) );
2508
2509exit:
2510 psa_hash_abort( &op_source );
2511 psa_hash_abort( &op_init );
2512 psa_hash_abort( &op_setup );
2513 psa_hash_abort( &op_finished );
2514 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002515 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002516}
2517/* END_CASE */
2518
2519/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2520void hash_clone_target_state( )
2521{
2522 psa_algorithm_t alg = PSA_ALG_SHA_256;
2523 unsigned char hash[PSA_HASH_MAX_SIZE];
2524 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2525 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2526 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2527 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2528 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2529 size_t hash_len;
2530
2531 PSA_ASSERT( psa_crypto_init( ) );
2532
2533 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2534 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2535 PSA_ASSERT( psa_hash_finish( &op_finished,
2536 hash, sizeof( hash ), &hash_len ) );
2537 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2538 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2539
2540 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2541 PSA_ASSERT( psa_hash_finish( &op_target,
2542 hash, sizeof( hash ), &hash_len ) );
2543
2544 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2545 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2546 PSA_ERROR_BAD_STATE );
2547 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2548 PSA_ERROR_BAD_STATE );
2549
2550exit:
2551 psa_hash_abort( &op_target );
2552 psa_hash_abort( &op_init );
2553 psa_hash_abort( &op_setup );
2554 psa_hash_abort( &op_finished );
2555 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002556 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002557}
2558/* END_CASE */
2559
itayzafrir58028322018-10-25 10:22:01 +03002560/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002561void mac_operation_init( )
2562{
Jaeden Amero252ef282019-02-15 14:05:35 +00002563 const uint8_t input[1] = { 0 };
2564
Jaeden Amero769ce272019-01-04 11:48:03 +00002565 /* Test each valid way of initializing the object, except for `= {0}`, as
2566 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2567 * though it's OK by the C standard. We could test for this, but we'd need
2568 * to supress the Clang warning for the test. */
2569 psa_mac_operation_t func = psa_mac_operation_init( );
2570 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2571 psa_mac_operation_t zero;
2572
2573 memset( &zero, 0, sizeof( zero ) );
2574
Jaeden Amero252ef282019-02-15 14:05:35 +00002575 /* A freshly-initialized MAC operation should not be usable. */
2576 TEST_EQUAL( psa_mac_update( &func,
2577 input, sizeof( input ) ),
2578 PSA_ERROR_BAD_STATE );
2579 TEST_EQUAL( psa_mac_update( &init,
2580 input, sizeof( input ) ),
2581 PSA_ERROR_BAD_STATE );
2582 TEST_EQUAL( psa_mac_update( &zero,
2583 input, sizeof( input ) ),
2584 PSA_ERROR_BAD_STATE );
2585
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002586 /* A default MAC operation should be abortable without error. */
2587 PSA_ASSERT( psa_mac_abort( &func ) );
2588 PSA_ASSERT( psa_mac_abort( &init ) );
2589 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002590}
2591/* END_CASE */
2592
2593/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002594void mac_setup( int key_type_arg,
2595 data_t *key,
2596 int alg_arg,
2597 int expected_status_arg )
2598{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002599 psa_key_type_t key_type = key_type_arg;
2600 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002601 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002602 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002603 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2604#if defined(KNOWN_SUPPORTED_MAC_ALG)
2605 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2606#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002607
Gilles Peskine8817f612018-12-18 00:18:46 +01002608 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002609
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002610 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2611 &operation, &status ) )
2612 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002613 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002614
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002615 /* The operation object should be reusable. */
2616#if defined(KNOWN_SUPPORTED_MAC_ALG)
2617 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2618 smoke_test_key_data,
2619 sizeof( smoke_test_key_data ),
2620 KNOWN_SUPPORTED_MAC_ALG,
2621 &operation, &status ) )
2622 goto exit;
2623 TEST_EQUAL( status, PSA_SUCCESS );
2624#endif
2625
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002626exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002627 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002628}
2629/* END_CASE */
2630
2631/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002632void mac_bad_order( )
2633{
2634 psa_key_handle_t handle = 0;
2635 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2636 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2637 const uint8_t key[] = {
2638 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2639 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2640 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002641 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002642 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2643 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2644 size_t sign_mac_length = 0;
2645 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2646 const uint8_t verify_mac[] = {
2647 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2648 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2649 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2650
2651 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002652 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
2653 psa_set_key_algorithm( &attributes, alg );
2654 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002655
Gilles Peskine73676cb2019-05-15 20:15:10 +02002656 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002657
Jaeden Amero252ef282019-02-15 14:05:35 +00002658 /* Call update without calling setup beforehand. */
2659 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2660 PSA_ERROR_BAD_STATE );
2661 PSA_ASSERT( psa_mac_abort( &operation ) );
2662
2663 /* Call sign finish without calling setup beforehand. */
2664 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2665 &sign_mac_length),
2666 PSA_ERROR_BAD_STATE );
2667 PSA_ASSERT( psa_mac_abort( &operation ) );
2668
2669 /* Call verify finish without calling setup beforehand. */
2670 TEST_EQUAL( psa_mac_verify_finish( &operation,
2671 verify_mac, sizeof( verify_mac ) ),
2672 PSA_ERROR_BAD_STATE );
2673 PSA_ASSERT( psa_mac_abort( &operation ) );
2674
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002675 /* Call setup twice in a row. */
2676 PSA_ASSERT( psa_mac_sign_setup( &operation,
2677 handle, alg ) );
2678 TEST_EQUAL( psa_mac_sign_setup( &operation,
2679 handle, alg ),
2680 PSA_ERROR_BAD_STATE );
2681 PSA_ASSERT( psa_mac_abort( &operation ) );
2682
Jaeden Amero252ef282019-02-15 14:05:35 +00002683 /* Call update after sign finish. */
2684 PSA_ASSERT( psa_mac_sign_setup( &operation,
2685 handle, alg ) );
2686 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2687 PSA_ASSERT( psa_mac_sign_finish( &operation,
2688 sign_mac, sizeof( sign_mac ),
2689 &sign_mac_length ) );
2690 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2691 PSA_ERROR_BAD_STATE );
2692 PSA_ASSERT( psa_mac_abort( &operation ) );
2693
2694 /* Call update after verify finish. */
2695 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002696 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002697 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2698 PSA_ASSERT( psa_mac_verify_finish( &operation,
2699 verify_mac, sizeof( verify_mac ) ) );
2700 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2701 PSA_ERROR_BAD_STATE );
2702 PSA_ASSERT( psa_mac_abort( &operation ) );
2703
2704 /* Call sign finish twice in a row. */
2705 PSA_ASSERT( psa_mac_sign_setup( &operation,
2706 handle, alg ) );
2707 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2708 PSA_ASSERT( psa_mac_sign_finish( &operation,
2709 sign_mac, sizeof( sign_mac ),
2710 &sign_mac_length ) );
2711 TEST_EQUAL( psa_mac_sign_finish( &operation,
2712 sign_mac, sizeof( sign_mac ),
2713 &sign_mac_length ),
2714 PSA_ERROR_BAD_STATE );
2715 PSA_ASSERT( psa_mac_abort( &operation ) );
2716
2717 /* Call verify finish twice in a row. */
2718 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002719 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002720 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2721 PSA_ASSERT( psa_mac_verify_finish( &operation,
2722 verify_mac, sizeof( verify_mac ) ) );
2723 TEST_EQUAL( psa_mac_verify_finish( &operation,
2724 verify_mac, sizeof( verify_mac ) ),
2725 PSA_ERROR_BAD_STATE );
2726 PSA_ASSERT( psa_mac_abort( &operation ) );
2727
2728 /* Setup sign but try verify. */
2729 PSA_ASSERT( psa_mac_sign_setup( &operation,
2730 handle, alg ) );
2731 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2732 TEST_EQUAL( psa_mac_verify_finish( &operation,
2733 verify_mac, sizeof( verify_mac ) ),
2734 PSA_ERROR_BAD_STATE );
2735 PSA_ASSERT( psa_mac_abort( &operation ) );
2736
2737 /* Setup verify but try sign. */
2738 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002739 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002740 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2741 TEST_EQUAL( psa_mac_sign_finish( &operation,
2742 sign_mac, sizeof( sign_mac ),
2743 &sign_mac_length ),
2744 PSA_ERROR_BAD_STATE );
2745 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002746
Gilles Peskine76b29a72019-05-28 14:08:50 +02002747 PSA_ASSERT( psa_destroy_key( handle ) );
2748
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002749exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002750 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002751}
2752/* END_CASE */
2753
2754/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002755void mac_sign( int key_type_arg,
2756 data_t *key,
2757 int alg_arg,
2758 data_t *input,
2759 data_t *expected_mac )
2760{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002761 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002762 psa_key_type_t key_type = key_type_arg;
2763 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002764 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002765 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002766 /* Leave a little extra room in the output buffer. At the end of the
2767 * test, we'll check that the implementation didn't overwrite onto
2768 * this extra room. */
2769 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2770 size_t mac_buffer_size =
2771 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2772 size_t mac_length = 0;
2773
2774 memset( actual_mac, '+', sizeof( actual_mac ) );
2775 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2776 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2777
Gilles Peskine8817f612018-12-18 00:18:46 +01002778 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002779
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002780 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
2781 psa_set_key_algorithm( &attributes, alg );
2782 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002783
Gilles Peskine73676cb2019-05-15 20:15:10 +02002784 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002785
2786 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002787 PSA_ASSERT( psa_mac_sign_setup( &operation,
2788 handle, alg ) );
2789 PSA_ASSERT( psa_mac_update( &operation,
2790 input->x, input->len ) );
2791 PSA_ASSERT( psa_mac_sign_finish( &operation,
2792 actual_mac, mac_buffer_size,
2793 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002794
2795 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002796 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2797 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002798
2799 /* Verify that the end of the buffer is untouched. */
2800 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2801 sizeof( actual_mac ) - mac_length ) );
2802
2803exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002804 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002805 PSA_DONE( );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002806}
2807/* END_CASE */
2808
2809/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002810void mac_verify( int key_type_arg,
2811 data_t *key,
2812 int alg_arg,
2813 data_t *input,
2814 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002815{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002816 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002817 psa_key_type_t key_type = key_type_arg;
2818 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002819 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002820 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002821
Gilles Peskine69c12672018-06-28 00:07:19 +02002822 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2823
Gilles Peskine8817f612018-12-18 00:18:46 +01002824 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002825
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002826 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
2827 psa_set_key_algorithm( &attributes, alg );
2828 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002829
Gilles Peskine73676cb2019-05-15 20:15:10 +02002830 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002831
Gilles Peskine8817f612018-12-18 00:18:46 +01002832 PSA_ASSERT( psa_mac_verify_setup( &operation,
2833 handle, alg ) );
2834 PSA_ASSERT( psa_destroy_key( handle ) );
2835 PSA_ASSERT( psa_mac_update( &operation,
2836 input->x, input->len ) );
2837 PSA_ASSERT( psa_mac_verify_finish( &operation,
2838 expected_mac->x,
2839 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002840
2841exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002842 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002843 PSA_DONE( );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002844}
2845/* END_CASE */
2846
2847/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002848void cipher_operation_init( )
2849{
Jaeden Ameroab439972019-02-15 14:12:05 +00002850 const uint8_t input[1] = { 0 };
2851 unsigned char output[1] = { 0 };
2852 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002853 /* Test each valid way of initializing the object, except for `= {0}`, as
2854 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2855 * though it's OK by the C standard. We could test for this, but we'd need
2856 * to supress the Clang warning for the test. */
2857 psa_cipher_operation_t func = psa_cipher_operation_init( );
2858 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2859 psa_cipher_operation_t zero;
2860
2861 memset( &zero, 0, sizeof( zero ) );
2862
Jaeden Ameroab439972019-02-15 14:12:05 +00002863 /* A freshly-initialized cipher operation should not be usable. */
2864 TEST_EQUAL( psa_cipher_update( &func,
2865 input, sizeof( input ),
2866 output, sizeof( output ),
2867 &output_length ),
2868 PSA_ERROR_BAD_STATE );
2869 TEST_EQUAL( psa_cipher_update( &init,
2870 input, sizeof( input ),
2871 output, sizeof( output ),
2872 &output_length ),
2873 PSA_ERROR_BAD_STATE );
2874 TEST_EQUAL( psa_cipher_update( &zero,
2875 input, sizeof( input ),
2876 output, sizeof( output ),
2877 &output_length ),
2878 PSA_ERROR_BAD_STATE );
2879
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002880 /* A default cipher operation should be abortable without error. */
2881 PSA_ASSERT( psa_cipher_abort( &func ) );
2882 PSA_ASSERT( psa_cipher_abort( &init ) );
2883 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002884}
2885/* END_CASE */
2886
2887/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002888void cipher_setup( int key_type_arg,
2889 data_t *key,
2890 int alg_arg,
2891 int expected_status_arg )
2892{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002893 psa_key_type_t key_type = key_type_arg;
2894 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002895 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002896 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002897 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002898#if defined(KNOWN_SUPPORTED_MAC_ALG)
2899 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2900#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002901
Gilles Peskine8817f612018-12-18 00:18:46 +01002902 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002903
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002904 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2905 &operation, &status ) )
2906 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002907 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002908
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002909 /* The operation object should be reusable. */
2910#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2911 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2912 smoke_test_key_data,
2913 sizeof( smoke_test_key_data ),
2914 KNOWN_SUPPORTED_CIPHER_ALG,
2915 &operation, &status ) )
2916 goto exit;
2917 TEST_EQUAL( status, PSA_SUCCESS );
2918#endif
2919
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002920exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002921 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002922}
2923/* END_CASE */
2924
2925/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002926void cipher_bad_order( )
2927{
2928 psa_key_handle_t handle = 0;
2929 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2930 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002931 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002932 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2933 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2934 const uint8_t key[] = {
2935 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2936 0xaa, 0xaa, 0xaa, 0xaa };
2937 const uint8_t text[] = {
2938 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2939 0xbb, 0xbb, 0xbb, 0xbb };
2940 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2941 size_t length = 0;
2942
2943 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002944 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2945 psa_set_key_algorithm( &attributes, alg );
2946 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02002947 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002948
2949
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002950 /* Call encrypt setup twice in a row. */
2951 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2952 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2953 PSA_ERROR_BAD_STATE );
2954 PSA_ASSERT( psa_cipher_abort( &operation ) );
2955
2956 /* Call decrypt setup twice in a row. */
2957 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2958 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2959 PSA_ERROR_BAD_STATE );
2960 PSA_ASSERT( psa_cipher_abort( &operation ) );
2961
Jaeden Ameroab439972019-02-15 14:12:05 +00002962 /* Generate an IV without calling setup beforehand. */
2963 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2964 buffer, sizeof( buffer ),
2965 &length ),
2966 PSA_ERROR_BAD_STATE );
2967 PSA_ASSERT( psa_cipher_abort( &operation ) );
2968
2969 /* Generate an IV twice in a row. */
2970 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2971 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2972 buffer, sizeof( buffer ),
2973 &length ) );
2974 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2975 buffer, sizeof( buffer ),
2976 &length ),
2977 PSA_ERROR_BAD_STATE );
2978 PSA_ASSERT( psa_cipher_abort( &operation ) );
2979
2980 /* Generate an IV after it's already set. */
2981 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2982 PSA_ASSERT( psa_cipher_set_iv( &operation,
2983 iv, sizeof( iv ) ) );
2984 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2985 buffer, sizeof( buffer ),
2986 &length ),
2987 PSA_ERROR_BAD_STATE );
2988 PSA_ASSERT( psa_cipher_abort( &operation ) );
2989
2990 /* Set an IV without calling setup beforehand. */
2991 TEST_EQUAL( psa_cipher_set_iv( &operation,
2992 iv, sizeof( iv ) ),
2993 PSA_ERROR_BAD_STATE );
2994 PSA_ASSERT( psa_cipher_abort( &operation ) );
2995
2996 /* Set an IV after it's already set. */
2997 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2998 PSA_ASSERT( psa_cipher_set_iv( &operation,
2999 iv, sizeof( iv ) ) );
3000 TEST_EQUAL( psa_cipher_set_iv( &operation,
3001 iv, sizeof( iv ) ),
3002 PSA_ERROR_BAD_STATE );
3003 PSA_ASSERT( psa_cipher_abort( &operation ) );
3004
3005 /* Set an IV after it's already generated. */
3006 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3007 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3008 buffer, sizeof( buffer ),
3009 &length ) );
3010 TEST_EQUAL( psa_cipher_set_iv( &operation,
3011 iv, sizeof( iv ) ),
3012 PSA_ERROR_BAD_STATE );
3013 PSA_ASSERT( psa_cipher_abort( &operation ) );
3014
3015 /* Call update without calling setup beforehand. */
3016 TEST_EQUAL( psa_cipher_update( &operation,
3017 text, sizeof( text ),
3018 buffer, sizeof( buffer ),
3019 &length ),
3020 PSA_ERROR_BAD_STATE );
3021 PSA_ASSERT( psa_cipher_abort( &operation ) );
3022
3023 /* Call update without an IV where an IV is required. */
3024 TEST_EQUAL( psa_cipher_update( &operation,
3025 text, sizeof( text ),
3026 buffer, sizeof( buffer ),
3027 &length ),
3028 PSA_ERROR_BAD_STATE );
3029 PSA_ASSERT( psa_cipher_abort( &operation ) );
3030
3031 /* Call update after finish. */
3032 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3033 PSA_ASSERT( psa_cipher_set_iv( &operation,
3034 iv, sizeof( iv ) ) );
3035 PSA_ASSERT( psa_cipher_finish( &operation,
3036 buffer, sizeof( buffer ), &length ) );
3037 TEST_EQUAL( psa_cipher_update( &operation,
3038 text, sizeof( text ),
3039 buffer, sizeof( buffer ),
3040 &length ),
3041 PSA_ERROR_BAD_STATE );
3042 PSA_ASSERT( psa_cipher_abort( &operation ) );
3043
3044 /* Call finish without calling setup beforehand. */
3045 TEST_EQUAL( psa_cipher_finish( &operation,
3046 buffer, sizeof( buffer ), &length ),
3047 PSA_ERROR_BAD_STATE );
3048 PSA_ASSERT( psa_cipher_abort( &operation ) );
3049
3050 /* Call finish without an IV where an IV is required. */
3051 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3052 /* Not calling update means we are encrypting an empty buffer, which is OK
3053 * for cipher modes with padding. */
3054 TEST_EQUAL( psa_cipher_finish( &operation,
3055 buffer, sizeof( buffer ), &length ),
3056 PSA_ERROR_BAD_STATE );
3057 PSA_ASSERT( psa_cipher_abort( &operation ) );
3058
3059 /* Call finish twice in a row. */
3060 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3061 PSA_ASSERT( psa_cipher_set_iv( &operation,
3062 iv, sizeof( iv ) ) );
3063 PSA_ASSERT( psa_cipher_finish( &operation,
3064 buffer, sizeof( buffer ), &length ) );
3065 TEST_EQUAL( psa_cipher_finish( &operation,
3066 buffer, sizeof( buffer ), &length ),
3067 PSA_ERROR_BAD_STATE );
3068 PSA_ASSERT( psa_cipher_abort( &operation ) );
3069
Gilles Peskine76b29a72019-05-28 14:08:50 +02003070 PSA_ASSERT( psa_destroy_key( handle ) );
3071
Jaeden Ameroab439972019-02-15 14:12:05 +00003072exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003073 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003074}
3075/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003076
Gilles Peskine50e586b2018-06-08 14:28:46 +02003077/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003078void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003079 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003080 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003081 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003082{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003083 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003084 psa_status_t status;
3085 psa_key_type_t key_type = key_type_arg;
3086 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003087 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003088 unsigned char *output = NULL;
3089 size_t output_buffer_size = 0;
3090 size_t function_output_length = 0;
3091 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003092 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003093 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003094
Gilles Peskine8817f612018-12-18 00:18:46 +01003095 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003096
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003097 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3098 psa_set_key_algorithm( &attributes, alg );
3099 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003100
Gilles Peskine73676cb2019-05-15 20:15:10 +02003101 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003102
Gilles Peskine8817f612018-12-18 00:18:46 +01003103 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3104 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003105
Gilles Peskine423005e2019-05-06 15:22:57 +02003106 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003107 output_buffer_size = ( (size_t) input->len +
3108 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003109 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003110
Gilles Peskine8817f612018-12-18 00:18:46 +01003111 PSA_ASSERT( psa_cipher_update( &operation,
3112 input->x, input->len,
3113 output, output_buffer_size,
3114 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003115 total_output_length += function_output_length;
3116 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003117 output + total_output_length,
3118 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003119 &function_output_length );
3120 total_output_length += function_output_length;
3121
Gilles Peskinefe11b722018-12-18 00:24:04 +01003122 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003123 if( expected_status == PSA_SUCCESS )
3124 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003125 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003126 ASSERT_COMPARE( expected_output->x, expected_output->len,
3127 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003128 }
3129
3130exit:
3131 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003132 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003133 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003134}
3135/* END_CASE */
3136
3137/* BEGIN_CASE */
3138void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003139 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003140 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003141 int first_part_size_arg,
3142 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003143 data_t *expected_output )
3144{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003145 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003146 psa_key_type_t key_type = key_type_arg;
3147 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003148 size_t first_part_size = first_part_size_arg;
3149 size_t output1_length = output1_length_arg;
3150 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003151 unsigned char *output = NULL;
3152 size_t output_buffer_size = 0;
3153 size_t function_output_length = 0;
3154 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003155 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003156 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003157
Gilles Peskine8817f612018-12-18 00:18:46 +01003158 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003159
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003160 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3161 psa_set_key_algorithm( &attributes, alg );
3162 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003163
Gilles Peskine73676cb2019-05-15 20:15:10 +02003164 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003165
Gilles Peskine8817f612018-12-18 00:18:46 +01003166 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3167 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003168
Gilles Peskine423005e2019-05-06 15:22:57 +02003169 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003170 output_buffer_size = ( (size_t) input->len +
3171 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003172 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003173
Gilles Peskinee0866522019-02-19 19:44:00 +01003174 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003175 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3176 output, output_buffer_size,
3177 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003178 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003179 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003180 PSA_ASSERT( psa_cipher_update( &operation,
3181 input->x + first_part_size,
3182 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003183 output + total_output_length,
3184 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003185 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003186 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003187 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003188 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003189 output + total_output_length,
3190 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003191 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003192 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003193 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003194
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003195 ASSERT_COMPARE( expected_output->x, expected_output->len,
3196 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003197
3198exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003199 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003200 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003201 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003202}
3203/* END_CASE */
3204
3205/* BEGIN_CASE */
3206void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003207 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003208 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003209 int first_part_size_arg,
3210 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003211 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003212{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003213 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003214
3215 psa_key_type_t key_type = key_type_arg;
3216 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003217 size_t first_part_size = first_part_size_arg;
3218 size_t output1_length = output1_length_arg;
3219 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003220 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003221 size_t output_buffer_size = 0;
3222 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003223 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003224 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003225 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003226
Gilles Peskine8817f612018-12-18 00:18:46 +01003227 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003228
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003229 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3230 psa_set_key_algorithm( &attributes, alg );
3231 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003232
Gilles Peskine73676cb2019-05-15 20:15:10 +02003233 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003234
Gilles Peskine8817f612018-12-18 00:18:46 +01003235 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3236 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003237
Gilles Peskine423005e2019-05-06 15:22:57 +02003238 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003239
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003240 output_buffer_size = ( (size_t) input->len +
3241 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003242 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003243
Gilles Peskinee0866522019-02-19 19:44:00 +01003244 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003245 PSA_ASSERT( psa_cipher_update( &operation,
3246 input->x, first_part_size,
3247 output, output_buffer_size,
3248 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003249 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003250 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003251 PSA_ASSERT( psa_cipher_update( &operation,
3252 input->x + first_part_size,
3253 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003254 output + total_output_length,
3255 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003256 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003257 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003258 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003259 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003260 output + total_output_length,
3261 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003262 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003263 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003264 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003265
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003266 ASSERT_COMPARE( expected_output->x, expected_output->len,
3267 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003268
3269exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003270 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003271 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003272 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003273}
3274/* END_CASE */
3275
Gilles Peskine50e586b2018-06-08 14:28:46 +02003276/* BEGIN_CASE */
3277void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003278 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003279 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003280 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003281{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003282 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003283 psa_status_t status;
3284 psa_key_type_t key_type = key_type_arg;
3285 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003286 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003287 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003288 size_t output_buffer_size = 0;
3289 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003290 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003291 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003292 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003293
Gilles Peskine8817f612018-12-18 00:18:46 +01003294 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003295
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003296 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3297 psa_set_key_algorithm( &attributes, alg );
3298 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003299
Gilles Peskine73676cb2019-05-15 20:15:10 +02003300 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003301
Gilles Peskine8817f612018-12-18 00:18:46 +01003302 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3303 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003304
Gilles Peskine423005e2019-05-06 15:22:57 +02003305 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003306
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003307 output_buffer_size = ( (size_t) input->len +
3308 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003309 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003310
Gilles Peskine8817f612018-12-18 00:18:46 +01003311 PSA_ASSERT( psa_cipher_update( &operation,
3312 input->x, input->len,
3313 output, output_buffer_size,
3314 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003315 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003316 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003317 output + total_output_length,
3318 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003319 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003320 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003321 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003322
3323 if( expected_status == PSA_SUCCESS )
3324 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003325 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003326 ASSERT_COMPARE( expected_output->x, expected_output->len,
3327 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003328 }
3329
Gilles Peskine50e586b2018-06-08 14:28:46 +02003330exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003331 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003332 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003333 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003334}
3335/* END_CASE */
3336
Gilles Peskine50e586b2018-06-08 14:28:46 +02003337/* BEGIN_CASE */
3338void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003339 data_t *key,
3340 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003341{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003342 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003343 psa_key_type_t key_type = key_type_arg;
3344 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003345 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003346 size_t iv_size = 16;
3347 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003348 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003349 size_t output1_size = 0;
3350 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003351 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003352 size_t output2_size = 0;
3353 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003354 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003355 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3356 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003357 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003358
Gilles Peskine8817f612018-12-18 00:18:46 +01003359 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003360
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003361 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3362 psa_set_key_algorithm( &attributes, alg );
3363 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003364
Gilles Peskine73676cb2019-05-15 20:15:10 +02003365 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003366
Gilles Peskine8817f612018-12-18 00:18:46 +01003367 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3368 handle, alg ) );
3369 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3370 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003371
Gilles Peskine8817f612018-12-18 00:18:46 +01003372 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3373 iv, iv_size,
3374 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003375 output1_size = ( (size_t) input->len +
3376 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003377 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003378
Gilles Peskine8817f612018-12-18 00:18:46 +01003379 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3380 output1, output1_size,
3381 &output1_length ) );
3382 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003383 output1 + output1_length,
3384 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003385 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003386
Gilles Peskine048b7f02018-06-08 14:20:49 +02003387 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003388
Gilles Peskine8817f612018-12-18 00:18:46 +01003389 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003390
3391 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003392 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003393
Gilles Peskine8817f612018-12-18 00:18:46 +01003394 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3395 iv, iv_length ) );
3396 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3397 output2, output2_size,
3398 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003399 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003400 PSA_ASSERT( psa_cipher_finish( &operation2,
3401 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003402 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003403 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003404
Gilles Peskine048b7f02018-06-08 14:20:49 +02003405 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003406
Gilles Peskine8817f612018-12-18 00:18:46 +01003407 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003408
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003409 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003410
3411exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003412 mbedtls_free( output1 );
3413 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003414 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003415 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003416}
3417/* END_CASE */
3418
3419/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003420void cipher_verify_output_multipart( int alg_arg,
3421 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003422 data_t *key,
3423 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003424 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003425{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003426 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003427 psa_key_type_t key_type = key_type_arg;
3428 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003429 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003430 unsigned char iv[16] = {0};
3431 size_t iv_size = 16;
3432 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003433 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003434 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003435 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003436 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003437 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003438 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003439 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003440 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3441 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003442 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003443
Gilles Peskine8817f612018-12-18 00:18:46 +01003444 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003445
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003446 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3447 psa_set_key_algorithm( &attributes, alg );
3448 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003449
Gilles Peskine73676cb2019-05-15 20:15:10 +02003450 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003451
Gilles Peskine8817f612018-12-18 00:18:46 +01003452 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3453 handle, alg ) );
3454 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3455 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003456
Gilles Peskine8817f612018-12-18 00:18:46 +01003457 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3458 iv, iv_size,
3459 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003460 output1_buffer_size = ( (size_t) input->len +
3461 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003462 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003463
Gilles Peskinee0866522019-02-19 19:44:00 +01003464 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003465
Gilles Peskine8817f612018-12-18 00:18:46 +01003466 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3467 output1, output1_buffer_size,
3468 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003469 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003470
Gilles Peskine8817f612018-12-18 00:18:46 +01003471 PSA_ASSERT( psa_cipher_update( &operation1,
3472 input->x + first_part_size,
3473 input->len - first_part_size,
3474 output1, output1_buffer_size,
3475 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003476 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003477
Gilles Peskine8817f612018-12-18 00:18:46 +01003478 PSA_ASSERT( psa_cipher_finish( &operation1,
3479 output1 + output1_length,
3480 output1_buffer_size - output1_length,
3481 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003482 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003483
Gilles Peskine8817f612018-12-18 00:18:46 +01003484 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003485
Gilles Peskine048b7f02018-06-08 14:20:49 +02003486 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003487 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003488
Gilles Peskine8817f612018-12-18 00:18:46 +01003489 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3490 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003491
Gilles Peskine8817f612018-12-18 00:18:46 +01003492 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3493 output2, output2_buffer_size,
3494 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003495 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003496
Gilles Peskine8817f612018-12-18 00:18:46 +01003497 PSA_ASSERT( psa_cipher_update( &operation2,
3498 output1 + first_part_size,
3499 output1_length - first_part_size,
3500 output2, output2_buffer_size,
3501 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003502 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003503
Gilles Peskine8817f612018-12-18 00:18:46 +01003504 PSA_ASSERT( psa_cipher_finish( &operation2,
3505 output2 + output2_length,
3506 output2_buffer_size - output2_length,
3507 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003508 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003509
Gilles Peskine8817f612018-12-18 00:18:46 +01003510 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003511
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003512 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003513
3514exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003515 mbedtls_free( output1 );
3516 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003517 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003518 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003519}
3520/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003521
Gilles Peskine20035e32018-02-03 22:44:14 +01003522/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003523void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003524 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003525 data_t *nonce,
3526 data_t *additional_data,
3527 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003528 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003529{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003530 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003531 psa_key_type_t key_type = key_type_arg;
3532 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003533 unsigned char *output_data = NULL;
3534 size_t output_size = 0;
3535 size_t output_length = 0;
3536 unsigned char *output_data2 = NULL;
3537 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003538 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003539 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003540 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003541
Gilles Peskine4abf7412018-06-18 16:35:34 +02003542 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003543 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3544 * should be exact. */
3545 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3546 TEST_EQUAL( output_size,
3547 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003548 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003549
Gilles Peskine8817f612018-12-18 00:18:46 +01003550 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003551
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003552 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3553 psa_set_key_algorithm( &attributes, alg );
3554 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003555
Gilles Peskine049c7532019-05-15 20:22:09 +02003556 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3557 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003558
Gilles Peskinefe11b722018-12-18 00:24:04 +01003559 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3560 nonce->x, nonce->len,
3561 additional_data->x,
3562 additional_data->len,
3563 input_data->x, input_data->len,
3564 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003565 &output_length ),
3566 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003567
3568 if( PSA_SUCCESS == expected_result )
3569 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003570 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003571
Gilles Peskine003a4a92019-05-14 16:09:40 +02003572 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3573 * should be exact. */
3574 TEST_EQUAL( input_data->len,
3575 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3576
Gilles Peskinefe11b722018-12-18 00:24:04 +01003577 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3578 nonce->x, nonce->len,
3579 additional_data->x,
3580 additional_data->len,
3581 output_data, output_length,
3582 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003583 &output_length2 ),
3584 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003585
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003586 ASSERT_COMPARE( input_data->x, input_data->len,
3587 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003588 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003589
Gilles Peskinea1cac842018-06-11 19:33:02 +02003590exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003591 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003592 mbedtls_free( output_data );
3593 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003594 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003595}
3596/* END_CASE */
3597
3598/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003599void aead_encrypt( int key_type_arg, data_t *key_data,
3600 int alg_arg,
3601 data_t *nonce,
3602 data_t *additional_data,
3603 data_t *input_data,
3604 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003605{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003606 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003607 psa_key_type_t key_type = key_type_arg;
3608 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003609 unsigned char *output_data = NULL;
3610 size_t output_size = 0;
3611 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003612 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003613 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003614
Gilles Peskine4abf7412018-06-18 16:35:34 +02003615 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003616 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3617 * should be exact. */
3618 TEST_EQUAL( output_size,
3619 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003620 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003621
Gilles Peskine8817f612018-12-18 00:18:46 +01003622 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003623
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003624 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3625 psa_set_key_algorithm( &attributes, alg );
3626 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003627
Gilles Peskine049c7532019-05-15 20:22:09 +02003628 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3629 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003630
Gilles Peskine8817f612018-12-18 00:18:46 +01003631 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3632 nonce->x, nonce->len,
3633 additional_data->x, additional_data->len,
3634 input_data->x, input_data->len,
3635 output_data, output_size,
3636 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003637
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003638 ASSERT_COMPARE( expected_result->x, expected_result->len,
3639 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003640
Gilles Peskinea1cac842018-06-11 19:33:02 +02003641exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003642 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003643 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003644 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003645}
3646/* END_CASE */
3647
3648/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003649void aead_decrypt( int key_type_arg, data_t *key_data,
3650 int alg_arg,
3651 data_t *nonce,
3652 data_t *additional_data,
3653 data_t *input_data,
3654 data_t *expected_data,
3655 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003656{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003657 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003658 psa_key_type_t key_type = key_type_arg;
3659 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003660 unsigned char *output_data = NULL;
3661 size_t output_size = 0;
3662 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003663 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003664 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003665 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003666
Gilles Peskine003a4a92019-05-14 16:09:40 +02003667 output_size = input_data->len - tag_length;
3668 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3669 * should be exact. */
3670 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3671 TEST_EQUAL( output_size,
3672 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003673 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003674
Gilles Peskine8817f612018-12-18 00:18:46 +01003675 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003676
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003677 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3678 psa_set_key_algorithm( &attributes, alg );
3679 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003680
Gilles Peskine049c7532019-05-15 20:22:09 +02003681 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3682 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003683
Gilles Peskinefe11b722018-12-18 00:24:04 +01003684 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3685 nonce->x, nonce->len,
3686 additional_data->x,
3687 additional_data->len,
3688 input_data->x, input_data->len,
3689 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003690 &output_length ),
3691 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003692
Gilles Peskine2d277862018-06-18 15:41:12 +02003693 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003694 ASSERT_COMPARE( expected_data->x, expected_data->len,
3695 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003696
Gilles Peskinea1cac842018-06-11 19:33:02 +02003697exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003698 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003699 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003700 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003701}
3702/* END_CASE */
3703
3704/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003705void signature_size( int type_arg,
3706 int bits,
3707 int alg_arg,
3708 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003709{
3710 psa_key_type_t type = type_arg;
3711 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003712 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003713 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003714exit:
3715 ;
3716}
3717/* END_CASE */
3718
3719/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003720void sign_deterministic( int key_type_arg, data_t *key_data,
3721 int alg_arg, data_t *input_data,
3722 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003723{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003724 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003725 psa_key_type_t key_type = key_type_arg;
3726 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003727 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003728 unsigned char *signature = NULL;
3729 size_t signature_size;
3730 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003731 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003732
Gilles Peskine8817f612018-12-18 00:18:46 +01003733 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003734
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003735 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3736 psa_set_key_algorithm( &attributes, alg );
3737 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003738
Gilles Peskine049c7532019-05-15 20:22:09 +02003739 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3740 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003741 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3742 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003743
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003744 /* Allocate a buffer which has the size advertized by the
3745 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003746 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3747 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003748 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003749 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003750 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003751
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003752 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003753 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3754 input_data->x, input_data->len,
3755 signature, signature_size,
3756 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003757 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003758 ASSERT_COMPARE( output_data->x, output_data->len,
3759 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003760
3761exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003762 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003763 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003764 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003765 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003766}
3767/* END_CASE */
3768
3769/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003770void sign_fail( int key_type_arg, data_t *key_data,
3771 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003772 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003773{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003774 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003775 psa_key_type_t key_type = key_type_arg;
3776 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003777 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003778 psa_status_t actual_status;
3779 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003780 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003781 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003782 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003783
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003784 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003785
Gilles Peskine8817f612018-12-18 00:18:46 +01003786 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003787
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003788 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3789 psa_set_key_algorithm( &attributes, alg );
3790 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003791
Gilles Peskine049c7532019-05-15 20:22:09 +02003792 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3793 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003794
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003795 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003796 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003797 signature, signature_size,
3798 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003799 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003800 /* The value of *signature_length is unspecified on error, but
3801 * whatever it is, it should be less than signature_size, so that
3802 * if the caller tries to read *signature_length bytes without
3803 * checking the error code then they don't overflow a buffer. */
3804 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003805
3806exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003807 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003808 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003809 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003810 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003811}
3812/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003813
3814/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003815void sign_verify( int key_type_arg, data_t *key_data,
3816 int alg_arg, data_t *input_data )
3817{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003818 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003819 psa_key_type_t key_type = key_type_arg;
3820 psa_algorithm_t alg = alg_arg;
3821 size_t key_bits;
3822 unsigned char *signature = NULL;
3823 size_t signature_size;
3824 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003825 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003826
Gilles Peskine8817f612018-12-18 00:18:46 +01003827 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003828
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003829 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
3830 psa_set_key_algorithm( &attributes, alg );
3831 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003832
Gilles Peskine049c7532019-05-15 20:22:09 +02003833 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3834 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003835 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3836 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003837
3838 /* Allocate a buffer which has the size advertized by the
3839 * library. */
3840 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3841 key_bits, alg );
3842 TEST_ASSERT( signature_size != 0 );
3843 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003844 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003845
3846 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003847 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3848 input_data->x, input_data->len,
3849 signature, signature_size,
3850 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003851 /* Check that the signature length looks sensible. */
3852 TEST_ASSERT( signature_length <= signature_size );
3853 TEST_ASSERT( signature_length > 0 );
3854
3855 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003856 PSA_ASSERT( psa_asymmetric_verify(
3857 handle, alg,
3858 input_data->x, input_data->len,
3859 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003860
3861 if( input_data->len != 0 )
3862 {
3863 /* Flip a bit in the input and verify that the signature is now
3864 * detected as invalid. Flip a bit at the beginning, not at the end,
3865 * because ECDSA may ignore the last few bits of the input. */
3866 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003867 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3868 input_data->x, input_data->len,
3869 signature, signature_length ),
3870 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003871 }
3872
3873exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003874 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003875 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003876 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003877 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003878}
3879/* END_CASE */
3880
3881/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003882void asymmetric_verify( int key_type_arg, data_t *key_data,
3883 int alg_arg, data_t *hash_data,
3884 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003885{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003886 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003887 psa_key_type_t key_type = key_type_arg;
3888 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003889 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003890
Gilles Peskine69c12672018-06-28 00:07:19 +02003891 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3892
Gilles Peskine8817f612018-12-18 00:18:46 +01003893 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003894
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003895 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3896 psa_set_key_algorithm( &attributes, alg );
3897 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003898
Gilles Peskine049c7532019-05-15 20:22:09 +02003899 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3900 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03003901
Gilles Peskine8817f612018-12-18 00:18:46 +01003902 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3903 hash_data->x, hash_data->len,
3904 signature_data->x,
3905 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003906exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003907 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003908 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003909 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003910}
3911/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003912
3913/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003914void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3915 int alg_arg, data_t *hash_data,
3916 data_t *signature_data,
3917 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003918{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003919 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003920 psa_key_type_t key_type = key_type_arg;
3921 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003922 psa_status_t actual_status;
3923 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003924 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003925
Gilles Peskine8817f612018-12-18 00:18:46 +01003926 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003927
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003928 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3929 psa_set_key_algorithm( &attributes, alg );
3930 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003931
Gilles Peskine049c7532019-05-15 20:22:09 +02003932 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3933 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003934
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003935 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003936 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003937 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003938 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003939
Gilles Peskinefe11b722018-12-18 00:24:04 +01003940 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003941
3942exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003943 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003944 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003945 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003946}
3947/* END_CASE */
3948
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003949/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003950void asymmetric_encrypt( int key_type_arg,
3951 data_t *key_data,
3952 int alg_arg,
3953 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003954 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003955 int expected_output_length_arg,
3956 int expected_status_arg )
3957{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003958 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003959 psa_key_type_t key_type = key_type_arg;
3960 psa_algorithm_t alg = alg_arg;
3961 size_t expected_output_length = expected_output_length_arg;
3962 size_t key_bits;
3963 unsigned char *output = NULL;
3964 size_t output_size;
3965 size_t output_length = ~0;
3966 psa_status_t actual_status;
3967 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003968 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003969
Gilles Peskine8817f612018-12-18 00:18:46 +01003970 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003971
Gilles Peskine656896e2018-06-29 19:12:28 +02003972 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003973 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3974 psa_set_key_algorithm( &attributes, alg );
3975 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003976 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3977 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003978
3979 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003980 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3981 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02003982 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003983 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003984
3985 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003986 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003987 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003988 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003989 output, output_size,
3990 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003991 TEST_EQUAL( actual_status, expected_status );
3992 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003993
Gilles Peskine68428122018-06-30 18:42:41 +02003994 /* If the label is empty, the test framework puts a non-null pointer
3995 * in label->x. Test that a null pointer works as well. */
3996 if( label->len == 0 )
3997 {
3998 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003999 if( output_size != 0 )
4000 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004001 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004002 input_data->x, input_data->len,
4003 NULL, label->len,
4004 output, output_size,
4005 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004006 TEST_EQUAL( actual_status, expected_status );
4007 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004008 }
4009
Gilles Peskine656896e2018-06-29 19:12:28 +02004010exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004011 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004012 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02004013 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004014 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004015}
4016/* END_CASE */
4017
4018/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004019void asymmetric_encrypt_decrypt( int key_type_arg,
4020 data_t *key_data,
4021 int alg_arg,
4022 data_t *input_data,
4023 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004024{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004025 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004026 psa_key_type_t key_type = key_type_arg;
4027 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004028 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004029 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004030 size_t output_size;
4031 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004032 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004033 size_t output2_size;
4034 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004035 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004036
Gilles Peskine8817f612018-12-18 00:18:46 +01004037 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004038
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004039 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4040 psa_set_key_algorithm( &attributes, alg );
4041 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004042
Gilles Peskine049c7532019-05-15 20:22:09 +02004043 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4044 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004045
4046 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004047 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4048 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004049 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004050 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004051 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004052 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004053
Gilles Peskineeebd7382018-06-08 18:11:54 +02004054 /* We test encryption by checking that encrypt-then-decrypt gives back
4055 * the original plaintext because of the non-optional random
4056 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004057 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
4058 input_data->x, input_data->len,
4059 label->x, label->len,
4060 output, output_size,
4061 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004062 /* We don't know what ciphertext length to expect, but check that
4063 * it looks sensible. */
4064 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004065
Gilles Peskine8817f612018-12-18 00:18:46 +01004066 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4067 output, output_length,
4068 label->x, label->len,
4069 output2, output2_size,
4070 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004071 ASSERT_COMPARE( input_data->x, input_data->len,
4072 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004073
4074exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004075 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004076 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004077 mbedtls_free( output );
4078 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004079 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004080}
4081/* END_CASE */
4082
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004083/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004084void asymmetric_decrypt( int key_type_arg,
4085 data_t *key_data,
4086 int alg_arg,
4087 data_t *input_data,
4088 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004089 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004090{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004091 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004092 psa_key_type_t key_type = key_type_arg;
4093 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004094 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004095 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004096 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004097 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004098
Jaeden Amero412654a2019-02-06 12:57:46 +00004099 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004100 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004101
Gilles Peskine8817f612018-12-18 00:18:46 +01004102 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004103
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004104 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4105 psa_set_key_algorithm( &attributes, alg );
4106 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004107
Gilles Peskine049c7532019-05-15 20:22:09 +02004108 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4109 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004110
Gilles Peskine8817f612018-12-18 00:18:46 +01004111 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4112 input_data->x, input_data->len,
4113 label->x, label->len,
4114 output,
4115 output_size,
4116 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004117 ASSERT_COMPARE( expected_data->x, expected_data->len,
4118 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004119
Gilles Peskine68428122018-06-30 18:42:41 +02004120 /* If the label is empty, the test framework puts a non-null pointer
4121 * in label->x. Test that a null pointer works as well. */
4122 if( label->len == 0 )
4123 {
4124 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004125 if( output_size != 0 )
4126 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004127 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4128 input_data->x, input_data->len,
4129 NULL, label->len,
4130 output,
4131 output_size,
4132 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004133 ASSERT_COMPARE( expected_data->x, expected_data->len,
4134 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004135 }
4136
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004137exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004138 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004139 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004140 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004141 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004142}
4143/* END_CASE */
4144
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004145/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004146void asymmetric_decrypt_fail( int key_type_arg,
4147 data_t *key_data,
4148 int alg_arg,
4149 data_t *input_data,
4150 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004151 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004152 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004153{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004154 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004155 psa_key_type_t key_type = key_type_arg;
4156 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004157 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004158 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004159 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004160 psa_status_t actual_status;
4161 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004162 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004163
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004164 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004165
Gilles Peskine8817f612018-12-18 00:18:46 +01004166 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004167
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004168 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4169 psa_set_key_algorithm( &attributes, alg );
4170 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004171
Gilles Peskine049c7532019-05-15 20:22:09 +02004172 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4173 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004174
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004175 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004176 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004177 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004178 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004179 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004180 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004181 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004182
Gilles Peskine68428122018-06-30 18:42:41 +02004183 /* If the label is empty, the test framework puts a non-null pointer
4184 * in label->x. Test that a null pointer works as well. */
4185 if( label->len == 0 )
4186 {
4187 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004188 if( output_size != 0 )
4189 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004190 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004191 input_data->x, input_data->len,
4192 NULL, label->len,
4193 output, output_size,
4194 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004195 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004196 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004197 }
4198
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004199exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004200 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004201 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004202 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004203 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004204}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004205/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004206
4207/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004208void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004209{
4210 /* Test each valid way of initializing the object, except for `= {0}`, as
4211 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4212 * though it's OK by the C standard. We could test for this, but we'd need
4213 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004214 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004215 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4216 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4217 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004218
4219 memset( &zero, 0, sizeof( zero ) );
4220
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004221 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004222 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004223 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004224 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004225 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004226 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004227 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004228
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004229 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004230 PSA_ASSERT( psa_key_derivation_abort(&func) );
4231 PSA_ASSERT( psa_key_derivation_abort(&init) );
4232 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004233}
4234/* END_CASE */
4235
Janos Follath16de4a42019-06-13 16:32:24 +01004236/* BEGIN_CASE */
4237void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004238{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004239 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004240 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004241 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004242
Gilles Peskine8817f612018-12-18 00:18:46 +01004243 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004244
Janos Follath16de4a42019-06-13 16:32:24 +01004245 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004246 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004247
4248exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004249 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004250 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004251}
4252/* END_CASE */
4253
Janos Follathaf3c2a02019-06-12 12:34:34 +01004254/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004255void derive_set_capacity( int alg_arg, int capacity_arg,
4256 int expected_status_arg )
4257{
4258 psa_algorithm_t alg = alg_arg;
4259 size_t capacity = capacity_arg;
4260 psa_status_t expected_status = expected_status_arg;
4261 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4262
4263 PSA_ASSERT( psa_crypto_init( ) );
4264
4265 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4266
4267 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4268 expected_status );
4269
4270exit:
4271 psa_key_derivation_abort( &operation );
4272 PSA_DONE( );
4273}
4274/* END_CASE */
4275
4276/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004277void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004278 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004279 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004280 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004281 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004282 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004283 int expected_status_arg3,
4284 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004285{
4286 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004287 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4288 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004289 psa_status_t expected_statuses[] = {expected_status_arg1,
4290 expected_status_arg2,
4291 expected_status_arg3};
4292 data_t *inputs[] = {input1, input2, input3};
4293 psa_key_handle_t handles[] = {0, 0, 0};
4294 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4295 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4296 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004297 psa_key_type_t output_key_type = output_key_type_arg;
4298 psa_key_handle_t output_handle = 0;
4299 psa_status_t expected_output_status = expected_output_status_arg;
4300 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004301
4302 PSA_ASSERT( psa_crypto_init( ) );
4303
4304 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4305 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004306
4307 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4308
4309 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4310 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004311 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004312 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004313 psa_set_key_type( &attributes, key_types[i] );
4314 PSA_ASSERT( psa_import_key( &attributes,
4315 inputs[i]->x, inputs[i]->len,
4316 &handles[i] ) );
4317 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4318 handles[i] ),
4319 expected_statuses[i] );
4320 }
4321 else
4322 {
4323 TEST_EQUAL( psa_key_derivation_input_bytes(
4324 &operation, steps[i],
4325 inputs[i]->x, inputs[i]->len ),
4326 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004327 }
4328 }
4329
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004330 if( output_key_type != PSA_KEY_TYPE_NONE )
4331 {
4332 psa_reset_key_attributes( &attributes );
4333 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4334 psa_set_key_bits( &attributes, 8 );
4335 actual_output_status =
4336 psa_key_derivation_output_key( &attributes, &operation,
4337 &output_handle );
4338 }
4339 else
4340 {
4341 uint8_t buffer[1];
4342 actual_output_status =
4343 psa_key_derivation_output_bytes( &operation,
4344 buffer, sizeof( buffer ) );
4345 }
4346 TEST_EQUAL( actual_output_status, expected_output_status );
4347
Janos Follathaf3c2a02019-06-12 12:34:34 +01004348exit:
4349 psa_key_derivation_abort( &operation );
4350 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4351 psa_destroy_key( handles[i] );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004352 psa_destroy_key( output_handle );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004353 PSA_DONE( );
4354}
4355/* END_CASE */
4356
Janos Follathd958bb72019-07-03 15:02:16 +01004357/* BEGIN_CASE */
4358void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004359{
Janos Follathd958bb72019-07-03 15:02:16 +01004360 psa_algorithm_t alg = alg_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004361 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004362 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004363 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004364 unsigned char input1[] = "Input 1";
4365 size_t input1_length = sizeof( input1 );
4366 unsigned char input2[] = "Input 2";
4367 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004368 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004369 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004370 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4371 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4372 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004373 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004374
Gilles Peskine8817f612018-12-18 00:18:46 +01004375 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004376
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004377 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4378 psa_set_key_algorithm( &attributes, alg );
4379 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004380
Gilles Peskine73676cb2019-05-15 20:15:10 +02004381 PSA_ASSERT( psa_import_key( &attributes,
4382 key_data, sizeof( key_data ),
4383 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004384
4385 /* valid key derivation */
Janos Follathd958bb72019-07-03 15:02:16 +01004386 if( !setup_key_derivation_wrap( &operation, handle, alg,
4387 input1, input1_length,
4388 input2, input2_length,
4389 capacity ) )
4390 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004391
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004392 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004393 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004394 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004395
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004396 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004397
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004398 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004399 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004400
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004401exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004402 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004403 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004404 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004405}
4406/* END_CASE */
4407
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004408/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004409void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004410{
4411 uint8_t output_buffer[16];
4412 size_t buffer_size = 16;
4413 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004414 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004415
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004416 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4417 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004418 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004419
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004420 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004421 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004422
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004423 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004424
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004425 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4426 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004427 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004428
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004429 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004430 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004431
4432exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004433 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004434}
4435/* END_CASE */
4436
4437/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004438void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004439 int step1_arg, data_t *input1,
4440 int step2_arg, data_t *input2,
4441 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004442 int requested_capacity_arg,
4443 data_t *expected_output1,
4444 data_t *expected_output2 )
4445{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004446 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004447 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4448 data_t *inputs[] = {input1, input2, input3};
4449 psa_key_handle_t handles[] = {0, 0, 0};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004450 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004451 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004452 uint8_t *expected_outputs[2] =
4453 {expected_output1->x, expected_output2->x};
4454 size_t output_sizes[2] =
4455 {expected_output1->len, expected_output2->len};
4456 size_t output_buffer_size = 0;
4457 uint8_t *output_buffer = NULL;
4458 size_t expected_capacity;
4459 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004461 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004462 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004463
4464 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4465 {
4466 if( output_sizes[i] > output_buffer_size )
4467 output_buffer_size = output_sizes[i];
4468 if( output_sizes[i] == 0 )
4469 expected_outputs[i] = NULL;
4470 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004471 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004472 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004473
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004474 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4475 psa_set_key_algorithm( &attributes, alg );
4476 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004477
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004478 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004479 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4480 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4481 requested_capacity ) );
4482 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004483 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004484 switch( steps[i] )
4485 {
4486 case 0:
4487 break;
4488 case PSA_KEY_DERIVATION_INPUT_SECRET:
4489 PSA_ASSERT( psa_import_key( &attributes,
4490 inputs[i]->x, inputs[i]->len,
4491 &handles[i] ) );
4492 PSA_ASSERT( psa_key_derivation_input_key(
4493 &operation, steps[i],
4494 handles[i] ) );
4495 break;
4496 default:
4497 PSA_ASSERT( psa_key_derivation_input_bytes(
4498 &operation, steps[i],
4499 inputs[i]->x, inputs[i]->len ) );
4500 break;
4501 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004502 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004503
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004504 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004505 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004506 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004507 expected_capacity = requested_capacity;
4508
4509 /* Expansion phase. */
4510 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4511 {
4512 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004513 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004514 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004515 if( expected_capacity == 0 && output_sizes[i] == 0 )
4516 {
4517 /* Reading 0 bytes when 0 bytes are available can go either way. */
4518 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004519 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004520 continue;
4521 }
4522 else if( expected_capacity == 0 ||
4523 output_sizes[i] > expected_capacity )
4524 {
4525 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004526 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004527 expected_capacity = 0;
4528 continue;
4529 }
4530 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004531 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004532 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004533 ASSERT_COMPARE( output_buffer, output_sizes[i],
4534 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004535 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004536 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004537 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004538 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004539 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004540 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004541 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004542
4543exit:
4544 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004545 psa_key_derivation_abort( &operation );
Gilles Peskine1468da72019-05-29 17:35:49 +02004546 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4547 psa_destroy_key( handles[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004548 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004549}
4550/* END_CASE */
4551
4552/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004553void derive_full( int alg_arg,
4554 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004555 data_t *input1,
4556 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004557 int requested_capacity_arg )
4558{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004559 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004560 psa_algorithm_t alg = alg_arg;
4561 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004562 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004563 unsigned char output_buffer[16];
4564 size_t expected_capacity = requested_capacity;
4565 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004566 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004567
Gilles Peskine8817f612018-12-18 00:18:46 +01004568 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004569
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004570 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4571 psa_set_key_algorithm( &attributes, alg );
4572 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004573
Gilles Peskine049c7532019-05-15 20:22:09 +02004574 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4575 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004576
Janos Follathf2815ea2019-07-03 12:41:36 +01004577 if( !setup_key_derivation_wrap( &operation, handle, alg,
4578 input1->x, input1->len,
4579 input2->x, input2->len,
4580 requested_capacity ) )
4581 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004582
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004583 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004584 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004585 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004586
4587 /* Expansion phase. */
4588 while( current_capacity > 0 )
4589 {
4590 size_t read_size = sizeof( output_buffer );
4591 if( read_size > current_capacity )
4592 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004593 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004594 output_buffer,
4595 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004596 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004597 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004598 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004599 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004600 }
4601
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004602 /* Check that the operation refuses to go over capacity. */
4603 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004604 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004605
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004606 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004607
4608exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004609 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004610 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004611 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004612}
4613/* END_CASE */
4614
Janos Follathe60c9052019-07-03 13:51:30 +01004615/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004616void derive_key_exercise( int alg_arg,
4617 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004618 data_t *input1,
4619 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004620 int derived_type_arg,
4621 int derived_bits_arg,
4622 int derived_usage_arg,
4623 int derived_alg_arg )
4624{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004625 psa_key_handle_t base_handle = 0;
4626 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004627 psa_algorithm_t alg = alg_arg;
4628 psa_key_type_t derived_type = derived_type_arg;
4629 size_t derived_bits = derived_bits_arg;
4630 psa_key_usage_t derived_usage = derived_usage_arg;
4631 psa_algorithm_t derived_alg = derived_alg_arg;
4632 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004633 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004634 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004635 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004636
Gilles Peskine8817f612018-12-18 00:18:46 +01004637 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004638
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004639 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4640 psa_set_key_algorithm( &attributes, alg );
4641 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004642 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4643 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004644
4645 /* Derive a key. */
Janos Follathe60c9052019-07-03 13:51:30 +01004646 if ( setup_key_derivation_wrap( &operation, base_handle, alg,
4647 input1->x, input1->len,
4648 input2->x, input2->len, capacity ) )
4649 goto exit;
4650
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004651 psa_set_key_usage_flags( &attributes, derived_usage );
4652 psa_set_key_algorithm( &attributes, derived_alg );
4653 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004654 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004655 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004656 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004657
4658 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004659 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4660 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4661 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004662
4663 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004664 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004665 goto exit;
4666
4667exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004668 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004669 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004670 psa_destroy_key( base_handle );
4671 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004672 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004673}
4674/* END_CASE */
4675
Janos Follath42fd8882019-07-03 14:17:09 +01004676/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004677void derive_key_export( int alg_arg,
4678 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004679 data_t *input1,
4680 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004681 int bytes1_arg,
4682 int bytes2_arg )
4683{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004684 psa_key_handle_t base_handle = 0;
4685 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004686 psa_algorithm_t alg = alg_arg;
4687 size_t bytes1 = bytes1_arg;
4688 size_t bytes2 = bytes2_arg;
4689 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004690 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004691 uint8_t *output_buffer = NULL;
4692 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004693 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4694 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004695 size_t length;
4696
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004697 ASSERT_ALLOC( output_buffer, capacity );
4698 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004699 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004700
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004701 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4702 psa_set_key_algorithm( &base_attributes, alg );
4703 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004704 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4705 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004706
4707 /* Derive some material and output it. */
Janos Follath42fd8882019-07-03 14:17:09 +01004708 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4709 input1->x, input1->len,
4710 input2->x, input2->len, capacity ) )
4711 goto exit;
4712
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004713 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004714 output_buffer,
4715 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004716 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004717
4718 /* Derive the same output again, but this time store it in key objects. */
Janos Follath42fd8882019-07-03 14:17:09 +01004719 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4720 input1->x, input1->len,
4721 input2->x, input2->len, capacity ) )
4722 goto exit;
4723
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004724 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4725 psa_set_key_algorithm( &derived_attributes, 0 );
4726 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004727 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004728 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004729 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004730 PSA_ASSERT( psa_export_key( derived_handle,
4731 export_buffer, bytes1,
4732 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004733 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004734 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004735 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004736 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004737 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004738 PSA_ASSERT( psa_export_key( derived_handle,
4739 export_buffer + bytes1, bytes2,
4740 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004741 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004742
4743 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004744 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4745 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004746
4747exit:
4748 mbedtls_free( output_buffer );
4749 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004750 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004751 psa_destroy_key( base_handle );
4752 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004753 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004754}
4755/* END_CASE */
4756
4757/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004758void derive_key( int alg_arg,
4759 data_t *key_data, data_t *input1, data_t *input2,
4760 int type_arg, int bits_arg,
4761 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02004762{
4763 psa_key_handle_t base_handle = 0;
4764 psa_key_handle_t derived_handle = 0;
4765 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004766 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004767 size_t bits = bits_arg;
4768 psa_status_t expected_status = expected_status_arg;
4769 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4770 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4771 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4772
4773 PSA_ASSERT( psa_crypto_init( ) );
4774
4775 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4776 psa_set_key_algorithm( &base_attributes, alg );
4777 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4778 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4779 &base_handle ) );
4780
4781 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
4782 input1->x, input1->len,
4783 input2->x, input2->len, SIZE_MAX ) )
4784 goto exit;
4785
4786 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4787 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004788 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004789 psa_set_key_bits( &derived_attributes, bits );
4790 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
4791 &derived_handle ),
4792 expected_status );
4793
4794exit:
4795 psa_key_derivation_abort( &operation );
4796 psa_destroy_key( base_handle );
4797 psa_destroy_key( derived_handle );
4798 PSA_DONE( );
4799}
4800/* END_CASE */
4801
4802/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004803void key_agreement_setup( int alg_arg,
4804 int our_key_type_arg, data_t *our_key_data,
4805 data_t *peer_key_data,
4806 int expected_status_arg )
4807{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004808 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004809 psa_algorithm_t alg = alg_arg;
4810 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004811 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004812 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004813 psa_status_t expected_status = expected_status_arg;
4814 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004815
Gilles Peskine8817f612018-12-18 00:18:46 +01004816 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004817
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004818 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4819 psa_set_key_algorithm( &attributes, alg );
4820 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004821 PSA_ASSERT( psa_import_key( &attributes,
4822 our_key_data->x, our_key_data->len,
4823 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004824
Gilles Peskine77f40d82019-04-11 21:27:06 +02004825 /* The tests currently include inputs that should fail at either step.
4826 * Test cases that fail at the setup step should be changed to call
4827 * key_derivation_setup instead, and this function should be renamed
4828 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004829 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004830 if( status == PSA_SUCCESS )
4831 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004832 TEST_EQUAL( psa_key_derivation_key_agreement(
4833 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4834 our_key,
4835 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004836 expected_status );
4837 }
4838 else
4839 {
4840 TEST_ASSERT( status == expected_status );
4841 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004842
4843exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004844 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004845 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004846 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004847}
4848/* END_CASE */
4849
4850/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004851void raw_key_agreement( int alg_arg,
4852 int our_key_type_arg, data_t *our_key_data,
4853 data_t *peer_key_data,
4854 data_t *expected_output )
4855{
4856 psa_key_handle_t our_key = 0;
4857 psa_algorithm_t alg = alg_arg;
4858 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004859 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004860 unsigned char *output = NULL;
4861 size_t output_length = ~0;
4862
4863 ASSERT_ALLOC( output, expected_output->len );
4864 PSA_ASSERT( psa_crypto_init( ) );
4865
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004866 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4867 psa_set_key_algorithm( &attributes, alg );
4868 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004869 PSA_ASSERT( psa_import_key( &attributes,
4870 our_key_data->x, our_key_data->len,
4871 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004872
Gilles Peskinebe697d82019-05-16 18:00:41 +02004873 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4874 peer_key_data->x, peer_key_data->len,
4875 output, expected_output->len,
4876 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004877 ASSERT_COMPARE( output, output_length,
4878 expected_output->x, expected_output->len );
4879
4880exit:
4881 mbedtls_free( output );
4882 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004883 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004884}
4885/* END_CASE */
4886
4887/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004888void key_agreement_capacity( int alg_arg,
4889 int our_key_type_arg, data_t *our_key_data,
4890 data_t *peer_key_data,
4891 int expected_capacity_arg )
4892{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004893 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004894 psa_algorithm_t alg = alg_arg;
4895 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004896 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004897 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004898 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004899 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004900
Gilles Peskine8817f612018-12-18 00:18:46 +01004901 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004902
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004903 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4904 psa_set_key_algorithm( &attributes, alg );
4905 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004906 PSA_ASSERT( psa_import_key( &attributes,
4907 our_key_data->x, our_key_data->len,
4908 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004909
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004910 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004911 PSA_ASSERT( psa_key_derivation_key_agreement(
4912 &operation,
4913 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4914 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004915 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4916 {
4917 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004918 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004919 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004920 NULL, 0 ) );
4921 }
Gilles Peskine59685592018-09-18 12:11:34 +02004922
Gilles Peskinebf491972018-10-25 22:36:12 +02004923 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004924 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004925 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004926 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004927
Gilles Peskinebf491972018-10-25 22:36:12 +02004928 /* Test the actual capacity by reading the output. */
4929 while( actual_capacity > sizeof( output ) )
4930 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004931 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004932 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004933 actual_capacity -= sizeof( output );
4934 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004935 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004936 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004937 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004938 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004939
Gilles Peskine59685592018-09-18 12:11:34 +02004940exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004941 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004942 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004943 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004944}
4945/* END_CASE */
4946
4947/* BEGIN_CASE */
4948void key_agreement_output( int alg_arg,
4949 int our_key_type_arg, data_t *our_key_data,
4950 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004951 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004952{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004953 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004954 psa_algorithm_t alg = alg_arg;
4955 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004956 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004957 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004958 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004959
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004960 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4961 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004962
Gilles Peskine8817f612018-12-18 00:18:46 +01004963 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004964
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004965 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4966 psa_set_key_algorithm( &attributes, alg );
4967 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004968 PSA_ASSERT( psa_import_key( &attributes,
4969 our_key_data->x, our_key_data->len,
4970 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004971
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004972 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004973 PSA_ASSERT( psa_key_derivation_key_agreement(
4974 &operation,
4975 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4976 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004977 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4978 {
4979 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004980 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004981 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004982 NULL, 0 ) );
4983 }
Gilles Peskine59685592018-09-18 12:11:34 +02004984
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004985 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004986 actual_output,
4987 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004988 ASSERT_COMPARE( actual_output, expected_output1->len,
4989 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004990 if( expected_output2->len != 0 )
4991 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004992 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004993 actual_output,
4994 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004995 ASSERT_COMPARE( actual_output, expected_output2->len,
4996 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004997 }
Gilles Peskine59685592018-09-18 12:11:34 +02004998
4999exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005000 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005001 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005002 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005003 mbedtls_free( actual_output );
5004}
5005/* END_CASE */
5006
5007/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005008void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005009{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005010 size_t bytes = bytes_arg;
5011 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005012 unsigned char *output = NULL;
5013 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005014 size_t i;
5015 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005016
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005017 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5018 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005019 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005020
Gilles Peskine8817f612018-12-18 00:18:46 +01005021 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005022
Gilles Peskinea50d7392018-06-21 10:22:13 +02005023 /* Run several times, to ensure that every output byte will be
5024 * nonzero at least once with overwhelming probability
5025 * (2^(-8*number_of_runs)). */
5026 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005027 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005028 if( bytes != 0 )
5029 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005030 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005031
5032 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005033 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5034 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005035
5036 for( i = 0; i < bytes; i++ )
5037 {
5038 if( output[i] != 0 )
5039 ++changed[i];
5040 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005041 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005042
5043 /* Check that every byte was changed to nonzero at least once. This
5044 * validates that psa_generate_random is overwriting every byte of
5045 * the output buffer. */
5046 for( i = 0; i < bytes; i++ )
5047 {
5048 TEST_ASSERT( changed[i] != 0 );
5049 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005050
5051exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005052 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005053 mbedtls_free( output );
5054 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005055}
5056/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005057
5058/* BEGIN_CASE */
5059void generate_key( int type_arg,
5060 int bits_arg,
5061 int usage_arg,
5062 int alg_arg,
5063 int expected_status_arg )
5064{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005065 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005066 psa_key_type_t type = type_arg;
5067 psa_key_usage_t usage = usage_arg;
5068 size_t bits = bits_arg;
5069 psa_algorithm_t alg = alg_arg;
5070 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005071 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005072 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005073
Gilles Peskine8817f612018-12-18 00:18:46 +01005074 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005075
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005076 psa_set_key_usage_flags( &attributes, usage );
5077 psa_set_key_algorithm( &attributes, alg );
5078 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005079 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005080
5081 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005082 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005083 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005084 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005085
5086 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005087 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
5088 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5089 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005090
Gilles Peskine818ca122018-06-20 18:16:48 +02005091 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005092 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005093 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005094
5095exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005096 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005097 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005098 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005099}
5100/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005101
Gilles Peskinee56e8782019-04-26 17:34:02 +02005102/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5103void generate_key_rsa( int bits_arg,
5104 data_t *e_arg,
5105 int expected_status_arg )
5106{
5107 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005108 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005109 size_t bits = bits_arg;
5110 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5111 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5112 psa_status_t expected_status = expected_status_arg;
5113 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5114 uint8_t *exported = NULL;
5115 size_t exported_size =
5116 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5117 size_t exported_length = SIZE_MAX;
5118 uint8_t *e_read_buffer = NULL;
5119 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005120 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005121 size_t e_read_length = SIZE_MAX;
5122
5123 if( e_arg->len == 0 ||
5124 ( e_arg->len == 3 &&
5125 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5126 {
5127 is_default_public_exponent = 1;
5128 e_read_size = 0;
5129 }
5130 ASSERT_ALLOC( e_read_buffer, e_read_size );
5131 ASSERT_ALLOC( exported, exported_size );
5132
5133 PSA_ASSERT( psa_crypto_init( ) );
5134
5135 psa_set_key_usage_flags( &attributes, usage );
5136 psa_set_key_algorithm( &attributes, alg );
5137 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5138 e_arg->x, e_arg->len ) );
5139 psa_set_key_bits( &attributes, bits );
5140
5141 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005142 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005143 if( expected_status != PSA_SUCCESS )
5144 goto exit;
5145
5146 /* Test the key information */
5147 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5148 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5149 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5150 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5151 e_read_buffer, e_read_size,
5152 &e_read_length ) );
5153 if( is_default_public_exponent )
5154 TEST_EQUAL( e_read_length, 0 );
5155 else
5156 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5157
5158 /* Do something with the key according to its type and permitted usage. */
5159 if( ! exercise_key( handle, usage, alg ) )
5160 goto exit;
5161
5162 /* Export the key and check the public exponent. */
5163 PSA_ASSERT( psa_export_public_key( handle,
5164 exported, exported_size,
5165 &exported_length ) );
5166 {
5167 uint8_t *p = exported;
5168 uint8_t *end = exported + exported_length;
5169 size_t len;
5170 /* RSAPublicKey ::= SEQUENCE {
5171 * modulus INTEGER, -- n
5172 * publicExponent INTEGER } -- e
5173 */
5174 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005175 MBEDTLS_ASN1_SEQUENCE |
5176 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005177 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5178 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5179 MBEDTLS_ASN1_INTEGER ) );
5180 if( len >= 1 && p[0] == 0 )
5181 {
5182 ++p;
5183 --len;
5184 }
5185 if( e_arg->len == 0 )
5186 {
5187 TEST_EQUAL( len, 3 );
5188 TEST_EQUAL( p[0], 1 );
5189 TEST_EQUAL( p[1], 0 );
5190 TEST_EQUAL( p[2], 1 );
5191 }
5192 else
5193 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5194 }
5195
5196exit:
5197 psa_reset_key_attributes( &attributes );
5198 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005199 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005200 mbedtls_free( e_read_buffer );
5201 mbedtls_free( exported );
5202}
5203/* END_CASE */
5204
Darryl Greend49a4992018-06-18 17:27:26 +01005205/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005206void persistent_key_load_key_from_storage( data_t *data,
5207 int type_arg, int bits_arg,
5208 int usage_flags_arg, int alg_arg,
5209 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005210{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005211 psa_key_id_t key_id = 1;
5212 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005213 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005214 psa_key_handle_t base_key = 0;
5215 psa_key_type_t type = type_arg;
5216 size_t bits = bits_arg;
5217 psa_key_usage_t usage_flags = usage_flags_arg;
5218 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005219 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005220 unsigned char *first_export = NULL;
5221 unsigned char *second_export = NULL;
5222 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5223 size_t first_exported_length;
5224 size_t second_exported_length;
5225
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005226 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5227 {
5228 ASSERT_ALLOC( first_export, export_size );
5229 ASSERT_ALLOC( second_export, export_size );
5230 }
Darryl Greend49a4992018-06-18 17:27:26 +01005231
Gilles Peskine8817f612018-12-18 00:18:46 +01005232 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005233
Gilles Peskinec87af662019-05-15 16:12:22 +02005234 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005235 psa_set_key_usage_flags( &attributes, usage_flags );
5236 psa_set_key_algorithm( &attributes, alg );
5237 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005238 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005239
Darryl Green0c6575a2018-11-07 16:05:30 +00005240 switch( generation_method )
5241 {
5242 case IMPORT_KEY:
5243 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005244 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
5245 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005246 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005247
Darryl Green0c6575a2018-11-07 16:05:30 +00005248 case GENERATE_KEY:
5249 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005250 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005251 break;
5252
5253 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005254 {
5255 /* Create base key */
5256 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5257 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5258 psa_set_key_usage_flags( &base_attributes,
5259 PSA_KEY_USAGE_DERIVE );
5260 psa_set_key_algorithm( &base_attributes, derive_alg );
5261 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005262 PSA_ASSERT( psa_import_key( &base_attributes,
5263 data->x, data->len,
5264 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005265 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005266 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005267 PSA_ASSERT( psa_key_derivation_input_key(
5268 &operation,
5269 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005270 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005271 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005272 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005273 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5274 &operation,
5275 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005276 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005277 PSA_ASSERT( psa_destroy_key( base_key ) );
5278 base_key = 0;
5279 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005280 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005281 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005282 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005283
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005284 /* Export the key if permitted by the key policy. */
5285 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5286 {
5287 PSA_ASSERT( psa_export_key( handle,
5288 first_export, export_size,
5289 &first_exported_length ) );
5290 if( generation_method == IMPORT_KEY )
5291 ASSERT_COMPARE( data->x, data->len,
5292 first_export, first_exported_length );
5293 }
Darryl Greend49a4992018-06-18 17:27:26 +01005294
5295 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005296 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005297 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005298 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005299
Darryl Greend49a4992018-06-18 17:27:26 +01005300 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005301 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005302 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5303 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
5304 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5305 PSA_KEY_LIFETIME_PERSISTENT );
5306 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5307 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5308 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5309 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005310
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005311 /* Export the key again if permitted by the key policy. */
5312 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005313 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005314 PSA_ASSERT( psa_export_key( handle,
5315 second_export, export_size,
5316 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005317 ASSERT_COMPARE( first_export, first_exported_length,
5318 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005319 }
5320
5321 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005322 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005323 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005324
5325exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005326 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005327 mbedtls_free( first_export );
5328 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005329 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005330 psa_destroy_key( base_key );
5331 if( handle == 0 )
5332 {
5333 /* In case there was a test failure after creating the persistent key
5334 * but while it was not open, try to re-open the persistent key
5335 * to delete it. */
Gilles Peskine225010f2019-05-06 18:44:55 +02005336 psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005337 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005338 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005339 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005340}
5341/* END_CASE */