blob: 2659081fde3228590d883794c024a74a04688ed2 [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 Peskineb70a0fd2019-01-07 22:59:38 +0100532 if( PSA_ALG_IS_HKDF( alg ) )
533 {
534 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
535 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
536 PSA_KDF_STEP_SALT,
537 label,
538 label_length ) );
539 PSA_ASSERT( psa_key_derivation_input_key( &generator,
540 PSA_KDF_STEP_SECRET,
541 handle ) );
542 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
543 PSA_KDF_STEP_INFO,
544 seed,
545 seed_length ) );
546 }
547 else
548 {
549 // legacy
550 PSA_ASSERT( psa_key_derivation( &generator,
551 handle, alg,
552 label, label_length,
553 seed, seed_length,
554 sizeof( output ) ) );
555 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100556 PSA_ASSERT( psa_generator_read( &generator,
557 output,
558 sizeof( output ) ) );
559 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200560 }
561
562 return( 1 );
563
564exit:
565 return( 0 );
566}
567
Gilles Peskinec7998b72018-11-07 18:45:02 +0100568/* We need two keys to exercise key agreement. Exercise the
569 * private key against its own public key. */
570static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +0100571 psa_key_handle_t handle )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100572{
573 psa_key_type_t private_key_type;
574 psa_key_type_t public_key_type;
575 size_t key_bits;
576 uint8_t *public_key = NULL;
577 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200578 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinec7998b72018-11-07 18:45:02 +0100579 * psa_key_agreement fails. This isn't fully satisfactory, but it's
580 * good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200581 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100582
Gilles Peskine8817f612018-12-18 00:18:46 +0100583 PSA_ASSERT( psa_get_key_information( handle,
584 &private_key_type,
585 &key_bits ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100586 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
587 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
588 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100589 PSA_ASSERT( psa_export_public_key( handle,
590 public_key, public_key_length,
591 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100592
Gilles Peskine969c5d62019-01-16 15:53:06 +0100593 status = psa_key_agreement( generator, PSA_KDF_STEP_SECRET, handle,
594 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100595exit:
596 mbedtls_free( public_key );
597 return( status );
598}
599
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200600/* We need two keys to exercise key agreement. Exercise the
601 * private key against its own public key. */
602static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
603 psa_key_handle_t handle )
604{
605 psa_key_type_t private_key_type;
606 psa_key_type_t public_key_type;
607 size_t key_bits;
608 uint8_t *public_key = NULL;
609 size_t public_key_length;
610 uint8_t output[1024];
611 size_t output_length;
612 /* Return GENERIC_ERROR if something other than the final call to
613 * psa_key_agreement fails. This isn't fully satisfactory, but it's
614 * good enough: callers will report it as a failed test anyway. */
615 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
616
617 PSA_ASSERT( psa_get_key_information( handle,
618 &private_key_type,
619 &key_bits ) );
620 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
621 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
622 ASSERT_ALLOC( public_key, public_key_length );
623 PSA_ASSERT( psa_export_public_key( handle,
624 public_key, public_key_length,
625 &public_key_length ) );
626
627 status = psa_key_agreement_raw_shared_secret(
628 alg, handle,
629 public_key, public_key_length,
630 output, sizeof( output ), &output_length );
631exit:
632 mbedtls_free( public_key );
633 return( status );
634}
635
636static int exercise_raw_key_agreement_key( psa_key_handle_t handle,
637 psa_key_usage_t usage,
638 psa_algorithm_t alg )
639{
640 int ok = 0;
641
642 if( usage & PSA_KEY_USAGE_DERIVE )
643 {
644 /* We need two keys to exercise key agreement. Exercise the
645 * private key against its own public key. */
646 PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) );
647 }
648 ok = 1;
649
650exit:
651 return( ok );
652}
653
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100654static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200655 psa_key_usage_t usage,
656 psa_algorithm_t alg )
657{
658 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200659 unsigned char output[1];
660 int ok = 0;
661
662 if( usage & PSA_KEY_USAGE_DERIVE )
663 {
664 /* We need two keys to exercise key agreement. Exercise the
665 * private key against its own public key. */
Gilles Peskine969c5d62019-01-16 15:53:06 +0100666 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
667 PSA_ASSERT( key_agreement_with_self( &generator, handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100668 PSA_ASSERT( psa_generator_read( &generator,
669 output,
670 sizeof( output ) ) );
671 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200672 }
673 ok = 1;
674
675exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200676 return( ok );
677}
678
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200679static int is_oid_of_key_type( psa_key_type_t type,
680 const uint8_t *oid, size_t oid_length )
681{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200682 const uint8_t *expected_oid = NULL;
683 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200684#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200685 if( PSA_KEY_TYPE_IS_RSA( type ) )
686 {
687 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
688 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
689 }
690 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200691#endif /* MBEDTLS_RSA_C */
692#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200693 if( PSA_KEY_TYPE_IS_ECC( type ) )
694 {
695 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
696 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
697 }
698 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200699#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200700 {
701 char message[40];
702 mbedtls_snprintf( message, sizeof( message ),
703 "OID not known for key type=0x%08lx",
704 (unsigned long) type );
705 test_fail( message, __LINE__, __FILE__ );
706 return( 0 );
707 }
708
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200709 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200710 return( 1 );
711
712exit:
713 return( 0 );
714}
715
716static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
717 size_t min_bits, size_t max_bits,
718 int must_be_odd )
719{
720 size_t len;
721 size_t actual_bits;
722 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100723 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100724 MBEDTLS_ASN1_INTEGER ),
725 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200726 /* Tolerate a slight departure from DER encoding:
727 * - 0 may be represented by an empty string or a 1-byte string.
728 * - The sign bit may be used as a value bit. */
729 if( ( len == 1 && ( *p )[0] == 0 ) ||
730 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
731 {
732 ++( *p );
733 --len;
734 }
735 if( min_bits == 0 && len == 0 )
736 return( 1 );
737 msb = ( *p )[0];
738 TEST_ASSERT( msb != 0 );
739 actual_bits = 8 * ( len - 1 );
740 while( msb != 0 )
741 {
742 msb >>= 1;
743 ++actual_bits;
744 }
745 TEST_ASSERT( actual_bits >= min_bits );
746 TEST_ASSERT( actual_bits <= max_bits );
747 if( must_be_odd )
748 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
749 *p += len;
750 return( 1 );
751exit:
752 return( 0 );
753}
754
755static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
756 size_t *len,
757 unsigned char n, unsigned char tag )
758{
759 int ret;
760 ret = mbedtls_asn1_get_tag( p, end, len,
761 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
762 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
763 if( ret != 0 )
764 return( ret );
765 end = *p + *len;
766 ret = mbedtls_asn1_get_tag( p, end, len, tag );
767 if( ret != 0 )
768 return( ret );
769 if( *p + *len != end )
770 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
771 return( 0 );
772}
773
774static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
775 uint8_t *exported, size_t exported_length )
776{
777 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100778 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200779 else
780 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200781
782#if defined(MBEDTLS_DES_C)
783 if( type == PSA_KEY_TYPE_DES )
784 {
785 /* Check the parity bits. */
786 unsigned i;
787 for( i = 0; i < bits / 8; i++ )
788 {
789 unsigned bit_count = 0;
790 unsigned m;
791 for( m = 1; m <= 0x100; m <<= 1 )
792 {
793 if( exported[i] & m )
794 ++bit_count;
795 }
796 TEST_ASSERT( bit_count % 2 != 0 );
797 }
798 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200799 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200800#endif
801
802#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
803 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
804 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200805 uint8_t *p = exported;
806 uint8_t *end = exported + exported_length;
807 size_t len;
808 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200809 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200810 * modulus INTEGER, -- n
811 * publicExponent INTEGER, -- e
812 * privateExponent INTEGER, -- d
813 * prime1 INTEGER, -- p
814 * prime2 INTEGER, -- q
815 * exponent1 INTEGER, -- d mod (p-1)
816 * exponent2 INTEGER, -- d mod (q-1)
817 * coefficient INTEGER, -- (inverse of q) mod p
818 * }
819 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100820 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
821 MBEDTLS_ASN1_SEQUENCE |
822 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
823 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200824 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
825 goto exit;
826 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
827 goto exit;
828 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
829 goto exit;
830 /* Require d to be at least half the size of n. */
831 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
832 goto exit;
833 /* Require p and q to be at most half the size of n, rounded up. */
834 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
835 goto exit;
836 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
837 goto exit;
838 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
839 goto exit;
840 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
841 goto exit;
842 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
843 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100844 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100845 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200846 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200847#endif /* MBEDTLS_RSA_C */
848
849#if defined(MBEDTLS_ECP_C)
850 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
851 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100852 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100853 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100854 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200855 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200856#endif /* MBEDTLS_ECP_C */
857
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200858 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
859 {
860 uint8_t *p = exported;
861 uint8_t *end = exported + exported_length;
862 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200863#if defined(MBEDTLS_RSA_C)
864 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
865 {
866 /* RSAPublicKey ::= SEQUENCE {
867 * modulus INTEGER, -- n
868 * publicExponent INTEGER } -- e
869 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100870 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
871 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100872 MBEDTLS_ASN1_CONSTRUCTED ),
873 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100874 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200875 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
876 goto exit;
877 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
878 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100879 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200880 }
881 else
882#endif /* MBEDTLS_RSA_C */
883#if defined(MBEDTLS_ECP_C)
884 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
885 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000886 /* The representation of an ECC public key is:
887 * - The byte 0x04;
888 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
889 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
890 * - where m is the bit size associated with the curve.
Jaeden Amero6b196002019-01-10 10:23:21 +0000891 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100892 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
893 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200894 }
895 else
896#endif /* MBEDTLS_ECP_C */
897 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100898 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200899 mbedtls_snprintf( message, sizeof( message ),
900 "No sanity check for public key type=0x%08lx",
901 (unsigned long) type );
902 test_fail( message, __LINE__, __FILE__ );
903 return( 0 );
904 }
905 }
906 else
907
908 {
909 /* No sanity checks for other types */
910 }
911
912 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200913
914exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200915 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200916}
917
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100918static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200919 psa_key_usage_t usage )
920{
921 psa_key_type_t type;
922 size_t bits;
923 uint8_t *exported = NULL;
924 size_t exported_size = 0;
925 size_t exported_length = 0;
926 int ok = 0;
927
Gilles Peskine8817f612018-12-18 00:18:46 +0100928 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200929
930 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
931 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200932 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100933 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
934 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200935 return( 1 );
936 }
937
Gilles Peskined14664a2018-08-10 19:07:32 +0200938 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200939 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200940
Gilles Peskine8817f612018-12-18 00:18:46 +0100941 PSA_ASSERT( psa_export_key( handle,
942 exported, exported_size,
943 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200944 ok = exported_key_sanity_check( type, bits, exported, exported_length );
945
946exit:
947 mbedtls_free( exported );
948 return( ok );
949}
950
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100951static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200952{
953 psa_key_type_t type;
954 psa_key_type_t public_type;
955 size_t bits;
956 uint8_t *exported = NULL;
957 size_t exported_size = 0;
958 size_t exported_length = 0;
959 int ok = 0;
960
Gilles Peskine8817f612018-12-18 00:18:46 +0100961 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200962 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
963 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100964 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100965 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200966 return( 1 );
967 }
968
969 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
970 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200971 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200972
Gilles Peskine8817f612018-12-18 00:18:46 +0100973 PSA_ASSERT( psa_export_public_key( handle,
974 exported, exported_size,
975 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200976 ok = exported_key_sanity_check( public_type, bits,
977 exported, exported_length );
978
979exit:
980 mbedtls_free( exported );
981 return( ok );
982}
983
Gilles Peskinec9516fb2019-02-05 20:32:06 +0100984/** Do smoke tests on a key.
985 *
986 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
987 * sign/verify, or derivation) that is permitted according to \p usage.
988 * \p usage and \p alg should correspond to the expected policy on the
989 * key.
990 *
991 * Export the key if permitted by \p usage, and check that the output
992 * looks sensible. If \p usage forbids export, check that
993 * \p psa_export_key correctly rejects the attempt. If the key is
994 * asymmetric, also check \p psa_export_public_key.
995 *
996 * If the key fails the tests, this function calls the test framework's
997 * `test_fail` function and returns false. Otherwise this function returns
998 * true. Therefore it should be used as follows:
999 * ```
1000 * if( ! exercise_key( ... ) ) goto exit;
1001 * ```
1002 *
1003 * \param handle The key to exercise. It should be capable of performing
1004 * \p alg.
1005 * \param usage The usage flags to assume.
1006 * \param alg The algorithm to exercise.
1007 *
1008 * \retval 0 The key failed the smoke tests.
1009 * \retval 1 The key passed the smoke tests.
1010 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001011static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +02001012 psa_key_usage_t usage,
1013 psa_algorithm_t alg )
1014{
1015 int ok;
1016 if( alg == 0 )
1017 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1018 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001019 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001020 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001021 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001022 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001023 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001024 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001025 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001026 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001027 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001028 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001029 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001030 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
1031 ok = exercise_raw_key_agreement_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001032 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001033 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001034 else
1035 {
1036 char message[40];
1037 mbedtls_snprintf( message, sizeof( message ),
1038 "No code to exercise alg=0x%08lx",
1039 (unsigned long) alg );
1040 test_fail( message, __LINE__, __FILE__ );
1041 ok = 0;
1042 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001043
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001044 ok = ok && exercise_export_key( handle, usage );
1045 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +02001046
Gilles Peskine02b75072018-07-01 22:31:34 +02001047 return( ok );
1048}
1049
Gilles Peskine10df3412018-10-25 22:35:43 +02001050static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1051 psa_algorithm_t alg )
1052{
1053 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1054 {
1055 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1056 PSA_KEY_USAGE_VERIFY :
1057 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
1058 }
1059 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1060 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1061 {
1062 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1063 PSA_KEY_USAGE_ENCRYPT :
1064 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1065 }
1066 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1067 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1068 {
1069 return( PSA_KEY_USAGE_DERIVE );
1070 }
1071 else
1072 {
1073 return( 0 );
1074 }
1075
1076}
Darryl Green0c6575a2018-11-07 16:05:30 +00001077
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001078/* An overapproximation of the amount of storage needed for a key of the
1079 * given type and with the given content. The API doesn't make it easy
1080 * to find a good value for the size. The current implementation doesn't
1081 * care about the value anyway. */
1082#define KEY_BITS_FROM_DATA( type, data ) \
1083 ( data )->len
1084
Darryl Green0c6575a2018-11-07 16:05:30 +00001085typedef enum {
1086 IMPORT_KEY = 0,
1087 GENERATE_KEY = 1,
1088 DERIVE_KEY = 2
1089} generate_method;
1090
Gilles Peskinee59236f2018-01-27 23:32:46 +01001091/* END_HEADER */
1092
1093/* BEGIN_DEPENDENCIES
1094 * depends_on:MBEDTLS_PSA_CRYPTO_C
1095 * END_DEPENDENCIES
1096 */
1097
1098/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001099void static_checks( )
1100{
1101 size_t max_truncated_mac_size =
1102 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1103
1104 /* Check that the length for a truncated MAC always fits in the algorithm
1105 * encoding. The shifted mask is the maximum truncated value. The
1106 * untruncated algorithm may be one byte larger. */
1107 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
1108}
1109/* END_CASE */
1110
1111/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001112void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001113{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001114 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001115 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001116 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001117
Gilles Peskine8817f612018-12-18 00:18:46 +01001118 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001119
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001120 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001121 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001122 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001123 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001124 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001125
1126exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001127 mbedtls_psa_crypto_free( );
1128}
1129/* END_CASE */
1130
1131/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +01001132void import_twice( int alg_arg, int usage_arg,
1133 int type1_arg, data_t *data1,
1134 int expected_import1_status_arg,
1135 int type2_arg, data_t *data2,
1136 int expected_import2_status_arg )
1137{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001138 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +01001139 psa_algorithm_t alg = alg_arg;
1140 psa_key_usage_t usage = usage_arg;
1141 psa_key_type_t type1 = type1_arg;
1142 psa_status_t expected_import1_status = expected_import1_status_arg;
1143 psa_key_type_t type2 = type2_arg;
1144 psa_status_t expected_import2_status = expected_import2_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00001145 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea4261682018-12-03 11:34:01 +01001146 psa_status_t status;
1147
Gilles Peskine8817f612018-12-18 00:18:46 +01001148 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +01001149
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001150 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +01001151 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001152 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +01001153
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001154 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001155 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001156 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001157 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +01001158
1159 if( expected_import1_status == PSA_SUCCESS ||
1160 expected_import2_status == PSA_SUCCESS )
1161 {
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001162 if( ! exercise_key( handle, usage, alg ) )
1163 goto exit;
Gilles Peskinea4261682018-12-03 11:34:01 +01001164 }
1165
1166exit:
1167 mbedtls_psa_crypto_free( );
1168}
1169/* END_CASE */
1170
1171/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001172void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1173{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001174 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001175 size_t bits = bits_arg;
1176 psa_status_t expected_status = expected_status_arg;
1177 psa_status_t status;
1178 psa_key_type_t type =
1179 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
1180 size_t buffer_size = /* Slight overapproximations */
1181 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001182 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001183 unsigned char *p;
1184 int ret;
1185 size_t length;
1186
Gilles Peskine8817f612018-12-18 00:18:46 +01001187 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001188 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001189
1190 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1191 bits, keypair ) ) >= 0 );
1192 length = ret;
1193
1194 /* Try importing the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001195 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001196 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001197 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001198 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001199 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001200
1201exit:
1202 mbedtls_free( buffer );
1203 mbedtls_psa_crypto_free( );
1204}
1205/* END_CASE */
1206
1207/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001208void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001209 int type_arg,
1210 int alg_arg,
1211 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001212 int expected_bits,
1213 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001214 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001215 int canonical_input )
1216{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001217 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001218 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001219 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001220 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001221 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001222 unsigned char *exported = NULL;
1223 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001224 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001225 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001226 size_t reexported_length;
1227 psa_key_type_t got_type;
1228 size_t got_bits;
Jaeden Amero70261c52019-01-04 11:47:20 +00001229 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001230
Moran Pekercb088e72018-07-17 17:36:59 +03001231 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001232 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001233 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001234 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001235 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001236
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001237 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001238 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001239 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001240
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001241 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
David Saadab4ecc272019-02-14 13:48:10 +02001242 PSA_ERROR_DOES_NOT_EXIST );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001243
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001244 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001245 PSA_ASSERT( psa_import_key( handle, type,
1246 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001247
1248 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001249 PSA_ASSERT( psa_get_key_information( handle,
1250 &got_type,
1251 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001252 TEST_EQUAL( got_type, type );
1253 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001254
1255 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001256 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001257 exported, export_size,
1258 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001259 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001260
1261 /* The exported length must be set by psa_export_key() to a value between 0
1262 * and export_size. On errors, the exported length must be 0. */
1263 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1264 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1265 TEST_ASSERT( exported_length <= export_size );
1266
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001267 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001268 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001269 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001270 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001271 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001272 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001273 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001274
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001275 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001276 goto exit;
1277
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001278 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001279 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001280 else
1281 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001282 psa_key_handle_t handle2;
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001283 PSA_ASSERT( psa_allocate_key( &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001284 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001285
Gilles Peskine8817f612018-12-18 00:18:46 +01001286 PSA_ASSERT( psa_import_key( handle2, type,
1287 exported,
1288 exported_length ) );
1289 PSA_ASSERT( psa_export_key( handle2,
1290 reexported,
1291 export_size,
1292 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001293 ASSERT_COMPARE( exported, exported_length,
1294 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001295 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001296 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001297 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001298
1299destroy:
1300 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001301 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001302 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1303 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001304
1305exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001306 mbedtls_free( exported );
1307 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001308 mbedtls_psa_crypto_free( );
1309}
1310/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001311
Moran Pekerf709f4a2018-06-06 17:26:04 +03001312/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001313void import_key_nonempty_slot( )
1314{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001315 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001316 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1317 psa_status_t status;
1318 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001319 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001320
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001321 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001322
Moran Peker28a38e62018-11-07 16:18:24 +02001323 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001324 PSA_ASSERT( psa_import_key( handle, type,
1325 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001326
1327 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001328 status = psa_import_key( handle, type, data, sizeof( data ) );
David Saadab4ecc272019-02-14 13:48:10 +02001329 TEST_EQUAL( status, PSA_ERROR_ALREADY_EXISTS );
Moran Peker28a38e62018-11-07 16:18:24 +02001330
1331exit:
1332 mbedtls_psa_crypto_free( );
1333}
1334/* END_CASE */
1335
1336/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001337void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001338{
1339 psa_status_t status;
1340 unsigned char *exported = NULL;
1341 size_t export_size = 0;
1342 size_t exported_length = INVALID_EXPORT_LENGTH;
1343 psa_status_t expected_export_status = expected_export_status_arg;
1344
Gilles Peskine8817f612018-12-18 00:18:46 +01001345 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001346
1347 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001348 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001349 exported, export_size,
1350 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001351 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001352
1353exit:
1354 mbedtls_psa_crypto_free( );
1355}
1356/* END_CASE */
1357
1358/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001359void export_with_no_key_activity( )
1360{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001361 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001362 psa_algorithm_t alg = PSA_ALG_CTR;
1363 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001364 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001365 unsigned char *exported = NULL;
1366 size_t export_size = 0;
1367 size_t exported_length = INVALID_EXPORT_LENGTH;
1368
Gilles Peskine8817f612018-12-18 00:18:46 +01001369 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001370
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001371 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001372 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001373 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001374
1375 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001376 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001377 exported, export_size,
1378 &exported_length );
David Saadab4ecc272019-02-14 13:48:10 +02001379 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Peker34550092018-11-07 16:19:34 +02001380
1381exit:
1382 mbedtls_psa_crypto_free( );
1383}
1384/* END_CASE */
1385
1386/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001387void cipher_with_no_key_activity( )
1388{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001389 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001390 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001391 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001392 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001393 int exercise_alg = PSA_ALG_CTR;
1394
Gilles Peskine8817f612018-12-18 00:18:46 +01001395 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001396
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001397 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001398 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001399 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001400
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001401 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
David Saadab4ecc272019-02-14 13:48:10 +02001402 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Pekerce500072018-11-07 16:20:07 +02001403
1404exit:
1405 psa_cipher_abort( &operation );
1406 mbedtls_psa_crypto_free( );
1407}
1408/* END_CASE */
1409
1410/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001411void export_after_import_failure( data_t *data, int type_arg,
1412 int expected_import_status_arg )
1413{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001414 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001415 psa_key_type_t type = type_arg;
1416 psa_status_t status;
1417 unsigned char *exported = NULL;
1418 size_t export_size = 0;
1419 psa_status_t expected_import_status = expected_import_status_arg;
1420 size_t exported_length = INVALID_EXPORT_LENGTH;
1421
Gilles Peskine8817f612018-12-18 00:18:46 +01001422 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001423
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001424 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001425
Moran Peker34550092018-11-07 16:19:34 +02001426 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001427 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001428 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001429 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001430
1431 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001432 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001433 exported, export_size,
1434 &exported_length );
David Saadab4ecc272019-02-14 13:48:10 +02001435 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Peker34550092018-11-07 16:19:34 +02001436
1437exit:
1438 mbedtls_psa_crypto_free( );
1439}
1440/* END_CASE */
1441
1442/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001443void cipher_after_import_failure( data_t *data, int type_arg,
1444 int expected_import_status_arg )
1445{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001446 psa_key_handle_t handle = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001447 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001448 psa_key_type_t type = type_arg;
1449 psa_status_t status;
1450 psa_status_t expected_import_status = expected_import_status_arg;
1451 int exercise_alg = PSA_ALG_CTR;
1452
Gilles Peskine8817f612018-12-18 00:18:46 +01001453 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001454
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001455 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001456
Moran Pekerce500072018-11-07 16:20:07 +02001457 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001458 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001459 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001460 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001461
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001462 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
David Saadab4ecc272019-02-14 13:48:10 +02001463 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Pekerce500072018-11-07 16:20:07 +02001464
1465exit:
1466 psa_cipher_abort( &operation );
1467 mbedtls_psa_crypto_free( );
1468}
1469/* END_CASE */
1470
1471/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001472void export_after_destroy_key( data_t *data, int type_arg )
1473{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001474 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001475 psa_key_type_t type = type_arg;
1476 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001477 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001478 psa_algorithm_t alg = PSA_ALG_CTR;
1479 unsigned char *exported = NULL;
1480 size_t export_size = 0;
1481 size_t exported_length = INVALID_EXPORT_LENGTH;
1482
Gilles Peskine8817f612018-12-18 00:18:46 +01001483 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001484
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001485 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001486 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001487 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001488 export_size = (ptrdiff_t) data->len;
1489 ASSERT_ALLOC( exported, export_size );
1490
1491 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001492 PSA_ASSERT( psa_import_key( handle, type,
1493 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001494
Gilles Peskine8817f612018-12-18 00:18:46 +01001495 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1496 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001497
1498 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001499 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001500
1501 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001502 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001503 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001504 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001505
1506exit:
1507 mbedtls_free( exported );
1508 mbedtls_psa_crypto_free( );
1509}
1510/* END_CASE */
1511
1512/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001513void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001514 int type_arg,
1515 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001516 int export_size_delta,
1517 int expected_export_status_arg,
1518 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001519{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001520 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001521 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001522 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001523 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001524 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001525 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001526 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001527 size_t exported_length = INVALID_EXPORT_LENGTH;
Jaeden Amero70261c52019-01-04 11:47:20 +00001528 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001529
Gilles Peskine8817f612018-12-18 00:18:46 +01001530 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001531
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001532 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001533 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001534 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001535
1536 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001537 PSA_ASSERT( psa_import_key( handle, type,
1538 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001539
Gilles Peskine49c25912018-10-29 15:15:31 +01001540 /* Export the public key */
1541 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001542 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001543 exported, export_size,
1544 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001545 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001546 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001547 {
1548 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1549 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001550 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001551 TEST_ASSERT( expected_public_key->len <=
1552 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001553 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1554 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001555 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001556
1557exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001558 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001559 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001560 mbedtls_psa_crypto_free( );
1561}
1562/* END_CASE */
1563
Gilles Peskine20035e32018-02-03 22:44:14 +01001564/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001565void import_and_exercise_key( data_t *data,
1566 int type_arg,
1567 int bits_arg,
1568 int alg_arg )
1569{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001570 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001571 psa_key_type_t type = type_arg;
1572 size_t bits = bits_arg;
1573 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001574 psa_key_usage_t usage = usage_to_exercise( type, alg );
Jaeden Amero70261c52019-01-04 11:47:20 +00001575 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001576 psa_key_type_t got_type;
1577 size_t got_bits;
1578 psa_status_t status;
1579
Gilles Peskine8817f612018-12-18 00:18:46 +01001580 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001581
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001582 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001583 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001584 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001585
1586 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001587 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001588 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001589
1590 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001591 PSA_ASSERT( psa_get_key_information( handle,
1592 &got_type,
1593 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001594 TEST_EQUAL( got_type, type );
1595 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001596
1597 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001598 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001599 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001600
1601exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001602 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001603 mbedtls_psa_crypto_free( );
1604}
1605/* END_CASE */
1606
1607/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001608void key_policy( int usage_arg, int alg_arg )
1609{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001610 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001611 psa_algorithm_t alg = alg_arg;
1612 psa_key_usage_t usage = usage_arg;
1613 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1614 unsigned char key[32] = {0};
Jaeden Amero70261c52019-01-04 11:47:20 +00001615 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
1616 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001617
1618 memset( key, 0x2a, sizeof( key ) );
1619
Gilles Peskine8817f612018-12-18 00:18:46 +01001620 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001621
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001622 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001623 psa_key_policy_set_usage( &policy_set, usage, alg );
1624
Gilles Peskinefe11b722018-12-18 00:24:04 +01001625 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1626 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001627 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001628
Gilles Peskine8817f612018-12-18 00:18:46 +01001629 PSA_ASSERT( psa_import_key( handle, key_type,
1630 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001631
Gilles Peskine8817f612018-12-18 00:18:46 +01001632 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001633
Gilles Peskinefe11b722018-12-18 00:24:04 +01001634 TEST_EQUAL( policy_get.usage, policy_set.usage );
1635 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001636
1637exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001638 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001639 mbedtls_psa_crypto_free( );
1640}
1641/* END_CASE */
1642
1643/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001644void key_policy_init( )
1645{
1646 /* Test each valid way of initializing the object, except for `= {0}`, as
1647 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1648 * though it's OK by the C standard. We could test for this, but we'd need
1649 * to supress the Clang warning for the test. */
1650 psa_key_policy_t func = psa_key_policy_init( );
1651 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1652 psa_key_policy_t zero;
1653
1654 memset( &zero, 0, sizeof( zero ) );
1655
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001656 /* A default key policy should not permit any usage. */
1657 TEST_EQUAL( psa_key_policy_get_usage( &func ), 0 );
1658 TEST_EQUAL( psa_key_policy_get_usage( &init ), 0 );
1659 TEST_EQUAL( psa_key_policy_get_usage( &zero ), 0 );
1660
1661 /* A default key policy should not permit any algorithm. */
1662 TEST_EQUAL( psa_key_policy_get_algorithm( &func ), 0 );
1663 TEST_EQUAL( psa_key_policy_get_algorithm( &init ), 0 );
1664 TEST_EQUAL( psa_key_policy_get_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001665}
1666/* END_CASE */
1667
1668/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001669void mac_key_policy( int policy_usage,
1670 int policy_alg,
1671 int key_type,
1672 data_t *key_data,
1673 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001674{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001675 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001676 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001677 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001678 psa_status_t status;
1679 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001680
Gilles Peskine8817f612018-12-18 00:18:46 +01001681 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001682
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001683 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001684 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001685 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001686
Gilles Peskine8817f612018-12-18 00:18:46 +01001687 PSA_ASSERT( psa_import_key( handle, key_type,
1688 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001689
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001690 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001691 if( policy_alg == exercise_alg &&
1692 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001693 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001694 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001695 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001696 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001697
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001698 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001699 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001700 if( policy_alg == exercise_alg &&
1701 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001702 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001703 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001704 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001705
1706exit:
1707 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001708 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001709 mbedtls_psa_crypto_free( );
1710}
1711/* END_CASE */
1712
1713/* BEGIN_CASE */
1714void cipher_key_policy( int policy_usage,
1715 int policy_alg,
1716 int key_type,
1717 data_t *key_data,
1718 int exercise_alg )
1719{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001720 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001721 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001722 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001723 psa_status_t status;
1724
Gilles Peskine8817f612018-12-18 00:18:46 +01001725 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001726
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001727 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001728 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001729 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001730
Gilles Peskine8817f612018-12-18 00:18:46 +01001731 PSA_ASSERT( psa_import_key( handle, key_type,
1732 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001733
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001734 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001735 if( policy_alg == exercise_alg &&
1736 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001737 PSA_ASSERT( status );
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 psa_cipher_abort( &operation );
1741
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001742 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001743 if( policy_alg == exercise_alg &&
1744 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001745 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001746 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001747 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001748
1749exit:
1750 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001751 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001752 mbedtls_psa_crypto_free( );
1753}
1754/* END_CASE */
1755
1756/* BEGIN_CASE */
1757void aead_key_policy( int policy_usage,
1758 int policy_alg,
1759 int key_type,
1760 data_t *key_data,
1761 int nonce_length_arg,
1762 int tag_length_arg,
1763 int exercise_alg )
1764{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001765 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001766 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001767 psa_status_t status;
1768 unsigned char nonce[16] = {0};
1769 size_t nonce_length = nonce_length_arg;
1770 unsigned char tag[16];
1771 size_t tag_length = tag_length_arg;
1772 size_t output_length;
1773
1774 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1775 TEST_ASSERT( tag_length <= sizeof( tag ) );
1776
Gilles Peskine8817f612018-12-18 00:18:46 +01001777 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001778
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001779 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001780 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001781 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001782
Gilles Peskine8817f612018-12-18 00:18:46 +01001783 PSA_ASSERT( psa_import_key( handle, key_type,
1784 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001785
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001786 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001787 nonce, nonce_length,
1788 NULL, 0,
1789 NULL, 0,
1790 tag, tag_length,
1791 &output_length );
1792 if( policy_alg == exercise_alg &&
1793 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001794 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001795 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001796 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001797
1798 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001799 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001800 nonce, nonce_length,
1801 NULL, 0,
1802 tag, tag_length,
1803 NULL, 0,
1804 &output_length );
1805 if( policy_alg == exercise_alg &&
1806 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001807 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001808 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001809 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001810
1811exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001812 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001813 mbedtls_psa_crypto_free( );
1814}
1815/* END_CASE */
1816
1817/* BEGIN_CASE */
1818void asymmetric_encryption_key_policy( int policy_usage,
1819 int policy_alg,
1820 int key_type,
1821 data_t *key_data,
1822 int exercise_alg )
1823{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001824 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001825 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001826 psa_status_t status;
1827 size_t key_bits;
1828 size_t buffer_length;
1829 unsigned char *buffer = NULL;
1830 size_t output_length;
1831
Gilles Peskine8817f612018-12-18 00:18:46 +01001832 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001833
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001834 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001835 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001836 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001837
Gilles Peskine8817f612018-12-18 00:18:46 +01001838 PSA_ASSERT( psa_import_key( handle, key_type,
1839 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001840
Gilles Peskine8817f612018-12-18 00:18:46 +01001841 PSA_ASSERT( psa_get_key_information( handle,
1842 NULL,
1843 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001844 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1845 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001846 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001847
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001848 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001849 NULL, 0,
1850 NULL, 0,
1851 buffer, buffer_length,
1852 &output_length );
1853 if( policy_alg == exercise_alg &&
1854 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001855 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001856 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001857 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001858
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001859 if( buffer_length != 0 )
1860 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001861 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001862 buffer, buffer_length,
1863 NULL, 0,
1864 buffer, buffer_length,
1865 &output_length );
1866 if( policy_alg == exercise_alg &&
1867 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001868 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001869 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001870 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001871
1872exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001873 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001874 mbedtls_psa_crypto_free( );
1875 mbedtls_free( buffer );
1876}
1877/* END_CASE */
1878
1879/* BEGIN_CASE */
1880void asymmetric_signature_key_policy( int policy_usage,
1881 int policy_alg,
1882 int key_type,
1883 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001884 int exercise_alg,
1885 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001886{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001887 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001888 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001889 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001890 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1891 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1892 * compatible with the policy and `payload_length_arg` is supposed to be
1893 * a valid input length to sign. If `payload_length_arg <= 0`,
1894 * `exercise_alg` is supposed to be forbidden by the policy. */
1895 int compatible_alg = payload_length_arg > 0;
1896 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001897 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1898 size_t signature_length;
1899
Gilles Peskine8817f612018-12-18 00:18:46 +01001900 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001901
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001902 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001903 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001904 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001905
Gilles Peskine8817f612018-12-18 00:18:46 +01001906 PSA_ASSERT( psa_import_key( handle, key_type,
1907 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001908
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001909 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001910 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001911 signature, sizeof( signature ),
1912 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001913 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001914 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001915 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001916 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001917
1918 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001919 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001920 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001921 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001922 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001923 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001924 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001925 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001926
1927exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001928 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001929 mbedtls_psa_crypto_free( );
1930}
1931/* END_CASE */
1932
1933/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001934void derive_key_policy( int policy_usage,
1935 int policy_alg,
1936 int key_type,
1937 data_t *key_data,
1938 int exercise_alg )
1939{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001940 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001941 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001942 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1943 psa_status_t status;
1944
Gilles Peskine8817f612018-12-18 00:18:46 +01001945 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001946
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001947 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001948 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001949 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001950
Gilles Peskine8817f612018-12-18 00:18:46 +01001951 PSA_ASSERT( psa_import_key( handle, key_type,
1952 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001953
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001954 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001955 exercise_alg,
1956 NULL, 0,
1957 NULL, 0,
1958 1 );
1959 if( policy_alg == exercise_alg &&
1960 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001961 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001962 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001963 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001964
1965exit:
1966 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001967 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001968 mbedtls_psa_crypto_free( );
1969}
1970/* END_CASE */
1971
1972/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001973void agreement_key_policy( int policy_usage,
1974 int policy_alg,
1975 int key_type_arg,
1976 data_t *key_data,
1977 int exercise_alg )
1978{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001979 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001980 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001981 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001982 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1983 psa_status_t status;
1984
Gilles Peskine8817f612018-12-18 00:18:46 +01001985 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001986
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001987 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001988 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001989 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001990
Gilles Peskine8817f612018-12-18 00:18:46 +01001991 PSA_ASSERT( psa_import_key( handle, key_type,
1992 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001993
Gilles Peskine969c5d62019-01-16 15:53:06 +01001994 PSA_ASSERT( psa_key_derivation_setup( &generator, exercise_alg ) );
1995 status = key_agreement_with_self( &generator, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001996
Gilles Peskine01d718c2018-09-18 12:01:02 +02001997 if( policy_alg == exercise_alg &&
1998 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001999 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002000 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002001 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002002
2003exit:
2004 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002005 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002006 mbedtls_psa_crypto_free( );
2007}
2008/* END_CASE */
2009
2010/* BEGIN_CASE */
Gilles Peskine57ab7212019-01-28 13:03:09 +01002011void copy_key_policy( int source_usage_arg, int source_alg_arg,
2012 int type_arg, data_t *material,
2013 int target_usage_arg, int target_alg_arg,
2014 int constraint_usage_arg, int constraint_alg_arg,
2015 int expected_usage_arg, int expected_alg_arg )
2016{
2017 psa_key_usage_t source_usage = source_usage_arg;
2018 psa_algorithm_t source_alg = source_alg_arg;
2019 psa_key_handle_t source_handle = 0;
2020 psa_key_policy_t source_policy = PSA_KEY_POLICY_INIT;
2021 psa_key_type_t source_type = type_arg;
2022 size_t source_bits;
2023 psa_key_usage_t target_usage = target_usage_arg;
2024 psa_algorithm_t target_alg = target_alg_arg;
2025 psa_key_handle_t target_handle = 0;
2026 psa_key_policy_t target_policy = PSA_KEY_POLICY_INIT;
2027 psa_key_type_t target_type;
2028 size_t target_bits;
2029 psa_key_usage_t constraint_usage = constraint_usage_arg;
2030 psa_algorithm_t constraint_alg = constraint_alg_arg;
2031 psa_key_policy_t constraint = PSA_KEY_POLICY_INIT;
2032 psa_key_policy_t *p_constraint = NULL;
2033 psa_key_usage_t expected_usage = expected_usage_arg;
2034 psa_algorithm_t expected_alg = expected_alg_arg;
2035 uint8_t *export_buffer = NULL;
2036
2037 if( constraint_usage_arg != -1 )
2038 {
2039 p_constraint = &constraint;
2040 psa_key_policy_set_usage( p_constraint,
2041 constraint_usage, constraint_alg );
2042 }
2043
2044 PSA_ASSERT( psa_crypto_init( ) );
2045
2046 /* Populate the source slot. */
2047 PSA_ASSERT( psa_allocate_key( &source_handle ) );
2048 psa_key_policy_set_usage( &source_policy, source_usage, source_alg );
2049 PSA_ASSERT( psa_set_key_policy( source_handle, &source_policy ) );
2050 PSA_ASSERT( psa_import_key( source_handle, source_type,
2051 material->x, material->len ) );
2052 PSA_ASSERT( psa_get_key_information( source_handle, NULL, &source_bits ) );
2053
2054 /* Prepare the target slot. */
2055 PSA_ASSERT( psa_allocate_key( &target_handle ) );
2056 psa_key_policy_set_usage( &target_policy, target_usage, target_alg );
2057 PSA_ASSERT( psa_set_key_policy( target_handle, &target_policy ) );
2058 target_policy = psa_key_policy_init();
2059
2060 /* Copy the key. */
2061 PSA_ASSERT( psa_copy_key( source_handle, target_handle, p_constraint ) );
2062
2063 /* Destroy the source to ensure that this doesn't affect the target. */
2064 PSA_ASSERT( psa_destroy_key( source_handle ) );
2065
2066 /* Test that the target slot has the expected content and policy. */
2067 PSA_ASSERT( psa_get_key_information( target_handle,
2068 &target_type, &target_bits ) );
2069 TEST_EQUAL( source_type, target_type );
2070 TEST_EQUAL( source_bits, target_bits );
2071 PSA_ASSERT( psa_get_key_policy( target_handle, &target_policy ) );
2072 TEST_EQUAL( expected_usage, psa_key_policy_get_usage( &target_policy ) );
2073 TEST_EQUAL( expected_alg, psa_key_policy_get_algorithm( &target_policy ) );
2074 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2075 {
2076 size_t length;
2077 ASSERT_ALLOC( export_buffer, material->len );
2078 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2079 material->len, &length ) );
2080 ASSERT_COMPARE( material->x, material->len,
2081 export_buffer, length );
2082 }
2083 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2084 goto exit;
2085
2086 PSA_ASSERT( psa_close_key( target_handle ) );
2087
2088exit:
2089 mbedtls_psa_crypto_free( );
2090 mbedtls_free( export_buffer );
2091}
2092/* END_CASE */
2093
2094/* BEGIN_CASE */
2095void copy_fail( int source_usage_arg, int source_alg_arg,
2096 int type_arg, data_t *material,
2097 int target_usage_arg, int target_alg_arg,
2098 int constraint_usage_arg, int constraint_alg_arg,
2099 int expected_status_arg )
2100{
2101 /* Test copy failure into an empty slot. There is a test for copy failure
2102 * into an occupied slot in
2103 * test_suite_psa_crypto_slot_management.function. */
2104
2105 psa_key_usage_t source_usage = source_usage_arg;
2106 psa_algorithm_t source_alg = source_alg_arg;
2107 psa_key_handle_t source_handle = 0;
2108 psa_key_policy_t source_policy = PSA_KEY_POLICY_INIT;
2109 psa_key_type_t source_type = type_arg;
2110 size_t source_bits;
2111 psa_key_usage_t target_usage = target_usage_arg;
2112 psa_algorithm_t target_alg = target_alg_arg;
2113 psa_key_handle_t target_handle = 0;
2114 psa_key_policy_t target_policy = PSA_KEY_POLICY_INIT;
2115 psa_key_type_t target_type;
2116 size_t target_bits;
2117 psa_key_usage_t constraint_usage = constraint_usage_arg;
2118 psa_algorithm_t constraint_alg = constraint_alg_arg;
2119 psa_key_policy_t constraint = PSA_KEY_POLICY_INIT;
2120 psa_key_policy_t *p_constraint = NULL;
2121 psa_status_t expected_status = expected_status_arg;
2122
2123 if( constraint_usage_arg != -1 )
2124 {
2125 p_constraint = &constraint;
2126 psa_key_policy_set_usage( p_constraint,
2127 constraint_usage, constraint_alg );
2128 }
2129
2130 PSA_ASSERT( psa_crypto_init( ) );
2131
2132 /* Populate the source slot. */
2133 PSA_ASSERT( psa_allocate_key( &source_handle ) );
2134 psa_key_policy_set_usage( &source_policy, source_usage, source_alg );
2135 PSA_ASSERT( psa_set_key_policy( source_handle, &source_policy ) );
2136 PSA_ASSERT( psa_import_key( source_handle, source_type,
2137 material->x, material->len ) );
2138 PSA_ASSERT( psa_get_key_information( source_handle, NULL, &source_bits ) );
2139
2140 /* Prepare the target slot. */
2141 PSA_ASSERT( psa_allocate_key( &target_handle ) );
2142 psa_key_policy_set_usage( &target_policy, target_usage, target_alg );
2143 PSA_ASSERT( psa_set_key_policy( target_handle, &target_policy ) );
2144 target_policy = psa_key_policy_init();
2145
2146 /* Copy the key. */
2147 TEST_EQUAL( psa_copy_key( source_handle, target_handle, p_constraint ),
2148 expected_status );
2149
2150 /* Test that the target slot is unaffected. */
2151 TEST_EQUAL( psa_get_key_information( target_handle,
2152 &target_type, &target_bits ),
David Saadab4ecc272019-02-14 13:48:10 +02002153 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002154 PSA_ASSERT( psa_get_key_policy( target_handle, &target_policy ) );
2155 TEST_EQUAL( target_usage, psa_key_policy_get_usage( &target_policy ) );
2156 TEST_EQUAL( target_alg, psa_key_policy_get_algorithm( &target_policy ) );
2157
2158exit:
2159 mbedtls_psa_crypto_free( );
2160}
2161/* END_CASE */
2162
2163/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002164void hash_operation_init( )
2165{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002166 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002167 /* Test each valid way of initializing the object, except for `= {0}`, as
2168 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2169 * though it's OK by the C standard. We could test for this, but we'd need
2170 * to supress the Clang warning for the test. */
2171 psa_hash_operation_t func = psa_hash_operation_init( );
2172 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2173 psa_hash_operation_t zero;
2174
2175 memset( &zero, 0, sizeof( zero ) );
2176
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002177 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002178 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2179 PSA_ERROR_BAD_STATE );
2180 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2181 PSA_ERROR_BAD_STATE );
2182 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2183 PSA_ERROR_BAD_STATE );
2184
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002185 /* A default hash operation should be abortable without error. */
2186 PSA_ASSERT( psa_hash_abort( &func ) );
2187 PSA_ASSERT( psa_hash_abort( &init ) );
2188 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002189}
2190/* END_CASE */
2191
2192/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002193void hash_setup( int alg_arg,
2194 int expected_status_arg )
2195{
2196 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002197 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002198 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002199 psa_status_t status;
2200
Gilles Peskine8817f612018-12-18 00:18:46 +01002201 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002202
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002203 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002204 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002205
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002206 /* Whether setup succeeded or failed, abort must succeed. */
2207 PSA_ASSERT( psa_hash_abort( &operation ) );
2208
2209 /* If setup failed, reproduce the failure, so as to
2210 * test the resulting state of the operation object. */
2211 if( status != PSA_SUCCESS )
2212 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2213
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002214 /* Now the operation object should be reusable. */
2215#if defined(KNOWN_SUPPORTED_HASH_ALG)
2216 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2217 PSA_ASSERT( psa_hash_abort( &operation ) );
2218#endif
2219
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002220exit:
2221 mbedtls_psa_crypto_free( );
2222}
2223/* END_CASE */
2224
2225/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002226void hash_bad_order( )
2227{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002228 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002229 unsigned char input[] = "";
2230 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002231 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002232 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2233 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2234 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002235 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002236 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002237 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002238
Gilles Peskine8817f612018-12-18 00:18:46 +01002239 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002240
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002241 /* Call setup twice in a row. */
2242 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2243 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2244 PSA_ERROR_BAD_STATE );
2245 PSA_ASSERT( psa_hash_abort( &operation ) );
2246
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002247 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002248 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002249 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002250 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002251
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002252 /* Call update after finish. */
2253 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2254 PSA_ASSERT( psa_hash_finish( &operation,
2255 hash, sizeof( hash ), &hash_len ) );
2256 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002257 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002258 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002259
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002260 /* Call verify without calling setup beforehand. */
2261 TEST_EQUAL( psa_hash_verify( &operation,
2262 valid_hash, sizeof( valid_hash ) ),
2263 PSA_ERROR_BAD_STATE );
2264 PSA_ASSERT( psa_hash_abort( &operation ) );
2265
2266 /* Call verify after finish. */
2267 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2268 PSA_ASSERT( psa_hash_finish( &operation,
2269 hash, sizeof( hash ), &hash_len ) );
2270 TEST_EQUAL( psa_hash_verify( &operation,
2271 valid_hash, sizeof( valid_hash ) ),
2272 PSA_ERROR_BAD_STATE );
2273 PSA_ASSERT( psa_hash_abort( &operation ) );
2274
2275 /* Call verify twice in a row. */
2276 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2277 PSA_ASSERT( psa_hash_verify( &operation,
2278 valid_hash, sizeof( valid_hash ) ) );
2279 TEST_EQUAL( psa_hash_verify( &operation,
2280 valid_hash, sizeof( valid_hash ) ),
2281 PSA_ERROR_BAD_STATE );
2282 PSA_ASSERT( psa_hash_abort( &operation ) );
2283
2284 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002285 TEST_EQUAL( psa_hash_finish( &operation,
2286 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002287 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002288 PSA_ASSERT( psa_hash_abort( &operation ) );
2289
2290 /* Call finish twice in a row. */
2291 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2292 PSA_ASSERT( psa_hash_finish( &operation,
2293 hash, sizeof( hash ), &hash_len ) );
2294 TEST_EQUAL( psa_hash_finish( &operation,
2295 hash, sizeof( hash ), &hash_len ),
2296 PSA_ERROR_BAD_STATE );
2297 PSA_ASSERT( psa_hash_abort( &operation ) );
2298
2299 /* Call finish after calling verify. */
2300 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2301 PSA_ASSERT( psa_hash_verify( &operation,
2302 valid_hash, sizeof( valid_hash ) ) );
2303 TEST_EQUAL( psa_hash_finish( &operation,
2304 hash, sizeof( hash ), &hash_len ),
2305 PSA_ERROR_BAD_STATE );
2306 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002307
2308exit:
2309 mbedtls_psa_crypto_free( );
2310}
2311/* END_CASE */
2312
itayzafrir27e69452018-11-01 14:26:34 +02002313/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2314void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002315{
2316 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002317 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2318 * appended to it */
2319 unsigned char hash[] = {
2320 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2321 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2322 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002323 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002324 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002325
Gilles Peskine8817f612018-12-18 00:18:46 +01002326 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002327
itayzafrir27e69452018-11-01 14:26:34 +02002328 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002329 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002330 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002331 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002332
itayzafrir27e69452018-11-01 14:26:34 +02002333 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002334 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002335 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002336 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002337
itayzafrir27e69452018-11-01 14:26:34 +02002338 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002339 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002340 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002341 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002342
itayzafrirec93d302018-10-18 18:01:10 +03002343exit:
2344 mbedtls_psa_crypto_free( );
2345}
2346/* END_CASE */
2347
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002348/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2349void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002350{
2351 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002352 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002353 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002354 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002355 size_t hash_len;
2356
Gilles Peskine8817f612018-12-18 00:18:46 +01002357 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002358
itayzafrir58028322018-10-25 10:22:01 +03002359 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002360 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002361 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002362 hash, expected_size - 1, &hash_len ),
2363 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002364
2365exit:
2366 mbedtls_psa_crypto_free( );
2367}
2368/* END_CASE */
2369
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002370/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2371void hash_clone_source_state( )
2372{
2373 psa_algorithm_t alg = PSA_ALG_SHA_256;
2374 unsigned char hash[PSA_HASH_MAX_SIZE];
2375 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2376 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2377 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2378 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2379 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2380 size_t hash_len;
2381
2382 PSA_ASSERT( psa_crypto_init( ) );
2383 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2384
2385 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2386 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2387 PSA_ASSERT( psa_hash_finish( &op_finished,
2388 hash, sizeof( hash ), &hash_len ) );
2389 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2390 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2391
2392 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2393 PSA_ERROR_BAD_STATE );
2394
2395 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2396 PSA_ASSERT( psa_hash_finish( &op_init,
2397 hash, sizeof( hash ), &hash_len ) );
2398 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2399 PSA_ASSERT( psa_hash_finish( &op_finished,
2400 hash, sizeof( hash ), &hash_len ) );
2401 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2402 PSA_ASSERT( psa_hash_finish( &op_aborted,
2403 hash, sizeof( hash ), &hash_len ) );
2404
2405exit:
2406 psa_hash_abort( &op_source );
2407 psa_hash_abort( &op_init );
2408 psa_hash_abort( &op_setup );
2409 psa_hash_abort( &op_finished );
2410 psa_hash_abort( &op_aborted );
2411 mbedtls_psa_crypto_free( );
2412}
2413/* END_CASE */
2414
2415/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2416void hash_clone_target_state( )
2417{
2418 psa_algorithm_t alg = PSA_ALG_SHA_256;
2419 unsigned char hash[PSA_HASH_MAX_SIZE];
2420 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2421 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2422 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2423 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2424 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2425 size_t hash_len;
2426
2427 PSA_ASSERT( psa_crypto_init( ) );
2428
2429 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2430 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2431 PSA_ASSERT( psa_hash_finish( &op_finished,
2432 hash, sizeof( hash ), &hash_len ) );
2433 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2434 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2435
2436 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2437 PSA_ASSERT( psa_hash_finish( &op_target,
2438 hash, sizeof( hash ), &hash_len ) );
2439
2440 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2441 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2442 PSA_ERROR_BAD_STATE );
2443 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2444 PSA_ERROR_BAD_STATE );
2445
2446exit:
2447 psa_hash_abort( &op_target );
2448 psa_hash_abort( &op_init );
2449 psa_hash_abort( &op_setup );
2450 psa_hash_abort( &op_finished );
2451 psa_hash_abort( &op_aborted );
2452 mbedtls_psa_crypto_free( );
2453}
2454/* END_CASE */
2455
itayzafrir58028322018-10-25 10:22:01 +03002456/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002457void mac_operation_init( )
2458{
Jaeden Amero252ef282019-02-15 14:05:35 +00002459 const uint8_t input[1] = { 0 };
2460
Jaeden Amero769ce272019-01-04 11:48:03 +00002461 /* Test each valid way of initializing the object, except for `= {0}`, as
2462 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2463 * though it's OK by the C standard. We could test for this, but we'd need
2464 * to supress the Clang warning for the test. */
2465 psa_mac_operation_t func = psa_mac_operation_init( );
2466 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2467 psa_mac_operation_t zero;
2468
2469 memset( &zero, 0, sizeof( zero ) );
2470
Jaeden Amero252ef282019-02-15 14:05:35 +00002471 /* A freshly-initialized MAC operation should not be usable. */
2472 TEST_EQUAL( psa_mac_update( &func,
2473 input, sizeof( input ) ),
2474 PSA_ERROR_BAD_STATE );
2475 TEST_EQUAL( psa_mac_update( &init,
2476 input, sizeof( input ) ),
2477 PSA_ERROR_BAD_STATE );
2478 TEST_EQUAL( psa_mac_update( &zero,
2479 input, sizeof( input ) ),
2480 PSA_ERROR_BAD_STATE );
2481
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002482 /* A default MAC operation should be abortable without error. */
2483 PSA_ASSERT( psa_mac_abort( &func ) );
2484 PSA_ASSERT( psa_mac_abort( &init ) );
2485 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002486}
2487/* END_CASE */
2488
2489/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002490void mac_setup( int key_type_arg,
2491 data_t *key,
2492 int alg_arg,
2493 int expected_status_arg )
2494{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002495 psa_key_type_t key_type = key_type_arg;
2496 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002497 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002498 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002499 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2500#if defined(KNOWN_SUPPORTED_MAC_ALG)
2501 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2502#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002503
Gilles Peskine8817f612018-12-18 00:18:46 +01002504 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002505
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002506 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2507 &operation, &status ) )
2508 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002509 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002510
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002511 /* The operation object should be reusable. */
2512#if defined(KNOWN_SUPPORTED_MAC_ALG)
2513 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2514 smoke_test_key_data,
2515 sizeof( smoke_test_key_data ),
2516 KNOWN_SUPPORTED_MAC_ALG,
2517 &operation, &status ) )
2518 goto exit;
2519 TEST_EQUAL( status, PSA_SUCCESS );
2520#endif
2521
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002522exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002523 mbedtls_psa_crypto_free( );
2524}
2525/* END_CASE */
2526
2527/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002528void mac_bad_order( )
2529{
2530 psa_key_handle_t handle = 0;
2531 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2532 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2533 const uint8_t key[] = {
2534 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2535 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2536 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
2537 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2538 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2539 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2540 size_t sign_mac_length = 0;
2541 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2542 const uint8_t verify_mac[] = {
2543 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2544 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2545 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2546
2547 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002548 PSA_ASSERT( psa_allocate_key( &handle ) );
2549 psa_key_policy_set_usage( &policy,
2550 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002551 alg );
2552 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2553
2554 PSA_ASSERT( psa_import_key( handle, key_type,
Jaeden Amero252ef282019-02-15 14:05:35 +00002555 key, sizeof(key) ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002556
Jaeden Amero252ef282019-02-15 14:05:35 +00002557 /* Call update without calling setup beforehand. */
2558 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2559 PSA_ERROR_BAD_STATE );
2560 PSA_ASSERT( psa_mac_abort( &operation ) );
2561
2562 /* Call sign finish without calling setup beforehand. */
2563 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2564 &sign_mac_length),
2565 PSA_ERROR_BAD_STATE );
2566 PSA_ASSERT( psa_mac_abort( &operation ) );
2567
2568 /* Call verify finish without calling setup beforehand. */
2569 TEST_EQUAL( psa_mac_verify_finish( &operation,
2570 verify_mac, sizeof( verify_mac ) ),
2571 PSA_ERROR_BAD_STATE );
2572 PSA_ASSERT( psa_mac_abort( &operation ) );
2573
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002574 /* Call setup twice in a row. */
2575 PSA_ASSERT( psa_mac_sign_setup( &operation,
2576 handle, alg ) );
2577 TEST_EQUAL( psa_mac_sign_setup( &operation,
2578 handle, alg ),
2579 PSA_ERROR_BAD_STATE );
2580 PSA_ASSERT( psa_mac_abort( &operation ) );
2581
Jaeden Amero252ef282019-02-15 14:05:35 +00002582 /* Call update after sign finish. */
2583 PSA_ASSERT( psa_mac_sign_setup( &operation,
2584 handle, alg ) );
2585 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2586 PSA_ASSERT( psa_mac_sign_finish( &operation,
2587 sign_mac, sizeof( sign_mac ),
2588 &sign_mac_length ) );
2589 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2590 PSA_ERROR_BAD_STATE );
2591 PSA_ASSERT( psa_mac_abort( &operation ) );
2592
2593 /* Call update after verify finish. */
2594 PSA_ASSERT( psa_mac_verify_setup( &operation,
2595 handle, alg ) );
2596 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2597 PSA_ASSERT( psa_mac_verify_finish( &operation,
2598 verify_mac, sizeof( verify_mac ) ) );
2599 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2600 PSA_ERROR_BAD_STATE );
2601 PSA_ASSERT( psa_mac_abort( &operation ) );
2602
2603 /* Call sign finish twice in a row. */
2604 PSA_ASSERT( psa_mac_sign_setup( &operation,
2605 handle, alg ) );
2606 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2607 PSA_ASSERT( psa_mac_sign_finish( &operation,
2608 sign_mac, sizeof( sign_mac ),
2609 &sign_mac_length ) );
2610 TEST_EQUAL( psa_mac_sign_finish( &operation,
2611 sign_mac, sizeof( sign_mac ),
2612 &sign_mac_length ),
2613 PSA_ERROR_BAD_STATE );
2614 PSA_ASSERT( psa_mac_abort( &operation ) );
2615
2616 /* Call verify finish twice in a row. */
2617 PSA_ASSERT( psa_mac_verify_setup( &operation,
2618 handle, alg ) );
2619 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2620 PSA_ASSERT( psa_mac_verify_finish( &operation,
2621 verify_mac, sizeof( verify_mac ) ) );
2622 TEST_EQUAL( psa_mac_verify_finish( &operation,
2623 verify_mac, sizeof( verify_mac ) ),
2624 PSA_ERROR_BAD_STATE );
2625 PSA_ASSERT( psa_mac_abort( &operation ) );
2626
2627 /* Setup sign but try verify. */
2628 PSA_ASSERT( psa_mac_sign_setup( &operation,
2629 handle, alg ) );
2630 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2631 TEST_EQUAL( psa_mac_verify_finish( &operation,
2632 verify_mac, sizeof( verify_mac ) ),
2633 PSA_ERROR_BAD_STATE );
2634 PSA_ASSERT( psa_mac_abort( &operation ) );
2635
2636 /* Setup verify but try sign. */
2637 PSA_ASSERT( psa_mac_verify_setup( &operation,
2638 handle, alg ) );
2639 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2640 TEST_EQUAL( psa_mac_sign_finish( &operation,
2641 sign_mac, sizeof( sign_mac ),
2642 &sign_mac_length ),
2643 PSA_ERROR_BAD_STATE );
2644 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002645
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002646exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002647 mbedtls_psa_crypto_free( );
2648}
2649/* END_CASE */
2650
2651/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002652void mac_sign( int key_type_arg,
2653 data_t *key,
2654 int alg_arg,
2655 data_t *input,
2656 data_t *expected_mac )
2657{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002658 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002659 psa_key_type_t key_type = key_type_arg;
2660 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002661 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002662 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002663 /* Leave a little extra room in the output buffer. At the end of the
2664 * test, we'll check that the implementation didn't overwrite onto
2665 * this extra room. */
2666 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2667 size_t mac_buffer_size =
2668 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2669 size_t mac_length = 0;
2670
2671 memset( actual_mac, '+', sizeof( actual_mac ) );
2672 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2673 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2674
Gilles Peskine8817f612018-12-18 00:18:46 +01002675 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002676
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002677 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002678 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002679 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002680
Gilles Peskine8817f612018-12-18 00:18:46 +01002681 PSA_ASSERT( psa_import_key( handle, key_type,
2682 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002683
2684 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002685 PSA_ASSERT( psa_mac_sign_setup( &operation,
2686 handle, alg ) );
2687 PSA_ASSERT( psa_mac_update( &operation,
2688 input->x, input->len ) );
2689 PSA_ASSERT( psa_mac_sign_finish( &operation,
2690 actual_mac, mac_buffer_size,
2691 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002692
2693 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002694 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2695 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002696
2697 /* Verify that the end of the buffer is untouched. */
2698 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2699 sizeof( actual_mac ) - mac_length ) );
2700
2701exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002702 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002703 mbedtls_psa_crypto_free( );
2704}
2705/* END_CASE */
2706
2707/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002708void mac_verify( int key_type_arg,
2709 data_t *key,
2710 int alg_arg,
2711 data_t *input,
2712 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002713{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002714 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002715 psa_key_type_t key_type = key_type_arg;
2716 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002717 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002718 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002719
Gilles Peskine69c12672018-06-28 00:07:19 +02002720 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2721
Gilles Peskine8817f612018-12-18 00:18:46 +01002722 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002723
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002724 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002725 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002726 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002727
Gilles Peskine8817f612018-12-18 00:18:46 +01002728 PSA_ASSERT( psa_import_key( handle, key_type,
2729 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002730
Gilles Peskine8817f612018-12-18 00:18:46 +01002731 PSA_ASSERT( psa_mac_verify_setup( &operation,
2732 handle, alg ) );
2733 PSA_ASSERT( psa_destroy_key( handle ) );
2734 PSA_ASSERT( psa_mac_update( &operation,
2735 input->x, input->len ) );
2736 PSA_ASSERT( psa_mac_verify_finish( &operation,
2737 expected_mac->x,
2738 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002739
2740exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002741 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002742 mbedtls_psa_crypto_free( );
2743}
2744/* END_CASE */
2745
2746/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002747void cipher_operation_init( )
2748{
Jaeden Ameroab439972019-02-15 14:12:05 +00002749 const uint8_t input[1] = { 0 };
2750 unsigned char output[1] = { 0 };
2751 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002752 /* Test each valid way of initializing the object, except for `= {0}`, as
2753 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2754 * though it's OK by the C standard. We could test for this, but we'd need
2755 * to supress the Clang warning for the test. */
2756 psa_cipher_operation_t func = psa_cipher_operation_init( );
2757 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2758 psa_cipher_operation_t zero;
2759
2760 memset( &zero, 0, sizeof( zero ) );
2761
Jaeden Ameroab439972019-02-15 14:12:05 +00002762 /* A freshly-initialized cipher operation should not be usable. */
2763 TEST_EQUAL( psa_cipher_update( &func,
2764 input, sizeof( input ),
2765 output, sizeof( output ),
2766 &output_length ),
2767 PSA_ERROR_BAD_STATE );
2768 TEST_EQUAL( psa_cipher_update( &init,
2769 input, sizeof( input ),
2770 output, sizeof( output ),
2771 &output_length ),
2772 PSA_ERROR_BAD_STATE );
2773 TEST_EQUAL( psa_cipher_update( &zero,
2774 input, sizeof( input ),
2775 output, sizeof( output ),
2776 &output_length ),
2777 PSA_ERROR_BAD_STATE );
2778
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002779 /* A default cipher operation should be abortable without error. */
2780 PSA_ASSERT( psa_cipher_abort( &func ) );
2781 PSA_ASSERT( psa_cipher_abort( &init ) );
2782 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002783}
2784/* END_CASE */
2785
2786/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002787void cipher_setup( int key_type_arg,
2788 data_t *key,
2789 int alg_arg,
2790 int expected_status_arg )
2791{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002792 psa_key_type_t key_type = key_type_arg;
2793 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002794 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002795 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002796 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002797#if defined(KNOWN_SUPPORTED_MAC_ALG)
2798 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2799#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002800
Gilles Peskine8817f612018-12-18 00:18:46 +01002801 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002802
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002803 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2804 &operation, &status ) )
2805 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002806 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002807
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002808 /* The operation object should be reusable. */
2809#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2810 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2811 smoke_test_key_data,
2812 sizeof( smoke_test_key_data ),
2813 KNOWN_SUPPORTED_CIPHER_ALG,
2814 &operation, &status ) )
2815 goto exit;
2816 TEST_EQUAL( status, PSA_SUCCESS );
2817#endif
2818
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002819exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002820 mbedtls_psa_crypto_free( );
2821}
2822/* END_CASE */
2823
2824/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002825void cipher_bad_order( )
2826{
2827 psa_key_handle_t handle = 0;
2828 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2829 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
2830 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2831 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2832 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2833 const uint8_t key[] = {
2834 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2835 0xaa, 0xaa, 0xaa, 0xaa };
2836 const uint8_t text[] = {
2837 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2838 0xbb, 0xbb, 0xbb, 0xbb };
2839 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2840 size_t length = 0;
2841
2842 PSA_ASSERT( psa_crypto_init( ) );
2843 PSA_ASSERT( psa_allocate_key( &handle ) );
2844 psa_key_policy_set_usage( &policy,
2845 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2846 alg );
2847 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2848 PSA_ASSERT( psa_import_key( handle, key_type,
2849 key, sizeof(key) ) );
2850
2851
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002852 /* Call encrypt setup twice in a row. */
2853 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2854 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2855 PSA_ERROR_BAD_STATE );
2856 PSA_ASSERT( psa_cipher_abort( &operation ) );
2857
2858 /* Call decrypt setup twice in a row. */
2859 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2860 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2861 PSA_ERROR_BAD_STATE );
2862 PSA_ASSERT( psa_cipher_abort( &operation ) );
2863
Jaeden Ameroab439972019-02-15 14:12:05 +00002864 /* Generate an IV without calling setup beforehand. */
2865 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2866 buffer, sizeof( buffer ),
2867 &length ),
2868 PSA_ERROR_BAD_STATE );
2869 PSA_ASSERT( psa_cipher_abort( &operation ) );
2870
2871 /* Generate an IV twice in a row. */
2872 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2873 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2874 buffer, sizeof( buffer ),
2875 &length ) );
2876 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2877 buffer, sizeof( buffer ),
2878 &length ),
2879 PSA_ERROR_BAD_STATE );
2880 PSA_ASSERT( psa_cipher_abort( &operation ) );
2881
2882 /* Generate an IV after it's already set. */
2883 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2884 PSA_ASSERT( psa_cipher_set_iv( &operation,
2885 iv, sizeof( iv ) ) );
2886 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2887 buffer, sizeof( buffer ),
2888 &length ),
2889 PSA_ERROR_BAD_STATE );
2890 PSA_ASSERT( psa_cipher_abort( &operation ) );
2891
2892 /* Set an IV without calling setup beforehand. */
2893 TEST_EQUAL( psa_cipher_set_iv( &operation,
2894 iv, sizeof( iv ) ),
2895 PSA_ERROR_BAD_STATE );
2896 PSA_ASSERT( psa_cipher_abort( &operation ) );
2897
2898 /* Set an IV after it's already set. */
2899 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2900 PSA_ASSERT( psa_cipher_set_iv( &operation,
2901 iv, sizeof( iv ) ) );
2902 TEST_EQUAL( psa_cipher_set_iv( &operation,
2903 iv, sizeof( iv ) ),
2904 PSA_ERROR_BAD_STATE );
2905 PSA_ASSERT( psa_cipher_abort( &operation ) );
2906
2907 /* Set an IV after it's already generated. */
2908 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2909 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2910 buffer, sizeof( buffer ),
2911 &length ) );
2912 TEST_EQUAL( psa_cipher_set_iv( &operation,
2913 iv, sizeof( iv ) ),
2914 PSA_ERROR_BAD_STATE );
2915 PSA_ASSERT( psa_cipher_abort( &operation ) );
2916
2917 /* Call update without calling setup beforehand. */
2918 TEST_EQUAL( psa_cipher_update( &operation,
2919 text, sizeof( text ),
2920 buffer, sizeof( buffer ),
2921 &length ),
2922 PSA_ERROR_BAD_STATE );
2923 PSA_ASSERT( psa_cipher_abort( &operation ) );
2924
2925 /* Call update without an IV where an IV is required. */
2926 TEST_EQUAL( psa_cipher_update( &operation,
2927 text, sizeof( text ),
2928 buffer, sizeof( buffer ),
2929 &length ),
2930 PSA_ERROR_BAD_STATE );
2931 PSA_ASSERT( psa_cipher_abort( &operation ) );
2932
2933 /* Call update after finish. */
2934 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2935 PSA_ASSERT( psa_cipher_set_iv( &operation,
2936 iv, sizeof( iv ) ) );
2937 PSA_ASSERT( psa_cipher_finish( &operation,
2938 buffer, sizeof( buffer ), &length ) );
2939 TEST_EQUAL( psa_cipher_update( &operation,
2940 text, sizeof( text ),
2941 buffer, sizeof( buffer ),
2942 &length ),
2943 PSA_ERROR_BAD_STATE );
2944 PSA_ASSERT( psa_cipher_abort( &operation ) );
2945
2946 /* Call finish without calling setup beforehand. */
2947 TEST_EQUAL( psa_cipher_finish( &operation,
2948 buffer, sizeof( buffer ), &length ),
2949 PSA_ERROR_BAD_STATE );
2950 PSA_ASSERT( psa_cipher_abort( &operation ) );
2951
2952 /* Call finish without an IV where an IV is required. */
2953 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2954 /* Not calling update means we are encrypting an empty buffer, which is OK
2955 * for cipher modes with padding. */
2956 TEST_EQUAL( psa_cipher_finish( &operation,
2957 buffer, sizeof( buffer ), &length ),
2958 PSA_ERROR_BAD_STATE );
2959 PSA_ASSERT( psa_cipher_abort( &operation ) );
2960
2961 /* Call finish twice in a row. */
2962 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2963 PSA_ASSERT( psa_cipher_set_iv( &operation,
2964 iv, sizeof( iv ) ) );
2965 PSA_ASSERT( psa_cipher_finish( &operation,
2966 buffer, sizeof( buffer ), &length ) );
2967 TEST_EQUAL( psa_cipher_finish( &operation,
2968 buffer, sizeof( buffer ), &length ),
2969 PSA_ERROR_BAD_STATE );
2970 PSA_ASSERT( psa_cipher_abort( &operation ) );
2971
2972exit:
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002973 mbedtls_psa_crypto_free( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002974}
2975/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002976
Gilles Peskine50e586b2018-06-08 14:28:46 +02002977/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002978void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002979 data_t *key,
2980 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002981 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002982{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002983 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002984 psa_status_t status;
2985 psa_key_type_t key_type = key_type_arg;
2986 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002987 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002988 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002989 size_t iv_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002990 unsigned char *output = NULL;
2991 size_t output_buffer_size = 0;
2992 size_t function_output_length = 0;
2993 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002994 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002995 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002996
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002997 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2998 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002999
Gilles Peskine8817f612018-12-18 00:18:46 +01003000 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003001
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003002 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003003 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003004 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003005
Gilles Peskine8817f612018-12-18 00:18:46 +01003006 PSA_ASSERT( psa_import_key( handle, key_type,
3007 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003008
Gilles Peskine8817f612018-12-18 00:18:46 +01003009 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3010 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003011
Gilles Peskine8817f612018-12-18 00:18:46 +01003012 PSA_ASSERT( psa_cipher_set_iv( &operation,
3013 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003014 output_buffer_size = ( (size_t) input->len +
3015 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003016 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003017
Gilles Peskine8817f612018-12-18 00:18:46 +01003018 PSA_ASSERT( psa_cipher_update( &operation,
3019 input->x, input->len,
3020 output, output_buffer_size,
3021 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003022 total_output_length += function_output_length;
3023 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003024 output + total_output_length,
3025 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003026 &function_output_length );
3027 total_output_length += function_output_length;
3028
Gilles Peskinefe11b722018-12-18 00:24:04 +01003029 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003030 if( expected_status == PSA_SUCCESS )
3031 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003032 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003033 ASSERT_COMPARE( expected_output->x, expected_output->len,
3034 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003035 }
3036
3037exit:
3038 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003039 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003040 mbedtls_psa_crypto_free( );
3041}
3042/* END_CASE */
3043
3044/* BEGIN_CASE */
3045void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
3046 data_t *key,
3047 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003048 int first_part_size_arg,
3049 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003050 data_t *expected_output )
3051{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003052 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003053 psa_key_type_t key_type = key_type_arg;
3054 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003055 size_t first_part_size = first_part_size_arg;
3056 size_t output1_length = output1_length_arg;
3057 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003058 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003059 size_t iv_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003060 unsigned char *output = NULL;
3061 size_t output_buffer_size = 0;
3062 size_t function_output_length = 0;
3063 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003064 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003065 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003066
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003067 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3068 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003069
Gilles Peskine8817f612018-12-18 00:18:46 +01003070 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003071
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003072 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003073 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003074 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003075
Gilles Peskine8817f612018-12-18 00:18:46 +01003076 PSA_ASSERT( psa_import_key( handle, key_type,
3077 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003078
Gilles Peskine8817f612018-12-18 00:18:46 +01003079 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3080 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003081
Gilles Peskine8817f612018-12-18 00:18:46 +01003082 PSA_ASSERT( psa_cipher_set_iv( &operation,
3083 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003084 output_buffer_size = ( (size_t) input->len +
3085 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003086 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003087
Gilles Peskinee0866522019-02-19 19:44:00 +01003088 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003089 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3090 output, output_buffer_size,
3091 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003092 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003093 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003094 PSA_ASSERT( psa_cipher_update( &operation,
3095 input->x + first_part_size,
3096 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003097 output + total_output_length,
3098 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003099 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003100 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003101 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003102 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003103 output + total_output_length,
3104 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003105 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003106 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003107 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003108
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003109 ASSERT_COMPARE( expected_output->x, expected_output->len,
3110 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003111
3112exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003113 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003114 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003115 mbedtls_psa_crypto_free( );
3116}
3117/* END_CASE */
3118
3119/* BEGIN_CASE */
3120void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003121 data_t *key,
3122 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003123 int first_part_size_arg,
3124 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003125 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003126{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003127 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003128
3129 psa_key_type_t key_type = key_type_arg;
3130 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003131 size_t first_part_size = first_part_size_arg;
3132 size_t output1_length = output1_length_arg;
3133 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003134 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003135 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003136 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003137 size_t output_buffer_size = 0;
3138 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003139 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003140 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003141 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003142
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003143 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3144 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003145
Gilles Peskine8817f612018-12-18 00:18:46 +01003146 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003147
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003148 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003149 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003150 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003151
Gilles Peskine8817f612018-12-18 00:18:46 +01003152 PSA_ASSERT( psa_import_key( handle, key_type,
3153 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003154
Gilles Peskine8817f612018-12-18 00:18:46 +01003155 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3156 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003157
Gilles Peskine8817f612018-12-18 00:18:46 +01003158 PSA_ASSERT( psa_cipher_set_iv( &operation,
3159 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003160
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003161 output_buffer_size = ( (size_t) input->len +
3162 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003163 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003164
Gilles Peskinee0866522019-02-19 19:44:00 +01003165 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003166 PSA_ASSERT( psa_cipher_update( &operation,
3167 input->x, first_part_size,
3168 output, output_buffer_size,
3169 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003170 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003171 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003172 PSA_ASSERT( psa_cipher_update( &operation,
3173 input->x + first_part_size,
3174 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003175 output + total_output_length,
3176 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003177 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003178 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003179 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003180 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003181 output + total_output_length,
3182 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003183 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003184 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003185 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003186
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003187 ASSERT_COMPARE( expected_output->x, expected_output->len,
3188 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003189
3190exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003191 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003192 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003193 mbedtls_psa_crypto_free( );
3194}
3195/* END_CASE */
3196
Gilles Peskine50e586b2018-06-08 14:28:46 +02003197/* BEGIN_CASE */
3198void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003199 data_t *key,
3200 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003201 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003202{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003203 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003204 psa_status_t status;
3205 psa_key_type_t key_type = key_type_arg;
3206 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003207 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003208 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003209 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003210 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003211 size_t output_buffer_size = 0;
3212 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003213 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003214 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003215 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003216
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003217 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3218 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003219
Gilles Peskine8817f612018-12-18 00:18:46 +01003220 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003221
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003222 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003223 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003224 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003225
Gilles Peskine8817f612018-12-18 00:18:46 +01003226 PSA_ASSERT( psa_import_key( handle, key_type,
3227 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003228
Gilles Peskine8817f612018-12-18 00:18:46 +01003229 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3230 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003231
Gilles Peskine8817f612018-12-18 00:18:46 +01003232 PSA_ASSERT( psa_cipher_set_iv( &operation,
3233 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003234
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003235 output_buffer_size = ( (size_t) input->len +
3236 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003237 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003238
Gilles Peskine8817f612018-12-18 00:18:46 +01003239 PSA_ASSERT( psa_cipher_update( &operation,
3240 input->x, input->len,
3241 output, output_buffer_size,
3242 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003243 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003244 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003245 output + total_output_length,
3246 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003247 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003248 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003249 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003250
3251 if( expected_status == PSA_SUCCESS )
3252 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003253 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003254 ASSERT_COMPARE( expected_output->x, expected_output->len,
3255 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003256 }
3257
Gilles Peskine50e586b2018-06-08 14:28:46 +02003258exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003259 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003260 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003261 mbedtls_psa_crypto_free( );
3262}
3263/* END_CASE */
3264
Gilles Peskine50e586b2018-06-08 14:28:46 +02003265/* BEGIN_CASE */
3266void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003267 data_t *key,
3268 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003269{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003270 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003271 psa_key_type_t key_type = key_type_arg;
3272 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003273 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003274 size_t iv_size = 16;
3275 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003276 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003277 size_t output1_size = 0;
3278 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003279 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003280 size_t output2_size = 0;
3281 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003282 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003283 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3284 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003285 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003286
Gilles Peskine8817f612018-12-18 00:18:46 +01003287 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003288
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003289 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003290 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003291 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003292
Gilles Peskine8817f612018-12-18 00:18:46 +01003293 PSA_ASSERT( psa_import_key( handle, key_type,
3294 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003295
Gilles Peskine8817f612018-12-18 00:18:46 +01003296 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3297 handle, alg ) );
3298 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3299 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003300
Gilles Peskine8817f612018-12-18 00:18:46 +01003301 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3302 iv, iv_size,
3303 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003304 output1_size = ( (size_t) input->len +
3305 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003306 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003307
Gilles Peskine8817f612018-12-18 00:18:46 +01003308 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3309 output1, output1_size,
3310 &output1_length ) );
3311 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003312 output1 + output1_length,
3313 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003314 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003315
Gilles Peskine048b7f02018-06-08 14:20:49 +02003316 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003317
Gilles Peskine8817f612018-12-18 00:18:46 +01003318 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003319
3320 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003321 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003322
Gilles Peskine8817f612018-12-18 00:18:46 +01003323 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3324 iv, iv_length ) );
3325 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3326 output2, output2_size,
3327 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003328 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003329 PSA_ASSERT( psa_cipher_finish( &operation2,
3330 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003331 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003332 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003333
Gilles Peskine048b7f02018-06-08 14:20:49 +02003334 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003335
Gilles Peskine8817f612018-12-18 00:18:46 +01003336 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003337
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003338 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003339
3340exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003341 mbedtls_free( output1 );
3342 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003343 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03003344 mbedtls_psa_crypto_free( );
3345}
3346/* END_CASE */
3347
3348/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003349void cipher_verify_output_multipart( int alg_arg,
3350 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003351 data_t *key,
3352 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003353 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003354{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003355 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003356 psa_key_type_t key_type = key_type_arg;
3357 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003358 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003359 unsigned char iv[16] = {0};
3360 size_t iv_size = 16;
3361 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003362 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003363 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003364 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003365 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003366 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003367 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003368 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003369 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3370 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003371 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003372
Gilles Peskine8817f612018-12-18 00:18:46 +01003373 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003374
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003375 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003376 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003377 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003378
Gilles Peskine8817f612018-12-18 00:18:46 +01003379 PSA_ASSERT( psa_import_key( handle, key_type,
3380 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003381
Gilles Peskine8817f612018-12-18 00:18:46 +01003382 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3383 handle, alg ) );
3384 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3385 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003386
Gilles Peskine8817f612018-12-18 00:18:46 +01003387 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3388 iv, iv_size,
3389 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003390 output1_buffer_size = ( (size_t) input->len +
3391 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003392 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003393
Gilles Peskinee0866522019-02-19 19:44:00 +01003394 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003395
Gilles Peskine8817f612018-12-18 00:18:46 +01003396 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3397 output1, output1_buffer_size,
3398 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003399 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003400
Gilles Peskine8817f612018-12-18 00:18:46 +01003401 PSA_ASSERT( psa_cipher_update( &operation1,
3402 input->x + first_part_size,
3403 input->len - first_part_size,
3404 output1, output1_buffer_size,
3405 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003406 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003407
Gilles Peskine8817f612018-12-18 00:18:46 +01003408 PSA_ASSERT( psa_cipher_finish( &operation1,
3409 output1 + output1_length,
3410 output1_buffer_size - output1_length,
3411 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003412 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003413
Gilles Peskine8817f612018-12-18 00:18:46 +01003414 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003415
Gilles Peskine048b7f02018-06-08 14:20:49 +02003416 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003417 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003418
Gilles Peskine8817f612018-12-18 00:18:46 +01003419 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3420 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003421
Gilles Peskine8817f612018-12-18 00:18:46 +01003422 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3423 output2, output2_buffer_size,
3424 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003425 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003426
Gilles Peskine8817f612018-12-18 00:18:46 +01003427 PSA_ASSERT( psa_cipher_update( &operation2,
3428 output1 + first_part_size,
3429 output1_length - first_part_size,
3430 output2, output2_buffer_size,
3431 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003432 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003433
Gilles Peskine8817f612018-12-18 00:18:46 +01003434 PSA_ASSERT( psa_cipher_finish( &operation2,
3435 output2 + output2_length,
3436 output2_buffer_size - output2_length,
3437 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003438 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003439
Gilles Peskine8817f612018-12-18 00:18:46 +01003440 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003441
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003442 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003443
3444exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003445 mbedtls_free( output1 );
3446 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003447 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003448 mbedtls_psa_crypto_free( );
3449}
3450/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003451
Gilles Peskine20035e32018-02-03 22:44:14 +01003452/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003453void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003454 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003455 data_t *nonce,
3456 data_t *additional_data,
3457 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003458 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003459{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003460 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003461 psa_key_type_t key_type = key_type_arg;
3462 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003463 unsigned char *output_data = NULL;
3464 size_t output_size = 0;
3465 size_t output_length = 0;
3466 unsigned char *output_data2 = NULL;
3467 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003468 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003469 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003470 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003471
Gilles Peskine4abf7412018-06-18 16:35:34 +02003472 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003473 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003474
Gilles Peskine8817f612018-12-18 00:18:46 +01003475 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003476
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003477 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003478 psa_key_policy_set_usage( &policy,
3479 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
3480 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003481 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003482
Gilles Peskine8817f612018-12-18 00:18:46 +01003483 PSA_ASSERT( psa_import_key( handle, key_type,
3484 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003485
Gilles Peskinefe11b722018-12-18 00:24:04 +01003486 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3487 nonce->x, nonce->len,
3488 additional_data->x,
3489 additional_data->len,
3490 input_data->x, input_data->len,
3491 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003492 &output_length ),
3493 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003494
3495 if( PSA_SUCCESS == expected_result )
3496 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003497 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003498
Gilles Peskinefe11b722018-12-18 00:24:04 +01003499 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3500 nonce->x, nonce->len,
3501 additional_data->x,
3502 additional_data->len,
3503 output_data, output_length,
3504 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003505 &output_length2 ),
3506 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003507
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003508 ASSERT_COMPARE( input_data->x, input_data->len,
3509 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003510 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003511
Gilles Peskinea1cac842018-06-11 19:33:02 +02003512exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003513 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003514 mbedtls_free( output_data );
3515 mbedtls_free( output_data2 );
3516 mbedtls_psa_crypto_free( );
3517}
3518/* END_CASE */
3519
3520/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003521void aead_encrypt( int key_type_arg, data_t *key_data,
3522 int alg_arg,
3523 data_t *nonce,
3524 data_t *additional_data,
3525 data_t *input_data,
3526 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003527{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003528 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003529 psa_key_type_t key_type = key_type_arg;
3530 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003531 unsigned char *output_data = NULL;
3532 size_t output_size = 0;
3533 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003534 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00003535 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003536
Gilles Peskine4abf7412018-06-18 16:35:34 +02003537 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003538 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003539
Gilles Peskine8817f612018-12-18 00:18:46 +01003540 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003541
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003542 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003543 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003544 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003545
Gilles Peskine8817f612018-12-18 00:18:46 +01003546 PSA_ASSERT( psa_import_key( handle, key_type,
3547 key_data->x,
3548 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003549
Gilles Peskine8817f612018-12-18 00:18:46 +01003550 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3551 nonce->x, nonce->len,
3552 additional_data->x, additional_data->len,
3553 input_data->x, input_data->len,
3554 output_data, output_size,
3555 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003556
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003557 ASSERT_COMPARE( expected_result->x, expected_result->len,
3558 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003559
Gilles Peskinea1cac842018-06-11 19:33:02 +02003560exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003561 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003562 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003563 mbedtls_psa_crypto_free( );
3564}
3565/* END_CASE */
3566
3567/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003568void aead_decrypt( int key_type_arg, data_t *key_data,
3569 int alg_arg,
3570 data_t *nonce,
3571 data_t *additional_data,
3572 data_t *input_data,
3573 data_t *expected_data,
3574 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003575{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003576 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003577 psa_key_type_t key_type = key_type_arg;
3578 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003579 unsigned char *output_data = NULL;
3580 size_t output_size = 0;
3581 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003582 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00003583 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003584 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003585
Gilles Peskine4abf7412018-06-18 16:35:34 +02003586 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003587 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003588
Gilles Peskine8817f612018-12-18 00:18:46 +01003589 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003590
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003591 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003592 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003593 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003594
Gilles Peskine8817f612018-12-18 00:18:46 +01003595 PSA_ASSERT( psa_import_key( handle, key_type,
3596 key_data->x,
3597 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003598
Gilles Peskinefe11b722018-12-18 00:24:04 +01003599 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3600 nonce->x, nonce->len,
3601 additional_data->x,
3602 additional_data->len,
3603 input_data->x, input_data->len,
3604 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003605 &output_length ),
3606 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003607
Gilles Peskine2d277862018-06-18 15:41:12 +02003608 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003609 ASSERT_COMPARE( expected_data->x, expected_data->len,
3610 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003611
Gilles Peskinea1cac842018-06-11 19:33:02 +02003612exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003613 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003614 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003615 mbedtls_psa_crypto_free( );
3616}
3617/* END_CASE */
3618
3619/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003620void signature_size( int type_arg,
3621 int bits,
3622 int alg_arg,
3623 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003624{
3625 psa_key_type_t type = type_arg;
3626 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003627 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003628 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003629exit:
3630 ;
3631}
3632/* END_CASE */
3633
3634/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003635void sign_deterministic( int key_type_arg, data_t *key_data,
3636 int alg_arg, data_t *input_data,
3637 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003638{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003639 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003640 psa_key_type_t key_type = key_type_arg;
3641 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003642 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003643 unsigned char *signature = NULL;
3644 size_t signature_size;
3645 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003646 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003647
Gilles Peskine8817f612018-12-18 00:18:46 +01003648 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003649
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003650 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003651 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003652 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003653
Gilles Peskine8817f612018-12-18 00:18:46 +01003654 PSA_ASSERT( psa_import_key( handle, key_type,
3655 key_data->x,
3656 key_data->len ) );
3657 PSA_ASSERT( psa_get_key_information( handle,
3658 NULL,
3659 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003660
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003661 /* Allocate a buffer which has the size advertized by the
3662 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003663 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3664 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003665 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003666 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003667 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003668
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003669 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003670 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3671 input_data->x, input_data->len,
3672 signature, signature_size,
3673 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003674 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003675 ASSERT_COMPARE( output_data->x, output_data->len,
3676 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003677
3678exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003679 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003680 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01003681 mbedtls_psa_crypto_free( );
3682}
3683/* END_CASE */
3684
3685/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003686void sign_fail( int key_type_arg, data_t *key_data,
3687 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003688 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003689{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003690 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003691 psa_key_type_t key_type = key_type_arg;
3692 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003693 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003694 psa_status_t actual_status;
3695 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003696 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003697 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003698 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003699
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003700 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003701
Gilles Peskine8817f612018-12-18 00:18:46 +01003702 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003703
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003704 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003705 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003706 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003707
Gilles Peskine8817f612018-12-18 00:18:46 +01003708 PSA_ASSERT( psa_import_key( handle, key_type,
3709 key_data->x,
3710 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003711
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003712 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003713 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003714 signature, signature_size,
3715 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003716 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003717 /* The value of *signature_length is unspecified on error, but
3718 * whatever it is, it should be less than signature_size, so that
3719 * if the caller tries to read *signature_length bytes without
3720 * checking the error code then they don't overflow a buffer. */
3721 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003722
3723exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003724 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003725 mbedtls_free( signature );
3726 mbedtls_psa_crypto_free( );
3727}
3728/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003729
3730/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003731void sign_verify( int key_type_arg, data_t *key_data,
3732 int alg_arg, data_t *input_data )
3733{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003734 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003735 psa_key_type_t key_type = key_type_arg;
3736 psa_algorithm_t alg = alg_arg;
3737 size_t key_bits;
3738 unsigned char *signature = NULL;
3739 size_t signature_size;
3740 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003741 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003742
Gilles Peskine8817f612018-12-18 00:18:46 +01003743 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003744
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003745 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003746 psa_key_policy_set_usage( &policy,
3747 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
3748 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003749 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003750
Gilles Peskine8817f612018-12-18 00:18:46 +01003751 PSA_ASSERT( psa_import_key( handle, key_type,
3752 key_data->x,
3753 key_data->len ) );
3754 PSA_ASSERT( psa_get_key_information( handle,
3755 NULL,
3756 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003757
3758 /* Allocate a buffer which has the size advertized by the
3759 * library. */
3760 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3761 key_bits, alg );
3762 TEST_ASSERT( signature_size != 0 );
3763 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003764 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003765
3766 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003767 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3768 input_data->x, input_data->len,
3769 signature, signature_size,
3770 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003771 /* Check that the signature length looks sensible. */
3772 TEST_ASSERT( signature_length <= signature_size );
3773 TEST_ASSERT( signature_length > 0 );
3774
3775 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003776 PSA_ASSERT( psa_asymmetric_verify(
3777 handle, alg,
3778 input_data->x, input_data->len,
3779 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003780
3781 if( input_data->len != 0 )
3782 {
3783 /* Flip a bit in the input and verify that the signature is now
3784 * detected as invalid. Flip a bit at the beginning, not at the end,
3785 * because ECDSA may ignore the last few bits of the input. */
3786 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003787 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3788 input_data->x, input_data->len,
3789 signature, signature_length ),
3790 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003791 }
3792
3793exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003794 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003795 mbedtls_free( signature );
3796 mbedtls_psa_crypto_free( );
3797}
3798/* END_CASE */
3799
3800/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003801void asymmetric_verify( int key_type_arg, data_t *key_data,
3802 int alg_arg, data_t *hash_data,
3803 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003804{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003805 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003806 psa_key_type_t key_type = key_type_arg;
3807 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003808 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003809
Gilles Peskine69c12672018-06-28 00:07:19 +02003810 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3811
Gilles Peskine8817f612018-12-18 00:18:46 +01003812 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003813
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003814 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003815 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003816 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03003817
Gilles Peskine8817f612018-12-18 00:18:46 +01003818 PSA_ASSERT( psa_import_key( handle, key_type,
3819 key_data->x,
3820 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003821
Gilles Peskine8817f612018-12-18 00:18:46 +01003822 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3823 hash_data->x, hash_data->len,
3824 signature_data->x,
3825 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003826exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003827 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003828 mbedtls_psa_crypto_free( );
3829}
3830/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003831
3832/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003833void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3834 int alg_arg, data_t *hash_data,
3835 data_t *signature_data,
3836 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003837{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003838 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003839 psa_key_type_t key_type = key_type_arg;
3840 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003841 psa_status_t actual_status;
3842 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003843 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003844
Gilles Peskine8817f612018-12-18 00:18:46 +01003845 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003846
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003847 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003848 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003849 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003850
Gilles Peskine8817f612018-12-18 00:18:46 +01003851 PSA_ASSERT( psa_import_key( handle, key_type,
3852 key_data->x,
3853 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003854
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003855 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003856 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003857 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003858 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003859
Gilles Peskinefe11b722018-12-18 00:24:04 +01003860 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003861
3862exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003863 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003864 mbedtls_psa_crypto_free( );
3865}
3866/* END_CASE */
3867
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003868/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003869void asymmetric_encrypt( int key_type_arg,
3870 data_t *key_data,
3871 int alg_arg,
3872 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003873 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003874 int expected_output_length_arg,
3875 int expected_status_arg )
3876{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003877 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003878 psa_key_type_t key_type = key_type_arg;
3879 psa_algorithm_t alg = alg_arg;
3880 size_t expected_output_length = expected_output_length_arg;
3881 size_t key_bits;
3882 unsigned char *output = NULL;
3883 size_t output_size;
3884 size_t output_length = ~0;
3885 psa_status_t actual_status;
3886 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003887 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003888
Gilles Peskine8817f612018-12-18 00:18:46 +01003889 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003890
Gilles Peskine656896e2018-06-29 19:12:28 +02003891 /* Import the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003892 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003893 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003894 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3895 PSA_ASSERT( psa_import_key( handle, key_type,
3896 key_data->x,
3897 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003898
3899 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003900 PSA_ASSERT( psa_get_key_information( handle,
3901 NULL,
3902 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003903 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003904 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003905
3906 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003907 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003908 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003909 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003910 output, output_size,
3911 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003912 TEST_EQUAL( actual_status, expected_status );
3913 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003914
Gilles Peskine68428122018-06-30 18:42:41 +02003915 /* If the label is empty, the test framework puts a non-null pointer
3916 * in label->x. Test that a null pointer works as well. */
3917 if( label->len == 0 )
3918 {
3919 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003920 if( output_size != 0 )
3921 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003922 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003923 input_data->x, input_data->len,
3924 NULL, label->len,
3925 output, output_size,
3926 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003927 TEST_EQUAL( actual_status, expected_status );
3928 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003929 }
3930
Gilles Peskine656896e2018-06-29 19:12:28 +02003931exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003932 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003933 mbedtls_free( output );
3934 mbedtls_psa_crypto_free( );
3935}
3936/* END_CASE */
3937
3938/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003939void asymmetric_encrypt_decrypt( int key_type_arg,
3940 data_t *key_data,
3941 int alg_arg,
3942 data_t *input_data,
3943 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003944{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003945 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003946 psa_key_type_t key_type = key_type_arg;
3947 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003948 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003949 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003950 size_t output_size;
3951 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003952 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003953 size_t output2_size;
3954 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003955 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003956
Gilles Peskine8817f612018-12-18 00:18:46 +01003957 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003958
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003959 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003960 psa_key_policy_set_usage( &policy,
3961 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003962 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003963 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003964
Gilles Peskine8817f612018-12-18 00:18:46 +01003965 PSA_ASSERT( psa_import_key( handle, key_type,
3966 key_data->x,
3967 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003968
3969 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003970 PSA_ASSERT( psa_get_key_information( handle,
3971 NULL,
3972 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003973 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003974 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003975 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003976 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003977
Gilles Peskineeebd7382018-06-08 18:11:54 +02003978 /* We test encryption by checking that encrypt-then-decrypt gives back
3979 * the original plaintext because of the non-optional random
3980 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003981 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3982 input_data->x, input_data->len,
3983 label->x, label->len,
3984 output, output_size,
3985 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003986 /* We don't know what ciphertext length to expect, but check that
3987 * it looks sensible. */
3988 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003989
Gilles Peskine8817f612018-12-18 00:18:46 +01003990 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3991 output, output_length,
3992 label->x, label->len,
3993 output2, output2_size,
3994 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003995 ASSERT_COMPARE( input_data->x, input_data->len,
3996 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003997
3998exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003999 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004000 mbedtls_free( output );
4001 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004002 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004003}
4004/* END_CASE */
4005
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004006/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004007void asymmetric_decrypt( int key_type_arg,
4008 data_t *key_data,
4009 int alg_arg,
4010 data_t *input_data,
4011 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004012 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004013{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004014 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004015 psa_key_type_t key_type = key_type_arg;
4016 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004017 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004018 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004019 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00004020 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004021
Jaeden Amero412654a2019-02-06 12:57:46 +00004022 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004023 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004024
Gilles Peskine8817f612018-12-18 00:18:46 +01004025 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004026
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004027 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02004028 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004029 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004030
Gilles Peskine8817f612018-12-18 00:18:46 +01004031 PSA_ASSERT( psa_import_key( handle, key_type,
4032 key_data->x,
4033 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004034
Gilles Peskine8817f612018-12-18 00:18:46 +01004035 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4036 input_data->x, input_data->len,
4037 label->x, label->len,
4038 output,
4039 output_size,
4040 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004041 ASSERT_COMPARE( expected_data->x, expected_data->len,
4042 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004043
Gilles Peskine68428122018-06-30 18:42:41 +02004044 /* If the label is empty, the test framework puts a non-null pointer
4045 * in label->x. Test that a null pointer works as well. */
4046 if( label->len == 0 )
4047 {
4048 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004049 if( output_size != 0 )
4050 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004051 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4052 input_data->x, input_data->len,
4053 NULL, label->len,
4054 output,
4055 output_size,
4056 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004057 ASSERT_COMPARE( expected_data->x, expected_data->len,
4058 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004059 }
4060
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004061exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004062 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004063 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004064 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004065}
4066/* END_CASE */
4067
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004068/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004069void asymmetric_decrypt_fail( int key_type_arg,
4070 data_t *key_data,
4071 int alg_arg,
4072 data_t *input_data,
4073 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004074 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004075 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004076{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004077 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004078 psa_key_type_t key_type = key_type_arg;
4079 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004080 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004081 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004082 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004083 psa_status_t actual_status;
4084 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004085 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004086
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004087 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004088
Gilles Peskine8817f612018-12-18 00:18:46 +01004089 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004090
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004091 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02004092 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004093 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004094
Gilles Peskine8817f612018-12-18 00:18:46 +01004095 PSA_ASSERT( psa_import_key( handle, key_type,
4096 key_data->x,
4097 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004098
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004099 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004100 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004101 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004102 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004103 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004104 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004105 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004106
Gilles Peskine68428122018-06-30 18:42:41 +02004107 /* If the label is empty, the test framework puts a non-null pointer
4108 * in label->x. Test that a null pointer works as well. */
4109 if( label->len == 0 )
4110 {
4111 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004112 if( output_size != 0 )
4113 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004114 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004115 input_data->x, input_data->len,
4116 NULL, label->len,
4117 output, output_size,
4118 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004119 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004120 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004121 }
4122
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004123exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004124 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004125 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004126 mbedtls_psa_crypto_free( );
4127}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004128/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004129
4130/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00004131void crypto_generator_init( )
4132{
4133 /* Test each valid way of initializing the object, except for `= {0}`, as
4134 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4135 * though it's OK by the C standard. We could test for this, but we'd need
4136 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004137 size_t capacity;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004138 psa_crypto_generator_t func = psa_crypto_generator_init( );
4139 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
4140 psa_crypto_generator_t zero;
4141
4142 memset( &zero, 0, sizeof( zero ) );
4143
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004144 /* A default generator should not be able to report its capacity. */
4145 TEST_EQUAL( psa_get_generator_capacity( &func, &capacity ),
4146 PSA_ERROR_BAD_STATE );
4147 TEST_EQUAL( psa_get_generator_capacity( &init, &capacity ),
4148 PSA_ERROR_BAD_STATE );
4149 TEST_EQUAL( psa_get_generator_capacity( &zero, &capacity ),
4150 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004151
4152 /* A default generator should be abortable without error. */
4153 PSA_ASSERT( psa_generator_abort(&func) );
4154 PSA_ASSERT( psa_generator_abort(&init) );
4155 PSA_ASSERT( psa_generator_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004156}
4157/* END_CASE */
4158
4159/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004160void derive_setup( int key_type_arg,
4161 data_t *key_data,
4162 int alg_arg,
4163 data_t *salt,
4164 data_t *label,
4165 int requested_capacity_arg,
4166 int expected_status_arg )
4167{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004168 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004169 size_t key_type = key_type_arg;
4170 psa_algorithm_t alg = alg_arg;
4171 size_t requested_capacity = requested_capacity_arg;
4172 psa_status_t expected_status = expected_status_arg;
4173 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004174 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004175
Gilles Peskine8817f612018-12-18 00:18:46 +01004176 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004177
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004178 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004179 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004180 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004181
Gilles Peskine8817f612018-12-18 00:18:46 +01004182 PSA_ASSERT( psa_import_key( handle, key_type,
4183 key_data->x,
4184 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004185
Gilles Peskinefe11b722018-12-18 00:24:04 +01004186 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4187 salt->x, salt->len,
4188 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004189 requested_capacity ),
4190 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004191
4192exit:
4193 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004194 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004195 mbedtls_psa_crypto_free( );
4196}
4197/* END_CASE */
4198
4199/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004200void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004201{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004202 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004203 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004204 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004205 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004206 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004207 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004208 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4209 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4210 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00004211 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004212
Gilles Peskine8817f612018-12-18 00:18:46 +01004213 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004214
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004215 PSA_ASSERT( psa_allocate_key( &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004216 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004217 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004218
Gilles Peskine8817f612018-12-18 00:18:46 +01004219 PSA_ASSERT( psa_import_key( handle, key_type,
4220 key_data,
4221 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004222
4223 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01004224 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4225 NULL, 0,
4226 NULL, 0,
4227 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004228
4229 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01004230 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4231 NULL, 0,
4232 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004233 capacity ),
4234 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004235
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004236 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004237
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004238 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004239 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004240
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004241exit:
4242 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004243 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004244 mbedtls_psa_crypto_free( );
4245}
4246/* END_CASE */
4247
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004248/* BEGIN_CASE */
4249void test_derive_invalid_generator_tests( )
4250{
4251 uint8_t output_buffer[16];
4252 size_t buffer_size = 16;
4253 size_t capacity = 0;
4254 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4255
Nir Sonnenschein50789302018-10-31 12:16:38 +02004256 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004257 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004258
4259 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004260 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004261
Gilles Peskine8817f612018-12-18 00:18:46 +01004262 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004263
Nir Sonnenschein50789302018-10-31 12:16:38 +02004264 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004265 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004266
Nir Sonnenschein50789302018-10-31 12:16:38 +02004267 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004268 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004269
4270exit:
4271 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004272}
4273/* END_CASE */
4274
4275/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004276void derive_output( int alg_arg,
4277 data_t *key_data,
4278 data_t *salt,
4279 data_t *label,
4280 int requested_capacity_arg,
4281 data_t *expected_output1,
4282 data_t *expected_output2 )
4283{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004284 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004285 psa_algorithm_t alg = alg_arg;
4286 size_t requested_capacity = requested_capacity_arg;
4287 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4288 uint8_t *expected_outputs[2] =
4289 {expected_output1->x, expected_output2->x};
4290 size_t output_sizes[2] =
4291 {expected_output1->len, expected_output2->len};
4292 size_t output_buffer_size = 0;
4293 uint8_t *output_buffer = NULL;
4294 size_t expected_capacity;
4295 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00004296 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004297 psa_status_t status;
4298 unsigned i;
4299
4300 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4301 {
4302 if( output_sizes[i] > output_buffer_size )
4303 output_buffer_size = output_sizes[i];
4304 if( output_sizes[i] == 0 )
4305 expected_outputs[i] = NULL;
4306 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004307 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004308 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004309
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004310 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004311 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004312 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004313
Gilles Peskine8817f612018-12-18 00:18:46 +01004314 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
4315 key_data->x,
4316 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004317
4318 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004319 if( PSA_ALG_IS_HKDF( alg ) )
4320 {
4321 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4322 PSA_ASSERT( psa_set_generator_capacity( &generator,
4323 requested_capacity ) );
4324 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4325 PSA_KDF_STEP_SALT,
4326 salt->x, salt->len ) );
4327 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4328 PSA_KDF_STEP_SECRET,
4329 handle ) );
4330 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4331 PSA_KDF_STEP_INFO,
4332 label->x, label->len ) );
4333 }
4334 else
4335 {
4336 // legacy
4337 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4338 salt->x, salt->len,
4339 label->x, label->len,
4340 requested_capacity ) );
4341 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004342 PSA_ASSERT( psa_get_generator_capacity( &generator,
4343 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004344 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004345 expected_capacity = requested_capacity;
4346
4347 /* Expansion phase. */
4348 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4349 {
4350 /* Read some bytes. */
4351 status = psa_generator_read( &generator,
4352 output_buffer, output_sizes[i] );
4353 if( expected_capacity == 0 && output_sizes[i] == 0 )
4354 {
4355 /* Reading 0 bytes when 0 bytes are available can go either way. */
4356 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004357 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004358 continue;
4359 }
4360 else if( expected_capacity == 0 ||
4361 output_sizes[i] > expected_capacity )
4362 {
4363 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004364 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004365 expected_capacity = 0;
4366 continue;
4367 }
4368 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004369 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004370 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004371 ASSERT_COMPARE( output_buffer, output_sizes[i],
4372 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004373 /* Check the generator status. */
4374 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01004375 PSA_ASSERT( psa_get_generator_capacity( &generator,
4376 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004377 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004378 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004379 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004380
4381exit:
4382 mbedtls_free( output_buffer );
4383 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004384 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004385 mbedtls_psa_crypto_free( );
4386}
4387/* END_CASE */
4388
4389/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004390void derive_full( int alg_arg,
4391 data_t *key_data,
4392 data_t *salt,
4393 data_t *label,
4394 int requested_capacity_arg )
4395{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004396 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004397 psa_algorithm_t alg = alg_arg;
4398 size_t requested_capacity = requested_capacity_arg;
4399 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4400 unsigned char output_buffer[16];
4401 size_t expected_capacity = requested_capacity;
4402 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00004403 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004404
Gilles Peskine8817f612018-12-18 00:18:46 +01004405 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004406
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004407 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004408 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004409 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004410
Gilles Peskine8817f612018-12-18 00:18:46 +01004411 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
4412 key_data->x,
4413 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004414
4415 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004416 if( PSA_ALG_IS_HKDF( alg ) )
4417 {
4418 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4419 PSA_ASSERT( psa_set_generator_capacity( &generator,
4420 requested_capacity ) );
4421 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4422 PSA_KDF_STEP_SALT,
4423 salt->x, salt->len ) );
4424 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4425 PSA_KDF_STEP_SECRET,
4426 handle ) );
4427 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4428 PSA_KDF_STEP_INFO,
4429 label->x, label->len ) );
4430 }
4431 else
4432 {
4433 // legacy
4434 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4435 salt->x, salt->len,
4436 label->x, label->len,
4437 requested_capacity ) );
4438 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004439 PSA_ASSERT( psa_get_generator_capacity( &generator,
4440 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004441 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004442
4443 /* Expansion phase. */
4444 while( current_capacity > 0 )
4445 {
4446 size_t read_size = sizeof( output_buffer );
4447 if( read_size > current_capacity )
4448 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01004449 PSA_ASSERT( psa_generator_read( &generator,
4450 output_buffer,
4451 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004452 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01004453 PSA_ASSERT( psa_get_generator_capacity( &generator,
4454 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004455 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004456 }
4457
4458 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004459 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004460 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004461
Gilles Peskine8817f612018-12-18 00:18:46 +01004462 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004463
4464exit:
4465 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004466 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02004467 mbedtls_psa_crypto_free( );
4468}
4469/* END_CASE */
4470
4471/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004472void derive_key_exercise( int alg_arg,
4473 data_t *key_data,
4474 data_t *salt,
4475 data_t *label,
4476 int derived_type_arg,
4477 int derived_bits_arg,
4478 int derived_usage_arg,
4479 int derived_alg_arg )
4480{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004481 psa_key_handle_t base_handle = 0;
4482 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004483 psa_algorithm_t alg = alg_arg;
4484 psa_key_type_t derived_type = derived_type_arg;
4485 size_t derived_bits = derived_bits_arg;
4486 psa_key_usage_t derived_usage = derived_usage_arg;
4487 psa_algorithm_t derived_alg = derived_alg_arg;
4488 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
4489 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004490 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004491 psa_key_type_t got_type;
4492 size_t got_bits;
4493
Gilles Peskine8817f612018-12-18 00:18:46 +01004494 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004495
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004496 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004497 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004498 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
4499 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
4500 key_data->x,
4501 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004502
4503 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004504 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4505 salt->x, salt->len,
4506 label->x, label->len,
4507 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004508 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004509 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004510 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4511 PSA_ASSERT( psa_generator_import_key( derived_handle,
4512 derived_type,
4513 derived_bits,
4514 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004515
4516 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01004517 PSA_ASSERT( psa_get_key_information( derived_handle,
4518 &got_type,
4519 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004520 TEST_EQUAL( got_type, derived_type );
4521 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004522
4523 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004524 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004525 goto exit;
4526
4527exit:
4528 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004529 psa_destroy_key( base_handle );
4530 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004531 mbedtls_psa_crypto_free( );
4532}
4533/* END_CASE */
4534
4535/* BEGIN_CASE */
4536void derive_key_export( int alg_arg,
4537 data_t *key_data,
4538 data_t *salt,
4539 data_t *label,
4540 int bytes1_arg,
4541 int bytes2_arg )
4542{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004543 psa_key_handle_t base_handle = 0;
4544 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004545 psa_algorithm_t alg = alg_arg;
4546 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004547 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004548 size_t bytes2 = bytes2_arg;
4549 size_t capacity = bytes1 + bytes2;
4550 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004551 uint8_t *output_buffer = NULL;
4552 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00004553 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004554 size_t length;
4555
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004556 ASSERT_ALLOC( output_buffer, capacity );
4557 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004558 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004559
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004560 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +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( base_handle, &policy ) );
4563 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
4564 key_data->x,
4565 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004566
4567 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004568 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4569 salt->x, salt->len,
4570 label->x, label->len,
4571 capacity ) );
4572 PSA_ASSERT( psa_generator_read( &generator,
4573 output_buffer,
4574 capacity ) );
4575 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004576
4577 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004578 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4579 salt->x, salt->len,
4580 label->x, label->len,
4581 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004582 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004583 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004584 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4585 PSA_ASSERT( psa_generator_import_key( derived_handle,
4586 PSA_KEY_TYPE_RAW_DATA,
4587 derived_bits,
4588 &generator ) );
4589 PSA_ASSERT( psa_export_key( derived_handle,
4590 export_buffer, bytes1,
4591 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004592 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004593 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004594 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004595 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4596 PSA_ASSERT( psa_generator_import_key( derived_handle,
4597 PSA_KEY_TYPE_RAW_DATA,
4598 PSA_BYTES_TO_BITS( bytes2 ),
4599 &generator ) );
4600 PSA_ASSERT( psa_export_key( derived_handle,
4601 export_buffer + bytes1, bytes2,
4602 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004603 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004604
4605 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004606 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4607 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004608
4609exit:
4610 mbedtls_free( output_buffer );
4611 mbedtls_free( export_buffer );
4612 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004613 psa_destroy_key( base_handle );
4614 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004615 mbedtls_psa_crypto_free( );
4616}
4617/* END_CASE */
4618
4619/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004620void key_agreement_setup( int alg_arg,
4621 int our_key_type_arg, data_t *our_key_data,
4622 data_t *peer_key_data,
4623 int expected_status_arg )
4624{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004625 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004626 psa_algorithm_t alg = alg_arg;
4627 psa_key_type_t our_key_type = our_key_type_arg;
4628 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004629 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004630
Gilles Peskine8817f612018-12-18 00:18:46 +01004631 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004632
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004633 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004634 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004635 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4636 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4637 our_key_data->x,
4638 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004639
Gilles Peskine969c5d62019-01-16 15:53:06 +01004640 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4641 TEST_EQUAL( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004642 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004643 peer_key_data->x, peer_key_data->len ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004644 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004645
4646exit:
4647 psa_generator_abort( &generator );
4648 psa_destroy_key( our_key );
4649 mbedtls_psa_crypto_free( );
4650}
4651/* END_CASE */
4652
4653/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004654void key_agreement_capacity( int alg_arg,
4655 int our_key_type_arg, data_t *our_key_data,
4656 data_t *peer_key_data,
4657 int expected_capacity_arg )
4658{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004659 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004660 psa_algorithm_t alg = alg_arg;
4661 psa_key_type_t our_key_type = our_key_type_arg;
4662 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004663 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004664 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004665 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004666
Gilles Peskine8817f612018-12-18 00:18:46 +01004667 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004668
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004669 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004670 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004671 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4672 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4673 our_key_data->x,
4674 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004675
Gilles Peskine969c5d62019-01-16 15:53:06 +01004676 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4677 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004678 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004679 peer_key_data->x, peer_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004680
Gilles Peskinebf491972018-10-25 22:36:12 +02004681 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004682 PSA_ASSERT( psa_get_generator_capacity(
4683 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004684 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004685
Gilles Peskinebf491972018-10-25 22:36:12 +02004686 /* Test the actual capacity by reading the output. */
4687 while( actual_capacity > sizeof( output ) )
4688 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004689 PSA_ASSERT( psa_generator_read( &generator,
4690 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004691 actual_capacity -= sizeof( output );
4692 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004693 PSA_ASSERT( psa_generator_read( &generator,
4694 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004695 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004696 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004697
Gilles Peskine59685592018-09-18 12:11:34 +02004698exit:
4699 psa_generator_abort( &generator );
4700 psa_destroy_key( our_key );
4701 mbedtls_psa_crypto_free( );
4702}
4703/* END_CASE */
4704
4705/* BEGIN_CASE */
4706void key_agreement_output( int alg_arg,
4707 int our_key_type_arg, data_t *our_key_data,
4708 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004709 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004710{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004711 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004712 psa_algorithm_t alg = alg_arg;
4713 psa_key_type_t our_key_type = our_key_type_arg;
4714 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004715 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004716 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004717
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004718 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4719 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004720
Gilles Peskine8817f612018-12-18 00:18:46 +01004721 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004722
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004723 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004724 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004725 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4726 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4727 our_key_data->x,
4728 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004729
Gilles Peskine969c5d62019-01-16 15:53:06 +01004730 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4731 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004732 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004733 peer_key_data->x, peer_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004734
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004735 PSA_ASSERT( psa_generator_read( &generator,
4736 actual_output,
4737 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004738 ASSERT_COMPARE( actual_output, expected_output1->len,
4739 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004740 if( expected_output2->len != 0 )
4741 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004742 PSA_ASSERT( psa_generator_read( &generator,
4743 actual_output,
4744 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004745 ASSERT_COMPARE( actual_output, expected_output2->len,
4746 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004747 }
Gilles Peskine59685592018-09-18 12:11:34 +02004748
4749exit:
4750 psa_generator_abort( &generator );
4751 psa_destroy_key( our_key );
4752 mbedtls_psa_crypto_free( );
4753 mbedtls_free( actual_output );
4754}
4755/* END_CASE */
4756
4757/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004758void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004759{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004760 size_t bytes = bytes_arg;
4761 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004762 unsigned char *output = NULL;
4763 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004764 size_t i;
4765 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004766
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004767 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4768 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004769 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004770
Gilles Peskine8817f612018-12-18 00:18:46 +01004771 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004772
Gilles Peskinea50d7392018-06-21 10:22:13 +02004773 /* Run several times, to ensure that every output byte will be
4774 * nonzero at least once with overwhelming probability
4775 * (2^(-8*number_of_runs)). */
4776 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004777 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004778 if( bytes != 0 )
4779 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004780 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004781
4782 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004783 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4784 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004785
4786 for( i = 0; i < bytes; i++ )
4787 {
4788 if( output[i] != 0 )
4789 ++changed[i];
4790 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004791 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004792
4793 /* Check that every byte was changed to nonzero at least once. This
4794 * validates that psa_generate_random is overwriting every byte of
4795 * the output buffer. */
4796 for( i = 0; i < bytes; i++ )
4797 {
4798 TEST_ASSERT( changed[i] != 0 );
4799 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004800
4801exit:
4802 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004803 mbedtls_free( output );
4804 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004805}
4806/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004807
4808/* BEGIN_CASE */
4809void generate_key( int type_arg,
4810 int bits_arg,
4811 int usage_arg,
4812 int alg_arg,
4813 int expected_status_arg )
4814{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004815 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004816 psa_key_type_t type = type_arg;
4817 psa_key_usage_t usage = usage_arg;
4818 size_t bits = bits_arg;
4819 psa_algorithm_t alg = alg_arg;
4820 psa_status_t expected_status = expected_status_arg;
4821 psa_key_type_t got_type;
4822 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004823 psa_status_t expected_info_status =
David Saadab4ecc272019-02-14 13:48:10 +02004824 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Jaeden Amero70261c52019-01-04 11:47:20 +00004825 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004826
Gilles Peskine8817f612018-12-18 00:18:46 +01004827 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004828
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004829 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004830 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004831 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004832
4833 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004834 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
4835 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004836
4837 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004838 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
4839 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004840 if( expected_info_status != PSA_SUCCESS )
4841 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004842 TEST_EQUAL( got_type, type );
4843 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004844
Gilles Peskine818ca122018-06-20 18:16:48 +02004845 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004846 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004847 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004848
4849exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004850 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004851 mbedtls_psa_crypto_free( );
4852}
4853/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004854
Darryl Greend49a4992018-06-18 17:27:26 +01004855/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4856void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4857 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004858 int alg_arg, int generation_method,
4859 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004860{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004861 psa_key_handle_t handle = 0;
4862 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004863 psa_key_type_t type = (psa_key_type_t) type_arg;
4864 psa_key_type_t type_get;
4865 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00004866 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
4867 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004868 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4869 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004870 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00004871 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4872 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004873 unsigned char *first_export = NULL;
4874 unsigned char *second_export = NULL;
4875 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4876 size_t first_exported_length;
4877 size_t second_exported_length;
4878
4879 ASSERT_ALLOC( first_export, export_size );
4880 ASSERT_ALLOC( second_export, export_size );
4881
Gilles Peskine8817f612018-12-18 00:18:46 +01004882 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004883
Gilles Peskine8817f612018-12-18 00:18:46 +01004884 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
Gilles Peskine8817f612018-12-18 00:18:46 +01004885 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004886 psa_key_policy_set_usage( &policy_set, policy_usage,
4887 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004888 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004889
Darryl Green0c6575a2018-11-07 16:05:30 +00004890 switch( generation_method )
4891 {
4892 case IMPORT_KEY:
4893 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004894 PSA_ASSERT( psa_import_key( handle, type,
4895 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004896 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004897
Darryl Green0c6575a2018-11-07 16:05:30 +00004898 case GENERATE_KEY:
4899 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004900 PSA_ASSERT( psa_generate_key( handle, type, bits,
4901 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004902 break;
4903
4904 case DERIVE_KEY:
4905 /* Create base key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004906 PSA_ASSERT( psa_allocate_key( &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004907 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4908 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004909 PSA_ASSERT( psa_set_key_policy(
4910 base_key, &base_policy_set ) );
4911 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4912 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004913 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004914 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4915 base_policy_alg,
4916 NULL, 0, NULL, 0,
4917 export_size ) );
4918 PSA_ASSERT( psa_generator_import_key(
4919 handle, PSA_KEY_TYPE_RAW_DATA,
4920 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004921 break;
4922 }
Darryl Greend49a4992018-06-18 17:27:26 +01004923
4924 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004925 TEST_EQUAL( psa_export_key( handle,
4926 first_export, export_size,
4927 &first_exported_length ),
4928 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004929
4930 /* Shutdown and restart */
4931 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004932 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004933
Darryl Greend49a4992018-06-18 17:27:26 +01004934 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004935 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4936 &handle ) );
4937 PSA_ASSERT( psa_get_key_information(
4938 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004939 TEST_EQUAL( type_get, type );
4940 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004941
Gilles Peskine8817f612018-12-18 00:18:46 +01004942 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004943 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4944 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004945
4946 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004947 TEST_EQUAL( psa_export_key( handle,
4948 second_export, export_size,
4949 &second_exported_length ),
4950 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004951
Darryl Green0c6575a2018-11-07 16:05:30 +00004952 if( export_status == PSA_SUCCESS )
4953 {
4954 ASSERT_COMPARE( first_export, first_exported_length,
4955 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004956
Darryl Green0c6575a2018-11-07 16:05:30 +00004957 switch( generation_method )
4958 {
4959 case IMPORT_KEY:
4960 ASSERT_COMPARE( data->x, data->len,
4961 first_export, first_exported_length );
4962 break;
4963 default:
4964 break;
4965 }
4966 }
4967
4968 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004969 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004970 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004971
4972exit:
4973 mbedtls_free( first_export );
4974 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004975 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004976 mbedtls_psa_crypto_free();
4977}
4978/* END_CASE */