blob: cbe6616fdd7b51c0b26281e9245a8fdb7d5ce96b [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
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02009#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +020010#include "mbedtls/oid.h"
11
Gilles Peskinee59236f2018-01-27 23:32:46 +010012#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Jaeden Amerof24c7f82018-06-27 17:20:43 +010014/** An invalid export length that will never be set by psa_export_key(). */
15static const size_t INVALID_EXPORT_LENGTH = ~0U;
16
Gilles Peskinef426e0f2019-02-25 17:42:03 +010017/* A hash algorithm that is known to be supported.
18 *
19 * This is used in some smoke tests.
20 */
21#if defined(MBEDTLS_MD2_C)
22#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
23#elif defined(MBEDTLS_MD4_C)
24#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
25#elif defined(MBEDTLS_MD5_C)
26#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
27/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
28 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
29 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
30 * implausible anyway. */
31#elif defined(MBEDTLS_SHA1_C)
32#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
33#elif defined(MBEDTLS_SHA256_C)
34#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
35#elif defined(MBEDTLS_SHA512_C)
36#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
37#elif defined(MBEDTLS_SHA3_C)
38#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
39#else
40#undef KNOWN_SUPPORTED_HASH_ALG
41#endif
42
43/* A block cipher that is known to be supported.
44 *
45 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
46 */
47#if defined(MBEDTLS_AES_C)
48#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
49#elif defined(MBEDTLS_ARIA_C)
50#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
51#elif defined(MBEDTLS_CAMELLIA_C)
52#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
53#undef KNOWN_SUPPORTED_BLOCK_CIPHER
54#endif
55
56/* A MAC mode that is known to be supported.
57 *
58 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
59 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
60 *
61 * This is used in some smoke tests.
62 */
63#if defined(KNOWN_SUPPORTED_HASH_ALG)
64#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
65#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
66#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
67#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
68#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
69#else
70#undef KNOWN_SUPPORTED_MAC_ALG
71#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
72#endif
73
74/* A cipher algorithm and key type that are known to be supported.
75 *
76 * This is used in some smoke tests.
77 */
78#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
79#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
80#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
81#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
82#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
83#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
84#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
85#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
86#else
87#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
88#endif
89#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
90#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
91#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
92#elif defined(MBEDTLS_RC4_C)
93#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
94#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
95#else
96#undef KNOWN_SUPPORTED_CIPHER_ALG
97#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
98#endif
99
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200100/** Test if a buffer contains a constant byte value.
101 *
102 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200103 *
104 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200105 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200106 * \param size Size of the buffer in bytes.
107 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200108 * \return 1 if the buffer is all-bits-zero.
109 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200110 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200111static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200112{
113 size_t i;
114 for( i = 0; i < size; i++ )
115 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200116 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200117 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200118 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200119 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200120}
Gilles Peskine818ca122018-06-20 18:16:48 +0200121
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200122/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
123static int asn1_write_10x( unsigned char **p,
124 unsigned char *start,
125 size_t bits,
126 unsigned char x )
127{
128 int ret;
129 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200130 if( bits == 0 )
131 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
132 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200133 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300134 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200135 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
136 *p -= len;
137 ( *p )[len-1] = x;
138 if( bits % 8 == 0 )
139 ( *p )[1] |= 1;
140 else
141 ( *p )[0] |= 1 << ( bits % 8 );
142 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
143 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
144 MBEDTLS_ASN1_INTEGER ) );
145 return( len );
146}
147
148static int construct_fake_rsa_key( unsigned char *buffer,
149 size_t buffer_size,
150 unsigned char **p,
151 size_t bits,
152 int keypair )
153{
154 size_t half_bits = ( bits + 1 ) / 2;
155 int ret;
156 int len = 0;
157 /* Construct something that looks like a DER encoding of
158 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
159 * RSAPrivateKey ::= SEQUENCE {
160 * version Version,
161 * modulus INTEGER, -- n
162 * publicExponent INTEGER, -- e
163 * privateExponent INTEGER, -- d
164 * prime1 INTEGER, -- p
165 * prime2 INTEGER, -- q
166 * exponent1 INTEGER, -- d mod (p-1)
167 * exponent2 INTEGER, -- d mod (q-1)
168 * coefficient INTEGER, -- (inverse of q) mod p
169 * otherPrimeInfos OtherPrimeInfos OPTIONAL
170 * }
171 * Or, for a public key, the same structure with only
172 * version, modulus and publicExponent.
173 */
174 *p = buffer + buffer_size;
175 if( keypair )
176 {
177 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
178 asn1_write_10x( p, buffer, half_bits, 1 ) );
179 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
180 asn1_write_10x( p, buffer, half_bits, 1 ) );
181 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
182 asn1_write_10x( p, buffer, half_bits, 1 ) );
183 MBEDTLS_ASN1_CHK_ADD( len, /* q */
184 asn1_write_10x( p, buffer, half_bits, 1 ) );
185 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
186 asn1_write_10x( p, buffer, half_bits, 3 ) );
187 MBEDTLS_ASN1_CHK_ADD( len, /* d */
188 asn1_write_10x( p, buffer, bits, 1 ) );
189 }
190 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
191 asn1_write_10x( p, buffer, 17, 1 ) );
192 MBEDTLS_ASN1_CHK_ADD( len, /* n */
193 asn1_write_10x( p, buffer, bits, 1 ) );
194 if( keypair )
195 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
196 mbedtls_asn1_write_int( p, buffer, 0 ) );
197 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
198 {
199 const unsigned char tag =
200 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
201 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
202 }
203 return( len );
204}
205
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100206int exercise_mac_setup( psa_key_type_t key_type,
207 const unsigned char *key_bytes,
208 size_t key_length,
209 psa_algorithm_t alg,
210 psa_mac_operation_t *operation,
211 psa_status_t *status )
212{
213 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100215
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200216 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
217 psa_set_key_algorithm( &attributes, alg );
218 psa_set_key_type( &attributes, key_type );
219 PSA_ASSERT( psa_import_key( &attributes, &handle, key_bytes, key_length ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100220
221 *status = psa_mac_sign_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100222 /* Whether setup succeeded or failed, abort must succeed. */
223 PSA_ASSERT( psa_mac_abort( operation ) );
224 /* If setup failed, reproduce the failure, so that the caller can
225 * test the resulting state of the operation object. */
226 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100227 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100228 TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ),
229 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100230 }
231
232 psa_destroy_key( handle );
233 return( 1 );
234
235exit:
236 psa_destroy_key( handle );
237 return( 0 );
238}
239
240int exercise_cipher_setup( psa_key_type_t key_type,
241 const unsigned char *key_bytes,
242 size_t key_length,
243 psa_algorithm_t alg,
244 psa_cipher_operation_t *operation,
245 psa_status_t *status )
246{
247 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100249
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200250 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
251 psa_set_key_algorithm( &attributes, alg );
252 psa_set_key_type( &attributes, key_type );
253 PSA_ASSERT( psa_import_key( &attributes, &handle, key_bytes, key_length ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100254
255 *status = psa_cipher_encrypt_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100256 /* Whether setup succeeded or failed, abort must succeed. */
257 PSA_ASSERT( psa_cipher_abort( operation ) );
258 /* If setup failed, reproduce the failure, so that the caller can
259 * test the resulting state of the operation object. */
260 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100261 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100262 TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ),
263 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100264 }
265
266 psa_destroy_key( handle );
267 return( 1 );
268
269exit:
270 psa_destroy_key( handle );
271 return( 0 );
272}
273
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100274static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200275 psa_key_usage_t usage,
276 psa_algorithm_t alg )
277{
Jaeden Amero769ce272019-01-04 11:48:03 +0000278 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200279 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200280 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200281 size_t mac_length = sizeof( mac );
282
283 if( usage & PSA_KEY_USAGE_SIGN )
284 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100285 PSA_ASSERT( psa_mac_sign_setup( &operation,
286 handle, alg ) );
287 PSA_ASSERT( psa_mac_update( &operation,
288 input, sizeof( input ) ) );
289 PSA_ASSERT( psa_mac_sign_finish( &operation,
290 mac, sizeof( mac ),
291 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200292 }
293
294 if( usage & PSA_KEY_USAGE_VERIFY )
295 {
296 psa_status_t verify_status =
297 ( usage & PSA_KEY_USAGE_SIGN ?
298 PSA_SUCCESS :
299 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100300 PSA_ASSERT( psa_mac_verify_setup( &operation,
301 handle, alg ) );
302 PSA_ASSERT( psa_mac_update( &operation,
303 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100304 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
305 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200306 }
307
308 return( 1 );
309
310exit:
311 psa_mac_abort( &operation );
312 return( 0 );
313}
314
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100315static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200316 psa_key_usage_t usage,
317 psa_algorithm_t alg )
318{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000319 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200320 unsigned char iv[16] = {0};
321 size_t iv_length = sizeof( iv );
322 const unsigned char plaintext[16] = "Hello, world...";
323 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
324 size_t ciphertext_length = sizeof( ciphertext );
325 unsigned char decrypted[sizeof( ciphertext )];
326 size_t part_length;
327
328 if( usage & PSA_KEY_USAGE_ENCRYPT )
329 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100330 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
331 handle, alg ) );
332 PSA_ASSERT( psa_cipher_generate_iv( &operation,
333 iv, sizeof( iv ),
334 &iv_length ) );
335 PSA_ASSERT( psa_cipher_update( &operation,
336 plaintext, sizeof( plaintext ),
337 ciphertext, sizeof( ciphertext ),
338 &ciphertext_length ) );
339 PSA_ASSERT( psa_cipher_finish( &operation,
340 ciphertext + ciphertext_length,
341 sizeof( ciphertext ) - ciphertext_length,
342 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200343 ciphertext_length += part_length;
344 }
345
346 if( usage & PSA_KEY_USAGE_DECRYPT )
347 {
348 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200349 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200350 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
351 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
353 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
354 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
355 * have this macro yet. */
356 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE(
357 psa_get_key_type( &attributes ) );
358 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200359 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100360 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
361 handle, alg ) );
362 PSA_ASSERT( psa_cipher_set_iv( &operation,
363 iv, iv_length ) );
364 PSA_ASSERT( psa_cipher_update( &operation,
365 ciphertext, ciphertext_length,
366 decrypted, sizeof( decrypted ),
367 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200368 status = psa_cipher_finish( &operation,
369 decrypted + part_length,
370 sizeof( decrypted ) - part_length,
371 &part_length );
372 /* For a stream cipher, all inputs are valid. For a block cipher,
373 * if the input is some aribtrary data rather than an actual
374 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200375 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200376 TEST_ASSERT( status == PSA_SUCCESS ||
377 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200378 else
379 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200380 }
381
382 return( 1 );
383
384exit:
385 psa_cipher_abort( &operation );
386 return( 0 );
387}
388
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100389static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200390 psa_key_usage_t usage,
391 psa_algorithm_t alg )
392{
393 unsigned char nonce[16] = {0};
394 size_t nonce_length = sizeof( nonce );
395 unsigned char plaintext[16] = "Hello, world...";
396 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
397 size_t ciphertext_length = sizeof( ciphertext );
398 size_t plaintext_length = sizeof( ciphertext );
399
400 if( usage & PSA_KEY_USAGE_ENCRYPT )
401 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100402 PSA_ASSERT( psa_aead_encrypt( handle, alg,
403 nonce, nonce_length,
404 NULL, 0,
405 plaintext, sizeof( plaintext ),
406 ciphertext, sizeof( ciphertext ),
407 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200408 }
409
410 if( usage & PSA_KEY_USAGE_DECRYPT )
411 {
412 psa_status_t verify_status =
413 ( usage & PSA_KEY_USAGE_ENCRYPT ?
414 PSA_SUCCESS :
415 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100416 TEST_EQUAL( psa_aead_decrypt( handle, alg,
417 nonce, nonce_length,
418 NULL, 0,
419 ciphertext, ciphertext_length,
420 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100421 &plaintext_length ),
422 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200423 }
424
425 return( 1 );
426
427exit:
428 return( 0 );
429}
430
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100431static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200432 psa_key_usage_t usage,
433 psa_algorithm_t alg )
434{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200435 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
436 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200437 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200438 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100439 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
440
441 /* If the policy allows signing with any hash, just pick one. */
442 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
443 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100444#if defined(KNOWN_SUPPORTED_HASH_ALG)
445 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
446 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100447#else
448 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100449 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100450#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100451 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200452
453 if( usage & PSA_KEY_USAGE_SIGN )
454 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200455 /* Some algorithms require the payload to have the size of
456 * the hash encoded in the algorithm. Use this input size
457 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200458 if( hash_alg != 0 )
459 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100460 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
461 payload, payload_length,
462 signature, sizeof( signature ),
463 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200464 }
465
466 if( usage & PSA_KEY_USAGE_VERIFY )
467 {
468 psa_status_t verify_status =
469 ( usage & PSA_KEY_USAGE_SIGN ?
470 PSA_SUCCESS :
471 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100472 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
473 payload, payload_length,
474 signature, signature_length ),
475 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200476 }
477
478 return( 1 );
479
480exit:
481 return( 0 );
482}
483
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100484static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200485 psa_key_usage_t usage,
486 psa_algorithm_t alg )
487{
488 unsigned char plaintext[256] = "Hello, world...";
489 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
490 size_t ciphertext_length = sizeof( ciphertext );
491 size_t plaintext_length = 16;
492
493 if( usage & PSA_KEY_USAGE_ENCRYPT )
494 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100495 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
496 plaintext, plaintext_length,
497 NULL, 0,
498 ciphertext, sizeof( ciphertext ),
499 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200500 }
501
502 if( usage & PSA_KEY_USAGE_DECRYPT )
503 {
504 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100505 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200506 ciphertext, ciphertext_length,
507 NULL, 0,
508 plaintext, sizeof( plaintext ),
509 &plaintext_length );
510 TEST_ASSERT( status == PSA_SUCCESS ||
511 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
512 ( status == PSA_ERROR_INVALID_ARGUMENT ||
513 status == PSA_ERROR_INVALID_PADDING ) ) );
514 }
515
516 return( 1 );
517
518exit:
519 return( 0 );
520}
Gilles Peskine02b75072018-07-01 22:31:34 +0200521
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100522static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200523 psa_key_usage_t usage,
524 psa_algorithm_t alg )
525{
526 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
527 unsigned char label[16] = "This is a label.";
528 size_t label_length = sizeof( label );
529 unsigned char seed[16] = "abcdefghijklmnop";
530 size_t seed_length = sizeof( seed );
531 unsigned char output[1];
532
533 if( usage & PSA_KEY_USAGE_DERIVE )
534 {
Gilles Peskineb70a0fd2019-01-07 22:59:38 +0100535 if( PSA_ALG_IS_HKDF( alg ) )
536 {
537 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
538 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
539 PSA_KDF_STEP_SALT,
540 label,
541 label_length ) );
542 PSA_ASSERT( psa_key_derivation_input_key( &generator,
543 PSA_KDF_STEP_SECRET,
544 handle ) );
545 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
546 PSA_KDF_STEP_INFO,
547 seed,
548 seed_length ) );
549 }
550 else
551 {
552 // legacy
553 PSA_ASSERT( psa_key_derivation( &generator,
554 handle, alg,
555 label, label_length,
556 seed, seed_length,
557 sizeof( output ) ) );
558 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100559 PSA_ASSERT( psa_generator_read( &generator,
560 output,
561 sizeof( output ) ) );
562 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200563 }
564
565 return( 1 );
566
567exit:
568 return( 0 );
569}
570
Gilles Peskinec7998b72018-11-07 18:45:02 +0100571/* We need two keys to exercise key agreement. Exercise the
572 * private key against its own public key. */
573static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +0100574 psa_key_handle_t handle )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100575{
576 psa_key_type_t private_key_type;
577 psa_key_type_t public_key_type;
578 size_t key_bits;
579 uint8_t *public_key = NULL;
580 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200581 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinec7998b72018-11-07 18:45:02 +0100582 * psa_key_agreement fails. This isn't fully satisfactory, but it's
583 * good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200584 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200585 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100586
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200587 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
588 private_key_type = psa_get_key_type( &attributes );
589 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100590 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
591 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
592 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100593 PSA_ASSERT( psa_export_public_key( handle,
594 public_key, public_key_length,
595 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100596
Gilles Peskine969c5d62019-01-16 15:53:06 +0100597 status = psa_key_agreement( generator, PSA_KDF_STEP_SECRET, handle,
598 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100599exit:
600 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200601 psa_reset_key_attributes( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100602 return( status );
603}
604
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200605/* We need two keys to exercise key agreement. Exercise the
606 * private key against its own public key. */
607static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
608 psa_key_handle_t handle )
609{
610 psa_key_type_t private_key_type;
611 psa_key_type_t public_key_type;
612 size_t key_bits;
613 uint8_t *public_key = NULL;
614 size_t public_key_length;
615 uint8_t output[1024];
616 size_t output_length;
617 /* Return GENERIC_ERROR if something other than the final call to
618 * psa_key_agreement fails. This isn't fully satisfactory, but it's
619 * good enough: callers will report it as a failed test anyway. */
620 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200621 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200622
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200623 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
624 private_key_type = psa_get_key_type( &attributes );
625 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200626 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
627 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
628 ASSERT_ALLOC( public_key, public_key_length );
629 PSA_ASSERT( psa_export_public_key( handle,
630 public_key, public_key_length,
631 &public_key_length ) );
632
633 status = psa_key_agreement_raw_shared_secret(
634 alg, handle,
635 public_key, public_key_length,
636 output, sizeof( output ), &output_length );
637exit:
638 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200639 psa_reset_key_attributes( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200640 return( status );
641}
642
643static int exercise_raw_key_agreement_key( psa_key_handle_t handle,
644 psa_key_usage_t usage,
645 psa_algorithm_t alg )
646{
647 int ok = 0;
648
649 if( usage & PSA_KEY_USAGE_DERIVE )
650 {
651 /* We need two keys to exercise key agreement. Exercise the
652 * private key against its own public key. */
653 PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) );
654 }
655 ok = 1;
656
657exit:
658 return( ok );
659}
660
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100661static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200662 psa_key_usage_t usage,
663 psa_algorithm_t alg )
664{
665 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200666 unsigned char output[1];
667 int ok = 0;
668
669 if( usage & PSA_KEY_USAGE_DERIVE )
670 {
671 /* We need two keys to exercise key agreement. Exercise the
672 * private key against its own public key. */
Gilles Peskine969c5d62019-01-16 15:53:06 +0100673 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
674 PSA_ASSERT( key_agreement_with_self( &generator, handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100675 PSA_ASSERT( psa_generator_read( &generator,
676 output,
677 sizeof( output ) ) );
678 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200679 }
680 ok = 1;
681
682exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200683 return( ok );
684}
685
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200686static int is_oid_of_key_type( psa_key_type_t type,
687 const uint8_t *oid, size_t oid_length )
688{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200689 const uint8_t *expected_oid = NULL;
690 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200691#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200692 if( PSA_KEY_TYPE_IS_RSA( type ) )
693 {
694 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
695 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
696 }
697 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200698#endif /* MBEDTLS_RSA_C */
699#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200700 if( PSA_KEY_TYPE_IS_ECC( type ) )
701 {
702 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
703 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
704 }
705 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200706#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200707 {
708 char message[40];
709 mbedtls_snprintf( message, sizeof( message ),
710 "OID not known for key type=0x%08lx",
711 (unsigned long) type );
712 test_fail( message, __LINE__, __FILE__ );
713 return( 0 );
714 }
715
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200716 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200717 return( 1 );
718
719exit:
720 return( 0 );
721}
722
723static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
724 size_t min_bits, size_t max_bits,
725 int must_be_odd )
726{
727 size_t len;
728 size_t actual_bits;
729 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100730 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100731 MBEDTLS_ASN1_INTEGER ),
732 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200733 /* Tolerate a slight departure from DER encoding:
734 * - 0 may be represented by an empty string or a 1-byte string.
735 * - The sign bit may be used as a value bit. */
736 if( ( len == 1 && ( *p )[0] == 0 ) ||
737 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
738 {
739 ++( *p );
740 --len;
741 }
742 if( min_bits == 0 && len == 0 )
743 return( 1 );
744 msb = ( *p )[0];
745 TEST_ASSERT( msb != 0 );
746 actual_bits = 8 * ( len - 1 );
747 while( msb != 0 )
748 {
749 msb >>= 1;
750 ++actual_bits;
751 }
752 TEST_ASSERT( actual_bits >= min_bits );
753 TEST_ASSERT( actual_bits <= max_bits );
754 if( must_be_odd )
755 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
756 *p += len;
757 return( 1 );
758exit:
759 return( 0 );
760}
761
762static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
763 size_t *len,
764 unsigned char n, unsigned char tag )
765{
766 int ret;
767 ret = mbedtls_asn1_get_tag( p, end, len,
768 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
769 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
770 if( ret != 0 )
771 return( ret );
772 end = *p + *len;
773 ret = mbedtls_asn1_get_tag( p, end, len, tag );
774 if( ret != 0 )
775 return( ret );
776 if( *p + *len != end )
777 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
778 return( 0 );
779}
780
781static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
782 uint8_t *exported, size_t exported_length )
783{
784 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100785 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200786 else
787 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200788
789#if defined(MBEDTLS_DES_C)
790 if( type == PSA_KEY_TYPE_DES )
791 {
792 /* Check the parity bits. */
793 unsigned i;
794 for( i = 0; i < bits / 8; i++ )
795 {
796 unsigned bit_count = 0;
797 unsigned m;
798 for( m = 1; m <= 0x100; m <<= 1 )
799 {
800 if( exported[i] & m )
801 ++bit_count;
802 }
803 TEST_ASSERT( bit_count % 2 != 0 );
804 }
805 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200806 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200807#endif
808
809#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
810 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
811 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200812 uint8_t *p = exported;
813 uint8_t *end = exported + exported_length;
814 size_t len;
815 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200816 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200817 * modulus INTEGER, -- n
818 * publicExponent INTEGER, -- e
819 * privateExponent INTEGER, -- d
820 * prime1 INTEGER, -- p
821 * prime2 INTEGER, -- q
822 * exponent1 INTEGER, -- d mod (p-1)
823 * exponent2 INTEGER, -- d mod (q-1)
824 * coefficient INTEGER, -- (inverse of q) mod p
825 * }
826 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100827 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
828 MBEDTLS_ASN1_SEQUENCE |
829 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
830 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200831 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
832 goto exit;
833 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
834 goto exit;
835 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
836 goto exit;
837 /* Require d to be at least half the size of n. */
838 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
839 goto exit;
840 /* Require p and q to be at most half the size of n, rounded up. */
841 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
842 goto exit;
843 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
844 goto exit;
845 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
846 goto exit;
847 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
848 goto exit;
849 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
850 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100851 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100852 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200853 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200854#endif /* MBEDTLS_RSA_C */
855
856#if defined(MBEDTLS_ECP_C)
857 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
858 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100859 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100860 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100861 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200862 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200863#endif /* MBEDTLS_ECP_C */
864
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200865 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
866 {
867 uint8_t *p = exported;
868 uint8_t *end = exported + exported_length;
869 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200870#if defined(MBEDTLS_RSA_C)
871 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
872 {
873 /* RSAPublicKey ::= SEQUENCE {
874 * modulus INTEGER, -- n
875 * publicExponent INTEGER } -- e
876 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100877 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
878 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100879 MBEDTLS_ASN1_CONSTRUCTED ),
880 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100881 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200882 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
883 goto exit;
884 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
885 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100886 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200887 }
888 else
889#endif /* MBEDTLS_RSA_C */
890#if defined(MBEDTLS_ECP_C)
891 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
892 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000893 /* The representation of an ECC public key is:
894 * - The byte 0x04;
895 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
896 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
897 * - where m is the bit size associated with the curve.
Jaeden Amero6b196002019-01-10 10:23:21 +0000898 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100899 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
900 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200901 }
902 else
903#endif /* MBEDTLS_ECP_C */
904 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100905 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200906 mbedtls_snprintf( message, sizeof( message ),
907 "No sanity check for public key type=0x%08lx",
908 (unsigned long) type );
909 test_fail( message, __LINE__, __FILE__ );
910 return( 0 );
911 }
912 }
913 else
914
915 {
916 /* No sanity checks for other types */
917 }
918
919 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200920
921exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200922 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200923}
924
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100925static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200926 psa_key_usage_t usage )
927{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200928 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200929 uint8_t *exported = NULL;
930 size_t exported_size = 0;
931 size_t exported_length = 0;
932 int ok = 0;
933
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200934 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200935
936 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200937 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200938 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100939 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
940 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200941 ok = 1;
942 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +0200943 }
944
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200945 exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ),
946 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200947 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200948
Gilles Peskine8817f612018-12-18 00:18:46 +0100949 PSA_ASSERT( psa_export_key( handle,
950 exported, exported_size,
951 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200952 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
953 psa_get_key_bits( &attributes ),
954 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +0200955
956exit:
957 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200958 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +0200959 return( ok );
960}
961
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100962static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200963{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200964 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200965 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +0200966 uint8_t *exported = NULL;
967 size_t exported_size = 0;
968 size_t exported_length = 0;
969 int ok = 0;
970
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200971 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
972 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200973 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100974 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100975 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200976 return( 1 );
977 }
978
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200979 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(
980 psa_get_key_type( &attributes ) );
981 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type,
982 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200983 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200984
Gilles Peskine8817f612018-12-18 00:18:46 +0100985 PSA_ASSERT( psa_export_public_key( handle,
986 exported, exported_size,
987 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200988 ok = exported_key_sanity_check( public_type,
989 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +0200990 exported, exported_length );
991
992exit:
993 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200994 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +0200995 return( ok );
996}
997
Gilles Peskinec9516fb2019-02-05 20:32:06 +0100998/** Do smoke tests on a key.
999 *
1000 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1001 * sign/verify, or derivation) that is permitted according to \p usage.
1002 * \p usage and \p alg should correspond to the expected policy on the
1003 * key.
1004 *
1005 * Export the key if permitted by \p usage, and check that the output
1006 * looks sensible. If \p usage forbids export, check that
1007 * \p psa_export_key correctly rejects the attempt. If the key is
1008 * asymmetric, also check \p psa_export_public_key.
1009 *
1010 * If the key fails the tests, this function calls the test framework's
1011 * `test_fail` function and returns false. Otherwise this function returns
1012 * true. Therefore it should be used as follows:
1013 * ```
1014 * if( ! exercise_key( ... ) ) goto exit;
1015 * ```
1016 *
1017 * \param handle The key to exercise. It should be capable of performing
1018 * \p alg.
1019 * \param usage The usage flags to assume.
1020 * \param alg The algorithm to exercise.
1021 *
1022 * \retval 0 The key failed the smoke tests.
1023 * \retval 1 The key passed the smoke tests.
1024 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001025static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +02001026 psa_key_usage_t usage,
1027 psa_algorithm_t alg )
1028{
1029 int ok;
1030 if( alg == 0 )
1031 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1032 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001033 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001034 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001035 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001036 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001037 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001038 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001039 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001040 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001041 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001042 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001043 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001044 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
1045 ok = exercise_raw_key_agreement_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001046 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001047 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001048 else
1049 {
1050 char message[40];
1051 mbedtls_snprintf( message, sizeof( message ),
1052 "No code to exercise alg=0x%08lx",
1053 (unsigned long) alg );
1054 test_fail( message, __LINE__, __FILE__ );
1055 ok = 0;
1056 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001057
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001058 ok = ok && exercise_export_key( handle, usage );
1059 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +02001060
Gilles Peskine02b75072018-07-01 22:31:34 +02001061 return( ok );
1062}
1063
Gilles Peskine10df3412018-10-25 22:35:43 +02001064static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1065 psa_algorithm_t alg )
1066{
1067 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1068 {
1069 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1070 PSA_KEY_USAGE_VERIFY :
1071 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
1072 }
1073 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1074 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1075 {
1076 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1077 PSA_KEY_USAGE_ENCRYPT :
1078 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1079 }
1080 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1081 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1082 {
1083 return( PSA_KEY_USAGE_DERIVE );
1084 }
1085 else
1086 {
1087 return( 0 );
1088 }
1089
1090}
Darryl Green0c6575a2018-11-07 16:05:30 +00001091
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001092static int test_operations_on_invalid_handle( psa_key_handle_t handle )
1093{
1094 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1095 uint8_t buffer[1];
1096 size_t length;
1097 int ok = 0;
1098
Gilles Peskinec87af662019-05-15 16:12:22 +02001099 psa_set_key_id( &attributes, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001100 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1101 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1102 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
1103 TEST_EQUAL( psa_get_key_attributes( handle, &attributes ),
1104 PSA_ERROR_INVALID_HANDLE );
1105 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001106 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001107 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1108 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1109 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1110 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1111
1112 TEST_EQUAL( psa_export_key( handle,
1113 buffer, sizeof( buffer ), &length ),
1114 PSA_ERROR_INVALID_HANDLE );
1115 TEST_EQUAL( psa_export_public_key( handle,
1116 buffer, sizeof( buffer ), &length ),
1117 PSA_ERROR_INVALID_HANDLE );
1118
1119 TEST_EQUAL( psa_close_key( handle ), PSA_ERROR_INVALID_HANDLE );
1120 TEST_EQUAL( psa_destroy_key( handle ), PSA_ERROR_INVALID_HANDLE );
1121
1122 ok = 1;
1123
1124exit:
1125 psa_reset_key_attributes( &attributes );
1126 return( ok );
1127}
1128
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001129/* An overapproximation of the amount of storage needed for a key of the
1130 * given type and with the given content. The API doesn't make it easy
1131 * to find a good value for the size. The current implementation doesn't
1132 * care about the value anyway. */
1133#define KEY_BITS_FROM_DATA( type, data ) \
1134 ( data )->len
1135
Darryl Green0c6575a2018-11-07 16:05:30 +00001136typedef enum {
1137 IMPORT_KEY = 0,
1138 GENERATE_KEY = 1,
1139 DERIVE_KEY = 2
1140} generate_method;
1141
Gilles Peskinee59236f2018-01-27 23:32:46 +01001142/* END_HEADER */
1143
1144/* BEGIN_DEPENDENCIES
1145 * depends_on:MBEDTLS_PSA_CRYPTO_C
1146 * END_DEPENDENCIES
1147 */
1148
1149/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001150void static_checks( )
1151{
1152 size_t max_truncated_mac_size =
1153 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1154
1155 /* Check that the length for a truncated MAC always fits in the algorithm
1156 * encoding. The shifted mask is the maximum truncated value. The
1157 * untruncated algorithm may be one byte larger. */
1158 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
1159}
1160/* END_CASE */
1161
1162/* BEGIN_CASE */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001163void attributes_set_get( int id_arg, int lifetime_arg,
1164 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001165 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001166{
Gilles Peskine4747d192019-04-17 15:05:45 +02001167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001168 psa_key_id_t id = id_arg;
1169 psa_key_lifetime_t lifetime = lifetime_arg;
1170 psa_key_usage_t usage_flags = usage_flags_arg;
1171 psa_algorithm_t alg = alg_arg;
1172 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001173 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001174
1175 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1176 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1177 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1178 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1179 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001180 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001181
Gilles Peskinec87af662019-05-15 16:12:22 +02001182 psa_set_key_id( &attributes, id );
1183 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001184 psa_set_key_usage_flags( &attributes, usage_flags );
1185 psa_set_key_algorithm( &attributes, alg );
1186 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001187 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001188
1189 TEST_EQUAL( psa_get_key_id( &attributes ), id );
1190 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1191 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1192 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1193 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001194 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001195
1196 psa_reset_key_attributes( &attributes );
1197
1198 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1199 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1200 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1201 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1202 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001203 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001204}
1205/* END_CASE */
1206
1207/* BEGIN_CASE */
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001208void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg,
1209 int expected_id_arg, int expected_lifetime_arg )
1210{
1211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1212 psa_key_id_t id1 = id1_arg;
1213 psa_key_lifetime_t lifetime = lifetime_arg;
1214 psa_key_id_t id2 = id2_arg;
1215 psa_key_id_t expected_id = expected_id_arg;
1216 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1217
1218 if( id1_arg != -1 )
1219 psa_set_key_id( &attributes, id1 );
1220 if( lifetime_arg != -1 )
1221 psa_set_key_lifetime( &attributes, lifetime );
1222 if( id2_arg != -1 )
1223 psa_set_key_id( &attributes, id2 );
1224
1225 TEST_EQUAL( psa_get_key_id( &attributes ), expected_id );
1226 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1227}
1228/* END_CASE */
1229
1230/* BEGIN_CASE */
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001231void import( data_t *data, int type_arg,
1232 int attr_bits_arg,
1233 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001234{
1235 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1236 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001237 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001238 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001239 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001240 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001241 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001242
Gilles Peskine8817f612018-12-18 00:18:46 +01001243 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001244
Gilles Peskine4747d192019-04-17 15:05:45 +02001245 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001246 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine4747d192019-04-17 15:05:45 +02001247 status = psa_import_key( &attributes, &handle, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001248 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001249 if( status != PSA_SUCCESS )
1250 goto exit;
1251
1252 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1253 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001254 if( attr_bits != 0 )
1255 TEST_EQUAL( attr_bits, got_attributes.bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001256
1257 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001258 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001259
1260exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001261 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001262 psa_reset_key_attributes( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001263 mbedtls_psa_crypto_free( );
1264}
1265/* END_CASE */
1266
1267/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001268void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1269{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001270 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001271 size_t bits = bits_arg;
1272 psa_status_t expected_status = expected_status_arg;
1273 psa_status_t status;
1274 psa_key_type_t type =
1275 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
1276 size_t buffer_size = /* Slight overapproximations */
1277 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001278 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001279 unsigned char *p;
1280 int ret;
1281 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001282 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001283
Gilles Peskine8817f612018-12-18 00:18:46 +01001284 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001285 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001286
1287 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1288 bits, keypair ) ) >= 0 );
1289 length = ret;
1290
1291 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001292 psa_set_key_type( &attributes, type );
1293 status = psa_import_key( &attributes, &handle, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001294 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001295 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001296 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001297
1298exit:
1299 mbedtls_free( buffer );
1300 mbedtls_psa_crypto_free( );
1301}
1302/* END_CASE */
1303
1304/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001305void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001306 int type_arg,
1307 int alg_arg,
1308 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001309 int expected_bits,
1310 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001311 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001312 int canonical_input )
1313{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001314 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001315 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001316 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001317 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001318 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001319 unsigned char *exported = NULL;
1320 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001321 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001322 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001323 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001324 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001325 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001326
Moran Pekercb088e72018-07-17 17:36:59 +03001327 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001328 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001329 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001330 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001331 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001332
Gilles Peskine4747d192019-04-17 15:05:45 +02001333 psa_set_key_usage_flags( &attributes, usage_arg );
1334 psa_set_key_algorithm( &attributes, alg );
1335 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001336
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001337 /* Import the key */
Gilles Peskine4747d192019-04-17 15:05:45 +02001338 PSA_ASSERT( psa_import_key( &attributes, &handle, data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001339
1340 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001341 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1342 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1343 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001344
1345 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001346 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001347 exported, export_size,
1348 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001349 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001350
1351 /* The exported length must be set by psa_export_key() to a value between 0
1352 * and export_size. On errors, the exported length must be 0. */
1353 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1354 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1355 TEST_ASSERT( exported_length <= export_size );
1356
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001357 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001358 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001359 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001360 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001361 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001362 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001363 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001364
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001365 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001366 goto exit;
1367
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001368 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001369 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001370 else
1371 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001372 psa_key_handle_t handle2;
Gilles Peskine4747d192019-04-17 15:05:45 +02001373 PSA_ASSERT( psa_import_key( &attributes, &handle2,
1374 exported, exported_length ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001375 PSA_ASSERT( psa_export_key( handle2,
1376 reexported,
1377 export_size,
1378 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001379 ASSERT_COMPARE( exported, exported_length,
1380 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001381 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001382 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001383 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001384
1385destroy:
1386 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001387 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001388 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001389
1390exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001391 mbedtls_free( exported );
1392 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001393 psa_reset_key_attributes( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001394 mbedtls_psa_crypto_free( );
1395}
1396/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001397
Moran Pekerf709f4a2018-06-06 17:26:04 +03001398/* BEGIN_CASE */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001399void invalid_handle( int handle )
Moran Peker28a38e62018-11-07 16:18:24 +02001400{
Gilles Peskine8817f612018-12-18 00:18:46 +01001401 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001402 test_operations_on_invalid_handle( handle );
Moran Peker28a38e62018-11-07 16:18:24 +02001403
1404exit:
1405 mbedtls_psa_crypto_free( );
1406}
1407/* END_CASE */
1408
1409/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001410void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001411 int type_arg,
1412 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001413 int export_size_delta,
1414 int expected_export_status_arg,
1415 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001416{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001417 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001418 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001419 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001420 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001421 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001422 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001423 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001424 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001425 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001426
Gilles Peskine8817f612018-12-18 00:18:46 +01001427 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001428
Gilles Peskine4747d192019-04-17 15:05:45 +02001429 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1430 psa_set_key_algorithm( &attributes, alg );
1431 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001432
1433 /* Import the key */
Gilles Peskine4747d192019-04-17 15:05:45 +02001434 PSA_ASSERT( psa_import_key( &attributes, &handle, data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001435
Gilles Peskine49c25912018-10-29 15:15:31 +01001436 /* Export the public key */
1437 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001438 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001439 exported, export_size,
1440 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001441 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001442 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001443 {
1444 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1445 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001446 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1447 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001448 TEST_ASSERT( expected_public_key->len <=
1449 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001450 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1451 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001452 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001453
1454exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001455 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001456 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001457 psa_reset_key_attributes( &attributes );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001458 mbedtls_psa_crypto_free( );
1459}
1460/* END_CASE */
1461
Gilles Peskine20035e32018-02-03 22:44:14 +01001462/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001463void import_and_exercise_key( data_t *data,
1464 int type_arg,
1465 int bits_arg,
1466 int alg_arg )
1467{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001468 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001469 psa_key_type_t type = type_arg;
1470 size_t bits = bits_arg;
1471 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001472 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001473 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001474 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001475
Gilles Peskine8817f612018-12-18 00:18:46 +01001476 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001477
Gilles Peskine4747d192019-04-17 15:05:45 +02001478 psa_set_key_usage_flags( &attributes, usage );
1479 psa_set_key_algorithm( &attributes, alg );
1480 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001481
1482 /* Import the key */
Gilles Peskine4747d192019-04-17 15:05:45 +02001483 PSA_ASSERT( psa_import_key( &attributes, &handle, data->x, data->len ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001484
1485 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001486 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1487 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1488 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001489
1490 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001491 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001492 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001493
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001494 PSA_ASSERT( psa_destroy_key( handle ) );
1495 test_operations_on_invalid_handle( handle );
1496
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001497exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001498 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001499 psa_reset_key_attributes( &got_attributes );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001500 mbedtls_psa_crypto_free( );
1501}
1502/* END_CASE */
1503
1504/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001505void key_policy( int usage_arg, int alg_arg )
1506{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001507 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001508 psa_algorithm_t alg = alg_arg;
1509 psa_key_usage_t usage = usage_arg;
1510 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1511 unsigned char key[32] = {0};
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001512 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001513
1514 memset( key, 0x2a, sizeof( key ) );
1515
Gilles Peskine8817f612018-12-18 00:18:46 +01001516 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001517
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001518 psa_set_key_usage_flags( &attributes, usage );
1519 psa_set_key_algorithm( &attributes, alg );
1520 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001521
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001522 PSA_ASSERT( psa_import_key( &attributes, &handle, key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001523
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001524 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1525 TEST_EQUAL( psa_get_key_type( &attributes ), key_type );
1526 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage );
1527 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001528
1529exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001530 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001531 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001532 mbedtls_psa_crypto_free( );
1533}
1534/* END_CASE */
1535
1536/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001537void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001538{
1539 /* Test each valid way of initializing the object, except for `= {0}`, as
1540 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1541 * though it's OK by the C standard. We could test for this, but we'd need
1542 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001543 psa_key_attributes_t func = psa_key_attributes_init( );
1544 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1545 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001546
1547 memset( &zero, 0, sizeof( zero ) );
1548
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001549 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1550 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1551 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001552
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001553 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1554 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1555 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1556
1557 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1558 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1559 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1560
1561 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1562 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1563 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1564
1565 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1566 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1567 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001568}
1569/* END_CASE */
1570
1571/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001572void mac_key_policy( int policy_usage,
1573 int policy_alg,
1574 int key_type,
1575 data_t *key_data,
1576 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001577{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001578 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001579 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001580 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001581 psa_status_t status;
1582 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001583
Gilles Peskine8817f612018-12-18 00:18:46 +01001584 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001585
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001586 psa_set_key_usage_flags( &attributes, policy_usage );
1587 psa_set_key_algorithm( &attributes, policy_alg );
1588 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001589
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001590 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001591 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001592
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001593 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001594 if( policy_alg == exercise_alg &&
1595 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001596 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001597 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001598 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001599 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001600
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001601 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001602 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001603 if( policy_alg == exercise_alg &&
1604 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001605 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001606 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001607 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001608
1609exit:
1610 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001611 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001612 mbedtls_psa_crypto_free( );
1613}
1614/* END_CASE */
1615
1616/* BEGIN_CASE */
1617void cipher_key_policy( int policy_usage,
1618 int policy_alg,
1619 int key_type,
1620 data_t *key_data,
1621 int exercise_alg )
1622{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001623 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001624 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001625 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001626 psa_status_t status;
1627
Gilles Peskine8817f612018-12-18 00:18:46 +01001628 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001629
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001630 psa_set_key_usage_flags( &attributes, policy_usage );
1631 psa_set_key_algorithm( &attributes, policy_alg );
1632 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001633
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001634 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001635 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001636
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001637 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001638 if( policy_alg == exercise_alg &&
1639 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001640 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001641 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001642 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001643 psa_cipher_abort( &operation );
1644
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001645 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001646 if( policy_alg == exercise_alg &&
1647 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001648 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001649 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001650 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001651
1652exit:
1653 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001654 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001655 mbedtls_psa_crypto_free( );
1656}
1657/* END_CASE */
1658
1659/* BEGIN_CASE */
1660void aead_key_policy( int policy_usage,
1661 int policy_alg,
1662 int key_type,
1663 data_t *key_data,
1664 int nonce_length_arg,
1665 int tag_length_arg,
1666 int exercise_alg )
1667{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001668 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001669 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001670 psa_status_t status;
1671 unsigned char nonce[16] = {0};
1672 size_t nonce_length = nonce_length_arg;
1673 unsigned char tag[16];
1674 size_t tag_length = tag_length_arg;
1675 size_t output_length;
1676
1677 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1678 TEST_ASSERT( tag_length <= sizeof( tag ) );
1679
Gilles Peskine8817f612018-12-18 00:18:46 +01001680 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001681
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001682 psa_set_key_usage_flags( &attributes, policy_usage );
1683 psa_set_key_algorithm( &attributes, policy_alg );
1684 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001685
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001686 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001687 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001688
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001689 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001690 nonce, nonce_length,
1691 NULL, 0,
1692 NULL, 0,
1693 tag, tag_length,
1694 &output_length );
1695 if( policy_alg == exercise_alg &&
1696 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001697 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001698 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001699 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001700
1701 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001702 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001703 nonce, nonce_length,
1704 NULL, 0,
1705 tag, tag_length,
1706 NULL, 0,
1707 &output_length );
1708 if( policy_alg == exercise_alg &&
1709 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001710 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001711 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001712 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001713
1714exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001715 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001716 mbedtls_psa_crypto_free( );
1717}
1718/* END_CASE */
1719
1720/* BEGIN_CASE */
1721void asymmetric_encryption_key_policy( int policy_usage,
1722 int policy_alg,
1723 int key_type,
1724 data_t *key_data,
1725 int exercise_alg )
1726{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001727 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001728 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001729 psa_status_t status;
1730 size_t key_bits;
1731 size_t buffer_length;
1732 unsigned char *buffer = NULL;
1733 size_t output_length;
1734
Gilles Peskine8817f612018-12-18 00:18:46 +01001735 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001736
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001737 psa_set_key_usage_flags( &attributes, policy_usage );
1738 psa_set_key_algorithm( &attributes, policy_alg );
1739 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001740
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001741 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001742 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001743
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001744 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1745 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001746 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1747 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001748 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001749
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001750 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001751 NULL, 0,
1752 NULL, 0,
1753 buffer, buffer_length,
1754 &output_length );
1755 if( policy_alg == exercise_alg &&
1756 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001757 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001758 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001759 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001760
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001761 if( buffer_length != 0 )
1762 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001763 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001764 buffer, buffer_length,
1765 NULL, 0,
1766 buffer, buffer_length,
1767 &output_length );
1768 if( policy_alg == exercise_alg &&
1769 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001770 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001771 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001772 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001773
1774exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001775 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001776 psa_reset_key_attributes( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001777 mbedtls_psa_crypto_free( );
1778 mbedtls_free( buffer );
1779}
1780/* END_CASE */
1781
1782/* BEGIN_CASE */
1783void asymmetric_signature_key_policy( int policy_usage,
1784 int policy_alg,
1785 int key_type,
1786 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001787 int exercise_alg,
1788 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001789{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001790 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001791 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001792 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001793 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1794 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1795 * compatible with the policy and `payload_length_arg` is supposed to be
1796 * a valid input length to sign. If `payload_length_arg <= 0`,
1797 * `exercise_alg` is supposed to be forbidden by the policy. */
1798 int compatible_alg = payload_length_arg > 0;
1799 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001800 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1801 size_t signature_length;
1802
Gilles Peskine8817f612018-12-18 00:18:46 +01001803 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001804
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001805 psa_set_key_usage_flags( &attributes, policy_usage );
1806 psa_set_key_algorithm( &attributes, policy_alg );
1807 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001808
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001809 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001810 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001811
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001812 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001813 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001814 signature, sizeof( signature ),
1815 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001816 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001817 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001818 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001819 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001820
1821 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001822 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001823 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001824 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001825 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001826 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001827 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001828 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001829
1830exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001831 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001832 mbedtls_psa_crypto_free( );
1833}
1834/* END_CASE */
1835
1836/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001837void derive_key_policy( int policy_usage,
1838 int policy_alg,
1839 int key_type,
1840 data_t *key_data,
1841 int exercise_alg )
1842{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001843 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001844 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001845 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1846 psa_status_t status;
1847
Gilles Peskine8817f612018-12-18 00:18:46 +01001848 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001849
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001850 psa_set_key_usage_flags( &attributes, policy_usage );
1851 psa_set_key_algorithm( &attributes, policy_alg );
1852 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001853
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001854 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001855 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001856
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001857 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001858 exercise_alg,
1859 NULL, 0,
1860 NULL, 0,
1861 1 );
1862 if( policy_alg == exercise_alg &&
1863 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001864 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001865 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001866 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001867
1868exit:
1869 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001870 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001871 mbedtls_psa_crypto_free( );
1872}
1873/* END_CASE */
1874
1875/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001876void agreement_key_policy( int policy_usage,
1877 int policy_alg,
1878 int key_type_arg,
1879 data_t *key_data,
1880 int exercise_alg )
1881{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001882 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001883 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001884 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001885 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1886 psa_status_t status;
1887
Gilles Peskine8817f612018-12-18 00:18:46 +01001888 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001889
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001890 psa_set_key_usage_flags( &attributes, policy_usage );
1891 psa_set_key_algorithm( &attributes, policy_alg );
1892 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001893
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001894 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01001895 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001896
Gilles Peskine969c5d62019-01-16 15:53:06 +01001897 PSA_ASSERT( psa_key_derivation_setup( &generator, exercise_alg ) );
1898 status = key_agreement_with_self( &generator, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001899
Gilles Peskine01d718c2018-09-18 12:01:02 +02001900 if( policy_alg == exercise_alg &&
1901 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001902 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001903 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001904 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001905
1906exit:
1907 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001908 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001909 mbedtls_psa_crypto_free( );
1910}
1911/* END_CASE */
1912
1913/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001914void raw_agreement_key_policy( int policy_usage,
1915 int policy_alg,
1916 int key_type_arg,
1917 data_t *key_data,
1918 int exercise_alg )
1919{
1920 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001921 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001922 psa_key_type_t key_type = key_type_arg;
1923 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1924 psa_status_t status;
1925
1926 PSA_ASSERT( psa_crypto_init( ) );
1927
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001928 psa_set_key_usage_flags( &attributes, policy_usage );
1929 psa_set_key_algorithm( &attributes, policy_alg );
1930 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001931
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001932 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001933 key_data->x, key_data->len ) );
1934
1935 status = raw_key_agreement_with_self( exercise_alg, handle );
1936
1937 if( policy_alg == exercise_alg &&
1938 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1939 PSA_ASSERT( status );
1940 else
1941 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1942
1943exit:
1944 psa_generator_abort( &generator );
1945 psa_destroy_key( handle );
1946 mbedtls_psa_crypto_free( );
1947}
1948/* END_CASE */
1949
1950/* BEGIN_CASE */
Gilles Peskine4a644642019-05-03 17:14:08 +02001951void copy_success( int source_usage_arg, int source_alg_arg,
1952 int type_arg, data_t *material,
1953 int copy_attributes,
1954 int target_usage_arg, int target_alg_arg,
1955 int expected_usage_arg, int expected_alg_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001956{
Gilles Peskineca25db92019-04-19 11:43:08 +02001957 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1958 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001959 psa_key_usage_t expected_usage = expected_usage_arg;
1960 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02001961 psa_key_handle_t source_handle = 0;
1962 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001963 uint8_t *export_buffer = NULL;
1964
Gilles Peskine57ab7212019-01-28 13:03:09 +01001965 PSA_ASSERT( psa_crypto_init( ) );
1966
Gilles Peskineca25db92019-04-19 11:43:08 +02001967 /* Prepare the source key. */
1968 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1969 psa_set_key_algorithm( &source_attributes, source_alg_arg );
1970 psa_set_key_type( &source_attributes, type_arg );
1971 PSA_ASSERT( psa_import_key( &source_attributes, &source_handle,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001972 material->x, material->len ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001973 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001974
Gilles Peskineca25db92019-04-19 11:43:08 +02001975 /* Prepare the target attributes. */
1976 if( copy_attributes )
1977 target_attributes = source_attributes;
1978 if( target_usage_arg != -1 )
1979 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1980 if( target_alg_arg != -1 )
1981 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001982
1983 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02001984 PSA_ASSERT( psa_copy_key( source_handle,
1985 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001986
1987 /* Destroy the source to ensure that this doesn't affect the target. */
1988 PSA_ASSERT( psa_destroy_key( source_handle ) );
1989
1990 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02001991 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
1992 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1993 psa_get_key_type( &target_attributes ) );
1994 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1995 psa_get_key_bits( &target_attributes ) );
1996 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1997 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001998 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1999 {
2000 size_t length;
2001 ASSERT_ALLOC( export_buffer, material->len );
2002 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2003 material->len, &length ) );
2004 ASSERT_COMPARE( material->x, material->len,
2005 export_buffer, length );
2006 }
2007 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2008 goto exit;
2009
2010 PSA_ASSERT( psa_close_key( target_handle ) );
2011
2012exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002013 psa_reset_key_attributes( &source_attributes );
2014 psa_reset_key_attributes( &target_attributes );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002015 mbedtls_psa_crypto_free( );
2016 mbedtls_free( export_buffer );
2017}
2018/* END_CASE */
2019
2020/* BEGIN_CASE */
Gilles Peskine4a644642019-05-03 17:14:08 +02002021void copy_fail( int source_usage_arg, int source_alg_arg,
2022 int type_arg, data_t *material,
2023 int target_type_arg, int target_bits_arg,
2024 int target_usage_arg, int target_alg_arg,
2025 int expected_status_arg )
2026{
2027 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2028 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2029 psa_key_handle_t source_handle = 0;
2030 psa_key_handle_t target_handle = 0;
2031
2032 PSA_ASSERT( psa_crypto_init( ) );
2033
2034 /* Prepare the source key. */
2035 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2036 psa_set_key_algorithm( &source_attributes, source_alg_arg );
2037 psa_set_key_type( &source_attributes, type_arg );
2038 PSA_ASSERT( psa_import_key( &source_attributes, &source_handle,
2039 material->x, material->len ) );
2040
2041 /* Prepare the target attributes. */
2042 psa_set_key_type( &target_attributes, target_type_arg );
2043 psa_set_key_bits( &target_attributes, target_bits_arg );
2044 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2045 psa_set_key_algorithm( &target_attributes, target_alg_arg );
2046
2047 /* Try to copy the key. */
2048 TEST_EQUAL( psa_copy_key( source_handle,
2049 &target_attributes, &target_handle ),
2050 expected_status_arg );
2051exit:
2052 psa_reset_key_attributes( &source_attributes );
2053 psa_reset_key_attributes( &target_attributes );
2054 mbedtls_psa_crypto_free( );
2055}
2056/* END_CASE */
2057
2058/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002059void hash_operation_init( )
2060{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002061 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002062 /* Test each valid way of initializing the object, except for `= {0}`, as
2063 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2064 * though it's OK by the C standard. We could test for this, but we'd need
2065 * to supress the Clang warning for the test. */
2066 psa_hash_operation_t func = psa_hash_operation_init( );
2067 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2068 psa_hash_operation_t zero;
2069
2070 memset( &zero, 0, sizeof( zero ) );
2071
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002072 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002073 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2074 PSA_ERROR_BAD_STATE );
2075 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2076 PSA_ERROR_BAD_STATE );
2077 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2078 PSA_ERROR_BAD_STATE );
2079
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002080 /* A default hash operation should be abortable without error. */
2081 PSA_ASSERT( psa_hash_abort( &func ) );
2082 PSA_ASSERT( psa_hash_abort( &init ) );
2083 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002084}
2085/* END_CASE */
2086
2087/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002088void hash_setup( int alg_arg,
2089 int expected_status_arg )
2090{
2091 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002092 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002093 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002094 psa_status_t status;
2095
Gilles Peskine8817f612018-12-18 00:18:46 +01002096 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002097
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002098 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002099 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002100
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002101 /* Whether setup succeeded or failed, abort must succeed. */
2102 PSA_ASSERT( psa_hash_abort( &operation ) );
2103
2104 /* If setup failed, reproduce the failure, so as to
2105 * test the resulting state of the operation object. */
2106 if( status != PSA_SUCCESS )
2107 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2108
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002109 /* Now the operation object should be reusable. */
2110#if defined(KNOWN_SUPPORTED_HASH_ALG)
2111 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2112 PSA_ASSERT( psa_hash_abort( &operation ) );
2113#endif
2114
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002115exit:
2116 mbedtls_psa_crypto_free( );
2117}
2118/* END_CASE */
2119
2120/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002121void hash_bad_order( )
2122{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002123 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002124 unsigned char input[] = "";
2125 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002126 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002127 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2128 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2129 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002130 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002131 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002132 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002133
Gilles Peskine8817f612018-12-18 00:18:46 +01002134 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002135
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002136 /* Call setup twice in a row. */
2137 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2138 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2139 PSA_ERROR_BAD_STATE );
2140 PSA_ASSERT( psa_hash_abort( &operation ) );
2141
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002142 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002143 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002144 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002145 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002146
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002147 /* Call update after finish. */
2148 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2149 PSA_ASSERT( psa_hash_finish( &operation,
2150 hash, sizeof( hash ), &hash_len ) );
2151 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002152 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002153 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002154
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002155 /* Call verify without calling setup beforehand. */
2156 TEST_EQUAL( psa_hash_verify( &operation,
2157 valid_hash, sizeof( valid_hash ) ),
2158 PSA_ERROR_BAD_STATE );
2159 PSA_ASSERT( psa_hash_abort( &operation ) );
2160
2161 /* Call verify after finish. */
2162 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2163 PSA_ASSERT( psa_hash_finish( &operation,
2164 hash, sizeof( hash ), &hash_len ) );
2165 TEST_EQUAL( psa_hash_verify( &operation,
2166 valid_hash, sizeof( valid_hash ) ),
2167 PSA_ERROR_BAD_STATE );
2168 PSA_ASSERT( psa_hash_abort( &operation ) );
2169
2170 /* Call verify twice in a row. */
2171 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2172 PSA_ASSERT( psa_hash_verify( &operation,
2173 valid_hash, sizeof( valid_hash ) ) );
2174 TEST_EQUAL( psa_hash_verify( &operation,
2175 valid_hash, sizeof( valid_hash ) ),
2176 PSA_ERROR_BAD_STATE );
2177 PSA_ASSERT( psa_hash_abort( &operation ) );
2178
2179 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002180 TEST_EQUAL( psa_hash_finish( &operation,
2181 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002182 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002183 PSA_ASSERT( psa_hash_abort( &operation ) );
2184
2185 /* Call finish twice in a row. */
2186 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2187 PSA_ASSERT( psa_hash_finish( &operation,
2188 hash, sizeof( hash ), &hash_len ) );
2189 TEST_EQUAL( psa_hash_finish( &operation,
2190 hash, sizeof( hash ), &hash_len ),
2191 PSA_ERROR_BAD_STATE );
2192 PSA_ASSERT( psa_hash_abort( &operation ) );
2193
2194 /* Call finish after calling verify. */
2195 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2196 PSA_ASSERT( psa_hash_verify( &operation,
2197 valid_hash, sizeof( valid_hash ) ) );
2198 TEST_EQUAL( psa_hash_finish( &operation,
2199 hash, sizeof( hash ), &hash_len ),
2200 PSA_ERROR_BAD_STATE );
2201 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002202
2203exit:
2204 mbedtls_psa_crypto_free( );
2205}
2206/* END_CASE */
2207
itayzafrir27e69452018-11-01 14:26:34 +02002208/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2209void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002210{
2211 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002212 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2213 * appended to it */
2214 unsigned char hash[] = {
2215 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2216 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2217 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002218 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002219 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002220
Gilles Peskine8817f612018-12-18 00:18:46 +01002221 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002222
itayzafrir27e69452018-11-01 14:26:34 +02002223 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002224 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002225 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002226 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002227
itayzafrir27e69452018-11-01 14:26:34 +02002228 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002229 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002230 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002231 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002232
itayzafrir27e69452018-11-01 14:26:34 +02002233 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002234 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002235 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002236 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002237
itayzafrirec93d302018-10-18 18:01:10 +03002238exit:
2239 mbedtls_psa_crypto_free( );
2240}
2241/* END_CASE */
2242
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002243/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2244void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002245{
2246 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002247 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002248 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002249 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002250 size_t hash_len;
2251
Gilles Peskine8817f612018-12-18 00:18:46 +01002252 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002253
itayzafrir58028322018-10-25 10:22:01 +03002254 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002255 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002256 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002257 hash, expected_size - 1, &hash_len ),
2258 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002259
2260exit:
2261 mbedtls_psa_crypto_free( );
2262}
2263/* END_CASE */
2264
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002265/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2266void hash_clone_source_state( )
2267{
2268 psa_algorithm_t alg = PSA_ALG_SHA_256;
2269 unsigned char hash[PSA_HASH_MAX_SIZE];
2270 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2271 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2272 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2273 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2274 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2275 size_t hash_len;
2276
2277 PSA_ASSERT( psa_crypto_init( ) );
2278 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2279
2280 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2281 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2282 PSA_ASSERT( psa_hash_finish( &op_finished,
2283 hash, sizeof( hash ), &hash_len ) );
2284 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2285 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2286
2287 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2288 PSA_ERROR_BAD_STATE );
2289
2290 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2291 PSA_ASSERT( psa_hash_finish( &op_init,
2292 hash, sizeof( hash ), &hash_len ) );
2293 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2294 PSA_ASSERT( psa_hash_finish( &op_finished,
2295 hash, sizeof( hash ), &hash_len ) );
2296 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2297 PSA_ASSERT( psa_hash_finish( &op_aborted,
2298 hash, sizeof( hash ), &hash_len ) );
2299
2300exit:
2301 psa_hash_abort( &op_source );
2302 psa_hash_abort( &op_init );
2303 psa_hash_abort( &op_setup );
2304 psa_hash_abort( &op_finished );
2305 psa_hash_abort( &op_aborted );
2306 mbedtls_psa_crypto_free( );
2307}
2308/* END_CASE */
2309
2310/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2311void hash_clone_target_state( )
2312{
2313 psa_algorithm_t alg = PSA_ALG_SHA_256;
2314 unsigned char hash[PSA_HASH_MAX_SIZE];
2315 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2316 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2317 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2318 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2319 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2320 size_t hash_len;
2321
2322 PSA_ASSERT( psa_crypto_init( ) );
2323
2324 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2325 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2326 PSA_ASSERT( psa_hash_finish( &op_finished,
2327 hash, sizeof( hash ), &hash_len ) );
2328 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2329 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2330
2331 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2332 PSA_ASSERT( psa_hash_finish( &op_target,
2333 hash, sizeof( hash ), &hash_len ) );
2334
2335 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2336 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2337 PSA_ERROR_BAD_STATE );
2338 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2339 PSA_ERROR_BAD_STATE );
2340
2341exit:
2342 psa_hash_abort( &op_target );
2343 psa_hash_abort( &op_init );
2344 psa_hash_abort( &op_setup );
2345 psa_hash_abort( &op_finished );
2346 psa_hash_abort( &op_aborted );
2347 mbedtls_psa_crypto_free( );
2348}
2349/* END_CASE */
2350
itayzafrir58028322018-10-25 10:22:01 +03002351/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002352void mac_operation_init( )
2353{
Jaeden Amero252ef282019-02-15 14:05:35 +00002354 const uint8_t input[1] = { 0 };
2355
Jaeden Amero769ce272019-01-04 11:48:03 +00002356 /* Test each valid way of initializing the object, except for `= {0}`, as
2357 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2358 * though it's OK by the C standard. We could test for this, but we'd need
2359 * to supress the Clang warning for the test. */
2360 psa_mac_operation_t func = psa_mac_operation_init( );
2361 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2362 psa_mac_operation_t zero;
2363
2364 memset( &zero, 0, sizeof( zero ) );
2365
Jaeden Amero252ef282019-02-15 14:05:35 +00002366 /* A freshly-initialized MAC operation should not be usable. */
2367 TEST_EQUAL( psa_mac_update( &func,
2368 input, sizeof( input ) ),
2369 PSA_ERROR_BAD_STATE );
2370 TEST_EQUAL( psa_mac_update( &init,
2371 input, sizeof( input ) ),
2372 PSA_ERROR_BAD_STATE );
2373 TEST_EQUAL( psa_mac_update( &zero,
2374 input, sizeof( input ) ),
2375 PSA_ERROR_BAD_STATE );
2376
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002377 /* A default MAC operation should be abortable without error. */
2378 PSA_ASSERT( psa_mac_abort( &func ) );
2379 PSA_ASSERT( psa_mac_abort( &init ) );
2380 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002381}
2382/* END_CASE */
2383
2384/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002385void mac_setup( int key_type_arg,
2386 data_t *key,
2387 int alg_arg,
2388 int expected_status_arg )
2389{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002390 psa_key_type_t key_type = key_type_arg;
2391 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002392 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002393 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002394 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2395#if defined(KNOWN_SUPPORTED_MAC_ALG)
2396 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2397#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002398
Gilles Peskine8817f612018-12-18 00:18:46 +01002399 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002400
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002401 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2402 &operation, &status ) )
2403 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002404 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002405
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002406 /* The operation object should be reusable. */
2407#if defined(KNOWN_SUPPORTED_MAC_ALG)
2408 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2409 smoke_test_key_data,
2410 sizeof( smoke_test_key_data ),
2411 KNOWN_SUPPORTED_MAC_ALG,
2412 &operation, &status ) )
2413 goto exit;
2414 TEST_EQUAL( status, PSA_SUCCESS );
2415#endif
2416
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002417exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002418 mbedtls_psa_crypto_free( );
2419}
2420/* END_CASE */
2421
2422/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002423void mac_bad_order( )
2424{
2425 psa_key_handle_t handle = 0;
2426 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2427 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2428 const uint8_t key[] = {
2429 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2430 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2431 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002432 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002433 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2434 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2435 size_t sign_mac_length = 0;
2436 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2437 const uint8_t verify_mac[] = {
2438 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2439 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2440 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2441
2442 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002443 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
2444 psa_set_key_algorithm( &attributes, alg );
2445 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002446
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002447 PSA_ASSERT( psa_import_key( &attributes, &handle,
Jaeden Amero252ef282019-02-15 14:05:35 +00002448 key, sizeof(key) ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002449
Jaeden Amero252ef282019-02-15 14:05:35 +00002450 /* Call update without calling setup beforehand. */
2451 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2452 PSA_ERROR_BAD_STATE );
2453 PSA_ASSERT( psa_mac_abort( &operation ) );
2454
2455 /* Call sign finish without calling setup beforehand. */
2456 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2457 &sign_mac_length),
2458 PSA_ERROR_BAD_STATE );
2459 PSA_ASSERT( psa_mac_abort( &operation ) );
2460
2461 /* Call verify finish without calling setup beforehand. */
2462 TEST_EQUAL( psa_mac_verify_finish( &operation,
2463 verify_mac, sizeof( verify_mac ) ),
2464 PSA_ERROR_BAD_STATE );
2465 PSA_ASSERT( psa_mac_abort( &operation ) );
2466
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002467 /* Call setup twice in a row. */
2468 PSA_ASSERT( psa_mac_sign_setup( &operation,
2469 handle, alg ) );
2470 TEST_EQUAL( psa_mac_sign_setup( &operation,
2471 handle, alg ),
2472 PSA_ERROR_BAD_STATE );
2473 PSA_ASSERT( psa_mac_abort( &operation ) );
2474
Jaeden Amero252ef282019-02-15 14:05:35 +00002475 /* Call update after sign finish. */
2476 PSA_ASSERT( psa_mac_sign_setup( &operation,
2477 handle, alg ) );
2478 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2479 PSA_ASSERT( psa_mac_sign_finish( &operation,
2480 sign_mac, sizeof( sign_mac ),
2481 &sign_mac_length ) );
2482 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2483 PSA_ERROR_BAD_STATE );
2484 PSA_ASSERT( psa_mac_abort( &operation ) );
2485
2486 /* Call update after verify finish. */
2487 PSA_ASSERT( psa_mac_verify_setup( &operation,
2488 handle, alg ) );
2489 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2490 PSA_ASSERT( psa_mac_verify_finish( &operation,
2491 verify_mac, sizeof( verify_mac ) ) );
2492 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2493 PSA_ERROR_BAD_STATE );
2494 PSA_ASSERT( psa_mac_abort( &operation ) );
2495
2496 /* Call sign finish twice in a row. */
2497 PSA_ASSERT( psa_mac_sign_setup( &operation,
2498 handle, alg ) );
2499 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2500 PSA_ASSERT( psa_mac_sign_finish( &operation,
2501 sign_mac, sizeof( sign_mac ),
2502 &sign_mac_length ) );
2503 TEST_EQUAL( psa_mac_sign_finish( &operation,
2504 sign_mac, sizeof( sign_mac ),
2505 &sign_mac_length ),
2506 PSA_ERROR_BAD_STATE );
2507 PSA_ASSERT( psa_mac_abort( &operation ) );
2508
2509 /* Call verify finish twice in a row. */
2510 PSA_ASSERT( psa_mac_verify_setup( &operation,
2511 handle, alg ) );
2512 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2513 PSA_ASSERT( psa_mac_verify_finish( &operation,
2514 verify_mac, sizeof( verify_mac ) ) );
2515 TEST_EQUAL( psa_mac_verify_finish( &operation,
2516 verify_mac, sizeof( verify_mac ) ),
2517 PSA_ERROR_BAD_STATE );
2518 PSA_ASSERT( psa_mac_abort( &operation ) );
2519
2520 /* Setup sign but try verify. */
2521 PSA_ASSERT( psa_mac_sign_setup( &operation,
2522 handle, alg ) );
2523 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2524 TEST_EQUAL( psa_mac_verify_finish( &operation,
2525 verify_mac, sizeof( verify_mac ) ),
2526 PSA_ERROR_BAD_STATE );
2527 PSA_ASSERT( psa_mac_abort( &operation ) );
2528
2529 /* Setup verify but try sign. */
2530 PSA_ASSERT( psa_mac_verify_setup( &operation,
2531 handle, alg ) );
2532 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2533 TEST_EQUAL( psa_mac_sign_finish( &operation,
2534 sign_mac, sizeof( sign_mac ),
2535 &sign_mac_length ),
2536 PSA_ERROR_BAD_STATE );
2537 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002538
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002539exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002540 mbedtls_psa_crypto_free( );
2541}
2542/* END_CASE */
2543
2544/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002545void mac_sign( int key_type_arg,
2546 data_t *key,
2547 int alg_arg,
2548 data_t *input,
2549 data_t *expected_mac )
2550{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002551 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002552 psa_key_type_t key_type = key_type_arg;
2553 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002554 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002555 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002556 /* Leave a little extra room in the output buffer. At the end of the
2557 * test, we'll check that the implementation didn't overwrite onto
2558 * this extra room. */
2559 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2560 size_t mac_buffer_size =
2561 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2562 size_t mac_length = 0;
2563
2564 memset( actual_mac, '+', sizeof( actual_mac ) );
2565 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2566 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2567
Gilles Peskine8817f612018-12-18 00:18:46 +01002568 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002569
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002570 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
2571 psa_set_key_algorithm( &attributes, alg );
2572 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002573
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002574 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002575 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002576
2577 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002578 PSA_ASSERT( psa_mac_sign_setup( &operation,
2579 handle, alg ) );
2580 PSA_ASSERT( psa_mac_update( &operation,
2581 input->x, input->len ) );
2582 PSA_ASSERT( psa_mac_sign_finish( &operation,
2583 actual_mac, mac_buffer_size,
2584 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002585
2586 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002587 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2588 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002589
2590 /* Verify that the end of the buffer is untouched. */
2591 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2592 sizeof( actual_mac ) - mac_length ) );
2593
2594exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002595 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002596 mbedtls_psa_crypto_free( );
2597}
2598/* END_CASE */
2599
2600/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002601void mac_verify( int key_type_arg,
2602 data_t *key,
2603 int alg_arg,
2604 data_t *input,
2605 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002606{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002607 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002608 psa_key_type_t key_type = key_type_arg;
2609 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002610 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002611 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002612
Gilles Peskine69c12672018-06-28 00:07:19 +02002613 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2614
Gilles Peskine8817f612018-12-18 00:18:46 +01002615 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002616
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002617 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
2618 psa_set_key_algorithm( &attributes, alg );
2619 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002620
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002621 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002622 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002623
Gilles Peskine8817f612018-12-18 00:18:46 +01002624 PSA_ASSERT( psa_mac_verify_setup( &operation,
2625 handle, alg ) );
2626 PSA_ASSERT( psa_destroy_key( handle ) );
2627 PSA_ASSERT( psa_mac_update( &operation,
2628 input->x, input->len ) );
2629 PSA_ASSERT( psa_mac_verify_finish( &operation,
2630 expected_mac->x,
2631 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002632
2633exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002634 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002635 mbedtls_psa_crypto_free( );
2636}
2637/* END_CASE */
2638
2639/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002640void cipher_operation_init( )
2641{
Jaeden Ameroab439972019-02-15 14:12:05 +00002642 const uint8_t input[1] = { 0 };
2643 unsigned char output[1] = { 0 };
2644 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002645 /* Test each valid way of initializing the object, except for `= {0}`, as
2646 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2647 * though it's OK by the C standard. We could test for this, but we'd need
2648 * to supress the Clang warning for the test. */
2649 psa_cipher_operation_t func = psa_cipher_operation_init( );
2650 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2651 psa_cipher_operation_t zero;
2652
2653 memset( &zero, 0, sizeof( zero ) );
2654
Jaeden Ameroab439972019-02-15 14:12:05 +00002655 /* A freshly-initialized cipher operation should not be usable. */
2656 TEST_EQUAL( psa_cipher_update( &func,
2657 input, sizeof( input ),
2658 output, sizeof( output ),
2659 &output_length ),
2660 PSA_ERROR_BAD_STATE );
2661 TEST_EQUAL( psa_cipher_update( &init,
2662 input, sizeof( input ),
2663 output, sizeof( output ),
2664 &output_length ),
2665 PSA_ERROR_BAD_STATE );
2666 TEST_EQUAL( psa_cipher_update( &zero,
2667 input, sizeof( input ),
2668 output, sizeof( output ),
2669 &output_length ),
2670 PSA_ERROR_BAD_STATE );
2671
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002672 /* A default cipher operation should be abortable without error. */
2673 PSA_ASSERT( psa_cipher_abort( &func ) );
2674 PSA_ASSERT( psa_cipher_abort( &init ) );
2675 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002676}
2677/* END_CASE */
2678
2679/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002680void cipher_setup( int key_type_arg,
2681 data_t *key,
2682 int alg_arg,
2683 int expected_status_arg )
2684{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002685 psa_key_type_t key_type = key_type_arg;
2686 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002687 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002688 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002689 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002690#if defined(KNOWN_SUPPORTED_MAC_ALG)
2691 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2692#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002693
Gilles Peskine8817f612018-12-18 00:18:46 +01002694 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002695
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002696 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2697 &operation, &status ) )
2698 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002699 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002700
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002701 /* The operation object should be reusable. */
2702#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2703 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2704 smoke_test_key_data,
2705 sizeof( smoke_test_key_data ),
2706 KNOWN_SUPPORTED_CIPHER_ALG,
2707 &operation, &status ) )
2708 goto exit;
2709 TEST_EQUAL( status, PSA_SUCCESS );
2710#endif
2711
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002712exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002713 mbedtls_psa_crypto_free( );
2714}
2715/* END_CASE */
2716
2717/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002718void cipher_bad_order( )
2719{
2720 psa_key_handle_t handle = 0;
2721 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2722 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002723 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002724 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2725 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2726 const uint8_t key[] = {
2727 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2728 0xaa, 0xaa, 0xaa, 0xaa };
2729 const uint8_t text[] = {
2730 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2731 0xbb, 0xbb, 0xbb, 0xbb };
2732 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2733 size_t length = 0;
2734
2735 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002736 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2737 psa_set_key_algorithm( &attributes, alg );
2738 psa_set_key_type( &attributes, key_type );
2739 PSA_ASSERT( psa_import_key( &attributes, &handle,
Jaeden Ameroab439972019-02-15 14:12:05 +00002740 key, sizeof(key) ) );
2741
2742
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002743 /* Call encrypt setup twice in a row. */
2744 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2745 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2746 PSA_ERROR_BAD_STATE );
2747 PSA_ASSERT( psa_cipher_abort( &operation ) );
2748
2749 /* Call decrypt setup twice in a row. */
2750 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2751 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2752 PSA_ERROR_BAD_STATE );
2753 PSA_ASSERT( psa_cipher_abort( &operation ) );
2754
Jaeden Ameroab439972019-02-15 14:12:05 +00002755 /* Generate an IV without calling setup beforehand. */
2756 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2757 buffer, sizeof( buffer ),
2758 &length ),
2759 PSA_ERROR_BAD_STATE );
2760 PSA_ASSERT( psa_cipher_abort( &operation ) );
2761
2762 /* Generate an IV twice in a row. */
2763 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2764 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2765 buffer, sizeof( buffer ),
2766 &length ) );
2767 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2768 buffer, sizeof( buffer ),
2769 &length ),
2770 PSA_ERROR_BAD_STATE );
2771 PSA_ASSERT( psa_cipher_abort( &operation ) );
2772
2773 /* Generate an IV after it's already set. */
2774 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2775 PSA_ASSERT( psa_cipher_set_iv( &operation,
2776 iv, sizeof( iv ) ) );
2777 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2778 buffer, sizeof( buffer ),
2779 &length ),
2780 PSA_ERROR_BAD_STATE );
2781 PSA_ASSERT( psa_cipher_abort( &operation ) );
2782
2783 /* Set an IV without calling setup beforehand. */
2784 TEST_EQUAL( psa_cipher_set_iv( &operation,
2785 iv, sizeof( iv ) ),
2786 PSA_ERROR_BAD_STATE );
2787 PSA_ASSERT( psa_cipher_abort( &operation ) );
2788
2789 /* Set an IV after it's already set. */
2790 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2791 PSA_ASSERT( psa_cipher_set_iv( &operation,
2792 iv, sizeof( iv ) ) );
2793 TEST_EQUAL( psa_cipher_set_iv( &operation,
2794 iv, sizeof( iv ) ),
2795 PSA_ERROR_BAD_STATE );
2796 PSA_ASSERT( psa_cipher_abort( &operation ) );
2797
2798 /* Set an IV after it's already generated. */
2799 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2800 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2801 buffer, sizeof( buffer ),
2802 &length ) );
2803 TEST_EQUAL( psa_cipher_set_iv( &operation,
2804 iv, sizeof( iv ) ),
2805 PSA_ERROR_BAD_STATE );
2806 PSA_ASSERT( psa_cipher_abort( &operation ) );
2807
2808 /* Call update without calling setup beforehand. */
2809 TEST_EQUAL( psa_cipher_update( &operation,
2810 text, sizeof( text ),
2811 buffer, sizeof( buffer ),
2812 &length ),
2813 PSA_ERROR_BAD_STATE );
2814 PSA_ASSERT( psa_cipher_abort( &operation ) );
2815
2816 /* Call update without an IV where an IV is required. */
2817 TEST_EQUAL( psa_cipher_update( &operation,
2818 text, sizeof( text ),
2819 buffer, sizeof( buffer ),
2820 &length ),
2821 PSA_ERROR_BAD_STATE );
2822 PSA_ASSERT( psa_cipher_abort( &operation ) );
2823
2824 /* Call update after finish. */
2825 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2826 PSA_ASSERT( psa_cipher_set_iv( &operation,
2827 iv, sizeof( iv ) ) );
2828 PSA_ASSERT( psa_cipher_finish( &operation,
2829 buffer, sizeof( buffer ), &length ) );
2830 TEST_EQUAL( psa_cipher_update( &operation,
2831 text, sizeof( text ),
2832 buffer, sizeof( buffer ),
2833 &length ),
2834 PSA_ERROR_BAD_STATE );
2835 PSA_ASSERT( psa_cipher_abort( &operation ) );
2836
2837 /* Call finish without calling setup beforehand. */
2838 TEST_EQUAL( psa_cipher_finish( &operation,
2839 buffer, sizeof( buffer ), &length ),
2840 PSA_ERROR_BAD_STATE );
2841 PSA_ASSERT( psa_cipher_abort( &operation ) );
2842
2843 /* Call finish without an IV where an IV is required. */
2844 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2845 /* Not calling update means we are encrypting an empty buffer, which is OK
2846 * for cipher modes with padding. */
2847 TEST_EQUAL( psa_cipher_finish( &operation,
2848 buffer, sizeof( buffer ), &length ),
2849 PSA_ERROR_BAD_STATE );
2850 PSA_ASSERT( psa_cipher_abort( &operation ) );
2851
2852 /* Call finish twice in a row. */
2853 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2854 PSA_ASSERT( psa_cipher_set_iv( &operation,
2855 iv, sizeof( iv ) ) );
2856 PSA_ASSERT( psa_cipher_finish( &operation,
2857 buffer, sizeof( buffer ), &length ) );
2858 TEST_EQUAL( psa_cipher_finish( &operation,
2859 buffer, sizeof( buffer ), &length ),
2860 PSA_ERROR_BAD_STATE );
2861 PSA_ASSERT( psa_cipher_abort( &operation ) );
2862
2863exit:
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002864 mbedtls_psa_crypto_free( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002865}
2866/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002867
Gilles Peskine50e586b2018-06-08 14:28:46 +02002868/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002869void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002870 data_t *key,
2871 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002872 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002873{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002874 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002875 psa_status_t status;
2876 psa_key_type_t key_type = key_type_arg;
2877 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002878 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002879 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002880 size_t iv_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002881 unsigned char *output = NULL;
2882 size_t output_buffer_size = 0;
2883 size_t function_output_length = 0;
2884 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002885 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002886 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002887
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002888 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2889 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002890
Gilles Peskine8817f612018-12-18 00:18:46 +01002891 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002892
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002893 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2894 psa_set_key_algorithm( &attributes, alg );
2895 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002896
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002897 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002898 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002899
Gilles Peskine8817f612018-12-18 00:18:46 +01002900 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2901 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002902
Gilles Peskine8817f612018-12-18 00:18:46 +01002903 PSA_ASSERT( psa_cipher_set_iv( &operation,
2904 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002905 output_buffer_size = ( (size_t) input->len +
2906 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002907 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002908
Gilles Peskine8817f612018-12-18 00:18:46 +01002909 PSA_ASSERT( psa_cipher_update( &operation,
2910 input->x, input->len,
2911 output, output_buffer_size,
2912 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002913 total_output_length += function_output_length;
2914 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002915 output + total_output_length,
2916 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002917 &function_output_length );
2918 total_output_length += function_output_length;
2919
Gilles Peskinefe11b722018-12-18 00:24:04 +01002920 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002921 if( expected_status == PSA_SUCCESS )
2922 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002923 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002924 ASSERT_COMPARE( expected_output->x, expected_output->len,
2925 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002926 }
2927
2928exit:
2929 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002930 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002931 mbedtls_psa_crypto_free( );
2932}
2933/* END_CASE */
2934
2935/* BEGIN_CASE */
2936void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
2937 data_t *key,
2938 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002939 int first_part_size_arg,
2940 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002941 data_t *expected_output )
2942{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002943 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002944 psa_key_type_t key_type = key_type_arg;
2945 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002946 size_t first_part_size = first_part_size_arg;
2947 size_t output1_length = output1_length_arg;
2948 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002949 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002950 size_t iv_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002951 unsigned char *output = NULL;
2952 size_t output_buffer_size = 0;
2953 size_t function_output_length = 0;
2954 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002955 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002956 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002957
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002958 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2959 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002960
Gilles Peskine8817f612018-12-18 00:18:46 +01002961 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002962
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002963 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2964 psa_set_key_algorithm( &attributes, alg );
2965 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002966
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002967 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01002968 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002969
Gilles Peskine8817f612018-12-18 00:18:46 +01002970 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2971 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002972
Gilles Peskine8817f612018-12-18 00:18:46 +01002973 PSA_ASSERT( psa_cipher_set_iv( &operation,
2974 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002975 output_buffer_size = ( (size_t) input->len +
2976 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002977 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002978
Gilles Peskinee0866522019-02-19 19:44:00 +01002979 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002980 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2981 output, output_buffer_size,
2982 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002983 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002984 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002985 PSA_ASSERT( psa_cipher_update( &operation,
2986 input->x + first_part_size,
2987 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002988 output + total_output_length,
2989 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002990 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002991 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002992 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002993 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002994 output + total_output_length,
2995 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002996 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002997 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002998 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002999
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003000 ASSERT_COMPARE( expected_output->x, expected_output->len,
3001 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003002
3003exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003004 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003005 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003006 mbedtls_psa_crypto_free( );
3007}
3008/* END_CASE */
3009
3010/* BEGIN_CASE */
3011void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003012 data_t *key,
3013 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003014 int first_part_size_arg,
3015 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003016 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003017{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003018 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003019
3020 psa_key_type_t key_type = key_type_arg;
3021 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003022 size_t first_part_size = first_part_size_arg;
3023 size_t output1_length = output1_length_arg;
3024 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003025 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003026 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003027 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003028 size_t output_buffer_size = 0;
3029 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003030 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003031 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003032 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003033
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003034 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3035 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003036
Gilles Peskine8817f612018-12-18 00:18:46 +01003037 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003038
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003039 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3040 psa_set_key_algorithm( &attributes, alg );
3041 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003042
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003043 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003044 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003045
Gilles Peskine8817f612018-12-18 00:18:46 +01003046 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3047 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003048
Gilles Peskine8817f612018-12-18 00:18:46 +01003049 PSA_ASSERT( psa_cipher_set_iv( &operation,
3050 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003051
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003052 output_buffer_size = ( (size_t) input->len +
3053 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003054 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003055
Gilles Peskinee0866522019-02-19 19:44:00 +01003056 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003057 PSA_ASSERT( psa_cipher_update( &operation,
3058 input->x, first_part_size,
3059 output, output_buffer_size,
3060 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003061 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003062 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003063 PSA_ASSERT( psa_cipher_update( &operation,
3064 input->x + first_part_size,
3065 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003066 output + total_output_length,
3067 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003068 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003069 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003070 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003071 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003072 output + total_output_length,
3073 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003074 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003075 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003076 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003077
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003078 ASSERT_COMPARE( expected_output->x, expected_output->len,
3079 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003080
3081exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003082 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003083 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003084 mbedtls_psa_crypto_free( );
3085}
3086/* END_CASE */
3087
Gilles Peskine50e586b2018-06-08 14:28:46 +02003088/* BEGIN_CASE */
3089void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003090 data_t *key,
3091 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003092 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003093{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003094 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003095 psa_status_t status;
3096 psa_key_type_t key_type = key_type_arg;
3097 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003098 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003099 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003100 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003101 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003102 size_t output_buffer_size = 0;
3103 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003104 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003105 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003106 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003107
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003108 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3109 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003110
Gilles Peskine8817f612018-12-18 00:18:46 +01003111 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003112
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003113 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3114 psa_set_key_algorithm( &attributes, alg );
3115 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003116
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003117 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003118 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003119
Gilles Peskine8817f612018-12-18 00:18:46 +01003120 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3121 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003122
Gilles Peskine8817f612018-12-18 00:18:46 +01003123 PSA_ASSERT( psa_cipher_set_iv( &operation,
3124 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003125
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003126 output_buffer_size = ( (size_t) input->len +
3127 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003128 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003129
Gilles Peskine8817f612018-12-18 00:18:46 +01003130 PSA_ASSERT( psa_cipher_update( &operation,
3131 input->x, input->len,
3132 output, output_buffer_size,
3133 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003134 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003135 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003136 output + total_output_length,
3137 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003138 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003139 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003140 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003141
3142 if( expected_status == PSA_SUCCESS )
3143 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003144 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003145 ASSERT_COMPARE( expected_output->x, expected_output->len,
3146 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003147 }
3148
Gilles Peskine50e586b2018-06-08 14:28:46 +02003149exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003150 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003151 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003152 mbedtls_psa_crypto_free( );
3153}
3154/* END_CASE */
3155
Gilles Peskine50e586b2018-06-08 14:28:46 +02003156/* BEGIN_CASE */
3157void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003158 data_t *key,
3159 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003160{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003161 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003162 psa_key_type_t key_type = key_type_arg;
3163 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003164 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003165 size_t iv_size = 16;
3166 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003167 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003168 size_t output1_size = 0;
3169 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003170 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003171 size_t output2_size = 0;
3172 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003173 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003174 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3175 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003176 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003177
Gilles Peskine8817f612018-12-18 00:18:46 +01003178 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003179
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003180 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3181 psa_set_key_algorithm( &attributes, alg );
3182 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003183
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003184 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003185 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003186
Gilles Peskine8817f612018-12-18 00:18:46 +01003187 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3188 handle, alg ) );
3189 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3190 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003191
Gilles Peskine8817f612018-12-18 00:18:46 +01003192 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3193 iv, iv_size,
3194 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003195 output1_size = ( (size_t) input->len +
3196 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003197 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003198
Gilles Peskine8817f612018-12-18 00:18:46 +01003199 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3200 output1, output1_size,
3201 &output1_length ) );
3202 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003203 output1 + output1_length,
3204 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003205 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003206
Gilles Peskine048b7f02018-06-08 14:20:49 +02003207 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003208
Gilles Peskine8817f612018-12-18 00:18:46 +01003209 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003210
3211 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003212 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003213
Gilles Peskine8817f612018-12-18 00:18:46 +01003214 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3215 iv, iv_length ) );
3216 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3217 output2, output2_size,
3218 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003219 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003220 PSA_ASSERT( psa_cipher_finish( &operation2,
3221 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003222 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003223 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003224
Gilles Peskine048b7f02018-06-08 14:20:49 +02003225 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003226
Gilles Peskine8817f612018-12-18 00:18:46 +01003227 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003228
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003229 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003230
3231exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003232 mbedtls_free( output1 );
3233 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003234 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03003235 mbedtls_psa_crypto_free( );
3236}
3237/* END_CASE */
3238
3239/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003240void cipher_verify_output_multipart( int alg_arg,
3241 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003242 data_t *key,
3243 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003244 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003245{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003246 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003247 psa_key_type_t key_type = key_type_arg;
3248 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003249 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003250 unsigned char iv[16] = {0};
3251 size_t iv_size = 16;
3252 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003253 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003254 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003255 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003256 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003257 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003258 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003259 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003260 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3261 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003262 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003263
Gilles Peskine8817f612018-12-18 00:18:46 +01003264 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003265
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003266 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3267 psa_set_key_algorithm( &attributes, alg );
3268 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003269
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003270 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003271 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003272
Gilles Peskine8817f612018-12-18 00:18:46 +01003273 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3274 handle, alg ) );
3275 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3276 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003277
Gilles Peskine8817f612018-12-18 00:18:46 +01003278 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3279 iv, iv_size,
3280 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003281 output1_buffer_size = ( (size_t) input->len +
3282 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003283 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003284
Gilles Peskinee0866522019-02-19 19:44:00 +01003285 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003286
Gilles Peskine8817f612018-12-18 00:18:46 +01003287 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3288 output1, output1_buffer_size,
3289 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003290 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003291
Gilles Peskine8817f612018-12-18 00:18:46 +01003292 PSA_ASSERT( psa_cipher_update( &operation1,
3293 input->x + first_part_size,
3294 input->len - first_part_size,
3295 output1, output1_buffer_size,
3296 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003297 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003298
Gilles Peskine8817f612018-12-18 00:18:46 +01003299 PSA_ASSERT( psa_cipher_finish( &operation1,
3300 output1 + output1_length,
3301 output1_buffer_size - output1_length,
3302 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003303 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003304
Gilles Peskine8817f612018-12-18 00:18:46 +01003305 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003306
Gilles Peskine048b7f02018-06-08 14:20:49 +02003307 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003308 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003309
Gilles Peskine8817f612018-12-18 00:18:46 +01003310 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3311 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003312
Gilles Peskine8817f612018-12-18 00:18:46 +01003313 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3314 output2, output2_buffer_size,
3315 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003316 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003317
Gilles Peskine8817f612018-12-18 00:18:46 +01003318 PSA_ASSERT( psa_cipher_update( &operation2,
3319 output1 + first_part_size,
3320 output1_length - first_part_size,
3321 output2, output2_buffer_size,
3322 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003323 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003324
Gilles Peskine8817f612018-12-18 00:18:46 +01003325 PSA_ASSERT( psa_cipher_finish( &operation2,
3326 output2 + output2_length,
3327 output2_buffer_size - output2_length,
3328 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003329 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003330
Gilles Peskine8817f612018-12-18 00:18:46 +01003331 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003332
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003333 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003334
3335exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003336 mbedtls_free( output1 );
3337 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003338 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003339 mbedtls_psa_crypto_free( );
3340}
3341/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003342
Gilles Peskine20035e32018-02-03 22:44:14 +01003343/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003344void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003345 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003346 data_t *nonce,
3347 data_t *additional_data,
3348 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003349 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003350{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003351 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003352 psa_key_type_t key_type = key_type_arg;
3353 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003354 unsigned char *output_data = NULL;
3355 size_t output_size = 0;
3356 size_t output_length = 0;
3357 unsigned char *output_data2 = NULL;
3358 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003359 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003360 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003361 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003362
Gilles Peskine4abf7412018-06-18 16:35:34 +02003363 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003364 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003365
Gilles Peskine8817f612018-12-18 00:18:46 +01003366 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003367
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003368 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3369 psa_set_key_algorithm( &attributes, alg );
3370 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003371
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003372 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01003373 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003374
Gilles Peskinefe11b722018-12-18 00:24:04 +01003375 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3376 nonce->x, nonce->len,
3377 additional_data->x,
3378 additional_data->len,
3379 input_data->x, input_data->len,
3380 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003381 &output_length ),
3382 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003383
3384 if( PSA_SUCCESS == expected_result )
3385 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003386 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003387
Gilles Peskinefe11b722018-12-18 00:24:04 +01003388 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3389 nonce->x, nonce->len,
3390 additional_data->x,
3391 additional_data->len,
3392 output_data, output_length,
3393 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003394 &output_length2 ),
3395 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003396
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003397 ASSERT_COMPARE( input_data->x, input_data->len,
3398 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003399 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003400
Gilles Peskinea1cac842018-06-11 19:33:02 +02003401exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003402 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003403 mbedtls_free( output_data );
3404 mbedtls_free( output_data2 );
3405 mbedtls_psa_crypto_free( );
3406}
3407/* END_CASE */
3408
3409/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003410void aead_encrypt( int key_type_arg, data_t *key_data,
3411 int alg_arg,
3412 data_t *nonce,
3413 data_t *additional_data,
3414 data_t *input_data,
3415 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003416{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003417 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003418 psa_key_type_t key_type = key_type_arg;
3419 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003420 unsigned char *output_data = NULL;
3421 size_t output_size = 0;
3422 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003423 size_t tag_length = 16;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003424 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003425
Gilles Peskine4abf7412018-06-18 16:35:34 +02003426 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003427 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003428
Gilles Peskine8817f612018-12-18 00:18:46 +01003429 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003430
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003431 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3432 psa_set_key_algorithm( &attributes, alg );
3433 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003434
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003435 PSA_ASSERT( psa_import_key( &attributes, &handle,
3436 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003437
Gilles Peskine8817f612018-12-18 00:18:46 +01003438 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3439 nonce->x, nonce->len,
3440 additional_data->x, additional_data->len,
3441 input_data->x, input_data->len,
3442 output_data, output_size,
3443 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003444
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003445 ASSERT_COMPARE( expected_result->x, expected_result->len,
3446 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003447
Gilles Peskinea1cac842018-06-11 19:33:02 +02003448exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003449 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003450 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003451 mbedtls_psa_crypto_free( );
3452}
3453/* END_CASE */
3454
3455/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003456void aead_decrypt( int key_type_arg, data_t *key_data,
3457 int alg_arg,
3458 data_t *nonce,
3459 data_t *additional_data,
3460 data_t *input_data,
3461 data_t *expected_data,
3462 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003463{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003464 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003465 psa_key_type_t key_type = key_type_arg;
3466 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003467 unsigned char *output_data = NULL;
3468 size_t output_size = 0;
3469 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003470 size_t tag_length = 16;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003471 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003472 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003473
Gilles Peskine4abf7412018-06-18 16:35:34 +02003474 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003475 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003476
Gilles Peskine8817f612018-12-18 00:18:46 +01003477 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003478
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003479 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3480 psa_set_key_algorithm( &attributes, alg );
3481 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003482
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003483 PSA_ASSERT( psa_import_key( &attributes, &handle,
3484 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003485
Gilles Peskinefe11b722018-12-18 00:24:04 +01003486 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3487 nonce->x, nonce->len,
3488 additional_data->x,
3489 additional_data->len,
3490 input_data->x, input_data->len,
3491 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003492 &output_length ),
3493 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003494
Gilles Peskine2d277862018-06-18 15:41:12 +02003495 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003496 ASSERT_COMPARE( expected_data->x, expected_data->len,
3497 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003498
Gilles Peskinea1cac842018-06-11 19:33:02 +02003499exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003500 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003501 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003502 mbedtls_psa_crypto_free( );
3503}
3504/* END_CASE */
3505
3506/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003507void signature_size( int type_arg,
3508 int bits,
3509 int alg_arg,
3510 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003511{
3512 psa_key_type_t type = type_arg;
3513 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003514 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003515 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003516exit:
3517 ;
3518}
3519/* END_CASE */
3520
3521/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003522void sign_deterministic( int key_type_arg, data_t *key_data,
3523 int alg_arg, data_t *input_data,
3524 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003525{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003526 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003527 psa_key_type_t key_type = key_type_arg;
3528 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003529 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003530 unsigned char *signature = NULL;
3531 size_t signature_size;
3532 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003533 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003534
Gilles Peskine8817f612018-12-18 00:18:46 +01003535 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003536
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003537 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3538 psa_set_key_algorithm( &attributes, alg );
3539 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003540
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003541 PSA_ASSERT( psa_import_key( &attributes, &handle,
3542 key_data->x, key_data->len ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003543 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3544 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003545
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003546 /* Allocate a buffer which has the size advertized by the
3547 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003548 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3549 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003550 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003551 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003552 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003553
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003554 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003555 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3556 input_data->x, input_data->len,
3557 signature, signature_size,
3558 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003559 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003560 ASSERT_COMPARE( output_data->x, output_data->len,
3561 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003562
3563exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003564 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003565 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003566 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01003567 mbedtls_psa_crypto_free( );
3568}
3569/* END_CASE */
3570
3571/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003572void sign_fail( int key_type_arg, data_t *key_data,
3573 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003574 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003575{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003576 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003577 psa_key_type_t key_type = key_type_arg;
3578 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003579 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003580 psa_status_t actual_status;
3581 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003582 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003583 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003584 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003585
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003586 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003587
Gilles Peskine8817f612018-12-18 00:18:46 +01003588 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003589
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003590 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3591 psa_set_key_algorithm( &attributes, alg );
3592 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003593
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003594 PSA_ASSERT( psa_import_key( &attributes, &handle,
3595 key_data->x, key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003596
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003597 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003598 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003599 signature, signature_size,
3600 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003601 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003602 /* The value of *signature_length is unspecified on error, but
3603 * whatever it is, it should be less than signature_size, so that
3604 * if the caller tries to read *signature_length bytes without
3605 * checking the error code then they don't overflow a buffer. */
3606 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003607
3608exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003609 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003610 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003611 mbedtls_free( signature );
3612 mbedtls_psa_crypto_free( );
3613}
3614/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003615
3616/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003617void sign_verify( int key_type_arg, data_t *key_data,
3618 int alg_arg, data_t *input_data )
3619{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003620 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003621 psa_key_type_t key_type = key_type_arg;
3622 psa_algorithm_t alg = alg_arg;
3623 size_t key_bits;
3624 unsigned char *signature = NULL;
3625 size_t signature_size;
3626 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003627 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003628
Gilles Peskine8817f612018-12-18 00:18:46 +01003629 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003630
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003631 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
3632 psa_set_key_algorithm( &attributes, alg );
3633 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003634
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003635 PSA_ASSERT( psa_import_key( &attributes, &handle,
3636 key_data->x, key_data->len ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003637 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3638 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003639
3640 /* Allocate a buffer which has the size advertized by the
3641 * library. */
3642 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3643 key_bits, alg );
3644 TEST_ASSERT( signature_size != 0 );
3645 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003646 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003647
3648 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003649 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3650 input_data->x, input_data->len,
3651 signature, signature_size,
3652 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003653 /* Check that the signature length looks sensible. */
3654 TEST_ASSERT( signature_length <= signature_size );
3655 TEST_ASSERT( signature_length > 0 );
3656
3657 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003658 PSA_ASSERT( psa_asymmetric_verify(
3659 handle, alg,
3660 input_data->x, input_data->len,
3661 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003662
3663 if( input_data->len != 0 )
3664 {
3665 /* Flip a bit in the input and verify that the signature is now
3666 * detected as invalid. Flip a bit at the beginning, not at the end,
3667 * because ECDSA may ignore the last few bits of the input. */
3668 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003669 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3670 input_data->x, input_data->len,
3671 signature, signature_length ),
3672 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003673 }
3674
3675exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003676 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003677 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003678 mbedtls_free( signature );
3679 mbedtls_psa_crypto_free( );
3680}
3681/* END_CASE */
3682
3683/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003684void asymmetric_verify( int key_type_arg, data_t *key_data,
3685 int alg_arg, data_t *hash_data,
3686 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003687{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003688 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003689 psa_key_type_t key_type = key_type_arg;
3690 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003691 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003692
Gilles Peskine69c12672018-06-28 00:07:19 +02003693 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3694
Gilles Peskine8817f612018-12-18 00:18:46 +01003695 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003696
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003697 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3698 psa_set_key_algorithm( &attributes, alg );
3699 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003700
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003701 PSA_ASSERT( psa_import_key( &attributes, &handle,
3702 key_data->x, key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003703
Gilles Peskine8817f612018-12-18 00:18:46 +01003704 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3705 hash_data->x, hash_data->len,
3706 signature_data->x,
3707 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003708exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003709 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003710 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003711 mbedtls_psa_crypto_free( );
3712}
3713/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003714
3715/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003716void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3717 int alg_arg, data_t *hash_data,
3718 data_t *signature_data,
3719 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003720{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003721 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003722 psa_key_type_t key_type = key_type_arg;
3723 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003724 psa_status_t actual_status;
3725 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003726 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003727
Gilles Peskine8817f612018-12-18 00:18:46 +01003728 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003729
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003730 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3731 psa_set_key_algorithm( &attributes, alg );
3732 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003733
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003734 PSA_ASSERT( psa_import_key( &attributes, &handle,
3735 key_data->x, key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003736
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003737 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003738 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003739 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003740 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003741
Gilles Peskinefe11b722018-12-18 00:24:04 +01003742 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003743
3744exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003745 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003746 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003747 mbedtls_psa_crypto_free( );
3748}
3749/* END_CASE */
3750
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003751/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003752void asymmetric_encrypt( int key_type_arg,
3753 data_t *key_data,
3754 int alg_arg,
3755 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003756 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003757 int expected_output_length_arg,
3758 int expected_status_arg )
3759{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003760 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003761 psa_key_type_t key_type = key_type_arg;
3762 psa_algorithm_t alg = alg_arg;
3763 size_t expected_output_length = expected_output_length_arg;
3764 size_t key_bits;
3765 unsigned char *output = NULL;
3766 size_t output_size;
3767 size_t output_length = ~0;
3768 psa_status_t actual_status;
3769 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003770 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003771
Gilles Peskine8817f612018-12-18 00:18:46 +01003772 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003773
Gilles Peskine656896e2018-06-29 19:12:28 +02003774 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003775 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3776 psa_set_key_algorithm( &attributes, alg );
3777 psa_set_key_type( &attributes, key_type );
3778 PSA_ASSERT( psa_import_key( &attributes, &handle,
3779 key_data->x, key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003780
3781 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003782 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3783 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02003784 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003785 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003786
3787 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003788 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003789 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003790 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003791 output, output_size,
3792 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003793 TEST_EQUAL( actual_status, expected_status );
3794 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003795
Gilles Peskine68428122018-06-30 18:42:41 +02003796 /* If the label is empty, the test framework puts a non-null pointer
3797 * in label->x. Test that a null pointer works as well. */
3798 if( label->len == 0 )
3799 {
3800 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003801 if( output_size != 0 )
3802 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003803 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003804 input_data->x, input_data->len,
3805 NULL, label->len,
3806 output, output_size,
3807 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003808 TEST_EQUAL( actual_status, expected_status );
3809 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003810 }
3811
Gilles Peskine656896e2018-06-29 19:12:28 +02003812exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003813 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003814 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003815 mbedtls_free( output );
3816 mbedtls_psa_crypto_free( );
3817}
3818/* END_CASE */
3819
3820/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003821void asymmetric_encrypt_decrypt( int key_type_arg,
3822 data_t *key_data,
3823 int alg_arg,
3824 data_t *input_data,
3825 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003826{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003827 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003828 psa_key_type_t key_type = key_type_arg;
3829 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003830 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003831 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003832 size_t output_size;
3833 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003834 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003835 size_t output2_size;
3836 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003837 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003838
Gilles Peskine8817f612018-12-18 00:18:46 +01003839 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003840
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003841 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3842 psa_set_key_algorithm( &attributes, alg );
3843 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003844
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003845 PSA_ASSERT( psa_import_key( &attributes, &handle,
3846 key_data->x, key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003847
3848 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003849 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3850 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003851 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003852 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003853 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003854 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003855
Gilles Peskineeebd7382018-06-08 18:11:54 +02003856 /* We test encryption by checking that encrypt-then-decrypt gives back
3857 * the original plaintext because of the non-optional random
3858 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003859 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3860 input_data->x, input_data->len,
3861 label->x, label->len,
3862 output, output_size,
3863 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003864 /* We don't know what ciphertext length to expect, but check that
3865 * it looks sensible. */
3866 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003867
Gilles Peskine8817f612018-12-18 00:18:46 +01003868 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3869 output, output_length,
3870 label->x, label->len,
3871 output2, output2_size,
3872 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003873 ASSERT_COMPARE( input_data->x, input_data->len,
3874 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003875
3876exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003877 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003878 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003879 mbedtls_free( output );
3880 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003881 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003882}
3883/* END_CASE */
3884
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003885/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003886void asymmetric_decrypt( int key_type_arg,
3887 data_t *key_data,
3888 int alg_arg,
3889 data_t *input_data,
3890 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003891 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003892{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003893 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003894 psa_key_type_t key_type = key_type_arg;
3895 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003896 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003897 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003898 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003899 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003900
Jaeden Amero412654a2019-02-06 12:57:46 +00003901 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003902 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003903
Gilles Peskine8817f612018-12-18 00:18:46 +01003904 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003905
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003906 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3907 psa_set_key_algorithm( &attributes, alg );
3908 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003909
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003910 PSA_ASSERT( psa_import_key( &attributes, &handle,
3911 key_data->x, key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003912
Gilles Peskine8817f612018-12-18 00:18:46 +01003913 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3914 input_data->x, input_data->len,
3915 label->x, label->len,
3916 output,
3917 output_size,
3918 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003919 ASSERT_COMPARE( expected_data->x, expected_data->len,
3920 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003921
Gilles Peskine68428122018-06-30 18:42:41 +02003922 /* If the label is empty, the test framework puts a non-null pointer
3923 * in label->x. Test that a null pointer works as well. */
3924 if( label->len == 0 )
3925 {
3926 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003927 if( output_size != 0 )
3928 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003929 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3930 input_data->x, input_data->len,
3931 NULL, label->len,
3932 output,
3933 output_size,
3934 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003935 ASSERT_COMPARE( expected_data->x, expected_data->len,
3936 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003937 }
3938
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003939exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003940 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003941 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003942 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003943 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003944}
3945/* END_CASE */
3946
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003947/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003948void asymmetric_decrypt_fail( int key_type_arg,
3949 data_t *key_data,
3950 int alg_arg,
3951 data_t *input_data,
3952 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003953 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003954 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003955{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003956 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003957 psa_key_type_t key_type = key_type_arg;
3958 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003959 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003960 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003961 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003962 psa_status_t actual_status;
3963 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003964 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003965
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003966 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003967
Gilles Peskine8817f612018-12-18 00:18:46 +01003968 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003969
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003970 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3971 psa_set_key_algorithm( &attributes, alg );
3972 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003973
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003974 PSA_ASSERT( psa_import_key( &attributes, &handle,
3975 key_data->x, key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003976
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003977 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003978 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003979 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003980 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003981 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003982 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003983 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003984
Gilles Peskine68428122018-06-30 18:42:41 +02003985 /* If the label is empty, the test framework puts a non-null pointer
3986 * in label->x. Test that a null pointer works as well. */
3987 if( label->len == 0 )
3988 {
3989 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003990 if( output_size != 0 )
3991 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003992 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003993 input_data->x, input_data->len,
3994 NULL, label->len,
3995 output, output_size,
3996 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003997 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003998 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003999 }
4000
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004001exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004002 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004003 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004004 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004005 mbedtls_psa_crypto_free( );
4006}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004007/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004008
4009/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00004010void crypto_generator_init( )
4011{
4012 /* Test each valid way of initializing the object, except for `= {0}`, as
4013 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4014 * though it's OK by the C standard. We could test for this, but we'd need
4015 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004016 size_t capacity;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004017 psa_crypto_generator_t func = psa_crypto_generator_init( );
4018 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
4019 psa_crypto_generator_t zero;
4020
4021 memset( &zero, 0, sizeof( zero ) );
4022
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004023 /* A default generator should not be able to report its capacity. */
4024 TEST_EQUAL( psa_get_generator_capacity( &func, &capacity ),
4025 PSA_ERROR_BAD_STATE );
4026 TEST_EQUAL( psa_get_generator_capacity( &init, &capacity ),
4027 PSA_ERROR_BAD_STATE );
4028 TEST_EQUAL( psa_get_generator_capacity( &zero, &capacity ),
4029 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004030
4031 /* A default generator should be abortable without error. */
4032 PSA_ASSERT( psa_generator_abort(&func) );
4033 PSA_ASSERT( psa_generator_abort(&init) );
4034 PSA_ASSERT( psa_generator_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004035}
4036/* END_CASE */
4037
4038/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004039void derive_setup( int key_type_arg,
4040 data_t *key_data,
4041 int alg_arg,
4042 data_t *salt,
4043 data_t *label,
4044 int requested_capacity_arg,
4045 int expected_status_arg )
4046{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004047 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004048 size_t key_type = key_type_arg;
4049 psa_algorithm_t alg = alg_arg;
4050 size_t requested_capacity = requested_capacity_arg;
4051 psa_status_t expected_status = expected_status_arg;
4052 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004053 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004054
Gilles Peskine8817f612018-12-18 00:18:46 +01004055 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004056
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004057 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4058 psa_set_key_algorithm( &attributes, alg );
4059 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004060
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004061 PSA_ASSERT( psa_import_key( &attributes, &handle,
4062 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004063
Gilles Peskinefe11b722018-12-18 00:24:04 +01004064 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4065 salt->x, salt->len,
4066 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004067 requested_capacity ),
4068 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004069
4070exit:
4071 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004072 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004073 mbedtls_psa_crypto_free( );
4074}
4075/* END_CASE */
4076
4077/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004078void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004079{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004080 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004081 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004082 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004083 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004084 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004085 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004086 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4087 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4088 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004089 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004090
Gilles Peskine8817f612018-12-18 00:18:46 +01004091 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004092
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004093 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4094 psa_set_key_algorithm( &attributes, alg );
4095 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004096
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004097 PSA_ASSERT( psa_import_key( &attributes, &handle,
4098 key_data, sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004099
4100 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01004101 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4102 NULL, 0,
4103 NULL, 0,
4104 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004105
4106 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01004107 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4108 NULL, 0,
4109 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004110 capacity ),
4111 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004112
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004113 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004114
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004115 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004116 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004117
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004118exit:
4119 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004120 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004121 mbedtls_psa_crypto_free( );
4122}
4123/* END_CASE */
4124
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004125/* BEGIN_CASE */
4126void test_derive_invalid_generator_tests( )
4127{
4128 uint8_t output_buffer[16];
4129 size_t buffer_size = 16;
4130 size_t capacity = 0;
4131 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4132
Nir Sonnenschein50789302018-10-31 12:16:38 +02004133 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004134 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004135
4136 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004137 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004138
Gilles Peskine8817f612018-12-18 00:18:46 +01004139 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004140
Nir Sonnenschein50789302018-10-31 12:16:38 +02004141 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004142 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004143
Nir Sonnenschein50789302018-10-31 12:16:38 +02004144 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004145 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004146
4147exit:
4148 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004149}
4150/* END_CASE */
4151
4152/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004153void derive_output( int alg_arg,
4154 data_t *key_data,
4155 data_t *salt,
4156 data_t *label,
4157 int requested_capacity_arg,
4158 data_t *expected_output1,
4159 data_t *expected_output2 )
4160{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004161 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004162 psa_algorithm_t alg = alg_arg;
4163 size_t requested_capacity = requested_capacity_arg;
4164 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4165 uint8_t *expected_outputs[2] =
4166 {expected_output1->x, expected_output2->x};
4167 size_t output_sizes[2] =
4168 {expected_output1->len, expected_output2->len};
4169 size_t output_buffer_size = 0;
4170 uint8_t *output_buffer = NULL;
4171 size_t expected_capacity;
4172 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004173 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004174 psa_status_t status;
4175 unsigned i;
4176
4177 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4178 {
4179 if( output_sizes[i] > output_buffer_size )
4180 output_buffer_size = output_sizes[i];
4181 if( output_sizes[i] == 0 )
4182 expected_outputs[i] = NULL;
4183 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004184 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004185 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004186
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004187 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4188 psa_set_key_algorithm( &attributes, alg );
4189 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004190
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004191 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004192 key_data->x, key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004193
4194 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004195 if( PSA_ALG_IS_HKDF( alg ) )
4196 {
4197 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4198 PSA_ASSERT( psa_set_generator_capacity( &generator,
4199 requested_capacity ) );
4200 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4201 PSA_KDF_STEP_SALT,
4202 salt->x, salt->len ) );
4203 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4204 PSA_KDF_STEP_SECRET,
4205 handle ) );
4206 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4207 PSA_KDF_STEP_INFO,
4208 label->x, label->len ) );
4209 }
4210 else
4211 {
4212 // legacy
4213 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4214 salt->x, salt->len,
4215 label->x, label->len,
4216 requested_capacity ) );
4217 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004218 PSA_ASSERT( psa_get_generator_capacity( &generator,
4219 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004220 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004221 expected_capacity = requested_capacity;
4222
4223 /* Expansion phase. */
4224 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4225 {
4226 /* Read some bytes. */
4227 status = psa_generator_read( &generator,
4228 output_buffer, output_sizes[i] );
4229 if( expected_capacity == 0 && output_sizes[i] == 0 )
4230 {
4231 /* Reading 0 bytes when 0 bytes are available can go either way. */
4232 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004233 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004234 continue;
4235 }
4236 else if( expected_capacity == 0 ||
4237 output_sizes[i] > expected_capacity )
4238 {
4239 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004240 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004241 expected_capacity = 0;
4242 continue;
4243 }
4244 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004245 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004246 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004247 ASSERT_COMPARE( output_buffer, output_sizes[i],
4248 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004249 /* Check the generator status. */
4250 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01004251 PSA_ASSERT( psa_get_generator_capacity( &generator,
4252 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004253 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004254 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004255 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004256
4257exit:
4258 mbedtls_free( output_buffer );
4259 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004260 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004261 mbedtls_psa_crypto_free( );
4262}
4263/* END_CASE */
4264
4265/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004266void derive_full( int alg_arg,
4267 data_t *key_data,
4268 data_t *salt,
4269 data_t *label,
4270 int requested_capacity_arg )
4271{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004272 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004273 psa_algorithm_t alg = alg_arg;
4274 size_t requested_capacity = requested_capacity_arg;
4275 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4276 unsigned char output_buffer[16];
4277 size_t expected_capacity = requested_capacity;
4278 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004279 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004280
Gilles Peskine8817f612018-12-18 00:18:46 +01004281 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004282
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004283 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4284 psa_set_key_algorithm( &attributes, alg );
4285 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004286
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004287 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004288 key_data->x, key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004289
4290 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004291 if( PSA_ALG_IS_HKDF( alg ) )
4292 {
4293 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4294 PSA_ASSERT( psa_set_generator_capacity( &generator,
4295 requested_capacity ) );
4296 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4297 PSA_KDF_STEP_SALT,
4298 salt->x, salt->len ) );
4299 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4300 PSA_KDF_STEP_SECRET,
4301 handle ) );
4302 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4303 PSA_KDF_STEP_INFO,
4304 label->x, label->len ) );
4305 }
4306 else
4307 {
4308 // legacy
4309 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4310 salt->x, salt->len,
4311 label->x, label->len,
4312 requested_capacity ) );
4313 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004314 PSA_ASSERT( psa_get_generator_capacity( &generator,
4315 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004316 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004317
4318 /* Expansion phase. */
4319 while( current_capacity > 0 )
4320 {
4321 size_t read_size = sizeof( output_buffer );
4322 if( read_size > current_capacity )
4323 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01004324 PSA_ASSERT( psa_generator_read( &generator,
4325 output_buffer,
4326 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004327 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01004328 PSA_ASSERT( psa_get_generator_capacity( &generator,
4329 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004330 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004331 }
4332
4333 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004334 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004335 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004336
Gilles Peskine8817f612018-12-18 00:18:46 +01004337 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004338
4339exit:
4340 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004341 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02004342 mbedtls_psa_crypto_free( );
4343}
4344/* END_CASE */
4345
4346/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004347void derive_key_exercise( int alg_arg,
4348 data_t *key_data,
4349 data_t *salt,
4350 data_t *label,
4351 int derived_type_arg,
4352 int derived_bits_arg,
4353 int derived_usage_arg,
4354 int derived_alg_arg )
4355{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004356 psa_key_handle_t base_handle = 0;
4357 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004358 psa_algorithm_t alg = alg_arg;
4359 psa_key_type_t derived_type = derived_type_arg;
4360 size_t derived_bits = derived_bits_arg;
4361 psa_key_usage_t derived_usage = derived_usage_arg;
4362 psa_algorithm_t derived_alg = derived_alg_arg;
4363 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
4364 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004365 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004366 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004367
Gilles Peskine8817f612018-12-18 00:18:46 +01004368 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004369
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004370 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4371 psa_set_key_algorithm( &attributes, alg );
4372 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
4373 PSA_ASSERT( psa_import_key( &attributes, &base_handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004374 key_data->x, key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004375
4376 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004377 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4378 salt->x, salt->len,
4379 label->x, label->len,
4380 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004381 psa_set_key_usage_flags( &attributes, derived_usage );
4382 psa_set_key_algorithm( &attributes, derived_alg );
4383 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004384 psa_set_key_bits( &attributes, derived_bits );
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004385 PSA_ASSERT( psa_generate_derived_key( &attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004386 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004387
4388 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004389 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4390 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4391 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004392
4393 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004394 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004395 goto exit;
4396
4397exit:
4398 psa_generator_abort( &generator );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004399 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004400 psa_destroy_key( base_handle );
4401 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004402 mbedtls_psa_crypto_free( );
4403}
4404/* END_CASE */
4405
4406/* BEGIN_CASE */
4407void derive_key_export( int alg_arg,
4408 data_t *key_data,
4409 data_t *salt,
4410 data_t *label,
4411 int bytes1_arg,
4412 int bytes2_arg )
4413{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004414 psa_key_handle_t base_handle = 0;
4415 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004416 psa_algorithm_t alg = alg_arg;
4417 size_t bytes1 = bytes1_arg;
4418 size_t bytes2 = bytes2_arg;
4419 size_t capacity = bytes1 + bytes2;
4420 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004421 uint8_t *output_buffer = NULL;
4422 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004423 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4424 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004425 size_t length;
4426
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004427 ASSERT_ALLOC( output_buffer, capacity );
4428 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004429 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004430
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004431 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4432 psa_set_key_algorithm( &base_attributes, alg );
4433 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4434 PSA_ASSERT( psa_import_key( &base_attributes, &base_handle,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004435 key_data->x, key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004436
4437 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004438 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4439 salt->x, salt->len,
4440 label->x, label->len,
4441 capacity ) );
4442 PSA_ASSERT( psa_generator_read( &generator,
4443 output_buffer,
4444 capacity ) );
4445 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004446
4447 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004448 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4449 salt->x, salt->len,
4450 label->x, label->len,
4451 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004452 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4453 psa_set_key_algorithm( &derived_attributes, 0 );
4454 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004455 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004456 PSA_ASSERT( psa_generate_derived_key( &derived_attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004457 &generator ) );
4458 PSA_ASSERT( psa_export_key( derived_handle,
4459 export_buffer, bytes1,
4460 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004461 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004462 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004463 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004464 PSA_ASSERT( psa_generate_derived_key( &derived_attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004465 &generator ) );
4466 PSA_ASSERT( psa_export_key( derived_handle,
4467 export_buffer + bytes1, bytes2,
4468 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004469 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004470
4471 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004472 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4473 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004474
4475exit:
4476 mbedtls_free( output_buffer );
4477 mbedtls_free( export_buffer );
4478 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004479 psa_destroy_key( base_handle );
4480 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004481 mbedtls_psa_crypto_free( );
4482}
4483/* END_CASE */
4484
4485/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004486void key_agreement_setup( int alg_arg,
4487 int our_key_type_arg, data_t *our_key_data,
4488 data_t *peer_key_data,
4489 int expected_status_arg )
4490{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004491 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004492 psa_algorithm_t alg = alg_arg;
4493 psa_key_type_t our_key_type = our_key_type_arg;
4494 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004495 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004496 psa_status_t expected_status = expected_status_arg;
4497 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004498
Gilles Peskine8817f612018-12-18 00:18:46 +01004499 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004500
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004501 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4502 psa_set_key_algorithm( &attributes, alg );
4503 psa_set_key_type( &attributes, our_key_type );
4504 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004505 our_key_data->x, our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004506
Gilles Peskine77f40d82019-04-11 21:27:06 +02004507 /* The tests currently include inputs that should fail at either step.
4508 * Test cases that fail at the setup step should be changed to call
4509 * key_derivation_setup instead, and this function should be renamed
4510 * to key_agreement_fail. */
4511 status = psa_key_derivation_setup( &generator, alg );
4512 if( status == PSA_SUCCESS )
4513 {
4514 TEST_EQUAL( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
4515 our_key,
4516 peer_key_data->x, peer_key_data->len ),
4517 expected_status );
4518 }
4519 else
4520 {
4521 TEST_ASSERT( status == expected_status );
4522 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004523
4524exit:
4525 psa_generator_abort( &generator );
4526 psa_destroy_key( our_key );
4527 mbedtls_psa_crypto_free( );
4528}
4529/* END_CASE */
4530
4531/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004532void raw_key_agreement( int alg_arg,
4533 int our_key_type_arg, data_t *our_key_data,
4534 data_t *peer_key_data,
4535 data_t *expected_output )
4536{
4537 psa_key_handle_t our_key = 0;
4538 psa_algorithm_t alg = alg_arg;
4539 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004540 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004541 unsigned char *output = NULL;
4542 size_t output_length = ~0;
4543
4544 ASSERT_ALLOC( output, expected_output->len );
4545 PSA_ASSERT( psa_crypto_init( ) );
4546
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004547 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4548 psa_set_key_algorithm( &attributes, alg );
4549 psa_set_key_type( &attributes, our_key_type );
4550 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004551 our_key_data->x, our_key_data->len ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004552
4553 PSA_ASSERT( psa_key_agreement_raw_shared_secret(
4554 alg, our_key,
4555 peer_key_data->x, peer_key_data->len,
4556 output, expected_output->len, &output_length ) );
4557 ASSERT_COMPARE( output, output_length,
4558 expected_output->x, expected_output->len );
4559
4560exit:
4561 mbedtls_free( output );
4562 psa_destroy_key( our_key );
4563 mbedtls_psa_crypto_free( );
4564}
4565/* END_CASE */
4566
4567/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004568void key_agreement_capacity( int alg_arg,
4569 int our_key_type_arg, data_t *our_key_data,
4570 data_t *peer_key_data,
4571 int expected_capacity_arg )
4572{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004573 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004574 psa_algorithm_t alg = alg_arg;
4575 psa_key_type_t our_key_type = our_key_type_arg;
4576 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004577 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004578 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004579 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004580
Gilles Peskine8817f612018-12-18 00:18:46 +01004581 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004582
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004583 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4584 psa_set_key_algorithm( &attributes, alg );
4585 psa_set_key_type( &attributes, our_key_type );
4586 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004587 our_key_data->x, our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004588
Gilles Peskine969c5d62019-01-16 15:53:06 +01004589 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4590 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004591 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004592 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004593 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4594 {
4595 /* The test data is for info="" */
4596 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4597 PSA_KDF_STEP_INFO,
4598 NULL, 0 ) );
4599 }
Gilles Peskine59685592018-09-18 12:11:34 +02004600
Gilles Peskinebf491972018-10-25 22:36:12 +02004601 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004602 PSA_ASSERT( psa_get_generator_capacity(
4603 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004604 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004605
Gilles Peskinebf491972018-10-25 22:36:12 +02004606 /* Test the actual capacity by reading the output. */
4607 while( actual_capacity > sizeof( output ) )
4608 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004609 PSA_ASSERT( psa_generator_read( &generator,
4610 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004611 actual_capacity -= sizeof( output );
4612 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004613 PSA_ASSERT( psa_generator_read( &generator,
4614 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004615 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004616 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004617
Gilles Peskine59685592018-09-18 12:11:34 +02004618exit:
4619 psa_generator_abort( &generator );
4620 psa_destroy_key( our_key );
4621 mbedtls_psa_crypto_free( );
4622}
4623/* END_CASE */
4624
4625/* BEGIN_CASE */
4626void key_agreement_output( int alg_arg,
4627 int our_key_type_arg, data_t *our_key_data,
4628 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004629 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004630{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004631 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004632 psa_algorithm_t alg = alg_arg;
4633 psa_key_type_t our_key_type = our_key_type_arg;
4634 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004635 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004636 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004637
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004638 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4639 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004640
Gilles Peskine8817f612018-12-18 00:18:46 +01004641 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004642
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004643 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4644 psa_set_key_algorithm( &attributes, alg );
4645 psa_set_key_type( &attributes, our_key_type );
4646 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004647 our_key_data->x, our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004648
Gilles Peskine969c5d62019-01-16 15:53:06 +01004649 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4650 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004651 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004652 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004653 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4654 {
4655 /* The test data is for info="" */
4656 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4657 PSA_KDF_STEP_INFO,
4658 NULL, 0 ) );
4659 }
Gilles Peskine59685592018-09-18 12:11:34 +02004660
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004661 PSA_ASSERT( psa_generator_read( &generator,
4662 actual_output,
4663 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004664 ASSERT_COMPARE( actual_output, expected_output1->len,
4665 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004666 if( expected_output2->len != 0 )
4667 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004668 PSA_ASSERT( psa_generator_read( &generator,
4669 actual_output,
4670 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004671 ASSERT_COMPARE( actual_output, expected_output2->len,
4672 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004673 }
Gilles Peskine59685592018-09-18 12:11:34 +02004674
4675exit:
4676 psa_generator_abort( &generator );
4677 psa_destroy_key( our_key );
4678 mbedtls_psa_crypto_free( );
4679 mbedtls_free( actual_output );
4680}
4681/* END_CASE */
4682
4683/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004684void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004685{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004686 size_t bytes = bytes_arg;
4687 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004688 unsigned char *output = NULL;
4689 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004690 size_t i;
4691 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004692
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004693 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4694 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004695 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004696
Gilles Peskine8817f612018-12-18 00:18:46 +01004697 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004698
Gilles Peskinea50d7392018-06-21 10:22:13 +02004699 /* Run several times, to ensure that every output byte will be
4700 * nonzero at least once with overwhelming probability
4701 * (2^(-8*number_of_runs)). */
4702 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004703 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004704 if( bytes != 0 )
4705 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004706 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004707
4708 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004709 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4710 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004711
4712 for( i = 0; i < bytes; i++ )
4713 {
4714 if( output[i] != 0 )
4715 ++changed[i];
4716 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004717 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004718
4719 /* Check that every byte was changed to nonzero at least once. This
4720 * validates that psa_generate_random is overwriting every byte of
4721 * the output buffer. */
4722 for( i = 0; i < bytes; i++ )
4723 {
4724 TEST_ASSERT( changed[i] != 0 );
4725 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004726
4727exit:
4728 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004729 mbedtls_free( output );
4730 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004731}
4732/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004733
4734/* BEGIN_CASE */
4735void generate_key( int type_arg,
4736 int bits_arg,
4737 int usage_arg,
4738 int alg_arg,
4739 int expected_status_arg )
4740{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004741 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004742 psa_key_type_t type = type_arg;
4743 psa_key_usage_t usage = usage_arg;
4744 size_t bits = bits_arg;
4745 psa_algorithm_t alg = alg_arg;
4746 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004747 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004748 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004749
Gilles Peskine8817f612018-12-18 00:18:46 +01004750 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004751
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004752 psa_set_key_usage_flags( &attributes, usage );
4753 psa_set_key_algorithm( &attributes, alg );
4754 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004755 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004756
4757 /* Generate a key */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004758 TEST_EQUAL( psa_generate_random_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004759 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004760 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004761
4762 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004763 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
4764 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4765 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004766
Gilles Peskine818ca122018-06-20 18:16:48 +02004767 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004768 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004769 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004770
4771exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004772 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004773 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004774 mbedtls_psa_crypto_free( );
4775}
4776/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004777
Gilles Peskinee56e8782019-04-26 17:34:02 +02004778/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
4779void generate_key_rsa( int bits_arg,
4780 data_t *e_arg,
4781 int expected_status_arg )
4782{
4783 psa_key_handle_t handle = 0;
4784 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEYPAIR;
4785 size_t bits = bits_arg;
4786 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4787 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4788 psa_status_t expected_status = expected_status_arg;
4789 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4790 uint8_t *exported = NULL;
4791 size_t exported_size =
4792 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
4793 size_t exported_length = SIZE_MAX;
4794 uint8_t *e_read_buffer = NULL;
4795 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004796 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004797 size_t e_read_length = SIZE_MAX;
4798
4799 if( e_arg->len == 0 ||
4800 ( e_arg->len == 3 &&
4801 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4802 {
4803 is_default_public_exponent = 1;
4804 e_read_size = 0;
4805 }
4806 ASSERT_ALLOC( e_read_buffer, e_read_size );
4807 ASSERT_ALLOC( exported, exported_size );
4808
4809 PSA_ASSERT( psa_crypto_init( ) );
4810
4811 psa_set_key_usage_flags( &attributes, usage );
4812 psa_set_key_algorithm( &attributes, alg );
4813 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4814 e_arg->x, e_arg->len ) );
4815 psa_set_key_bits( &attributes, bits );
4816
4817 /* Generate a key */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004818 TEST_EQUAL( psa_generate_random_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004819 if( expected_status != PSA_SUCCESS )
4820 goto exit;
4821
4822 /* Test the key information */
4823 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4824 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4825 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4826 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4827 e_read_buffer, e_read_size,
4828 &e_read_length ) );
4829 if( is_default_public_exponent )
4830 TEST_EQUAL( e_read_length, 0 );
4831 else
4832 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4833
4834 /* Do something with the key according to its type and permitted usage. */
4835 if( ! exercise_key( handle, usage, alg ) )
4836 goto exit;
4837
4838 /* Export the key and check the public exponent. */
4839 PSA_ASSERT( psa_export_public_key( handle,
4840 exported, exported_size,
4841 &exported_length ) );
4842 {
4843 uint8_t *p = exported;
4844 uint8_t *end = exported + exported_length;
4845 size_t len;
4846 /* RSAPublicKey ::= SEQUENCE {
4847 * modulus INTEGER, -- n
4848 * publicExponent INTEGER } -- e
4849 */
4850 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4851 MBEDTLS_ASN1_SEQUENCE |
4852 MBEDTLS_ASN1_CONSTRUCTED ) );
4853 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
4854 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4855 MBEDTLS_ASN1_INTEGER ) );
4856 if( len >= 1 && p[0] == 0 )
4857 {
4858 ++p;
4859 --len;
4860 }
4861 if( e_arg->len == 0 )
4862 {
4863 TEST_EQUAL( len, 3 );
4864 TEST_EQUAL( p[0], 1 );
4865 TEST_EQUAL( p[1], 0 );
4866 TEST_EQUAL( p[2], 1 );
4867 }
4868 else
4869 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4870 }
4871
4872exit:
4873 psa_reset_key_attributes( &attributes );
4874 psa_destroy_key( handle );
4875 mbedtls_psa_crypto_free( );
4876 mbedtls_free( e_read_buffer );
4877 mbedtls_free( exported );
4878}
4879/* END_CASE */
4880
Darryl Greend49a4992018-06-18 17:27:26 +01004881/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004882void persistent_key_load_key_from_storage( data_t *data,
4883 int type_arg, int bits_arg,
4884 int usage_flags_arg, int alg_arg,
4885 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004886{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004887 psa_key_id_t key_id = 1;
4888 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004889 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004890 psa_key_handle_t base_key = 0;
4891 psa_key_type_t type = type_arg;
4892 size_t bits = bits_arg;
4893 psa_key_usage_t usage_flags = usage_flags_arg;
4894 psa_algorithm_t alg = alg_arg;
Darryl Green0c6575a2018-11-07 16:05:30 +00004895 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004896 unsigned char *first_export = NULL;
4897 unsigned char *second_export = NULL;
4898 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4899 size_t first_exported_length;
4900 size_t second_exported_length;
4901
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004902 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4903 {
4904 ASSERT_ALLOC( first_export, export_size );
4905 ASSERT_ALLOC( second_export, export_size );
4906 }
Darryl Greend49a4992018-06-18 17:27:26 +01004907
Gilles Peskine8817f612018-12-18 00:18:46 +01004908 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004909
Gilles Peskinec87af662019-05-15 16:12:22 +02004910 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004911 psa_set_key_usage_flags( &attributes, usage_flags );
4912 psa_set_key_algorithm( &attributes, alg );
4913 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004914 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004915
Darryl Green0c6575a2018-11-07 16:05:30 +00004916 switch( generation_method )
4917 {
4918 case IMPORT_KEY:
4919 /* Import the key */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004920 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004921 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004922 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004923
Darryl Green0c6575a2018-11-07 16:05:30 +00004924 case GENERATE_KEY:
4925 /* Generate a key */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004926 PSA_ASSERT( psa_generate_random_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004927 break;
4928
4929 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004930 {
4931 /* Create base key */
4932 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
4933 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4934 psa_set_key_usage_flags( &base_attributes,
4935 PSA_KEY_USAGE_DERIVE );
4936 psa_set_key_algorithm( &base_attributes, derive_alg );
4937 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4938 PSA_ASSERT( psa_import_key( &base_attributes, &base_key,
4939 data->x, data->len ) );
4940 /* Derive a key. */
4941 PSA_ASSERT( psa_key_derivation_setup( &generator, derive_alg ) );
4942 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4943 PSA_KDF_STEP_SECRET,
4944 base_key ) );
4945 PSA_ASSERT( psa_key_derivation_input_bytes(
4946 &generator, PSA_KDF_STEP_INFO,
4947 NULL, 0 ) );
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004948 PSA_ASSERT( psa_generate_derived_key( &attributes, &handle,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004949 &generator ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004950 PSA_ASSERT( psa_generator_abort( &generator ) );
4951 PSA_ASSERT( psa_destroy_key( base_key ) );
4952 base_key = 0;
4953 }
Darryl Green0c6575a2018-11-07 16:05:30 +00004954 break;
4955 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004956 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004957
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004958 /* Export the key if permitted by the key policy. */
4959 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4960 {
4961 PSA_ASSERT( psa_export_key( handle,
4962 first_export, export_size,
4963 &first_exported_length ) );
4964 if( generation_method == IMPORT_KEY )
4965 ASSERT_COMPARE( data->x, data->len,
4966 first_export, first_exported_length );
4967 }
Darryl Greend49a4992018-06-18 17:27:26 +01004968
4969 /* Shutdown and restart */
4970 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004971 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004972
Darryl Greend49a4992018-06-18 17:27:26 +01004973 /* Check key slot still contains key data */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004974 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
Gilles Peskine8817f612018-12-18 00:18:46 +01004975 &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004976 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4977 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
4978 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
4979 PSA_KEY_LIFETIME_PERSISTENT );
4980 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4981 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4982 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
4983 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004984
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004985 /* Export the key again if permitted by the key policy. */
4986 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00004987 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004988 PSA_ASSERT( psa_export_key( handle,
4989 second_export, export_size,
4990 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004991 ASSERT_COMPARE( first_export, first_exported_length,
4992 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00004993 }
4994
4995 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004996 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004997 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004998
4999exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005000 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005001 mbedtls_free( first_export );
5002 mbedtls_free( second_export );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005003 psa_generator_abort( &generator );
5004 psa_destroy_key( base_key );
5005 if( handle == 0 )
5006 {
5007 /* In case there was a test failure after creating the persistent key
5008 * but while it was not open, try to re-open the persistent key
5009 * to delete it. */
5010 psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id, &handle );
5011 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005012 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01005013 mbedtls_psa_crypto_free();
5014}
5015/* END_CASE */