blob: 597e391cc6477dc6faf40754f245597446d11f5e [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 ) );
219 PSA_ASSERT( psa_import_key( handle, key_type, key_bytes, key_length ) );
220
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 ) );
253 PSA_ASSERT( psa_import_key( handle, key_type, key_bytes, key_length ) );
254
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;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700349 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200350 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
351 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200352 size_t bits;
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100353 TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200354 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
355 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100356 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
357 handle, alg ) );
358 PSA_ASSERT( psa_cipher_set_iv( &operation,
359 iv, iv_length ) );
360 PSA_ASSERT( psa_cipher_update( &operation,
361 ciphertext, ciphertext_length,
362 decrypted, sizeof( decrypted ),
363 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200364 status = psa_cipher_finish( &operation,
365 decrypted + part_length,
366 sizeof( decrypted ) - part_length,
367 &part_length );
368 /* For a stream cipher, all inputs are valid. For a block cipher,
369 * if the input is some aribtrary data rather than an actual
370 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700371 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700372 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100373 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200374 else
375 TEST_ASSERT( status == PSA_SUCCESS ||
376 status == PSA_ERROR_INVALID_PADDING );
377 }
378
379 return( 1 );
380
381exit:
382 psa_cipher_abort( &operation );
383 return( 0 );
384}
385
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100386static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200387 psa_key_usage_t usage,
388 psa_algorithm_t alg )
389{
390 unsigned char nonce[16] = {0};
391 size_t nonce_length = sizeof( nonce );
392 unsigned char plaintext[16] = "Hello, world...";
393 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
394 size_t ciphertext_length = sizeof( ciphertext );
395 size_t plaintext_length = sizeof( ciphertext );
396
397 if( usage & PSA_KEY_USAGE_ENCRYPT )
398 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100399 PSA_ASSERT( psa_aead_encrypt( handle, alg,
400 nonce, nonce_length,
401 NULL, 0,
402 plaintext, sizeof( plaintext ),
403 ciphertext, sizeof( ciphertext ),
404 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200405 }
406
407 if( usage & PSA_KEY_USAGE_DECRYPT )
408 {
409 psa_status_t verify_status =
410 ( usage & PSA_KEY_USAGE_ENCRYPT ?
411 PSA_SUCCESS :
412 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100413 TEST_EQUAL( psa_aead_decrypt( handle, alg,
414 nonce, nonce_length,
415 NULL, 0,
416 ciphertext, ciphertext_length,
417 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100418 &plaintext_length ),
419 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200420 }
421
422 return( 1 );
423
424exit:
425 return( 0 );
426}
427
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100428static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200429 psa_key_usage_t usage,
430 psa_algorithm_t alg )
431{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200432 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
433 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200434 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200435 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100436 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
437
438 /* If the policy allows signing with any hash, just pick one. */
439 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
440 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100441#if defined(KNOWN_SUPPORTED_HASH_ALG)
442 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
443 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100444#else
445 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100446 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100447#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100448 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200449
450 if( usage & PSA_KEY_USAGE_SIGN )
451 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200452 /* Some algorithms require the payload to have the size of
453 * the hash encoded in the algorithm. Use this input size
454 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200455 if( hash_alg != 0 )
456 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100457 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
458 payload, payload_length,
459 signature, sizeof( signature ),
460 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200461 }
462
463 if( usage & PSA_KEY_USAGE_VERIFY )
464 {
465 psa_status_t verify_status =
466 ( usage & PSA_KEY_USAGE_SIGN ?
467 PSA_SUCCESS :
468 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100469 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
470 payload, payload_length,
471 signature, signature_length ),
472 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200473 }
474
475 return( 1 );
476
477exit:
478 return( 0 );
479}
480
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100481static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200482 psa_key_usage_t usage,
483 psa_algorithm_t alg )
484{
485 unsigned char plaintext[256] = "Hello, world...";
486 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
487 size_t ciphertext_length = sizeof( ciphertext );
488 size_t plaintext_length = 16;
489
490 if( usage & PSA_KEY_USAGE_ENCRYPT )
491 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100492 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
493 plaintext, plaintext_length,
494 NULL, 0,
495 ciphertext, sizeof( ciphertext ),
496 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200497 }
498
499 if( usage & PSA_KEY_USAGE_DECRYPT )
500 {
501 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100502 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200503 ciphertext, ciphertext_length,
504 NULL, 0,
505 plaintext, sizeof( plaintext ),
506 &plaintext_length );
507 TEST_ASSERT( status == PSA_SUCCESS ||
508 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
509 ( status == PSA_ERROR_INVALID_ARGUMENT ||
510 status == PSA_ERROR_INVALID_PADDING ) ) );
511 }
512
513 return( 1 );
514
515exit:
516 return( 0 );
517}
Gilles Peskine02b75072018-07-01 22:31:34 +0200518
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100519static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200520 psa_key_usage_t usage,
521 psa_algorithm_t alg )
522{
523 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
524 unsigned char label[16] = "This is a label.";
525 size_t label_length = sizeof( label );
526 unsigned char seed[16] = "abcdefghijklmnop";
527 size_t seed_length = sizeof( seed );
528 unsigned char output[1];
529
530 if( usage & PSA_KEY_USAGE_DERIVE )
531 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100532 PSA_ASSERT( psa_key_derivation( &generator,
533 handle, alg,
534 label, label_length,
535 seed, seed_length,
536 sizeof( output ) ) );
537 PSA_ASSERT( psa_generator_read( &generator,
538 output,
539 sizeof( output ) ) );
540 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200541 }
542
543 return( 1 );
544
545exit:
546 return( 0 );
547}
548
Gilles Peskinec7998b72018-11-07 18:45:02 +0100549/* We need two keys to exercise key agreement. Exercise the
550 * private key against its own public key. */
551static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100552 psa_key_handle_t handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100553 psa_algorithm_t alg )
554{
555 psa_key_type_t private_key_type;
556 psa_key_type_t public_key_type;
557 size_t key_bits;
558 uint8_t *public_key = NULL;
559 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200560 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinec7998b72018-11-07 18:45:02 +0100561 * psa_key_agreement fails. This isn't fully satisfactory, but it's
562 * good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200563 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100564
Gilles Peskine8817f612018-12-18 00:18:46 +0100565 PSA_ASSERT( psa_get_key_information( handle,
566 &private_key_type,
567 &key_bits ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100568 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
569 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
570 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100571 PSA_ASSERT( psa_export_public_key( handle,
572 public_key, public_key_length,
573 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100574
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100575 status = psa_key_agreement( generator, handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100576 public_key, public_key_length,
577 alg );
578exit:
579 mbedtls_free( public_key );
580 return( status );
581}
582
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100583static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200584 psa_key_usage_t usage,
585 psa_algorithm_t alg )
586{
587 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200588 unsigned char output[1];
589 int ok = 0;
590
591 if( usage & PSA_KEY_USAGE_DERIVE )
592 {
593 /* We need two keys to exercise key agreement. Exercise the
594 * private key against its own public key. */
Gilles Peskine8817f612018-12-18 00:18:46 +0100595 PSA_ASSERT( key_agreement_with_self( &generator, handle, alg ) );
596 PSA_ASSERT( psa_generator_read( &generator,
597 output,
598 sizeof( output ) ) );
599 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200600 }
601 ok = 1;
602
603exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200604 return( ok );
605}
606
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200607static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
608 size_t min_bits, size_t max_bits,
609 int must_be_odd )
610{
611 size_t len;
612 size_t actual_bits;
613 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100614 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100615 MBEDTLS_ASN1_INTEGER ),
616 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200617 /* Tolerate a slight departure from DER encoding:
618 * - 0 may be represented by an empty string or a 1-byte string.
619 * - The sign bit may be used as a value bit. */
620 if( ( len == 1 && ( *p )[0] == 0 ) ||
621 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
622 {
623 ++( *p );
624 --len;
625 }
626 if( min_bits == 0 && len == 0 )
627 return( 1 );
628 msb = ( *p )[0];
629 TEST_ASSERT( msb != 0 );
630 actual_bits = 8 * ( len - 1 );
631 while( msb != 0 )
632 {
633 msb >>= 1;
634 ++actual_bits;
635 }
636 TEST_ASSERT( actual_bits >= min_bits );
637 TEST_ASSERT( actual_bits <= max_bits );
638 if( must_be_odd )
639 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
640 *p += len;
641 return( 1 );
642exit:
643 return( 0 );
644}
645
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200646static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
647 uint8_t *exported, size_t exported_length )
648{
649 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100650 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200651 else
652 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200653
654#if defined(MBEDTLS_DES_C)
655 if( type == PSA_KEY_TYPE_DES )
656 {
657 /* Check the parity bits. */
658 unsigned i;
659 for( i = 0; i < bits / 8; i++ )
660 {
661 unsigned bit_count = 0;
662 unsigned m;
663 for( m = 1; m <= 0x100; m <<= 1 )
664 {
665 if( exported[i] & m )
666 ++bit_count;
667 }
668 TEST_ASSERT( bit_count % 2 != 0 );
669 }
670 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200671 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200672#endif
673
674#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
675 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
676 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200677 uint8_t *p = exported;
678 uint8_t *end = exported + exported_length;
679 size_t len;
680 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200681 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200682 * modulus INTEGER, -- n
683 * publicExponent INTEGER, -- e
684 * privateExponent INTEGER, -- d
685 * prime1 INTEGER, -- p
686 * prime2 INTEGER, -- q
687 * exponent1 INTEGER, -- d mod (p-1)
688 * exponent2 INTEGER, -- d mod (q-1)
689 * coefficient INTEGER, -- (inverse of q) mod p
690 * }
691 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100692 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
693 MBEDTLS_ASN1_SEQUENCE |
694 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
695 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200696 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
697 goto exit;
698 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
699 goto exit;
700 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
701 goto exit;
702 /* Require d to be at least half the size of n. */
703 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
704 goto exit;
705 /* Require p and q to be at most half the size of n, rounded up. */
706 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
707 goto exit;
708 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
709 goto exit;
710 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
711 goto exit;
712 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
713 goto exit;
714 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
715 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100716 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100717 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200718 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200719#endif /* MBEDTLS_RSA_C */
720
721#if defined(MBEDTLS_ECP_C)
722 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
723 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100724 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100725 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100726 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200727 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200728#endif /* MBEDTLS_ECP_C */
729
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200730 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
731 {
732 uint8_t *p = exported;
733 uint8_t *end = exported + exported_length;
734 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200735#if defined(MBEDTLS_RSA_C)
736 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
737 {
738 /* RSAPublicKey ::= SEQUENCE {
739 * modulus INTEGER, -- n
740 * publicExponent INTEGER } -- e
741 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100742 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
743 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100744 MBEDTLS_ASN1_CONSTRUCTED ),
745 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100746 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200747 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
748 goto exit;
749 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
750 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100751 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200752 }
753 else
754#endif /* MBEDTLS_RSA_C */
755#if defined(MBEDTLS_ECP_C)
756 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
757 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000758 /* The representation of an ECC public key is:
759 * - The byte 0x04;
760 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
761 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
762 * - where m is the bit size associated with the curve.
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200763 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100764 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
765 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200766 }
767 else
768#endif /* MBEDTLS_ECP_C */
769 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100770 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200771 mbedtls_snprintf( message, sizeof( message ),
772 "No sanity check for public key type=0x%08lx",
773 (unsigned long) type );
774 test_fail( message, __LINE__, __FILE__ );
775 return( 0 );
776 }
777 }
778 else
779
780 {
781 /* No sanity checks for other types */
782 }
783
784 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200785
786exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200787 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200788}
789
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100790static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200791 psa_key_usage_t usage )
792{
793 psa_key_type_t type;
794 size_t bits;
795 uint8_t *exported = NULL;
796 size_t exported_size = 0;
797 size_t exported_length = 0;
798 int ok = 0;
799
Gilles Peskine8817f612018-12-18 00:18:46 +0100800 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200801
802 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
803 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200804 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100805 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
806 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200807 return( 1 );
808 }
809
Gilles Peskined14664a2018-08-10 19:07:32 +0200810 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200811 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200812
Gilles Peskine8817f612018-12-18 00:18:46 +0100813 PSA_ASSERT( psa_export_key( handle,
814 exported, exported_size,
815 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200816 ok = exported_key_sanity_check( type, bits, exported, exported_length );
817
818exit:
819 mbedtls_free( exported );
820 return( ok );
821}
822
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100823static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200824{
825 psa_key_type_t type;
826 psa_key_type_t public_type;
827 size_t bits;
828 uint8_t *exported = NULL;
829 size_t exported_size = 0;
830 size_t exported_length = 0;
831 int ok = 0;
832
Gilles Peskine8817f612018-12-18 00:18:46 +0100833 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200834 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
835 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100836 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100837 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200838 return( 1 );
839 }
840
841 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
842 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200843 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200844
Gilles Peskine8817f612018-12-18 00:18:46 +0100845 PSA_ASSERT( psa_export_public_key( handle,
846 exported, exported_size,
847 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200848 ok = exported_key_sanity_check( public_type, bits,
849 exported, exported_length );
850
851exit:
852 mbedtls_free( exported );
853 return( ok );
854}
855
Gilles Peskinec9516fb2019-02-05 20:32:06 +0100856/** Do smoke tests on a key.
857 *
858 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
859 * sign/verify, or derivation) that is permitted according to \p usage.
860 * \p usage and \p alg should correspond to the expected policy on the
861 * key.
862 *
863 * Export the key if permitted by \p usage, and check that the output
864 * looks sensible. If \p usage forbids export, check that
865 * \p psa_export_key correctly rejects the attempt. If the key is
866 * asymmetric, also check \p psa_export_public_key.
867 *
868 * If the key fails the tests, this function calls the test framework's
869 * `test_fail` function and returns false. Otherwise this function returns
870 * true. Therefore it should be used as follows:
871 * ```
872 * if( ! exercise_key( ... ) ) goto exit;
873 * ```
874 *
875 * \param handle The key to exercise. It should be capable of performing
876 * \p alg.
877 * \param usage The usage flags to assume.
878 * \param alg The algorithm to exercise.
879 *
880 * \retval 0 The key failed the smoke tests.
881 * \retval 1 The key passed the smoke tests.
882 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100883static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200884 psa_key_usage_t usage,
885 psa_algorithm_t alg )
886{
887 int ok;
888 if( alg == 0 )
889 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
890 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100891 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200892 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100893 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200894 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100895 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200896 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100897 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200898 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100899 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200900 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100901 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200902 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100903 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200904 else
905 {
906 char message[40];
907 mbedtls_snprintf( message, sizeof( message ),
908 "No code to exercise alg=0x%08lx",
909 (unsigned long) alg );
910 test_fail( message, __LINE__, __FILE__ );
911 ok = 0;
912 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200913
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100914 ok = ok && exercise_export_key( handle, usage );
915 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200916
Gilles Peskine02b75072018-07-01 22:31:34 +0200917 return( ok );
918}
919
Gilles Peskine10df3412018-10-25 22:35:43 +0200920static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
921 psa_algorithm_t alg )
922{
923 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
924 {
925 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
926 PSA_KEY_USAGE_VERIFY :
927 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
928 }
929 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
930 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
931 {
932 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
933 PSA_KEY_USAGE_ENCRYPT :
934 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
935 }
936 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
937 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
938 {
939 return( PSA_KEY_USAGE_DERIVE );
940 }
941 else
942 {
943 return( 0 );
944 }
945
946}
Darryl Green0c6575a2018-11-07 16:05:30 +0000947
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100948/* An overapproximation of the amount of storage needed for a key of the
949 * given type and with the given content. The API doesn't make it easy
950 * to find a good value for the size. The current implementation doesn't
951 * care about the value anyway. */
952#define KEY_BITS_FROM_DATA( type, data ) \
953 ( data )->len
954
Darryl Green0c6575a2018-11-07 16:05:30 +0000955typedef enum {
956 IMPORT_KEY = 0,
957 GENERATE_KEY = 1,
958 DERIVE_KEY = 2
959} generate_method;
960
Gilles Peskinee59236f2018-01-27 23:32:46 +0100961/* END_HEADER */
962
963/* BEGIN_DEPENDENCIES
964 * depends_on:MBEDTLS_PSA_CRYPTO_C
965 * END_DEPENDENCIES
966 */
967
968/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200969void static_checks( )
970{
971 size_t max_truncated_mac_size =
972 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
973
974 /* Check that the length for a truncated MAC always fits in the algorithm
975 * encoding. The shifted mask is the maximum truncated value. The
976 * untruncated algorithm may be one byte larger. */
977 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
978}
979/* END_CASE */
980
981/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200982void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100983{
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100984 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200985 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100986 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100987
Gilles Peskine8817f612018-12-18 00:18:46 +0100988 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100989
Gilles Peskined40c1fb2019-01-19 12:20:52 +0100990 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100991 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100992 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100993 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +0100994 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995
996exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100997 mbedtls_psa_crypto_free( );
998}
999/* END_CASE */
1000
1001/* BEGIN_CASE */
Gilles Peskineca5bed72019-05-13 11:29:51 +02001002void import_twice( int usage_arg, int alg_arg,
Gilles Peskinea4261682018-12-03 11:34:01 +01001003 int type1_arg, data_t *data1,
1004 int expected_import1_status_arg,
1005 int type2_arg, data_t *data2,
1006 int expected_import2_status_arg )
1007{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001008 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +01001009 psa_algorithm_t alg = alg_arg;
1010 psa_key_usage_t usage = usage_arg;
1011 psa_key_type_t type1 = type1_arg;
1012 psa_status_t expected_import1_status = expected_import1_status_arg;
1013 psa_key_type_t type2 = type2_arg;
1014 psa_status_t expected_import2_status = expected_import2_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00001015 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea4261682018-12-03 11:34:01 +01001016 psa_status_t status;
1017
Gilles Peskine8817f612018-12-18 00:18:46 +01001018 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +01001019
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001020 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +01001021 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001022 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +01001023
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001024 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001025 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001026 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001027 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +01001028
1029 if( expected_import1_status == PSA_SUCCESS ||
1030 expected_import2_status == PSA_SUCCESS )
1031 {
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001032 if( ! exercise_key( handle, usage, alg ) )
1033 goto exit;
Gilles Peskinea4261682018-12-03 11:34:01 +01001034 }
1035
1036exit:
1037 mbedtls_psa_crypto_free( );
1038}
1039/* END_CASE */
1040
1041/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001042void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1043{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001044 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001045 size_t bits = bits_arg;
1046 psa_status_t expected_status = expected_status_arg;
1047 psa_status_t status;
1048 psa_key_type_t type =
1049 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
1050 size_t buffer_size = /* Slight overapproximations */
1051 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001052 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001053 unsigned char *p;
1054 int ret;
1055 size_t length;
1056
Gilles Peskine8817f612018-12-18 00:18:46 +01001057 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001058 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001059
1060 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1061 bits, keypair ) ) >= 0 );
1062 length = ret;
1063
1064 /* Try importing the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001065 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001066 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001067 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001068 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001069 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001070
1071exit:
1072 mbedtls_free( buffer );
1073 mbedtls_psa_crypto_free( );
1074}
1075/* END_CASE */
1076
1077/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001078void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001079 int type_arg,
Gilles Peskineca5bed72019-05-13 11:29:51 +02001080 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001081 int expected_bits,
1082 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001083 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001084 int canonical_input )
1085{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001086 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001087 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001088 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001089 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001090 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001091 unsigned char *exported = NULL;
1092 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001093 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001094 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001095 size_t reexported_length;
1096 psa_key_type_t got_type;
1097 size_t got_bits;
Jaeden Amero70261c52019-01-04 11:47:20 +00001098 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001099
Moran Pekercb088e72018-07-17 17:36:59 +03001100 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001101 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001102 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001103 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001104 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001105
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001106 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001107 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001108 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001109
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001110 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
David Saadab4ecc272019-02-14 13:48:10 +02001111 PSA_ERROR_DOES_NOT_EXIST );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001112
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001113 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001114 PSA_ASSERT( psa_import_key( handle, type,
1115 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001116
1117 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001118 PSA_ASSERT( psa_get_key_information( handle,
1119 &got_type,
1120 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001121 TEST_EQUAL( got_type, type );
1122 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001123
1124 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001125 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001126 exported, export_size,
1127 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001128 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001129
1130 /* The exported length must be set by psa_export_key() to a value between 0
1131 * and export_size. On errors, the exported length must be 0. */
1132 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1133 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1134 TEST_ASSERT( exported_length <= export_size );
1135
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001136 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001137 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001138 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001139 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001140 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001141 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001142 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001143
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001144 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001145 goto exit;
1146
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001147 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001148 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001149 else
1150 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001151 psa_key_handle_t handle2;
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001152 PSA_ASSERT( psa_allocate_key( &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001153 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001154
Gilles Peskine8817f612018-12-18 00:18:46 +01001155 PSA_ASSERT( psa_import_key( handle2, type,
1156 exported,
1157 exported_length ) );
1158 PSA_ASSERT( psa_export_key( handle2,
1159 reexported,
1160 export_size,
1161 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001162 ASSERT_COMPARE( exported, exported_length,
1163 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001164 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001165 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001166 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001167
1168destroy:
1169 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001170 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001171 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1172 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001173
1174exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001175 mbedtls_free( exported );
1176 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001177 mbedtls_psa_crypto_free( );
1178}
1179/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001180
Moran Pekerf709f4a2018-06-06 17:26:04 +03001181/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001182void import_key_nonempty_slot( )
1183{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001184 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001185 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1186 psa_status_t status;
1187 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001188 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001189
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001190 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001191
Moran Peker28a38e62018-11-07 16:18:24 +02001192 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001193 PSA_ASSERT( psa_import_key( handle, type,
1194 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001195
1196 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001197 status = psa_import_key( handle, type, data, sizeof( data ) );
David Saadab4ecc272019-02-14 13:48:10 +02001198 TEST_EQUAL( status, PSA_ERROR_ALREADY_EXISTS );
Moran Peker28a38e62018-11-07 16:18:24 +02001199
1200exit:
1201 mbedtls_psa_crypto_free( );
1202}
1203/* END_CASE */
1204
1205/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001206void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001207{
1208 psa_status_t status;
1209 unsigned char *exported = NULL;
1210 size_t export_size = 0;
1211 size_t exported_length = INVALID_EXPORT_LENGTH;
1212 psa_status_t expected_export_status = expected_export_status_arg;
1213
Gilles Peskine8817f612018-12-18 00:18:46 +01001214 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001215
1216 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001217 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001218 exported, export_size,
1219 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001220 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001221
1222exit:
1223 mbedtls_psa_crypto_free( );
1224}
1225/* END_CASE */
1226
1227/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001228void export_with_no_key_activity( )
1229{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001230 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001231 psa_algorithm_t alg = PSA_ALG_CTR;
1232 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001233 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001234 unsigned char *exported = NULL;
1235 size_t export_size = 0;
1236 size_t exported_length = INVALID_EXPORT_LENGTH;
1237
Gilles Peskine8817f612018-12-18 00:18:46 +01001238 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001239
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001240 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001241 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001242 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001243
1244 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001245 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001246 exported, export_size,
1247 &exported_length );
David Saadab4ecc272019-02-14 13:48:10 +02001248 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Peker34550092018-11-07 16:19:34 +02001249
1250exit:
1251 mbedtls_psa_crypto_free( );
1252}
1253/* END_CASE */
1254
1255/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001256void cipher_with_no_key_activity( )
1257{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001258 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001259 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001260 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001261 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001262 int exercise_alg = PSA_ALG_CTR;
1263
Gilles Peskine8817f612018-12-18 00:18:46 +01001264 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001265
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001266 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001267 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001268 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001269
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001270 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
David Saadab4ecc272019-02-14 13:48:10 +02001271 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Pekerce500072018-11-07 16:20:07 +02001272
1273exit:
1274 psa_cipher_abort( &operation );
1275 mbedtls_psa_crypto_free( );
1276}
1277/* END_CASE */
1278
1279/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001280void export_after_import_failure( data_t *data, int type_arg,
1281 int expected_import_status_arg )
1282{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001283 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001284 psa_key_type_t type = type_arg;
1285 psa_status_t status;
1286 unsigned char *exported = NULL;
1287 size_t export_size = 0;
1288 psa_status_t expected_import_status = expected_import_status_arg;
1289 size_t exported_length = INVALID_EXPORT_LENGTH;
1290
Gilles Peskine8817f612018-12-18 00:18:46 +01001291 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001292
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001293 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001294
Moran Peker34550092018-11-07 16:19:34 +02001295 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001296 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001297 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001298 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001299
1300 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001301 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001302 exported, export_size,
1303 &exported_length );
David Saadab4ecc272019-02-14 13:48:10 +02001304 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Peker34550092018-11-07 16:19:34 +02001305
1306exit:
1307 mbedtls_psa_crypto_free( );
1308}
1309/* END_CASE */
1310
1311/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001312void cipher_after_import_failure( data_t *data, int type_arg,
1313 int expected_import_status_arg )
1314{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001315 psa_key_handle_t handle = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001316 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001317 psa_key_type_t type = type_arg;
1318 psa_status_t status;
1319 psa_status_t expected_import_status = expected_import_status_arg;
1320 int exercise_alg = PSA_ALG_CTR;
1321
Gilles Peskine8817f612018-12-18 00:18:46 +01001322 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001323
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001324 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001325
Moran Pekerce500072018-11-07 16:20:07 +02001326 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001327 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001328 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001329 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001330
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001331 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
David Saadab4ecc272019-02-14 13:48:10 +02001332 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Pekerce500072018-11-07 16:20:07 +02001333
1334exit:
1335 psa_cipher_abort( &operation );
1336 mbedtls_psa_crypto_free( );
1337}
1338/* END_CASE */
1339
1340/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001341void export_after_destroy_key( data_t *data, int type_arg )
1342{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001343 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001344 psa_key_type_t type = type_arg;
1345 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001346 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001347 psa_algorithm_t alg = PSA_ALG_CTR;
1348 unsigned char *exported = NULL;
1349 size_t export_size = 0;
1350 size_t exported_length = INVALID_EXPORT_LENGTH;
1351
Gilles Peskine8817f612018-12-18 00:18:46 +01001352 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001353
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001354 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001355 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001356 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001357 export_size = (ptrdiff_t) data->len;
1358 ASSERT_ALLOC( exported, export_size );
1359
1360 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001361 PSA_ASSERT( psa_import_key( handle, type,
1362 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001363
Gilles Peskine8817f612018-12-18 00:18:46 +01001364 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1365 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001366
1367 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001368 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001369
1370 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001371 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001372 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001373 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001374
1375exit:
1376 mbedtls_free( exported );
1377 mbedtls_psa_crypto_free( );
1378}
1379/* END_CASE */
1380
1381/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001382void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001383 int type_arg,
1384 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001385 int export_size_delta,
1386 int expected_export_status_arg,
1387 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001388{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001389 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001390 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001391 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001392 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001393 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001394 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001395 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001396 size_t exported_length = INVALID_EXPORT_LENGTH;
Jaeden Amero70261c52019-01-04 11:47:20 +00001397 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001398
Gilles Peskine8817f612018-12-18 00:18:46 +01001399 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001400
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001401 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001402 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001403 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001404
1405 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001406 PSA_ASSERT( psa_import_key( handle, type,
1407 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001408
Gilles Peskine49c25912018-10-29 15:15:31 +01001409 /* Export the public key */
1410 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001411 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001412 exported, export_size,
1413 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001414 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001415 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001416 {
1417 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1418 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001419 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001420 TEST_ASSERT( expected_public_key->len <=
1421 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001422 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1423 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001424 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001425
1426exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001427 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001428 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001429 mbedtls_psa_crypto_free( );
1430}
1431/* END_CASE */
1432
Gilles Peskine20035e32018-02-03 22:44:14 +01001433/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001434void import_and_exercise_key( data_t *data,
1435 int type_arg,
1436 int bits_arg,
1437 int alg_arg )
1438{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001439 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001440 psa_key_type_t type = type_arg;
1441 size_t bits = bits_arg;
1442 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001443 psa_key_usage_t usage = usage_to_exercise( type, alg );
Jaeden Amero70261c52019-01-04 11:47:20 +00001444 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001445 psa_key_type_t got_type;
1446 size_t got_bits;
1447 psa_status_t status;
1448
Gilles Peskine8817f612018-12-18 00:18:46 +01001449 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001450
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001451 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001452 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001453 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001454
1455 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001456 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001457 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001458
1459 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001460 PSA_ASSERT( psa_get_key_information( handle,
1461 &got_type,
1462 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001463 TEST_EQUAL( got_type, type );
1464 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001465
1466 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001467 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001468 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001469
1470exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001471 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001472 mbedtls_psa_crypto_free( );
1473}
1474/* END_CASE */
1475
1476/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001477void key_policy( int usage_arg, int alg_arg )
1478{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001479 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001480 psa_algorithm_t alg = alg_arg;
1481 psa_key_usage_t usage = usage_arg;
1482 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1483 unsigned char key[32] = {0};
Jaeden Amero70261c52019-01-04 11:47:20 +00001484 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
1485 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001486
1487 memset( key, 0x2a, sizeof( key ) );
1488
Gilles Peskine8817f612018-12-18 00:18:46 +01001489 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001490
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001491 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001492 psa_key_policy_set_usage( &policy_set, usage, alg );
1493
Gilles Peskinefe11b722018-12-18 00:24:04 +01001494 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1495 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001496 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001497
Gilles Peskine8817f612018-12-18 00:18:46 +01001498 PSA_ASSERT( psa_import_key( handle, key_type,
1499 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001500
Gilles Peskine8817f612018-12-18 00:18:46 +01001501 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001502
Gilles Peskinefe11b722018-12-18 00:24:04 +01001503 TEST_EQUAL( policy_get.usage, policy_set.usage );
1504 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001505
1506exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001507 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001508 mbedtls_psa_crypto_free( );
1509}
1510/* END_CASE */
1511
1512/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001513void key_policy_init( )
1514{
1515 /* Test each valid way of initializing the object, except for `= {0}`, as
1516 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1517 * though it's OK by the C standard. We could test for this, but we'd need
1518 * to supress the Clang warning for the test. */
1519 psa_key_policy_t func = psa_key_policy_init( );
1520 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1521 psa_key_policy_t zero;
1522
1523 memset( &zero, 0, sizeof( zero ) );
1524
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001525 /* A default key policy should not permit any usage. */
1526 TEST_EQUAL( psa_key_policy_get_usage( &func ), 0 );
1527 TEST_EQUAL( psa_key_policy_get_usage( &init ), 0 );
1528 TEST_EQUAL( psa_key_policy_get_usage( &zero ), 0 );
1529
1530 /* A default key policy should not permit any algorithm. */
1531 TEST_EQUAL( psa_key_policy_get_algorithm( &func ), 0 );
1532 TEST_EQUAL( psa_key_policy_get_algorithm( &init ), 0 );
1533 TEST_EQUAL( psa_key_policy_get_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001534}
1535/* END_CASE */
1536
1537/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001538void mac_key_policy( int policy_usage,
1539 int policy_alg,
1540 int key_type,
1541 data_t *key_data,
1542 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001543{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001544 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001545 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001546 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001547 psa_status_t status;
1548 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001549
Gilles Peskine8817f612018-12-18 00:18:46 +01001550 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001551
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001552 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001553 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001554 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001555
Gilles Peskine8817f612018-12-18 00:18:46 +01001556 PSA_ASSERT( psa_import_key( handle, key_type,
1557 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001558
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001559 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001560 if( policy_alg == exercise_alg &&
1561 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001562 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001563 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001564 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001565 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001566
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001567 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001568 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001569 if( policy_alg == exercise_alg &&
1570 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001571 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001572 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001573 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001574
1575exit:
1576 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001577 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001578 mbedtls_psa_crypto_free( );
1579}
1580/* END_CASE */
1581
1582/* BEGIN_CASE */
1583void cipher_key_policy( int policy_usage,
1584 int policy_alg,
1585 int key_type,
1586 data_t *key_data,
1587 int exercise_alg )
1588{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001589 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001590 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001591 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001592 psa_status_t status;
1593
Gilles Peskine8817f612018-12-18 00:18:46 +01001594 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001595
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001596 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001597 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001598 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001599
Gilles Peskine8817f612018-12-18 00:18:46 +01001600 PSA_ASSERT( psa_import_key( handle, key_type,
1601 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001602
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001603 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001604 if( policy_alg == exercise_alg &&
1605 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001606 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001607 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001608 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001609 psa_cipher_abort( &operation );
1610
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001611 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001612 if( policy_alg == exercise_alg &&
1613 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001614 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001615 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001616 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001617
1618exit:
1619 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001620 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001621 mbedtls_psa_crypto_free( );
1622}
1623/* END_CASE */
1624
1625/* BEGIN_CASE */
1626void aead_key_policy( int policy_usage,
1627 int policy_alg,
1628 int key_type,
1629 data_t *key_data,
1630 int nonce_length_arg,
1631 int tag_length_arg,
1632 int exercise_alg )
1633{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001634 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001635 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001636 psa_status_t status;
1637 unsigned char nonce[16] = {0};
1638 size_t nonce_length = nonce_length_arg;
1639 unsigned char tag[16];
1640 size_t tag_length = tag_length_arg;
1641 size_t output_length;
1642
1643 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1644 TEST_ASSERT( tag_length <= sizeof( tag ) );
1645
Gilles Peskine8817f612018-12-18 00:18:46 +01001646 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001647
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001648 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001649 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001650 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001651
Gilles Peskine8817f612018-12-18 00:18:46 +01001652 PSA_ASSERT( psa_import_key( handle, key_type,
1653 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001654
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001655 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001656 nonce, nonce_length,
1657 NULL, 0,
1658 NULL, 0,
1659 tag, tag_length,
1660 &output_length );
1661 if( policy_alg == exercise_alg &&
1662 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001663 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001664 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001665 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001666
1667 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001668 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001669 nonce, nonce_length,
1670 NULL, 0,
1671 tag, tag_length,
1672 NULL, 0,
1673 &output_length );
1674 if( policy_alg == exercise_alg &&
1675 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001676 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001677 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001678 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001679
1680exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001681 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001682 mbedtls_psa_crypto_free( );
1683}
1684/* END_CASE */
1685
1686/* BEGIN_CASE */
1687void asymmetric_encryption_key_policy( int policy_usage,
1688 int policy_alg,
1689 int key_type,
1690 data_t *key_data,
1691 int exercise_alg )
1692{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001693 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001694 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001695 psa_status_t status;
1696 size_t key_bits;
1697 size_t buffer_length;
1698 unsigned char *buffer = NULL;
1699 size_t output_length;
1700
Gilles Peskine8817f612018-12-18 00:18:46 +01001701 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001702
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001703 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001704 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001705 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001706
Gilles Peskine8817f612018-12-18 00:18:46 +01001707 PSA_ASSERT( psa_import_key( handle, key_type,
1708 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001709
Gilles Peskine8817f612018-12-18 00:18:46 +01001710 PSA_ASSERT( psa_get_key_information( handle,
1711 NULL,
1712 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001713 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1714 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001715 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001716
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001717 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001718 NULL, 0,
1719 NULL, 0,
1720 buffer, buffer_length,
1721 &output_length );
1722 if( policy_alg == exercise_alg &&
1723 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001724 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001725 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001726 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001727
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001728 if( buffer_length != 0 )
1729 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001730 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001731 buffer, buffer_length,
1732 NULL, 0,
1733 buffer, buffer_length,
1734 &output_length );
1735 if( policy_alg == exercise_alg &&
1736 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001737 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001738 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001739 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001740
1741exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001742 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001743 mbedtls_psa_crypto_free( );
1744 mbedtls_free( buffer );
1745}
1746/* END_CASE */
1747
1748/* BEGIN_CASE */
1749void asymmetric_signature_key_policy( int policy_usage,
1750 int policy_alg,
1751 int key_type,
1752 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001753 int exercise_alg,
1754 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001755{
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;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001758 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001759 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1760 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1761 * compatible with the policy and `payload_length_arg` is supposed to be
1762 * a valid input length to sign. If `payload_length_arg <= 0`,
1763 * `exercise_alg` is supposed to be forbidden by the policy. */
1764 int compatible_alg = payload_length_arg > 0;
1765 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001766 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1767 size_t signature_length;
1768
Gilles Peskine8817f612018-12-18 00:18:46 +01001769 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001770
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001771 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001772 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001773 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001774
Gilles Peskine8817f612018-12-18 00:18:46 +01001775 PSA_ASSERT( psa_import_key( handle, key_type,
1776 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001777
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001778 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001779 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001780 signature, sizeof( signature ),
1781 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001782 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001783 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001784 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001785 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001786
1787 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001788 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001789 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001790 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001791 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001792 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001793 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001794 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001795
1796exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001797 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001798 mbedtls_psa_crypto_free( );
1799}
1800/* END_CASE */
1801
1802/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001803void derive_key_policy( int policy_usage,
1804 int policy_alg,
1805 int key_type,
1806 data_t *key_data,
1807 int exercise_alg )
1808{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001809 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001810 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001811 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1812 psa_status_t status;
1813
Gilles Peskine8817f612018-12-18 00:18:46 +01001814 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001815
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001816 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001817 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001818 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001819
Gilles Peskine8817f612018-12-18 00:18:46 +01001820 PSA_ASSERT( psa_import_key( handle, key_type,
1821 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001822
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001823 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001824 exercise_alg,
1825 NULL, 0,
1826 NULL, 0,
1827 1 );
1828 if( policy_alg == exercise_alg &&
1829 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001830 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001831 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001832 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001833
1834exit:
1835 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001836 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001837 mbedtls_psa_crypto_free( );
1838}
1839/* END_CASE */
1840
1841/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001842void agreement_key_policy( int policy_usage,
1843 int policy_alg,
1844 int key_type_arg,
1845 data_t *key_data,
1846 int exercise_alg )
1847{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001848 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001849 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001850 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001851 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1852 psa_status_t status;
1853
Gilles Peskine8817f612018-12-18 00:18:46 +01001854 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001855
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001856 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001857 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001858 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001859
Gilles Peskine8817f612018-12-18 00:18:46 +01001860 PSA_ASSERT( psa_import_key( handle, key_type,
1861 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001862
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001863 status = key_agreement_with_self( &generator, handle, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001864
Gilles Peskine01d718c2018-09-18 12:01:02 +02001865 if( policy_alg == exercise_alg &&
1866 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001867 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001868 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001869 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001870
1871exit:
1872 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001873 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001874 mbedtls_psa_crypto_free( );
1875}
1876/* END_CASE */
1877
1878/* BEGIN_CASE */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001879void key_policy_alg2( int key_type_arg, data_t *key_data,
1880 int usage_arg, int alg_arg, int alg2_arg )
1881{
1882 psa_key_handle_t handle = 0;
1883 psa_key_type_t key_type = key_type_arg;
1884 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
1885 psa_key_policy_t got_policy = PSA_KEY_POLICY_INIT;
1886 psa_key_usage_t usage = usage_arg;
1887 psa_algorithm_t alg = alg_arg;
1888 psa_algorithm_t alg2 = alg2_arg;
1889
1890 PSA_ASSERT( psa_crypto_init( ) );
1891
1892 PSA_ASSERT( psa_allocate_key( &handle ) );
1893 psa_key_policy_set_usage( &policy, usage, alg );
1894 psa_key_policy_set_enrollment_algorithm( &policy, alg2 );
1895 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
1896 PSA_ASSERT( psa_import_key( handle, key_type,
1897 key_data->x, key_data->len ) );
1898
1899 PSA_ASSERT( psa_get_key_policy( handle, &got_policy ) );
1900 TEST_EQUAL( psa_key_policy_get_usage( &got_policy ), usage );
1901 TEST_EQUAL( psa_key_policy_get_algorithm( &got_policy ), alg );
1902 TEST_EQUAL( psa_key_policy_get_enrollment_algorithm( &got_policy ), alg2 );
1903
1904 if( ! exercise_key( handle, usage, alg ) )
1905 goto exit;
1906 if( ! exercise_key( handle, usage, alg2 ) )
1907 goto exit;
1908
1909exit:
1910 psa_destroy_key( handle );
1911 mbedtls_psa_crypto_free( );
1912}
1913/* END_CASE */
1914
1915/* BEGIN_CASE */
Gilles Peskine705cbfd2019-05-20 17:28:11 +02001916void copy_key_policy( int source_usage_arg,
1917 int source_alg_arg, int source_alg2_arg,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001918 int type_arg, data_t *material,
Gilles Peskine705cbfd2019-05-20 17:28:11 +02001919 int target_usage_arg,
1920 int target_alg_arg, int target_alg2_arg,
1921 int constraint_usage_arg,
1922 int constraint_alg_arg, int constraint_alg2_arg,
1923 int expected_usage_arg,
1924 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001925{
1926 psa_key_usage_t source_usage = source_usage_arg;
1927 psa_algorithm_t source_alg = source_alg_arg;
Gilles Peskine705cbfd2019-05-20 17:28:11 +02001928 psa_algorithm_t source_alg2 = source_alg2_arg;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001929 psa_key_handle_t source_handle = 0;
1930 psa_key_policy_t source_policy = PSA_KEY_POLICY_INIT;
1931 psa_key_type_t source_type = type_arg;
1932 size_t source_bits;
1933 psa_key_usage_t target_usage = target_usage_arg;
1934 psa_algorithm_t target_alg = target_alg_arg;
Gilles Peskine705cbfd2019-05-20 17:28:11 +02001935 psa_algorithm_t target_alg2 = target_alg2_arg;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001936 psa_key_handle_t target_handle = 0;
1937 psa_key_policy_t target_policy = PSA_KEY_POLICY_INIT;
1938 psa_key_type_t target_type;
1939 size_t target_bits;
1940 psa_key_usage_t constraint_usage = constraint_usage_arg;
1941 psa_algorithm_t constraint_alg = constraint_alg_arg;
Gilles Peskine705cbfd2019-05-20 17:28:11 +02001942 psa_algorithm_t constraint_alg2 = constraint_alg2_arg;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001943 psa_key_policy_t constraint = PSA_KEY_POLICY_INIT;
1944 psa_key_policy_t *p_constraint = NULL;
1945 psa_key_usage_t expected_usage = expected_usage_arg;
1946 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskine705cbfd2019-05-20 17:28:11 +02001947 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001948 uint8_t *export_buffer = NULL;
1949
1950 if( constraint_usage_arg != -1 )
1951 {
1952 p_constraint = &constraint;
1953 psa_key_policy_set_usage( p_constraint,
1954 constraint_usage, constraint_alg );
Gilles Peskine705cbfd2019-05-20 17:28:11 +02001955 psa_key_policy_set_enrollment_algorithm( p_constraint,
1956 constraint_alg2 );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001957 }
1958
1959 PSA_ASSERT( psa_crypto_init( ) );
1960
1961 /* Populate the source slot. */
1962 PSA_ASSERT( psa_allocate_key( &source_handle ) );
1963 psa_key_policy_set_usage( &source_policy, source_usage, source_alg );
Gilles Peskine705cbfd2019-05-20 17:28:11 +02001964 psa_key_policy_set_enrollment_algorithm( &source_policy, source_alg2 );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001965 PSA_ASSERT( psa_set_key_policy( source_handle, &source_policy ) );
1966 PSA_ASSERT( psa_import_key( source_handle, source_type,
1967 material->x, material->len ) );
1968 PSA_ASSERT( psa_get_key_information( source_handle, NULL, &source_bits ) );
1969
1970 /* Prepare the target slot. */
1971 PSA_ASSERT( psa_allocate_key( &target_handle ) );
1972 psa_key_policy_set_usage( &target_policy, target_usage, target_alg );
Gilles Peskine705cbfd2019-05-20 17:28:11 +02001973 psa_key_policy_set_enrollment_algorithm( &target_policy, target_alg2 );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001974 PSA_ASSERT( psa_set_key_policy( target_handle, &target_policy ) );
1975 target_policy = psa_key_policy_init();
1976
1977 /* Copy the key. */
1978 PSA_ASSERT( psa_copy_key( source_handle, target_handle, p_constraint ) );
1979
1980 /* Destroy the source to ensure that this doesn't affect the target. */
1981 PSA_ASSERT( psa_destroy_key( source_handle ) );
1982
1983 /* Test that the target slot has the expected content and policy. */
1984 PSA_ASSERT( psa_get_key_information( target_handle,
1985 &target_type, &target_bits ) );
1986 TEST_EQUAL( source_type, target_type );
1987 TEST_EQUAL( source_bits, target_bits );
1988 PSA_ASSERT( psa_get_key_policy( target_handle, &target_policy ) );
1989 TEST_EQUAL( expected_usage, psa_key_policy_get_usage( &target_policy ) );
1990 TEST_EQUAL( expected_alg, psa_key_policy_get_algorithm( &target_policy ) );
Gilles Peskine705cbfd2019-05-20 17:28:11 +02001991 TEST_EQUAL( expected_alg2,
1992 psa_key_policy_get_enrollment_algorithm( &target_policy ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001993 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1994 {
1995 size_t length;
1996 ASSERT_ALLOC( export_buffer, material->len );
1997 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
1998 material->len, &length ) );
1999 ASSERT_COMPARE( material->x, material->len,
2000 export_buffer, length );
2001 }
2002 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2003 goto exit;
2004
2005 PSA_ASSERT( psa_close_key( target_handle ) );
2006
2007exit:
2008 mbedtls_psa_crypto_free( );
2009 mbedtls_free( export_buffer );
2010}
2011/* END_CASE */
2012
2013/* BEGIN_CASE */
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002014void copy_fail( int source_usage_arg, int source_alg_arg, int source_alg2_arg,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002015 int type_arg, data_t *material,
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002016 int target_usage_arg, int target_alg_arg, int target_alg2_arg,
2017 int constraint_usage_arg,
2018 int constraint_alg_arg, int constraint_alg2_arg,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002019 int expected_status_arg )
2020{
2021 /* Test copy failure into an empty slot. There is a test for copy failure
2022 * into an occupied slot in
2023 * test_suite_psa_crypto_slot_management.function. */
2024
2025 psa_key_usage_t source_usage = source_usage_arg;
2026 psa_algorithm_t source_alg = source_alg_arg;
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002027 psa_algorithm_t source_alg2 = source_alg2_arg;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002028 psa_key_handle_t source_handle = 0;
2029 psa_key_policy_t source_policy = PSA_KEY_POLICY_INIT;
2030 psa_key_type_t source_type = type_arg;
2031 size_t source_bits;
2032 psa_key_usage_t target_usage = target_usage_arg;
2033 psa_algorithm_t target_alg = target_alg_arg;
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002034 psa_algorithm_t target_alg2 = target_alg2_arg;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002035 psa_key_handle_t target_handle = 0;
2036 psa_key_policy_t target_policy = PSA_KEY_POLICY_INIT;
2037 psa_key_type_t target_type;
2038 size_t target_bits;
2039 psa_key_usage_t constraint_usage = constraint_usage_arg;
2040 psa_algorithm_t constraint_alg = constraint_alg_arg;
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002041 psa_algorithm_t constraint_alg2 = constraint_alg2_arg;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002042 psa_key_policy_t constraint = PSA_KEY_POLICY_INIT;
2043 psa_key_policy_t *p_constraint = NULL;
2044 psa_status_t expected_status = expected_status_arg;
2045
2046 if( constraint_usage_arg != -1 )
2047 {
2048 p_constraint = &constraint;
2049 psa_key_policy_set_usage( p_constraint,
2050 constraint_usage, constraint_alg );
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002051 psa_key_policy_set_enrollment_algorithm( p_constraint,
2052 constraint_alg2 );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002053 }
2054
2055 PSA_ASSERT( psa_crypto_init( ) );
2056
2057 /* Populate the source slot. */
2058 PSA_ASSERT( psa_allocate_key( &source_handle ) );
2059 psa_key_policy_set_usage( &source_policy, source_usage, source_alg );
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002060 psa_key_policy_set_enrollment_algorithm( &source_policy, source_alg2 );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002061 PSA_ASSERT( psa_set_key_policy( source_handle, &source_policy ) );
2062 PSA_ASSERT( psa_import_key( source_handle, source_type,
2063 material->x, material->len ) );
2064 PSA_ASSERT( psa_get_key_information( source_handle, NULL, &source_bits ) );
2065
2066 /* Prepare the target slot. */
2067 PSA_ASSERT( psa_allocate_key( &target_handle ) );
2068 psa_key_policy_set_usage( &target_policy, target_usage, target_alg );
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002069 psa_key_policy_set_enrollment_algorithm( &target_policy, target_alg2 );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002070 PSA_ASSERT( psa_set_key_policy( target_handle, &target_policy ) );
2071 target_policy = psa_key_policy_init();
2072
2073 /* Copy the key. */
2074 TEST_EQUAL( psa_copy_key( source_handle, target_handle, p_constraint ),
2075 expected_status );
2076
2077 /* Test that the target slot is unaffected. */
2078 TEST_EQUAL( psa_get_key_information( target_handle,
2079 &target_type, &target_bits ),
David Saadab4ecc272019-02-14 13:48:10 +02002080 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002081 PSA_ASSERT( psa_get_key_policy( target_handle, &target_policy ) );
2082 TEST_EQUAL( target_usage, psa_key_policy_get_usage( &target_policy ) );
2083 TEST_EQUAL( target_alg, psa_key_policy_get_algorithm( &target_policy ) );
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002084 TEST_EQUAL( target_alg2,
2085 psa_key_policy_get_enrollment_algorithm( &target_policy ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002086
2087exit:
2088 mbedtls_psa_crypto_free( );
2089}
2090/* END_CASE */
2091
2092/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002093void hash_operation_init( )
2094{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002095 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002096 /* Test each valid way of initializing the object, except for `= {0}`, as
2097 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2098 * though it's OK by the C standard. We could test for this, but we'd need
2099 * to supress the Clang warning for the test. */
2100 psa_hash_operation_t func = psa_hash_operation_init( );
2101 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2102 psa_hash_operation_t zero;
2103
2104 memset( &zero, 0, sizeof( zero ) );
2105
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002106 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002107 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2108 PSA_ERROR_BAD_STATE );
2109 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2110 PSA_ERROR_BAD_STATE );
2111 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2112 PSA_ERROR_BAD_STATE );
2113
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002114 /* A default hash operation should be abortable without error. */
2115 PSA_ASSERT( psa_hash_abort( &func ) );
2116 PSA_ASSERT( psa_hash_abort( &init ) );
2117 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002118}
2119/* END_CASE */
2120
2121/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002122void hash_setup( int alg_arg,
2123 int expected_status_arg )
2124{
2125 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002126 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002127 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002128 psa_status_t status;
2129
Gilles Peskine8817f612018-12-18 00:18:46 +01002130 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002131
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002132 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002133 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002134
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002135 /* Whether setup succeeded or failed, abort must succeed. */
2136 PSA_ASSERT( psa_hash_abort( &operation ) );
2137
2138 /* If setup failed, reproduce the failure, so as to
2139 * test the resulting state of the operation object. */
2140 if( status != PSA_SUCCESS )
2141 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2142
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002143 /* Now the operation object should be reusable. */
2144#if defined(KNOWN_SUPPORTED_HASH_ALG)
2145 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2146 PSA_ASSERT( psa_hash_abort( &operation ) );
2147#endif
2148
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002149exit:
2150 mbedtls_psa_crypto_free( );
2151}
2152/* END_CASE */
2153
2154/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002155void hash_bad_order( )
2156{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002157 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002158 unsigned char input[] = "";
2159 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002160 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002161 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2162 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2163 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002164 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002165 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002166 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002167
Gilles Peskine8817f612018-12-18 00:18:46 +01002168 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002169
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002170 /* Call setup twice in a row. */
2171 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2172 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2173 PSA_ERROR_BAD_STATE );
2174 PSA_ASSERT( psa_hash_abort( &operation ) );
2175
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002176 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002177 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002178 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002179 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002180
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002181 /* Call update after finish. */
2182 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2183 PSA_ASSERT( psa_hash_finish( &operation,
2184 hash, sizeof( hash ), &hash_len ) );
2185 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002186 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002187 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002188
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002189 /* Call verify without calling setup beforehand. */
2190 TEST_EQUAL( psa_hash_verify( &operation,
2191 valid_hash, sizeof( valid_hash ) ),
2192 PSA_ERROR_BAD_STATE );
2193 PSA_ASSERT( psa_hash_abort( &operation ) );
2194
2195 /* Call verify after finish. */
2196 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2197 PSA_ASSERT( psa_hash_finish( &operation,
2198 hash, sizeof( hash ), &hash_len ) );
2199 TEST_EQUAL( psa_hash_verify( &operation,
2200 valid_hash, sizeof( valid_hash ) ),
2201 PSA_ERROR_BAD_STATE );
2202 PSA_ASSERT( psa_hash_abort( &operation ) );
2203
2204 /* Call verify twice in a row. */
2205 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2206 PSA_ASSERT( psa_hash_verify( &operation,
2207 valid_hash, sizeof( valid_hash ) ) );
2208 TEST_EQUAL( psa_hash_verify( &operation,
2209 valid_hash, sizeof( valid_hash ) ),
2210 PSA_ERROR_BAD_STATE );
2211 PSA_ASSERT( psa_hash_abort( &operation ) );
2212
2213 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002214 TEST_EQUAL( psa_hash_finish( &operation,
2215 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002216 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002217 PSA_ASSERT( psa_hash_abort( &operation ) );
2218
2219 /* Call finish twice in a row. */
2220 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2221 PSA_ASSERT( psa_hash_finish( &operation,
2222 hash, sizeof( hash ), &hash_len ) );
2223 TEST_EQUAL( psa_hash_finish( &operation,
2224 hash, sizeof( hash ), &hash_len ),
2225 PSA_ERROR_BAD_STATE );
2226 PSA_ASSERT( psa_hash_abort( &operation ) );
2227
2228 /* Call finish after calling verify. */
2229 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2230 PSA_ASSERT( psa_hash_verify( &operation,
2231 valid_hash, sizeof( valid_hash ) ) );
2232 TEST_EQUAL( psa_hash_finish( &operation,
2233 hash, sizeof( hash ), &hash_len ),
2234 PSA_ERROR_BAD_STATE );
2235 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002236
2237exit:
2238 mbedtls_psa_crypto_free( );
2239}
2240/* END_CASE */
2241
itayzafrir27e69452018-11-01 14:26:34 +02002242/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2243void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002244{
2245 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002246 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2247 * appended to it */
2248 unsigned char hash[] = {
2249 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2250 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2251 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002252 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002253 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002254
Gilles Peskine8817f612018-12-18 00:18:46 +01002255 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002256
itayzafrir27e69452018-11-01 14:26:34 +02002257 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002258 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002259 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002260 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002261
itayzafrir27e69452018-11-01 14:26:34 +02002262 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002263 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002264 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002265 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002266
itayzafrir27e69452018-11-01 14:26:34 +02002267 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002268 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002269 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002270 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002271
itayzafrirec93d302018-10-18 18:01:10 +03002272exit:
2273 mbedtls_psa_crypto_free( );
2274}
2275/* END_CASE */
2276
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002277/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2278void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002279{
2280 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002281 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002282 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002283 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002284 size_t hash_len;
2285
Gilles Peskine8817f612018-12-18 00:18:46 +01002286 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002287
itayzafrir58028322018-10-25 10:22:01 +03002288 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002289 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002290 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002291 hash, expected_size - 1, &hash_len ),
2292 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002293
2294exit:
2295 mbedtls_psa_crypto_free( );
2296}
2297/* END_CASE */
2298
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002299/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2300void hash_clone_source_state( )
2301{
2302 psa_algorithm_t alg = PSA_ALG_SHA_256;
2303 unsigned char hash[PSA_HASH_MAX_SIZE];
2304 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2305 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2306 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2307 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2308 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2309 size_t hash_len;
2310
2311 PSA_ASSERT( psa_crypto_init( ) );
2312 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2313
2314 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2315 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2316 PSA_ASSERT( psa_hash_finish( &op_finished,
2317 hash, sizeof( hash ), &hash_len ) );
2318 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2319 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2320
2321 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2322 PSA_ERROR_BAD_STATE );
2323
2324 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2325 PSA_ASSERT( psa_hash_finish( &op_init,
2326 hash, sizeof( hash ), &hash_len ) );
2327 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2328 PSA_ASSERT( psa_hash_finish( &op_finished,
2329 hash, sizeof( hash ), &hash_len ) );
2330 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2331 PSA_ASSERT( psa_hash_finish( &op_aborted,
2332 hash, sizeof( hash ), &hash_len ) );
2333
2334exit:
2335 psa_hash_abort( &op_source );
2336 psa_hash_abort( &op_init );
2337 psa_hash_abort( &op_setup );
2338 psa_hash_abort( &op_finished );
2339 psa_hash_abort( &op_aborted );
2340 mbedtls_psa_crypto_free( );
2341}
2342/* END_CASE */
2343
2344/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2345void hash_clone_target_state( )
2346{
2347 psa_algorithm_t alg = PSA_ALG_SHA_256;
2348 unsigned char hash[PSA_HASH_MAX_SIZE];
2349 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2350 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2351 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2352 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2353 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2354 size_t hash_len;
2355
2356 PSA_ASSERT( psa_crypto_init( ) );
2357
2358 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2359 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2360 PSA_ASSERT( psa_hash_finish( &op_finished,
2361 hash, sizeof( hash ), &hash_len ) );
2362 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2363 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2364
2365 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2366 PSA_ASSERT( psa_hash_finish( &op_target,
2367 hash, sizeof( hash ), &hash_len ) );
2368
2369 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2370 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2371 PSA_ERROR_BAD_STATE );
2372 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2373 PSA_ERROR_BAD_STATE );
2374
2375exit:
2376 psa_hash_abort( &op_target );
2377 psa_hash_abort( &op_init );
2378 psa_hash_abort( &op_setup );
2379 psa_hash_abort( &op_finished );
2380 psa_hash_abort( &op_aborted );
2381 mbedtls_psa_crypto_free( );
2382}
2383/* END_CASE */
2384
itayzafrir58028322018-10-25 10:22:01 +03002385/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002386void mac_operation_init( )
2387{
Jaeden Amero252ef282019-02-15 14:05:35 +00002388 const uint8_t input[1] = { 0 };
2389
Jaeden Amero769ce272019-01-04 11:48:03 +00002390 /* Test each valid way of initializing the object, except for `= {0}`, as
2391 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2392 * though it's OK by the C standard. We could test for this, but we'd need
2393 * to supress the Clang warning for the test. */
2394 psa_mac_operation_t func = psa_mac_operation_init( );
2395 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2396 psa_mac_operation_t zero;
2397
2398 memset( &zero, 0, sizeof( zero ) );
2399
Jaeden Amero252ef282019-02-15 14:05:35 +00002400 /* A freshly-initialized MAC operation should not be usable. */
2401 TEST_EQUAL( psa_mac_update( &func,
2402 input, sizeof( input ) ),
2403 PSA_ERROR_BAD_STATE );
2404 TEST_EQUAL( psa_mac_update( &init,
2405 input, sizeof( input ) ),
2406 PSA_ERROR_BAD_STATE );
2407 TEST_EQUAL( psa_mac_update( &zero,
2408 input, sizeof( input ) ),
2409 PSA_ERROR_BAD_STATE );
2410
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002411 /* A default MAC operation should be abortable without error. */
2412 PSA_ASSERT( psa_mac_abort( &func ) );
2413 PSA_ASSERT( psa_mac_abort( &init ) );
2414 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002415}
2416/* END_CASE */
2417
2418/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002419void mac_setup( int key_type_arg,
2420 data_t *key,
2421 int alg_arg,
2422 int expected_status_arg )
2423{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002424 psa_key_type_t key_type = key_type_arg;
2425 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002426 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002427 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002428 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2429#if defined(KNOWN_SUPPORTED_MAC_ALG)
2430 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2431#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002432
Gilles Peskine8817f612018-12-18 00:18:46 +01002433 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002434
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002435 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2436 &operation, &status ) )
2437 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002438 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002439
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002440 /* The operation object should be reusable. */
2441#if defined(KNOWN_SUPPORTED_MAC_ALG)
2442 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2443 smoke_test_key_data,
2444 sizeof( smoke_test_key_data ),
2445 KNOWN_SUPPORTED_MAC_ALG,
2446 &operation, &status ) )
2447 goto exit;
2448 TEST_EQUAL( status, PSA_SUCCESS );
2449#endif
2450
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002451exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002452 mbedtls_psa_crypto_free( );
2453}
2454/* END_CASE */
2455
2456/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002457void mac_bad_order( )
2458{
2459 psa_key_handle_t handle = 0;
2460 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2461 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2462 const uint8_t key[] = {
2463 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2464 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2465 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
2466 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2467 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2468 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2469 size_t sign_mac_length = 0;
2470 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2471 const uint8_t verify_mac[] = {
2472 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2473 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2474 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2475
2476 PSA_ASSERT( psa_crypto_init( ) );
2477 PSA_ASSERT( psa_allocate_key( &handle ) );
2478 psa_key_policy_set_usage( &policy,
2479 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2480 alg );
2481 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2482
2483 PSA_ASSERT( psa_import_key( handle, key_type,
2484 key, sizeof(key) ) );
2485
2486 /* Call update without calling setup beforehand. */
2487 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2488 PSA_ERROR_BAD_STATE );
2489 PSA_ASSERT( psa_mac_abort( &operation ) );
2490
2491 /* Call sign finish without calling setup beforehand. */
2492 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2493 &sign_mac_length),
2494 PSA_ERROR_BAD_STATE );
2495 PSA_ASSERT( psa_mac_abort( &operation ) );
2496
2497 /* Call verify finish without calling setup beforehand. */
2498 TEST_EQUAL( psa_mac_verify_finish( &operation,
2499 verify_mac, sizeof( verify_mac ) ),
2500 PSA_ERROR_BAD_STATE );
2501 PSA_ASSERT( psa_mac_abort( &operation ) );
2502
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002503 /* Call setup twice in a row. */
2504 PSA_ASSERT( psa_mac_sign_setup( &operation,
2505 handle, alg ) );
2506 TEST_EQUAL( psa_mac_sign_setup( &operation,
2507 handle, alg ),
2508 PSA_ERROR_BAD_STATE );
2509 PSA_ASSERT( psa_mac_abort( &operation ) );
2510
Jaeden Amero252ef282019-02-15 14:05:35 +00002511 /* Call update after sign finish. */
2512 PSA_ASSERT( psa_mac_sign_setup( &operation,
2513 handle, alg ) );
2514 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2515 PSA_ASSERT( psa_mac_sign_finish( &operation,
2516 sign_mac, sizeof( sign_mac ),
2517 &sign_mac_length ) );
2518 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2519 PSA_ERROR_BAD_STATE );
2520 PSA_ASSERT( psa_mac_abort( &operation ) );
2521
2522 /* Call update after verify finish. */
2523 PSA_ASSERT( psa_mac_verify_setup( &operation,
2524 handle, alg ) );
2525 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2526 PSA_ASSERT( psa_mac_verify_finish( &operation,
2527 verify_mac, sizeof( verify_mac ) ) );
2528 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2529 PSA_ERROR_BAD_STATE );
2530 PSA_ASSERT( psa_mac_abort( &operation ) );
2531
2532 /* Call sign finish twice in a row. */
2533 PSA_ASSERT( psa_mac_sign_setup( &operation,
2534 handle, alg ) );
2535 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2536 PSA_ASSERT( psa_mac_sign_finish( &operation,
2537 sign_mac, sizeof( sign_mac ),
2538 &sign_mac_length ) );
2539 TEST_EQUAL( psa_mac_sign_finish( &operation,
2540 sign_mac, sizeof( sign_mac ),
2541 &sign_mac_length ),
2542 PSA_ERROR_BAD_STATE );
2543 PSA_ASSERT( psa_mac_abort( &operation ) );
2544
2545 /* Call verify finish twice in a row. */
2546 PSA_ASSERT( psa_mac_verify_setup( &operation,
2547 handle, alg ) );
2548 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2549 PSA_ASSERT( psa_mac_verify_finish( &operation,
2550 verify_mac, sizeof( verify_mac ) ) );
2551 TEST_EQUAL( psa_mac_verify_finish( &operation,
2552 verify_mac, sizeof( verify_mac ) ),
2553 PSA_ERROR_BAD_STATE );
2554 PSA_ASSERT( psa_mac_abort( &operation ) );
2555
2556 /* Setup sign but try verify. */
2557 PSA_ASSERT( psa_mac_sign_setup( &operation,
2558 handle, alg ) );
2559 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2560 TEST_EQUAL( psa_mac_verify_finish( &operation,
2561 verify_mac, sizeof( verify_mac ) ),
2562 PSA_ERROR_BAD_STATE );
2563 PSA_ASSERT( psa_mac_abort( &operation ) );
2564
2565 /* Setup verify but try sign. */
2566 PSA_ASSERT( psa_mac_verify_setup( &operation,
2567 handle, alg ) );
2568 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2569 TEST_EQUAL( psa_mac_sign_finish( &operation,
2570 sign_mac, sizeof( sign_mac ),
2571 &sign_mac_length ),
2572 PSA_ERROR_BAD_STATE );
2573 PSA_ASSERT( psa_mac_abort( &operation ) );
2574
2575exit:
2576 mbedtls_psa_crypto_free( );
2577}
2578/* END_CASE */
2579
2580/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002581void mac_sign( int key_type_arg,
2582 data_t *key,
2583 int alg_arg,
2584 data_t *input,
2585 data_t *expected_mac )
2586{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002587 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002588 psa_key_type_t key_type = key_type_arg;
2589 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002590 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002591 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002592 /* Leave a little extra room in the output buffer. At the end of the
2593 * test, we'll check that the implementation didn't overwrite onto
2594 * this extra room. */
2595 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2596 size_t mac_buffer_size =
2597 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2598 size_t mac_length = 0;
2599
2600 memset( actual_mac, '+', sizeof( actual_mac ) );
2601 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2602 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2603
Gilles Peskine8817f612018-12-18 00:18:46 +01002604 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002605
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002606 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002607 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002608 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002609
Gilles Peskine8817f612018-12-18 00:18:46 +01002610 PSA_ASSERT( psa_import_key( handle, key_type,
2611 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002612
2613 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002614 PSA_ASSERT( psa_mac_sign_setup( &operation,
2615 handle, alg ) );
2616 PSA_ASSERT( psa_mac_update( &operation,
2617 input->x, input->len ) );
2618 PSA_ASSERT( psa_mac_sign_finish( &operation,
2619 actual_mac, mac_buffer_size,
2620 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002621
2622 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002623 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2624 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002625
2626 /* Verify that the end of the buffer is untouched. */
2627 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2628 sizeof( actual_mac ) - mac_length ) );
2629
2630exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002631 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002632 mbedtls_psa_crypto_free( );
2633}
2634/* END_CASE */
2635
2636/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002637void mac_verify( int key_type_arg,
2638 data_t *key,
2639 int alg_arg,
2640 data_t *input,
2641 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002642{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002643 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002644 psa_key_type_t key_type = key_type_arg;
2645 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002646 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002647 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002648
Gilles Peskine69c12672018-06-28 00:07:19 +02002649 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2650
Gilles Peskine8817f612018-12-18 00:18:46 +01002651 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002652
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002653 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002654 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002655 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002656
Gilles Peskine8817f612018-12-18 00:18:46 +01002657 PSA_ASSERT( psa_import_key( handle, key_type,
2658 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002659
Gilles Peskine8817f612018-12-18 00:18:46 +01002660 PSA_ASSERT( psa_mac_verify_setup( &operation,
2661 handle, alg ) );
2662 PSA_ASSERT( psa_destroy_key( handle ) );
2663 PSA_ASSERT( psa_mac_update( &operation,
2664 input->x, input->len ) );
2665 PSA_ASSERT( psa_mac_verify_finish( &operation,
2666 expected_mac->x,
2667 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002668
2669exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002670 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002671 mbedtls_psa_crypto_free( );
2672}
2673/* END_CASE */
2674
2675/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002676void cipher_operation_init( )
2677{
Jaeden Ameroab439972019-02-15 14:12:05 +00002678 const uint8_t input[1] = { 0 };
2679 unsigned char output[1] = { 0 };
2680 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002681 /* Test each valid way of initializing the object, except for `= {0}`, as
2682 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2683 * though it's OK by the C standard. We could test for this, but we'd need
2684 * to supress the Clang warning for the test. */
2685 psa_cipher_operation_t func = psa_cipher_operation_init( );
2686 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2687 psa_cipher_operation_t zero;
2688
2689 memset( &zero, 0, sizeof( zero ) );
2690
Jaeden Ameroab439972019-02-15 14:12:05 +00002691 /* A freshly-initialized cipher operation should not be usable. */
2692 TEST_EQUAL( psa_cipher_update( &func,
2693 input, sizeof( input ),
2694 output, sizeof( output ),
2695 &output_length ),
2696 PSA_ERROR_BAD_STATE );
2697 TEST_EQUAL( psa_cipher_update( &init,
2698 input, sizeof( input ),
2699 output, sizeof( output ),
2700 &output_length ),
2701 PSA_ERROR_BAD_STATE );
2702 TEST_EQUAL( psa_cipher_update( &zero,
2703 input, sizeof( input ),
2704 output, sizeof( output ),
2705 &output_length ),
2706 PSA_ERROR_BAD_STATE );
2707
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002708 /* A default cipher operation should be abortable without error. */
2709 PSA_ASSERT( psa_cipher_abort( &func ) );
2710 PSA_ASSERT( psa_cipher_abort( &init ) );
2711 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002712}
2713/* END_CASE */
2714
2715/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002716void cipher_setup( int key_type_arg,
2717 data_t *key,
2718 int alg_arg,
2719 int expected_status_arg )
2720{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002721 psa_key_type_t key_type = key_type_arg;
2722 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002723 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002724 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002725 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002726#if defined(KNOWN_SUPPORTED_MAC_ALG)
2727 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2728#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002729
Gilles Peskine8817f612018-12-18 00:18:46 +01002730 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002731
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002732 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2733 &operation, &status ) )
2734 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002735 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002736
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002737 /* The operation object should be reusable. */
2738#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2739 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2740 smoke_test_key_data,
2741 sizeof( smoke_test_key_data ),
2742 KNOWN_SUPPORTED_CIPHER_ALG,
2743 &operation, &status ) )
2744 goto exit;
2745 TEST_EQUAL( status, PSA_SUCCESS );
2746#endif
2747
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002748exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002749 mbedtls_psa_crypto_free( );
2750}
2751/* END_CASE */
2752
2753/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002754void cipher_bad_order( )
2755{
2756 psa_key_handle_t handle = 0;
2757 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2758 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
2759 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2760 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2761 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2762 const uint8_t key[] = {
2763 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2764 0xaa, 0xaa, 0xaa, 0xaa };
2765 const uint8_t text[] = {
2766 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2767 0xbb, 0xbb, 0xbb, 0xbb };
2768 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2769 size_t length = 0;
2770
2771 PSA_ASSERT( psa_crypto_init( ) );
2772 PSA_ASSERT( psa_allocate_key( &handle ) );
2773 psa_key_policy_set_usage( &policy,
2774 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2775 alg );
2776 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2777 PSA_ASSERT( psa_import_key( handle, key_type,
2778 key, sizeof(key) ) );
2779
2780
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002781 /* Call encrypt setup twice in a row. */
2782 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2783 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2784 PSA_ERROR_BAD_STATE );
2785 PSA_ASSERT( psa_cipher_abort( &operation ) );
2786
2787 /* Call decrypt setup twice in a row. */
2788 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2789 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2790 PSA_ERROR_BAD_STATE );
2791 PSA_ASSERT( psa_cipher_abort( &operation ) );
2792
Jaeden Ameroab439972019-02-15 14:12:05 +00002793 /* Generate an IV without calling setup beforehand. */
2794 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2795 buffer, sizeof( buffer ),
2796 &length ),
2797 PSA_ERROR_BAD_STATE );
2798 PSA_ASSERT( psa_cipher_abort( &operation ) );
2799
2800 /* Generate an IV twice in a row. */
2801 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2802 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2803 buffer, sizeof( buffer ),
2804 &length ) );
2805 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2806 buffer, sizeof( buffer ),
2807 &length ),
2808 PSA_ERROR_BAD_STATE );
2809 PSA_ASSERT( psa_cipher_abort( &operation ) );
2810
2811 /* Generate an IV after it's already set. */
2812 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2813 PSA_ASSERT( psa_cipher_set_iv( &operation,
2814 iv, sizeof( iv ) ) );
2815 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2816 buffer, sizeof( buffer ),
2817 &length ),
2818 PSA_ERROR_BAD_STATE );
2819 PSA_ASSERT( psa_cipher_abort( &operation ) );
2820
2821 /* Set an IV without calling setup beforehand. */
2822 TEST_EQUAL( psa_cipher_set_iv( &operation,
2823 iv, sizeof( iv ) ),
2824 PSA_ERROR_BAD_STATE );
2825 PSA_ASSERT( psa_cipher_abort( &operation ) );
2826
2827 /* Set an IV after it's already set. */
2828 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2829 PSA_ASSERT( psa_cipher_set_iv( &operation,
2830 iv, sizeof( iv ) ) );
2831 TEST_EQUAL( psa_cipher_set_iv( &operation,
2832 iv, sizeof( iv ) ),
2833 PSA_ERROR_BAD_STATE );
2834 PSA_ASSERT( psa_cipher_abort( &operation ) );
2835
2836 /* Set an IV after it's already generated. */
2837 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2838 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2839 buffer, sizeof( buffer ),
2840 &length ) );
2841 TEST_EQUAL( psa_cipher_set_iv( &operation,
2842 iv, sizeof( iv ) ),
2843 PSA_ERROR_BAD_STATE );
2844 PSA_ASSERT( psa_cipher_abort( &operation ) );
2845
2846 /* Call update without calling setup beforehand. */
2847 TEST_EQUAL( psa_cipher_update( &operation,
2848 text, sizeof( text ),
2849 buffer, sizeof( buffer ),
2850 &length ),
2851 PSA_ERROR_BAD_STATE );
2852 PSA_ASSERT( psa_cipher_abort( &operation ) );
2853
2854 /* Call update without an IV where an IV is required. */
2855 TEST_EQUAL( psa_cipher_update( &operation,
2856 text, sizeof( text ),
2857 buffer, sizeof( buffer ),
2858 &length ),
2859 PSA_ERROR_BAD_STATE );
2860 PSA_ASSERT( psa_cipher_abort( &operation ) );
2861
2862 /* Call update after finish. */
2863 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2864 PSA_ASSERT( psa_cipher_set_iv( &operation,
2865 iv, sizeof( iv ) ) );
2866 PSA_ASSERT( psa_cipher_finish( &operation,
2867 buffer, sizeof( buffer ), &length ) );
2868 TEST_EQUAL( psa_cipher_update( &operation,
2869 text, sizeof( text ),
2870 buffer, sizeof( buffer ),
2871 &length ),
2872 PSA_ERROR_BAD_STATE );
2873 PSA_ASSERT( psa_cipher_abort( &operation ) );
2874
2875 /* Call finish without calling setup beforehand. */
2876 TEST_EQUAL( psa_cipher_finish( &operation,
2877 buffer, sizeof( buffer ), &length ),
2878 PSA_ERROR_BAD_STATE );
2879 PSA_ASSERT( psa_cipher_abort( &operation ) );
2880
2881 /* Call finish without an IV where an IV is required. */
2882 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2883 /* Not calling update means we are encrypting an empty buffer, which is OK
2884 * for cipher modes with padding. */
2885 TEST_EQUAL( psa_cipher_finish( &operation,
2886 buffer, sizeof( buffer ), &length ),
2887 PSA_ERROR_BAD_STATE );
2888 PSA_ASSERT( psa_cipher_abort( &operation ) );
2889
2890 /* Call finish twice in a row. */
2891 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2892 PSA_ASSERT( psa_cipher_set_iv( &operation,
2893 iv, sizeof( iv ) ) );
2894 PSA_ASSERT( psa_cipher_finish( &operation,
2895 buffer, sizeof( buffer ), &length ) );
2896 TEST_EQUAL( psa_cipher_finish( &operation,
2897 buffer, sizeof( buffer ), &length ),
2898 PSA_ERROR_BAD_STATE );
2899 PSA_ASSERT( psa_cipher_abort( &operation ) );
2900
2901exit:
2902 mbedtls_psa_crypto_free( );
2903}
2904/* END_CASE */
2905
2906/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002907void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002908 data_t *key,
2909 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002910 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002911{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002912 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002913 psa_status_t status;
2914 psa_key_type_t key_type = key_type_arg;
2915 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002916 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002917 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002918 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002919 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002920 size_t output_buffer_size = 0;
2921 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002922 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002923 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002924 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002925
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002926 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2927 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002928
Gilles Peskine8817f612018-12-18 00:18:46 +01002929 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002930
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002931 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002932 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002933 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002934
Gilles Peskine8817f612018-12-18 00:18:46 +01002935 PSA_ASSERT( psa_import_key( handle, key_type,
2936 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002937
Gilles Peskine8817f612018-12-18 00:18:46 +01002938 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2939 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002940
Gilles Peskine8817f612018-12-18 00:18:46 +01002941 PSA_ASSERT( psa_cipher_set_iv( &operation,
2942 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002943 output_buffer_size = ( (size_t) input->len +
2944 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002945 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002946
Gilles Peskine8817f612018-12-18 00:18:46 +01002947 PSA_ASSERT( psa_cipher_update( &operation,
2948 input->x, input->len,
2949 output, output_buffer_size,
2950 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002951 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002952 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002953 output + total_output_length,
2954 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002955 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002956 total_output_length += function_output_length;
2957
Gilles Peskinefe11b722018-12-18 00:24:04 +01002958 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002959 if( expected_status == PSA_SUCCESS )
2960 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002961 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002962 ASSERT_COMPARE( expected_output->x, expected_output->len,
2963 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002964 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002965
Gilles Peskine50e586b2018-06-08 14:28:46 +02002966exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002967 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002968 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002969 mbedtls_psa_crypto_free( );
2970}
2971/* END_CASE */
2972
2973/* BEGIN_CASE */
2974void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002975 data_t *key,
2976 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002977 int first_part_size_arg,
2978 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002979 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002980{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002981 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002982 psa_key_type_t key_type = key_type_arg;
2983 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002984 size_t first_part_size = first_part_size_arg;
2985 size_t output1_length = output1_length_arg;
2986 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002987 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002988 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002989 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002990 size_t output_buffer_size = 0;
2991 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002992 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002993 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002994 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002995
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002996 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2997 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002998
Gilles Peskine8817f612018-12-18 00:18:46 +01002999 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003000
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003001 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003002 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003003 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003004
Gilles Peskine8817f612018-12-18 00:18:46 +01003005 PSA_ASSERT( psa_import_key( handle, key_type,
3006 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003007
Gilles Peskine8817f612018-12-18 00:18:46 +01003008 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3009 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003010
Gilles Peskine8817f612018-12-18 00:18:46 +01003011 PSA_ASSERT( psa_cipher_set_iv( &operation,
3012 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003013 output_buffer_size = ( (size_t) input->len +
3014 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003015 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003016
Gilles Peskinee0866522019-02-19 19:44:00 +01003017 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003018 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3019 output, output_buffer_size,
3020 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003021 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003022 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003023 PSA_ASSERT( psa_cipher_update( &operation,
3024 input->x + first_part_size,
3025 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003026 output + total_output_length,
3027 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003028 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003029 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003030 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003031 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003032 output + total_output_length,
3033 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003034 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003035 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003036 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003037
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003038 ASSERT_COMPARE( expected_output->x, expected_output->len,
3039 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003040
3041exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003042 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003043 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003044 mbedtls_psa_crypto_free( );
3045}
3046/* END_CASE */
3047
3048/* BEGIN_CASE */
3049void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003050 data_t *key,
3051 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003052 int first_part_size_arg,
3053 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003054 data_t *expected_output )
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
3058 psa_key_type_t key_type = key_type_arg;
3059 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003060 size_t first_part_size = first_part_size_arg;
3061 size_t output1_length = output1_length_arg;
3062 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003063 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003064 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003065 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003066 size_t output_buffer_size = 0;
3067 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003068 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003069 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003070 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003071
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003072 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3073 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003074
Gilles Peskine8817f612018-12-18 00:18:46 +01003075 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003076
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003077 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003078 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003079 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003080
Gilles Peskine8817f612018-12-18 00:18:46 +01003081 PSA_ASSERT( psa_import_key( handle, key_type,
3082 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003083
Gilles Peskine8817f612018-12-18 00:18:46 +01003084 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3085 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003086
Gilles Peskine8817f612018-12-18 00:18:46 +01003087 PSA_ASSERT( psa_cipher_set_iv( &operation,
3088 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003089
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003090 output_buffer_size = ( (size_t) input->len +
3091 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003092 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003093
Gilles Peskinee0866522019-02-19 19:44:00 +01003094 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003095 PSA_ASSERT( psa_cipher_update( &operation,
3096 input->x, first_part_size,
3097 output, output_buffer_size,
3098 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003099 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003100 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003101 PSA_ASSERT( psa_cipher_update( &operation,
3102 input->x + first_part_size,
3103 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003104 output + total_output_length,
3105 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003106 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003107 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003108 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003109 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003110 output + total_output_length,
3111 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003112 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003113 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003114 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003115
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003116 ASSERT_COMPARE( expected_output->x, expected_output->len,
3117 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003118
3119exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003120 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003121 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003122 mbedtls_psa_crypto_free( );
3123}
3124/* END_CASE */
3125
Gilles Peskine50e586b2018-06-08 14:28:46 +02003126/* BEGIN_CASE */
3127void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003128 data_t *key,
3129 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003130 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003131{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003132 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003133 psa_status_t status;
3134 psa_key_type_t key_type = key_type_arg;
3135 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003136 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003137 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003138 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003139 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003140 size_t output_buffer_size = 0;
3141 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003142 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003143 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003144 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003145
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003146 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3147 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003148
Gilles Peskine8817f612018-12-18 00:18:46 +01003149 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003150
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003151 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003152 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003153 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003154
Gilles Peskine8817f612018-12-18 00:18:46 +01003155 PSA_ASSERT( psa_import_key( handle, key_type,
3156 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003157
Gilles Peskine8817f612018-12-18 00:18:46 +01003158 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3159 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003160
Gilles Peskine8817f612018-12-18 00:18:46 +01003161 PSA_ASSERT( psa_cipher_set_iv( &operation,
3162 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003163
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003164 output_buffer_size = ( (size_t) input->len +
3165 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003166 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003167
Gilles Peskine8817f612018-12-18 00:18:46 +01003168 PSA_ASSERT( psa_cipher_update( &operation,
3169 input->x, input->len,
3170 output, output_buffer_size,
3171 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003172 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003173 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003174 output + total_output_length,
3175 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003176 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003177 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003178 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003179
3180 if( expected_status == PSA_SUCCESS )
3181 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003182 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003183 ASSERT_COMPARE( expected_output->x, expected_output->len,
3184 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003185 }
3186
Gilles Peskine50e586b2018-06-08 14:28:46 +02003187exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003188 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003189 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003190 mbedtls_psa_crypto_free( );
3191}
3192/* END_CASE */
3193
Gilles Peskine50e586b2018-06-08 14:28:46 +02003194/* BEGIN_CASE */
3195void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003196 data_t *key,
3197 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003198{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003199 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003200 psa_key_type_t key_type = key_type_arg;
3201 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003202 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003203 size_t iv_size = 16;
3204 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003205 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003206 size_t output1_size = 0;
3207 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003208 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003209 size_t output2_size = 0;
3210 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003211 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003212 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3213 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003214 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003215
Gilles Peskine8817f612018-12-18 00:18:46 +01003216 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003217
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003218 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003219 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003220 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003221
Gilles Peskine8817f612018-12-18 00:18:46 +01003222 PSA_ASSERT( psa_import_key( handle, key_type,
3223 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003224
Gilles Peskine8817f612018-12-18 00:18:46 +01003225 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3226 handle, alg ) );
3227 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3228 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003229
Gilles Peskine8817f612018-12-18 00:18:46 +01003230 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3231 iv, iv_size,
3232 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003233 output1_size = ( (size_t) input->len +
3234 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003235 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003236
Gilles Peskine8817f612018-12-18 00:18:46 +01003237 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3238 output1, output1_size,
3239 &output1_length ) );
3240 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003241 output1 + output1_length,
3242 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003243 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003244
Gilles Peskine048b7f02018-06-08 14:20:49 +02003245 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003246
Gilles Peskine8817f612018-12-18 00:18:46 +01003247 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003248
3249 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003250 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003251
Gilles Peskine8817f612018-12-18 00:18:46 +01003252 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3253 iv, iv_length ) );
3254 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3255 output2, output2_size,
3256 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003257 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003258 PSA_ASSERT( psa_cipher_finish( &operation2,
3259 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003260 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003261 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003262
Gilles Peskine048b7f02018-06-08 14:20:49 +02003263 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003264
Gilles Peskine8817f612018-12-18 00:18:46 +01003265 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003266
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003267 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003268
3269exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003270 mbedtls_free( output1 );
3271 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003272 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03003273 mbedtls_psa_crypto_free( );
3274}
3275/* END_CASE */
3276
3277/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003278void cipher_verify_output_multipart( int alg_arg,
3279 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003280 data_t *key,
3281 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003282 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003283{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003284 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003285 psa_key_type_t key_type = key_type_arg;
3286 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003287 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003288 unsigned char iv[16] = {0};
3289 size_t iv_size = 16;
3290 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003291 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003292 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003293 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003294 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003295 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003296 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003297 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003298 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3299 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003300 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003301
Gilles Peskine8817f612018-12-18 00:18:46 +01003302 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003303
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003304 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003305 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003306 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003307
Gilles Peskine8817f612018-12-18 00:18:46 +01003308 PSA_ASSERT( psa_import_key( handle, key_type,
3309 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003310
Gilles Peskine8817f612018-12-18 00:18:46 +01003311 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3312 handle, alg ) );
3313 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3314 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003315
Gilles Peskine8817f612018-12-18 00:18:46 +01003316 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3317 iv, iv_size,
3318 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003319 output1_buffer_size = ( (size_t) input->len +
3320 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003321 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003322
Gilles Peskinee0866522019-02-19 19:44:00 +01003323 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003324
Gilles Peskine8817f612018-12-18 00:18:46 +01003325 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3326 output1, output1_buffer_size,
3327 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003328 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003329
Gilles Peskine8817f612018-12-18 00:18:46 +01003330 PSA_ASSERT( psa_cipher_update( &operation1,
3331 input->x + first_part_size,
3332 input->len - first_part_size,
3333 output1, output1_buffer_size,
3334 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003335 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003336
Gilles Peskine8817f612018-12-18 00:18:46 +01003337 PSA_ASSERT( psa_cipher_finish( &operation1,
3338 output1 + output1_length,
3339 output1_buffer_size - output1_length,
3340 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003341 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003342
Gilles Peskine8817f612018-12-18 00:18:46 +01003343 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003344
Gilles Peskine048b7f02018-06-08 14:20:49 +02003345 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003346 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003347
Gilles Peskine8817f612018-12-18 00:18:46 +01003348 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3349 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003350
Gilles Peskine8817f612018-12-18 00:18:46 +01003351 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3352 output2, output2_buffer_size,
3353 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003354 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003355
Gilles Peskine8817f612018-12-18 00:18:46 +01003356 PSA_ASSERT( psa_cipher_update( &operation2,
3357 output1 + first_part_size,
3358 output1_length - first_part_size,
3359 output2, output2_buffer_size,
3360 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003361 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003362
Gilles Peskine8817f612018-12-18 00:18:46 +01003363 PSA_ASSERT( psa_cipher_finish( &operation2,
3364 output2 + output2_length,
3365 output2_buffer_size - output2_length,
3366 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003367 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003368
Gilles Peskine8817f612018-12-18 00:18:46 +01003369 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003370
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003371 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003372
3373exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003374 mbedtls_free( output1 );
3375 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003376 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003377 mbedtls_psa_crypto_free( );
3378}
3379/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003380
Gilles Peskine20035e32018-02-03 22:44:14 +01003381/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003382void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003383 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003384 data_t *nonce,
3385 data_t *additional_data,
3386 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003387 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003388{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003389 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003390 psa_key_type_t key_type = key_type_arg;
3391 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003392 unsigned char *output_data = NULL;
3393 size_t output_size = 0;
3394 size_t output_length = 0;
3395 unsigned char *output_data2 = NULL;
3396 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003397 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003398 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003399 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003400
Gilles Peskine4abf7412018-06-18 16:35:34 +02003401 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003402 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003403
Gilles Peskine8817f612018-12-18 00:18:46 +01003404 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003405
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003406 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003407 psa_key_policy_set_usage( &policy,
3408 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
3409 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003410 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003411
Gilles Peskine8817f612018-12-18 00:18:46 +01003412 PSA_ASSERT( psa_import_key( handle, key_type,
3413 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003414
Gilles Peskinefe11b722018-12-18 00:24:04 +01003415 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3416 nonce->x, nonce->len,
3417 additional_data->x,
3418 additional_data->len,
3419 input_data->x, input_data->len,
3420 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003421 &output_length ),
3422 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003423
3424 if( PSA_SUCCESS == expected_result )
3425 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003426 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003427
Gilles Peskinefe11b722018-12-18 00:24:04 +01003428 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3429 nonce->x, nonce->len,
3430 additional_data->x,
3431 additional_data->len,
3432 output_data, output_length,
3433 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003434 &output_length2 ),
3435 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003436
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003437 ASSERT_COMPARE( input_data->x, input_data->len,
3438 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003439 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003440
Gilles Peskinea1cac842018-06-11 19:33:02 +02003441exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003442 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003443 mbedtls_free( output_data );
3444 mbedtls_free( output_data2 );
3445 mbedtls_psa_crypto_free( );
3446}
3447/* END_CASE */
3448
3449/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003450void aead_encrypt( int key_type_arg, data_t *key_data,
3451 int alg_arg,
3452 data_t *nonce,
3453 data_t *additional_data,
3454 data_t *input_data,
3455 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003456{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003457 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003458 psa_key_type_t key_type = key_type_arg;
3459 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003460 unsigned char *output_data = NULL;
3461 size_t output_size = 0;
3462 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003463 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00003464 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003465
Gilles Peskine4abf7412018-06-18 16:35:34 +02003466 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003467 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003468
Gilles Peskine8817f612018-12-18 00:18:46 +01003469 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003470
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003471 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003472 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003473 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003474
Gilles Peskine8817f612018-12-18 00:18:46 +01003475 PSA_ASSERT( psa_import_key( handle, key_type,
3476 key_data->x,
3477 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003478
Gilles Peskine8817f612018-12-18 00:18:46 +01003479 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3480 nonce->x, nonce->len,
3481 additional_data->x, additional_data->len,
3482 input_data->x, input_data->len,
3483 output_data, output_size,
3484 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003485
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003486 ASSERT_COMPARE( expected_result->x, expected_result->len,
3487 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003488
Gilles Peskinea1cac842018-06-11 19:33:02 +02003489exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003490 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003491 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003492 mbedtls_psa_crypto_free( );
3493}
3494/* END_CASE */
3495
3496/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003497void aead_decrypt( int key_type_arg, data_t *key_data,
3498 int alg_arg,
3499 data_t *nonce,
3500 data_t *additional_data,
3501 data_t *input_data,
3502 data_t *expected_data,
3503 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003504{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003505 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003506 psa_key_type_t key_type = key_type_arg;
3507 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003508 unsigned char *output_data = NULL;
3509 size_t output_size = 0;
3510 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003511 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00003512 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003513 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003514
Gilles Peskine4abf7412018-06-18 16:35:34 +02003515 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003516 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003517
Gilles Peskine8817f612018-12-18 00:18:46 +01003518 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003519
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003520 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003521 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003522 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003523
Gilles Peskine8817f612018-12-18 00:18:46 +01003524 PSA_ASSERT( psa_import_key( handle, key_type,
3525 key_data->x,
3526 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003527
Gilles Peskinefe11b722018-12-18 00:24:04 +01003528 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3529 nonce->x, nonce->len,
3530 additional_data->x,
3531 additional_data->len,
3532 input_data->x, input_data->len,
3533 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003534 &output_length ),
3535 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003536
Gilles Peskine2d277862018-06-18 15:41:12 +02003537 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003538 ASSERT_COMPARE( expected_data->x, expected_data->len,
3539 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003540
Gilles Peskinea1cac842018-06-11 19:33:02 +02003541exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003542 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003543 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003544 mbedtls_psa_crypto_free( );
3545}
3546/* END_CASE */
3547
3548/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003549void signature_size( int type_arg,
3550 int bits,
3551 int alg_arg,
3552 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003553{
3554 psa_key_type_t type = type_arg;
3555 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003556 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003557 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003558exit:
3559 ;
3560}
3561/* END_CASE */
3562
3563/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003564void sign_deterministic( int key_type_arg, data_t *key_data,
3565 int alg_arg, data_t *input_data,
3566 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003567{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003568 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003569 psa_key_type_t key_type = key_type_arg;
3570 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003571 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003572 unsigned char *signature = NULL;
3573 size_t signature_size;
3574 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003575 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003576
Gilles Peskine8817f612018-12-18 00:18:46 +01003577 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003578
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003579 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003580 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003581 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003582
Gilles Peskine8817f612018-12-18 00:18:46 +01003583 PSA_ASSERT( psa_import_key( handle, key_type,
3584 key_data->x,
3585 key_data->len ) );
3586 PSA_ASSERT( psa_get_key_information( handle,
3587 NULL,
3588 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003589
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003590 /* Allocate a buffer which has the size advertized by the
3591 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003592 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3593 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003594 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003595 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003596 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003597
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003598 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003599 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3600 input_data->x, input_data->len,
3601 signature, signature_size,
3602 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003603 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003604 ASSERT_COMPARE( output_data->x, output_data->len,
3605 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003606
3607exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003608 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003609 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01003610 mbedtls_psa_crypto_free( );
3611}
3612/* END_CASE */
3613
3614/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003615void sign_fail( int key_type_arg, data_t *key_data,
3616 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003617 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003618{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003619 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003620 psa_key_type_t key_type = key_type_arg;
3621 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003622 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003623 psa_status_t actual_status;
3624 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003625 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003626 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003627 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003628
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003629 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003630
Gilles Peskine8817f612018-12-18 00:18:46 +01003631 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003632
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003633 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003634 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003635 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003636
Gilles Peskine8817f612018-12-18 00:18:46 +01003637 PSA_ASSERT( psa_import_key( handle, key_type,
3638 key_data->x,
3639 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003640
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003641 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003642 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003643 signature, signature_size,
3644 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003645 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003646 /* The value of *signature_length is unspecified on error, but
3647 * whatever it is, it should be less than signature_size, so that
3648 * if the caller tries to read *signature_length bytes without
3649 * checking the error code then they don't overflow a buffer. */
3650 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003651
3652exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003653 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003654 mbedtls_free( signature );
3655 mbedtls_psa_crypto_free( );
3656}
3657/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003658
3659/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003660void sign_verify( int key_type_arg, data_t *key_data,
3661 int alg_arg, data_t *input_data )
3662{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003663 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003664 psa_key_type_t key_type = key_type_arg;
3665 psa_algorithm_t alg = alg_arg;
3666 size_t key_bits;
3667 unsigned char *signature = NULL;
3668 size_t signature_size;
3669 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003670 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003671
Gilles Peskine8817f612018-12-18 00:18:46 +01003672 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003673
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003674 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003675 psa_key_policy_set_usage( &policy,
3676 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
3677 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003678 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003679
Gilles Peskine8817f612018-12-18 00:18:46 +01003680 PSA_ASSERT( psa_import_key( handle, key_type,
3681 key_data->x,
3682 key_data->len ) );
3683 PSA_ASSERT( psa_get_key_information( handle,
3684 NULL,
3685 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003686
3687 /* Allocate a buffer which has the size advertized by the
3688 * library. */
3689 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3690 key_bits, alg );
3691 TEST_ASSERT( signature_size != 0 );
3692 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003693 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003694
3695 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003696 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3697 input_data->x, input_data->len,
3698 signature, signature_size,
3699 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003700 /* Check that the signature length looks sensible. */
3701 TEST_ASSERT( signature_length <= signature_size );
3702 TEST_ASSERT( signature_length > 0 );
3703
3704 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003705 PSA_ASSERT( psa_asymmetric_verify(
3706 handle, alg,
3707 input_data->x, input_data->len,
3708 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003709
3710 if( input_data->len != 0 )
3711 {
3712 /* Flip a bit in the input and verify that the signature is now
3713 * detected as invalid. Flip a bit at the beginning, not at the end,
3714 * because ECDSA may ignore the last few bits of the input. */
3715 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003716 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3717 input_data->x, input_data->len,
3718 signature, signature_length ),
3719 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003720 }
3721
3722exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003723 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003724 mbedtls_free( signature );
3725 mbedtls_psa_crypto_free( );
3726}
3727/* END_CASE */
3728
3729/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003730void asymmetric_verify( int key_type_arg, data_t *key_data,
3731 int alg_arg, data_t *hash_data,
3732 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003733{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003734 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003735 psa_key_type_t key_type = key_type_arg;
3736 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003737 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003738
Gilles Peskine69c12672018-06-28 00:07:19 +02003739 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3740
Gilles Peskine8817f612018-12-18 00:18:46 +01003741 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003742
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003743 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003744 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003745 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03003746
Gilles Peskine8817f612018-12-18 00:18:46 +01003747 PSA_ASSERT( psa_import_key( handle, key_type,
3748 key_data->x,
3749 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003750
Gilles Peskine8817f612018-12-18 00:18:46 +01003751 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3752 hash_data->x, hash_data->len,
3753 signature_data->x,
3754 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003755exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003756 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003757 mbedtls_psa_crypto_free( );
3758}
3759/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003760
3761/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003762void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3763 int alg_arg, data_t *hash_data,
3764 data_t *signature_data,
3765 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003766{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003767 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003768 psa_key_type_t key_type = key_type_arg;
3769 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003770 psa_status_t actual_status;
3771 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003772 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003773
Gilles Peskine8817f612018-12-18 00:18:46 +01003774 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003775
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003776 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003777 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003778 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003779
Gilles Peskine8817f612018-12-18 00:18:46 +01003780 PSA_ASSERT( psa_import_key( handle, key_type,
3781 key_data->x,
3782 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003783
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003784 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003785 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003786 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003787 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003788
Gilles Peskinefe11b722018-12-18 00:24:04 +01003789 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003790
3791exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003792 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003793 mbedtls_psa_crypto_free( );
3794}
3795/* END_CASE */
3796
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003797/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003798void asymmetric_encrypt( int key_type_arg,
3799 data_t *key_data,
3800 int alg_arg,
3801 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003802 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003803 int expected_output_length_arg,
3804 int expected_status_arg )
3805{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003806 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003807 psa_key_type_t key_type = key_type_arg;
3808 psa_algorithm_t alg = alg_arg;
3809 size_t expected_output_length = expected_output_length_arg;
3810 size_t key_bits;
3811 unsigned char *output = NULL;
3812 size_t output_size;
3813 size_t output_length = ~0;
3814 psa_status_t actual_status;
3815 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003816 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003817
Gilles Peskine8817f612018-12-18 00:18:46 +01003818 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003819
Gilles Peskine656896e2018-06-29 19:12:28 +02003820 /* Import the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003821 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003822 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003823 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3824 PSA_ASSERT( psa_import_key( handle, key_type,
3825 key_data->x,
3826 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003827
3828 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003829 PSA_ASSERT( psa_get_key_information( handle,
3830 NULL,
3831 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003832 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003833 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003834
3835 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003836 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003837 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003838 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003839 output, output_size,
3840 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003841 TEST_EQUAL( actual_status, expected_status );
3842 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003843
Gilles Peskine68428122018-06-30 18:42:41 +02003844 /* If the label is empty, the test framework puts a non-null pointer
3845 * in label->x. Test that a null pointer works as well. */
3846 if( label->len == 0 )
3847 {
3848 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003849 if( output_size != 0 )
3850 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003851 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003852 input_data->x, input_data->len,
3853 NULL, label->len,
3854 output, output_size,
3855 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003856 TEST_EQUAL( actual_status, expected_status );
3857 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003858 }
3859
Gilles Peskine656896e2018-06-29 19:12:28 +02003860exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003861 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003862 mbedtls_free( output );
3863 mbedtls_psa_crypto_free( );
3864}
3865/* END_CASE */
3866
3867/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003868void asymmetric_encrypt_decrypt( int key_type_arg,
3869 data_t *key_data,
3870 int alg_arg,
3871 data_t *input_data,
3872 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003873{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003874 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003875 psa_key_type_t key_type = key_type_arg;
3876 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003877 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003878 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003879 size_t output_size;
3880 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003881 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003882 size_t output2_size;
3883 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003884 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003885
Gilles Peskine8817f612018-12-18 00:18:46 +01003886 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003887
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003888 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003889 psa_key_policy_set_usage( &policy,
3890 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003891 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003892 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003893
Gilles Peskine8817f612018-12-18 00:18:46 +01003894 PSA_ASSERT( psa_import_key( handle, key_type,
3895 key_data->x,
3896 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003897
3898 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003899 PSA_ASSERT( psa_get_key_information( handle,
3900 NULL,
3901 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003902 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003903 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003904 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003905 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003906
Gilles Peskineeebd7382018-06-08 18:11:54 +02003907 /* We test encryption by checking that encrypt-then-decrypt gives back
3908 * the original plaintext because of the non-optional random
3909 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003910 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3911 input_data->x, input_data->len,
3912 label->x, label->len,
3913 output, output_size,
3914 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003915 /* We don't know what ciphertext length to expect, but check that
3916 * it looks sensible. */
3917 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003918
Gilles Peskine8817f612018-12-18 00:18:46 +01003919 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3920 output, output_length,
3921 label->x, label->len,
3922 output2, output2_size,
3923 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003924 ASSERT_COMPARE( input_data->x, input_data->len,
3925 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003926
3927exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003928 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003929 mbedtls_free( output );
3930 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003931 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003932}
3933/* END_CASE */
3934
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003935/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003936void asymmetric_decrypt( int key_type_arg,
3937 data_t *key_data,
3938 int alg_arg,
3939 data_t *input_data,
3940 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003941 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003942{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003943 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003944 psa_key_type_t key_type = key_type_arg;
3945 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003946 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003947 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003948 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003949 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003950
Jaeden Amero412654a2019-02-06 12:57:46 +00003951 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003952 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003953
Gilles Peskine8817f612018-12-18 00:18:46 +01003954 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003955
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003956 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003957 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003958 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003959
Gilles Peskine8817f612018-12-18 00:18:46 +01003960 PSA_ASSERT( psa_import_key( handle, key_type,
3961 key_data->x,
3962 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003963
Gilles Peskine8817f612018-12-18 00:18:46 +01003964 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3965 input_data->x, input_data->len,
3966 label->x, label->len,
3967 output,
3968 output_size,
3969 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003970 ASSERT_COMPARE( expected_data->x, expected_data->len,
3971 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003972
Gilles Peskine68428122018-06-30 18:42:41 +02003973 /* If the label is empty, the test framework puts a non-null pointer
3974 * in label->x. Test that a null pointer works as well. */
3975 if( label->len == 0 )
3976 {
3977 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003978 if( output_size != 0 )
3979 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003980 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3981 input_data->x, input_data->len,
3982 NULL, label->len,
3983 output,
3984 output_size,
3985 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003986 ASSERT_COMPARE( expected_data->x, expected_data->len,
3987 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003988 }
3989
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003990exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003991 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003992 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003993 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003994}
3995/* END_CASE */
3996
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003997/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003998void asymmetric_decrypt_fail( int key_type_arg,
3999 data_t *key_data,
4000 int alg_arg,
4001 data_t *input_data,
4002 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004003 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004004 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004005{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004006 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004007 psa_key_type_t key_type = key_type_arg;
4008 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004009 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004010 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004011 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004012 psa_status_t actual_status;
4013 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004014 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004015
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004016 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004017
Gilles Peskine8817f612018-12-18 00:18:46 +01004018 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004019
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004020 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02004021 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004022 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004023
Gilles Peskine8817f612018-12-18 00:18:46 +01004024 PSA_ASSERT( psa_import_key( handle, key_type,
4025 key_data->x,
4026 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004027
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004028 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004029 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004030 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004031 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004032 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004033 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004034 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004035
Gilles Peskine68428122018-06-30 18:42:41 +02004036 /* If the label is empty, the test framework puts a non-null pointer
4037 * in label->x. Test that a null pointer works as well. */
4038 if( label->len == 0 )
4039 {
4040 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004041 if( output_size != 0 )
4042 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004043 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004044 input_data->x, input_data->len,
4045 NULL, label->len,
4046 output, output_size,
4047 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004048 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004049 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004050 }
4051
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004052exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004053 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004054 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004055 mbedtls_psa_crypto_free( );
4056}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004057/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004058
4059/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00004060void crypto_generator_init( )
4061{
4062 /* Test each valid way of initializing the object, except for `= {0}`, as
4063 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4064 * though it's OK by the C standard. We could test for this, but we'd need
4065 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004066 size_t capacity;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004067 psa_crypto_generator_t func = psa_crypto_generator_init( );
4068 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
4069 psa_crypto_generator_t zero;
4070
4071 memset( &zero, 0, sizeof( zero ) );
4072
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004073 /* A default generator should not be able to report its capacity. */
4074 TEST_EQUAL( psa_get_generator_capacity( &func, &capacity ),
4075 PSA_ERROR_BAD_STATE );
4076 TEST_EQUAL( psa_get_generator_capacity( &init, &capacity ),
4077 PSA_ERROR_BAD_STATE );
4078 TEST_EQUAL( psa_get_generator_capacity( &zero, &capacity ),
4079 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004080
4081 /* A default generator should be abortable without error. */
4082 PSA_ASSERT( psa_generator_abort(&func) );
4083 PSA_ASSERT( psa_generator_abort(&init) );
4084 PSA_ASSERT( psa_generator_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004085}
4086/* END_CASE */
4087
4088/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004089void derive_setup( int key_type_arg,
4090 data_t *key_data,
4091 int alg_arg,
4092 data_t *salt,
4093 data_t *label,
4094 int requested_capacity_arg,
4095 int expected_status_arg )
4096{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004097 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004098 size_t key_type = key_type_arg;
4099 psa_algorithm_t alg = alg_arg;
4100 size_t requested_capacity = requested_capacity_arg;
4101 psa_status_t expected_status = expected_status_arg;
4102 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004103 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004104
Gilles Peskine8817f612018-12-18 00:18:46 +01004105 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004106
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004107 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004108 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004109 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004110
Gilles Peskine8817f612018-12-18 00:18:46 +01004111 PSA_ASSERT( psa_import_key( handle, key_type,
4112 key_data->x,
4113 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004114
Gilles Peskinefe11b722018-12-18 00:24:04 +01004115 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4116 salt->x, salt->len,
4117 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004118 requested_capacity ),
4119 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004120
4121exit:
4122 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004123 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004124 mbedtls_psa_crypto_free( );
4125}
4126/* END_CASE */
4127
4128/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004129void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004130{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004131 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004132 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004133 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004134 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004135 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004136 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004137 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4138 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4139 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00004140 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004141
Gilles Peskine8817f612018-12-18 00:18:46 +01004142 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004143
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004144 PSA_ASSERT( psa_allocate_key( &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004145 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004146 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004147
Gilles Peskine8817f612018-12-18 00:18:46 +01004148 PSA_ASSERT( psa_import_key( handle, key_type,
4149 key_data,
4150 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004151
4152 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01004153 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4154 NULL, 0,
4155 NULL, 0,
4156 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004157
4158 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01004159 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4160 NULL, 0,
4161 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004162 capacity ),
4163 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004164
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004165 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004166
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004167 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004168 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004169
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004170exit:
4171 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004172 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004173 mbedtls_psa_crypto_free( );
4174}
4175/* END_CASE */
4176
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004177/* BEGIN_CASE */
4178void test_derive_invalid_generator_tests( )
4179{
4180 uint8_t output_buffer[16];
4181 size_t buffer_size = 16;
4182 size_t capacity = 0;
4183 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4184
Nir Sonnenschein50789302018-10-31 12:16:38 +02004185 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004186 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004187
4188 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004189 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004190
Gilles Peskine8817f612018-12-18 00:18:46 +01004191 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004192
Nir Sonnenschein50789302018-10-31 12:16:38 +02004193 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004194 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004195
Nir Sonnenschein50789302018-10-31 12:16:38 +02004196 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004197 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004198
4199exit:
4200 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004201}
4202/* END_CASE */
4203
4204/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004205void derive_output( int alg_arg,
4206 data_t *key_data,
4207 data_t *salt,
4208 data_t *label,
4209 int requested_capacity_arg,
4210 data_t *expected_output1,
4211 data_t *expected_output2 )
4212{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004213 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004214 psa_algorithm_t alg = alg_arg;
4215 size_t requested_capacity = requested_capacity_arg;
4216 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4217 uint8_t *expected_outputs[2] =
4218 {expected_output1->x, expected_output2->x};
4219 size_t output_sizes[2] =
4220 {expected_output1->len, expected_output2->len};
4221 size_t output_buffer_size = 0;
4222 uint8_t *output_buffer = NULL;
4223 size_t expected_capacity;
4224 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00004225 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004226 psa_status_t status;
4227 unsigned i;
4228
4229 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4230 {
4231 if( output_sizes[i] > output_buffer_size )
4232 output_buffer_size = output_sizes[i];
4233 if( output_sizes[i] == 0 )
4234 expected_outputs[i] = NULL;
4235 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004236 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004237 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004238
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004239 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004240 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004241 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004242
Gilles Peskine8817f612018-12-18 00:18:46 +01004243 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
4244 key_data->x,
4245 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004246
4247 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004248 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4249 salt->x, salt->len,
4250 label->x, label->len,
4251 requested_capacity ) );
4252 PSA_ASSERT( psa_get_generator_capacity( &generator,
4253 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004254 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004255 expected_capacity = requested_capacity;
4256
4257 /* Expansion phase. */
4258 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4259 {
4260 /* Read some bytes. */
4261 status = psa_generator_read( &generator,
4262 output_buffer, output_sizes[i] );
4263 if( expected_capacity == 0 && output_sizes[i] == 0 )
4264 {
4265 /* Reading 0 bytes when 0 bytes are available can go either way. */
4266 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004267 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004268 continue;
4269 }
4270 else if( expected_capacity == 0 ||
4271 output_sizes[i] > expected_capacity )
4272 {
4273 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004274 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004275 expected_capacity = 0;
4276 continue;
4277 }
4278 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004279 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004280 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004281 ASSERT_COMPARE( output_buffer, output_sizes[i],
4282 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004283 /* Check the generator status. */
4284 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01004285 PSA_ASSERT( psa_get_generator_capacity( &generator,
4286 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004287 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004288 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004289 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004290
4291exit:
4292 mbedtls_free( output_buffer );
4293 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004294 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004295 mbedtls_psa_crypto_free( );
4296}
4297/* END_CASE */
4298
4299/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004300void derive_full( int alg_arg,
4301 data_t *key_data,
4302 data_t *salt,
4303 data_t *label,
4304 int requested_capacity_arg )
4305{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004306 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004307 psa_algorithm_t alg = alg_arg;
4308 size_t requested_capacity = requested_capacity_arg;
4309 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4310 unsigned char output_buffer[16];
4311 size_t expected_capacity = requested_capacity;
4312 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00004313 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004314
Gilles Peskine8817f612018-12-18 00:18:46 +01004315 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004316
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004317 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004318 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004319 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004320
Gilles Peskine8817f612018-12-18 00:18:46 +01004321 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
4322 key_data->x,
4323 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004324
4325 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004326 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4327 salt->x, salt->len,
4328 label->x, label->len,
4329 requested_capacity ) );
4330 PSA_ASSERT( psa_get_generator_capacity( &generator,
4331 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004332 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004333
4334 /* Expansion phase. */
4335 while( current_capacity > 0 )
4336 {
4337 size_t read_size = sizeof( output_buffer );
4338 if( read_size > current_capacity )
4339 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01004340 PSA_ASSERT( psa_generator_read( &generator,
4341 output_buffer,
4342 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004343 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01004344 PSA_ASSERT( psa_get_generator_capacity( &generator,
4345 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004346 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004347 }
4348
4349 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004350 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004351 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004352
Gilles Peskine8817f612018-12-18 00:18:46 +01004353 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004354
4355exit:
4356 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004357 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02004358 mbedtls_psa_crypto_free( );
4359}
4360/* END_CASE */
4361
4362/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004363void derive_key_exercise( int alg_arg,
4364 data_t *key_data,
4365 data_t *salt,
4366 data_t *label,
4367 int derived_type_arg,
4368 int derived_bits_arg,
4369 int derived_usage_arg,
4370 int derived_alg_arg )
4371{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004372 psa_key_handle_t base_handle = 0;
4373 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004374 psa_algorithm_t alg = alg_arg;
4375 psa_key_type_t derived_type = derived_type_arg;
4376 size_t derived_bits = derived_bits_arg;
4377 psa_key_usage_t derived_usage = derived_usage_arg;
4378 psa_algorithm_t derived_alg = derived_alg_arg;
4379 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
4380 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004381 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004382 psa_key_type_t got_type;
4383 size_t got_bits;
4384
Gilles Peskine8817f612018-12-18 00:18:46 +01004385 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004386
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004387 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004388 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004389 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
4390 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
4391 key_data->x,
4392 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004393
4394 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004395 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4396 salt->x, salt->len,
4397 label->x, label->len,
4398 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004399 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004400 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004401 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4402 PSA_ASSERT( psa_generator_import_key( derived_handle,
4403 derived_type,
4404 derived_bits,
4405 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004406
4407 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01004408 PSA_ASSERT( psa_get_key_information( derived_handle,
4409 &got_type,
4410 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004411 TEST_EQUAL( got_type, derived_type );
4412 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004413
4414 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004415 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004416 goto exit;
4417
4418exit:
4419 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004420 psa_destroy_key( base_handle );
4421 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004422 mbedtls_psa_crypto_free( );
4423}
4424/* END_CASE */
4425
4426/* BEGIN_CASE */
4427void derive_key_export( int alg_arg,
4428 data_t *key_data,
4429 data_t *salt,
4430 data_t *label,
4431 int bytes1_arg,
4432 int bytes2_arg )
4433{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004434 psa_key_handle_t base_handle = 0;
4435 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004436 psa_algorithm_t alg = alg_arg;
4437 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004438 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004439 size_t bytes2 = bytes2_arg;
4440 size_t capacity = bytes1 + bytes2;
4441 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004442 uint8_t *output_buffer = NULL;
4443 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00004444 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004445 size_t length;
4446
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004447 ASSERT_ALLOC( output_buffer, capacity );
4448 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004449 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004450
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004451 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004452 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004453 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
4454 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
4455 key_data->x,
4456 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004457
4458 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004459 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4460 salt->x, salt->len,
4461 label->x, label->len,
4462 capacity ) );
4463 PSA_ASSERT( psa_generator_read( &generator,
4464 output_buffer,
4465 capacity ) );
4466 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004467
4468 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004469 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4470 salt->x, salt->len,
4471 label->x, label->len,
4472 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004473 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004474 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004475 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4476 PSA_ASSERT( psa_generator_import_key( derived_handle,
4477 PSA_KEY_TYPE_RAW_DATA,
4478 derived_bits,
4479 &generator ) );
4480 PSA_ASSERT( psa_export_key( derived_handle,
4481 export_buffer, bytes1,
4482 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004483 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004484 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004485 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004486 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4487 PSA_ASSERT( psa_generator_import_key( derived_handle,
4488 PSA_KEY_TYPE_RAW_DATA,
4489 PSA_BYTES_TO_BITS( bytes2 ),
4490 &generator ) );
4491 PSA_ASSERT( psa_export_key( derived_handle,
4492 export_buffer + bytes1, bytes2,
4493 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004494 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004495
4496 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004497 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4498 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004499
4500exit:
4501 mbedtls_free( output_buffer );
4502 mbedtls_free( export_buffer );
4503 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004504 psa_destroy_key( base_handle );
4505 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004506 mbedtls_psa_crypto_free( );
4507}
4508/* END_CASE */
4509
4510/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004511void key_agreement_setup( int alg_arg,
4512 int our_key_type_arg, data_t *our_key_data,
4513 data_t *peer_key_data,
4514 int expected_status_arg )
4515{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004516 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004517 psa_algorithm_t alg = alg_arg;
4518 psa_key_type_t our_key_type = our_key_type_arg;
4519 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004520 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004521
Gilles Peskine8817f612018-12-18 00:18:46 +01004522 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004523
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004524 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004525 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004526 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4527 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4528 our_key_data->x,
4529 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004530
Gilles Peskinefe11b722018-12-18 00:24:04 +01004531 TEST_EQUAL( psa_key_agreement( &generator,
4532 our_key,
4533 peer_key_data->x, peer_key_data->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004534 alg ),
4535 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004536
4537exit:
4538 psa_generator_abort( &generator );
4539 psa_destroy_key( our_key );
4540 mbedtls_psa_crypto_free( );
4541}
4542/* END_CASE */
4543
4544/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004545void key_agreement_capacity( int alg_arg,
4546 int our_key_type_arg, data_t *our_key_data,
4547 data_t *peer_key_data,
4548 int expected_capacity_arg )
4549{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004550 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004551 psa_algorithm_t alg = alg_arg;
4552 psa_key_type_t our_key_type = our_key_type_arg;
4553 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004554 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004555 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004556 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004557
Gilles Peskine8817f612018-12-18 00:18:46 +01004558 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004559
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004560 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004561 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004562 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4563 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4564 our_key_data->x,
4565 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004566
Gilles Peskine8817f612018-12-18 00:18:46 +01004567 PSA_ASSERT( psa_key_agreement( &generator,
4568 our_key,
4569 peer_key_data->x, peer_key_data->len,
4570 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004571
Gilles Peskinebf491972018-10-25 22:36:12 +02004572 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004573 PSA_ASSERT( psa_get_generator_capacity(
4574 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004575 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004576
Gilles Peskinebf491972018-10-25 22:36:12 +02004577 /* Test the actual capacity by reading the output. */
4578 while( actual_capacity > sizeof( output ) )
4579 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004580 PSA_ASSERT( psa_generator_read( &generator,
4581 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004582 actual_capacity -= sizeof( output );
4583 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004584 PSA_ASSERT( psa_generator_read( &generator,
4585 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004586 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004587 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004588
Gilles Peskine59685592018-09-18 12:11:34 +02004589exit:
4590 psa_generator_abort( &generator );
4591 psa_destroy_key( our_key );
4592 mbedtls_psa_crypto_free( );
4593}
4594/* END_CASE */
4595
4596/* BEGIN_CASE */
4597void key_agreement_output( int alg_arg,
4598 int our_key_type_arg, data_t *our_key_data,
4599 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004600 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004601{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004602 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004603 psa_algorithm_t alg = alg_arg;
4604 psa_key_type_t our_key_type = our_key_type_arg;
4605 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004606 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004607 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004608
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004609 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4610 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004611
Gilles Peskine8817f612018-12-18 00:18:46 +01004612 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004613
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004614 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004615 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004616 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4617 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4618 our_key_data->x,
4619 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004620
Gilles Peskine8817f612018-12-18 00:18:46 +01004621 PSA_ASSERT( psa_key_agreement( &generator,
4622 our_key,
4623 peer_key_data->x, peer_key_data->len,
4624 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004625
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004626 PSA_ASSERT( psa_generator_read( &generator,
4627 actual_output,
4628 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004629 ASSERT_COMPARE( actual_output, expected_output1->len,
4630 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004631 if( expected_output2->len != 0 )
4632 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004633 PSA_ASSERT( psa_generator_read( &generator,
4634 actual_output,
4635 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004636 ASSERT_COMPARE( actual_output, expected_output2->len,
4637 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004638 }
Gilles Peskine59685592018-09-18 12:11:34 +02004639
4640exit:
4641 psa_generator_abort( &generator );
4642 psa_destroy_key( our_key );
4643 mbedtls_psa_crypto_free( );
4644 mbedtls_free( actual_output );
4645}
4646/* END_CASE */
4647
4648/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004649void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004650{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004651 size_t bytes = bytes_arg;
4652 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004653 unsigned char *output = NULL;
4654 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004655 size_t i;
4656 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004657
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004658 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4659 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004660 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004661
Gilles Peskine8817f612018-12-18 00:18:46 +01004662 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004663
Gilles Peskinea50d7392018-06-21 10:22:13 +02004664 /* Run several times, to ensure that every output byte will be
4665 * nonzero at least once with overwhelming probability
4666 * (2^(-8*number_of_runs)). */
4667 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004668 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004669 if( bytes != 0 )
4670 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004671 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004672
4673 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004674 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4675 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004676
4677 for( i = 0; i < bytes; i++ )
4678 {
4679 if( output[i] != 0 )
4680 ++changed[i];
4681 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004682 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004683
4684 /* Check that every byte was changed to nonzero at least once. This
4685 * validates that psa_generate_random is overwriting every byte of
4686 * the output buffer. */
4687 for( i = 0; i < bytes; i++ )
4688 {
4689 TEST_ASSERT( changed[i] != 0 );
4690 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004691
4692exit:
4693 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004694 mbedtls_free( output );
4695 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004696}
4697/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004698
4699/* BEGIN_CASE */
4700void generate_key( int type_arg,
4701 int bits_arg,
4702 int usage_arg,
4703 int alg_arg,
4704 int expected_status_arg )
4705{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004706 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004707 psa_key_type_t type = type_arg;
4708 psa_key_usage_t usage = usage_arg;
4709 size_t bits = bits_arg;
4710 psa_algorithm_t alg = alg_arg;
4711 psa_status_t expected_status = expected_status_arg;
4712 psa_key_type_t got_type;
4713 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004714 psa_status_t expected_info_status =
David Saadab4ecc272019-02-14 13:48:10 +02004715 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Jaeden Amero70261c52019-01-04 11:47:20 +00004716 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004717
Gilles Peskine8817f612018-12-18 00:18:46 +01004718 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004719
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004720 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004721 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004722 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004723
4724 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004725 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
4726 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004727
4728 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004729 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
4730 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004731 if( expected_info_status != PSA_SUCCESS )
4732 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004733 TEST_EQUAL( got_type, type );
4734 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004735
Gilles Peskine818ca122018-06-20 18:16:48 +02004736 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004737 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004738 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004739
4740exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004741 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004742 mbedtls_psa_crypto_free( );
4743}
4744/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004745
Darryl Greend49a4992018-06-18 17:27:26 +01004746/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4747void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4748 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004749 int alg_arg, int generation_method,
4750 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004751{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004752 psa_key_handle_t handle = 0;
4753 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004754 psa_key_type_t type = (psa_key_type_t) type_arg;
4755 psa_key_type_t type_get;
4756 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00004757 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
4758 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004759 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4760 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004761 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00004762 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4763 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004764 unsigned char *first_export = NULL;
4765 unsigned char *second_export = NULL;
4766 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4767 size_t first_exported_length;
4768 size_t second_exported_length;
4769
4770 ASSERT_ALLOC( first_export, export_size );
4771 ASSERT_ALLOC( second_export, export_size );
4772
Gilles Peskine8817f612018-12-18 00:18:46 +01004773 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004774
Gilles Peskine8817f612018-12-18 00:18:46 +01004775 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
Gilles Peskine8817f612018-12-18 00:18:46 +01004776 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004777 psa_key_policy_set_usage( &policy_set, policy_usage,
4778 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004779 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004780
Darryl Green0c6575a2018-11-07 16:05:30 +00004781 switch( generation_method )
4782 {
4783 case IMPORT_KEY:
4784 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004785 PSA_ASSERT( psa_import_key( handle, type,
4786 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004787 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004788
Darryl Green0c6575a2018-11-07 16:05:30 +00004789 case GENERATE_KEY:
4790 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004791 PSA_ASSERT( psa_generate_key( handle, type, bits,
4792 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004793 break;
4794
4795 case DERIVE_KEY:
4796 /* Create base key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004797 PSA_ASSERT( psa_allocate_key( &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004798 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4799 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004800 PSA_ASSERT( psa_set_key_policy(
4801 base_key, &base_policy_set ) );
4802 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4803 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004804 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004805 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4806 base_policy_alg,
4807 NULL, 0, NULL, 0,
4808 export_size ) );
4809 PSA_ASSERT( psa_generator_import_key(
4810 handle, PSA_KEY_TYPE_RAW_DATA,
4811 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004812 break;
4813 }
Darryl Greend49a4992018-06-18 17:27:26 +01004814
4815 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004816 TEST_EQUAL( psa_export_key( handle,
4817 first_export, export_size,
4818 &first_exported_length ),
4819 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004820
4821 /* Shutdown and restart */
4822 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004823 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004824
Darryl Greend49a4992018-06-18 17:27:26 +01004825 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004826 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4827 &handle ) );
4828 PSA_ASSERT( psa_get_key_information(
4829 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004830 TEST_EQUAL( type_get, type );
4831 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004832
Gilles Peskine8817f612018-12-18 00:18:46 +01004833 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004834 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4835 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004836
4837 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004838 TEST_EQUAL( psa_export_key( handle,
4839 second_export, export_size,
4840 &second_exported_length ),
4841 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004842
Darryl Green0c6575a2018-11-07 16:05:30 +00004843 if( export_status == PSA_SUCCESS )
4844 {
4845 ASSERT_COMPARE( first_export, first_exported_length,
4846 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004847
Darryl Green0c6575a2018-11-07 16:05:30 +00004848 switch( generation_method )
4849 {
4850 case IMPORT_KEY:
4851 ASSERT_COMPARE( data->x, data->len,
4852 first_export, first_exported_length );
4853 break;
4854 default:
4855 break;
4856 }
4857 }
4858
4859 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004860 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004861 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004862
4863exit:
4864 mbedtls_free( first_export );
4865 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004866 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004867 mbedtls_psa_crypto_free();
4868}
4869/* END_CASE */