blob: ddcbd8a35fdca51c6fd76a05fd90e55ddb7e296f [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;
214 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
215
216 PSA_ASSERT( psa_allocate_key( &handle ) );
217 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
218 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine87a5e562019-04-17 12:28:25 +0200219 PSA_ASSERT( psa_import_key_to_handle( handle, key_type, 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;
248 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
249
250 PSA_ASSERT( psa_allocate_key( &handle ) );
251 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
252 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine87a5e562019-04-17 12:28:25 +0200253 PSA_ASSERT( psa_import_key_to_handle( handle, key_type, 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 );
601 return( status );
602}
603
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200604/* We need two keys to exercise key agreement. Exercise the
605 * private key against its own public key. */
606static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
607 psa_key_handle_t handle )
608{
609 psa_key_type_t private_key_type;
610 psa_key_type_t public_key_type;
611 size_t key_bits;
612 uint8_t *public_key = NULL;
613 size_t public_key_length;
614 uint8_t output[1024];
615 size_t output_length;
616 /* Return GENERIC_ERROR if something other than the final call to
617 * psa_key_agreement fails. This isn't fully satisfactory, but it's
618 * good enough: callers will report it as a failed test anyway. */
619 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200620 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200621
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200622 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
623 private_key_type = psa_get_key_type( &attributes );
624 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200625 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
626 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
627 ASSERT_ALLOC( public_key, public_key_length );
628 PSA_ASSERT( psa_export_public_key( handle,
629 public_key, public_key_length,
630 &public_key_length ) );
631
632 status = psa_key_agreement_raw_shared_secret(
633 alg, handle,
634 public_key, public_key_length,
635 output, sizeof( output ), &output_length );
636exit:
637 mbedtls_free( public_key );
638 return( status );
639}
640
641static int exercise_raw_key_agreement_key( psa_key_handle_t handle,
642 psa_key_usage_t usage,
643 psa_algorithm_t alg )
644{
645 int ok = 0;
646
647 if( usage & PSA_KEY_USAGE_DERIVE )
648 {
649 /* We need two keys to exercise key agreement. Exercise the
650 * private key against its own public key. */
651 PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) );
652 }
653 ok = 1;
654
655exit:
656 return( ok );
657}
658
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100659static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200660 psa_key_usage_t usage,
661 psa_algorithm_t alg )
662{
663 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200664 unsigned char output[1];
665 int ok = 0;
666
667 if( usage & PSA_KEY_USAGE_DERIVE )
668 {
669 /* We need two keys to exercise key agreement. Exercise the
670 * private key against its own public key. */
Gilles Peskine969c5d62019-01-16 15:53:06 +0100671 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
672 PSA_ASSERT( key_agreement_with_self( &generator, handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100673 PSA_ASSERT( psa_generator_read( &generator,
674 output,
675 sizeof( output ) ) );
676 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200677 }
678 ok = 1;
679
680exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200681 return( ok );
682}
683
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200684static int is_oid_of_key_type( psa_key_type_t type,
685 const uint8_t *oid, size_t oid_length )
686{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200687 const uint8_t *expected_oid = NULL;
688 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200689#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200690 if( PSA_KEY_TYPE_IS_RSA( type ) )
691 {
692 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
693 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
694 }
695 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200696#endif /* MBEDTLS_RSA_C */
697#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200698 if( PSA_KEY_TYPE_IS_ECC( type ) )
699 {
700 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
701 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
702 }
703 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200704#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200705 {
706 char message[40];
707 mbedtls_snprintf( message, sizeof( message ),
708 "OID not known for key type=0x%08lx",
709 (unsigned long) type );
710 test_fail( message, __LINE__, __FILE__ );
711 return( 0 );
712 }
713
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200714 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200715 return( 1 );
716
717exit:
718 return( 0 );
719}
720
721static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
722 size_t min_bits, size_t max_bits,
723 int must_be_odd )
724{
725 size_t len;
726 size_t actual_bits;
727 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100728 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100729 MBEDTLS_ASN1_INTEGER ),
730 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200731 /* Tolerate a slight departure from DER encoding:
732 * - 0 may be represented by an empty string or a 1-byte string.
733 * - The sign bit may be used as a value bit. */
734 if( ( len == 1 && ( *p )[0] == 0 ) ||
735 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
736 {
737 ++( *p );
738 --len;
739 }
740 if( min_bits == 0 && len == 0 )
741 return( 1 );
742 msb = ( *p )[0];
743 TEST_ASSERT( msb != 0 );
744 actual_bits = 8 * ( len - 1 );
745 while( msb != 0 )
746 {
747 msb >>= 1;
748 ++actual_bits;
749 }
750 TEST_ASSERT( actual_bits >= min_bits );
751 TEST_ASSERT( actual_bits <= max_bits );
752 if( must_be_odd )
753 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
754 *p += len;
755 return( 1 );
756exit:
757 return( 0 );
758}
759
760static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
761 size_t *len,
762 unsigned char n, unsigned char tag )
763{
764 int ret;
765 ret = mbedtls_asn1_get_tag( p, end, len,
766 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
767 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
768 if( ret != 0 )
769 return( ret );
770 end = *p + *len;
771 ret = mbedtls_asn1_get_tag( p, end, len, tag );
772 if( ret != 0 )
773 return( ret );
774 if( *p + *len != end )
775 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
776 return( 0 );
777}
778
779static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
780 uint8_t *exported, size_t exported_length )
781{
782 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100783 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200784 else
785 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200786
787#if defined(MBEDTLS_DES_C)
788 if( type == PSA_KEY_TYPE_DES )
789 {
790 /* Check the parity bits. */
791 unsigned i;
792 for( i = 0; i < bits / 8; i++ )
793 {
794 unsigned bit_count = 0;
795 unsigned m;
796 for( m = 1; m <= 0x100; m <<= 1 )
797 {
798 if( exported[i] & m )
799 ++bit_count;
800 }
801 TEST_ASSERT( bit_count % 2 != 0 );
802 }
803 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200804 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200805#endif
806
807#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
808 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
809 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200810 uint8_t *p = exported;
811 uint8_t *end = exported + exported_length;
812 size_t len;
813 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200814 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200815 * modulus INTEGER, -- n
816 * publicExponent INTEGER, -- e
817 * privateExponent INTEGER, -- d
818 * prime1 INTEGER, -- p
819 * prime2 INTEGER, -- q
820 * exponent1 INTEGER, -- d mod (p-1)
821 * exponent2 INTEGER, -- d mod (q-1)
822 * coefficient INTEGER, -- (inverse of q) mod p
823 * }
824 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100825 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
826 MBEDTLS_ASN1_SEQUENCE |
827 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
828 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200829 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
830 goto exit;
831 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
832 goto exit;
833 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
834 goto exit;
835 /* Require d to be at least half the size of n. */
836 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
837 goto exit;
838 /* Require p and q to be at most half the size of n, rounded up. */
839 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
840 goto exit;
841 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
842 goto exit;
843 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
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;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100849 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100850 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200851 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200852#endif /* MBEDTLS_RSA_C */
853
854#if defined(MBEDTLS_ECP_C)
855 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
856 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100857 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100858 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100859 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200860 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200861#endif /* MBEDTLS_ECP_C */
862
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200863 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
864 {
865 uint8_t *p = exported;
866 uint8_t *end = exported + exported_length;
867 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200868#if defined(MBEDTLS_RSA_C)
869 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
870 {
871 /* RSAPublicKey ::= SEQUENCE {
872 * modulus INTEGER, -- n
873 * publicExponent INTEGER } -- e
874 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100875 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
876 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100877 MBEDTLS_ASN1_CONSTRUCTED ),
878 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100879 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200880 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
881 goto exit;
882 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
883 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100884 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200885 }
886 else
887#endif /* MBEDTLS_RSA_C */
888#if defined(MBEDTLS_ECP_C)
889 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
890 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000891 /* The representation of an ECC public key is:
892 * - The byte 0x04;
893 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
894 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
895 * - where m is the bit size associated with the curve.
Jaeden Amero6b196002019-01-10 10:23:21 +0000896 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100897 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
898 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200899 }
900 else
901#endif /* MBEDTLS_ECP_C */
902 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100903 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200904 mbedtls_snprintf( message, sizeof( message ),
905 "No sanity check for public key type=0x%08lx",
906 (unsigned long) type );
907 test_fail( message, __LINE__, __FILE__ );
908 return( 0 );
909 }
910 }
911 else
912
913 {
914 /* No sanity checks for other types */
915 }
916
917 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200918
919exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200920 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200921}
922
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100923static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200924 psa_key_usage_t usage )
925{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200926 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200927 uint8_t *exported = NULL;
928 size_t exported_size = 0;
929 size_t exported_length = 0;
930 int ok = 0;
931
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200932 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200933
934 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200935 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200936 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100937 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
938 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200939 return( 1 );
940 }
941
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200942 exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ),
943 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200944 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200945
Gilles Peskine8817f612018-12-18 00:18:46 +0100946 PSA_ASSERT( psa_export_key( handle,
947 exported, exported_size,
948 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200949 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
950 psa_get_key_bits( &attributes ),
951 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +0200952
953exit:
954 mbedtls_free( exported );
955 return( ok );
956}
957
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100958static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200959{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200960 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200961 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +0200962 uint8_t *exported = NULL;
963 size_t exported_size = 0;
964 size_t exported_length = 0;
965 int ok = 0;
966
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200967 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
968 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200969 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100970 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100971 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200972 return( 1 );
973 }
974
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200975 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(
976 psa_get_key_type( &attributes ) );
977 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type,
978 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200979 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200980
Gilles Peskine8817f612018-12-18 00:18:46 +0100981 PSA_ASSERT( psa_export_public_key( handle,
982 exported, exported_size,
983 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200984 ok = exported_key_sanity_check( public_type,
985 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +0200986 exported, exported_length );
987
988exit:
989 mbedtls_free( exported );
990 return( ok );
991}
992
Gilles Peskinec9516fb2019-02-05 20:32:06 +0100993/** Do smoke tests on a key.
994 *
995 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
996 * sign/verify, or derivation) that is permitted according to \p usage.
997 * \p usage and \p alg should correspond to the expected policy on the
998 * key.
999 *
1000 * Export the key if permitted by \p usage, and check that the output
1001 * looks sensible. If \p usage forbids export, check that
1002 * \p psa_export_key correctly rejects the attempt. If the key is
1003 * asymmetric, also check \p psa_export_public_key.
1004 *
1005 * If the key fails the tests, this function calls the test framework's
1006 * `test_fail` function and returns false. Otherwise this function returns
1007 * true. Therefore it should be used as follows:
1008 * ```
1009 * if( ! exercise_key( ... ) ) goto exit;
1010 * ```
1011 *
1012 * \param handle The key to exercise. It should be capable of performing
1013 * \p alg.
1014 * \param usage The usage flags to assume.
1015 * \param alg The algorithm to exercise.
1016 *
1017 * \retval 0 The key failed the smoke tests.
1018 * \retval 1 The key passed the smoke tests.
1019 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001020static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +02001021 psa_key_usage_t usage,
1022 psa_algorithm_t alg )
1023{
1024 int ok;
1025 if( alg == 0 )
1026 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1027 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001028 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001029 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001030 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001031 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001032 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001033 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001034 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001035 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001036 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001037 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001038 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001039 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
1040 ok = exercise_raw_key_agreement_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001041 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001042 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001043 else
1044 {
1045 char message[40];
1046 mbedtls_snprintf( message, sizeof( message ),
1047 "No code to exercise alg=0x%08lx",
1048 (unsigned long) alg );
1049 test_fail( message, __LINE__, __FILE__ );
1050 ok = 0;
1051 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001052
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001053 ok = ok && exercise_export_key( handle, usage );
1054 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +02001055
Gilles Peskine02b75072018-07-01 22:31:34 +02001056 return( ok );
1057}
1058
Gilles Peskine10df3412018-10-25 22:35:43 +02001059static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1060 psa_algorithm_t alg )
1061{
1062 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1063 {
1064 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1065 PSA_KEY_USAGE_VERIFY :
1066 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
1067 }
1068 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1069 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1070 {
1071 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1072 PSA_KEY_USAGE_ENCRYPT :
1073 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1074 }
1075 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1076 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1077 {
1078 return( PSA_KEY_USAGE_DERIVE );
1079 }
1080 else
1081 {
1082 return( 0 );
1083 }
1084
1085}
Darryl Green0c6575a2018-11-07 16:05:30 +00001086
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001087/* An overapproximation of the amount of storage needed for a key of the
1088 * given type and with the given content. The API doesn't make it easy
1089 * to find a good value for the size. The current implementation doesn't
1090 * care about the value anyway. */
1091#define KEY_BITS_FROM_DATA( type, data ) \
1092 ( data )->len
1093
Darryl Green0c6575a2018-11-07 16:05:30 +00001094typedef enum {
1095 IMPORT_KEY = 0,
1096 GENERATE_KEY = 1,
1097 DERIVE_KEY = 2
1098} generate_method;
1099
Gilles Peskinee59236f2018-01-27 23:32:46 +01001100/* END_HEADER */
1101
1102/* BEGIN_DEPENDENCIES
1103 * depends_on:MBEDTLS_PSA_CRYPTO_C
1104 * END_DEPENDENCIES
1105 */
1106
1107/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001108void static_checks( )
1109{
1110 size_t max_truncated_mac_size =
1111 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1112
1113 /* Check that the length for a truncated MAC always fits in the algorithm
1114 * encoding. The shifted mask is the maximum truncated value. The
1115 * untruncated algorithm may be one byte larger. */
1116 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
1117}
1118/* END_CASE */
1119
1120/* BEGIN_CASE */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001121void attributes_set_get( int id_arg, int lifetime_arg,
1122 int usage_flags_arg, int alg_arg,
1123 int type_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001124{
Gilles Peskine4747d192019-04-17 15:05:45 +02001125 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001126 psa_key_id_t id = id_arg;
1127 psa_key_lifetime_t lifetime = lifetime_arg;
1128 psa_key_usage_t usage_flags = usage_flags_arg;
1129 psa_algorithm_t alg = alg_arg;
1130 psa_key_type_t type = type_arg;
1131
1132 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1133 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1134 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1135 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1136 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1137
1138 psa_make_key_persistent( &attributes, id, lifetime );
1139 psa_set_key_usage_flags( &attributes, usage_flags );
1140 psa_set_key_algorithm( &attributes, alg );
1141 psa_set_key_type( &attributes, type );
1142
1143 TEST_EQUAL( psa_get_key_id( &attributes ), id );
1144 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1145 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1146 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1147 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1148
1149 psa_reset_key_attributes( &attributes );
1150
1151 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1152 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1153 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1154 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1155 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1156}
1157/* END_CASE */
1158
1159/* BEGIN_CASE */
1160void import( data_t *data, int type_arg, int expected_status_arg )
1161{
1162 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1163 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001164 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001165 psa_key_type_t type = type_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001166 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001167 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001168
Gilles Peskine8817f612018-12-18 00:18:46 +01001169 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001170
Gilles Peskine4747d192019-04-17 15:05:45 +02001171 psa_set_key_type( &attributes, type );
1172 status = psa_import_key( &attributes, &handle, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001173 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001174 if( status != PSA_SUCCESS )
1175 goto exit;
1176
1177 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1178 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1179
1180 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001181
1182exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001183 psa_destroy_key( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001184 mbedtls_psa_crypto_free( );
1185}
1186/* END_CASE */
1187
1188/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +01001189void import_twice( int alg_arg, int usage_arg,
1190 int type1_arg, data_t *data1,
1191 int expected_import1_status_arg,
1192 int type2_arg, data_t *data2,
1193 int expected_import2_status_arg )
1194{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001195 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +01001196 psa_algorithm_t alg = alg_arg;
1197 psa_key_usage_t usage = usage_arg;
1198 psa_key_type_t type1 = type1_arg;
1199 psa_status_t expected_import1_status = expected_import1_status_arg;
1200 psa_key_type_t type2 = type2_arg;
1201 psa_status_t expected_import2_status = expected_import2_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00001202 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea4261682018-12-03 11:34:01 +01001203 psa_status_t status;
1204
Gilles Peskine8817f612018-12-18 00:18:46 +01001205 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +01001206
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001207 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +01001208 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001209 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +01001210
Gilles Peskine87a5e562019-04-17 12:28:25 +02001211 status = psa_import_key_to_handle( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001212 TEST_EQUAL( status, expected_import1_status );
Gilles Peskine87a5e562019-04-17 12:28:25 +02001213 status = psa_import_key_to_handle( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001214 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +01001215
1216 if( expected_import1_status == PSA_SUCCESS ||
1217 expected_import2_status == PSA_SUCCESS )
1218 {
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001219 if( ! exercise_key( handle, usage, alg ) )
1220 goto exit;
Gilles Peskinea4261682018-12-03 11:34:01 +01001221 }
1222
1223exit:
1224 mbedtls_psa_crypto_free( );
1225}
1226/* END_CASE */
1227
1228/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001229void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1230{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001231 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001232 size_t bits = bits_arg;
1233 psa_status_t expected_status = expected_status_arg;
1234 psa_status_t status;
1235 psa_key_type_t type =
1236 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
1237 size_t buffer_size = /* Slight overapproximations */
1238 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001239 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001240 unsigned char *p;
1241 int ret;
1242 size_t length;
1243
Gilles Peskine8817f612018-12-18 00:18:46 +01001244 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001245 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001246
1247 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1248 bits, keypair ) ) >= 0 );
1249 length = ret;
1250
1251 /* Try importing the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001252 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine87a5e562019-04-17 12:28:25 +02001253 status = psa_import_key_to_handle( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001254 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001255 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001256 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001257
1258exit:
1259 mbedtls_free( buffer );
1260 mbedtls_psa_crypto_free( );
1261}
1262/* END_CASE */
1263
1264/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001265void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001266 int type_arg,
1267 int alg_arg,
1268 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001269 int expected_bits,
1270 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001271 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001272 int canonical_input )
1273{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001274 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001275 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001276 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001277 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001278 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001279 unsigned char *exported = NULL;
1280 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001281 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001282 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001283 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001284 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001285 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001286
Moran Pekercb088e72018-07-17 17:36:59 +03001287 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001288 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001289 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001290 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001291 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001292
Gilles Peskine4747d192019-04-17 15:05:45 +02001293 psa_set_key_usage_flags( &attributes, usage_arg );
1294 psa_set_key_algorithm( &attributes, alg );
1295 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001296
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001297 /* Import the key */
Gilles Peskine4747d192019-04-17 15:05:45 +02001298 PSA_ASSERT( psa_import_key( &attributes, &handle, data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001299
1300 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001301 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1302 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1303 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001304
1305 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001306 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001307 exported, export_size,
1308 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001309 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001310
1311 /* The exported length must be set by psa_export_key() to a value between 0
1312 * and export_size. On errors, the exported length must be 0. */
1313 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1314 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1315 TEST_ASSERT( exported_length <= export_size );
1316
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001317 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001318 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001319 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001320 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001321 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001322 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001323 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001324
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001325 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001326 goto exit;
1327
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001328 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001329 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001330 else
1331 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001332 psa_key_handle_t handle2;
Gilles Peskine4747d192019-04-17 15:05:45 +02001333 PSA_ASSERT( psa_import_key( &attributes, &handle2,
1334 exported, exported_length ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001335 PSA_ASSERT( psa_export_key( handle2,
1336 reexported,
1337 export_size,
1338 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001339 ASSERT_COMPARE( exported, exported_length,
1340 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001341 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001342 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001343 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001344
1345destroy:
1346 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001347 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001348 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1349 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001350
1351exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001352 mbedtls_free( exported );
1353 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001354 mbedtls_psa_crypto_free( );
1355}
1356/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001357
Moran Pekerf709f4a2018-06-06 17:26:04 +03001358/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001359void import_key_nonempty_slot( )
1360{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001361 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001362 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1363 psa_status_t status;
1364 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001365 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001366
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001367 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001368
Moran Peker28a38e62018-11-07 16:18:24 +02001369 /* Import the key */
Gilles Peskine87a5e562019-04-17 12:28:25 +02001370 PSA_ASSERT( psa_import_key_to_handle( handle, type,
Gilles Peskine8817f612018-12-18 00:18:46 +01001371 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001372
1373 /* Import the key again */
Gilles Peskine87a5e562019-04-17 12:28:25 +02001374 status = psa_import_key_to_handle( handle, type, data, sizeof( data ) );
David Saadab4ecc272019-02-14 13:48:10 +02001375 TEST_EQUAL( status, PSA_ERROR_ALREADY_EXISTS );
Moran Peker28a38e62018-11-07 16:18:24 +02001376
1377exit:
1378 mbedtls_psa_crypto_free( );
1379}
1380/* END_CASE */
1381
1382/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001383void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001384{
1385 psa_status_t status;
1386 unsigned char *exported = NULL;
1387 size_t export_size = 0;
1388 size_t exported_length = INVALID_EXPORT_LENGTH;
1389 psa_status_t expected_export_status = expected_export_status_arg;
1390
Gilles Peskine8817f612018-12-18 00:18:46 +01001391 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001392
1393 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001394 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001395 exported, export_size,
1396 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001397 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001398
1399exit:
1400 mbedtls_psa_crypto_free( );
1401}
1402/* END_CASE */
1403
1404/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001405void export_with_no_key_activity( )
1406{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001407 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001408 psa_algorithm_t alg = PSA_ALG_CTR;
1409 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001410 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001411 unsigned char *exported = NULL;
1412 size_t export_size = 0;
1413 size_t exported_length = INVALID_EXPORT_LENGTH;
1414
Gilles Peskine8817f612018-12-18 00:18:46 +01001415 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001416
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001417 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001418 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001419 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001420
1421 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001422 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001423 exported, export_size,
1424 &exported_length );
David Saadab4ecc272019-02-14 13:48:10 +02001425 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Peker34550092018-11-07 16:19:34 +02001426
1427exit:
1428 mbedtls_psa_crypto_free( );
1429}
1430/* END_CASE */
1431
1432/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001433void cipher_with_no_key_activity( )
1434{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001435 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001436 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001437 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001438 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001439 int exercise_alg = PSA_ALG_CTR;
1440
Gilles Peskine8817f612018-12-18 00:18:46 +01001441 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001442
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001443 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001444 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001445 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001446
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001447 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
David Saadab4ecc272019-02-14 13:48:10 +02001448 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Pekerce500072018-11-07 16:20:07 +02001449
1450exit:
1451 psa_cipher_abort( &operation );
1452 mbedtls_psa_crypto_free( );
1453}
1454/* END_CASE */
1455
1456/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001457void export_after_import_failure( data_t *data, int type_arg,
1458 int expected_import_status_arg )
1459{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001460 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001461 psa_key_type_t type = type_arg;
1462 psa_status_t status;
1463 unsigned char *exported = NULL;
1464 size_t export_size = 0;
1465 psa_status_t expected_import_status = expected_import_status_arg;
1466 size_t exported_length = INVALID_EXPORT_LENGTH;
1467
Gilles Peskine8817f612018-12-18 00:18:46 +01001468 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001469
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001470 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001471
Moran Peker34550092018-11-07 16:19:34 +02001472 /* Import the key - expect failure */
Gilles Peskine87a5e562019-04-17 12:28:25 +02001473 status = psa_import_key_to_handle( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001474 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001475 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001476
1477 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001478 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001479 exported, export_size,
1480 &exported_length );
David Saadab4ecc272019-02-14 13:48:10 +02001481 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Peker34550092018-11-07 16:19:34 +02001482
1483exit:
1484 mbedtls_psa_crypto_free( );
1485}
1486/* END_CASE */
1487
1488/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001489void cipher_after_import_failure( data_t *data, int type_arg,
1490 int expected_import_status_arg )
1491{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001492 psa_key_handle_t handle = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001493 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001494 psa_key_type_t type = type_arg;
1495 psa_status_t status;
1496 psa_status_t expected_import_status = expected_import_status_arg;
1497 int exercise_alg = PSA_ALG_CTR;
1498
Gilles Peskine8817f612018-12-18 00:18:46 +01001499 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001500
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001501 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001502
Moran Pekerce500072018-11-07 16:20:07 +02001503 /* Import the key - expect failure */
Gilles Peskine87a5e562019-04-17 12:28:25 +02001504 status = psa_import_key_to_handle( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001505 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001506 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001507
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001508 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
David Saadab4ecc272019-02-14 13:48:10 +02001509 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Pekerce500072018-11-07 16:20:07 +02001510
1511exit:
1512 psa_cipher_abort( &operation );
1513 mbedtls_psa_crypto_free( );
1514}
1515/* END_CASE */
1516
1517/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001518void export_after_destroy_key( data_t *data, int type_arg )
1519{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001520 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001521 psa_key_type_t type = type_arg;
1522 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001523 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001524 psa_algorithm_t alg = PSA_ALG_CTR;
1525 unsigned char *exported = NULL;
1526 size_t export_size = 0;
1527 size_t exported_length = INVALID_EXPORT_LENGTH;
1528
Gilles Peskine8817f612018-12-18 00:18:46 +01001529 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001530
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001531 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001532 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001533 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001534 export_size = (ptrdiff_t) data->len;
1535 ASSERT_ALLOC( exported, export_size );
1536
1537 /* Import the key */
Gilles Peskine87a5e562019-04-17 12:28:25 +02001538 PSA_ASSERT( psa_import_key_to_handle( handle, type,
Gilles Peskine8817f612018-12-18 00:18:46 +01001539 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001540
Gilles Peskine8817f612018-12-18 00:18:46 +01001541 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1542 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001543
1544 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001545 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001546
1547 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001548 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001549 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001550 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001551
1552exit:
1553 mbedtls_free( exported );
1554 mbedtls_psa_crypto_free( );
1555}
1556/* END_CASE */
1557
1558/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001559void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001560 int type_arg,
1561 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001562 int export_size_delta,
1563 int expected_export_status_arg,
1564 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001565{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001566 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001567 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001568 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001569 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001570 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001571 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001572 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001573 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001574 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001575
Gilles Peskine8817f612018-12-18 00:18:46 +01001576 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001577
Gilles Peskine4747d192019-04-17 15:05:45 +02001578 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1579 psa_set_key_algorithm( &attributes, alg );
1580 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001581
1582 /* Import the key */
Gilles Peskine4747d192019-04-17 15:05:45 +02001583 PSA_ASSERT( psa_import_key( &attributes, &handle, data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001584
Gilles Peskine49c25912018-10-29 15:15:31 +01001585 /* Export the public key */
1586 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001587 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001588 exported, export_size,
1589 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001590 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001591 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001592 {
1593 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1594 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001595 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1596 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001597 TEST_ASSERT( expected_public_key->len <=
1598 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001599 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1600 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001601 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001602
1603exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001604 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001605 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001606 mbedtls_psa_crypto_free( );
1607}
1608/* END_CASE */
1609
Gilles Peskine20035e32018-02-03 22:44:14 +01001610/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001611void import_and_exercise_key( data_t *data,
1612 int type_arg,
1613 int bits_arg,
1614 int alg_arg )
1615{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001616 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001617 psa_key_type_t type = type_arg;
1618 size_t bits = bits_arg;
1619 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001620 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001621 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001622 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001623
Gilles Peskine8817f612018-12-18 00:18:46 +01001624 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001625
Gilles Peskine4747d192019-04-17 15:05:45 +02001626 psa_set_key_usage_flags( &attributes, usage );
1627 psa_set_key_algorithm( &attributes, alg );
1628 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001629
1630 /* Import the key */
Gilles Peskine4747d192019-04-17 15:05:45 +02001631 PSA_ASSERT( psa_import_key( &attributes, &handle, data->x, data->len ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001632
1633 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001634 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1635 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1636 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001637
1638 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001639 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001640 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001641
1642exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001643 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001644 mbedtls_psa_crypto_free( );
1645}
1646/* END_CASE */
1647
1648/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001649void key_policy( int usage_arg, int alg_arg )
1650{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001651 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001652 psa_algorithm_t alg = alg_arg;
1653 psa_key_usage_t usage = usage_arg;
1654 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1655 unsigned char key[32] = {0};
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001656 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001657
1658 memset( key, 0x2a, sizeof( key ) );
1659
Gilles Peskine8817f612018-12-18 00:18:46 +01001660 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001661
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001662 psa_set_key_usage_flags( &attributes, usage );
1663 psa_set_key_algorithm( &attributes, alg );
1664 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001665
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001666 PSA_ASSERT( psa_import_key( &attributes, &handle, key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001667
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001668 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1669 TEST_EQUAL( psa_get_key_type( &attributes ), key_type );
1670 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage );
1671 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001672
1673exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001674 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001675 mbedtls_psa_crypto_free( );
1676}
1677/* END_CASE */
1678
1679/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001680void key_policy_init( )
1681{
1682 /* Test each valid way of initializing the object, except for `= {0}`, as
1683 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1684 * though it's OK by the C standard. We could test for this, but we'd need
1685 * to supress the Clang warning for the test. */
1686 psa_key_policy_t func = psa_key_policy_init( );
1687 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1688 psa_key_policy_t zero;
1689
1690 memset( &zero, 0, sizeof( zero ) );
1691
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001692 /* A default key policy should not permit any usage. */
1693 TEST_EQUAL( psa_key_policy_get_usage( &func ), 0 );
1694 TEST_EQUAL( psa_key_policy_get_usage( &init ), 0 );
1695 TEST_EQUAL( psa_key_policy_get_usage( &zero ), 0 );
1696
1697 /* A default key policy should not permit any algorithm. */
1698 TEST_EQUAL( psa_key_policy_get_algorithm( &func ), 0 );
1699 TEST_EQUAL( psa_key_policy_get_algorithm( &init ), 0 );
1700 TEST_EQUAL( psa_key_policy_get_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001701}
1702/* END_CASE */
1703
1704/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001705void mac_key_policy( int policy_usage,
1706 int policy_alg,
1707 int key_type,
1708 data_t *key_data,
1709 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001710{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001711 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001712 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001713 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001714 psa_status_t status;
1715 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001716
Gilles Peskine8817f612018-12-18 00:18:46 +01001717 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001718
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001719 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001720 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001721 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001722
Gilles Peskine87a5e562019-04-17 12:28:25 +02001723 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01001724 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001725
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001726 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001727 if( policy_alg == exercise_alg &&
1728 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001729 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001730 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001731 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001732 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001733
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001734 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001735 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001736 if( policy_alg == exercise_alg &&
1737 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001738 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001739 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001740 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001741
1742exit:
1743 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001744 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001745 mbedtls_psa_crypto_free( );
1746}
1747/* END_CASE */
1748
1749/* BEGIN_CASE */
1750void cipher_key_policy( int policy_usage,
1751 int policy_alg,
1752 int key_type,
1753 data_t *key_data,
1754 int exercise_alg )
1755{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001756 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001757 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001758 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001759 psa_status_t status;
1760
Gilles Peskine8817f612018-12-18 00:18:46 +01001761 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001762
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001763 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001764 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001765 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001766
Gilles Peskine87a5e562019-04-17 12:28:25 +02001767 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01001768 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001769
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001770 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001771 if( policy_alg == exercise_alg &&
1772 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001773 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001774 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001775 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001776 psa_cipher_abort( &operation );
1777
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001778 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001779 if( policy_alg == exercise_alg &&
1780 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001781 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001782 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001783 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001784
1785exit:
1786 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001787 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001788 mbedtls_psa_crypto_free( );
1789}
1790/* END_CASE */
1791
1792/* BEGIN_CASE */
1793void aead_key_policy( int policy_usage,
1794 int policy_alg,
1795 int key_type,
1796 data_t *key_data,
1797 int nonce_length_arg,
1798 int tag_length_arg,
1799 int exercise_alg )
1800{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001801 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001802 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001803 psa_status_t status;
1804 unsigned char nonce[16] = {0};
1805 size_t nonce_length = nonce_length_arg;
1806 unsigned char tag[16];
1807 size_t tag_length = tag_length_arg;
1808 size_t output_length;
1809
1810 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1811 TEST_ASSERT( tag_length <= sizeof( tag ) );
1812
Gilles Peskine8817f612018-12-18 00:18:46 +01001813 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001814
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001815 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001816 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001817 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001818
Gilles Peskine87a5e562019-04-17 12:28:25 +02001819 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01001820 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001821
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001822 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001823 nonce, nonce_length,
1824 NULL, 0,
1825 NULL, 0,
1826 tag, tag_length,
1827 &output_length );
1828 if( policy_alg == exercise_alg &&
1829 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001830 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001831 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001832 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001833
1834 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001835 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001836 nonce, nonce_length,
1837 NULL, 0,
1838 tag, tag_length,
1839 NULL, 0,
1840 &output_length );
1841 if( policy_alg == exercise_alg &&
1842 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001843 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001844 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001845 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001846
1847exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001848 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001849 mbedtls_psa_crypto_free( );
1850}
1851/* END_CASE */
1852
1853/* BEGIN_CASE */
1854void asymmetric_encryption_key_policy( int policy_usage,
1855 int policy_alg,
1856 int key_type,
1857 data_t *key_data,
1858 int exercise_alg )
1859{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001860 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001861 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001862 psa_status_t status;
1863 size_t key_bits;
1864 size_t buffer_length;
1865 unsigned char *buffer = NULL;
1866 size_t output_length;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001867 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001868
Gilles Peskine8817f612018-12-18 00:18:46 +01001869 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001870
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001871 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001872 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001873 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001874
Gilles Peskine87a5e562019-04-17 12:28:25 +02001875 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01001876 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001877
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001878 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1879 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001880 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1881 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001882 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001883
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001884 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001885 NULL, 0,
1886 NULL, 0,
1887 buffer, buffer_length,
1888 &output_length );
1889 if( policy_alg == exercise_alg &&
1890 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001891 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001892 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001893 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001894
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001895 if( buffer_length != 0 )
1896 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001897 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001898 buffer, buffer_length,
1899 NULL, 0,
1900 buffer, buffer_length,
1901 &output_length );
1902 if( policy_alg == exercise_alg &&
1903 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001904 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001905 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001906 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001907
1908exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001909 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001910 mbedtls_psa_crypto_free( );
1911 mbedtls_free( buffer );
1912}
1913/* END_CASE */
1914
1915/* BEGIN_CASE */
1916void asymmetric_signature_key_policy( int policy_usage,
1917 int policy_alg,
1918 int key_type,
1919 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001920 int exercise_alg,
1921 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001922{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001923 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001924 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001925 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001926 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1927 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1928 * compatible with the policy and `payload_length_arg` is supposed to be
1929 * a valid input length to sign. If `payload_length_arg <= 0`,
1930 * `exercise_alg` is supposed to be forbidden by the policy. */
1931 int compatible_alg = payload_length_arg > 0;
1932 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001933 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1934 size_t signature_length;
1935
Gilles Peskine8817f612018-12-18 00:18:46 +01001936 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001937
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001938 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001939 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001940 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001941
Gilles Peskine87a5e562019-04-17 12:28:25 +02001942 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01001943 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001944
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001945 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001946 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001947 signature, sizeof( signature ),
1948 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001949 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001950 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001951 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001952 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001953
1954 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001955 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001956 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001957 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001958 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001959 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001960 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001961 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001962
1963exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001964 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001965 mbedtls_psa_crypto_free( );
1966}
1967/* END_CASE */
1968
1969/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001970void derive_key_policy( int policy_usage,
1971 int policy_alg,
1972 int key_type,
1973 data_t *key_data,
1974 int exercise_alg )
1975{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001976 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001977 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001978 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1979 psa_status_t status;
1980
Gilles Peskine8817f612018-12-18 00:18:46 +01001981 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001982
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001983 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001984 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001985 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001986
Gilles Peskine87a5e562019-04-17 12:28:25 +02001987 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01001988 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001989
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001990 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001991 exercise_alg,
1992 NULL, 0,
1993 NULL, 0,
1994 1 );
1995 if( policy_alg == exercise_alg &&
1996 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001997 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001998 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001999 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002000
2001exit:
2002 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002003 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002004 mbedtls_psa_crypto_free( );
2005}
2006/* END_CASE */
2007
2008/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002009void agreement_key_policy( int policy_usage,
2010 int policy_alg,
2011 int key_type_arg,
2012 data_t *key_data,
2013 int exercise_alg )
2014{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002015 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00002016 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002017 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002018 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2019 psa_status_t status;
2020
Gilles Peskine8817f612018-12-18 00:18:46 +01002021 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002022
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002023 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002024 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002025 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002026
Gilles Peskine87a5e562019-04-17 12:28:25 +02002027 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01002028 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002029
Gilles Peskine969c5d62019-01-16 15:53:06 +01002030 PSA_ASSERT( psa_key_derivation_setup( &generator, exercise_alg ) );
2031 status = key_agreement_with_self( &generator, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002032
Gilles Peskine01d718c2018-09-18 12:01:02 +02002033 if( policy_alg == exercise_alg &&
2034 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002035 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002036 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002037 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002038
2039exit:
2040 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002041 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002042 mbedtls_psa_crypto_free( );
2043}
2044/* END_CASE */
2045
2046/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002047void raw_agreement_key_policy( int policy_usage,
2048 int policy_alg,
2049 int key_type_arg,
2050 data_t *key_data,
2051 int exercise_alg )
2052{
2053 psa_key_handle_t handle = 0;
2054 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2055 psa_key_type_t key_type = key_type_arg;
2056 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2057 psa_status_t status;
2058
2059 PSA_ASSERT( psa_crypto_init( ) );
2060
2061 PSA_ASSERT( psa_allocate_key( &handle ) );
2062 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
2063 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2064
Gilles Peskine87a5e562019-04-17 12:28:25 +02002065 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002066 key_data->x, key_data->len ) );
2067
2068 status = raw_key_agreement_with_self( exercise_alg, handle );
2069
2070 if( policy_alg == exercise_alg &&
2071 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
2072 PSA_ASSERT( status );
2073 else
2074 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2075
2076exit:
2077 psa_generator_abort( &generator );
2078 psa_destroy_key( handle );
2079 mbedtls_psa_crypto_free( );
2080}
2081/* END_CASE */
2082
2083/* BEGIN_CASE */
Gilles Peskine57ab7212019-01-28 13:03:09 +01002084void copy_key_policy( int source_usage_arg, int source_alg_arg,
2085 int type_arg, data_t *material,
2086 int target_usage_arg, int target_alg_arg,
2087 int constraint_usage_arg, int constraint_alg_arg,
2088 int expected_usage_arg, int expected_alg_arg )
2089{
2090 psa_key_usage_t source_usage = source_usage_arg;
2091 psa_algorithm_t source_alg = source_alg_arg;
2092 psa_key_handle_t source_handle = 0;
2093 psa_key_policy_t source_policy = PSA_KEY_POLICY_INIT;
2094 psa_key_type_t source_type = type_arg;
2095 size_t source_bits;
2096 psa_key_usage_t target_usage = target_usage_arg;
2097 psa_algorithm_t target_alg = target_alg_arg;
2098 psa_key_handle_t target_handle = 0;
2099 psa_key_policy_t target_policy = PSA_KEY_POLICY_INIT;
2100 psa_key_type_t target_type;
2101 size_t target_bits;
2102 psa_key_usage_t constraint_usage = constraint_usage_arg;
2103 psa_algorithm_t constraint_alg = constraint_alg_arg;
2104 psa_key_policy_t constraint = PSA_KEY_POLICY_INIT;
2105 psa_key_policy_t *p_constraint = NULL;
2106 psa_key_usage_t expected_usage = expected_usage_arg;
2107 psa_algorithm_t expected_alg = expected_alg_arg;
2108 uint8_t *export_buffer = NULL;
2109
2110 if( constraint_usage_arg != -1 )
2111 {
2112 p_constraint = &constraint;
2113 psa_key_policy_set_usage( p_constraint,
2114 constraint_usage, constraint_alg );
2115 }
2116
2117 PSA_ASSERT( psa_crypto_init( ) );
2118
2119 /* Populate the source slot. */
2120 PSA_ASSERT( psa_allocate_key( &source_handle ) );
2121 psa_key_policy_set_usage( &source_policy, source_usage, source_alg );
2122 PSA_ASSERT( psa_set_key_policy( source_handle, &source_policy ) );
Gilles Peskine87a5e562019-04-17 12:28:25 +02002123 PSA_ASSERT( psa_import_key_to_handle( source_handle, source_type,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002124 material->x, material->len ) );
2125 PSA_ASSERT( psa_get_key_information( source_handle, NULL, &source_bits ) );
2126
2127 /* Prepare the target slot. */
2128 PSA_ASSERT( psa_allocate_key( &target_handle ) );
2129 psa_key_policy_set_usage( &target_policy, target_usage, target_alg );
2130 PSA_ASSERT( psa_set_key_policy( target_handle, &target_policy ) );
2131 target_policy = psa_key_policy_init();
2132
2133 /* Copy the key. */
Gilles Peskine87a5e562019-04-17 12:28:25 +02002134 PSA_ASSERT( psa_copy_key_to_handle( source_handle, target_handle, p_constraint ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002135
2136 /* Destroy the source to ensure that this doesn't affect the target. */
2137 PSA_ASSERT( psa_destroy_key( source_handle ) );
2138
2139 /* Test that the target slot has the expected content and policy. */
2140 PSA_ASSERT( psa_get_key_information( target_handle,
2141 &target_type, &target_bits ) );
2142 TEST_EQUAL( source_type, target_type );
2143 TEST_EQUAL( source_bits, target_bits );
2144 PSA_ASSERT( psa_get_key_policy( target_handle, &target_policy ) );
2145 TEST_EQUAL( expected_usage, psa_key_policy_get_usage( &target_policy ) );
2146 TEST_EQUAL( expected_alg, psa_key_policy_get_algorithm( &target_policy ) );
2147 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2148 {
2149 size_t length;
2150 ASSERT_ALLOC( export_buffer, material->len );
2151 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2152 material->len, &length ) );
2153 ASSERT_COMPARE( material->x, material->len,
2154 export_buffer, length );
2155 }
2156 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2157 goto exit;
2158
2159 PSA_ASSERT( psa_close_key( target_handle ) );
2160
2161exit:
2162 mbedtls_psa_crypto_free( );
2163 mbedtls_free( export_buffer );
2164}
2165/* END_CASE */
2166
2167/* BEGIN_CASE */
2168void copy_fail( int source_usage_arg, int source_alg_arg,
2169 int type_arg, data_t *material,
2170 int target_usage_arg, int target_alg_arg,
2171 int constraint_usage_arg, int constraint_alg_arg,
2172 int expected_status_arg )
2173{
2174 /* Test copy failure into an empty slot. There is a test for copy failure
2175 * into an occupied slot in
2176 * test_suite_psa_crypto_slot_management.function. */
2177
2178 psa_key_usage_t source_usage = source_usage_arg;
2179 psa_algorithm_t source_alg = source_alg_arg;
2180 psa_key_handle_t source_handle = 0;
2181 psa_key_policy_t source_policy = PSA_KEY_POLICY_INIT;
2182 psa_key_type_t source_type = type_arg;
2183 size_t source_bits;
2184 psa_key_usage_t target_usage = target_usage_arg;
2185 psa_algorithm_t target_alg = target_alg_arg;
2186 psa_key_handle_t target_handle = 0;
2187 psa_key_policy_t target_policy = PSA_KEY_POLICY_INIT;
2188 psa_key_type_t target_type;
2189 size_t target_bits;
2190 psa_key_usage_t constraint_usage = constraint_usage_arg;
2191 psa_algorithm_t constraint_alg = constraint_alg_arg;
2192 psa_key_policy_t constraint = PSA_KEY_POLICY_INIT;
2193 psa_key_policy_t *p_constraint = NULL;
2194 psa_status_t expected_status = expected_status_arg;
2195
2196 if( constraint_usage_arg != -1 )
2197 {
2198 p_constraint = &constraint;
2199 psa_key_policy_set_usage( p_constraint,
2200 constraint_usage, constraint_alg );
2201 }
2202
2203 PSA_ASSERT( psa_crypto_init( ) );
2204
2205 /* Populate the source slot. */
2206 PSA_ASSERT( psa_allocate_key( &source_handle ) );
2207 psa_key_policy_set_usage( &source_policy, source_usage, source_alg );
2208 PSA_ASSERT( psa_set_key_policy( source_handle, &source_policy ) );
Gilles Peskine87a5e562019-04-17 12:28:25 +02002209 PSA_ASSERT( psa_import_key_to_handle( source_handle, source_type,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002210 material->x, material->len ) );
2211 PSA_ASSERT( psa_get_key_information( source_handle, NULL, &source_bits ) );
2212
2213 /* Prepare the target slot. */
2214 PSA_ASSERT( psa_allocate_key( &target_handle ) );
2215 psa_key_policy_set_usage( &target_policy, target_usage, target_alg );
2216 PSA_ASSERT( psa_set_key_policy( target_handle, &target_policy ) );
2217 target_policy = psa_key_policy_init();
2218
2219 /* Copy the key. */
Gilles Peskine87a5e562019-04-17 12:28:25 +02002220 TEST_EQUAL( psa_copy_key_to_handle( source_handle, target_handle, p_constraint ),
Gilles Peskine57ab7212019-01-28 13:03:09 +01002221 expected_status );
2222
2223 /* Test that the target slot is unaffected. */
2224 TEST_EQUAL( psa_get_key_information( target_handle,
2225 &target_type, &target_bits ),
David Saadab4ecc272019-02-14 13:48:10 +02002226 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002227 PSA_ASSERT( psa_get_key_policy( target_handle, &target_policy ) );
2228 TEST_EQUAL( target_usage, psa_key_policy_get_usage( &target_policy ) );
2229 TEST_EQUAL( target_alg, psa_key_policy_get_algorithm( &target_policy ) );
2230
2231exit:
2232 mbedtls_psa_crypto_free( );
2233}
2234/* END_CASE */
2235
2236/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002237void hash_operation_init( )
2238{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002239 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002240 /* Test each valid way of initializing the object, except for `= {0}`, as
2241 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2242 * though it's OK by the C standard. We could test for this, but we'd need
2243 * to supress the Clang warning for the test. */
2244 psa_hash_operation_t func = psa_hash_operation_init( );
2245 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2246 psa_hash_operation_t zero;
2247
2248 memset( &zero, 0, sizeof( zero ) );
2249
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002250 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002251 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2252 PSA_ERROR_BAD_STATE );
2253 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2254 PSA_ERROR_BAD_STATE );
2255 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2256 PSA_ERROR_BAD_STATE );
2257
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002258 /* A default hash operation should be abortable without error. */
2259 PSA_ASSERT( psa_hash_abort( &func ) );
2260 PSA_ASSERT( psa_hash_abort( &init ) );
2261 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002262}
2263/* END_CASE */
2264
2265/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002266void hash_setup( int alg_arg,
2267 int expected_status_arg )
2268{
2269 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002270 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002271 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002272 psa_status_t status;
2273
Gilles Peskine8817f612018-12-18 00:18:46 +01002274 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002275
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002276 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002277 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002278
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002279 /* Whether setup succeeded or failed, abort must succeed. */
2280 PSA_ASSERT( psa_hash_abort( &operation ) );
2281
2282 /* If setup failed, reproduce the failure, so as to
2283 * test the resulting state of the operation object. */
2284 if( status != PSA_SUCCESS )
2285 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2286
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002287 /* Now the operation object should be reusable. */
2288#if defined(KNOWN_SUPPORTED_HASH_ALG)
2289 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2290 PSA_ASSERT( psa_hash_abort( &operation ) );
2291#endif
2292
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002293exit:
2294 mbedtls_psa_crypto_free( );
2295}
2296/* END_CASE */
2297
2298/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002299void hash_bad_order( )
2300{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002301 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002302 unsigned char input[] = "";
2303 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002304 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002305 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2306 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2307 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002308 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002309 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002310 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002311
Gilles Peskine8817f612018-12-18 00:18:46 +01002312 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002313
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002314 /* Call setup twice in a row. */
2315 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2316 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2317 PSA_ERROR_BAD_STATE );
2318 PSA_ASSERT( psa_hash_abort( &operation ) );
2319
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002320 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002321 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002322 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002323 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002324
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002325 /* Call update after finish. */
2326 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2327 PSA_ASSERT( psa_hash_finish( &operation,
2328 hash, sizeof( hash ), &hash_len ) );
2329 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002330 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002331 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002332
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002333 /* Call verify without calling setup beforehand. */
2334 TEST_EQUAL( psa_hash_verify( &operation,
2335 valid_hash, sizeof( valid_hash ) ),
2336 PSA_ERROR_BAD_STATE );
2337 PSA_ASSERT( psa_hash_abort( &operation ) );
2338
2339 /* Call verify after finish. */
2340 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2341 PSA_ASSERT( psa_hash_finish( &operation,
2342 hash, sizeof( hash ), &hash_len ) );
2343 TEST_EQUAL( psa_hash_verify( &operation,
2344 valid_hash, sizeof( valid_hash ) ),
2345 PSA_ERROR_BAD_STATE );
2346 PSA_ASSERT( psa_hash_abort( &operation ) );
2347
2348 /* Call verify twice in a row. */
2349 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2350 PSA_ASSERT( psa_hash_verify( &operation,
2351 valid_hash, sizeof( valid_hash ) ) );
2352 TEST_EQUAL( psa_hash_verify( &operation,
2353 valid_hash, sizeof( valid_hash ) ),
2354 PSA_ERROR_BAD_STATE );
2355 PSA_ASSERT( psa_hash_abort( &operation ) );
2356
2357 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002358 TEST_EQUAL( psa_hash_finish( &operation,
2359 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002360 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002361 PSA_ASSERT( psa_hash_abort( &operation ) );
2362
2363 /* Call finish twice in a row. */
2364 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2365 PSA_ASSERT( psa_hash_finish( &operation,
2366 hash, sizeof( hash ), &hash_len ) );
2367 TEST_EQUAL( psa_hash_finish( &operation,
2368 hash, sizeof( hash ), &hash_len ),
2369 PSA_ERROR_BAD_STATE );
2370 PSA_ASSERT( psa_hash_abort( &operation ) );
2371
2372 /* Call finish after calling verify. */
2373 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2374 PSA_ASSERT( psa_hash_verify( &operation,
2375 valid_hash, sizeof( valid_hash ) ) );
2376 TEST_EQUAL( psa_hash_finish( &operation,
2377 hash, sizeof( hash ), &hash_len ),
2378 PSA_ERROR_BAD_STATE );
2379 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002380
2381exit:
2382 mbedtls_psa_crypto_free( );
2383}
2384/* END_CASE */
2385
itayzafrir27e69452018-11-01 14:26:34 +02002386/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2387void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002388{
2389 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002390 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2391 * appended to it */
2392 unsigned char hash[] = {
2393 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2394 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2395 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002396 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002397 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002398
Gilles Peskine8817f612018-12-18 00:18:46 +01002399 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002400
itayzafrir27e69452018-11-01 14:26:34 +02002401 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002402 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002403 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002404 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002405
itayzafrir27e69452018-11-01 14:26:34 +02002406 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002407 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002408 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002409 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002410
itayzafrir27e69452018-11-01 14:26:34 +02002411 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002412 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002413 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002414 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002415
itayzafrirec93d302018-10-18 18:01:10 +03002416exit:
2417 mbedtls_psa_crypto_free( );
2418}
2419/* END_CASE */
2420
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002421/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2422void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002423{
2424 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002425 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002426 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002427 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002428 size_t hash_len;
2429
Gilles Peskine8817f612018-12-18 00:18:46 +01002430 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002431
itayzafrir58028322018-10-25 10:22:01 +03002432 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002433 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002434 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002435 hash, expected_size - 1, &hash_len ),
2436 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002437
2438exit:
2439 mbedtls_psa_crypto_free( );
2440}
2441/* END_CASE */
2442
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002443/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2444void hash_clone_source_state( )
2445{
2446 psa_algorithm_t alg = PSA_ALG_SHA_256;
2447 unsigned char hash[PSA_HASH_MAX_SIZE];
2448 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2449 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2450 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2451 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2452 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2453 size_t hash_len;
2454
2455 PSA_ASSERT( psa_crypto_init( ) );
2456 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2457
2458 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2459 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2460 PSA_ASSERT( psa_hash_finish( &op_finished,
2461 hash, sizeof( hash ), &hash_len ) );
2462 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2463 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2464
2465 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2466 PSA_ERROR_BAD_STATE );
2467
2468 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2469 PSA_ASSERT( psa_hash_finish( &op_init,
2470 hash, sizeof( hash ), &hash_len ) );
2471 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2472 PSA_ASSERT( psa_hash_finish( &op_finished,
2473 hash, sizeof( hash ), &hash_len ) );
2474 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2475 PSA_ASSERT( psa_hash_finish( &op_aborted,
2476 hash, sizeof( hash ), &hash_len ) );
2477
2478exit:
2479 psa_hash_abort( &op_source );
2480 psa_hash_abort( &op_init );
2481 psa_hash_abort( &op_setup );
2482 psa_hash_abort( &op_finished );
2483 psa_hash_abort( &op_aborted );
2484 mbedtls_psa_crypto_free( );
2485}
2486/* END_CASE */
2487
2488/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2489void hash_clone_target_state( )
2490{
2491 psa_algorithm_t alg = PSA_ALG_SHA_256;
2492 unsigned char hash[PSA_HASH_MAX_SIZE];
2493 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2494 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2495 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2496 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2497 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2498 size_t hash_len;
2499
2500 PSA_ASSERT( psa_crypto_init( ) );
2501
2502 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2503 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2504 PSA_ASSERT( psa_hash_finish( &op_finished,
2505 hash, sizeof( hash ), &hash_len ) );
2506 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2507 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2508
2509 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2510 PSA_ASSERT( psa_hash_finish( &op_target,
2511 hash, sizeof( hash ), &hash_len ) );
2512
2513 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2514 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2515 PSA_ERROR_BAD_STATE );
2516 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2517 PSA_ERROR_BAD_STATE );
2518
2519exit:
2520 psa_hash_abort( &op_target );
2521 psa_hash_abort( &op_init );
2522 psa_hash_abort( &op_setup );
2523 psa_hash_abort( &op_finished );
2524 psa_hash_abort( &op_aborted );
2525 mbedtls_psa_crypto_free( );
2526}
2527/* END_CASE */
2528
itayzafrir58028322018-10-25 10:22:01 +03002529/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002530void mac_operation_init( )
2531{
Jaeden Amero252ef282019-02-15 14:05:35 +00002532 const uint8_t input[1] = { 0 };
2533
Jaeden Amero769ce272019-01-04 11:48:03 +00002534 /* Test each valid way of initializing the object, except for `= {0}`, as
2535 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2536 * though it's OK by the C standard. We could test for this, but we'd need
2537 * to supress the Clang warning for the test. */
2538 psa_mac_operation_t func = psa_mac_operation_init( );
2539 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2540 psa_mac_operation_t zero;
2541
2542 memset( &zero, 0, sizeof( zero ) );
2543
Jaeden Amero252ef282019-02-15 14:05:35 +00002544 /* A freshly-initialized MAC operation should not be usable. */
2545 TEST_EQUAL( psa_mac_update( &func,
2546 input, sizeof( input ) ),
2547 PSA_ERROR_BAD_STATE );
2548 TEST_EQUAL( psa_mac_update( &init,
2549 input, sizeof( input ) ),
2550 PSA_ERROR_BAD_STATE );
2551 TEST_EQUAL( psa_mac_update( &zero,
2552 input, sizeof( input ) ),
2553 PSA_ERROR_BAD_STATE );
2554
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002555 /* A default MAC operation should be abortable without error. */
2556 PSA_ASSERT( psa_mac_abort( &func ) );
2557 PSA_ASSERT( psa_mac_abort( &init ) );
2558 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002559}
2560/* END_CASE */
2561
2562/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002563void mac_setup( int key_type_arg,
2564 data_t *key,
2565 int alg_arg,
2566 int expected_status_arg )
2567{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002568 psa_key_type_t key_type = key_type_arg;
2569 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002570 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002571 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002572 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2573#if defined(KNOWN_SUPPORTED_MAC_ALG)
2574 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2575#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002576
Gilles Peskine8817f612018-12-18 00:18:46 +01002577 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002578
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002579 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2580 &operation, &status ) )
2581 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002582 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002583
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002584 /* The operation object should be reusable. */
2585#if defined(KNOWN_SUPPORTED_MAC_ALG)
2586 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2587 smoke_test_key_data,
2588 sizeof( smoke_test_key_data ),
2589 KNOWN_SUPPORTED_MAC_ALG,
2590 &operation, &status ) )
2591 goto exit;
2592 TEST_EQUAL( status, PSA_SUCCESS );
2593#endif
2594
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002595exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002596 mbedtls_psa_crypto_free( );
2597}
2598/* END_CASE */
2599
2600/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002601void mac_bad_order( )
2602{
2603 psa_key_handle_t handle = 0;
2604 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2605 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2606 const uint8_t key[] = {
2607 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2608 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2609 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
2610 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2611 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2612 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2613 size_t sign_mac_length = 0;
2614 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2615 const uint8_t verify_mac[] = {
2616 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2617 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2618 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2619
2620 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002621 PSA_ASSERT( psa_allocate_key( &handle ) );
2622 psa_key_policy_set_usage( &policy,
2623 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002624 alg );
2625 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2626
Gilles Peskine87a5e562019-04-17 12:28:25 +02002627 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Jaeden Amero252ef282019-02-15 14:05:35 +00002628 key, sizeof(key) ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002629
Jaeden Amero252ef282019-02-15 14:05:35 +00002630 /* Call update without calling setup beforehand. */
2631 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2632 PSA_ERROR_BAD_STATE );
2633 PSA_ASSERT( psa_mac_abort( &operation ) );
2634
2635 /* Call sign finish without calling setup beforehand. */
2636 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2637 &sign_mac_length),
2638 PSA_ERROR_BAD_STATE );
2639 PSA_ASSERT( psa_mac_abort( &operation ) );
2640
2641 /* Call verify finish without calling setup beforehand. */
2642 TEST_EQUAL( psa_mac_verify_finish( &operation,
2643 verify_mac, sizeof( verify_mac ) ),
2644 PSA_ERROR_BAD_STATE );
2645 PSA_ASSERT( psa_mac_abort( &operation ) );
2646
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002647 /* Call setup twice in a row. */
2648 PSA_ASSERT( psa_mac_sign_setup( &operation,
2649 handle, alg ) );
2650 TEST_EQUAL( psa_mac_sign_setup( &operation,
2651 handle, alg ),
2652 PSA_ERROR_BAD_STATE );
2653 PSA_ASSERT( psa_mac_abort( &operation ) );
2654
Jaeden Amero252ef282019-02-15 14:05:35 +00002655 /* Call update after sign finish. */
2656 PSA_ASSERT( psa_mac_sign_setup( &operation,
2657 handle, alg ) );
2658 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2659 PSA_ASSERT( psa_mac_sign_finish( &operation,
2660 sign_mac, sizeof( sign_mac ),
2661 &sign_mac_length ) );
2662 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2663 PSA_ERROR_BAD_STATE );
2664 PSA_ASSERT( psa_mac_abort( &operation ) );
2665
2666 /* Call update after verify finish. */
2667 PSA_ASSERT( psa_mac_verify_setup( &operation,
2668 handle, alg ) );
2669 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2670 PSA_ASSERT( psa_mac_verify_finish( &operation,
2671 verify_mac, sizeof( verify_mac ) ) );
2672 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2673 PSA_ERROR_BAD_STATE );
2674 PSA_ASSERT( psa_mac_abort( &operation ) );
2675
2676 /* Call sign finish twice in a row. */
2677 PSA_ASSERT( psa_mac_sign_setup( &operation,
2678 handle, alg ) );
2679 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2680 PSA_ASSERT( psa_mac_sign_finish( &operation,
2681 sign_mac, sizeof( sign_mac ),
2682 &sign_mac_length ) );
2683 TEST_EQUAL( psa_mac_sign_finish( &operation,
2684 sign_mac, sizeof( sign_mac ),
2685 &sign_mac_length ),
2686 PSA_ERROR_BAD_STATE );
2687 PSA_ASSERT( psa_mac_abort( &operation ) );
2688
2689 /* Call verify finish twice in a row. */
2690 PSA_ASSERT( psa_mac_verify_setup( &operation,
2691 handle, alg ) );
2692 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2693 PSA_ASSERT( psa_mac_verify_finish( &operation,
2694 verify_mac, sizeof( verify_mac ) ) );
2695 TEST_EQUAL( psa_mac_verify_finish( &operation,
2696 verify_mac, sizeof( verify_mac ) ),
2697 PSA_ERROR_BAD_STATE );
2698 PSA_ASSERT( psa_mac_abort( &operation ) );
2699
2700 /* Setup sign but try verify. */
2701 PSA_ASSERT( psa_mac_sign_setup( &operation,
2702 handle, alg ) );
2703 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2704 TEST_EQUAL( psa_mac_verify_finish( &operation,
2705 verify_mac, sizeof( verify_mac ) ),
2706 PSA_ERROR_BAD_STATE );
2707 PSA_ASSERT( psa_mac_abort( &operation ) );
2708
2709 /* Setup verify but try sign. */
2710 PSA_ASSERT( psa_mac_verify_setup( &operation,
2711 handle, alg ) );
2712 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2713 TEST_EQUAL( psa_mac_sign_finish( &operation,
2714 sign_mac, sizeof( sign_mac ),
2715 &sign_mac_length ),
2716 PSA_ERROR_BAD_STATE );
2717 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002718
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002719exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002720 mbedtls_psa_crypto_free( );
2721}
2722/* END_CASE */
2723
2724/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002725void mac_sign( int key_type_arg,
2726 data_t *key,
2727 int alg_arg,
2728 data_t *input,
2729 data_t *expected_mac )
2730{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002731 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002732 psa_key_type_t key_type = key_type_arg;
2733 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002734 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002735 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002736 /* Leave a little extra room in the output buffer. At the end of the
2737 * test, we'll check that the implementation didn't overwrite onto
2738 * this extra room. */
2739 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2740 size_t mac_buffer_size =
2741 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2742 size_t mac_length = 0;
2743
2744 memset( actual_mac, '+', sizeof( actual_mac ) );
2745 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2746 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2747
Gilles Peskine8817f612018-12-18 00:18:46 +01002748 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002749
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002750 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002751 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002752 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002753
Gilles Peskine87a5e562019-04-17 12:28:25 +02002754 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01002755 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002756
2757 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002758 PSA_ASSERT( psa_mac_sign_setup( &operation,
2759 handle, alg ) );
2760 PSA_ASSERT( psa_mac_update( &operation,
2761 input->x, input->len ) );
2762 PSA_ASSERT( psa_mac_sign_finish( &operation,
2763 actual_mac, mac_buffer_size,
2764 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002765
2766 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002767 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2768 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002769
2770 /* Verify that the end of the buffer is untouched. */
2771 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2772 sizeof( actual_mac ) - mac_length ) );
2773
2774exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002775 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002776 mbedtls_psa_crypto_free( );
2777}
2778/* END_CASE */
2779
2780/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002781void mac_verify( int key_type_arg,
2782 data_t *key,
2783 int alg_arg,
2784 data_t *input,
2785 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002786{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002787 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002788 psa_key_type_t key_type = key_type_arg;
2789 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002790 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002791 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002792
Gilles Peskine69c12672018-06-28 00:07:19 +02002793 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2794
Gilles Peskine8817f612018-12-18 00:18:46 +01002795 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002796
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002797 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002798 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002799 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002800
Gilles Peskine87a5e562019-04-17 12:28:25 +02002801 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01002802 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002803
Gilles Peskine8817f612018-12-18 00:18:46 +01002804 PSA_ASSERT( psa_mac_verify_setup( &operation,
2805 handle, alg ) );
2806 PSA_ASSERT( psa_destroy_key( handle ) );
2807 PSA_ASSERT( psa_mac_update( &operation,
2808 input->x, input->len ) );
2809 PSA_ASSERT( psa_mac_verify_finish( &operation,
2810 expected_mac->x,
2811 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002812
2813exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002814 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002815 mbedtls_psa_crypto_free( );
2816}
2817/* END_CASE */
2818
2819/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002820void cipher_operation_init( )
2821{
Jaeden Ameroab439972019-02-15 14:12:05 +00002822 const uint8_t input[1] = { 0 };
2823 unsigned char output[1] = { 0 };
2824 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002825 /* Test each valid way of initializing the object, except for `= {0}`, as
2826 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2827 * though it's OK by the C standard. We could test for this, but we'd need
2828 * to supress the Clang warning for the test. */
2829 psa_cipher_operation_t func = psa_cipher_operation_init( );
2830 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2831 psa_cipher_operation_t zero;
2832
2833 memset( &zero, 0, sizeof( zero ) );
2834
Jaeden Ameroab439972019-02-15 14:12:05 +00002835 /* A freshly-initialized cipher operation should not be usable. */
2836 TEST_EQUAL( psa_cipher_update( &func,
2837 input, sizeof( input ),
2838 output, sizeof( output ),
2839 &output_length ),
2840 PSA_ERROR_BAD_STATE );
2841 TEST_EQUAL( psa_cipher_update( &init,
2842 input, sizeof( input ),
2843 output, sizeof( output ),
2844 &output_length ),
2845 PSA_ERROR_BAD_STATE );
2846 TEST_EQUAL( psa_cipher_update( &zero,
2847 input, sizeof( input ),
2848 output, sizeof( output ),
2849 &output_length ),
2850 PSA_ERROR_BAD_STATE );
2851
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002852 /* A default cipher operation should be abortable without error. */
2853 PSA_ASSERT( psa_cipher_abort( &func ) );
2854 PSA_ASSERT( psa_cipher_abort( &init ) );
2855 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002856}
2857/* END_CASE */
2858
2859/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002860void cipher_setup( int key_type_arg,
2861 data_t *key,
2862 int alg_arg,
2863 int expected_status_arg )
2864{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002865 psa_key_type_t key_type = key_type_arg;
2866 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002867 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002868 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002869 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002870#if defined(KNOWN_SUPPORTED_MAC_ALG)
2871 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2872#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002873
Gilles Peskine8817f612018-12-18 00:18:46 +01002874 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002875
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002876 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2877 &operation, &status ) )
2878 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002879 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002880
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002881 /* The operation object should be reusable. */
2882#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2883 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2884 smoke_test_key_data,
2885 sizeof( smoke_test_key_data ),
2886 KNOWN_SUPPORTED_CIPHER_ALG,
2887 &operation, &status ) )
2888 goto exit;
2889 TEST_EQUAL( status, PSA_SUCCESS );
2890#endif
2891
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002892exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002893 mbedtls_psa_crypto_free( );
2894}
2895/* END_CASE */
2896
2897/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002898void cipher_bad_order( )
2899{
2900 psa_key_handle_t handle = 0;
2901 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2902 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
2903 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2904 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2905 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2906 const uint8_t key[] = {
2907 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2908 0xaa, 0xaa, 0xaa, 0xaa };
2909 const uint8_t text[] = {
2910 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2911 0xbb, 0xbb, 0xbb, 0xbb };
2912 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2913 size_t length = 0;
2914
2915 PSA_ASSERT( psa_crypto_init( ) );
2916 PSA_ASSERT( psa_allocate_key( &handle ) );
2917 psa_key_policy_set_usage( &policy,
2918 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2919 alg );
2920 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine87a5e562019-04-17 12:28:25 +02002921 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Jaeden Ameroab439972019-02-15 14:12:05 +00002922 key, sizeof(key) ) );
2923
2924
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002925 /* Call encrypt setup twice in a row. */
2926 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2927 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2928 PSA_ERROR_BAD_STATE );
2929 PSA_ASSERT( psa_cipher_abort( &operation ) );
2930
2931 /* Call decrypt setup twice in a row. */
2932 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2933 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2934 PSA_ERROR_BAD_STATE );
2935 PSA_ASSERT( psa_cipher_abort( &operation ) );
2936
Jaeden Ameroab439972019-02-15 14:12:05 +00002937 /* Generate an IV without calling setup beforehand. */
2938 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2939 buffer, sizeof( buffer ),
2940 &length ),
2941 PSA_ERROR_BAD_STATE );
2942 PSA_ASSERT( psa_cipher_abort( &operation ) );
2943
2944 /* Generate an IV twice in a row. */
2945 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2946 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2947 buffer, sizeof( buffer ),
2948 &length ) );
2949 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2950 buffer, sizeof( buffer ),
2951 &length ),
2952 PSA_ERROR_BAD_STATE );
2953 PSA_ASSERT( psa_cipher_abort( &operation ) );
2954
2955 /* Generate an IV after it's already set. */
2956 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2957 PSA_ASSERT( psa_cipher_set_iv( &operation,
2958 iv, sizeof( iv ) ) );
2959 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2960 buffer, sizeof( buffer ),
2961 &length ),
2962 PSA_ERROR_BAD_STATE );
2963 PSA_ASSERT( psa_cipher_abort( &operation ) );
2964
2965 /* Set an IV without calling setup beforehand. */
2966 TEST_EQUAL( psa_cipher_set_iv( &operation,
2967 iv, sizeof( iv ) ),
2968 PSA_ERROR_BAD_STATE );
2969 PSA_ASSERT( psa_cipher_abort( &operation ) );
2970
2971 /* Set an IV after it's already set. */
2972 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2973 PSA_ASSERT( psa_cipher_set_iv( &operation,
2974 iv, sizeof( iv ) ) );
2975 TEST_EQUAL( psa_cipher_set_iv( &operation,
2976 iv, sizeof( iv ) ),
2977 PSA_ERROR_BAD_STATE );
2978 PSA_ASSERT( psa_cipher_abort( &operation ) );
2979
2980 /* Set an IV after it's already generated. */
2981 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2982 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2983 buffer, sizeof( buffer ),
2984 &length ) );
2985 TEST_EQUAL( psa_cipher_set_iv( &operation,
2986 iv, sizeof( iv ) ),
2987 PSA_ERROR_BAD_STATE );
2988 PSA_ASSERT( psa_cipher_abort( &operation ) );
2989
2990 /* Call update without calling setup beforehand. */
2991 TEST_EQUAL( psa_cipher_update( &operation,
2992 text, sizeof( text ),
2993 buffer, sizeof( buffer ),
2994 &length ),
2995 PSA_ERROR_BAD_STATE );
2996 PSA_ASSERT( psa_cipher_abort( &operation ) );
2997
2998 /* Call update without an IV where an IV is required. */
2999 TEST_EQUAL( psa_cipher_update( &operation,
3000 text, sizeof( text ),
3001 buffer, sizeof( buffer ),
3002 &length ),
3003 PSA_ERROR_BAD_STATE );
3004 PSA_ASSERT( psa_cipher_abort( &operation ) );
3005
3006 /* Call update after finish. */
3007 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3008 PSA_ASSERT( psa_cipher_set_iv( &operation,
3009 iv, sizeof( iv ) ) );
3010 PSA_ASSERT( psa_cipher_finish( &operation,
3011 buffer, sizeof( buffer ), &length ) );
3012 TEST_EQUAL( psa_cipher_update( &operation,
3013 text, sizeof( text ),
3014 buffer, sizeof( buffer ),
3015 &length ),
3016 PSA_ERROR_BAD_STATE );
3017 PSA_ASSERT( psa_cipher_abort( &operation ) );
3018
3019 /* Call finish without calling setup beforehand. */
3020 TEST_EQUAL( psa_cipher_finish( &operation,
3021 buffer, sizeof( buffer ), &length ),
3022 PSA_ERROR_BAD_STATE );
3023 PSA_ASSERT( psa_cipher_abort( &operation ) );
3024
3025 /* Call finish without an IV where an IV is required. */
3026 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3027 /* Not calling update means we are encrypting an empty buffer, which is OK
3028 * for cipher modes with padding. */
3029 TEST_EQUAL( psa_cipher_finish( &operation,
3030 buffer, sizeof( buffer ), &length ),
3031 PSA_ERROR_BAD_STATE );
3032 PSA_ASSERT( psa_cipher_abort( &operation ) );
3033
3034 /* Call finish twice in a row. */
3035 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3036 PSA_ASSERT( psa_cipher_set_iv( &operation,
3037 iv, sizeof( iv ) ) );
3038 PSA_ASSERT( psa_cipher_finish( &operation,
3039 buffer, sizeof( buffer ), &length ) );
3040 TEST_EQUAL( psa_cipher_finish( &operation,
3041 buffer, sizeof( buffer ), &length ),
3042 PSA_ERROR_BAD_STATE );
3043 PSA_ASSERT( psa_cipher_abort( &operation ) );
3044
3045exit:
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003046 mbedtls_psa_crypto_free( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003047}
3048/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003049
Gilles Peskine50e586b2018-06-08 14:28:46 +02003050/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003051void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003052 data_t *key,
3053 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003054 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003055{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003056 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003057 psa_status_t status;
3058 psa_key_type_t key_type = key_type_arg;
3059 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003060 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003061 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003062 size_t iv_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003063 unsigned char *output = NULL;
3064 size_t output_buffer_size = 0;
3065 size_t function_output_length = 0;
3066 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003067 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003068 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003069
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003070 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3071 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003072
Gilles Peskine8817f612018-12-18 00:18:46 +01003073 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003074
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003075 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003076 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003077 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003078
Gilles Peskine87a5e562019-04-17 12:28:25 +02003079 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01003080 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003081
Gilles Peskine8817f612018-12-18 00:18:46 +01003082 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3083 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003084
Gilles Peskine8817f612018-12-18 00:18:46 +01003085 PSA_ASSERT( psa_cipher_set_iv( &operation,
3086 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003087 output_buffer_size = ( (size_t) input->len +
3088 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003089 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003090
Gilles Peskine8817f612018-12-18 00:18:46 +01003091 PSA_ASSERT( psa_cipher_update( &operation,
3092 input->x, input->len,
3093 output, output_buffer_size,
3094 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003095 total_output_length += function_output_length;
3096 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003097 output + total_output_length,
3098 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003099 &function_output_length );
3100 total_output_length += function_output_length;
3101
Gilles Peskinefe11b722018-12-18 00:24:04 +01003102 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003103 if( expected_status == PSA_SUCCESS )
3104 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003105 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003106 ASSERT_COMPARE( expected_output->x, expected_output->len,
3107 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003108 }
3109
3110exit:
3111 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003112 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003113 mbedtls_psa_crypto_free( );
3114}
3115/* END_CASE */
3116
3117/* BEGIN_CASE */
3118void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
3119 data_t *key,
3120 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003121 int first_part_size_arg,
3122 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003123 data_t *expected_output )
3124{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003125 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003126 psa_key_type_t key_type = key_type_arg;
3127 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003128 size_t first_part_size = first_part_size_arg;
3129 size_t output1_length = output1_length_arg;
3130 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003131 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003132 size_t iv_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003133 unsigned char *output = NULL;
3134 size_t output_buffer_size = 0;
3135 size_t function_output_length = 0;
3136 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003137 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003138 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003139
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003140 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3141 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003142
Gilles Peskine8817f612018-12-18 00:18:46 +01003143 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003144
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003145 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003146 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003147 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003148
Gilles Peskine87a5e562019-04-17 12:28:25 +02003149 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01003150 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003151
Gilles Peskine8817f612018-12-18 00:18:46 +01003152 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3153 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003154
Gilles Peskine8817f612018-12-18 00:18:46 +01003155 PSA_ASSERT( psa_cipher_set_iv( &operation,
3156 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003157 output_buffer_size = ( (size_t) input->len +
3158 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003159 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003160
Gilles Peskinee0866522019-02-19 19:44:00 +01003161 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003162 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3163 output, output_buffer_size,
3164 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003165 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003166 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003167 PSA_ASSERT( psa_cipher_update( &operation,
3168 input->x + first_part_size,
3169 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003170 output + total_output_length,
3171 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003172 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003173 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003174 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003175 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003176 output + total_output_length,
3177 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003178 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003179 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003180 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003181
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003182 ASSERT_COMPARE( expected_output->x, expected_output->len,
3183 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003184
3185exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003186 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003187 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003188 mbedtls_psa_crypto_free( );
3189}
3190/* END_CASE */
3191
3192/* BEGIN_CASE */
3193void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003194 data_t *key,
3195 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003196 int first_part_size_arg,
3197 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003198 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003199{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003200 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003201
3202 psa_key_type_t key_type = key_type_arg;
3203 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003204 size_t first_part_size = first_part_size_arg;
3205 size_t output1_length = output1_length_arg;
3206 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003207 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003208 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003209 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003210 size_t output_buffer_size = 0;
3211 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003212 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003213 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003214 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003215
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003216 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3217 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003218
Gilles Peskine8817f612018-12-18 00:18:46 +01003219 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003220
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003221 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003222 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003223 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003224
Gilles Peskine87a5e562019-04-17 12:28:25 +02003225 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01003226 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003227
Gilles Peskine8817f612018-12-18 00:18:46 +01003228 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3229 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003230
Gilles Peskine8817f612018-12-18 00:18:46 +01003231 PSA_ASSERT( psa_cipher_set_iv( &operation,
3232 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003233
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003234 output_buffer_size = ( (size_t) input->len +
3235 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003236 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003237
Gilles Peskinee0866522019-02-19 19:44:00 +01003238 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003239 PSA_ASSERT( psa_cipher_update( &operation,
3240 input->x, first_part_size,
3241 output, output_buffer_size,
3242 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003243 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003244 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003245 PSA_ASSERT( psa_cipher_update( &operation,
3246 input->x + first_part_size,
3247 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003248 output + total_output_length,
3249 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003250 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003251 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003252 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003253 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003254 output + total_output_length,
3255 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003256 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003257 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003258 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003259
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003260 ASSERT_COMPARE( expected_output->x, expected_output->len,
3261 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003262
3263exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003264 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003265 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003266 mbedtls_psa_crypto_free( );
3267}
3268/* END_CASE */
3269
Gilles Peskine50e586b2018-06-08 14:28:46 +02003270/* BEGIN_CASE */
3271void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003272 data_t *key,
3273 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003274 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003275{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003276 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003277 psa_status_t status;
3278 psa_key_type_t key_type = key_type_arg;
3279 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003280 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003281 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003282 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003283 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003284 size_t output_buffer_size = 0;
3285 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003286 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003287 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003288 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003289
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003290 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3291 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003292
Gilles Peskine8817f612018-12-18 00:18:46 +01003293 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003294
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003295 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003296 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003297 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003298
Gilles Peskine87a5e562019-04-17 12:28:25 +02003299 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01003300 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003301
Gilles Peskine8817f612018-12-18 00:18:46 +01003302 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3303 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003304
Gilles Peskine8817f612018-12-18 00:18:46 +01003305 PSA_ASSERT( psa_cipher_set_iv( &operation,
3306 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003307
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003308 output_buffer_size = ( (size_t) input->len +
3309 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003310 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003311
Gilles Peskine8817f612018-12-18 00:18:46 +01003312 PSA_ASSERT( psa_cipher_update( &operation,
3313 input->x, input->len,
3314 output, output_buffer_size,
3315 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003316 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003317 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003318 output + total_output_length,
3319 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003320 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003321 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003322 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003323
3324 if( expected_status == PSA_SUCCESS )
3325 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003326 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003327 ASSERT_COMPARE( expected_output->x, expected_output->len,
3328 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003329 }
3330
Gilles Peskine50e586b2018-06-08 14:28:46 +02003331exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003332 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003333 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003334 mbedtls_psa_crypto_free( );
3335}
3336/* END_CASE */
3337
Gilles Peskine50e586b2018-06-08 14:28:46 +02003338/* BEGIN_CASE */
3339void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003340 data_t *key,
3341 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003342{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003343 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003344 psa_key_type_t key_type = key_type_arg;
3345 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003346 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003347 size_t iv_size = 16;
3348 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003349 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003350 size_t output1_size = 0;
3351 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003352 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003353 size_t output2_size = 0;
3354 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003355 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003356 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3357 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003358 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003359
Gilles Peskine8817f612018-12-18 00:18:46 +01003360 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003361
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003362 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003363 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003364 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003365
Gilles Peskine87a5e562019-04-17 12:28:25 +02003366 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01003367 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003368
Gilles Peskine8817f612018-12-18 00:18:46 +01003369 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3370 handle, alg ) );
3371 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3372 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003373
Gilles Peskine8817f612018-12-18 00:18:46 +01003374 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3375 iv, iv_size,
3376 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003377 output1_size = ( (size_t) input->len +
3378 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003379 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003380
Gilles Peskine8817f612018-12-18 00:18:46 +01003381 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3382 output1, output1_size,
3383 &output1_length ) );
3384 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003385 output1 + output1_length,
3386 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003387 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003388
Gilles Peskine048b7f02018-06-08 14:20:49 +02003389 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003390
Gilles Peskine8817f612018-12-18 00:18:46 +01003391 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003392
3393 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003394 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003395
Gilles Peskine8817f612018-12-18 00:18:46 +01003396 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3397 iv, iv_length ) );
3398 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3399 output2, output2_size,
3400 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003401 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003402 PSA_ASSERT( psa_cipher_finish( &operation2,
3403 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003404 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003405 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003406
Gilles Peskine048b7f02018-06-08 14:20:49 +02003407 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003408
Gilles Peskine8817f612018-12-18 00:18:46 +01003409 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003410
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003411 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003412
3413exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003414 mbedtls_free( output1 );
3415 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003416 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03003417 mbedtls_psa_crypto_free( );
3418}
3419/* END_CASE */
3420
3421/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003422void cipher_verify_output_multipart( int alg_arg,
3423 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003424 data_t *key,
3425 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003426 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003427{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003428 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003429 psa_key_type_t key_type = key_type_arg;
3430 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003431 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003432 unsigned char iv[16] = {0};
3433 size_t iv_size = 16;
3434 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003435 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003436 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003437 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003438 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003439 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003440 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003441 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003442 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3443 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003444 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003445
Gilles Peskine8817f612018-12-18 00:18:46 +01003446 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003447
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003448 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003449 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003450 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003451
Gilles Peskine87a5e562019-04-17 12:28:25 +02003452 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01003453 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003454
Gilles Peskine8817f612018-12-18 00:18:46 +01003455 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3456 handle, alg ) );
3457 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3458 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003459
Gilles Peskine8817f612018-12-18 00:18:46 +01003460 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3461 iv, iv_size,
3462 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003463 output1_buffer_size = ( (size_t) input->len +
3464 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003465 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003466
Gilles Peskinee0866522019-02-19 19:44:00 +01003467 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003468
Gilles Peskine8817f612018-12-18 00:18:46 +01003469 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3470 output1, output1_buffer_size,
3471 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003472 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003473
Gilles Peskine8817f612018-12-18 00:18:46 +01003474 PSA_ASSERT( psa_cipher_update( &operation1,
3475 input->x + first_part_size,
3476 input->len - first_part_size,
3477 output1, output1_buffer_size,
3478 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003479 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003480
Gilles Peskine8817f612018-12-18 00:18:46 +01003481 PSA_ASSERT( psa_cipher_finish( &operation1,
3482 output1 + output1_length,
3483 output1_buffer_size - output1_length,
3484 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003485 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003486
Gilles Peskine8817f612018-12-18 00:18:46 +01003487 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003488
Gilles Peskine048b7f02018-06-08 14:20:49 +02003489 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003490 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003491
Gilles Peskine8817f612018-12-18 00:18:46 +01003492 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3493 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003494
Gilles Peskine8817f612018-12-18 00:18:46 +01003495 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3496 output2, output2_buffer_size,
3497 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003498 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003499
Gilles Peskine8817f612018-12-18 00:18:46 +01003500 PSA_ASSERT( psa_cipher_update( &operation2,
3501 output1 + first_part_size,
3502 output1_length - first_part_size,
3503 output2, output2_buffer_size,
3504 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003505 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003506
Gilles Peskine8817f612018-12-18 00:18:46 +01003507 PSA_ASSERT( psa_cipher_finish( &operation2,
3508 output2 + output2_length,
3509 output2_buffer_size - output2_length,
3510 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003511 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003512
Gilles Peskine8817f612018-12-18 00:18:46 +01003513 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003514
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003515 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003516
3517exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003518 mbedtls_free( output1 );
3519 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003520 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003521 mbedtls_psa_crypto_free( );
3522}
3523/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003524
Gilles Peskine20035e32018-02-03 22:44:14 +01003525/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003526void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003527 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003528 data_t *nonce,
3529 data_t *additional_data,
3530 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003531 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003532{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003533 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003534 psa_key_type_t key_type = key_type_arg;
3535 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003536 unsigned char *output_data = NULL;
3537 size_t output_size = 0;
3538 size_t output_length = 0;
3539 unsigned char *output_data2 = NULL;
3540 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003541 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003542 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003543 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003544
Gilles Peskine4abf7412018-06-18 16:35:34 +02003545 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003546 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003547
Gilles Peskine8817f612018-12-18 00:18:46 +01003548 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003549
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003550 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003551 psa_key_policy_set_usage( &policy,
3552 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
3553 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003554 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003555
Gilles Peskine87a5e562019-04-17 12:28:25 +02003556 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01003557 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003558
Gilles Peskinefe11b722018-12-18 00:24:04 +01003559 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3560 nonce->x, nonce->len,
3561 additional_data->x,
3562 additional_data->len,
3563 input_data->x, input_data->len,
3564 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003565 &output_length ),
3566 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003567
3568 if( PSA_SUCCESS == expected_result )
3569 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003570 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003571
Gilles Peskinefe11b722018-12-18 00:24:04 +01003572 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3573 nonce->x, nonce->len,
3574 additional_data->x,
3575 additional_data->len,
3576 output_data, output_length,
3577 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003578 &output_length2 ),
3579 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003580
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003581 ASSERT_COMPARE( input_data->x, input_data->len,
3582 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003583 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003584
Gilles Peskinea1cac842018-06-11 19:33:02 +02003585exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003586 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003587 mbedtls_free( output_data );
3588 mbedtls_free( output_data2 );
3589 mbedtls_psa_crypto_free( );
3590}
3591/* END_CASE */
3592
3593/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003594void aead_encrypt( int key_type_arg, data_t *key_data,
3595 int alg_arg,
3596 data_t *nonce,
3597 data_t *additional_data,
3598 data_t *input_data,
3599 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003600{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003601 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003602 psa_key_type_t key_type = key_type_arg;
3603 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003604 unsigned char *output_data = NULL;
3605 size_t output_size = 0;
3606 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003607 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00003608 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003609
Gilles Peskine4abf7412018-06-18 16:35:34 +02003610 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003611 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003612
Gilles Peskine8817f612018-12-18 00:18:46 +01003613 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003614
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003615 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003616 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003617 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003618
Gilles Peskine87a5e562019-04-17 12:28:25 +02003619 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01003620 key_data->x,
3621 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003622
Gilles Peskine8817f612018-12-18 00:18:46 +01003623 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3624 nonce->x, nonce->len,
3625 additional_data->x, additional_data->len,
3626 input_data->x, input_data->len,
3627 output_data, output_size,
3628 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003629
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003630 ASSERT_COMPARE( expected_result->x, expected_result->len,
3631 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003632
Gilles Peskinea1cac842018-06-11 19:33:02 +02003633exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003634 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003635 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003636 mbedtls_psa_crypto_free( );
3637}
3638/* END_CASE */
3639
3640/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003641void aead_decrypt( int key_type_arg, data_t *key_data,
3642 int alg_arg,
3643 data_t *nonce,
3644 data_t *additional_data,
3645 data_t *input_data,
3646 data_t *expected_data,
3647 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003648{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003649 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003650 psa_key_type_t key_type = key_type_arg;
3651 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003652 unsigned char *output_data = NULL;
3653 size_t output_size = 0;
3654 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003655 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00003656 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003657 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003658
Gilles Peskine4abf7412018-06-18 16:35:34 +02003659 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003660 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003661
Gilles Peskine8817f612018-12-18 00:18:46 +01003662 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003663
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003664 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003665 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003666 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003667
Gilles Peskine87a5e562019-04-17 12:28:25 +02003668 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01003669 key_data->x,
3670 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003671
Gilles Peskinefe11b722018-12-18 00:24:04 +01003672 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3673 nonce->x, nonce->len,
3674 additional_data->x,
3675 additional_data->len,
3676 input_data->x, input_data->len,
3677 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003678 &output_length ),
3679 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003680
Gilles Peskine2d277862018-06-18 15:41:12 +02003681 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003682 ASSERT_COMPARE( expected_data->x, expected_data->len,
3683 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003684
Gilles Peskinea1cac842018-06-11 19:33:02 +02003685exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003686 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003687 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003688 mbedtls_psa_crypto_free( );
3689}
3690/* END_CASE */
3691
3692/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003693void signature_size( int type_arg,
3694 int bits,
3695 int alg_arg,
3696 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003697{
3698 psa_key_type_t type = type_arg;
3699 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003700 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003701 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003702exit:
3703 ;
3704}
3705/* END_CASE */
3706
3707/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003708void sign_deterministic( int key_type_arg, data_t *key_data,
3709 int alg_arg, data_t *input_data,
3710 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003711{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003712 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003713 psa_key_type_t key_type = key_type_arg;
3714 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003715 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003716 unsigned char *signature = NULL;
3717 size_t signature_size;
3718 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003719 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003720 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003721
Gilles Peskine8817f612018-12-18 00:18:46 +01003722 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003723
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003724 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003725 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003726 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003727
Gilles Peskine87a5e562019-04-17 12:28:25 +02003728 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01003729 key_data->x,
3730 key_data->len ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003731 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3732 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003733
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003734 /* Allocate a buffer which has the size advertized by the
3735 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003736 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3737 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003738 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003739 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003740 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003741
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003742 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003743 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3744 input_data->x, input_data->len,
3745 signature, signature_size,
3746 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003747 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003748 ASSERT_COMPARE( output_data->x, output_data->len,
3749 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003750
3751exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003752 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003753 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01003754 mbedtls_psa_crypto_free( );
3755}
3756/* END_CASE */
3757
3758/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003759void sign_fail( int key_type_arg, data_t *key_data,
3760 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003761 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003762{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003763 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003764 psa_key_type_t key_type = key_type_arg;
3765 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003766 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003767 psa_status_t actual_status;
3768 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003769 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003770 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003771 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003772
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003773 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003774
Gilles Peskine8817f612018-12-18 00:18:46 +01003775 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003776
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003777 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003778 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003779 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003780
Gilles Peskine87a5e562019-04-17 12:28:25 +02003781 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01003782 key_data->x,
3783 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003784
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003785 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003786 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003787 signature, signature_size,
3788 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003789 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003790 /* The value of *signature_length is unspecified on error, but
3791 * whatever it is, it should be less than signature_size, so that
3792 * if the caller tries to read *signature_length bytes without
3793 * checking the error code then they don't overflow a buffer. */
3794 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003795
3796exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003797 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003798 mbedtls_free( signature );
3799 mbedtls_psa_crypto_free( );
3800}
3801/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003802
3803/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003804void sign_verify( int key_type_arg, data_t *key_data,
3805 int alg_arg, data_t *input_data )
3806{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003807 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003808 psa_key_type_t key_type = key_type_arg;
3809 psa_algorithm_t alg = alg_arg;
3810 size_t key_bits;
3811 unsigned char *signature = NULL;
3812 size_t signature_size;
3813 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003814 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003815 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003816
Gilles Peskine8817f612018-12-18 00:18:46 +01003817 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003818
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003819 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003820 psa_key_policy_set_usage( &policy,
3821 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
3822 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003823 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003824
Gilles Peskine87a5e562019-04-17 12:28:25 +02003825 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01003826 key_data->x,
3827 key_data->len ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003828 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3829 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003830
3831 /* Allocate a buffer which has the size advertized by the
3832 * library. */
3833 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3834 key_bits, alg );
3835 TEST_ASSERT( signature_size != 0 );
3836 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003837 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003838
3839 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003840 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3841 input_data->x, input_data->len,
3842 signature, signature_size,
3843 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003844 /* Check that the signature length looks sensible. */
3845 TEST_ASSERT( signature_length <= signature_size );
3846 TEST_ASSERT( signature_length > 0 );
3847
3848 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003849 PSA_ASSERT( psa_asymmetric_verify(
3850 handle, alg,
3851 input_data->x, input_data->len,
3852 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003853
3854 if( input_data->len != 0 )
3855 {
3856 /* Flip a bit in the input and verify that the signature is now
3857 * detected as invalid. Flip a bit at the beginning, not at the end,
3858 * because ECDSA may ignore the last few bits of the input. */
3859 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003860 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3861 input_data->x, input_data->len,
3862 signature, signature_length ),
3863 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003864 }
3865
3866exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003867 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003868 mbedtls_free( signature );
3869 mbedtls_psa_crypto_free( );
3870}
3871/* END_CASE */
3872
3873/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003874void asymmetric_verify( int key_type_arg, data_t *key_data,
3875 int alg_arg, data_t *hash_data,
3876 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003877{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003878 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003879 psa_key_type_t key_type = key_type_arg;
3880 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003881 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003882
Gilles Peskine69c12672018-06-28 00:07:19 +02003883 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3884
Gilles Peskine8817f612018-12-18 00:18:46 +01003885 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003886
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003887 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003888 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003889 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03003890
Gilles Peskine87a5e562019-04-17 12:28:25 +02003891 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01003892 key_data->x,
3893 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003894
Gilles Peskine8817f612018-12-18 00:18:46 +01003895 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3896 hash_data->x, hash_data->len,
3897 signature_data->x,
3898 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003899exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003900 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003901 mbedtls_psa_crypto_free( );
3902}
3903/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003904
3905/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003906void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3907 int alg_arg, data_t *hash_data,
3908 data_t *signature_data,
3909 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003910{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003911 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003912 psa_key_type_t key_type = key_type_arg;
3913 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003914 psa_status_t actual_status;
3915 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003916 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003917
Gilles Peskine8817f612018-12-18 00:18:46 +01003918 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003919
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003920 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003921 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003922 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003923
Gilles Peskine87a5e562019-04-17 12:28:25 +02003924 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01003925 key_data->x,
3926 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003927
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003928 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003929 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003930 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003931 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003932
Gilles Peskinefe11b722018-12-18 00:24:04 +01003933 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003934
3935exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003936 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003937 mbedtls_psa_crypto_free( );
3938}
3939/* END_CASE */
3940
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003941/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003942void asymmetric_encrypt( int key_type_arg,
3943 data_t *key_data,
3944 int alg_arg,
3945 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003946 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003947 int expected_output_length_arg,
3948 int expected_status_arg )
3949{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003950 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003951 psa_key_type_t key_type = key_type_arg;
3952 psa_algorithm_t alg = alg_arg;
3953 size_t expected_output_length = expected_output_length_arg;
3954 size_t key_bits;
3955 unsigned char *output = NULL;
3956 size_t output_size;
3957 size_t output_length = ~0;
3958 psa_status_t actual_status;
3959 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003960 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003961 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003962
Gilles Peskine8817f612018-12-18 00:18:46 +01003963 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003964
Gilles Peskine656896e2018-06-29 19:12:28 +02003965 /* Import the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003966 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003967 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003968 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine87a5e562019-04-17 12:28:25 +02003969 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01003970 key_data->x,
3971 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003972
3973 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003974 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3975 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02003976 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003977 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003978
3979 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003980 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003981 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003982 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003983 output, output_size,
3984 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003985 TEST_EQUAL( actual_status, expected_status );
3986 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003987
Gilles Peskine68428122018-06-30 18:42:41 +02003988 /* If the label is empty, the test framework puts a non-null pointer
3989 * in label->x. Test that a null pointer works as well. */
3990 if( label->len == 0 )
3991 {
3992 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003993 if( output_size != 0 )
3994 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003995 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003996 input_data->x, input_data->len,
3997 NULL, label->len,
3998 output, output_size,
3999 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004000 TEST_EQUAL( actual_status, expected_status );
4001 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004002 }
4003
Gilles Peskine656896e2018-06-29 19:12:28 +02004004exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004005 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02004006 mbedtls_free( output );
4007 mbedtls_psa_crypto_free( );
4008}
4009/* END_CASE */
4010
4011/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004012void asymmetric_encrypt_decrypt( int key_type_arg,
4013 data_t *key_data,
4014 int alg_arg,
4015 data_t *input_data,
4016 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004017{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004018 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004019 psa_key_type_t key_type = key_type_arg;
4020 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004021 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004022 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004023 size_t output_size;
4024 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004025 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004026 size_t output2_size;
4027 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00004028 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004029 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004030
Gilles Peskine8817f612018-12-18 00:18:46 +01004031 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004032
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004033 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004034 psa_key_policy_set_usage( &policy,
4035 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004036 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004037 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004038
Gilles Peskine87a5e562019-04-17 12:28:25 +02004039 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01004040 key_data->x,
4041 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004042
4043 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004044 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4045 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004046 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004047 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004048 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004049 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004050
Gilles Peskineeebd7382018-06-08 18:11:54 +02004051 /* We test encryption by checking that encrypt-then-decrypt gives back
4052 * the original plaintext because of the non-optional random
4053 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004054 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
4055 input_data->x, input_data->len,
4056 label->x, label->len,
4057 output, output_size,
4058 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004059 /* We don't know what ciphertext length to expect, but check that
4060 * it looks sensible. */
4061 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004062
Gilles Peskine8817f612018-12-18 00:18:46 +01004063 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4064 output, output_length,
4065 label->x, label->len,
4066 output2, output2_size,
4067 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004068 ASSERT_COMPARE( input_data->x, input_data->len,
4069 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004070
4071exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004072 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004073 mbedtls_free( output );
4074 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004075 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004076}
4077/* END_CASE */
4078
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004079/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004080void asymmetric_decrypt( int key_type_arg,
4081 data_t *key_data,
4082 int alg_arg,
4083 data_t *input_data,
4084 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004085 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004086{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004087 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004088 psa_key_type_t key_type = key_type_arg;
4089 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004090 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004091 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004092 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00004093 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004094
Jaeden Amero412654a2019-02-06 12:57:46 +00004095 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004096 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004097
Gilles Peskine8817f612018-12-18 00:18:46 +01004098 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004099
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004100 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02004101 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004102 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004103
Gilles Peskine87a5e562019-04-17 12:28:25 +02004104 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01004105 key_data->x,
4106 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004107
Gilles Peskine8817f612018-12-18 00:18:46 +01004108 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4109 input_data->x, input_data->len,
4110 label->x, label->len,
4111 output,
4112 output_size,
4113 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004114 ASSERT_COMPARE( expected_data->x, expected_data->len,
4115 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004116
Gilles Peskine68428122018-06-30 18:42:41 +02004117 /* If the label is empty, the test framework puts a non-null pointer
4118 * in label->x. Test that a null pointer works as well. */
4119 if( label->len == 0 )
4120 {
4121 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004122 if( output_size != 0 )
4123 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004124 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4125 input_data->x, input_data->len,
4126 NULL, label->len,
4127 output,
4128 output_size,
4129 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004130 ASSERT_COMPARE( expected_data->x, expected_data->len,
4131 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004132 }
4133
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004134exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004135 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004136 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004137 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004138}
4139/* END_CASE */
4140
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004141/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004142void asymmetric_decrypt_fail( int key_type_arg,
4143 data_t *key_data,
4144 int alg_arg,
4145 data_t *input_data,
4146 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004147 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004148 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004149{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004150 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004151 psa_key_type_t key_type = key_type_arg;
4152 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004153 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004154 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004155 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004156 psa_status_t actual_status;
4157 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004158 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004159
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004160 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004161
Gilles Peskine8817f612018-12-18 00:18:46 +01004162 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004163
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004164 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02004165 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004166 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004167
Gilles Peskine87a5e562019-04-17 12:28:25 +02004168 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01004169 key_data->x,
4170 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004171
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004172 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004173 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004174 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004175 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004176 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004177 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004178 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004179
Gilles Peskine68428122018-06-30 18:42:41 +02004180 /* If the label is empty, the test framework puts a non-null pointer
4181 * in label->x. Test that a null pointer works as well. */
4182 if( label->len == 0 )
4183 {
4184 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004185 if( output_size != 0 )
4186 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004187 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004188 input_data->x, input_data->len,
4189 NULL, label->len,
4190 output, output_size,
4191 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004192 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004193 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004194 }
4195
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004196exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004197 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004198 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004199 mbedtls_psa_crypto_free( );
4200}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004201/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004202
4203/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00004204void crypto_generator_init( )
4205{
4206 /* Test each valid way of initializing the object, except for `= {0}`, as
4207 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4208 * though it's OK by the C standard. We could test for this, but we'd need
4209 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004210 size_t capacity;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004211 psa_crypto_generator_t func = psa_crypto_generator_init( );
4212 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
4213 psa_crypto_generator_t zero;
4214
4215 memset( &zero, 0, sizeof( zero ) );
4216
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004217 /* A default generator should not be able to report its capacity. */
4218 TEST_EQUAL( psa_get_generator_capacity( &func, &capacity ),
4219 PSA_ERROR_BAD_STATE );
4220 TEST_EQUAL( psa_get_generator_capacity( &init, &capacity ),
4221 PSA_ERROR_BAD_STATE );
4222 TEST_EQUAL( psa_get_generator_capacity( &zero, &capacity ),
4223 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004224
4225 /* A default generator should be abortable without error. */
4226 PSA_ASSERT( psa_generator_abort(&func) );
4227 PSA_ASSERT( psa_generator_abort(&init) );
4228 PSA_ASSERT( psa_generator_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004229}
4230/* END_CASE */
4231
4232/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004233void derive_setup( int key_type_arg,
4234 data_t *key_data,
4235 int alg_arg,
4236 data_t *salt,
4237 data_t *label,
4238 int requested_capacity_arg,
4239 int expected_status_arg )
4240{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004241 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004242 size_t key_type = key_type_arg;
4243 psa_algorithm_t alg = alg_arg;
4244 size_t requested_capacity = requested_capacity_arg;
4245 psa_status_t expected_status = expected_status_arg;
4246 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004247 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004248
Gilles Peskine8817f612018-12-18 00:18:46 +01004249 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004250
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004251 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004252 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004253 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004254
Gilles Peskine87a5e562019-04-17 12:28:25 +02004255 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01004256 key_data->x,
4257 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004258
Gilles Peskinefe11b722018-12-18 00:24:04 +01004259 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4260 salt->x, salt->len,
4261 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004262 requested_capacity ),
4263 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004264
4265exit:
4266 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004267 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004268 mbedtls_psa_crypto_free( );
4269}
4270/* END_CASE */
4271
4272/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004273void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004274{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004275 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004276 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004277 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004278 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004279 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004280 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004281 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4282 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4283 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00004284 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004285
Gilles Peskine8817f612018-12-18 00:18:46 +01004286 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004287
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004288 PSA_ASSERT( psa_allocate_key( &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004289 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004290 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004291
Gilles Peskine87a5e562019-04-17 12:28:25 +02004292 PSA_ASSERT( psa_import_key_to_handle( handle, key_type,
Gilles Peskine8817f612018-12-18 00:18:46 +01004293 key_data,
4294 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004295
4296 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01004297 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4298 NULL, 0,
4299 NULL, 0,
4300 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004301
4302 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01004303 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4304 NULL, 0,
4305 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004306 capacity ),
4307 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004308
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004309 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004310
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004311 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004312 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004313
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004314exit:
4315 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004316 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004317 mbedtls_psa_crypto_free( );
4318}
4319/* END_CASE */
4320
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004321/* BEGIN_CASE */
4322void test_derive_invalid_generator_tests( )
4323{
4324 uint8_t output_buffer[16];
4325 size_t buffer_size = 16;
4326 size_t capacity = 0;
4327 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4328
Nir Sonnenschein50789302018-10-31 12:16:38 +02004329 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004330 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004331
4332 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004333 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004334
Gilles Peskine8817f612018-12-18 00:18:46 +01004335 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004336
Nir Sonnenschein50789302018-10-31 12:16:38 +02004337 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004338 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004339
Nir Sonnenschein50789302018-10-31 12:16:38 +02004340 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004341 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004342
4343exit:
4344 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004345}
4346/* END_CASE */
4347
4348/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004349void derive_output( int alg_arg,
4350 data_t *key_data,
4351 data_t *salt,
4352 data_t *label,
4353 int requested_capacity_arg,
4354 data_t *expected_output1,
4355 data_t *expected_output2 )
4356{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004357 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004358 psa_algorithm_t alg = alg_arg;
4359 size_t requested_capacity = requested_capacity_arg;
4360 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4361 uint8_t *expected_outputs[2] =
4362 {expected_output1->x, expected_output2->x};
4363 size_t output_sizes[2] =
4364 {expected_output1->len, expected_output2->len};
4365 size_t output_buffer_size = 0;
4366 uint8_t *output_buffer = NULL;
4367 size_t expected_capacity;
4368 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004369 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004370 psa_status_t status;
4371 unsigned i;
4372
4373 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4374 {
4375 if( output_sizes[i] > output_buffer_size )
4376 output_buffer_size = output_sizes[i];
4377 if( output_sizes[i] == 0 )
4378 expected_outputs[i] = NULL;
4379 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004380 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004381 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004382
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004383 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4384 psa_set_key_algorithm( &attributes, alg );
4385 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004386
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004387 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004388 key_data->x,
4389 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004390
4391 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004392 if( PSA_ALG_IS_HKDF( alg ) )
4393 {
4394 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4395 PSA_ASSERT( psa_set_generator_capacity( &generator,
4396 requested_capacity ) );
4397 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4398 PSA_KDF_STEP_SALT,
4399 salt->x, salt->len ) );
4400 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4401 PSA_KDF_STEP_SECRET,
4402 handle ) );
4403 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4404 PSA_KDF_STEP_INFO,
4405 label->x, label->len ) );
4406 }
4407 else
4408 {
4409 // legacy
4410 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4411 salt->x, salt->len,
4412 label->x, label->len,
4413 requested_capacity ) );
4414 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004415 PSA_ASSERT( psa_get_generator_capacity( &generator,
4416 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004417 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004418 expected_capacity = requested_capacity;
4419
4420 /* Expansion phase. */
4421 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4422 {
4423 /* Read some bytes. */
4424 status = psa_generator_read( &generator,
4425 output_buffer, output_sizes[i] );
4426 if( expected_capacity == 0 && output_sizes[i] == 0 )
4427 {
4428 /* Reading 0 bytes when 0 bytes are available can go either way. */
4429 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004430 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004431 continue;
4432 }
4433 else if( expected_capacity == 0 ||
4434 output_sizes[i] > expected_capacity )
4435 {
4436 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004437 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004438 expected_capacity = 0;
4439 continue;
4440 }
4441 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004442 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004443 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004444 ASSERT_COMPARE( output_buffer, output_sizes[i],
4445 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004446 /* Check the generator status. */
4447 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01004448 PSA_ASSERT( psa_get_generator_capacity( &generator,
4449 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004450 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004451 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004452 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004453
4454exit:
4455 mbedtls_free( output_buffer );
4456 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004457 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004458 mbedtls_psa_crypto_free( );
4459}
4460/* END_CASE */
4461
4462/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004463void derive_full( int alg_arg,
4464 data_t *key_data,
4465 data_t *salt,
4466 data_t *label,
4467 int requested_capacity_arg )
4468{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004469 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004470 psa_algorithm_t alg = alg_arg;
4471 size_t requested_capacity = requested_capacity_arg;
4472 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4473 unsigned char output_buffer[16];
4474 size_t expected_capacity = requested_capacity;
4475 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004476 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004477
Gilles Peskine8817f612018-12-18 00:18:46 +01004478 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004479
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004480 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4481 psa_set_key_algorithm( &attributes, alg );
4482 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004483
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004484 PSA_ASSERT( psa_import_key( &attributes, &handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004485 key_data->x,
4486 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004487
4488 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004489 if( PSA_ALG_IS_HKDF( alg ) )
4490 {
4491 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4492 PSA_ASSERT( psa_set_generator_capacity( &generator,
4493 requested_capacity ) );
4494 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4495 PSA_KDF_STEP_SALT,
4496 salt->x, salt->len ) );
4497 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4498 PSA_KDF_STEP_SECRET,
4499 handle ) );
4500 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4501 PSA_KDF_STEP_INFO,
4502 label->x, label->len ) );
4503 }
4504 else
4505 {
4506 // legacy
4507 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4508 salt->x, salt->len,
4509 label->x, label->len,
4510 requested_capacity ) );
4511 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004512 PSA_ASSERT( psa_get_generator_capacity( &generator,
4513 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004514 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004515
4516 /* Expansion phase. */
4517 while( current_capacity > 0 )
4518 {
4519 size_t read_size = sizeof( output_buffer );
4520 if( read_size > current_capacity )
4521 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01004522 PSA_ASSERT( psa_generator_read( &generator,
4523 output_buffer,
4524 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004525 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01004526 PSA_ASSERT( psa_get_generator_capacity( &generator,
4527 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004528 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004529 }
4530
4531 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004532 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004533 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004534
Gilles Peskine8817f612018-12-18 00:18:46 +01004535 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004536
4537exit:
4538 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004539 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02004540 mbedtls_psa_crypto_free( );
4541}
4542/* END_CASE */
4543
4544/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004545void derive_key_exercise( int alg_arg,
4546 data_t *key_data,
4547 data_t *salt,
4548 data_t *label,
4549 int derived_type_arg,
4550 int derived_bits_arg,
4551 int derived_usage_arg,
4552 int derived_alg_arg )
4553{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004554 psa_key_handle_t base_handle = 0;
4555 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004556 psa_algorithm_t alg = alg_arg;
4557 psa_key_type_t derived_type = derived_type_arg;
4558 size_t derived_bits = derived_bits_arg;
4559 psa_key_usage_t derived_usage = derived_usage_arg;
4560 psa_algorithm_t derived_alg = derived_alg_arg;
4561 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
4562 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004563 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004564 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004565
Gilles Peskine8817f612018-12-18 00:18:46 +01004566 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004567
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004568 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4569 psa_set_key_algorithm( &attributes, alg );
4570 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
4571 PSA_ASSERT( psa_import_key( &attributes, &base_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004572 key_data->x,
4573 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004574
4575 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004576 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4577 salt->x, salt->len,
4578 label->x, label->len,
4579 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004580 psa_set_key_usage_flags( &attributes, derived_usage );
4581 psa_set_key_algorithm( &attributes, derived_alg );
4582 psa_set_key_type( &attributes, derived_type );
4583 PSA_ASSERT( psa_generator_import_key( &attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004584 derived_bits,
4585 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004586
4587 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004588 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4589 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4590 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004591
4592 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004593 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004594 goto exit;
4595
4596exit:
4597 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004598 psa_destroy_key( base_handle );
4599 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004600 mbedtls_psa_crypto_free( );
4601}
4602/* END_CASE */
4603
4604/* BEGIN_CASE */
4605void derive_key_export( int alg_arg,
4606 data_t *key_data,
4607 data_t *salt,
4608 data_t *label,
4609 int bytes1_arg,
4610 int bytes2_arg )
4611{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004612 psa_key_handle_t base_handle = 0;
4613 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004614 psa_algorithm_t alg = alg_arg;
4615 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004616 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004617 size_t bytes2 = bytes2_arg;
4618 size_t capacity = bytes1 + bytes2;
4619 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004620 uint8_t *output_buffer = NULL;
4621 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004622 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4623 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004624 size_t length;
4625
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004626 ASSERT_ALLOC( output_buffer, capacity );
4627 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004628 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004629
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004630 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4631 psa_set_key_algorithm( &base_attributes, alg );
4632 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4633 PSA_ASSERT( psa_import_key( &base_attributes, &base_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004634 key_data->x,
4635 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004636
4637 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004638 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4639 salt->x, salt->len,
4640 label->x, label->len,
4641 capacity ) );
4642 PSA_ASSERT( psa_generator_read( &generator,
4643 output_buffer,
4644 capacity ) );
4645 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004646
4647 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004648 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4649 salt->x, salt->len,
4650 label->x, label->len,
4651 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004652 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4653 psa_set_key_algorithm( &derived_attributes, 0 );
4654 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
4655 PSA_ASSERT( psa_generator_import_key( &derived_attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004656 derived_bits,
4657 &generator ) );
4658 PSA_ASSERT( psa_export_key( derived_handle,
4659 export_buffer, bytes1,
4660 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004661 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004662 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004663 PSA_ASSERT( psa_generator_import_key( &derived_attributes, &derived_handle,
Gilles Peskine8817f612018-12-18 00:18:46 +01004664 PSA_BYTES_TO_BITS( bytes2 ),
4665 &generator ) );
4666 PSA_ASSERT( psa_export_key( derived_handle,
4667 export_buffer + bytes1, bytes2,
4668 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004669 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004670
4671 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004672 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4673 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004674
4675exit:
4676 mbedtls_free( output_buffer );
4677 mbedtls_free( export_buffer );
4678 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004679 psa_destroy_key( base_handle );
4680 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004681 mbedtls_psa_crypto_free( );
4682}
4683/* END_CASE */
4684
4685/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004686void key_agreement_setup( int alg_arg,
4687 int our_key_type_arg, data_t *our_key_data,
4688 data_t *peer_key_data,
4689 int expected_status_arg )
4690{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004691 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004692 psa_algorithm_t alg = alg_arg;
4693 psa_key_type_t our_key_type = our_key_type_arg;
4694 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004695 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004696 psa_status_t expected_status = expected_status_arg;
4697 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004698
Gilles Peskine8817f612018-12-18 00:18:46 +01004699 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004700
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004701 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4702 psa_set_key_algorithm( &attributes, alg );
4703 psa_set_key_type( &attributes, our_key_type );
4704 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004705 our_key_data->x,
4706 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004707
Gilles Peskine77f40d82019-04-11 21:27:06 +02004708 /* The tests currently include inputs that should fail at either step.
4709 * Test cases that fail at the setup step should be changed to call
4710 * key_derivation_setup instead, and this function should be renamed
4711 * to key_agreement_fail. */
4712 status = psa_key_derivation_setup( &generator, alg );
4713 if( status == PSA_SUCCESS )
4714 {
4715 TEST_EQUAL( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
4716 our_key,
4717 peer_key_data->x, peer_key_data->len ),
4718 expected_status );
4719 }
4720 else
4721 {
4722 TEST_ASSERT( status == expected_status );
4723 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004724
4725exit:
4726 psa_generator_abort( &generator );
4727 psa_destroy_key( our_key );
4728 mbedtls_psa_crypto_free( );
4729}
4730/* END_CASE */
4731
4732/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004733void raw_key_agreement( int alg_arg,
4734 int our_key_type_arg, data_t *our_key_data,
4735 data_t *peer_key_data,
4736 data_t *expected_output )
4737{
4738 psa_key_handle_t our_key = 0;
4739 psa_algorithm_t alg = alg_arg;
4740 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004741 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004742 unsigned char *output = NULL;
4743 size_t output_length = ~0;
4744
4745 ASSERT_ALLOC( output, expected_output->len );
4746 PSA_ASSERT( psa_crypto_init( ) );
4747
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004748 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4749 psa_set_key_algorithm( &attributes, alg );
4750 psa_set_key_type( &attributes, our_key_type );
4751 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskinef0cba732019-04-11 22:12:38 +02004752 our_key_data->x,
4753 our_key_data->len ) );
4754
4755 PSA_ASSERT( psa_key_agreement_raw_shared_secret(
4756 alg, our_key,
4757 peer_key_data->x, peer_key_data->len,
4758 output, expected_output->len, &output_length ) );
4759 ASSERT_COMPARE( output, output_length,
4760 expected_output->x, expected_output->len );
4761
4762exit:
4763 mbedtls_free( output );
4764 psa_destroy_key( our_key );
4765 mbedtls_psa_crypto_free( );
4766}
4767/* END_CASE */
4768
4769/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004770void key_agreement_capacity( int alg_arg,
4771 int our_key_type_arg, data_t *our_key_data,
4772 data_t *peer_key_data,
4773 int expected_capacity_arg )
4774{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004775 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004776 psa_algorithm_t alg = alg_arg;
4777 psa_key_type_t our_key_type = our_key_type_arg;
4778 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004779 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004780 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004781 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004782
Gilles Peskine8817f612018-12-18 00:18:46 +01004783 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004784
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004785 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4786 psa_set_key_algorithm( &attributes, alg );
4787 psa_set_key_type( &attributes, our_key_type );
4788 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004789 our_key_data->x,
4790 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004791
Gilles Peskine969c5d62019-01-16 15:53:06 +01004792 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4793 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004794 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004795 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004796 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4797 {
4798 /* The test data is for info="" */
4799 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4800 PSA_KDF_STEP_INFO,
4801 NULL, 0 ) );
4802 }
Gilles Peskine59685592018-09-18 12:11:34 +02004803
Gilles Peskinebf491972018-10-25 22:36:12 +02004804 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004805 PSA_ASSERT( psa_get_generator_capacity(
4806 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004807 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004808
Gilles Peskinebf491972018-10-25 22:36:12 +02004809 /* Test the actual capacity by reading the output. */
4810 while( actual_capacity > sizeof( output ) )
4811 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004812 PSA_ASSERT( psa_generator_read( &generator,
4813 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004814 actual_capacity -= sizeof( output );
4815 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004816 PSA_ASSERT( psa_generator_read( &generator,
4817 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004818 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004819 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004820
Gilles Peskine59685592018-09-18 12:11:34 +02004821exit:
4822 psa_generator_abort( &generator );
4823 psa_destroy_key( our_key );
4824 mbedtls_psa_crypto_free( );
4825}
4826/* END_CASE */
4827
4828/* BEGIN_CASE */
4829void key_agreement_output( int alg_arg,
4830 int our_key_type_arg, data_t *our_key_data,
4831 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004832 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004833{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004834 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004835 psa_algorithm_t alg = alg_arg;
4836 psa_key_type_t our_key_type = our_key_type_arg;
4837 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004838 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004839 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004840
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004841 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4842 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004843
Gilles Peskine8817f612018-12-18 00:18:46 +01004844 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004845
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004846 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4847 psa_set_key_algorithm( &attributes, alg );
4848 psa_set_key_type( &attributes, our_key_type );
4849 PSA_ASSERT( psa_import_key( &attributes, &our_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004850 our_key_data->x,
4851 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004852
Gilles Peskine969c5d62019-01-16 15:53:06 +01004853 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4854 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004855 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004856 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004857 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4858 {
4859 /* The test data is for info="" */
4860 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4861 PSA_KDF_STEP_INFO,
4862 NULL, 0 ) );
4863 }
Gilles Peskine59685592018-09-18 12:11:34 +02004864
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004865 PSA_ASSERT( psa_generator_read( &generator,
4866 actual_output,
4867 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004868 ASSERT_COMPARE( actual_output, expected_output1->len,
4869 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004870 if( expected_output2->len != 0 )
4871 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004872 PSA_ASSERT( psa_generator_read( &generator,
4873 actual_output,
4874 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004875 ASSERT_COMPARE( actual_output, expected_output2->len,
4876 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004877 }
Gilles Peskine59685592018-09-18 12:11:34 +02004878
4879exit:
4880 psa_generator_abort( &generator );
4881 psa_destroy_key( our_key );
4882 mbedtls_psa_crypto_free( );
4883 mbedtls_free( actual_output );
4884}
4885/* END_CASE */
4886
4887/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004888void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004889{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004890 size_t bytes = bytes_arg;
4891 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004892 unsigned char *output = NULL;
4893 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004894 size_t i;
4895 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004896
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004897 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4898 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004899 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004900
Gilles Peskine8817f612018-12-18 00:18:46 +01004901 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004902
Gilles Peskinea50d7392018-06-21 10:22:13 +02004903 /* Run several times, to ensure that every output byte will be
4904 * nonzero at least once with overwhelming probability
4905 * (2^(-8*number_of_runs)). */
4906 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004907 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004908 if( bytes != 0 )
4909 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004910 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004911
4912 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004913 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4914 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004915
4916 for( i = 0; i < bytes; i++ )
4917 {
4918 if( output[i] != 0 )
4919 ++changed[i];
4920 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004921 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004922
4923 /* Check that every byte was changed to nonzero at least once. This
4924 * validates that psa_generate_random is overwriting every byte of
4925 * the output buffer. */
4926 for( i = 0; i < bytes; i++ )
4927 {
4928 TEST_ASSERT( changed[i] != 0 );
4929 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004930
4931exit:
4932 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004933 mbedtls_free( output );
4934 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004935}
4936/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004937
4938/* BEGIN_CASE */
4939void generate_key( int type_arg,
4940 int bits_arg,
4941 int usage_arg,
4942 int alg_arg,
4943 int expected_status_arg )
4944{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004945 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004946 psa_key_type_t type = type_arg;
4947 psa_key_usage_t usage = usage_arg;
4948 size_t bits = bits_arg;
4949 psa_algorithm_t alg = alg_arg;
4950 psa_status_t expected_status = expected_status_arg;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004951 psa_status_t expected_info_status =
David Saadab4ecc272019-02-14 13:48:10 +02004952 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004953 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004954 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004955
Gilles Peskine8817f612018-12-18 00:18:46 +01004956 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004957
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004958 psa_set_key_usage_flags( &attributes, usage );
4959 psa_set_key_algorithm( &attributes, alg );
4960 psa_set_key_type( &attributes, type );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004961
4962 /* Generate a key */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004963 TEST_EQUAL( psa_generate_key( &attributes, &handle, bits, NULL, 0 ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004964 expected_status );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004965 if( expected_info_status != PSA_SUCCESS )
4966 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004967
4968 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004969 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
4970 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4971 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004972
Gilles Peskine818ca122018-06-20 18:16:48 +02004973 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004974 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004975 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004976
4977exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004978 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004979 mbedtls_psa_crypto_free( );
4980}
4981/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004982
Darryl Greend49a4992018-06-18 17:27:26 +01004983/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4984void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4985 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004986 int alg_arg, int generation_method,
4987 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004988{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004989 psa_key_handle_t handle = 0;
4990 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004991 psa_key_type_t type = (psa_key_type_t) type_arg;
4992 psa_key_type_t type_get;
4993 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00004994 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
4995 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004996 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4997 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004998 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00004999 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
5000 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005001 unsigned char *first_export = NULL;
5002 unsigned char *second_export = NULL;
5003 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5004 size_t first_exported_length;
5005 size_t second_exported_length;
5006
5007 ASSERT_ALLOC( first_export, export_size );
5008 ASSERT_ALLOC( second_export, export_size );
5009
Gilles Peskine8817f612018-12-18 00:18:46 +01005010 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005011
Gilles Peskine8817f612018-12-18 00:18:46 +01005012 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
Gilles Peskine8817f612018-12-18 00:18:46 +01005013 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01005014 psa_key_policy_set_usage( &policy_set, policy_usage,
5015 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01005016 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01005017
Darryl Green0c6575a2018-11-07 16:05:30 +00005018 switch( generation_method )
5019 {
5020 case IMPORT_KEY:
5021 /* Import the key */
Gilles Peskine87a5e562019-04-17 12:28:25 +02005022 PSA_ASSERT( psa_import_key_to_handle( handle, type,
Gilles Peskine8817f612018-12-18 00:18:46 +01005023 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005024 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005025
Darryl Green0c6575a2018-11-07 16:05:30 +00005026 case GENERATE_KEY:
5027 /* Generate a key */
Gilles Peskine87a5e562019-04-17 12:28:25 +02005028 PSA_ASSERT( psa_generate_key_to_handle( handle, type, bits,
Gilles Peskine8817f612018-12-18 00:18:46 +01005029 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005030 break;
5031
5032 case DERIVE_KEY:
5033 /* Create base key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01005034 PSA_ASSERT( psa_allocate_key( &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005035 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
5036 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01005037 PSA_ASSERT( psa_set_key_policy(
5038 base_key, &base_policy_set ) );
Gilles Peskine87a5e562019-04-17 12:28:25 +02005039 PSA_ASSERT( psa_import_key_to_handle( base_key, PSA_KEY_TYPE_DERIVE,
Gilles Peskine8817f612018-12-18 00:18:46 +01005040 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005041 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01005042 PSA_ASSERT( psa_key_derivation( &generator, base_key,
5043 base_policy_alg,
5044 NULL, 0, NULL, 0,
5045 export_size ) );
Gilles Peskine87a5e562019-04-17 12:28:25 +02005046 PSA_ASSERT( psa_generator_import_key_to_handle(
Gilles Peskine8817f612018-12-18 00:18:46 +01005047 handle, PSA_KEY_TYPE_RAW_DATA,
5048 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005049 break;
5050 }
Darryl Greend49a4992018-06-18 17:27:26 +01005051
5052 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005053 TEST_EQUAL( psa_export_key( handle,
5054 first_export, export_size,
5055 &first_exported_length ),
5056 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01005057
5058 /* Shutdown and restart */
5059 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01005060 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005061
Darryl Greend49a4992018-06-18 17:27:26 +01005062 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01005063 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
5064 &handle ) );
5065 PSA_ASSERT( psa_get_key_information(
5066 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005067 TEST_EQUAL( type_get, type );
5068 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005069
Gilles Peskine8817f612018-12-18 00:18:46 +01005070 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005071 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
5072 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005073
5074 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01005075 TEST_EQUAL( psa_export_key( handle,
5076 second_export, export_size,
5077 &second_exported_length ),
5078 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01005079
Darryl Green0c6575a2018-11-07 16:05:30 +00005080 if( export_status == PSA_SUCCESS )
5081 {
5082 ASSERT_COMPARE( first_export, first_exported_length,
5083 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01005084
Darryl Green0c6575a2018-11-07 16:05:30 +00005085 switch( generation_method )
5086 {
5087 case IMPORT_KEY:
5088 ASSERT_COMPARE( data->x, data->len,
5089 first_export, first_exported_length );
5090 break;
5091 default:
5092 break;
5093 }
5094 }
5095
5096 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005097 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005098 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005099
5100exit:
5101 mbedtls_free( first_export );
5102 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005103 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01005104 mbedtls_psa_crypto_free();
5105}
5106/* END_CASE */