blob: d70b7eb7f006b7143f9ddda999ff8e061e94b1fd [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 Peskinebdf309c2018-12-03 15:36:32 +0100600static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200601 psa_key_usage_t usage,
602 psa_algorithm_t alg )
603{
604 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200605 unsigned char output[1];
606 int ok = 0;
607
608 if( usage & PSA_KEY_USAGE_DERIVE )
609 {
610 /* We need two keys to exercise key agreement. Exercise the
611 * private key against its own public key. */
Gilles Peskine969c5d62019-01-16 15:53:06 +0100612 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
613 PSA_ASSERT( key_agreement_with_self( &generator, handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100614 PSA_ASSERT( psa_generator_read( &generator,
615 output,
616 sizeof( output ) ) );
617 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200618 }
619 ok = 1;
620
621exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200622 return( ok );
623}
624
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200625static int is_oid_of_key_type( psa_key_type_t type,
626 const uint8_t *oid, size_t oid_length )
627{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200628 const uint8_t *expected_oid = NULL;
629 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200630#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200631 if( PSA_KEY_TYPE_IS_RSA( type ) )
632 {
633 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
634 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
635 }
636 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200637#endif /* MBEDTLS_RSA_C */
638#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200639 if( PSA_KEY_TYPE_IS_ECC( type ) )
640 {
641 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
642 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
643 }
644 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200645#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200646 {
647 char message[40];
648 mbedtls_snprintf( message, sizeof( message ),
649 "OID not known for key type=0x%08lx",
650 (unsigned long) type );
651 test_fail( message, __LINE__, __FILE__ );
652 return( 0 );
653 }
654
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200655 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200656 return( 1 );
657
658exit:
659 return( 0 );
660}
661
662static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
663 size_t min_bits, size_t max_bits,
664 int must_be_odd )
665{
666 size_t len;
667 size_t actual_bits;
668 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100669 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100670 MBEDTLS_ASN1_INTEGER ),
671 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200672 /* Tolerate a slight departure from DER encoding:
673 * - 0 may be represented by an empty string or a 1-byte string.
674 * - The sign bit may be used as a value bit. */
675 if( ( len == 1 && ( *p )[0] == 0 ) ||
676 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
677 {
678 ++( *p );
679 --len;
680 }
681 if( min_bits == 0 && len == 0 )
682 return( 1 );
683 msb = ( *p )[0];
684 TEST_ASSERT( msb != 0 );
685 actual_bits = 8 * ( len - 1 );
686 while( msb != 0 )
687 {
688 msb >>= 1;
689 ++actual_bits;
690 }
691 TEST_ASSERT( actual_bits >= min_bits );
692 TEST_ASSERT( actual_bits <= max_bits );
693 if( must_be_odd )
694 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
695 *p += len;
696 return( 1 );
697exit:
698 return( 0 );
699}
700
701static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
702 size_t *len,
703 unsigned char n, unsigned char tag )
704{
705 int ret;
706 ret = mbedtls_asn1_get_tag( p, end, len,
707 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
708 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
709 if( ret != 0 )
710 return( ret );
711 end = *p + *len;
712 ret = mbedtls_asn1_get_tag( p, end, len, tag );
713 if( ret != 0 )
714 return( ret );
715 if( *p + *len != end )
716 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
717 return( 0 );
718}
719
720static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
721 uint8_t *exported, size_t exported_length )
722{
723 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100724 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200725 else
726 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200727
728#if defined(MBEDTLS_DES_C)
729 if( type == PSA_KEY_TYPE_DES )
730 {
731 /* Check the parity bits. */
732 unsigned i;
733 for( i = 0; i < bits / 8; i++ )
734 {
735 unsigned bit_count = 0;
736 unsigned m;
737 for( m = 1; m <= 0x100; m <<= 1 )
738 {
739 if( exported[i] & m )
740 ++bit_count;
741 }
742 TEST_ASSERT( bit_count % 2 != 0 );
743 }
744 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200745 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200746#endif
747
748#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
749 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
750 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200751 uint8_t *p = exported;
752 uint8_t *end = exported + exported_length;
753 size_t len;
754 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200755 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200756 * modulus INTEGER, -- n
757 * publicExponent INTEGER, -- e
758 * privateExponent INTEGER, -- d
759 * prime1 INTEGER, -- p
760 * prime2 INTEGER, -- q
761 * exponent1 INTEGER, -- d mod (p-1)
762 * exponent2 INTEGER, -- d mod (q-1)
763 * coefficient INTEGER, -- (inverse of q) mod p
764 * }
765 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100766 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
767 MBEDTLS_ASN1_SEQUENCE |
768 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
769 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200770 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
771 goto exit;
772 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
773 goto exit;
774 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
775 goto exit;
776 /* Require d to be at least half the size of n. */
777 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
778 goto exit;
779 /* Require p and q to be at most half the size of n, rounded up. */
780 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
781 goto exit;
782 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
783 goto exit;
784 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
785 goto exit;
786 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
787 goto exit;
788 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
789 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100790 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100791 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200792 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200793#endif /* MBEDTLS_RSA_C */
794
795#if defined(MBEDTLS_ECP_C)
796 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
797 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100798 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100799 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100800 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200801 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200802#endif /* MBEDTLS_ECP_C */
803
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200804 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
805 {
806 uint8_t *p = exported;
807 uint8_t *end = exported + exported_length;
808 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200809#if defined(MBEDTLS_RSA_C)
810 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
811 {
812 /* RSAPublicKey ::= SEQUENCE {
813 * modulus INTEGER, -- n
814 * publicExponent INTEGER } -- e
815 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100816 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
817 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100818 MBEDTLS_ASN1_CONSTRUCTED ),
819 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100820 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200821 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
822 goto exit;
823 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
824 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100825 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200826 }
827 else
828#endif /* MBEDTLS_RSA_C */
829#if defined(MBEDTLS_ECP_C)
830 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
831 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000832 /* The representation of an ECC public key is:
833 * - The byte 0x04;
834 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
835 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
836 * - where m is the bit size associated with the curve.
Jaeden Amero6b196002019-01-10 10:23:21 +0000837 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100838 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
839 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200840 }
841 else
842#endif /* MBEDTLS_ECP_C */
843 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100844 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200845 mbedtls_snprintf( message, sizeof( message ),
846 "No sanity check for public key type=0x%08lx",
847 (unsigned long) type );
848 test_fail( message, __LINE__, __FILE__ );
849 return( 0 );
850 }
851 }
852 else
853
854 {
855 /* No sanity checks for other types */
856 }
857
858 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200859
860exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200861 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200862}
863
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100864static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200865 psa_key_usage_t usage )
866{
867 psa_key_type_t type;
868 size_t bits;
869 uint8_t *exported = NULL;
870 size_t exported_size = 0;
871 size_t exported_length = 0;
872 int ok = 0;
873
Gilles Peskine8817f612018-12-18 00:18:46 +0100874 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200875
876 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
877 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200878 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100879 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
880 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200881 return( 1 );
882 }
883
Gilles Peskined14664a2018-08-10 19:07:32 +0200884 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200885 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200886
Gilles Peskine8817f612018-12-18 00:18:46 +0100887 PSA_ASSERT( psa_export_key( handle,
888 exported, exported_size,
889 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200890 ok = exported_key_sanity_check( type, bits, exported, exported_length );
891
892exit:
893 mbedtls_free( exported );
894 return( ok );
895}
896
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100897static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200898{
899 psa_key_type_t type;
900 psa_key_type_t public_type;
901 size_t bits;
902 uint8_t *exported = NULL;
903 size_t exported_size = 0;
904 size_t exported_length = 0;
905 int ok = 0;
906
Gilles Peskine8817f612018-12-18 00:18:46 +0100907 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200908 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
909 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100910 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100911 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200912 return( 1 );
913 }
914
915 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
916 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200917 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200918
Gilles Peskine8817f612018-12-18 00:18:46 +0100919 PSA_ASSERT( psa_export_public_key( handle,
920 exported, exported_size,
921 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200922 ok = exported_key_sanity_check( public_type, bits,
923 exported, exported_length );
924
925exit:
926 mbedtls_free( exported );
927 return( ok );
928}
929
Gilles Peskinec9516fb2019-02-05 20:32:06 +0100930/** Do smoke tests on a key.
931 *
932 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
933 * sign/verify, or derivation) that is permitted according to \p usage.
934 * \p usage and \p alg should correspond to the expected policy on the
935 * key.
936 *
937 * Export the key if permitted by \p usage, and check that the output
938 * looks sensible. If \p usage forbids export, check that
939 * \p psa_export_key correctly rejects the attempt. If the key is
940 * asymmetric, also check \p psa_export_public_key.
941 *
942 * If the key fails the tests, this function calls the test framework's
943 * `test_fail` function and returns false. Otherwise this function returns
944 * true. Therefore it should be used as follows:
945 * ```
946 * if( ! exercise_key( ... ) ) goto exit;
947 * ```
948 *
949 * \param handle The key to exercise. It should be capable of performing
950 * \p alg.
951 * \param usage The usage flags to assume.
952 * \param alg The algorithm to exercise.
953 *
954 * \retval 0 The key failed the smoke tests.
955 * \retval 1 The key passed the smoke tests.
956 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100957static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200958 psa_key_usage_t usage,
959 psa_algorithm_t alg )
960{
961 int ok;
962 if( alg == 0 )
963 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
964 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100965 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200966 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100967 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200968 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100969 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200970 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100971 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200972 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100973 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200974 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100975 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200976 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100977 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200978 else
979 {
980 char message[40];
981 mbedtls_snprintf( message, sizeof( message ),
982 "No code to exercise alg=0x%08lx",
983 (unsigned long) alg );
984 test_fail( message, __LINE__, __FILE__ );
985 ok = 0;
986 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200987
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100988 ok = ok && exercise_export_key( handle, usage );
989 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200990
Gilles Peskine02b75072018-07-01 22:31:34 +0200991 return( ok );
992}
993
Gilles Peskine10df3412018-10-25 22:35:43 +0200994static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
995 psa_algorithm_t alg )
996{
997 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
998 {
999 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1000 PSA_KEY_USAGE_VERIFY :
1001 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
1002 }
1003 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1004 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1005 {
1006 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1007 PSA_KEY_USAGE_ENCRYPT :
1008 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1009 }
1010 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1011 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1012 {
1013 return( PSA_KEY_USAGE_DERIVE );
1014 }
1015 else
1016 {
1017 return( 0 );
1018 }
1019
1020}
Darryl Green0c6575a2018-11-07 16:05:30 +00001021
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001022/* An overapproximation of the amount of storage needed for a key of the
1023 * given type and with the given content. The API doesn't make it easy
1024 * to find a good value for the size. The current implementation doesn't
1025 * care about the value anyway. */
1026#define KEY_BITS_FROM_DATA( type, data ) \
1027 ( data )->len
1028
Darryl Green0c6575a2018-11-07 16:05:30 +00001029typedef enum {
1030 IMPORT_KEY = 0,
1031 GENERATE_KEY = 1,
1032 DERIVE_KEY = 2
1033} generate_method;
1034
Gilles Peskinee59236f2018-01-27 23:32:46 +01001035/* END_HEADER */
1036
1037/* BEGIN_DEPENDENCIES
1038 * depends_on:MBEDTLS_PSA_CRYPTO_C
1039 * END_DEPENDENCIES
1040 */
1041
1042/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001043void static_checks( )
1044{
1045 size_t max_truncated_mac_size =
1046 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1047
1048 /* Check that the length for a truncated MAC always fits in the algorithm
1049 * encoding. The shifted mask is the maximum truncated value. The
1050 * untruncated algorithm may be one byte larger. */
1051 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
1052}
1053/* END_CASE */
1054
1055/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001056void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001057{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001058 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001059 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001060 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001061
Gilles Peskine8817f612018-12-18 00:18:46 +01001062 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001063
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001064 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001065 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001066 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001067 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001068 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001069
1070exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001071 mbedtls_psa_crypto_free( );
1072}
1073/* END_CASE */
1074
1075/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +01001076void import_twice( int alg_arg, int usage_arg,
1077 int type1_arg, data_t *data1,
1078 int expected_import1_status_arg,
1079 int type2_arg, data_t *data2,
1080 int expected_import2_status_arg )
1081{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001082 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +01001083 psa_algorithm_t alg = alg_arg;
1084 psa_key_usage_t usage = usage_arg;
1085 psa_key_type_t type1 = type1_arg;
1086 psa_status_t expected_import1_status = expected_import1_status_arg;
1087 psa_key_type_t type2 = type2_arg;
1088 psa_status_t expected_import2_status = expected_import2_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00001089 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea4261682018-12-03 11:34:01 +01001090 psa_status_t status;
1091
Gilles Peskine8817f612018-12-18 00:18:46 +01001092 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +01001093
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001094 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +01001095 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001096 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +01001097
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001098 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001099 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001100 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001101 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +01001102
1103 if( expected_import1_status == PSA_SUCCESS ||
1104 expected_import2_status == PSA_SUCCESS )
1105 {
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001106 if( ! exercise_key( handle, usage, alg ) )
1107 goto exit;
Gilles Peskinea4261682018-12-03 11:34:01 +01001108 }
1109
1110exit:
1111 mbedtls_psa_crypto_free( );
1112}
1113/* END_CASE */
1114
1115/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001116void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1117{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001118 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001119 size_t bits = bits_arg;
1120 psa_status_t expected_status = expected_status_arg;
1121 psa_status_t status;
1122 psa_key_type_t type =
1123 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
1124 size_t buffer_size = /* Slight overapproximations */
1125 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001126 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001127 unsigned char *p;
1128 int ret;
1129 size_t length;
1130
Gilles Peskine8817f612018-12-18 00:18:46 +01001131 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001132 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001133
1134 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1135 bits, keypair ) ) >= 0 );
1136 length = ret;
1137
1138 /* Try importing the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001139 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001140 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001141 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001142 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001143 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001144
1145exit:
1146 mbedtls_free( buffer );
1147 mbedtls_psa_crypto_free( );
1148}
1149/* END_CASE */
1150
1151/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001152void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001153 int type_arg,
1154 int alg_arg,
1155 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001156 int expected_bits,
1157 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001158 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001159 int canonical_input )
1160{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001161 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001162 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001163 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001164 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001165 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001166 unsigned char *exported = NULL;
1167 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001168 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001169 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001170 size_t reexported_length;
1171 psa_key_type_t got_type;
1172 size_t got_bits;
Jaeden Amero70261c52019-01-04 11:47:20 +00001173 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001174
Moran Pekercb088e72018-07-17 17:36:59 +03001175 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001176 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001177 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001178 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001179 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001180
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001181 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001182 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001183 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001184
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001185 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
David Saadab4ecc272019-02-14 13:48:10 +02001186 PSA_ERROR_DOES_NOT_EXIST );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001187
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001188 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001189 PSA_ASSERT( psa_import_key( handle, type,
1190 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001191
1192 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001193 PSA_ASSERT( psa_get_key_information( handle,
1194 &got_type,
1195 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001196 TEST_EQUAL( got_type, type );
1197 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001198
1199 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001200 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001201 exported, export_size,
1202 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001203 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001204
1205 /* The exported length must be set by psa_export_key() to a value between 0
1206 * and export_size. On errors, the exported length must be 0. */
1207 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1208 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1209 TEST_ASSERT( exported_length <= export_size );
1210
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001211 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001212 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001213 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001214 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001215 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001216 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001217 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001218
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001219 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001220 goto exit;
1221
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001222 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001223 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001224 else
1225 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001226 psa_key_handle_t handle2;
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001227 PSA_ASSERT( psa_allocate_key( &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001228 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001229
Gilles Peskine8817f612018-12-18 00:18:46 +01001230 PSA_ASSERT( psa_import_key( handle2, type,
1231 exported,
1232 exported_length ) );
1233 PSA_ASSERT( psa_export_key( handle2,
1234 reexported,
1235 export_size,
1236 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001237 ASSERT_COMPARE( exported, exported_length,
1238 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001239 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001240 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001241 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001242
1243destroy:
1244 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001245 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001246 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1247 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001248
1249exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001250 mbedtls_free( exported );
1251 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001252 mbedtls_psa_crypto_free( );
1253}
1254/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001255
Moran Pekerf709f4a2018-06-06 17:26:04 +03001256/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001257void import_key_nonempty_slot( )
1258{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001259 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001260 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1261 psa_status_t status;
1262 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001263 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001264
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001265 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001266
Moran Peker28a38e62018-11-07 16:18:24 +02001267 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001268 PSA_ASSERT( psa_import_key( handle, type,
1269 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001270
1271 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001272 status = psa_import_key( handle, type, data, sizeof( data ) );
David Saadab4ecc272019-02-14 13:48:10 +02001273 TEST_EQUAL( status, PSA_ERROR_ALREADY_EXISTS );
Moran Peker28a38e62018-11-07 16:18:24 +02001274
1275exit:
1276 mbedtls_psa_crypto_free( );
1277}
1278/* END_CASE */
1279
1280/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001281void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001282{
1283 psa_status_t status;
1284 unsigned char *exported = NULL;
1285 size_t export_size = 0;
1286 size_t exported_length = INVALID_EXPORT_LENGTH;
1287 psa_status_t expected_export_status = expected_export_status_arg;
1288
Gilles Peskine8817f612018-12-18 00:18:46 +01001289 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001290
1291 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001292 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001293 exported, export_size,
1294 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001295 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001296
1297exit:
1298 mbedtls_psa_crypto_free( );
1299}
1300/* END_CASE */
1301
1302/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001303void export_with_no_key_activity( )
1304{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001305 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001306 psa_algorithm_t alg = PSA_ALG_CTR;
1307 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001308 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001309 unsigned char *exported = NULL;
1310 size_t export_size = 0;
1311 size_t exported_length = INVALID_EXPORT_LENGTH;
1312
Gilles Peskine8817f612018-12-18 00:18:46 +01001313 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001314
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001315 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001316 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001317 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001318
1319 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001320 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001321 exported, export_size,
1322 &exported_length );
David Saadab4ecc272019-02-14 13:48:10 +02001323 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Peker34550092018-11-07 16:19:34 +02001324
1325exit:
1326 mbedtls_psa_crypto_free( );
1327}
1328/* END_CASE */
1329
1330/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001331void cipher_with_no_key_activity( )
1332{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001333 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001334 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001335 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001336 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001337 int exercise_alg = PSA_ALG_CTR;
1338
Gilles Peskine8817f612018-12-18 00:18:46 +01001339 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001340
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001341 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001342 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001343 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001344
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001345 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
David Saadab4ecc272019-02-14 13:48:10 +02001346 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Pekerce500072018-11-07 16:20:07 +02001347
1348exit:
1349 psa_cipher_abort( &operation );
1350 mbedtls_psa_crypto_free( );
1351}
1352/* END_CASE */
1353
1354/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001355void export_after_import_failure( data_t *data, int type_arg,
1356 int expected_import_status_arg )
1357{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001358 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001359 psa_key_type_t type = type_arg;
1360 psa_status_t status;
1361 unsigned char *exported = NULL;
1362 size_t export_size = 0;
1363 psa_status_t expected_import_status = expected_import_status_arg;
1364 size_t exported_length = INVALID_EXPORT_LENGTH;
1365
Gilles Peskine8817f612018-12-18 00:18:46 +01001366 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001367
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001368 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001369
Moran Peker34550092018-11-07 16:19:34 +02001370 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001371 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001372 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001373 TEST_EQUAL( status, expected_import_status );
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_after_import_failure( data_t *data, int type_arg,
1388 int expected_import_status_arg )
1389{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001390 psa_key_handle_t handle = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001391 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001392 psa_key_type_t type = type_arg;
1393 psa_status_t status;
1394 psa_status_t expected_import_status = expected_import_status_arg;
1395 int exercise_alg = PSA_ALG_CTR;
1396
Gilles Peskine8817f612018-12-18 00:18:46 +01001397 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001398
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001399 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001400
Moran Pekerce500072018-11-07 16:20:07 +02001401 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001402 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001403 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001404 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001405
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001406 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
David Saadab4ecc272019-02-14 13:48:10 +02001407 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Pekerce500072018-11-07 16:20:07 +02001408
1409exit:
1410 psa_cipher_abort( &operation );
1411 mbedtls_psa_crypto_free( );
1412}
1413/* END_CASE */
1414
1415/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001416void export_after_destroy_key( data_t *data, int type_arg )
1417{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001418 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001419 psa_key_type_t type = type_arg;
1420 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001421 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001422 psa_algorithm_t alg = PSA_ALG_CTR;
1423 unsigned char *exported = NULL;
1424 size_t export_size = 0;
1425 size_t exported_length = INVALID_EXPORT_LENGTH;
1426
Gilles Peskine8817f612018-12-18 00:18:46 +01001427 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001428
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001429 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001430 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001431 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001432 export_size = (ptrdiff_t) data->len;
1433 ASSERT_ALLOC( exported, export_size );
1434
1435 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001436 PSA_ASSERT( psa_import_key( handle, type,
1437 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001438
Gilles Peskine8817f612018-12-18 00:18:46 +01001439 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1440 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001441
1442 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001443 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001444
1445 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001446 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001447 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001448 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001449
1450exit:
1451 mbedtls_free( exported );
1452 mbedtls_psa_crypto_free( );
1453}
1454/* END_CASE */
1455
1456/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001457void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001458 int type_arg,
1459 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001460 int export_size_delta,
1461 int expected_export_status_arg,
1462 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001463{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001464 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001465 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001466 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001467 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001468 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001469 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001470 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001471 size_t exported_length = INVALID_EXPORT_LENGTH;
Jaeden Amero70261c52019-01-04 11:47:20 +00001472 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001473
Gilles Peskine8817f612018-12-18 00:18:46 +01001474 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001475
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001476 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001477 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001478 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001479
1480 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001481 PSA_ASSERT( psa_import_key( handle, type,
1482 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001483
Gilles Peskine49c25912018-10-29 15:15:31 +01001484 /* Export the public key */
1485 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001486 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001487 exported, export_size,
1488 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001489 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001490 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001491 {
1492 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1493 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001494 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001495 TEST_ASSERT( expected_public_key->len <=
1496 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001497 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1498 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001499 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001500
1501exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001502 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001503 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001504 mbedtls_psa_crypto_free( );
1505}
1506/* END_CASE */
1507
Gilles Peskine20035e32018-02-03 22:44:14 +01001508/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001509void import_and_exercise_key( data_t *data,
1510 int type_arg,
1511 int bits_arg,
1512 int alg_arg )
1513{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001514 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001515 psa_key_type_t type = type_arg;
1516 size_t bits = bits_arg;
1517 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001518 psa_key_usage_t usage = usage_to_exercise( type, alg );
Jaeden Amero70261c52019-01-04 11:47:20 +00001519 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001520 psa_key_type_t got_type;
1521 size_t got_bits;
1522 psa_status_t status;
1523
Gilles Peskine8817f612018-12-18 00:18:46 +01001524 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001525
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001526 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001527 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001528 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001529
1530 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001531 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001532 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001533
1534 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001535 PSA_ASSERT( psa_get_key_information( handle,
1536 &got_type,
1537 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001538 TEST_EQUAL( got_type, type );
1539 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001540
1541 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001542 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001543 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001544
1545exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001546 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001547 mbedtls_psa_crypto_free( );
1548}
1549/* END_CASE */
1550
1551/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001552void key_policy( int usage_arg, int alg_arg )
1553{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001554 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001555 psa_algorithm_t alg = alg_arg;
1556 psa_key_usage_t usage = usage_arg;
1557 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1558 unsigned char key[32] = {0};
Jaeden Amero70261c52019-01-04 11:47:20 +00001559 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
1560 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001561
1562 memset( key, 0x2a, sizeof( key ) );
1563
Gilles Peskine8817f612018-12-18 00:18:46 +01001564 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001565
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001566 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001567 psa_key_policy_set_usage( &policy_set, usage, alg );
1568
Gilles Peskinefe11b722018-12-18 00:24:04 +01001569 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1570 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001571 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001572
Gilles Peskine8817f612018-12-18 00:18:46 +01001573 PSA_ASSERT( psa_import_key( handle, key_type,
1574 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001575
Gilles Peskine8817f612018-12-18 00:18:46 +01001576 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001577
Gilles Peskinefe11b722018-12-18 00:24:04 +01001578 TEST_EQUAL( policy_get.usage, policy_set.usage );
1579 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001580
1581exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001582 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001583 mbedtls_psa_crypto_free( );
1584}
1585/* END_CASE */
1586
1587/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001588void key_policy_init( )
1589{
1590 /* Test each valid way of initializing the object, except for `= {0}`, as
1591 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1592 * though it's OK by the C standard. We could test for this, but we'd need
1593 * to supress the Clang warning for the test. */
1594 psa_key_policy_t func = psa_key_policy_init( );
1595 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1596 psa_key_policy_t zero;
1597
1598 memset( &zero, 0, sizeof( zero ) );
1599
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001600 /* A default key policy should not permit any usage. */
1601 TEST_EQUAL( psa_key_policy_get_usage( &func ), 0 );
1602 TEST_EQUAL( psa_key_policy_get_usage( &init ), 0 );
1603 TEST_EQUAL( psa_key_policy_get_usage( &zero ), 0 );
1604
1605 /* A default key policy should not permit any algorithm. */
1606 TEST_EQUAL( psa_key_policy_get_algorithm( &func ), 0 );
1607 TEST_EQUAL( psa_key_policy_get_algorithm( &init ), 0 );
1608 TEST_EQUAL( psa_key_policy_get_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001609}
1610/* END_CASE */
1611
1612/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001613void mac_key_policy( int policy_usage,
1614 int policy_alg,
1615 int key_type,
1616 data_t *key_data,
1617 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001618{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001619 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001620 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001621 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001622 psa_status_t status;
1623 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001624
Gilles Peskine8817f612018-12-18 00:18:46 +01001625 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001626
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001627 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001628 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001629 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001630
Gilles Peskine8817f612018-12-18 00:18:46 +01001631 PSA_ASSERT( psa_import_key( handle, key_type,
1632 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001633
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001634 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001635 if( policy_alg == exercise_alg &&
1636 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001637 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001638 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001639 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001640 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001641
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001642 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001643 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001644 if( policy_alg == exercise_alg &&
1645 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001646 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001647 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001648 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001649
1650exit:
1651 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001652 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001653 mbedtls_psa_crypto_free( );
1654}
1655/* END_CASE */
1656
1657/* BEGIN_CASE */
1658void cipher_key_policy( int policy_usage,
1659 int policy_alg,
1660 int key_type,
1661 data_t *key_data,
1662 int exercise_alg )
1663{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001664 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001665 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001666 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001667 psa_status_t status;
1668
Gilles Peskine8817f612018-12-18 00:18:46 +01001669 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001670
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001671 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001672 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001673 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001674
Gilles Peskine8817f612018-12-18 00:18:46 +01001675 PSA_ASSERT( psa_import_key( handle, key_type,
1676 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001677
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001678 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001679 if( policy_alg == exercise_alg &&
1680 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001681 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001682 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001683 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001684 psa_cipher_abort( &operation );
1685
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001686 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001687 if( policy_alg == exercise_alg &&
1688 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001689 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001690 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001691 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001692
1693exit:
1694 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001695 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001696 mbedtls_psa_crypto_free( );
1697}
1698/* END_CASE */
1699
1700/* BEGIN_CASE */
1701void aead_key_policy( int policy_usage,
1702 int policy_alg,
1703 int key_type,
1704 data_t *key_data,
1705 int nonce_length_arg,
1706 int tag_length_arg,
1707 int exercise_alg )
1708{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001709 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001710 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001711 psa_status_t status;
1712 unsigned char nonce[16] = {0};
1713 size_t nonce_length = nonce_length_arg;
1714 unsigned char tag[16];
1715 size_t tag_length = tag_length_arg;
1716 size_t output_length;
1717
1718 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1719 TEST_ASSERT( tag_length <= sizeof( tag ) );
1720
Gilles Peskine8817f612018-12-18 00:18:46 +01001721 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001722
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001723 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001724 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001725 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001726
Gilles Peskine8817f612018-12-18 00:18:46 +01001727 PSA_ASSERT( psa_import_key( handle, key_type,
1728 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001729
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001730 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001731 nonce, nonce_length,
1732 NULL, 0,
1733 NULL, 0,
1734 tag, tag_length,
1735 &output_length );
1736 if( policy_alg == exercise_alg &&
1737 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001738 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001739 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001740 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001741
1742 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001743 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001744 nonce, nonce_length,
1745 NULL, 0,
1746 tag, tag_length,
1747 NULL, 0,
1748 &output_length );
1749 if( policy_alg == exercise_alg &&
1750 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001751 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001752 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001753 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001754
1755exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001756 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001757 mbedtls_psa_crypto_free( );
1758}
1759/* END_CASE */
1760
1761/* BEGIN_CASE */
1762void asymmetric_encryption_key_policy( int policy_usage,
1763 int policy_alg,
1764 int key_type,
1765 data_t *key_data,
1766 int exercise_alg )
1767{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001768 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001769 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001770 psa_status_t status;
1771 size_t key_bits;
1772 size_t buffer_length;
1773 unsigned char *buffer = NULL;
1774 size_t output_length;
1775
Gilles Peskine8817f612018-12-18 00:18:46 +01001776 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001777
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001778 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001779 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001780 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001781
Gilles Peskine8817f612018-12-18 00:18:46 +01001782 PSA_ASSERT( psa_import_key( handle, key_type,
1783 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001784
Gilles Peskine8817f612018-12-18 00:18:46 +01001785 PSA_ASSERT( psa_get_key_information( handle,
1786 NULL,
1787 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001788 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1789 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001790 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001791
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001792 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001793 NULL, 0,
1794 NULL, 0,
1795 buffer, buffer_length,
1796 &output_length );
1797 if( policy_alg == exercise_alg &&
1798 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001799 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001800 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001801 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001802
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001803 if( buffer_length != 0 )
1804 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001805 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001806 buffer, buffer_length,
1807 NULL, 0,
1808 buffer, buffer_length,
1809 &output_length );
1810 if( policy_alg == exercise_alg &&
1811 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001812 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001813 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001814 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001815
1816exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001817 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001818 mbedtls_psa_crypto_free( );
1819 mbedtls_free( buffer );
1820}
1821/* END_CASE */
1822
1823/* BEGIN_CASE */
1824void asymmetric_signature_key_policy( int policy_usage,
1825 int policy_alg,
1826 int key_type,
1827 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001828 int exercise_alg,
1829 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001830{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001831 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001832 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001833 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001834 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1835 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1836 * compatible with the policy and `payload_length_arg` is supposed to be
1837 * a valid input length to sign. If `payload_length_arg <= 0`,
1838 * `exercise_alg` is supposed to be forbidden by the policy. */
1839 int compatible_alg = payload_length_arg > 0;
1840 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001841 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1842 size_t signature_length;
1843
Gilles Peskine8817f612018-12-18 00:18:46 +01001844 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001845
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001846 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001847 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001848 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001849
Gilles Peskine8817f612018-12-18 00:18:46 +01001850 PSA_ASSERT( psa_import_key( handle, key_type,
1851 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001852
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001853 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001854 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001855 signature, sizeof( signature ),
1856 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001857 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001858 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001859 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001860 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001861
1862 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001863 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001864 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001865 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001866 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001867 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001868 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001869 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001870
1871exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001872 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001873 mbedtls_psa_crypto_free( );
1874}
1875/* END_CASE */
1876
1877/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001878void derive_key_policy( int policy_usage,
1879 int policy_alg,
1880 int key_type,
1881 data_t *key_data,
1882 int exercise_alg )
1883{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001884 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001885 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001886 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1887 psa_status_t status;
1888
Gilles Peskine8817f612018-12-18 00:18:46 +01001889 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001890
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001891 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001892 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001893 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001894
Gilles Peskine8817f612018-12-18 00:18:46 +01001895 PSA_ASSERT( psa_import_key( handle, key_type,
1896 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001897
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001898 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001899 exercise_alg,
1900 NULL, 0,
1901 NULL, 0,
1902 1 );
1903 if( policy_alg == exercise_alg &&
1904 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001905 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001906 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001907 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001908
1909exit:
1910 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001911 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001912 mbedtls_psa_crypto_free( );
1913}
1914/* END_CASE */
1915
1916/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001917void agreement_key_policy( int policy_usage,
1918 int policy_alg,
1919 int key_type_arg,
1920 data_t *key_data,
1921 int exercise_alg )
1922{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001923 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001924 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001925 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001926 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1927 psa_status_t status;
1928
Gilles Peskine8817f612018-12-18 00:18:46 +01001929 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001930
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001931 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001932 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001933 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001934
Gilles Peskine8817f612018-12-18 00:18:46 +01001935 PSA_ASSERT( psa_import_key( handle, key_type,
1936 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001937
Gilles Peskine969c5d62019-01-16 15:53:06 +01001938 PSA_ASSERT( psa_key_derivation_setup( &generator, exercise_alg ) );
1939 status = key_agreement_with_self( &generator, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001940
Gilles Peskine01d718c2018-09-18 12:01:02 +02001941 if( policy_alg == exercise_alg &&
1942 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001943 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001944 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001945 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001946
1947exit:
1948 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001949 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001950 mbedtls_psa_crypto_free( );
1951}
1952/* END_CASE */
1953
1954/* BEGIN_CASE */
Gilles Peskine57ab7212019-01-28 13:03:09 +01001955void copy_key_policy( int source_usage_arg, int source_alg_arg,
1956 int type_arg, data_t *material,
1957 int target_usage_arg, int target_alg_arg,
1958 int constraint_usage_arg, int constraint_alg_arg,
1959 int expected_usage_arg, int expected_alg_arg )
1960{
1961 psa_key_usage_t source_usage = source_usage_arg;
1962 psa_algorithm_t source_alg = source_alg_arg;
1963 psa_key_handle_t source_handle = 0;
1964 psa_key_policy_t source_policy = PSA_KEY_POLICY_INIT;
1965 psa_key_type_t source_type = type_arg;
1966 size_t source_bits;
1967 psa_key_usage_t target_usage = target_usage_arg;
1968 psa_algorithm_t target_alg = target_alg_arg;
1969 psa_key_handle_t target_handle = 0;
1970 psa_key_policy_t target_policy = PSA_KEY_POLICY_INIT;
1971 psa_key_type_t target_type;
1972 size_t target_bits;
1973 psa_key_usage_t constraint_usage = constraint_usage_arg;
1974 psa_algorithm_t constraint_alg = constraint_alg_arg;
1975 psa_key_policy_t constraint = PSA_KEY_POLICY_INIT;
1976 psa_key_policy_t *p_constraint = NULL;
1977 psa_key_usage_t expected_usage = expected_usage_arg;
1978 psa_algorithm_t expected_alg = expected_alg_arg;
1979 uint8_t *export_buffer = NULL;
1980
1981 if( constraint_usage_arg != -1 )
1982 {
1983 p_constraint = &constraint;
1984 psa_key_policy_set_usage( p_constraint,
1985 constraint_usage, constraint_alg );
1986 }
1987
1988 PSA_ASSERT( psa_crypto_init( ) );
1989
1990 /* Populate the source slot. */
1991 PSA_ASSERT( psa_allocate_key( &source_handle ) );
1992 psa_key_policy_set_usage( &source_policy, source_usage, source_alg );
1993 PSA_ASSERT( psa_set_key_policy( source_handle, &source_policy ) );
1994 PSA_ASSERT( psa_import_key( source_handle, source_type,
1995 material->x, material->len ) );
1996 PSA_ASSERT( psa_get_key_information( source_handle, NULL, &source_bits ) );
1997
1998 /* Prepare the target slot. */
1999 PSA_ASSERT( psa_allocate_key( &target_handle ) );
2000 psa_key_policy_set_usage( &target_policy, target_usage, target_alg );
2001 PSA_ASSERT( psa_set_key_policy( target_handle, &target_policy ) );
2002 target_policy = psa_key_policy_init();
2003
2004 /* Copy the key. */
2005 PSA_ASSERT( psa_copy_key( source_handle, target_handle, p_constraint ) );
2006
2007 /* Destroy the source to ensure that this doesn't affect the target. */
2008 PSA_ASSERT( psa_destroy_key( source_handle ) );
2009
2010 /* Test that the target slot has the expected content and policy. */
2011 PSA_ASSERT( psa_get_key_information( target_handle,
2012 &target_type, &target_bits ) );
2013 TEST_EQUAL( source_type, target_type );
2014 TEST_EQUAL( source_bits, target_bits );
2015 PSA_ASSERT( psa_get_key_policy( target_handle, &target_policy ) );
2016 TEST_EQUAL( expected_usage, psa_key_policy_get_usage( &target_policy ) );
2017 TEST_EQUAL( expected_alg, psa_key_policy_get_algorithm( &target_policy ) );
2018 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2019 {
2020 size_t length;
2021 ASSERT_ALLOC( export_buffer, material->len );
2022 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2023 material->len, &length ) );
2024 ASSERT_COMPARE( material->x, material->len,
2025 export_buffer, length );
2026 }
2027 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2028 goto exit;
2029
2030 PSA_ASSERT( psa_close_key( target_handle ) );
2031
2032exit:
2033 mbedtls_psa_crypto_free( );
2034 mbedtls_free( export_buffer );
2035}
2036/* END_CASE */
2037
2038/* BEGIN_CASE */
2039void copy_fail( int source_usage_arg, int source_alg_arg,
2040 int type_arg, data_t *material,
2041 int target_usage_arg, int target_alg_arg,
2042 int constraint_usage_arg, int constraint_alg_arg,
2043 int expected_status_arg )
2044{
2045 /* Test copy failure into an empty slot. There is a test for copy failure
2046 * into an occupied slot in
2047 * test_suite_psa_crypto_slot_management.function. */
2048
2049 psa_key_usage_t source_usage = source_usage_arg;
2050 psa_algorithm_t source_alg = source_alg_arg;
2051 psa_key_handle_t source_handle = 0;
2052 psa_key_policy_t source_policy = PSA_KEY_POLICY_INIT;
2053 psa_key_type_t source_type = type_arg;
2054 size_t source_bits;
2055 psa_key_usage_t target_usage = target_usage_arg;
2056 psa_algorithm_t target_alg = target_alg_arg;
2057 psa_key_handle_t target_handle = 0;
2058 psa_key_policy_t target_policy = PSA_KEY_POLICY_INIT;
2059 psa_key_type_t target_type;
2060 size_t target_bits;
2061 psa_key_usage_t constraint_usage = constraint_usage_arg;
2062 psa_algorithm_t constraint_alg = constraint_alg_arg;
2063 psa_key_policy_t constraint = PSA_KEY_POLICY_INIT;
2064 psa_key_policy_t *p_constraint = NULL;
2065 psa_status_t expected_status = expected_status_arg;
2066
2067 if( constraint_usage_arg != -1 )
2068 {
2069 p_constraint = &constraint;
2070 psa_key_policy_set_usage( p_constraint,
2071 constraint_usage, constraint_alg );
2072 }
2073
2074 PSA_ASSERT( psa_crypto_init( ) );
2075
2076 /* Populate the source slot. */
2077 PSA_ASSERT( psa_allocate_key( &source_handle ) );
2078 psa_key_policy_set_usage( &source_policy, source_usage, source_alg );
2079 PSA_ASSERT( psa_set_key_policy( source_handle, &source_policy ) );
2080 PSA_ASSERT( psa_import_key( source_handle, source_type,
2081 material->x, material->len ) );
2082 PSA_ASSERT( psa_get_key_information( source_handle, NULL, &source_bits ) );
2083
2084 /* Prepare the target slot. */
2085 PSA_ASSERT( psa_allocate_key( &target_handle ) );
2086 psa_key_policy_set_usage( &target_policy, target_usage, target_alg );
2087 PSA_ASSERT( psa_set_key_policy( target_handle, &target_policy ) );
2088 target_policy = psa_key_policy_init();
2089
2090 /* Copy the key. */
2091 TEST_EQUAL( psa_copy_key( source_handle, target_handle, p_constraint ),
2092 expected_status );
2093
2094 /* Test that the target slot is unaffected. */
2095 TEST_EQUAL( psa_get_key_information( target_handle,
2096 &target_type, &target_bits ),
David Saadab4ecc272019-02-14 13:48:10 +02002097 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002098 PSA_ASSERT( psa_get_key_policy( target_handle, &target_policy ) );
2099 TEST_EQUAL( target_usage, psa_key_policy_get_usage( &target_policy ) );
2100 TEST_EQUAL( target_alg, psa_key_policy_get_algorithm( &target_policy ) );
2101
2102exit:
2103 mbedtls_psa_crypto_free( );
2104}
2105/* END_CASE */
2106
2107/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002108void hash_operation_init( )
2109{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002110 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002111 /* Test each valid way of initializing the object, except for `= {0}`, as
2112 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2113 * though it's OK by the C standard. We could test for this, but we'd need
2114 * to supress the Clang warning for the test. */
2115 psa_hash_operation_t func = psa_hash_operation_init( );
2116 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2117 psa_hash_operation_t zero;
2118
2119 memset( &zero, 0, sizeof( zero ) );
2120
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002121 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002122 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2123 PSA_ERROR_BAD_STATE );
2124 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2125 PSA_ERROR_BAD_STATE );
2126 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2127 PSA_ERROR_BAD_STATE );
2128
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002129 /* A default hash operation should be abortable without error. */
2130 PSA_ASSERT( psa_hash_abort( &func ) );
2131 PSA_ASSERT( psa_hash_abort( &init ) );
2132 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002133}
2134/* END_CASE */
2135
2136/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002137void hash_setup( int alg_arg,
2138 int expected_status_arg )
2139{
2140 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002141 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002142 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002143 psa_status_t status;
2144
Gilles Peskine8817f612018-12-18 00:18:46 +01002145 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002146
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002147 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002148 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002149
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002150 /* Whether setup succeeded or failed, abort must succeed. */
2151 PSA_ASSERT( psa_hash_abort( &operation ) );
2152
2153 /* If setup failed, reproduce the failure, so as to
2154 * test the resulting state of the operation object. */
2155 if( status != PSA_SUCCESS )
2156 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2157
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002158 /* Now the operation object should be reusable. */
2159#if defined(KNOWN_SUPPORTED_HASH_ALG)
2160 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2161 PSA_ASSERT( psa_hash_abort( &operation ) );
2162#endif
2163
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002164exit:
2165 mbedtls_psa_crypto_free( );
2166}
2167/* END_CASE */
2168
2169/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002170void hash_bad_order( )
2171{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002172 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002173 unsigned char input[] = "";
2174 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002175 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002176 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2177 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2178 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002179 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002180 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002181 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002182
Gilles Peskine8817f612018-12-18 00:18:46 +01002183 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002184
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002185 /* Call setup twice in a row. */
2186 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2187 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2188 PSA_ERROR_BAD_STATE );
2189 PSA_ASSERT( psa_hash_abort( &operation ) );
2190
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002191 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002192 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002193 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002194 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002195
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002196 /* Call update after finish. */
2197 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2198 PSA_ASSERT( psa_hash_finish( &operation,
2199 hash, sizeof( hash ), &hash_len ) );
2200 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002201 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002202 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002203
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002204 /* Call verify without calling setup beforehand. */
2205 TEST_EQUAL( psa_hash_verify( &operation,
2206 valid_hash, sizeof( valid_hash ) ),
2207 PSA_ERROR_BAD_STATE );
2208 PSA_ASSERT( psa_hash_abort( &operation ) );
2209
2210 /* Call verify after finish. */
2211 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2212 PSA_ASSERT( psa_hash_finish( &operation,
2213 hash, sizeof( hash ), &hash_len ) );
2214 TEST_EQUAL( psa_hash_verify( &operation,
2215 valid_hash, sizeof( valid_hash ) ),
2216 PSA_ERROR_BAD_STATE );
2217 PSA_ASSERT( psa_hash_abort( &operation ) );
2218
2219 /* Call verify twice in a row. */
2220 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2221 PSA_ASSERT( psa_hash_verify( &operation,
2222 valid_hash, sizeof( valid_hash ) ) );
2223 TEST_EQUAL( psa_hash_verify( &operation,
2224 valid_hash, sizeof( valid_hash ) ),
2225 PSA_ERROR_BAD_STATE );
2226 PSA_ASSERT( psa_hash_abort( &operation ) );
2227
2228 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002229 TEST_EQUAL( psa_hash_finish( &operation,
2230 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002231 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002232 PSA_ASSERT( psa_hash_abort( &operation ) );
2233
2234 /* Call finish twice in a row. */
2235 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2236 PSA_ASSERT( psa_hash_finish( &operation,
2237 hash, sizeof( hash ), &hash_len ) );
2238 TEST_EQUAL( psa_hash_finish( &operation,
2239 hash, sizeof( hash ), &hash_len ),
2240 PSA_ERROR_BAD_STATE );
2241 PSA_ASSERT( psa_hash_abort( &operation ) );
2242
2243 /* Call finish after calling verify. */
2244 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2245 PSA_ASSERT( psa_hash_verify( &operation,
2246 valid_hash, sizeof( valid_hash ) ) );
2247 TEST_EQUAL( psa_hash_finish( &operation,
2248 hash, sizeof( hash ), &hash_len ),
2249 PSA_ERROR_BAD_STATE );
2250 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002251
2252exit:
2253 mbedtls_psa_crypto_free( );
2254}
2255/* END_CASE */
2256
itayzafrir27e69452018-11-01 14:26:34 +02002257/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2258void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002259{
2260 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002261 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2262 * appended to it */
2263 unsigned char hash[] = {
2264 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2265 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2266 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002267 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002268 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002269
Gilles Peskine8817f612018-12-18 00:18:46 +01002270 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002271
itayzafrir27e69452018-11-01 14:26:34 +02002272 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002273 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002274 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002275 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002276
itayzafrir27e69452018-11-01 14:26:34 +02002277 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002278 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002279 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002280 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002281
itayzafrir27e69452018-11-01 14:26:34 +02002282 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002283 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002284 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002285 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002286
itayzafrirec93d302018-10-18 18:01:10 +03002287exit:
2288 mbedtls_psa_crypto_free( );
2289}
2290/* END_CASE */
2291
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002292/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2293void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002294{
2295 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002296 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002297 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002298 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002299 size_t hash_len;
2300
Gilles Peskine8817f612018-12-18 00:18:46 +01002301 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002302
itayzafrir58028322018-10-25 10:22:01 +03002303 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002304 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002305 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002306 hash, expected_size - 1, &hash_len ),
2307 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002308
2309exit:
2310 mbedtls_psa_crypto_free( );
2311}
2312/* END_CASE */
2313
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002314/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2315void hash_clone_source_state( )
2316{
2317 psa_algorithm_t alg = PSA_ALG_SHA_256;
2318 unsigned char hash[PSA_HASH_MAX_SIZE];
2319 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2320 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2321 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2322 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2323 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2324 size_t hash_len;
2325
2326 PSA_ASSERT( psa_crypto_init( ) );
2327 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2328
2329 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2330 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2331 PSA_ASSERT( psa_hash_finish( &op_finished,
2332 hash, sizeof( hash ), &hash_len ) );
2333 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2334 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2335
2336 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2337 PSA_ERROR_BAD_STATE );
2338
2339 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2340 PSA_ASSERT( psa_hash_finish( &op_init,
2341 hash, sizeof( hash ), &hash_len ) );
2342 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2343 PSA_ASSERT( psa_hash_finish( &op_finished,
2344 hash, sizeof( hash ), &hash_len ) );
2345 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2346 PSA_ASSERT( psa_hash_finish( &op_aborted,
2347 hash, sizeof( hash ), &hash_len ) );
2348
2349exit:
2350 psa_hash_abort( &op_source );
2351 psa_hash_abort( &op_init );
2352 psa_hash_abort( &op_setup );
2353 psa_hash_abort( &op_finished );
2354 psa_hash_abort( &op_aborted );
2355 mbedtls_psa_crypto_free( );
2356}
2357/* END_CASE */
2358
2359/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2360void hash_clone_target_state( )
2361{
2362 psa_algorithm_t alg = PSA_ALG_SHA_256;
2363 unsigned char hash[PSA_HASH_MAX_SIZE];
2364 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2365 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2366 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2367 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2368 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2369 size_t hash_len;
2370
2371 PSA_ASSERT( psa_crypto_init( ) );
2372
2373 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2374 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2375 PSA_ASSERT( psa_hash_finish( &op_finished,
2376 hash, sizeof( hash ), &hash_len ) );
2377 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2378 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2379
2380 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2381 PSA_ASSERT( psa_hash_finish( &op_target,
2382 hash, sizeof( hash ), &hash_len ) );
2383
2384 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2385 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2386 PSA_ERROR_BAD_STATE );
2387 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2388 PSA_ERROR_BAD_STATE );
2389
2390exit:
2391 psa_hash_abort( &op_target );
2392 psa_hash_abort( &op_init );
2393 psa_hash_abort( &op_setup );
2394 psa_hash_abort( &op_finished );
2395 psa_hash_abort( &op_aborted );
2396 mbedtls_psa_crypto_free( );
2397}
2398/* END_CASE */
2399
itayzafrir58028322018-10-25 10:22:01 +03002400/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002401void mac_operation_init( )
2402{
Jaeden Amero252ef282019-02-15 14:05:35 +00002403 const uint8_t input[1] = { 0 };
2404
Jaeden Amero769ce272019-01-04 11:48:03 +00002405 /* Test each valid way of initializing the object, except for `= {0}`, as
2406 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2407 * though it's OK by the C standard. We could test for this, but we'd need
2408 * to supress the Clang warning for the test. */
2409 psa_mac_operation_t func = psa_mac_operation_init( );
2410 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2411 psa_mac_operation_t zero;
2412
2413 memset( &zero, 0, sizeof( zero ) );
2414
Jaeden Amero252ef282019-02-15 14:05:35 +00002415 /* A freshly-initialized MAC operation should not be usable. */
2416 TEST_EQUAL( psa_mac_update( &func,
2417 input, sizeof( input ) ),
2418 PSA_ERROR_BAD_STATE );
2419 TEST_EQUAL( psa_mac_update( &init,
2420 input, sizeof( input ) ),
2421 PSA_ERROR_BAD_STATE );
2422 TEST_EQUAL( psa_mac_update( &zero,
2423 input, sizeof( input ) ),
2424 PSA_ERROR_BAD_STATE );
2425
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002426 /* A default MAC operation should be abortable without error. */
2427 PSA_ASSERT( psa_mac_abort( &func ) );
2428 PSA_ASSERT( psa_mac_abort( &init ) );
2429 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002430}
2431/* END_CASE */
2432
2433/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002434void mac_setup( int key_type_arg,
2435 data_t *key,
2436 int alg_arg,
2437 int expected_status_arg )
2438{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002439 psa_key_type_t key_type = key_type_arg;
2440 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002441 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002442 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002443 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2444#if defined(KNOWN_SUPPORTED_MAC_ALG)
2445 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2446#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002447
Gilles Peskine8817f612018-12-18 00:18:46 +01002448 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002449
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002450 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2451 &operation, &status ) )
2452 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002453 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002454
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002455 /* The operation object should be reusable. */
2456#if defined(KNOWN_SUPPORTED_MAC_ALG)
2457 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2458 smoke_test_key_data,
2459 sizeof( smoke_test_key_data ),
2460 KNOWN_SUPPORTED_MAC_ALG,
2461 &operation, &status ) )
2462 goto exit;
2463 TEST_EQUAL( status, PSA_SUCCESS );
2464#endif
2465
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002466exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002467 mbedtls_psa_crypto_free( );
2468}
2469/* END_CASE */
2470
2471/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002472void mac_bad_order( )
2473{
2474 psa_key_handle_t handle = 0;
2475 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2476 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2477 const uint8_t key[] = {
2478 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2479 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2480 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
2481 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2482 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2483 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2484 size_t sign_mac_length = 0;
2485 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2486 const uint8_t verify_mac[] = {
2487 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2488 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2489 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2490
2491 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002492 PSA_ASSERT( psa_allocate_key( &handle ) );
2493 psa_key_policy_set_usage( &policy,
2494 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002495 alg );
2496 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2497
2498 PSA_ASSERT( psa_import_key( handle, key_type,
Jaeden Amero252ef282019-02-15 14:05:35 +00002499 key, sizeof(key) ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002500
Jaeden Amero252ef282019-02-15 14:05:35 +00002501 /* Call update without calling setup beforehand. */
2502 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2503 PSA_ERROR_BAD_STATE );
2504 PSA_ASSERT( psa_mac_abort( &operation ) );
2505
2506 /* Call sign finish without calling setup beforehand. */
2507 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2508 &sign_mac_length),
2509 PSA_ERROR_BAD_STATE );
2510 PSA_ASSERT( psa_mac_abort( &operation ) );
2511
2512 /* Call verify finish without calling setup beforehand. */
2513 TEST_EQUAL( psa_mac_verify_finish( &operation,
2514 verify_mac, sizeof( verify_mac ) ),
2515 PSA_ERROR_BAD_STATE );
2516 PSA_ASSERT( psa_mac_abort( &operation ) );
2517
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002518 /* Call setup twice in a row. */
2519 PSA_ASSERT( psa_mac_sign_setup( &operation,
2520 handle, alg ) );
2521 TEST_EQUAL( psa_mac_sign_setup( &operation,
2522 handle, alg ),
2523 PSA_ERROR_BAD_STATE );
2524 PSA_ASSERT( psa_mac_abort( &operation ) );
2525
Jaeden Amero252ef282019-02-15 14:05:35 +00002526 /* Call update after sign finish. */
2527 PSA_ASSERT( psa_mac_sign_setup( &operation,
2528 handle, alg ) );
2529 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2530 PSA_ASSERT( psa_mac_sign_finish( &operation,
2531 sign_mac, sizeof( sign_mac ),
2532 &sign_mac_length ) );
2533 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2534 PSA_ERROR_BAD_STATE );
2535 PSA_ASSERT( psa_mac_abort( &operation ) );
2536
2537 /* Call update after verify finish. */
2538 PSA_ASSERT( psa_mac_verify_setup( &operation,
2539 handle, alg ) );
2540 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2541 PSA_ASSERT( psa_mac_verify_finish( &operation,
2542 verify_mac, sizeof( verify_mac ) ) );
2543 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2544 PSA_ERROR_BAD_STATE );
2545 PSA_ASSERT( psa_mac_abort( &operation ) );
2546
2547 /* Call sign finish twice in a row. */
2548 PSA_ASSERT( psa_mac_sign_setup( &operation,
2549 handle, alg ) );
2550 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2551 PSA_ASSERT( psa_mac_sign_finish( &operation,
2552 sign_mac, sizeof( sign_mac ),
2553 &sign_mac_length ) );
2554 TEST_EQUAL( psa_mac_sign_finish( &operation,
2555 sign_mac, sizeof( sign_mac ),
2556 &sign_mac_length ),
2557 PSA_ERROR_BAD_STATE );
2558 PSA_ASSERT( psa_mac_abort( &operation ) );
2559
2560 /* Call verify finish twice in a row. */
2561 PSA_ASSERT( psa_mac_verify_setup( &operation,
2562 handle, alg ) );
2563 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2564 PSA_ASSERT( psa_mac_verify_finish( &operation,
2565 verify_mac, sizeof( verify_mac ) ) );
2566 TEST_EQUAL( psa_mac_verify_finish( &operation,
2567 verify_mac, sizeof( verify_mac ) ),
2568 PSA_ERROR_BAD_STATE );
2569 PSA_ASSERT( psa_mac_abort( &operation ) );
2570
2571 /* Setup sign but try verify. */
2572 PSA_ASSERT( psa_mac_sign_setup( &operation,
2573 handle, alg ) );
2574 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2575 TEST_EQUAL( psa_mac_verify_finish( &operation,
2576 verify_mac, sizeof( verify_mac ) ),
2577 PSA_ERROR_BAD_STATE );
2578 PSA_ASSERT( psa_mac_abort( &operation ) );
2579
2580 /* Setup verify but try sign. */
2581 PSA_ASSERT( psa_mac_verify_setup( &operation,
2582 handle, alg ) );
2583 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2584 TEST_EQUAL( psa_mac_sign_finish( &operation,
2585 sign_mac, sizeof( sign_mac ),
2586 &sign_mac_length ),
2587 PSA_ERROR_BAD_STATE );
2588 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002589
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002590exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002591 mbedtls_psa_crypto_free( );
2592}
2593/* END_CASE */
2594
2595/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002596void mac_sign( int key_type_arg,
2597 data_t *key,
2598 int alg_arg,
2599 data_t *input,
2600 data_t *expected_mac )
2601{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002602 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002603 psa_key_type_t key_type = key_type_arg;
2604 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002605 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002606 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002607 /* Leave a little extra room in the output buffer. At the end of the
2608 * test, we'll check that the implementation didn't overwrite onto
2609 * this extra room. */
2610 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2611 size_t mac_buffer_size =
2612 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2613 size_t mac_length = 0;
2614
2615 memset( actual_mac, '+', sizeof( actual_mac ) );
2616 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2617 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2618
Gilles Peskine8817f612018-12-18 00:18:46 +01002619 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002620
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002621 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002622 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002623 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002624
Gilles Peskine8817f612018-12-18 00:18:46 +01002625 PSA_ASSERT( psa_import_key( handle, key_type,
2626 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002627
2628 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002629 PSA_ASSERT( psa_mac_sign_setup( &operation,
2630 handle, alg ) );
2631 PSA_ASSERT( psa_mac_update( &operation,
2632 input->x, input->len ) );
2633 PSA_ASSERT( psa_mac_sign_finish( &operation,
2634 actual_mac, mac_buffer_size,
2635 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002636
2637 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002638 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2639 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002640
2641 /* Verify that the end of the buffer is untouched. */
2642 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2643 sizeof( actual_mac ) - mac_length ) );
2644
2645exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002646 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002647 mbedtls_psa_crypto_free( );
2648}
2649/* END_CASE */
2650
2651/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002652void mac_verify( int key_type_arg,
2653 data_t *key,
2654 int alg_arg,
2655 data_t *input,
2656 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002657{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002658 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002659 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 Peskine8c9def32018-02-08 10:02:12 +01002663
Gilles Peskine69c12672018-06-28 00:07:19 +02002664 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2665
Gilles Peskine8817f612018-12-18 00:18:46 +01002666 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002667
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002668 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002669 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002670 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002671
Gilles Peskine8817f612018-12-18 00:18:46 +01002672 PSA_ASSERT( psa_import_key( handle, key_type,
2673 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002674
Gilles Peskine8817f612018-12-18 00:18:46 +01002675 PSA_ASSERT( psa_mac_verify_setup( &operation,
2676 handle, alg ) );
2677 PSA_ASSERT( psa_destroy_key( handle ) );
2678 PSA_ASSERT( psa_mac_update( &operation,
2679 input->x, input->len ) );
2680 PSA_ASSERT( psa_mac_verify_finish( &operation,
2681 expected_mac->x,
2682 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002683
2684exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002685 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002686 mbedtls_psa_crypto_free( );
2687}
2688/* END_CASE */
2689
2690/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002691void cipher_operation_init( )
2692{
Jaeden Ameroab439972019-02-15 14:12:05 +00002693 const uint8_t input[1] = { 0 };
2694 unsigned char output[1] = { 0 };
2695 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002696 /* Test each valid way of initializing the object, except for `= {0}`, as
2697 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2698 * though it's OK by the C standard. We could test for this, but we'd need
2699 * to supress the Clang warning for the test. */
2700 psa_cipher_operation_t func = psa_cipher_operation_init( );
2701 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2702 psa_cipher_operation_t zero;
2703
2704 memset( &zero, 0, sizeof( zero ) );
2705
Jaeden Ameroab439972019-02-15 14:12:05 +00002706 /* A freshly-initialized cipher operation should not be usable. */
2707 TEST_EQUAL( psa_cipher_update( &func,
2708 input, sizeof( input ),
2709 output, sizeof( output ),
2710 &output_length ),
2711 PSA_ERROR_BAD_STATE );
2712 TEST_EQUAL( psa_cipher_update( &init,
2713 input, sizeof( input ),
2714 output, sizeof( output ),
2715 &output_length ),
2716 PSA_ERROR_BAD_STATE );
2717 TEST_EQUAL( psa_cipher_update( &zero,
2718 input, sizeof( input ),
2719 output, sizeof( output ),
2720 &output_length ),
2721 PSA_ERROR_BAD_STATE );
2722
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002723 /* A default cipher operation should be abortable without error. */
2724 PSA_ASSERT( psa_cipher_abort( &func ) );
2725 PSA_ASSERT( psa_cipher_abort( &init ) );
2726 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002727}
2728/* END_CASE */
2729
2730/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002731void cipher_setup( int key_type_arg,
2732 data_t *key,
2733 int alg_arg,
2734 int expected_status_arg )
2735{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002736 psa_key_type_t key_type = key_type_arg;
2737 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002738 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002739 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002740 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002741#if defined(KNOWN_SUPPORTED_MAC_ALG)
2742 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2743#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002744
Gilles Peskine8817f612018-12-18 00:18:46 +01002745 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002746
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002747 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2748 &operation, &status ) )
2749 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002750 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002751
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002752 /* The operation object should be reusable. */
2753#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2754 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2755 smoke_test_key_data,
2756 sizeof( smoke_test_key_data ),
2757 KNOWN_SUPPORTED_CIPHER_ALG,
2758 &operation, &status ) )
2759 goto exit;
2760 TEST_EQUAL( status, PSA_SUCCESS );
2761#endif
2762
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002763exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002764 mbedtls_psa_crypto_free( );
2765}
2766/* END_CASE */
2767
2768/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002769void cipher_bad_order( )
2770{
2771 psa_key_handle_t handle = 0;
2772 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2773 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
2774 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2775 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2776 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2777 const uint8_t key[] = {
2778 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2779 0xaa, 0xaa, 0xaa, 0xaa };
2780 const uint8_t text[] = {
2781 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2782 0xbb, 0xbb, 0xbb, 0xbb };
2783 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2784 size_t length = 0;
2785
2786 PSA_ASSERT( psa_crypto_init( ) );
2787 PSA_ASSERT( psa_allocate_key( &handle ) );
2788 psa_key_policy_set_usage( &policy,
2789 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2790 alg );
2791 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2792 PSA_ASSERT( psa_import_key( handle, key_type,
2793 key, sizeof(key) ) );
2794
2795
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002796 /* Call encrypt setup twice in a row. */
2797 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2798 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2799 PSA_ERROR_BAD_STATE );
2800 PSA_ASSERT( psa_cipher_abort( &operation ) );
2801
2802 /* Call decrypt setup twice in a row. */
2803 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2804 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2805 PSA_ERROR_BAD_STATE );
2806 PSA_ASSERT( psa_cipher_abort( &operation ) );
2807
Jaeden Ameroab439972019-02-15 14:12:05 +00002808 /* Generate an IV without calling setup beforehand. */
2809 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2810 buffer, sizeof( buffer ),
2811 &length ),
2812 PSA_ERROR_BAD_STATE );
2813 PSA_ASSERT( psa_cipher_abort( &operation ) );
2814
2815 /* Generate an IV twice in a row. */
2816 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2817 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2818 buffer, sizeof( buffer ),
2819 &length ) );
2820 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2821 buffer, sizeof( buffer ),
2822 &length ),
2823 PSA_ERROR_BAD_STATE );
2824 PSA_ASSERT( psa_cipher_abort( &operation ) );
2825
2826 /* Generate an IV after it's already set. */
2827 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2828 PSA_ASSERT( psa_cipher_set_iv( &operation,
2829 iv, sizeof( iv ) ) );
2830 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2831 buffer, sizeof( buffer ),
2832 &length ),
2833 PSA_ERROR_BAD_STATE );
2834 PSA_ASSERT( psa_cipher_abort( &operation ) );
2835
2836 /* Set an IV without calling setup beforehand. */
2837 TEST_EQUAL( psa_cipher_set_iv( &operation,
2838 iv, sizeof( iv ) ),
2839 PSA_ERROR_BAD_STATE );
2840 PSA_ASSERT( psa_cipher_abort( &operation ) );
2841
2842 /* Set an IV after it's already set. */
2843 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2844 PSA_ASSERT( psa_cipher_set_iv( &operation,
2845 iv, sizeof( iv ) ) );
2846 TEST_EQUAL( psa_cipher_set_iv( &operation,
2847 iv, sizeof( iv ) ),
2848 PSA_ERROR_BAD_STATE );
2849 PSA_ASSERT( psa_cipher_abort( &operation ) );
2850
2851 /* Set an IV after it's already generated. */
2852 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2853 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2854 buffer, sizeof( buffer ),
2855 &length ) );
2856 TEST_EQUAL( psa_cipher_set_iv( &operation,
2857 iv, sizeof( iv ) ),
2858 PSA_ERROR_BAD_STATE );
2859 PSA_ASSERT( psa_cipher_abort( &operation ) );
2860
2861 /* Call update without calling setup beforehand. */
2862 TEST_EQUAL( psa_cipher_update( &operation,
2863 text, sizeof( text ),
2864 buffer, sizeof( buffer ),
2865 &length ),
2866 PSA_ERROR_BAD_STATE );
2867 PSA_ASSERT( psa_cipher_abort( &operation ) );
2868
2869 /* Call update without an IV where an IV is required. */
2870 TEST_EQUAL( psa_cipher_update( &operation,
2871 text, sizeof( text ),
2872 buffer, sizeof( buffer ),
2873 &length ),
2874 PSA_ERROR_BAD_STATE );
2875 PSA_ASSERT( psa_cipher_abort( &operation ) );
2876
2877 /* Call update after finish. */
2878 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2879 PSA_ASSERT( psa_cipher_set_iv( &operation,
2880 iv, sizeof( iv ) ) );
2881 PSA_ASSERT( psa_cipher_finish( &operation,
2882 buffer, sizeof( buffer ), &length ) );
2883 TEST_EQUAL( psa_cipher_update( &operation,
2884 text, sizeof( text ),
2885 buffer, sizeof( buffer ),
2886 &length ),
2887 PSA_ERROR_BAD_STATE );
2888 PSA_ASSERT( psa_cipher_abort( &operation ) );
2889
2890 /* Call finish without calling setup beforehand. */
2891 TEST_EQUAL( psa_cipher_finish( &operation,
2892 buffer, sizeof( buffer ), &length ),
2893 PSA_ERROR_BAD_STATE );
2894 PSA_ASSERT( psa_cipher_abort( &operation ) );
2895
2896 /* Call finish without an IV where an IV is required. */
2897 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2898 /* Not calling update means we are encrypting an empty buffer, which is OK
2899 * for cipher modes with padding. */
2900 TEST_EQUAL( psa_cipher_finish( &operation,
2901 buffer, sizeof( buffer ), &length ),
2902 PSA_ERROR_BAD_STATE );
2903 PSA_ASSERT( psa_cipher_abort( &operation ) );
2904
2905 /* Call finish twice in a row. */
2906 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2907 PSA_ASSERT( psa_cipher_set_iv( &operation,
2908 iv, sizeof( iv ) ) );
2909 PSA_ASSERT( psa_cipher_finish( &operation,
2910 buffer, sizeof( buffer ), &length ) );
2911 TEST_EQUAL( psa_cipher_finish( &operation,
2912 buffer, sizeof( buffer ), &length ),
2913 PSA_ERROR_BAD_STATE );
2914 PSA_ASSERT( psa_cipher_abort( &operation ) );
2915
2916exit:
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002917 mbedtls_psa_crypto_free( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002918}
2919/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002920
Gilles Peskine50e586b2018-06-08 14:28:46 +02002921/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002922void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002923 data_t *key,
2924 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002925 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002926{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002927 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002928 psa_status_t status;
2929 psa_key_type_t key_type = key_type_arg;
2930 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002931 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002932 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002933 size_t iv_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002934 unsigned char *output = NULL;
2935 size_t output_buffer_size = 0;
2936 size_t function_output_length = 0;
2937 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002938 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002939 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002940
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002941 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2942 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002943
Gilles Peskine8817f612018-12-18 00:18:46 +01002944 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002945
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002946 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002947 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002948 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002949
Gilles Peskine8817f612018-12-18 00:18:46 +01002950 PSA_ASSERT( psa_import_key( handle, key_type,
2951 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002952
Gilles Peskine8817f612018-12-18 00:18:46 +01002953 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2954 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002955
Gilles Peskine8817f612018-12-18 00:18:46 +01002956 PSA_ASSERT( psa_cipher_set_iv( &operation,
2957 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002958 output_buffer_size = ( (size_t) input->len +
2959 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002960 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002961
Gilles Peskine8817f612018-12-18 00:18:46 +01002962 PSA_ASSERT( psa_cipher_update( &operation,
2963 input->x, input->len,
2964 output, output_buffer_size,
2965 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002966 total_output_length += function_output_length;
2967 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002968 output + total_output_length,
2969 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002970 &function_output_length );
2971 total_output_length += function_output_length;
2972
Gilles Peskinefe11b722018-12-18 00:24:04 +01002973 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002974 if( expected_status == PSA_SUCCESS )
2975 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002976 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002977 ASSERT_COMPARE( expected_output->x, expected_output->len,
2978 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002979 }
2980
2981exit:
2982 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002983 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002984 mbedtls_psa_crypto_free( );
2985}
2986/* END_CASE */
2987
2988/* BEGIN_CASE */
2989void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
2990 data_t *key,
2991 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002992 int first_part_size_arg,
2993 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002994 data_t *expected_output )
2995{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002996 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002997 psa_key_type_t key_type = key_type_arg;
2998 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002999 size_t first_part_size = first_part_size_arg;
3000 size_t output1_length = output1_length_arg;
3001 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003002 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003003 size_t iv_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003004 unsigned char *output = NULL;
3005 size_t output_buffer_size = 0;
3006 size_t function_output_length = 0;
3007 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003008 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003009 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003010
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003011 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3012 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003013
Gilles Peskine8817f612018-12-18 00:18:46 +01003014 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003015
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003016 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003017 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003018 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003019
Gilles Peskine8817f612018-12-18 00:18:46 +01003020 PSA_ASSERT( psa_import_key( handle, key_type,
3021 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003022
Gilles Peskine8817f612018-12-18 00:18:46 +01003023 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3024 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003025
Gilles Peskine8817f612018-12-18 00:18:46 +01003026 PSA_ASSERT( psa_cipher_set_iv( &operation,
3027 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003028 output_buffer_size = ( (size_t) input->len +
3029 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003030 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003031
Gilles Peskinee0866522019-02-19 19:44:00 +01003032 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003033 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3034 output, output_buffer_size,
3035 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003036 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003037 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003038 PSA_ASSERT( psa_cipher_update( &operation,
3039 input->x + first_part_size,
3040 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003041 output + total_output_length,
3042 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003043 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003044 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003045 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003046 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003047 output + total_output_length,
3048 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003049 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003050 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003051 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003052
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003053 ASSERT_COMPARE( expected_output->x, expected_output->len,
3054 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003055
3056exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003057 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003058 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003059 mbedtls_psa_crypto_free( );
3060}
3061/* END_CASE */
3062
3063/* BEGIN_CASE */
3064void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003065 data_t *key,
3066 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003067 int first_part_size_arg,
3068 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003069 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003070{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003071 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003072
3073 psa_key_type_t key_type = key_type_arg;
3074 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003075 size_t first_part_size = first_part_size_arg;
3076 size_t output1_length = output1_length_arg;
3077 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003078 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003079 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003080 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003081 size_t output_buffer_size = 0;
3082 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003083 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003084 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003085 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003086
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003087 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3088 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003089
Gilles Peskine8817f612018-12-18 00:18:46 +01003090 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003091
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003092 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003093 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003094 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003095
Gilles Peskine8817f612018-12-18 00:18:46 +01003096 PSA_ASSERT( psa_import_key( handle, key_type,
3097 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003098
Gilles Peskine8817f612018-12-18 00:18:46 +01003099 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3100 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003101
Gilles Peskine8817f612018-12-18 00:18:46 +01003102 PSA_ASSERT( psa_cipher_set_iv( &operation,
3103 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003104
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003105 output_buffer_size = ( (size_t) input->len +
3106 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003107 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003108
Gilles Peskinee0866522019-02-19 19:44:00 +01003109 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003110 PSA_ASSERT( psa_cipher_update( &operation,
3111 input->x, first_part_size,
3112 output, output_buffer_size,
3113 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003114 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003115 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003116 PSA_ASSERT( psa_cipher_update( &operation,
3117 input->x + first_part_size,
3118 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003119 output + total_output_length,
3120 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003121 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003122 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003123 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003124 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003125 output + total_output_length,
3126 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003127 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003128 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003129 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003130
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003131 ASSERT_COMPARE( expected_output->x, expected_output->len,
3132 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003133
3134exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003135 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003136 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003137 mbedtls_psa_crypto_free( );
3138}
3139/* END_CASE */
3140
Gilles Peskine50e586b2018-06-08 14:28:46 +02003141/* BEGIN_CASE */
3142void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003143 data_t *key,
3144 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003145 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003146{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003147 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003148 psa_status_t status;
3149 psa_key_type_t key_type = key_type_arg;
3150 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003151 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003152 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003153 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003154 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003155 size_t output_buffer_size = 0;
3156 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003157 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003158 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003159 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003160
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003161 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3162 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003163
Gilles Peskine8817f612018-12-18 00:18:46 +01003164 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003165
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003166 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003167 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003168 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003169
Gilles Peskine8817f612018-12-18 00:18:46 +01003170 PSA_ASSERT( psa_import_key( handle, key_type,
3171 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003172
Gilles Peskine8817f612018-12-18 00:18:46 +01003173 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3174 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003175
Gilles Peskine8817f612018-12-18 00:18:46 +01003176 PSA_ASSERT( psa_cipher_set_iv( &operation,
3177 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003178
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003179 output_buffer_size = ( (size_t) input->len +
3180 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003181 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003182
Gilles Peskine8817f612018-12-18 00:18:46 +01003183 PSA_ASSERT( psa_cipher_update( &operation,
3184 input->x, input->len,
3185 output, output_buffer_size,
3186 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003187 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003188 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003189 output + total_output_length,
3190 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003191 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003192 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003193 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003194
3195 if( expected_status == PSA_SUCCESS )
3196 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003197 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003198 ASSERT_COMPARE( expected_output->x, expected_output->len,
3199 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003200 }
3201
Gilles Peskine50e586b2018-06-08 14:28:46 +02003202exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003203 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003204 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003205 mbedtls_psa_crypto_free( );
3206}
3207/* END_CASE */
3208
Gilles Peskine50e586b2018-06-08 14:28:46 +02003209/* BEGIN_CASE */
3210void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003211 data_t *key,
3212 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003213{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003214 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003215 psa_key_type_t key_type = key_type_arg;
3216 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003217 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003218 size_t iv_size = 16;
3219 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003220 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003221 size_t output1_size = 0;
3222 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003223 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003224 size_t output2_size = 0;
3225 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003226 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003227 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3228 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003229 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003230
Gilles Peskine8817f612018-12-18 00:18:46 +01003231 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003232
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003233 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003234 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003235 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003236
Gilles Peskine8817f612018-12-18 00:18:46 +01003237 PSA_ASSERT( psa_import_key( handle, key_type,
3238 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003239
Gilles Peskine8817f612018-12-18 00:18:46 +01003240 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3241 handle, alg ) );
3242 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3243 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003244
Gilles Peskine8817f612018-12-18 00:18:46 +01003245 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3246 iv, iv_size,
3247 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003248 output1_size = ( (size_t) input->len +
3249 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003250 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003251
Gilles Peskine8817f612018-12-18 00:18:46 +01003252 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3253 output1, output1_size,
3254 &output1_length ) );
3255 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003256 output1 + output1_length,
3257 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003258 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003259
Gilles Peskine048b7f02018-06-08 14:20:49 +02003260 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003261
Gilles Peskine8817f612018-12-18 00:18:46 +01003262 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003263
3264 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003265 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003266
Gilles Peskine8817f612018-12-18 00:18:46 +01003267 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3268 iv, iv_length ) );
3269 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3270 output2, output2_size,
3271 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003272 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003273 PSA_ASSERT( psa_cipher_finish( &operation2,
3274 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003275 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003276 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003277
Gilles Peskine048b7f02018-06-08 14:20:49 +02003278 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003279
Gilles Peskine8817f612018-12-18 00:18:46 +01003280 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003281
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003282 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003283
3284exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003285 mbedtls_free( output1 );
3286 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003287 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03003288 mbedtls_psa_crypto_free( );
3289}
3290/* END_CASE */
3291
3292/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003293void cipher_verify_output_multipart( int alg_arg,
3294 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003295 data_t *key,
3296 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003297 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003298{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003299 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003300 psa_key_type_t key_type = key_type_arg;
3301 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003302 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003303 unsigned char iv[16] = {0};
3304 size_t iv_size = 16;
3305 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003306 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003307 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003308 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003309 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003310 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003311 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003312 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003313 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3314 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003315 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003316
Gilles Peskine8817f612018-12-18 00:18:46 +01003317 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003318
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003319 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003320 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003321 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003322
Gilles Peskine8817f612018-12-18 00:18:46 +01003323 PSA_ASSERT( psa_import_key( handle, key_type,
3324 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003325
Gilles Peskine8817f612018-12-18 00:18:46 +01003326 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3327 handle, alg ) );
3328 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3329 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003330
Gilles Peskine8817f612018-12-18 00:18:46 +01003331 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3332 iv, iv_size,
3333 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003334 output1_buffer_size = ( (size_t) input->len +
3335 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003336 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003337
Gilles Peskinee0866522019-02-19 19:44:00 +01003338 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003339
Gilles Peskine8817f612018-12-18 00:18:46 +01003340 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3341 output1, output1_buffer_size,
3342 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003343 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003344
Gilles Peskine8817f612018-12-18 00:18:46 +01003345 PSA_ASSERT( psa_cipher_update( &operation1,
3346 input->x + first_part_size,
3347 input->len - first_part_size,
3348 output1, output1_buffer_size,
3349 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003350 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003351
Gilles Peskine8817f612018-12-18 00:18:46 +01003352 PSA_ASSERT( psa_cipher_finish( &operation1,
3353 output1 + output1_length,
3354 output1_buffer_size - output1_length,
3355 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003356 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003357
Gilles Peskine8817f612018-12-18 00:18:46 +01003358 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003359
Gilles Peskine048b7f02018-06-08 14:20:49 +02003360 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003361 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003362
Gilles Peskine8817f612018-12-18 00:18:46 +01003363 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3364 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003365
Gilles Peskine8817f612018-12-18 00:18:46 +01003366 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3367 output2, output2_buffer_size,
3368 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003369 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003370
Gilles Peskine8817f612018-12-18 00:18:46 +01003371 PSA_ASSERT( psa_cipher_update( &operation2,
3372 output1 + first_part_size,
3373 output1_length - first_part_size,
3374 output2, output2_buffer_size,
3375 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003376 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003377
Gilles Peskine8817f612018-12-18 00:18:46 +01003378 PSA_ASSERT( psa_cipher_finish( &operation2,
3379 output2 + output2_length,
3380 output2_buffer_size - output2_length,
3381 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003382 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003383
Gilles Peskine8817f612018-12-18 00:18:46 +01003384 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003385
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003386 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003387
3388exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003389 mbedtls_free( output1 );
3390 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003391 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003392 mbedtls_psa_crypto_free( );
3393}
3394/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003395
Gilles Peskine20035e32018-02-03 22:44:14 +01003396/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003397void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003398 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003399 data_t *nonce,
3400 data_t *additional_data,
3401 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003402 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003403{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003404 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003405 psa_key_type_t key_type = key_type_arg;
3406 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003407 unsigned char *output_data = NULL;
3408 size_t output_size = 0;
3409 size_t output_length = 0;
3410 unsigned char *output_data2 = NULL;
3411 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003412 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003413 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003414 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003415
Gilles Peskine4abf7412018-06-18 16:35:34 +02003416 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003417 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003418
Gilles Peskine8817f612018-12-18 00:18:46 +01003419 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003420
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003421 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003422 psa_key_policy_set_usage( &policy,
3423 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
3424 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003425 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003426
Gilles Peskine8817f612018-12-18 00:18:46 +01003427 PSA_ASSERT( psa_import_key( handle, key_type,
3428 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003429
Gilles Peskinefe11b722018-12-18 00:24:04 +01003430 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3431 nonce->x, nonce->len,
3432 additional_data->x,
3433 additional_data->len,
3434 input_data->x, input_data->len,
3435 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003436 &output_length ),
3437 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003438
3439 if( PSA_SUCCESS == expected_result )
3440 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003441 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003442
Gilles Peskinefe11b722018-12-18 00:24:04 +01003443 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3444 nonce->x, nonce->len,
3445 additional_data->x,
3446 additional_data->len,
3447 output_data, output_length,
3448 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003449 &output_length2 ),
3450 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003451
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003452 ASSERT_COMPARE( input_data->x, input_data->len,
3453 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003454 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003455
Gilles Peskinea1cac842018-06-11 19:33:02 +02003456exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003457 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003458 mbedtls_free( output_data );
3459 mbedtls_free( output_data2 );
3460 mbedtls_psa_crypto_free( );
3461}
3462/* END_CASE */
3463
3464/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003465void aead_encrypt( int key_type_arg, data_t *key_data,
3466 int alg_arg,
3467 data_t *nonce,
3468 data_t *additional_data,
3469 data_t *input_data,
3470 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003471{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003472 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003473 psa_key_type_t key_type = key_type_arg;
3474 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003475 unsigned char *output_data = NULL;
3476 size_t output_size = 0;
3477 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003478 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00003479 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003480
Gilles Peskine4abf7412018-06-18 16:35:34 +02003481 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003482 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003483
Gilles Peskine8817f612018-12-18 00:18:46 +01003484 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003485
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003486 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003487 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003488 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003489
Gilles Peskine8817f612018-12-18 00:18:46 +01003490 PSA_ASSERT( psa_import_key( handle, key_type,
3491 key_data->x,
3492 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003493
Gilles Peskine8817f612018-12-18 00:18:46 +01003494 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3495 nonce->x, nonce->len,
3496 additional_data->x, additional_data->len,
3497 input_data->x, input_data->len,
3498 output_data, output_size,
3499 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003500
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003501 ASSERT_COMPARE( expected_result->x, expected_result->len,
3502 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003503
Gilles Peskinea1cac842018-06-11 19:33:02 +02003504exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003505 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003506 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003507 mbedtls_psa_crypto_free( );
3508}
3509/* END_CASE */
3510
3511/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003512void aead_decrypt( int key_type_arg, data_t *key_data,
3513 int alg_arg,
3514 data_t *nonce,
3515 data_t *additional_data,
3516 data_t *input_data,
3517 data_t *expected_data,
3518 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003519{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003520 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003521 psa_key_type_t key_type = key_type_arg;
3522 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003523 unsigned char *output_data = NULL;
3524 size_t output_size = 0;
3525 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003526 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00003527 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003528 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003529
Gilles Peskine4abf7412018-06-18 16:35:34 +02003530 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003531 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003532
Gilles Peskine8817f612018-12-18 00:18:46 +01003533 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003534
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003535 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003536 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003537 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003538
Gilles Peskine8817f612018-12-18 00:18:46 +01003539 PSA_ASSERT( psa_import_key( handle, key_type,
3540 key_data->x,
3541 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003542
Gilles Peskinefe11b722018-12-18 00:24:04 +01003543 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3544 nonce->x, nonce->len,
3545 additional_data->x,
3546 additional_data->len,
3547 input_data->x, input_data->len,
3548 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003549 &output_length ),
3550 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003551
Gilles Peskine2d277862018-06-18 15:41:12 +02003552 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003553 ASSERT_COMPARE( expected_data->x, expected_data->len,
3554 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003555
Gilles Peskinea1cac842018-06-11 19:33:02 +02003556exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003557 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003558 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003559 mbedtls_psa_crypto_free( );
3560}
3561/* END_CASE */
3562
3563/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003564void signature_size( int type_arg,
3565 int bits,
3566 int alg_arg,
3567 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003568{
3569 psa_key_type_t type = type_arg;
3570 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003571 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003572 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003573exit:
3574 ;
3575}
3576/* END_CASE */
3577
3578/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003579void sign_deterministic( int key_type_arg, data_t *key_data,
3580 int alg_arg, data_t *input_data,
3581 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003582{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003583 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003584 psa_key_type_t key_type = key_type_arg;
3585 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003586 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003587 unsigned char *signature = NULL;
3588 size_t signature_size;
3589 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003590 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003591
Gilles Peskine8817f612018-12-18 00:18:46 +01003592 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003593
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003594 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003595 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003596 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003597
Gilles Peskine8817f612018-12-18 00:18:46 +01003598 PSA_ASSERT( psa_import_key( handle, key_type,
3599 key_data->x,
3600 key_data->len ) );
3601 PSA_ASSERT( psa_get_key_information( handle,
3602 NULL,
3603 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003604
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003605 /* Allocate a buffer which has the size advertized by the
3606 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003607 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3608 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003609 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003610 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003611 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003612
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003613 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003614 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3615 input_data->x, input_data->len,
3616 signature, signature_size,
3617 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003618 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003619 ASSERT_COMPARE( output_data->x, output_data->len,
3620 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003621
3622exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003623 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003624 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01003625 mbedtls_psa_crypto_free( );
3626}
3627/* END_CASE */
3628
3629/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003630void sign_fail( int key_type_arg, data_t *key_data,
3631 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003632 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003633{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003634 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003635 psa_key_type_t key_type = key_type_arg;
3636 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003637 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003638 psa_status_t actual_status;
3639 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003640 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003641 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003642 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003643
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003644 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003645
Gilles Peskine8817f612018-12-18 00:18:46 +01003646 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003647
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003648 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003649 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003650 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003651
Gilles Peskine8817f612018-12-18 00:18:46 +01003652 PSA_ASSERT( psa_import_key( handle, key_type,
3653 key_data->x,
3654 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003655
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003656 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003657 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003658 signature, signature_size,
3659 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003660 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003661 /* The value of *signature_length is unspecified on error, but
3662 * whatever it is, it should be less than signature_size, so that
3663 * if the caller tries to read *signature_length bytes without
3664 * checking the error code then they don't overflow a buffer. */
3665 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003666
3667exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003668 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003669 mbedtls_free( signature );
3670 mbedtls_psa_crypto_free( );
3671}
3672/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003673
3674/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003675void sign_verify( int key_type_arg, data_t *key_data,
3676 int alg_arg, data_t *input_data )
3677{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003678 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003679 psa_key_type_t key_type = key_type_arg;
3680 psa_algorithm_t alg = alg_arg;
3681 size_t key_bits;
3682 unsigned char *signature = NULL;
3683 size_t signature_size;
3684 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003685 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003686
Gilles Peskine8817f612018-12-18 00:18:46 +01003687 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003688
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003689 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003690 psa_key_policy_set_usage( &policy,
3691 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
3692 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003693 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003694
Gilles Peskine8817f612018-12-18 00:18:46 +01003695 PSA_ASSERT( psa_import_key( handle, key_type,
3696 key_data->x,
3697 key_data->len ) );
3698 PSA_ASSERT( psa_get_key_information( handle,
3699 NULL,
3700 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003701
3702 /* Allocate a buffer which has the size advertized by the
3703 * library. */
3704 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3705 key_bits, alg );
3706 TEST_ASSERT( signature_size != 0 );
3707 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003708 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003709
3710 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003711 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3712 input_data->x, input_data->len,
3713 signature, signature_size,
3714 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003715 /* Check that the signature length looks sensible. */
3716 TEST_ASSERT( signature_length <= signature_size );
3717 TEST_ASSERT( signature_length > 0 );
3718
3719 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003720 PSA_ASSERT( psa_asymmetric_verify(
3721 handle, alg,
3722 input_data->x, input_data->len,
3723 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003724
3725 if( input_data->len != 0 )
3726 {
3727 /* Flip a bit in the input and verify that the signature is now
3728 * detected as invalid. Flip a bit at the beginning, not at the end,
3729 * because ECDSA may ignore the last few bits of the input. */
3730 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003731 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3732 input_data->x, input_data->len,
3733 signature, signature_length ),
3734 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003735 }
3736
3737exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003738 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003739 mbedtls_free( signature );
3740 mbedtls_psa_crypto_free( );
3741}
3742/* END_CASE */
3743
3744/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003745void asymmetric_verify( int key_type_arg, data_t *key_data,
3746 int alg_arg, data_t *hash_data,
3747 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003748{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003749 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003750 psa_key_type_t key_type = key_type_arg;
3751 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003752 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003753
Gilles Peskine69c12672018-06-28 00:07:19 +02003754 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3755
Gilles Peskine8817f612018-12-18 00:18:46 +01003756 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003757
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003758 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003759 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003760 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03003761
Gilles Peskine8817f612018-12-18 00:18:46 +01003762 PSA_ASSERT( psa_import_key( handle, key_type,
3763 key_data->x,
3764 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003765
Gilles Peskine8817f612018-12-18 00:18:46 +01003766 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3767 hash_data->x, hash_data->len,
3768 signature_data->x,
3769 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003770exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003771 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003772 mbedtls_psa_crypto_free( );
3773}
3774/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003775
3776/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003777void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3778 int alg_arg, data_t *hash_data,
3779 data_t *signature_data,
3780 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003781{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003782 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003783 psa_key_type_t key_type = key_type_arg;
3784 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003785 psa_status_t actual_status;
3786 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003787 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003788
Gilles Peskine8817f612018-12-18 00:18:46 +01003789 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003790
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003791 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003792 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003793 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003794
Gilles Peskine8817f612018-12-18 00:18:46 +01003795 PSA_ASSERT( psa_import_key( handle, key_type,
3796 key_data->x,
3797 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003798
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003799 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003800 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003801 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003802 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003803
Gilles Peskinefe11b722018-12-18 00:24:04 +01003804 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003805
3806exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003807 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003808 mbedtls_psa_crypto_free( );
3809}
3810/* END_CASE */
3811
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003812/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003813void asymmetric_encrypt( int key_type_arg,
3814 data_t *key_data,
3815 int alg_arg,
3816 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003817 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003818 int expected_output_length_arg,
3819 int expected_status_arg )
3820{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003821 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003822 psa_key_type_t key_type = key_type_arg;
3823 psa_algorithm_t alg = alg_arg;
3824 size_t expected_output_length = expected_output_length_arg;
3825 size_t key_bits;
3826 unsigned char *output = NULL;
3827 size_t output_size;
3828 size_t output_length = ~0;
3829 psa_status_t actual_status;
3830 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003831 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003832
Gilles Peskine8817f612018-12-18 00:18:46 +01003833 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003834
Gilles Peskine656896e2018-06-29 19:12:28 +02003835 /* Import the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003836 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003837 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003838 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3839 PSA_ASSERT( psa_import_key( handle, key_type,
3840 key_data->x,
3841 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003842
3843 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003844 PSA_ASSERT( psa_get_key_information( handle,
3845 NULL,
3846 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003847 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003848 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003849
3850 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003851 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003852 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003853 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003854 output, output_size,
3855 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003856 TEST_EQUAL( actual_status, expected_status );
3857 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003858
Gilles Peskine68428122018-06-30 18:42:41 +02003859 /* If the label is empty, the test framework puts a non-null pointer
3860 * in label->x. Test that a null pointer works as well. */
3861 if( label->len == 0 )
3862 {
3863 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003864 if( output_size != 0 )
3865 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003866 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003867 input_data->x, input_data->len,
3868 NULL, label->len,
3869 output, output_size,
3870 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003871 TEST_EQUAL( actual_status, expected_status );
3872 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003873 }
3874
Gilles Peskine656896e2018-06-29 19:12:28 +02003875exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003876 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003877 mbedtls_free( output );
3878 mbedtls_psa_crypto_free( );
3879}
3880/* END_CASE */
3881
3882/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003883void asymmetric_encrypt_decrypt( int key_type_arg,
3884 data_t *key_data,
3885 int alg_arg,
3886 data_t *input_data,
3887 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003888{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003889 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003890 psa_key_type_t key_type = key_type_arg;
3891 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003892 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003893 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003894 size_t output_size;
3895 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003896 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003897 size_t output2_size;
3898 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003899 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003900
Gilles Peskine8817f612018-12-18 00:18:46 +01003901 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003902
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003903 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003904 psa_key_policy_set_usage( &policy,
3905 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003906 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003907 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003908
Gilles Peskine8817f612018-12-18 00:18:46 +01003909 PSA_ASSERT( psa_import_key( handle, key_type,
3910 key_data->x,
3911 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003912
3913 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003914 PSA_ASSERT( psa_get_key_information( handle,
3915 NULL,
3916 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003917 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003918 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003919 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003920 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003921
Gilles Peskineeebd7382018-06-08 18:11:54 +02003922 /* We test encryption by checking that encrypt-then-decrypt gives back
3923 * the original plaintext because of the non-optional random
3924 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003925 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3926 input_data->x, input_data->len,
3927 label->x, label->len,
3928 output, output_size,
3929 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003930 /* We don't know what ciphertext length to expect, but check that
3931 * it looks sensible. */
3932 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003933
Gilles Peskine8817f612018-12-18 00:18:46 +01003934 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3935 output, output_length,
3936 label->x, label->len,
3937 output2, output2_size,
3938 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003939 ASSERT_COMPARE( input_data->x, input_data->len,
3940 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003941
3942exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003943 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003944 mbedtls_free( output );
3945 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003946 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003947}
3948/* END_CASE */
3949
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003950/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003951void asymmetric_decrypt( int key_type_arg,
3952 data_t *key_data,
3953 int alg_arg,
3954 data_t *input_data,
3955 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003956 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003957{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003958 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003959 psa_key_type_t key_type = key_type_arg;
3960 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003961 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003962 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003963 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003964 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003965
Jaeden Amero412654a2019-02-06 12:57:46 +00003966 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003967 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003968
Gilles Peskine8817f612018-12-18 00:18:46 +01003969 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003970
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003971 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003972 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003973 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003974
Gilles Peskine8817f612018-12-18 00:18:46 +01003975 PSA_ASSERT( psa_import_key( handle, key_type,
3976 key_data->x,
3977 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003978
Gilles Peskine8817f612018-12-18 00:18:46 +01003979 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3980 input_data->x, input_data->len,
3981 label->x, label->len,
3982 output,
3983 output_size,
3984 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003985 ASSERT_COMPARE( expected_data->x, expected_data->len,
3986 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003987
Gilles Peskine68428122018-06-30 18:42:41 +02003988 /* If the label is empty, the test framework puts a non-null pointer
3989 * in label->x. Test that a null pointer works as well. */
3990 if( label->len == 0 )
3991 {
3992 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003993 if( output_size != 0 )
3994 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003995 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3996 input_data->x, input_data->len,
3997 NULL, label->len,
3998 output,
3999 output_size,
4000 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004001 ASSERT_COMPARE( expected_data->x, expected_data->len,
4002 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004003 }
4004
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004005exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004006 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004007 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004008 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004009}
4010/* END_CASE */
4011
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004012/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004013void asymmetric_decrypt_fail( int key_type_arg,
4014 data_t *key_data,
4015 int alg_arg,
4016 data_t *input_data,
4017 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004018 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004019 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004020{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004021 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004022 psa_key_type_t key_type = key_type_arg;
4023 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004024 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004025 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004026 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004027 psa_status_t actual_status;
4028 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004029 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004030
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004031 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004032
Gilles Peskine8817f612018-12-18 00:18:46 +01004033 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004034
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004035 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02004036 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004037 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004038
Gilles Peskine8817f612018-12-18 00:18:46 +01004039 PSA_ASSERT( psa_import_key( handle, key_type,
4040 key_data->x,
4041 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004042
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004043 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004044 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004045 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004046 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004047 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004048 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004049 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004050
Gilles Peskine68428122018-06-30 18:42:41 +02004051 /* If the label is empty, the test framework puts a non-null pointer
4052 * in label->x. Test that a null pointer works as well. */
4053 if( label->len == 0 )
4054 {
4055 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004056 if( output_size != 0 )
4057 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004058 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004059 input_data->x, input_data->len,
4060 NULL, label->len,
4061 output, output_size,
4062 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004063 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004064 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004065 }
4066
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004067exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004068 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004069 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004070 mbedtls_psa_crypto_free( );
4071}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004072/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004073
4074/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00004075void crypto_generator_init( )
4076{
4077 /* Test each valid way of initializing the object, except for `= {0}`, as
4078 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4079 * though it's OK by the C standard. We could test for this, but we'd need
4080 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004081 size_t capacity;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004082 psa_crypto_generator_t func = psa_crypto_generator_init( );
4083 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
4084 psa_crypto_generator_t zero;
4085
4086 memset( &zero, 0, sizeof( zero ) );
4087
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004088 /* A default generator should not be able to report its capacity. */
4089 TEST_EQUAL( psa_get_generator_capacity( &func, &capacity ),
4090 PSA_ERROR_BAD_STATE );
4091 TEST_EQUAL( psa_get_generator_capacity( &init, &capacity ),
4092 PSA_ERROR_BAD_STATE );
4093 TEST_EQUAL( psa_get_generator_capacity( &zero, &capacity ),
4094 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004095
4096 /* A default generator should be abortable without error. */
4097 PSA_ASSERT( psa_generator_abort(&func) );
4098 PSA_ASSERT( psa_generator_abort(&init) );
4099 PSA_ASSERT( psa_generator_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004100}
4101/* END_CASE */
4102
4103/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004104void derive_setup( int key_type_arg,
4105 data_t *key_data,
4106 int alg_arg,
4107 data_t *salt,
4108 data_t *label,
4109 int requested_capacity_arg,
4110 int expected_status_arg )
4111{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004112 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004113 size_t key_type = key_type_arg;
4114 psa_algorithm_t alg = alg_arg;
4115 size_t requested_capacity = requested_capacity_arg;
4116 psa_status_t expected_status = expected_status_arg;
4117 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004118 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004119
Gilles Peskine8817f612018-12-18 00:18:46 +01004120 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004121
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004122 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004123 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004124 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004125
Gilles Peskine8817f612018-12-18 00:18:46 +01004126 PSA_ASSERT( psa_import_key( handle, key_type,
4127 key_data->x,
4128 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004129
Gilles Peskinefe11b722018-12-18 00:24:04 +01004130 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4131 salt->x, salt->len,
4132 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004133 requested_capacity ),
4134 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004135
4136exit:
4137 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004138 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004139 mbedtls_psa_crypto_free( );
4140}
4141/* END_CASE */
4142
4143/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004144void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004145{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004146 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004147 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004148 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004149 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004150 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004151 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004152 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4153 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4154 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00004155 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004156
Gilles Peskine8817f612018-12-18 00:18:46 +01004157 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004158
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004159 PSA_ASSERT( psa_allocate_key( &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004160 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004161 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004162
Gilles Peskine8817f612018-12-18 00:18:46 +01004163 PSA_ASSERT( psa_import_key( handle, key_type,
4164 key_data,
4165 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004166
4167 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01004168 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4169 NULL, 0,
4170 NULL, 0,
4171 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004172
4173 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01004174 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4175 NULL, 0,
4176 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004177 capacity ),
4178 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004179
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004180 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004181
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004182 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004183 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004184
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004185exit:
4186 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004187 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004188 mbedtls_psa_crypto_free( );
4189}
4190/* END_CASE */
4191
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004192/* BEGIN_CASE */
4193void test_derive_invalid_generator_tests( )
4194{
4195 uint8_t output_buffer[16];
4196 size_t buffer_size = 16;
4197 size_t capacity = 0;
4198 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4199
Nir Sonnenschein50789302018-10-31 12:16:38 +02004200 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004201 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004202
4203 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004204 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004205
Gilles Peskine8817f612018-12-18 00:18:46 +01004206 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004207
Nir Sonnenschein50789302018-10-31 12:16:38 +02004208 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004209 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004210
Nir Sonnenschein50789302018-10-31 12:16:38 +02004211 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004212 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004213
4214exit:
4215 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004216}
4217/* END_CASE */
4218
4219/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004220void derive_output( int alg_arg,
4221 data_t *key_data,
4222 data_t *salt,
4223 data_t *label,
4224 int requested_capacity_arg,
4225 data_t *expected_output1,
4226 data_t *expected_output2 )
4227{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004228 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004229 psa_algorithm_t alg = alg_arg;
4230 size_t requested_capacity = requested_capacity_arg;
4231 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4232 uint8_t *expected_outputs[2] =
4233 {expected_output1->x, expected_output2->x};
4234 size_t output_sizes[2] =
4235 {expected_output1->len, expected_output2->len};
4236 size_t output_buffer_size = 0;
4237 uint8_t *output_buffer = NULL;
4238 size_t expected_capacity;
4239 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00004240 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004241 psa_status_t status;
4242 unsigned i;
4243
4244 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4245 {
4246 if( output_sizes[i] > output_buffer_size )
4247 output_buffer_size = output_sizes[i];
4248 if( output_sizes[i] == 0 )
4249 expected_outputs[i] = NULL;
4250 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004251 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004252 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004253
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004254 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004255 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004256 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004257
Gilles Peskine8817f612018-12-18 00:18:46 +01004258 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
4259 key_data->x,
4260 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004261
4262 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004263 if( PSA_ALG_IS_HKDF( alg ) )
4264 {
4265 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4266 PSA_ASSERT( psa_set_generator_capacity( &generator,
4267 requested_capacity ) );
4268 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4269 PSA_KDF_STEP_SALT,
4270 salt->x, salt->len ) );
4271 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4272 PSA_KDF_STEP_SECRET,
4273 handle ) );
4274 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4275 PSA_KDF_STEP_INFO,
4276 label->x, label->len ) );
4277 }
4278 else
4279 {
4280 // legacy
4281 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4282 salt->x, salt->len,
4283 label->x, label->len,
4284 requested_capacity ) );
4285 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004286 PSA_ASSERT( psa_get_generator_capacity( &generator,
4287 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004288 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004289 expected_capacity = requested_capacity;
4290
4291 /* Expansion phase. */
4292 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4293 {
4294 /* Read some bytes. */
4295 status = psa_generator_read( &generator,
4296 output_buffer, output_sizes[i] );
4297 if( expected_capacity == 0 && output_sizes[i] == 0 )
4298 {
4299 /* Reading 0 bytes when 0 bytes are available can go either way. */
4300 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004301 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004302 continue;
4303 }
4304 else if( expected_capacity == 0 ||
4305 output_sizes[i] > expected_capacity )
4306 {
4307 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004308 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004309 expected_capacity = 0;
4310 continue;
4311 }
4312 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004313 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004314 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004315 ASSERT_COMPARE( output_buffer, output_sizes[i],
4316 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004317 /* Check the generator status. */
4318 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01004319 PSA_ASSERT( psa_get_generator_capacity( &generator,
4320 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004321 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004322 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004323 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004324
4325exit:
4326 mbedtls_free( output_buffer );
4327 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004328 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004329 mbedtls_psa_crypto_free( );
4330}
4331/* END_CASE */
4332
4333/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004334void derive_full( int alg_arg,
4335 data_t *key_data,
4336 data_t *salt,
4337 data_t *label,
4338 int requested_capacity_arg )
4339{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004340 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004341 psa_algorithm_t alg = alg_arg;
4342 size_t requested_capacity = requested_capacity_arg;
4343 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4344 unsigned char output_buffer[16];
4345 size_t expected_capacity = requested_capacity;
4346 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00004347 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004348
Gilles Peskine8817f612018-12-18 00:18:46 +01004349 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004350
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004351 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004352 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004353 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004354
Gilles Peskine8817f612018-12-18 00:18:46 +01004355 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
4356 key_data->x,
4357 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004358
4359 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004360 if( PSA_ALG_IS_HKDF( alg ) )
4361 {
4362 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4363 PSA_ASSERT( psa_set_generator_capacity( &generator,
4364 requested_capacity ) );
4365 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4366 PSA_KDF_STEP_SALT,
4367 salt->x, salt->len ) );
4368 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4369 PSA_KDF_STEP_SECRET,
4370 handle ) );
4371 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4372 PSA_KDF_STEP_INFO,
4373 label->x, label->len ) );
4374 }
4375 else
4376 {
4377 // legacy
4378 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4379 salt->x, salt->len,
4380 label->x, label->len,
4381 requested_capacity ) );
4382 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004383 PSA_ASSERT( psa_get_generator_capacity( &generator,
4384 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004385 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004386
4387 /* Expansion phase. */
4388 while( current_capacity > 0 )
4389 {
4390 size_t read_size = sizeof( output_buffer );
4391 if( read_size > current_capacity )
4392 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01004393 PSA_ASSERT( psa_generator_read( &generator,
4394 output_buffer,
4395 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004396 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01004397 PSA_ASSERT( psa_get_generator_capacity( &generator,
4398 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004399 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004400 }
4401
4402 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004403 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004404 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004405
Gilles Peskine8817f612018-12-18 00:18:46 +01004406 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004407
4408exit:
4409 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004410 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02004411 mbedtls_psa_crypto_free( );
4412}
4413/* END_CASE */
4414
4415/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004416void derive_key_exercise( int alg_arg,
4417 data_t *key_data,
4418 data_t *salt,
4419 data_t *label,
4420 int derived_type_arg,
4421 int derived_bits_arg,
4422 int derived_usage_arg,
4423 int derived_alg_arg )
4424{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004425 psa_key_handle_t base_handle = 0;
4426 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004427 psa_algorithm_t alg = alg_arg;
4428 psa_key_type_t derived_type = derived_type_arg;
4429 size_t derived_bits = derived_bits_arg;
4430 psa_key_usage_t derived_usage = derived_usage_arg;
4431 psa_algorithm_t derived_alg = derived_alg_arg;
4432 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
4433 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004434 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004435 psa_key_type_t got_type;
4436 size_t got_bits;
4437
Gilles Peskine8817f612018-12-18 00:18:46 +01004438 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004439
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004440 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004441 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004442 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
4443 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
4444 key_data->x,
4445 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004446
4447 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004448 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4449 salt->x, salt->len,
4450 label->x, label->len,
4451 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004452 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004453 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004454 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4455 PSA_ASSERT( psa_generator_import_key( derived_handle,
4456 derived_type,
4457 derived_bits,
4458 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004459
4460 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01004461 PSA_ASSERT( psa_get_key_information( derived_handle,
4462 &got_type,
4463 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004464 TEST_EQUAL( got_type, derived_type );
4465 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004466
4467 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004468 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004469 goto exit;
4470
4471exit:
4472 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004473 psa_destroy_key( base_handle );
4474 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004475 mbedtls_psa_crypto_free( );
4476}
4477/* END_CASE */
4478
4479/* BEGIN_CASE */
4480void derive_key_export( int alg_arg,
4481 data_t *key_data,
4482 data_t *salt,
4483 data_t *label,
4484 int bytes1_arg,
4485 int bytes2_arg )
4486{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004487 psa_key_handle_t base_handle = 0;
4488 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004489 psa_algorithm_t alg = alg_arg;
4490 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004491 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004492 size_t bytes2 = bytes2_arg;
4493 size_t capacity = bytes1 + bytes2;
4494 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004495 uint8_t *output_buffer = NULL;
4496 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00004497 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004498 size_t length;
4499
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004500 ASSERT_ALLOC( output_buffer, capacity );
4501 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004502 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004503
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004504 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004505 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004506 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
4507 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
4508 key_data->x,
4509 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004510
4511 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004512 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4513 salt->x, salt->len,
4514 label->x, label->len,
4515 capacity ) );
4516 PSA_ASSERT( psa_generator_read( &generator,
4517 output_buffer,
4518 capacity ) );
4519 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004520
4521 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004522 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4523 salt->x, salt->len,
4524 label->x, label->len,
4525 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004526 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004527 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004528 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4529 PSA_ASSERT( psa_generator_import_key( derived_handle,
4530 PSA_KEY_TYPE_RAW_DATA,
4531 derived_bits,
4532 &generator ) );
4533 PSA_ASSERT( psa_export_key( derived_handle,
4534 export_buffer, bytes1,
4535 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004536 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004537 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004538 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004539 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4540 PSA_ASSERT( psa_generator_import_key( derived_handle,
4541 PSA_KEY_TYPE_RAW_DATA,
4542 PSA_BYTES_TO_BITS( bytes2 ),
4543 &generator ) );
4544 PSA_ASSERT( psa_export_key( derived_handle,
4545 export_buffer + bytes1, bytes2,
4546 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004547 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004548
4549 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004550 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4551 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004552
4553exit:
4554 mbedtls_free( output_buffer );
4555 mbedtls_free( export_buffer );
4556 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004557 psa_destroy_key( base_handle );
4558 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004559 mbedtls_psa_crypto_free( );
4560}
4561/* END_CASE */
4562
4563/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004564void key_agreement_setup( int alg_arg,
4565 int our_key_type_arg, data_t *our_key_data,
4566 data_t *peer_key_data,
4567 int expected_status_arg )
4568{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004569 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004570 psa_algorithm_t alg = alg_arg;
4571 psa_key_type_t our_key_type = our_key_type_arg;
4572 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004573 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004574
Gilles Peskine8817f612018-12-18 00:18:46 +01004575 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004576
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004577 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004578 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004579 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4580 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4581 our_key_data->x,
4582 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004583
Gilles Peskine969c5d62019-01-16 15:53:06 +01004584 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4585 TEST_EQUAL( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004586 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004587 peer_key_data->x, peer_key_data->len ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004588 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004589
4590exit:
4591 psa_generator_abort( &generator );
4592 psa_destroy_key( our_key );
4593 mbedtls_psa_crypto_free( );
4594}
4595/* END_CASE */
4596
4597/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004598void key_agreement_capacity( int alg_arg,
4599 int our_key_type_arg, data_t *our_key_data,
4600 data_t *peer_key_data,
4601 int expected_capacity_arg )
4602{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004603 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004604 psa_algorithm_t alg = alg_arg;
4605 psa_key_type_t our_key_type = our_key_type_arg;
4606 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004607 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004608 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004609 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004610
Gilles Peskine8817f612018-12-18 00:18:46 +01004611 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004612
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004613 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004614 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004615 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4616 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4617 our_key_data->x,
4618 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004619
Gilles Peskine969c5d62019-01-16 15:53:06 +01004620 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4621 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004622 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004623 peer_key_data->x, peer_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004624
Gilles Peskinebf491972018-10-25 22:36:12 +02004625 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004626 PSA_ASSERT( psa_get_generator_capacity(
4627 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004628 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004629
Gilles Peskinebf491972018-10-25 22:36:12 +02004630 /* Test the actual capacity by reading the output. */
4631 while( actual_capacity > sizeof( output ) )
4632 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004633 PSA_ASSERT( psa_generator_read( &generator,
4634 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004635 actual_capacity -= sizeof( output );
4636 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004637 PSA_ASSERT( psa_generator_read( &generator,
4638 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004639 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004640 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004641
Gilles Peskine59685592018-09-18 12:11:34 +02004642exit:
4643 psa_generator_abort( &generator );
4644 psa_destroy_key( our_key );
4645 mbedtls_psa_crypto_free( );
4646}
4647/* END_CASE */
4648
4649/* BEGIN_CASE */
4650void key_agreement_output( int alg_arg,
4651 int our_key_type_arg, data_t *our_key_data,
4652 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004653 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004654{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004655 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004656 psa_algorithm_t alg = alg_arg;
4657 psa_key_type_t our_key_type = our_key_type_arg;
4658 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004659 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004660 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004661
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004662 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4663 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004664
Gilles Peskine8817f612018-12-18 00:18:46 +01004665 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004666
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004667 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004668 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004669 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4670 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4671 our_key_data->x,
4672 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004673
Gilles Peskine969c5d62019-01-16 15:53:06 +01004674 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4675 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004676 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004677 peer_key_data->x, peer_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004678
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004679 PSA_ASSERT( psa_generator_read( &generator,
4680 actual_output,
4681 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004682 ASSERT_COMPARE( actual_output, expected_output1->len,
4683 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004684 if( expected_output2->len != 0 )
4685 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004686 PSA_ASSERT( psa_generator_read( &generator,
4687 actual_output,
4688 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004689 ASSERT_COMPARE( actual_output, expected_output2->len,
4690 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004691 }
Gilles Peskine59685592018-09-18 12:11:34 +02004692
4693exit:
4694 psa_generator_abort( &generator );
4695 psa_destroy_key( our_key );
4696 mbedtls_psa_crypto_free( );
4697 mbedtls_free( actual_output );
4698}
4699/* END_CASE */
4700
4701/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004702void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004703{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004704 size_t bytes = bytes_arg;
4705 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004706 unsigned char *output = NULL;
4707 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004708 size_t i;
4709 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004710
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004711 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4712 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004713 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004714
Gilles Peskine8817f612018-12-18 00:18:46 +01004715 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004716
Gilles Peskinea50d7392018-06-21 10:22:13 +02004717 /* Run several times, to ensure that every output byte will be
4718 * nonzero at least once with overwhelming probability
4719 * (2^(-8*number_of_runs)). */
4720 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004721 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004722 if( bytes != 0 )
4723 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004724 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004725
4726 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004727 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4728 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004729
4730 for( i = 0; i < bytes; i++ )
4731 {
4732 if( output[i] != 0 )
4733 ++changed[i];
4734 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004735 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004736
4737 /* Check that every byte was changed to nonzero at least once. This
4738 * validates that psa_generate_random is overwriting every byte of
4739 * the output buffer. */
4740 for( i = 0; i < bytes; i++ )
4741 {
4742 TEST_ASSERT( changed[i] != 0 );
4743 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004744
4745exit:
4746 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004747 mbedtls_free( output );
4748 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004749}
4750/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004751
4752/* BEGIN_CASE */
4753void generate_key( int type_arg,
4754 int bits_arg,
4755 int usage_arg,
4756 int alg_arg,
4757 int expected_status_arg )
4758{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004759 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004760 psa_key_type_t type = type_arg;
4761 psa_key_usage_t usage = usage_arg;
4762 size_t bits = bits_arg;
4763 psa_algorithm_t alg = alg_arg;
4764 psa_status_t expected_status = expected_status_arg;
4765 psa_key_type_t got_type;
4766 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004767 psa_status_t expected_info_status =
David Saadab4ecc272019-02-14 13:48:10 +02004768 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Jaeden Amero70261c52019-01-04 11:47:20 +00004769 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004770
Gilles Peskine8817f612018-12-18 00:18:46 +01004771 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004772
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004773 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004774 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004775 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004776
4777 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004778 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
4779 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004780
4781 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004782 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
4783 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004784 if( expected_info_status != PSA_SUCCESS )
4785 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004786 TEST_EQUAL( got_type, type );
4787 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004788
Gilles Peskine818ca122018-06-20 18:16:48 +02004789 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004790 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004791 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004792
4793exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004794 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004795 mbedtls_psa_crypto_free( );
4796}
4797/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004798
Darryl Greend49a4992018-06-18 17:27:26 +01004799/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4800void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4801 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004802 int alg_arg, int generation_method,
4803 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004804{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004805 psa_key_handle_t handle = 0;
4806 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004807 psa_key_type_t type = (psa_key_type_t) type_arg;
4808 psa_key_type_t type_get;
4809 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00004810 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
4811 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004812 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4813 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004814 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00004815 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4816 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004817 unsigned char *first_export = NULL;
4818 unsigned char *second_export = NULL;
4819 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4820 size_t first_exported_length;
4821 size_t second_exported_length;
4822
4823 ASSERT_ALLOC( first_export, export_size );
4824 ASSERT_ALLOC( second_export, export_size );
4825
Gilles Peskine8817f612018-12-18 00:18:46 +01004826 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004827
Gilles Peskine8817f612018-12-18 00:18:46 +01004828 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
Gilles Peskine8817f612018-12-18 00:18:46 +01004829 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004830 psa_key_policy_set_usage( &policy_set, policy_usage,
4831 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004832 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004833
Darryl Green0c6575a2018-11-07 16:05:30 +00004834 switch( generation_method )
4835 {
4836 case IMPORT_KEY:
4837 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004838 PSA_ASSERT( psa_import_key( handle, type,
4839 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004840 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004841
Darryl Green0c6575a2018-11-07 16:05:30 +00004842 case GENERATE_KEY:
4843 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004844 PSA_ASSERT( psa_generate_key( handle, type, bits,
4845 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004846 break;
4847
4848 case DERIVE_KEY:
4849 /* Create base key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004850 PSA_ASSERT( psa_allocate_key( &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004851 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4852 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004853 PSA_ASSERT( psa_set_key_policy(
4854 base_key, &base_policy_set ) );
4855 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4856 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004857 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004858 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4859 base_policy_alg,
4860 NULL, 0, NULL, 0,
4861 export_size ) );
4862 PSA_ASSERT( psa_generator_import_key(
4863 handle, PSA_KEY_TYPE_RAW_DATA,
4864 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004865 break;
4866 }
Darryl Greend49a4992018-06-18 17:27:26 +01004867
4868 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004869 TEST_EQUAL( psa_export_key( handle,
4870 first_export, export_size,
4871 &first_exported_length ),
4872 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004873
4874 /* Shutdown and restart */
4875 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004876 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004877
Darryl Greend49a4992018-06-18 17:27:26 +01004878 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004879 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4880 &handle ) );
4881 PSA_ASSERT( psa_get_key_information(
4882 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004883 TEST_EQUAL( type_get, type );
4884 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004885
Gilles Peskine8817f612018-12-18 00:18:46 +01004886 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004887 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4888 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004889
4890 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004891 TEST_EQUAL( psa_export_key( handle,
4892 second_export, export_size,
4893 &second_exported_length ),
4894 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004895
Darryl Green0c6575a2018-11-07 16:05:30 +00004896 if( export_status == PSA_SUCCESS )
4897 {
4898 ASSERT_COMPARE( first_export, first_exported_length,
4899 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004900
Darryl Green0c6575a2018-11-07 16:05:30 +00004901 switch( generation_method )
4902 {
4903 case IMPORT_KEY:
4904 ASSERT_COMPARE( data->x, data->len,
4905 first_export, first_exported_length );
4906 break;
4907 default:
4908 break;
4909 }
4910 }
4911
4912 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004913 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004914 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004915
4916exit:
4917 mbedtls_free( first_export );
4918 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004919 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004920 mbedtls_psa_crypto_free();
4921}
4922/* END_CASE */