blob: 668b5a0d89461fe3cb1b4137bfed5426d1c7b1d6 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02009#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +020010#include "mbedtls/oid.h"
11
Gilles Peskinee59236f2018-01-27 23:32:46 +010012#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Jaeden Amerof24c7f82018-06-27 17:20:43 +010014/** An invalid export length that will never be set by psa_export_key(). */
15static const size_t INVALID_EXPORT_LENGTH = ~0U;
16
Gilles Peskinef426e0f2019-02-25 17:42:03 +010017/* A hash algorithm that is known to be supported.
18 *
19 * This is used in some smoke tests.
20 */
21#if defined(MBEDTLS_MD2_C)
22#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
23#elif defined(MBEDTLS_MD4_C)
24#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
25#elif defined(MBEDTLS_MD5_C)
26#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
27/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
28 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
29 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
30 * implausible anyway. */
31#elif defined(MBEDTLS_SHA1_C)
32#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
33#elif defined(MBEDTLS_SHA256_C)
34#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
35#elif defined(MBEDTLS_SHA512_C)
36#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
37#elif defined(MBEDTLS_SHA3_C)
38#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
39#else
40#undef KNOWN_SUPPORTED_HASH_ALG
41#endif
42
43/* A block cipher that is known to be supported.
44 *
45 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
46 */
47#if defined(MBEDTLS_AES_C)
48#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
49#elif defined(MBEDTLS_ARIA_C)
50#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
51#elif defined(MBEDTLS_CAMELLIA_C)
52#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
53#undef KNOWN_SUPPORTED_BLOCK_CIPHER
54#endif
55
56/* A MAC mode that is known to be supported.
57 *
58 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
59 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
60 *
61 * This is used in some smoke tests.
62 */
63#if defined(KNOWN_SUPPORTED_HASH_ALG)
64#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
65#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
66#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
67#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
68#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
69#else
70#undef KNOWN_SUPPORTED_MAC_ALG
71#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
72#endif
73
74/* A cipher algorithm and key type that are known to be supported.
75 *
76 * This is used in some smoke tests.
77 */
78#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
79#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
80#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
81#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
82#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
83#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
84#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
85#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
86#else
87#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
88#endif
89#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
90#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
91#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
92#elif defined(MBEDTLS_RC4_C)
93#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
94#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
95#else
96#undef KNOWN_SUPPORTED_CIPHER_ALG
97#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
98#endif
99
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200100/** Test if a buffer contains a constant byte value.
101 *
102 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200103 *
104 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200105 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200106 * \param size Size of the buffer in bytes.
107 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200108 * \return 1 if the buffer is all-bits-zero.
109 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200110 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200111static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200112{
113 size_t i;
114 for( i = 0; i < size; i++ )
115 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200116 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200117 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200118 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200119 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200120}
Gilles Peskine818ca122018-06-20 18:16:48 +0200121
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200122/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
123static int asn1_write_10x( unsigned char **p,
124 unsigned char *start,
125 size_t bits,
126 unsigned char x )
127{
128 int ret;
129 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200130 if( bits == 0 )
131 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
132 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200133 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300134 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200135 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
136 *p -= len;
137 ( *p )[len-1] = x;
138 if( bits % 8 == 0 )
139 ( *p )[1] |= 1;
140 else
141 ( *p )[0] |= 1 << ( bits % 8 );
142 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
143 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
144 MBEDTLS_ASN1_INTEGER ) );
145 return( len );
146}
147
148static int construct_fake_rsa_key( unsigned char *buffer,
149 size_t buffer_size,
150 unsigned char **p,
151 size_t bits,
152 int keypair )
153{
154 size_t half_bits = ( bits + 1 ) / 2;
155 int ret;
156 int len = 0;
157 /* Construct something that looks like a DER encoding of
158 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
159 * RSAPrivateKey ::= SEQUENCE {
160 * version Version,
161 * modulus INTEGER, -- n
162 * publicExponent INTEGER, -- e
163 * privateExponent INTEGER, -- d
164 * prime1 INTEGER, -- p
165 * prime2 INTEGER, -- q
166 * exponent1 INTEGER, -- d mod (p-1)
167 * exponent2 INTEGER, -- d mod (q-1)
168 * coefficient INTEGER, -- (inverse of q) mod p
169 * otherPrimeInfos OtherPrimeInfos OPTIONAL
170 * }
171 * Or, for a public key, the same structure with only
172 * version, modulus and publicExponent.
173 */
174 *p = buffer + buffer_size;
175 if( keypair )
176 {
177 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
178 asn1_write_10x( p, buffer, half_bits, 1 ) );
179 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
180 asn1_write_10x( p, buffer, half_bits, 1 ) );
181 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
182 asn1_write_10x( p, buffer, half_bits, 1 ) );
183 MBEDTLS_ASN1_CHK_ADD( len, /* q */
184 asn1_write_10x( p, buffer, half_bits, 1 ) );
185 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
186 asn1_write_10x( p, buffer, half_bits, 3 ) );
187 MBEDTLS_ASN1_CHK_ADD( len, /* d */
188 asn1_write_10x( p, buffer, bits, 1 ) );
189 }
190 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
191 asn1_write_10x( p, buffer, 17, 1 ) );
192 MBEDTLS_ASN1_CHK_ADD( len, /* n */
193 asn1_write_10x( p, buffer, bits, 1 ) );
194 if( keypair )
195 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
196 mbedtls_asn1_write_int( p, buffer, 0 ) );
197 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
198 {
199 const unsigned char tag =
200 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
201 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
202 }
203 return( len );
204}
205
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100206int exercise_mac_setup( psa_key_type_t key_type,
207 const unsigned char *key_bytes,
208 size_t key_length,
209 psa_algorithm_t alg,
210 psa_mac_operation_t *operation,
211 psa_status_t *status )
212{
213 psa_key_handle_t handle = 0;
214 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
215
216 PSA_ASSERT( psa_allocate_key( &handle ) );
217 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
218 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
219 PSA_ASSERT( psa_import_key( handle, key_type, key_bytes, key_length ) );
220
221 *status = psa_mac_sign_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100222 /* Whether setup succeeded or failed, abort must succeed. */
223 PSA_ASSERT( psa_mac_abort( operation ) );
224 /* If setup failed, reproduce the failure, so that the caller can
225 * test the resulting state of the operation object. */
226 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100227 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100228 TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ),
229 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100230 }
231
232 psa_destroy_key( handle );
233 return( 1 );
234
235exit:
236 psa_destroy_key( handle );
237 return( 0 );
238}
239
240int exercise_cipher_setup( psa_key_type_t key_type,
241 const unsigned char *key_bytes,
242 size_t key_length,
243 psa_algorithm_t alg,
244 psa_cipher_operation_t *operation,
245 psa_status_t *status )
246{
247 psa_key_handle_t handle = 0;
248 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
249
250 PSA_ASSERT( psa_allocate_key( &handle ) );
251 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
252 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
253 PSA_ASSERT( psa_import_key( handle, key_type, key_bytes, key_length ) );
254
255 *status = psa_cipher_encrypt_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100256 /* Whether setup succeeded or failed, abort must succeed. */
257 PSA_ASSERT( psa_cipher_abort( operation ) );
258 /* If setup failed, reproduce the failure, so that the caller can
259 * test the resulting state of the operation object. */
260 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100261 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100262 TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ),
263 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100264 }
265
266 psa_destroy_key( handle );
267 return( 1 );
268
269exit:
270 psa_destroy_key( handle );
271 return( 0 );
272}
273
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100274static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200275 psa_key_usage_t usage,
276 psa_algorithm_t alg )
277{
Jaeden Amero769ce272019-01-04 11:48:03 +0000278 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200279 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200280 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200281 size_t mac_length = sizeof( mac );
282
283 if( usage & PSA_KEY_USAGE_SIGN )
284 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100285 PSA_ASSERT( psa_mac_sign_setup( &operation,
286 handle, alg ) );
287 PSA_ASSERT( psa_mac_update( &operation,
288 input, sizeof( input ) ) );
289 PSA_ASSERT( psa_mac_sign_finish( &operation,
290 mac, sizeof( mac ),
291 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200292 }
293
294 if( usage & PSA_KEY_USAGE_VERIFY )
295 {
296 psa_status_t verify_status =
297 ( usage & PSA_KEY_USAGE_SIGN ?
298 PSA_SUCCESS :
299 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100300 PSA_ASSERT( psa_mac_verify_setup( &operation,
301 handle, alg ) );
302 PSA_ASSERT( psa_mac_update( &operation,
303 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100304 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
305 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200306 }
307
308 return( 1 );
309
310exit:
311 psa_mac_abort( &operation );
312 return( 0 );
313}
314
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100315static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200316 psa_key_usage_t usage,
317 psa_algorithm_t alg )
318{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000319 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200320 unsigned char iv[16] = {0};
321 size_t iv_length = sizeof( iv );
322 const unsigned char plaintext[16] = "Hello, world...";
323 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
324 size_t ciphertext_length = sizeof( ciphertext );
325 unsigned char decrypted[sizeof( ciphertext )];
326 size_t part_length;
327
328 if( usage & PSA_KEY_USAGE_ENCRYPT )
329 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100330 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
331 handle, alg ) );
332 PSA_ASSERT( psa_cipher_generate_iv( &operation,
333 iv, sizeof( iv ),
334 &iv_length ) );
335 PSA_ASSERT( psa_cipher_update( &operation,
336 plaintext, sizeof( plaintext ),
337 ciphertext, sizeof( ciphertext ),
338 &ciphertext_length ) );
339 PSA_ASSERT( psa_cipher_finish( &operation,
340 ciphertext + ciphertext_length,
341 sizeof( ciphertext ) - ciphertext_length,
342 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200343 ciphertext_length += part_length;
344 }
345
346 if( usage & PSA_KEY_USAGE_DECRYPT )
347 {
348 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700349 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200350 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
351 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200352 size_t bits;
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100353 TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200354 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
355 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100356 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
357 handle, alg ) );
358 PSA_ASSERT( psa_cipher_set_iv( &operation,
359 iv, iv_length ) );
360 PSA_ASSERT( psa_cipher_update( &operation,
361 ciphertext, ciphertext_length,
362 decrypted, sizeof( decrypted ),
363 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200364 status = psa_cipher_finish( &operation,
365 decrypted + part_length,
366 sizeof( decrypted ) - part_length,
367 &part_length );
368 /* For a stream cipher, all inputs are valid. For a block cipher,
369 * if the input is some aribtrary data rather than an actual
370 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700371 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700372 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100373 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200374 else
375 TEST_ASSERT( status == PSA_SUCCESS ||
376 status == PSA_ERROR_INVALID_PADDING );
377 }
378
379 return( 1 );
380
381exit:
382 psa_cipher_abort( &operation );
383 return( 0 );
384}
385
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100386static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200387 psa_key_usage_t usage,
388 psa_algorithm_t alg )
389{
390 unsigned char nonce[16] = {0};
391 size_t nonce_length = sizeof( nonce );
392 unsigned char plaintext[16] = "Hello, world...";
393 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
394 size_t ciphertext_length = sizeof( ciphertext );
395 size_t plaintext_length = sizeof( ciphertext );
396
397 if( usage & PSA_KEY_USAGE_ENCRYPT )
398 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100399 PSA_ASSERT( psa_aead_encrypt( handle, alg,
400 nonce, nonce_length,
401 NULL, 0,
402 plaintext, sizeof( plaintext ),
403 ciphertext, sizeof( ciphertext ),
404 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200405 }
406
407 if( usage & PSA_KEY_USAGE_DECRYPT )
408 {
409 psa_status_t verify_status =
410 ( usage & PSA_KEY_USAGE_ENCRYPT ?
411 PSA_SUCCESS :
412 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100413 TEST_EQUAL( psa_aead_decrypt( handle, alg,
414 nonce, nonce_length,
415 NULL, 0,
416 ciphertext, ciphertext_length,
417 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100418 &plaintext_length ),
419 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200420 }
421
422 return( 1 );
423
424exit:
425 return( 0 );
426}
427
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100428static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200429 psa_key_usage_t usage,
430 psa_algorithm_t alg )
431{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200432 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
433 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200434 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200435 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100436 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
437
438 /* If the policy allows signing with any hash, just pick one. */
439 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
440 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100441#if defined(KNOWN_SUPPORTED_HASH_ALG)
442 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
443 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100444#else
445 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100446 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100447#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100448 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200449
450 if( usage & PSA_KEY_USAGE_SIGN )
451 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200452 /* Some algorithms require the payload to have the size of
453 * the hash encoded in the algorithm. Use this input size
454 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200455 if( hash_alg != 0 )
456 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100457 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
458 payload, payload_length,
459 signature, sizeof( signature ),
460 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200461 }
462
463 if( usage & PSA_KEY_USAGE_VERIFY )
464 {
465 psa_status_t verify_status =
466 ( usage & PSA_KEY_USAGE_SIGN ?
467 PSA_SUCCESS :
468 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100469 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
470 payload, payload_length,
471 signature, signature_length ),
472 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200473 }
474
475 return( 1 );
476
477exit:
478 return( 0 );
479}
480
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100481static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200482 psa_key_usage_t usage,
483 psa_algorithm_t alg )
484{
485 unsigned char plaintext[256] = "Hello, world...";
486 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
487 size_t ciphertext_length = sizeof( ciphertext );
488 size_t plaintext_length = 16;
489
490 if( usage & PSA_KEY_USAGE_ENCRYPT )
491 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100492 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
493 plaintext, plaintext_length,
494 NULL, 0,
495 ciphertext, sizeof( ciphertext ),
496 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200497 }
498
499 if( usage & PSA_KEY_USAGE_DECRYPT )
500 {
501 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100502 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200503 ciphertext, ciphertext_length,
504 NULL, 0,
505 plaintext, sizeof( plaintext ),
506 &plaintext_length );
507 TEST_ASSERT( status == PSA_SUCCESS ||
508 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
509 ( status == PSA_ERROR_INVALID_ARGUMENT ||
510 status == PSA_ERROR_INVALID_PADDING ) ) );
511 }
512
513 return( 1 );
514
515exit:
516 return( 0 );
517}
Gilles Peskine02b75072018-07-01 22:31:34 +0200518
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100519static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200520 psa_key_usage_t usage,
521 psa_algorithm_t alg )
522{
523 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
524 unsigned char label[16] = "This is a label.";
525 size_t label_length = sizeof( label );
526 unsigned char seed[16] = "abcdefghijklmnop";
527 size_t seed_length = sizeof( seed );
528 unsigned char output[1];
529
530 if( usage & PSA_KEY_USAGE_DERIVE )
531 {
Gilles Peskineb70a0fd2019-01-07 22:59:38 +0100532 if( PSA_ALG_IS_HKDF( alg ) )
533 {
534 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
535 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
536 PSA_KDF_STEP_SALT,
537 label,
538 label_length ) );
539 PSA_ASSERT( psa_key_derivation_input_key( &generator,
540 PSA_KDF_STEP_SECRET,
541 handle ) );
542 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
543 PSA_KDF_STEP_INFO,
544 seed,
545 seed_length ) );
546 }
547 else
548 {
549 // legacy
550 PSA_ASSERT( psa_key_derivation( &generator,
551 handle, alg,
552 label, label_length,
553 seed, seed_length,
554 sizeof( output ) ) );
555 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100556 PSA_ASSERT( psa_generator_read( &generator,
557 output,
558 sizeof( output ) ) );
559 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200560 }
561
562 return( 1 );
563
564exit:
565 return( 0 );
566}
567
Gilles Peskinec7998b72018-11-07 18:45:02 +0100568/* We need two keys to exercise key agreement. Exercise the
569 * private key against its own public key. */
570static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +0100571 psa_key_handle_t handle )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100572{
573 psa_key_type_t private_key_type;
574 psa_key_type_t public_key_type;
575 size_t key_bits;
576 uint8_t *public_key = NULL;
577 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200578 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinec7998b72018-11-07 18:45:02 +0100579 * psa_key_agreement fails. This isn't fully satisfactory, but it's
580 * good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200581 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100582
Gilles Peskine8817f612018-12-18 00:18:46 +0100583 PSA_ASSERT( psa_get_key_information( handle,
584 &private_key_type,
585 &key_bits ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100586 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
587 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
588 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100589 PSA_ASSERT( psa_export_public_key( handle,
590 public_key, public_key_length,
591 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100592
Gilles Peskine969c5d62019-01-16 15:53:06 +0100593 status = psa_key_agreement( generator, PSA_KDF_STEP_SECRET, handle,
594 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100595exit:
596 mbedtls_free( public_key );
597 return( status );
598}
599
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200600/* We need two keys to exercise key agreement. Exercise the
601 * private key against its own public key. */
602static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
603 psa_key_handle_t handle )
604{
605 psa_key_type_t private_key_type;
606 psa_key_type_t public_key_type;
607 size_t key_bits;
608 uint8_t *public_key = NULL;
609 size_t public_key_length;
610 uint8_t output[1024];
611 size_t output_length;
612 /* Return GENERIC_ERROR if something other than the final call to
613 * psa_key_agreement fails. This isn't fully satisfactory, but it's
614 * good enough: callers will report it as a failed test anyway. */
615 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
616
617 PSA_ASSERT( psa_get_key_information( handle,
618 &private_key_type,
619 &key_bits ) );
620 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
621 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
622 ASSERT_ALLOC( public_key, public_key_length );
623 PSA_ASSERT( psa_export_public_key( handle,
624 public_key, public_key_length,
625 &public_key_length ) );
626
627 status = psa_key_agreement_raw_shared_secret(
628 alg, handle,
629 public_key, public_key_length,
630 output, sizeof( output ), &output_length );
631exit:
632 mbedtls_free( public_key );
633 return( status );
634}
635
636static int exercise_raw_key_agreement_key( psa_key_handle_t handle,
637 psa_key_usage_t usage,
638 psa_algorithm_t alg )
639{
640 int ok = 0;
641
642 if( usage & PSA_KEY_USAGE_DERIVE )
643 {
644 /* We need two keys to exercise key agreement. Exercise the
645 * private key against its own public key. */
646 PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) );
647 }
648 ok = 1;
649
650exit:
651 return( ok );
652}
653
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100654static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200655 psa_key_usage_t usage,
656 psa_algorithm_t alg )
657{
658 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200659 unsigned char output[1];
660 int ok = 0;
661
662 if( usage & PSA_KEY_USAGE_DERIVE )
663 {
664 /* We need two keys to exercise key agreement. Exercise the
665 * private key against its own public key. */
Gilles Peskine969c5d62019-01-16 15:53:06 +0100666 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
667 PSA_ASSERT( key_agreement_with_self( &generator, handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100668 PSA_ASSERT( psa_generator_read( &generator,
669 output,
670 sizeof( output ) ) );
671 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200672 }
673 ok = 1;
674
675exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200676 return( ok );
677}
678
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200679static int is_oid_of_key_type( psa_key_type_t type,
680 const uint8_t *oid, size_t oid_length )
681{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200682 const uint8_t *expected_oid = NULL;
683 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200684#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200685 if( PSA_KEY_TYPE_IS_RSA( type ) )
686 {
687 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
688 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
689 }
690 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200691#endif /* MBEDTLS_RSA_C */
692#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200693 if( PSA_KEY_TYPE_IS_ECC( type ) )
694 {
695 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
696 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
697 }
698 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200699#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200700 {
701 char message[40];
702 mbedtls_snprintf( message, sizeof( message ),
703 "OID not known for key type=0x%08lx",
704 (unsigned long) type );
705 test_fail( message, __LINE__, __FILE__ );
706 return( 0 );
707 }
708
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200709 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200710 return( 1 );
711
712exit:
713 return( 0 );
714}
715
716static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
717 size_t min_bits, size_t max_bits,
718 int must_be_odd )
719{
720 size_t len;
721 size_t actual_bits;
722 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100723 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100724 MBEDTLS_ASN1_INTEGER ),
725 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200726 /* Tolerate a slight departure from DER encoding:
727 * - 0 may be represented by an empty string or a 1-byte string.
728 * - The sign bit may be used as a value bit. */
729 if( ( len == 1 && ( *p )[0] == 0 ) ||
730 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
731 {
732 ++( *p );
733 --len;
734 }
735 if( min_bits == 0 && len == 0 )
736 return( 1 );
737 msb = ( *p )[0];
738 TEST_ASSERT( msb != 0 );
739 actual_bits = 8 * ( len - 1 );
740 while( msb != 0 )
741 {
742 msb >>= 1;
743 ++actual_bits;
744 }
745 TEST_ASSERT( actual_bits >= min_bits );
746 TEST_ASSERT( actual_bits <= max_bits );
747 if( must_be_odd )
748 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
749 *p += len;
750 return( 1 );
751exit:
752 return( 0 );
753}
754
755static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
756 size_t *len,
757 unsigned char n, unsigned char tag )
758{
759 int ret;
760 ret = mbedtls_asn1_get_tag( p, end, len,
761 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
762 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
763 if( ret != 0 )
764 return( ret );
765 end = *p + *len;
766 ret = mbedtls_asn1_get_tag( p, end, len, tag );
767 if( ret != 0 )
768 return( ret );
769 if( *p + *len != end )
770 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
771 return( 0 );
772}
773
774static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
775 uint8_t *exported, size_t exported_length )
776{
777 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100778 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200779 else
780 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200781
782#if defined(MBEDTLS_DES_C)
783 if( type == PSA_KEY_TYPE_DES )
784 {
785 /* Check the parity bits. */
786 unsigned i;
787 for( i = 0; i < bits / 8; i++ )
788 {
789 unsigned bit_count = 0;
790 unsigned m;
791 for( m = 1; m <= 0x100; m <<= 1 )
792 {
793 if( exported[i] & m )
794 ++bit_count;
795 }
796 TEST_ASSERT( bit_count % 2 != 0 );
797 }
798 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200799 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200800#endif
801
802#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
803 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
804 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200805 uint8_t *p = exported;
806 uint8_t *end = exported + exported_length;
807 size_t len;
808 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200809 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200810 * modulus INTEGER, -- n
811 * publicExponent INTEGER, -- e
812 * privateExponent INTEGER, -- d
813 * prime1 INTEGER, -- p
814 * prime2 INTEGER, -- q
815 * exponent1 INTEGER, -- d mod (p-1)
816 * exponent2 INTEGER, -- d mod (q-1)
817 * coefficient INTEGER, -- (inverse of q) mod p
818 * }
819 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100820 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
821 MBEDTLS_ASN1_SEQUENCE |
822 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
823 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200824 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
825 goto exit;
826 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
827 goto exit;
828 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
829 goto exit;
830 /* Require d to be at least half the size of n. */
831 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
832 goto exit;
833 /* Require p and q to be at most half the size of n, rounded up. */
834 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
835 goto exit;
836 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
837 goto exit;
838 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
839 goto exit;
840 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
841 goto exit;
842 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
843 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100844 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100845 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200846 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200847#endif /* MBEDTLS_RSA_C */
848
849#if defined(MBEDTLS_ECP_C)
850 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
851 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100852 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100853 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100854 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200855 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200856#endif /* MBEDTLS_ECP_C */
857
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200858 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
859 {
860 uint8_t *p = exported;
861 uint8_t *end = exported + exported_length;
862 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200863#if defined(MBEDTLS_RSA_C)
864 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
865 {
866 /* RSAPublicKey ::= SEQUENCE {
867 * modulus INTEGER, -- n
868 * publicExponent INTEGER } -- e
869 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100870 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
871 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100872 MBEDTLS_ASN1_CONSTRUCTED ),
873 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100874 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200875 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
876 goto exit;
877 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
878 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100879 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200880 }
881 else
882#endif /* MBEDTLS_RSA_C */
883#if defined(MBEDTLS_ECP_C)
884 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
885 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000886 /* The representation of an ECC public key is:
887 * - The byte 0x04;
888 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
889 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
890 * - where m is the bit size associated with the curve.
Jaeden Amero6b196002019-01-10 10:23:21 +0000891 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100892 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
893 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200894 }
895 else
896#endif /* MBEDTLS_ECP_C */
897 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100898 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200899 mbedtls_snprintf( message, sizeof( message ),
900 "No sanity check for public key type=0x%08lx",
901 (unsigned long) type );
902 test_fail( message, __LINE__, __FILE__ );
903 return( 0 );
904 }
905 }
906 else
907
908 {
909 /* No sanity checks for other types */
910 }
911
912 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200913
914exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200915 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200916}
917
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100918static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200919 psa_key_usage_t usage )
920{
921 psa_key_type_t type;
922 size_t bits;
923 uint8_t *exported = NULL;
924 size_t exported_size = 0;
925 size_t exported_length = 0;
926 int ok = 0;
927
Gilles Peskine8817f612018-12-18 00:18:46 +0100928 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200929
930 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
931 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200932 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100933 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
934 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200935 return( 1 );
936 }
937
Gilles Peskined14664a2018-08-10 19:07:32 +0200938 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200939 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200940
Gilles Peskine8817f612018-12-18 00:18:46 +0100941 PSA_ASSERT( psa_export_key( handle,
942 exported, exported_size,
943 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200944 ok = exported_key_sanity_check( type, bits, exported, exported_length );
945
946exit:
947 mbedtls_free( exported );
948 return( ok );
949}
950
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100951static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200952{
953 psa_key_type_t type;
954 psa_key_type_t public_type;
955 size_t bits;
956 uint8_t *exported = NULL;
957 size_t exported_size = 0;
958 size_t exported_length = 0;
959 int ok = 0;
960
Gilles Peskine8817f612018-12-18 00:18:46 +0100961 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200962 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
963 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100964 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100965 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200966 return( 1 );
967 }
968
969 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
970 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200971 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200972
Gilles Peskine8817f612018-12-18 00:18:46 +0100973 PSA_ASSERT( psa_export_public_key( handle,
974 exported, exported_size,
975 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200976 ok = exported_key_sanity_check( public_type, bits,
977 exported, exported_length );
978
979exit:
980 mbedtls_free( exported );
981 return( ok );
982}
983
Gilles Peskinec9516fb2019-02-05 20:32:06 +0100984/** Do smoke tests on a key.
985 *
986 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
987 * sign/verify, or derivation) that is permitted according to \p usage.
988 * \p usage and \p alg should correspond to the expected policy on the
989 * key.
990 *
991 * Export the key if permitted by \p usage, and check that the output
992 * looks sensible. If \p usage forbids export, check that
993 * \p psa_export_key correctly rejects the attempt. If the key is
994 * asymmetric, also check \p psa_export_public_key.
995 *
996 * If the key fails the tests, this function calls the test framework's
997 * `test_fail` function and returns false. Otherwise this function returns
998 * true. Therefore it should be used as follows:
999 * ```
1000 * if( ! exercise_key( ... ) ) goto exit;
1001 * ```
1002 *
1003 * \param handle The key to exercise. It should be capable of performing
1004 * \p alg.
1005 * \param usage The usage flags to assume.
1006 * \param alg The algorithm to exercise.
1007 *
1008 * \retval 0 The key failed the smoke tests.
1009 * \retval 1 The key passed the smoke tests.
1010 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001011static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +02001012 psa_key_usage_t usage,
1013 psa_algorithm_t alg )
1014{
1015 int ok;
1016 if( alg == 0 )
1017 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1018 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001019 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001020 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001021 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001022 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001023 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001024 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001025 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001026 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001027 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001028 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001029 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001030 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
1031 ok = exercise_raw_key_agreement_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001032 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001033 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001034 else
1035 {
1036 char message[40];
1037 mbedtls_snprintf( message, sizeof( message ),
1038 "No code to exercise alg=0x%08lx",
1039 (unsigned long) alg );
1040 test_fail( message, __LINE__, __FILE__ );
1041 ok = 0;
1042 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001043
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001044 ok = ok && exercise_export_key( handle, usage );
1045 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +02001046
Gilles Peskine02b75072018-07-01 22:31:34 +02001047 return( ok );
1048}
1049
Gilles Peskine10df3412018-10-25 22:35:43 +02001050static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1051 psa_algorithm_t alg )
1052{
1053 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1054 {
1055 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1056 PSA_KEY_USAGE_VERIFY :
1057 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
1058 }
1059 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1060 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1061 {
1062 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1063 PSA_KEY_USAGE_ENCRYPT :
1064 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1065 }
1066 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1067 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1068 {
1069 return( PSA_KEY_USAGE_DERIVE );
1070 }
1071 else
1072 {
1073 return( 0 );
1074 }
1075
1076}
Darryl Green0c6575a2018-11-07 16:05:30 +00001077
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001078/* An overapproximation of the amount of storage needed for a key of the
1079 * given type and with the given content. The API doesn't make it easy
1080 * to find a good value for the size. The current implementation doesn't
1081 * care about the value anyway. */
1082#define KEY_BITS_FROM_DATA( type, data ) \
1083 ( data )->len
1084
Darryl Green0c6575a2018-11-07 16:05:30 +00001085typedef enum {
1086 IMPORT_KEY = 0,
1087 GENERATE_KEY = 1,
1088 DERIVE_KEY = 2
1089} generate_method;
1090
Gilles Peskinee59236f2018-01-27 23:32:46 +01001091/* END_HEADER */
1092
1093/* BEGIN_DEPENDENCIES
1094 * depends_on:MBEDTLS_PSA_CRYPTO_C
1095 * END_DEPENDENCIES
1096 */
1097
1098/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001099void static_checks( )
1100{
1101 size_t max_truncated_mac_size =
1102 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1103
1104 /* Check that the length for a truncated MAC always fits in the algorithm
1105 * encoding. The shifted mask is the maximum truncated value. The
1106 * untruncated algorithm may be one byte larger. */
1107 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
1108}
1109/* END_CASE */
1110
1111/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001112void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001113{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001114 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001115 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001116 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001117
Gilles Peskine8817f612018-12-18 00:18:46 +01001118 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001119
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001120 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001121 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001122 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001123 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001124 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001125
1126exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001127 mbedtls_psa_crypto_free( );
1128}
1129/* END_CASE */
1130
1131/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +01001132void import_twice( int alg_arg, int usage_arg,
1133 int type1_arg, data_t *data1,
1134 int expected_import1_status_arg,
1135 int type2_arg, data_t *data2,
1136 int expected_import2_status_arg )
1137{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001138 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +01001139 psa_algorithm_t alg = alg_arg;
1140 psa_key_usage_t usage = usage_arg;
1141 psa_key_type_t type1 = type1_arg;
1142 psa_status_t expected_import1_status = expected_import1_status_arg;
1143 psa_key_type_t type2 = type2_arg;
1144 psa_status_t expected_import2_status = expected_import2_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00001145 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea4261682018-12-03 11:34:01 +01001146 psa_status_t status;
1147
Gilles Peskine8817f612018-12-18 00:18:46 +01001148 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +01001149
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001150 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +01001151 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001152 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +01001153
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001154 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001155 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001156 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001157 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +01001158
1159 if( expected_import1_status == PSA_SUCCESS ||
1160 expected_import2_status == PSA_SUCCESS )
1161 {
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001162 if( ! exercise_key( handle, usage, alg ) )
1163 goto exit;
Gilles Peskinea4261682018-12-03 11:34:01 +01001164 }
1165
1166exit:
1167 mbedtls_psa_crypto_free( );
1168}
1169/* END_CASE */
1170
1171/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001172void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1173{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001174 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001175 size_t bits = bits_arg;
1176 psa_status_t expected_status = expected_status_arg;
1177 psa_status_t status;
1178 psa_key_type_t type =
1179 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
1180 size_t buffer_size = /* Slight overapproximations */
1181 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001182 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001183 unsigned char *p;
1184 int ret;
1185 size_t length;
1186
Gilles Peskine8817f612018-12-18 00:18:46 +01001187 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001188 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001189
1190 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1191 bits, keypair ) ) >= 0 );
1192 length = ret;
1193
1194 /* Try importing the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001195 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001196 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001197 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001198 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001199 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001200
1201exit:
1202 mbedtls_free( buffer );
1203 mbedtls_psa_crypto_free( );
1204}
1205/* END_CASE */
1206
1207/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001208void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001209 int type_arg,
1210 int alg_arg,
1211 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001212 int expected_bits,
1213 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001214 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001215 int canonical_input )
1216{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001217 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001218 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001219 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001220 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001221 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001222 unsigned char *exported = NULL;
1223 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001224 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001225 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001226 size_t reexported_length;
1227 psa_key_type_t got_type;
1228 size_t got_bits;
Jaeden Amero70261c52019-01-04 11:47:20 +00001229 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001230
Moran Pekercb088e72018-07-17 17:36:59 +03001231 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001232 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001233 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001234 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001235 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001236
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001237 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001238 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001239 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001240
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001241 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
David Saadab4ecc272019-02-14 13:48:10 +02001242 PSA_ERROR_DOES_NOT_EXIST );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001243
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001244 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001245 PSA_ASSERT( psa_import_key( handle, type,
1246 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001247
1248 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001249 PSA_ASSERT( psa_get_key_information( handle,
1250 &got_type,
1251 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001252 TEST_EQUAL( got_type, type );
1253 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001254
1255 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001256 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001257 exported, export_size,
1258 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001259 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001260
1261 /* The exported length must be set by psa_export_key() to a value between 0
1262 * and export_size. On errors, the exported length must be 0. */
1263 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1264 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1265 TEST_ASSERT( exported_length <= export_size );
1266
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001267 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001268 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001269 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001270 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001271 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001272 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001273 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001274
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001275 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001276 goto exit;
1277
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001278 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001279 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001280 else
1281 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001282 psa_key_handle_t handle2;
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001283 PSA_ASSERT( psa_allocate_key( &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001284 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001285
Gilles Peskine8817f612018-12-18 00:18:46 +01001286 PSA_ASSERT( psa_import_key( handle2, type,
1287 exported,
1288 exported_length ) );
1289 PSA_ASSERT( psa_export_key( handle2,
1290 reexported,
1291 export_size,
1292 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001293 ASSERT_COMPARE( exported, exported_length,
1294 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001295 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001296 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001297 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001298
1299destroy:
1300 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001301 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001302 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1303 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001304
1305exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001306 mbedtls_free( exported );
1307 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001308 mbedtls_psa_crypto_free( );
1309}
1310/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001311
Moran Pekerf709f4a2018-06-06 17:26:04 +03001312/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001313void import_key_nonempty_slot( )
1314{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001315 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001316 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1317 psa_status_t status;
1318 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001319 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001320
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001321 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001322
Moran Peker28a38e62018-11-07 16:18:24 +02001323 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001324 PSA_ASSERT( psa_import_key( handle, type,
1325 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001326
1327 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001328 status = psa_import_key( handle, type, data, sizeof( data ) );
David Saadab4ecc272019-02-14 13:48:10 +02001329 TEST_EQUAL( status, PSA_ERROR_ALREADY_EXISTS );
Moran Peker28a38e62018-11-07 16:18:24 +02001330
1331exit:
1332 mbedtls_psa_crypto_free( );
1333}
1334/* END_CASE */
1335
1336/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001337void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001338{
1339 psa_status_t status;
1340 unsigned char *exported = NULL;
1341 size_t export_size = 0;
1342 size_t exported_length = INVALID_EXPORT_LENGTH;
1343 psa_status_t expected_export_status = expected_export_status_arg;
1344
Gilles Peskine8817f612018-12-18 00:18:46 +01001345 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001346
1347 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001348 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001349 exported, export_size,
1350 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001351 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001352
1353exit:
1354 mbedtls_psa_crypto_free( );
1355}
1356/* END_CASE */
1357
1358/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001359void export_with_no_key_activity( )
1360{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001361 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001362 psa_algorithm_t alg = PSA_ALG_CTR;
1363 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001364 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001365 unsigned char *exported = NULL;
1366 size_t export_size = 0;
1367 size_t exported_length = INVALID_EXPORT_LENGTH;
1368
Gilles Peskine8817f612018-12-18 00:18:46 +01001369 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001370
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001371 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001372 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001373 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001374
1375 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001376 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001377 exported, export_size,
1378 &exported_length );
David Saadab4ecc272019-02-14 13:48:10 +02001379 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Peker34550092018-11-07 16:19:34 +02001380
1381exit:
1382 mbedtls_psa_crypto_free( );
1383}
1384/* END_CASE */
1385
1386/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001387void cipher_with_no_key_activity( )
1388{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001389 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001390 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001391 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001392 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001393 int exercise_alg = PSA_ALG_CTR;
1394
Gilles Peskine8817f612018-12-18 00:18:46 +01001395 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001396
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001397 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001398 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001399 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001400
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001401 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
David Saadab4ecc272019-02-14 13:48:10 +02001402 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Pekerce500072018-11-07 16:20:07 +02001403
1404exit:
1405 psa_cipher_abort( &operation );
1406 mbedtls_psa_crypto_free( );
1407}
1408/* END_CASE */
1409
1410/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001411void export_after_import_failure( data_t *data, int type_arg,
1412 int expected_import_status_arg )
1413{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001414 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001415 psa_key_type_t type = type_arg;
1416 psa_status_t status;
1417 unsigned char *exported = NULL;
1418 size_t export_size = 0;
1419 psa_status_t expected_import_status = expected_import_status_arg;
1420 size_t exported_length = INVALID_EXPORT_LENGTH;
1421
Gilles Peskine8817f612018-12-18 00:18:46 +01001422 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001423
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001424 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001425
Moran Peker34550092018-11-07 16:19:34 +02001426 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001427 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001428 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001429 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001430
1431 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001432 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001433 exported, export_size,
1434 &exported_length );
David Saadab4ecc272019-02-14 13:48:10 +02001435 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Peker34550092018-11-07 16:19:34 +02001436
1437exit:
1438 mbedtls_psa_crypto_free( );
1439}
1440/* END_CASE */
1441
1442/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001443void cipher_after_import_failure( data_t *data, int type_arg,
1444 int expected_import_status_arg )
1445{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001446 psa_key_handle_t handle = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001447 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001448 psa_key_type_t type = type_arg;
1449 psa_status_t status;
1450 psa_status_t expected_import_status = expected_import_status_arg;
1451 int exercise_alg = PSA_ALG_CTR;
1452
Gilles Peskine8817f612018-12-18 00:18:46 +01001453 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001454
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001455 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001456
Moran Pekerce500072018-11-07 16:20:07 +02001457 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001458 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001459 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001460 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001461
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001462 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
David Saadab4ecc272019-02-14 13:48:10 +02001463 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Pekerce500072018-11-07 16:20:07 +02001464
1465exit:
1466 psa_cipher_abort( &operation );
1467 mbedtls_psa_crypto_free( );
1468}
1469/* END_CASE */
1470
1471/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001472void export_after_destroy_key( data_t *data, int type_arg )
1473{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001474 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001475 psa_key_type_t type = type_arg;
1476 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001477 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001478 psa_algorithm_t alg = PSA_ALG_CTR;
1479 unsigned char *exported = NULL;
1480 size_t export_size = 0;
1481 size_t exported_length = INVALID_EXPORT_LENGTH;
1482
Gilles Peskine8817f612018-12-18 00:18:46 +01001483 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001484
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001485 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001486 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001487 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001488 export_size = (ptrdiff_t) data->len;
1489 ASSERT_ALLOC( exported, export_size );
1490
1491 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001492 PSA_ASSERT( psa_import_key( handle, type,
1493 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001494
Gilles Peskine8817f612018-12-18 00:18:46 +01001495 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1496 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001497
1498 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001499 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001500
1501 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001502 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001503 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001504 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001505
1506exit:
1507 mbedtls_free( exported );
1508 mbedtls_psa_crypto_free( );
1509}
1510/* END_CASE */
1511
1512/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001513void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001514 int type_arg,
1515 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001516 int export_size_delta,
1517 int expected_export_status_arg,
1518 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001519{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001520 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001521 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001522 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001523 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001524 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001525 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001526 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001527 size_t exported_length = INVALID_EXPORT_LENGTH;
Jaeden Amero70261c52019-01-04 11:47:20 +00001528 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001529
Gilles Peskine8817f612018-12-18 00:18:46 +01001530 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001531
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001532 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001533 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001534 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001535
1536 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001537 PSA_ASSERT( psa_import_key( handle, type,
1538 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001539
Gilles Peskine49c25912018-10-29 15:15:31 +01001540 /* Export the public key */
1541 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001542 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001543 exported, export_size,
1544 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001545 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001546 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001547 {
1548 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1549 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001550 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001551 TEST_ASSERT( expected_public_key->len <=
1552 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001553 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1554 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001555 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001556
1557exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001558 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001559 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001560 mbedtls_psa_crypto_free( );
1561}
1562/* END_CASE */
1563
Gilles Peskine20035e32018-02-03 22:44:14 +01001564/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001565void import_and_exercise_key( data_t *data,
1566 int type_arg,
1567 int bits_arg,
1568 int alg_arg )
1569{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001570 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001571 psa_key_type_t type = type_arg;
1572 size_t bits = bits_arg;
1573 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001574 psa_key_usage_t usage = usage_to_exercise( type, alg );
Jaeden Amero70261c52019-01-04 11:47:20 +00001575 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001576 psa_key_type_t got_type;
1577 size_t got_bits;
1578 psa_status_t status;
1579
Gilles Peskine8817f612018-12-18 00:18:46 +01001580 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001581
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001582 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001583 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001584 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001585
1586 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001587 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001588 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001589
1590 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001591 PSA_ASSERT( psa_get_key_information( handle,
1592 &got_type,
1593 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001594 TEST_EQUAL( got_type, type );
1595 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001596
1597 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001598 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001599 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001600
1601exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001602 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001603 mbedtls_psa_crypto_free( );
1604}
1605/* END_CASE */
1606
1607/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001608void key_policy( int usage_arg, int alg_arg )
1609{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001610 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001611 psa_algorithm_t alg = alg_arg;
1612 psa_key_usage_t usage = usage_arg;
1613 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1614 unsigned char key[32] = {0};
Jaeden Amero70261c52019-01-04 11:47:20 +00001615 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
1616 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001617
1618 memset( key, 0x2a, sizeof( key ) );
1619
Gilles Peskine8817f612018-12-18 00:18:46 +01001620 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001621
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001622 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001623 psa_key_policy_set_usage( &policy_set, usage, alg );
1624
Gilles Peskinefe11b722018-12-18 00:24:04 +01001625 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1626 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001627 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001628
Gilles Peskine8817f612018-12-18 00:18:46 +01001629 PSA_ASSERT( psa_import_key( handle, key_type,
1630 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001631
Gilles Peskine8817f612018-12-18 00:18:46 +01001632 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001633
Gilles Peskinefe11b722018-12-18 00:24:04 +01001634 TEST_EQUAL( policy_get.usage, policy_set.usage );
1635 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001636
1637exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001638 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001639 mbedtls_psa_crypto_free( );
1640}
1641/* END_CASE */
1642
1643/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001644void key_policy_init( )
1645{
1646 /* Test each valid way of initializing the object, except for `= {0}`, as
1647 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1648 * though it's OK by the C standard. We could test for this, but we'd need
1649 * to supress the Clang warning for the test. */
1650 psa_key_policy_t func = psa_key_policy_init( );
1651 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1652 psa_key_policy_t zero;
1653
1654 memset( &zero, 0, sizeof( zero ) );
1655
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001656 /* A default key policy should not permit any usage. */
1657 TEST_EQUAL( psa_key_policy_get_usage( &func ), 0 );
1658 TEST_EQUAL( psa_key_policy_get_usage( &init ), 0 );
1659 TEST_EQUAL( psa_key_policy_get_usage( &zero ), 0 );
1660
1661 /* A default key policy should not permit any algorithm. */
1662 TEST_EQUAL( psa_key_policy_get_algorithm( &func ), 0 );
1663 TEST_EQUAL( psa_key_policy_get_algorithm( &init ), 0 );
1664 TEST_EQUAL( psa_key_policy_get_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001665}
1666/* END_CASE */
1667
1668/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001669void mac_key_policy( int policy_usage,
1670 int policy_alg,
1671 int key_type,
1672 data_t *key_data,
1673 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001674{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001675 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001676 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001677 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001678 psa_status_t status;
1679 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001680
Gilles Peskine8817f612018-12-18 00:18:46 +01001681 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001682
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001683 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001684 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001685 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001686
Gilles Peskine8817f612018-12-18 00:18:46 +01001687 PSA_ASSERT( psa_import_key( handle, key_type,
1688 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001689
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001690 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001691 if( policy_alg == exercise_alg &&
1692 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001693 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001694 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001695 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001696 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001697
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001698 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001699 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001700 if( policy_alg == exercise_alg &&
1701 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001702 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001703 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001704 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001705
1706exit:
1707 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001708 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001709 mbedtls_psa_crypto_free( );
1710}
1711/* END_CASE */
1712
1713/* BEGIN_CASE */
1714void cipher_key_policy( int policy_usage,
1715 int policy_alg,
1716 int key_type,
1717 data_t *key_data,
1718 int exercise_alg )
1719{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001720 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001721 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001722 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001723 psa_status_t status;
1724
Gilles Peskine8817f612018-12-18 00:18:46 +01001725 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001726
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001727 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001728 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001729 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001730
Gilles Peskine8817f612018-12-18 00:18:46 +01001731 PSA_ASSERT( psa_import_key( handle, key_type,
1732 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001733
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001734 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001735 if( policy_alg == exercise_alg &&
1736 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001737 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001738 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001739 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001740 psa_cipher_abort( &operation );
1741
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001742 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001743 if( policy_alg == exercise_alg &&
1744 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001745 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001746 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001747 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001748
1749exit:
1750 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001751 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001752 mbedtls_psa_crypto_free( );
1753}
1754/* END_CASE */
1755
1756/* BEGIN_CASE */
1757void aead_key_policy( int policy_usage,
1758 int policy_alg,
1759 int key_type,
1760 data_t *key_data,
1761 int nonce_length_arg,
1762 int tag_length_arg,
1763 int exercise_alg )
1764{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001765 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001766 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001767 psa_status_t status;
1768 unsigned char nonce[16] = {0};
1769 size_t nonce_length = nonce_length_arg;
1770 unsigned char tag[16];
1771 size_t tag_length = tag_length_arg;
1772 size_t output_length;
1773
1774 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1775 TEST_ASSERT( tag_length <= sizeof( tag ) );
1776
Gilles Peskine8817f612018-12-18 00:18:46 +01001777 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001778
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001779 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001780 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001781 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001782
Gilles Peskine8817f612018-12-18 00:18:46 +01001783 PSA_ASSERT( psa_import_key( handle, key_type,
1784 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001785
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001786 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001787 nonce, nonce_length,
1788 NULL, 0,
1789 NULL, 0,
1790 tag, tag_length,
1791 &output_length );
1792 if( policy_alg == exercise_alg &&
1793 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001794 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001795 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001796 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001797
1798 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001799 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001800 nonce, nonce_length,
1801 NULL, 0,
1802 tag, tag_length,
1803 NULL, 0,
1804 &output_length );
1805 if( policy_alg == exercise_alg &&
1806 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001807 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001808 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001809 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001810
1811exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001812 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001813 mbedtls_psa_crypto_free( );
1814}
1815/* END_CASE */
1816
1817/* BEGIN_CASE */
1818void asymmetric_encryption_key_policy( int policy_usage,
1819 int policy_alg,
1820 int key_type,
1821 data_t *key_data,
1822 int exercise_alg )
1823{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001824 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001825 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001826 psa_status_t status;
1827 size_t key_bits;
1828 size_t buffer_length;
1829 unsigned char *buffer = NULL;
1830 size_t output_length;
1831
Gilles Peskine8817f612018-12-18 00:18:46 +01001832 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001833
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001834 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001835 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001836 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001837
Gilles Peskine8817f612018-12-18 00:18:46 +01001838 PSA_ASSERT( psa_import_key( handle, key_type,
1839 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001840
Gilles Peskine8817f612018-12-18 00:18:46 +01001841 PSA_ASSERT( psa_get_key_information( handle,
1842 NULL,
1843 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001844 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1845 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001846 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001847
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001848 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001849 NULL, 0,
1850 NULL, 0,
1851 buffer, buffer_length,
1852 &output_length );
1853 if( policy_alg == exercise_alg &&
1854 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001855 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001856 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001857 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001858
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001859 if( buffer_length != 0 )
1860 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001861 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001862 buffer, buffer_length,
1863 NULL, 0,
1864 buffer, buffer_length,
1865 &output_length );
1866 if( policy_alg == exercise_alg &&
1867 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001868 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001869 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001870 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001871
1872exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001873 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001874 mbedtls_psa_crypto_free( );
1875 mbedtls_free( buffer );
1876}
1877/* END_CASE */
1878
1879/* BEGIN_CASE */
1880void asymmetric_signature_key_policy( int policy_usage,
1881 int policy_alg,
1882 int key_type,
1883 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001884 int exercise_alg,
1885 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001886{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001887 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001888 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001889 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001890 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1891 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1892 * compatible with the policy and `payload_length_arg` is supposed to be
1893 * a valid input length to sign. If `payload_length_arg <= 0`,
1894 * `exercise_alg` is supposed to be forbidden by the policy. */
1895 int compatible_alg = payload_length_arg > 0;
1896 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001897 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1898 size_t signature_length;
1899
Gilles Peskine8817f612018-12-18 00:18:46 +01001900 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001901
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001902 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001903 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001904 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001905
Gilles Peskine8817f612018-12-18 00:18:46 +01001906 PSA_ASSERT( psa_import_key( handle, key_type,
1907 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001908
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001909 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001910 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001911 signature, sizeof( signature ),
1912 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001913 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001914 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001915 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001916 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001917
1918 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001919 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001920 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001921 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001922 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001923 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001924 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001925 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001926
1927exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001928 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001929 mbedtls_psa_crypto_free( );
1930}
1931/* END_CASE */
1932
1933/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001934void derive_key_policy( int policy_usage,
1935 int policy_alg,
1936 int key_type,
1937 data_t *key_data,
1938 int exercise_alg )
1939{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001940 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001941 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001942 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1943 psa_status_t status;
1944
Gilles Peskine8817f612018-12-18 00:18:46 +01001945 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001946
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001947 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001948 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001949 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001950
Gilles Peskine8817f612018-12-18 00:18:46 +01001951 PSA_ASSERT( psa_import_key( handle, key_type,
1952 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001953
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001954 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001955 exercise_alg,
1956 NULL, 0,
1957 NULL, 0,
1958 1 );
1959 if( policy_alg == exercise_alg &&
1960 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001961 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001962 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001963 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001964
1965exit:
1966 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001967 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001968 mbedtls_psa_crypto_free( );
1969}
1970/* END_CASE */
1971
1972/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001973void agreement_key_policy( int policy_usage,
1974 int policy_alg,
1975 int key_type_arg,
1976 data_t *key_data,
1977 int exercise_alg )
1978{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001979 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001980 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001981 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001982 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1983 psa_status_t status;
1984
Gilles Peskine8817f612018-12-18 00:18:46 +01001985 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001986
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001987 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001988 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001989 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001990
Gilles Peskine8817f612018-12-18 00:18:46 +01001991 PSA_ASSERT( psa_import_key( handle, key_type,
1992 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001993
Gilles Peskine969c5d62019-01-16 15:53:06 +01001994 PSA_ASSERT( psa_key_derivation_setup( &generator, exercise_alg ) );
1995 status = key_agreement_with_self( &generator, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001996
Gilles Peskine01d718c2018-09-18 12:01:02 +02001997 if( policy_alg == exercise_alg &&
1998 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001999 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002000 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002001 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002002
2003exit:
2004 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002005 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002006 mbedtls_psa_crypto_free( );
2007}
2008/* END_CASE */
2009
2010/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002011void raw_agreement_key_policy( int policy_usage,
2012 int policy_alg,
2013 int key_type_arg,
2014 data_t *key_data,
2015 int exercise_alg )
2016{
2017 psa_key_handle_t handle = 0;
2018 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2019 psa_key_type_t key_type = key_type_arg;
2020 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
2021 psa_status_t status;
2022
2023 PSA_ASSERT( psa_crypto_init( ) );
2024
2025 PSA_ASSERT( psa_allocate_key( &handle ) );
2026 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
2027 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2028
2029 PSA_ASSERT( psa_import_key( handle, key_type,
2030 key_data->x, key_data->len ) );
2031
2032 status = raw_key_agreement_with_self( exercise_alg, handle );
2033
2034 if( policy_alg == exercise_alg &&
2035 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
2036 PSA_ASSERT( status );
2037 else
2038 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2039
2040exit:
2041 psa_generator_abort( &generator );
2042 psa_destroy_key( handle );
2043 mbedtls_psa_crypto_free( );
2044}
2045/* END_CASE */
2046
2047/* BEGIN_CASE */
Gilles Peskine57ab7212019-01-28 13:03:09 +01002048void copy_key_policy( int source_usage_arg, int source_alg_arg,
2049 int type_arg, data_t *material,
2050 int target_usage_arg, int target_alg_arg,
2051 int constraint_usage_arg, int constraint_alg_arg,
2052 int expected_usage_arg, int expected_alg_arg )
2053{
2054 psa_key_usage_t source_usage = source_usage_arg;
2055 psa_algorithm_t source_alg = source_alg_arg;
2056 psa_key_handle_t source_handle = 0;
2057 psa_key_policy_t source_policy = PSA_KEY_POLICY_INIT;
2058 psa_key_type_t source_type = type_arg;
2059 size_t source_bits;
2060 psa_key_usage_t target_usage = target_usage_arg;
2061 psa_algorithm_t target_alg = target_alg_arg;
2062 psa_key_handle_t target_handle = 0;
2063 psa_key_policy_t target_policy = PSA_KEY_POLICY_INIT;
2064 psa_key_type_t target_type;
2065 size_t target_bits;
2066 psa_key_usage_t constraint_usage = constraint_usage_arg;
2067 psa_algorithm_t constraint_alg = constraint_alg_arg;
2068 psa_key_policy_t constraint = PSA_KEY_POLICY_INIT;
2069 psa_key_policy_t *p_constraint = NULL;
2070 psa_key_usage_t expected_usage = expected_usage_arg;
2071 psa_algorithm_t expected_alg = expected_alg_arg;
2072 uint8_t *export_buffer = NULL;
2073
2074 if( constraint_usage_arg != -1 )
2075 {
2076 p_constraint = &constraint;
2077 psa_key_policy_set_usage( p_constraint,
2078 constraint_usage, constraint_alg );
2079 }
2080
2081 PSA_ASSERT( psa_crypto_init( ) );
2082
2083 /* Populate the source slot. */
2084 PSA_ASSERT( psa_allocate_key( &source_handle ) );
2085 psa_key_policy_set_usage( &source_policy, source_usage, source_alg );
2086 PSA_ASSERT( psa_set_key_policy( source_handle, &source_policy ) );
2087 PSA_ASSERT( psa_import_key( source_handle, source_type,
2088 material->x, material->len ) );
2089 PSA_ASSERT( psa_get_key_information( source_handle, NULL, &source_bits ) );
2090
2091 /* Prepare the target slot. */
2092 PSA_ASSERT( psa_allocate_key( &target_handle ) );
2093 psa_key_policy_set_usage( &target_policy, target_usage, target_alg );
2094 PSA_ASSERT( psa_set_key_policy( target_handle, &target_policy ) );
2095 target_policy = psa_key_policy_init();
2096
2097 /* Copy the key. */
2098 PSA_ASSERT( psa_copy_key( source_handle, target_handle, p_constraint ) );
2099
2100 /* Destroy the source to ensure that this doesn't affect the target. */
2101 PSA_ASSERT( psa_destroy_key( source_handle ) );
2102
2103 /* Test that the target slot has the expected content and policy. */
2104 PSA_ASSERT( psa_get_key_information( target_handle,
2105 &target_type, &target_bits ) );
2106 TEST_EQUAL( source_type, target_type );
2107 TEST_EQUAL( source_bits, target_bits );
2108 PSA_ASSERT( psa_get_key_policy( target_handle, &target_policy ) );
2109 TEST_EQUAL( expected_usage, psa_key_policy_get_usage( &target_policy ) );
2110 TEST_EQUAL( expected_alg, psa_key_policy_get_algorithm( &target_policy ) );
2111 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2112 {
2113 size_t length;
2114 ASSERT_ALLOC( export_buffer, material->len );
2115 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2116 material->len, &length ) );
2117 ASSERT_COMPARE( material->x, material->len,
2118 export_buffer, length );
2119 }
2120 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2121 goto exit;
2122
2123 PSA_ASSERT( psa_close_key( target_handle ) );
2124
2125exit:
2126 mbedtls_psa_crypto_free( );
2127 mbedtls_free( export_buffer );
2128}
2129/* END_CASE */
2130
2131/* BEGIN_CASE */
2132void copy_fail( int source_usage_arg, int source_alg_arg,
2133 int type_arg, data_t *material,
2134 int target_usage_arg, int target_alg_arg,
2135 int constraint_usage_arg, int constraint_alg_arg,
2136 int expected_status_arg )
2137{
2138 /* Test copy failure into an empty slot. There is a test for copy failure
2139 * into an occupied slot in
2140 * test_suite_psa_crypto_slot_management.function. */
2141
2142 psa_key_usage_t source_usage = source_usage_arg;
2143 psa_algorithm_t source_alg = source_alg_arg;
2144 psa_key_handle_t source_handle = 0;
2145 psa_key_policy_t source_policy = PSA_KEY_POLICY_INIT;
2146 psa_key_type_t source_type = type_arg;
2147 size_t source_bits;
2148 psa_key_usage_t target_usage = target_usage_arg;
2149 psa_algorithm_t target_alg = target_alg_arg;
2150 psa_key_handle_t target_handle = 0;
2151 psa_key_policy_t target_policy = PSA_KEY_POLICY_INIT;
2152 psa_key_type_t target_type;
2153 size_t target_bits;
2154 psa_key_usage_t constraint_usage = constraint_usage_arg;
2155 psa_algorithm_t constraint_alg = constraint_alg_arg;
2156 psa_key_policy_t constraint = PSA_KEY_POLICY_INIT;
2157 psa_key_policy_t *p_constraint = NULL;
2158 psa_status_t expected_status = expected_status_arg;
2159
2160 if( constraint_usage_arg != -1 )
2161 {
2162 p_constraint = &constraint;
2163 psa_key_policy_set_usage( p_constraint,
2164 constraint_usage, constraint_alg );
2165 }
2166
2167 PSA_ASSERT( psa_crypto_init( ) );
2168
2169 /* Populate the source slot. */
2170 PSA_ASSERT( psa_allocate_key( &source_handle ) );
2171 psa_key_policy_set_usage( &source_policy, source_usage, source_alg );
2172 PSA_ASSERT( psa_set_key_policy( source_handle, &source_policy ) );
2173 PSA_ASSERT( psa_import_key( source_handle, source_type,
2174 material->x, material->len ) );
2175 PSA_ASSERT( psa_get_key_information( source_handle, NULL, &source_bits ) );
2176
2177 /* Prepare the target slot. */
2178 PSA_ASSERT( psa_allocate_key( &target_handle ) );
2179 psa_key_policy_set_usage( &target_policy, target_usage, target_alg );
2180 PSA_ASSERT( psa_set_key_policy( target_handle, &target_policy ) );
2181 target_policy = psa_key_policy_init();
2182
2183 /* Copy the key. */
2184 TEST_EQUAL( psa_copy_key( source_handle, target_handle, p_constraint ),
2185 expected_status );
2186
2187 /* Test that the target slot is unaffected. */
2188 TEST_EQUAL( psa_get_key_information( target_handle,
2189 &target_type, &target_bits ),
David Saadab4ecc272019-02-14 13:48:10 +02002190 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002191 PSA_ASSERT( psa_get_key_policy( target_handle, &target_policy ) );
2192 TEST_EQUAL( target_usage, psa_key_policy_get_usage( &target_policy ) );
2193 TEST_EQUAL( target_alg, psa_key_policy_get_algorithm( &target_policy ) );
2194
2195exit:
2196 mbedtls_psa_crypto_free( );
2197}
2198/* END_CASE */
2199
2200/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002201void hash_operation_init( )
2202{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002203 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002204 /* Test each valid way of initializing the object, except for `= {0}`, as
2205 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2206 * though it's OK by the C standard. We could test for this, but we'd need
2207 * to supress the Clang warning for the test. */
2208 psa_hash_operation_t func = psa_hash_operation_init( );
2209 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2210 psa_hash_operation_t zero;
2211
2212 memset( &zero, 0, sizeof( zero ) );
2213
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002214 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002215 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2216 PSA_ERROR_BAD_STATE );
2217 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2218 PSA_ERROR_BAD_STATE );
2219 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2220 PSA_ERROR_BAD_STATE );
2221
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002222 /* A default hash operation should be abortable without error. */
2223 PSA_ASSERT( psa_hash_abort( &func ) );
2224 PSA_ASSERT( psa_hash_abort( &init ) );
2225 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002226}
2227/* END_CASE */
2228
2229/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002230void hash_setup( int alg_arg,
2231 int expected_status_arg )
2232{
2233 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002234 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002235 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002236 psa_status_t status;
2237
Gilles Peskine8817f612018-12-18 00:18:46 +01002238 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002239
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002240 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002241 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002242
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002243 /* Whether setup succeeded or failed, abort must succeed. */
2244 PSA_ASSERT( psa_hash_abort( &operation ) );
2245
2246 /* If setup failed, reproduce the failure, so as to
2247 * test the resulting state of the operation object. */
2248 if( status != PSA_SUCCESS )
2249 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2250
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002251 /* Now the operation object should be reusable. */
2252#if defined(KNOWN_SUPPORTED_HASH_ALG)
2253 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2254 PSA_ASSERT( psa_hash_abort( &operation ) );
2255#endif
2256
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002257exit:
2258 mbedtls_psa_crypto_free( );
2259}
2260/* END_CASE */
2261
2262/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002263void hash_bad_order( )
2264{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002265 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002266 unsigned char input[] = "";
2267 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002268 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002269 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2270 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2271 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002272 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002273 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002274 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002275
Gilles Peskine8817f612018-12-18 00:18:46 +01002276 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002277
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002278 /* Call setup twice in a row. */
2279 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2280 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2281 PSA_ERROR_BAD_STATE );
2282 PSA_ASSERT( psa_hash_abort( &operation ) );
2283
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002284 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002285 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002286 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002287 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002288
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002289 /* Call update after finish. */
2290 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2291 PSA_ASSERT( psa_hash_finish( &operation,
2292 hash, sizeof( hash ), &hash_len ) );
2293 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002294 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002295 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002296
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002297 /* Call verify without calling setup beforehand. */
2298 TEST_EQUAL( psa_hash_verify( &operation,
2299 valid_hash, sizeof( valid_hash ) ),
2300 PSA_ERROR_BAD_STATE );
2301 PSA_ASSERT( psa_hash_abort( &operation ) );
2302
2303 /* Call verify after finish. */
2304 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2305 PSA_ASSERT( psa_hash_finish( &operation,
2306 hash, sizeof( hash ), &hash_len ) );
2307 TEST_EQUAL( psa_hash_verify( &operation,
2308 valid_hash, sizeof( valid_hash ) ),
2309 PSA_ERROR_BAD_STATE );
2310 PSA_ASSERT( psa_hash_abort( &operation ) );
2311
2312 /* Call verify twice in a row. */
2313 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2314 PSA_ASSERT( psa_hash_verify( &operation,
2315 valid_hash, sizeof( valid_hash ) ) );
2316 TEST_EQUAL( psa_hash_verify( &operation,
2317 valid_hash, sizeof( valid_hash ) ),
2318 PSA_ERROR_BAD_STATE );
2319 PSA_ASSERT( psa_hash_abort( &operation ) );
2320
2321 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002322 TEST_EQUAL( psa_hash_finish( &operation,
2323 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002324 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002325 PSA_ASSERT( psa_hash_abort( &operation ) );
2326
2327 /* Call finish twice in a row. */
2328 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2329 PSA_ASSERT( psa_hash_finish( &operation,
2330 hash, sizeof( hash ), &hash_len ) );
2331 TEST_EQUAL( psa_hash_finish( &operation,
2332 hash, sizeof( hash ), &hash_len ),
2333 PSA_ERROR_BAD_STATE );
2334 PSA_ASSERT( psa_hash_abort( &operation ) );
2335
2336 /* Call finish after calling verify. */
2337 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2338 PSA_ASSERT( psa_hash_verify( &operation,
2339 valid_hash, sizeof( valid_hash ) ) );
2340 TEST_EQUAL( psa_hash_finish( &operation,
2341 hash, sizeof( hash ), &hash_len ),
2342 PSA_ERROR_BAD_STATE );
2343 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002344
2345exit:
2346 mbedtls_psa_crypto_free( );
2347}
2348/* END_CASE */
2349
itayzafrir27e69452018-11-01 14:26:34 +02002350/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2351void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002352{
2353 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002354 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2355 * appended to it */
2356 unsigned char hash[] = {
2357 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2358 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2359 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002360 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002361 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002362
Gilles Peskine8817f612018-12-18 00:18:46 +01002363 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002364
itayzafrir27e69452018-11-01 14:26:34 +02002365 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002366 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002367 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002368 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002369
itayzafrir27e69452018-11-01 14:26:34 +02002370 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002371 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002372 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002373 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002374
itayzafrir27e69452018-11-01 14:26:34 +02002375 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002376 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002377 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002378 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002379
itayzafrirec93d302018-10-18 18:01:10 +03002380exit:
2381 mbedtls_psa_crypto_free( );
2382}
2383/* END_CASE */
2384
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002385/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2386void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002387{
2388 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002389 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002390 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002391 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002392 size_t hash_len;
2393
Gilles Peskine8817f612018-12-18 00:18:46 +01002394 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002395
itayzafrir58028322018-10-25 10:22:01 +03002396 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002397 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002398 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002399 hash, expected_size - 1, &hash_len ),
2400 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002401
2402exit:
2403 mbedtls_psa_crypto_free( );
2404}
2405/* END_CASE */
2406
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002407/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2408void hash_clone_source_state( )
2409{
2410 psa_algorithm_t alg = PSA_ALG_SHA_256;
2411 unsigned char hash[PSA_HASH_MAX_SIZE];
2412 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2413 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2414 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2415 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2416 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2417 size_t hash_len;
2418
2419 PSA_ASSERT( psa_crypto_init( ) );
2420 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2421
2422 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2423 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2424 PSA_ASSERT( psa_hash_finish( &op_finished,
2425 hash, sizeof( hash ), &hash_len ) );
2426 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2427 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2428
2429 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2430 PSA_ERROR_BAD_STATE );
2431
2432 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2433 PSA_ASSERT( psa_hash_finish( &op_init,
2434 hash, sizeof( hash ), &hash_len ) );
2435 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2436 PSA_ASSERT( psa_hash_finish( &op_finished,
2437 hash, sizeof( hash ), &hash_len ) );
2438 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2439 PSA_ASSERT( psa_hash_finish( &op_aborted,
2440 hash, sizeof( hash ), &hash_len ) );
2441
2442exit:
2443 psa_hash_abort( &op_source );
2444 psa_hash_abort( &op_init );
2445 psa_hash_abort( &op_setup );
2446 psa_hash_abort( &op_finished );
2447 psa_hash_abort( &op_aborted );
2448 mbedtls_psa_crypto_free( );
2449}
2450/* END_CASE */
2451
2452/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2453void hash_clone_target_state( )
2454{
2455 psa_algorithm_t alg = PSA_ALG_SHA_256;
2456 unsigned char hash[PSA_HASH_MAX_SIZE];
2457 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2458 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2459 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2460 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2461 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2462 size_t hash_len;
2463
2464 PSA_ASSERT( psa_crypto_init( ) );
2465
2466 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2467 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2468 PSA_ASSERT( psa_hash_finish( &op_finished,
2469 hash, sizeof( hash ), &hash_len ) );
2470 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2471 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2472
2473 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2474 PSA_ASSERT( psa_hash_finish( &op_target,
2475 hash, sizeof( hash ), &hash_len ) );
2476
2477 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2478 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2479 PSA_ERROR_BAD_STATE );
2480 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2481 PSA_ERROR_BAD_STATE );
2482
2483exit:
2484 psa_hash_abort( &op_target );
2485 psa_hash_abort( &op_init );
2486 psa_hash_abort( &op_setup );
2487 psa_hash_abort( &op_finished );
2488 psa_hash_abort( &op_aborted );
2489 mbedtls_psa_crypto_free( );
2490}
2491/* END_CASE */
2492
itayzafrir58028322018-10-25 10:22:01 +03002493/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002494void mac_operation_init( )
2495{
Jaeden Amero252ef282019-02-15 14:05:35 +00002496 const uint8_t input[1] = { 0 };
2497
Jaeden Amero769ce272019-01-04 11:48:03 +00002498 /* Test each valid way of initializing the object, except for `= {0}`, as
2499 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2500 * though it's OK by the C standard. We could test for this, but we'd need
2501 * to supress the Clang warning for the test. */
2502 psa_mac_operation_t func = psa_mac_operation_init( );
2503 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2504 psa_mac_operation_t zero;
2505
2506 memset( &zero, 0, sizeof( zero ) );
2507
Jaeden Amero252ef282019-02-15 14:05:35 +00002508 /* A freshly-initialized MAC operation should not be usable. */
2509 TEST_EQUAL( psa_mac_update( &func,
2510 input, sizeof( input ) ),
2511 PSA_ERROR_BAD_STATE );
2512 TEST_EQUAL( psa_mac_update( &init,
2513 input, sizeof( input ) ),
2514 PSA_ERROR_BAD_STATE );
2515 TEST_EQUAL( psa_mac_update( &zero,
2516 input, sizeof( input ) ),
2517 PSA_ERROR_BAD_STATE );
2518
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002519 /* A default MAC operation should be abortable without error. */
2520 PSA_ASSERT( psa_mac_abort( &func ) );
2521 PSA_ASSERT( psa_mac_abort( &init ) );
2522 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002523}
2524/* END_CASE */
2525
2526/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002527void mac_setup( int key_type_arg,
2528 data_t *key,
2529 int alg_arg,
2530 int expected_status_arg )
2531{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002532 psa_key_type_t key_type = key_type_arg;
2533 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002534 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002535 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002536 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2537#if defined(KNOWN_SUPPORTED_MAC_ALG)
2538 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2539#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002540
Gilles Peskine8817f612018-12-18 00:18:46 +01002541 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002542
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002543 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2544 &operation, &status ) )
2545 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002546 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002547
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002548 /* The operation object should be reusable. */
2549#if defined(KNOWN_SUPPORTED_MAC_ALG)
2550 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2551 smoke_test_key_data,
2552 sizeof( smoke_test_key_data ),
2553 KNOWN_SUPPORTED_MAC_ALG,
2554 &operation, &status ) )
2555 goto exit;
2556 TEST_EQUAL( status, PSA_SUCCESS );
2557#endif
2558
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002559exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002560 mbedtls_psa_crypto_free( );
2561}
2562/* END_CASE */
2563
2564/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002565void mac_bad_order( )
2566{
2567 psa_key_handle_t handle = 0;
2568 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2569 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2570 const uint8_t key[] = {
2571 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2572 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2573 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
2574 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2575 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2576 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2577 size_t sign_mac_length = 0;
2578 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2579 const uint8_t verify_mac[] = {
2580 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2581 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2582 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2583
2584 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002585 PSA_ASSERT( psa_allocate_key( &handle ) );
2586 psa_key_policy_set_usage( &policy,
2587 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002588 alg );
2589 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2590
2591 PSA_ASSERT( psa_import_key( handle, key_type,
Jaeden Amero252ef282019-02-15 14:05:35 +00002592 key, sizeof(key) ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002593
Jaeden Amero252ef282019-02-15 14:05:35 +00002594 /* Call update without calling setup beforehand. */
2595 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2596 PSA_ERROR_BAD_STATE );
2597 PSA_ASSERT( psa_mac_abort( &operation ) );
2598
2599 /* Call sign finish without calling setup beforehand. */
2600 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2601 &sign_mac_length),
2602 PSA_ERROR_BAD_STATE );
2603 PSA_ASSERT( psa_mac_abort( &operation ) );
2604
2605 /* Call verify finish without calling setup beforehand. */
2606 TEST_EQUAL( psa_mac_verify_finish( &operation,
2607 verify_mac, sizeof( verify_mac ) ),
2608 PSA_ERROR_BAD_STATE );
2609 PSA_ASSERT( psa_mac_abort( &operation ) );
2610
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002611 /* Call setup twice in a row. */
2612 PSA_ASSERT( psa_mac_sign_setup( &operation,
2613 handle, alg ) );
2614 TEST_EQUAL( psa_mac_sign_setup( &operation,
2615 handle, alg ),
2616 PSA_ERROR_BAD_STATE );
2617 PSA_ASSERT( psa_mac_abort( &operation ) );
2618
Jaeden Amero252ef282019-02-15 14:05:35 +00002619 /* Call update after sign finish. */
2620 PSA_ASSERT( psa_mac_sign_setup( &operation,
2621 handle, alg ) );
2622 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2623 PSA_ASSERT( psa_mac_sign_finish( &operation,
2624 sign_mac, sizeof( sign_mac ),
2625 &sign_mac_length ) );
2626 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2627 PSA_ERROR_BAD_STATE );
2628 PSA_ASSERT( psa_mac_abort( &operation ) );
2629
2630 /* Call update after verify finish. */
2631 PSA_ASSERT( psa_mac_verify_setup( &operation,
2632 handle, alg ) );
2633 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2634 PSA_ASSERT( psa_mac_verify_finish( &operation,
2635 verify_mac, sizeof( verify_mac ) ) );
2636 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2637 PSA_ERROR_BAD_STATE );
2638 PSA_ASSERT( psa_mac_abort( &operation ) );
2639
2640 /* Call sign finish twice in a row. */
2641 PSA_ASSERT( psa_mac_sign_setup( &operation,
2642 handle, alg ) );
2643 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2644 PSA_ASSERT( psa_mac_sign_finish( &operation,
2645 sign_mac, sizeof( sign_mac ),
2646 &sign_mac_length ) );
2647 TEST_EQUAL( psa_mac_sign_finish( &operation,
2648 sign_mac, sizeof( sign_mac ),
2649 &sign_mac_length ),
2650 PSA_ERROR_BAD_STATE );
2651 PSA_ASSERT( psa_mac_abort( &operation ) );
2652
2653 /* Call verify finish twice in a row. */
2654 PSA_ASSERT( psa_mac_verify_setup( &operation,
2655 handle, alg ) );
2656 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2657 PSA_ASSERT( psa_mac_verify_finish( &operation,
2658 verify_mac, sizeof( verify_mac ) ) );
2659 TEST_EQUAL( psa_mac_verify_finish( &operation,
2660 verify_mac, sizeof( verify_mac ) ),
2661 PSA_ERROR_BAD_STATE );
2662 PSA_ASSERT( psa_mac_abort( &operation ) );
2663
2664 /* Setup sign but try verify. */
2665 PSA_ASSERT( psa_mac_sign_setup( &operation,
2666 handle, alg ) );
2667 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2668 TEST_EQUAL( psa_mac_verify_finish( &operation,
2669 verify_mac, sizeof( verify_mac ) ),
2670 PSA_ERROR_BAD_STATE );
2671 PSA_ASSERT( psa_mac_abort( &operation ) );
2672
2673 /* Setup verify but try sign. */
2674 PSA_ASSERT( psa_mac_verify_setup( &operation,
2675 handle, alg ) );
2676 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2677 TEST_EQUAL( psa_mac_sign_finish( &operation,
2678 sign_mac, sizeof( sign_mac ),
2679 &sign_mac_length ),
2680 PSA_ERROR_BAD_STATE );
2681 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002682
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002683exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002684 mbedtls_psa_crypto_free( );
2685}
2686/* END_CASE */
2687
2688/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002689void mac_sign( int key_type_arg,
2690 data_t *key,
2691 int alg_arg,
2692 data_t *input,
2693 data_t *expected_mac )
2694{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002695 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002696 psa_key_type_t key_type = key_type_arg;
2697 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002698 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002699 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002700 /* Leave a little extra room in the output buffer. At the end of the
2701 * test, we'll check that the implementation didn't overwrite onto
2702 * this extra room. */
2703 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2704 size_t mac_buffer_size =
2705 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2706 size_t mac_length = 0;
2707
2708 memset( actual_mac, '+', sizeof( actual_mac ) );
2709 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2710 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2711
Gilles Peskine8817f612018-12-18 00:18:46 +01002712 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002713
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002714 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002715 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002716 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002717
Gilles Peskine8817f612018-12-18 00:18:46 +01002718 PSA_ASSERT( psa_import_key( handle, key_type,
2719 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002720
2721 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002722 PSA_ASSERT( psa_mac_sign_setup( &operation,
2723 handle, alg ) );
2724 PSA_ASSERT( psa_mac_update( &operation,
2725 input->x, input->len ) );
2726 PSA_ASSERT( psa_mac_sign_finish( &operation,
2727 actual_mac, mac_buffer_size,
2728 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002729
2730 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002731 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2732 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002733
2734 /* Verify that the end of the buffer is untouched. */
2735 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2736 sizeof( actual_mac ) - mac_length ) );
2737
2738exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002739 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002740 mbedtls_psa_crypto_free( );
2741}
2742/* END_CASE */
2743
2744/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002745void mac_verify( int key_type_arg,
2746 data_t *key,
2747 int alg_arg,
2748 data_t *input,
2749 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002750{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002751 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002752 psa_key_type_t key_type = key_type_arg;
2753 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002754 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002755 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002756
Gilles Peskine69c12672018-06-28 00:07:19 +02002757 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2758
Gilles Peskine8817f612018-12-18 00:18:46 +01002759 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002760
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002761 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002762 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002763 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002764
Gilles Peskine8817f612018-12-18 00:18:46 +01002765 PSA_ASSERT( psa_import_key( handle, key_type,
2766 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002767
Gilles Peskine8817f612018-12-18 00:18:46 +01002768 PSA_ASSERT( psa_mac_verify_setup( &operation,
2769 handle, alg ) );
2770 PSA_ASSERT( psa_destroy_key( handle ) );
2771 PSA_ASSERT( psa_mac_update( &operation,
2772 input->x, input->len ) );
2773 PSA_ASSERT( psa_mac_verify_finish( &operation,
2774 expected_mac->x,
2775 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002776
2777exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002778 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002779 mbedtls_psa_crypto_free( );
2780}
2781/* END_CASE */
2782
2783/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002784void cipher_operation_init( )
2785{
Jaeden Ameroab439972019-02-15 14:12:05 +00002786 const uint8_t input[1] = { 0 };
2787 unsigned char output[1] = { 0 };
2788 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002789 /* Test each valid way of initializing the object, except for `= {0}`, as
2790 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2791 * though it's OK by the C standard. We could test for this, but we'd need
2792 * to supress the Clang warning for the test. */
2793 psa_cipher_operation_t func = psa_cipher_operation_init( );
2794 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2795 psa_cipher_operation_t zero;
2796
2797 memset( &zero, 0, sizeof( zero ) );
2798
Jaeden Ameroab439972019-02-15 14:12:05 +00002799 /* A freshly-initialized cipher operation should not be usable. */
2800 TEST_EQUAL( psa_cipher_update( &func,
2801 input, sizeof( input ),
2802 output, sizeof( output ),
2803 &output_length ),
2804 PSA_ERROR_BAD_STATE );
2805 TEST_EQUAL( psa_cipher_update( &init,
2806 input, sizeof( input ),
2807 output, sizeof( output ),
2808 &output_length ),
2809 PSA_ERROR_BAD_STATE );
2810 TEST_EQUAL( psa_cipher_update( &zero,
2811 input, sizeof( input ),
2812 output, sizeof( output ),
2813 &output_length ),
2814 PSA_ERROR_BAD_STATE );
2815
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002816 /* A default cipher operation should be abortable without error. */
2817 PSA_ASSERT( psa_cipher_abort( &func ) );
2818 PSA_ASSERT( psa_cipher_abort( &init ) );
2819 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002820}
2821/* END_CASE */
2822
2823/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002824void cipher_setup( int key_type_arg,
2825 data_t *key,
2826 int alg_arg,
2827 int expected_status_arg )
2828{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002829 psa_key_type_t key_type = key_type_arg;
2830 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002831 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002832 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002833 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002834#if defined(KNOWN_SUPPORTED_MAC_ALG)
2835 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2836#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002837
Gilles Peskine8817f612018-12-18 00:18:46 +01002838 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002839
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002840 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2841 &operation, &status ) )
2842 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002843 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002844
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002845 /* The operation object should be reusable. */
2846#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2847 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2848 smoke_test_key_data,
2849 sizeof( smoke_test_key_data ),
2850 KNOWN_SUPPORTED_CIPHER_ALG,
2851 &operation, &status ) )
2852 goto exit;
2853 TEST_EQUAL( status, PSA_SUCCESS );
2854#endif
2855
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002856exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002857 mbedtls_psa_crypto_free( );
2858}
2859/* END_CASE */
2860
2861/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002862void cipher_bad_order( )
2863{
2864 psa_key_handle_t handle = 0;
2865 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2866 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
2867 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2868 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2869 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2870 const uint8_t key[] = {
2871 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2872 0xaa, 0xaa, 0xaa, 0xaa };
2873 const uint8_t text[] = {
2874 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2875 0xbb, 0xbb, 0xbb, 0xbb };
2876 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2877 size_t length = 0;
2878
2879 PSA_ASSERT( psa_crypto_init( ) );
2880 PSA_ASSERT( psa_allocate_key( &handle ) );
2881 psa_key_policy_set_usage( &policy,
2882 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2883 alg );
2884 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2885 PSA_ASSERT( psa_import_key( handle, key_type,
2886 key, sizeof(key) ) );
2887
2888
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002889 /* Call encrypt setup twice in a row. */
2890 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2891 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2892 PSA_ERROR_BAD_STATE );
2893 PSA_ASSERT( psa_cipher_abort( &operation ) );
2894
2895 /* Call decrypt setup twice in a row. */
2896 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2897 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2898 PSA_ERROR_BAD_STATE );
2899 PSA_ASSERT( psa_cipher_abort( &operation ) );
2900
Jaeden Ameroab439972019-02-15 14:12:05 +00002901 /* Generate an IV without calling setup beforehand. */
2902 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2903 buffer, sizeof( buffer ),
2904 &length ),
2905 PSA_ERROR_BAD_STATE );
2906 PSA_ASSERT( psa_cipher_abort( &operation ) );
2907
2908 /* Generate an IV twice in a row. */
2909 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2910 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2911 buffer, sizeof( buffer ),
2912 &length ) );
2913 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2914 buffer, sizeof( buffer ),
2915 &length ),
2916 PSA_ERROR_BAD_STATE );
2917 PSA_ASSERT( psa_cipher_abort( &operation ) );
2918
2919 /* Generate an IV after it's already set. */
2920 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2921 PSA_ASSERT( psa_cipher_set_iv( &operation,
2922 iv, sizeof( iv ) ) );
2923 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2924 buffer, sizeof( buffer ),
2925 &length ),
2926 PSA_ERROR_BAD_STATE );
2927 PSA_ASSERT( psa_cipher_abort( &operation ) );
2928
2929 /* Set an IV without calling setup beforehand. */
2930 TEST_EQUAL( psa_cipher_set_iv( &operation,
2931 iv, sizeof( iv ) ),
2932 PSA_ERROR_BAD_STATE );
2933 PSA_ASSERT( psa_cipher_abort( &operation ) );
2934
2935 /* Set an IV after it's already set. */
2936 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2937 PSA_ASSERT( psa_cipher_set_iv( &operation,
2938 iv, sizeof( iv ) ) );
2939 TEST_EQUAL( psa_cipher_set_iv( &operation,
2940 iv, sizeof( iv ) ),
2941 PSA_ERROR_BAD_STATE );
2942 PSA_ASSERT( psa_cipher_abort( &operation ) );
2943
2944 /* Set an IV after it's already generated. */
2945 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2946 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2947 buffer, sizeof( buffer ),
2948 &length ) );
2949 TEST_EQUAL( psa_cipher_set_iv( &operation,
2950 iv, sizeof( iv ) ),
2951 PSA_ERROR_BAD_STATE );
2952 PSA_ASSERT( psa_cipher_abort( &operation ) );
2953
2954 /* Call update without calling setup beforehand. */
2955 TEST_EQUAL( psa_cipher_update( &operation,
2956 text, sizeof( text ),
2957 buffer, sizeof( buffer ),
2958 &length ),
2959 PSA_ERROR_BAD_STATE );
2960 PSA_ASSERT( psa_cipher_abort( &operation ) );
2961
2962 /* Call update without an IV where an IV is required. */
2963 TEST_EQUAL( psa_cipher_update( &operation,
2964 text, sizeof( text ),
2965 buffer, sizeof( buffer ),
2966 &length ),
2967 PSA_ERROR_BAD_STATE );
2968 PSA_ASSERT( psa_cipher_abort( &operation ) );
2969
2970 /* Call update after finish. */
2971 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2972 PSA_ASSERT( psa_cipher_set_iv( &operation,
2973 iv, sizeof( iv ) ) );
2974 PSA_ASSERT( psa_cipher_finish( &operation,
2975 buffer, sizeof( buffer ), &length ) );
2976 TEST_EQUAL( psa_cipher_update( &operation,
2977 text, sizeof( text ),
2978 buffer, sizeof( buffer ),
2979 &length ),
2980 PSA_ERROR_BAD_STATE );
2981 PSA_ASSERT( psa_cipher_abort( &operation ) );
2982
2983 /* Call finish without calling setup beforehand. */
2984 TEST_EQUAL( psa_cipher_finish( &operation,
2985 buffer, sizeof( buffer ), &length ),
2986 PSA_ERROR_BAD_STATE );
2987 PSA_ASSERT( psa_cipher_abort( &operation ) );
2988
2989 /* Call finish without an IV where an IV is required. */
2990 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2991 /* Not calling update means we are encrypting an empty buffer, which is OK
2992 * for cipher modes with padding. */
2993 TEST_EQUAL( psa_cipher_finish( &operation,
2994 buffer, sizeof( buffer ), &length ),
2995 PSA_ERROR_BAD_STATE );
2996 PSA_ASSERT( psa_cipher_abort( &operation ) );
2997
2998 /* Call finish twice in a row. */
2999 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3000 PSA_ASSERT( psa_cipher_set_iv( &operation,
3001 iv, sizeof( iv ) ) );
3002 PSA_ASSERT( psa_cipher_finish( &operation,
3003 buffer, sizeof( buffer ), &length ) );
3004 TEST_EQUAL( psa_cipher_finish( &operation,
3005 buffer, sizeof( buffer ), &length ),
3006 PSA_ERROR_BAD_STATE );
3007 PSA_ASSERT( psa_cipher_abort( &operation ) );
3008
3009exit:
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003010 mbedtls_psa_crypto_free( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003011}
3012/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003013
Gilles Peskine50e586b2018-06-08 14:28:46 +02003014/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003015void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003016 data_t *key,
3017 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003018 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003019{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003020 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003021 psa_status_t status;
3022 psa_key_type_t key_type = key_type_arg;
3023 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003024 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003025 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003026 size_t iv_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003027 unsigned char *output = NULL;
3028 size_t output_buffer_size = 0;
3029 size_t function_output_length = 0;
3030 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003031 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003032 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003033
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003034 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3035 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003036
Gilles Peskine8817f612018-12-18 00:18:46 +01003037 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003038
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003039 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003040 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003041 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003042
Gilles Peskine8817f612018-12-18 00:18:46 +01003043 PSA_ASSERT( psa_import_key( handle, key_type,
3044 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003045
Gilles Peskine8817f612018-12-18 00:18:46 +01003046 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3047 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003048
Gilles Peskine8817f612018-12-18 00:18:46 +01003049 PSA_ASSERT( psa_cipher_set_iv( &operation,
3050 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003051 output_buffer_size = ( (size_t) input->len +
3052 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003053 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003054
Gilles Peskine8817f612018-12-18 00:18:46 +01003055 PSA_ASSERT( psa_cipher_update( &operation,
3056 input->x, input->len,
3057 output, output_buffer_size,
3058 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003059 total_output_length += function_output_length;
3060 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003061 output + total_output_length,
3062 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003063 &function_output_length );
3064 total_output_length += function_output_length;
3065
Gilles Peskinefe11b722018-12-18 00:24:04 +01003066 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003067 if( expected_status == PSA_SUCCESS )
3068 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003069 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003070 ASSERT_COMPARE( expected_output->x, expected_output->len,
3071 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003072 }
3073
3074exit:
3075 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003076 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003077 mbedtls_psa_crypto_free( );
3078}
3079/* END_CASE */
3080
3081/* BEGIN_CASE */
3082void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
3083 data_t *key,
3084 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003085 int first_part_size_arg,
3086 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003087 data_t *expected_output )
3088{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003089 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003090 psa_key_type_t key_type = key_type_arg;
3091 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003092 size_t first_part_size = first_part_size_arg;
3093 size_t output1_length = output1_length_arg;
3094 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003095 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003096 size_t iv_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003097 unsigned char *output = NULL;
3098 size_t output_buffer_size = 0;
3099 size_t function_output_length = 0;
3100 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003101 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003102 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003103
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003104 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3105 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003106
Gilles Peskine8817f612018-12-18 00:18:46 +01003107 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003108
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003109 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003110 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003111 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003112
Gilles Peskine8817f612018-12-18 00:18:46 +01003113 PSA_ASSERT( psa_import_key( handle, key_type,
3114 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003115
Gilles Peskine8817f612018-12-18 00:18:46 +01003116 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3117 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003118
Gilles Peskine8817f612018-12-18 00:18:46 +01003119 PSA_ASSERT( psa_cipher_set_iv( &operation,
3120 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003121 output_buffer_size = ( (size_t) input->len +
3122 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003123 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003124
Gilles Peskinee0866522019-02-19 19:44:00 +01003125 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003126 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3127 output, output_buffer_size,
3128 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003129 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003130 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003131 PSA_ASSERT( psa_cipher_update( &operation,
3132 input->x + first_part_size,
3133 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003134 output + total_output_length,
3135 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003136 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003137 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003138 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003139 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003140 output + total_output_length,
3141 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003142 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003143 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003144 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003145
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003146 ASSERT_COMPARE( expected_output->x, expected_output->len,
3147 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003148
3149exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003150 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003151 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003152 mbedtls_psa_crypto_free( );
3153}
3154/* END_CASE */
3155
3156/* BEGIN_CASE */
3157void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003158 data_t *key,
3159 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003160 int first_part_size_arg,
3161 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003162 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003163{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003164 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003165
3166 psa_key_type_t key_type = key_type_arg;
3167 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003168 size_t first_part_size = first_part_size_arg;
3169 size_t output1_length = output1_length_arg;
3170 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003171 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003172 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003173 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003174 size_t output_buffer_size = 0;
3175 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003176 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003177 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003178 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003179
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003180 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3181 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003182
Gilles Peskine8817f612018-12-18 00:18:46 +01003183 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003184
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003185 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003186 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003187 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003188
Gilles Peskine8817f612018-12-18 00:18:46 +01003189 PSA_ASSERT( psa_import_key( handle, key_type,
3190 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003191
Gilles Peskine8817f612018-12-18 00:18:46 +01003192 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3193 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003194
Gilles Peskine8817f612018-12-18 00:18:46 +01003195 PSA_ASSERT( psa_cipher_set_iv( &operation,
3196 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003197
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003198 output_buffer_size = ( (size_t) input->len +
3199 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003200 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003201
Gilles Peskinee0866522019-02-19 19:44:00 +01003202 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003203 PSA_ASSERT( psa_cipher_update( &operation,
3204 input->x, first_part_size,
3205 output, output_buffer_size,
3206 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003207 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003208 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003209 PSA_ASSERT( psa_cipher_update( &operation,
3210 input->x + first_part_size,
3211 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003212 output + total_output_length,
3213 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003214 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003215 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003216 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003217 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003218 output + total_output_length,
3219 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003220 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003221 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003222 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003223
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003224 ASSERT_COMPARE( expected_output->x, expected_output->len,
3225 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003226
3227exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003228 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003229 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003230 mbedtls_psa_crypto_free( );
3231}
3232/* END_CASE */
3233
Gilles Peskine50e586b2018-06-08 14:28:46 +02003234/* BEGIN_CASE */
3235void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003236 data_t *key,
3237 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003238 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003239{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003240 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003241 psa_status_t status;
3242 psa_key_type_t key_type = key_type_arg;
3243 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003244 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003245 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003246 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003247 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003248 size_t output_buffer_size = 0;
3249 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003250 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003251 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003252 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003253
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003254 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3255 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003256
Gilles Peskine8817f612018-12-18 00:18:46 +01003257 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003258
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003259 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003260 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003261 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003262
Gilles Peskine8817f612018-12-18 00:18:46 +01003263 PSA_ASSERT( psa_import_key( handle, key_type,
3264 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003265
Gilles Peskine8817f612018-12-18 00:18:46 +01003266 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3267 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003268
Gilles Peskine8817f612018-12-18 00:18:46 +01003269 PSA_ASSERT( psa_cipher_set_iv( &operation,
3270 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003271
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003272 output_buffer_size = ( (size_t) input->len +
3273 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003274 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003275
Gilles Peskine8817f612018-12-18 00:18:46 +01003276 PSA_ASSERT( psa_cipher_update( &operation,
3277 input->x, input->len,
3278 output, output_buffer_size,
3279 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003280 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003281 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003282 output + total_output_length,
3283 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003284 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003285 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003286 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003287
3288 if( expected_status == PSA_SUCCESS )
3289 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003290 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003291 ASSERT_COMPARE( expected_output->x, expected_output->len,
3292 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003293 }
3294
Gilles Peskine50e586b2018-06-08 14:28:46 +02003295exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003296 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003297 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003298 mbedtls_psa_crypto_free( );
3299}
3300/* END_CASE */
3301
Gilles Peskine50e586b2018-06-08 14:28:46 +02003302/* BEGIN_CASE */
3303void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003304 data_t *key,
3305 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003306{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003307 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003308 psa_key_type_t key_type = key_type_arg;
3309 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003310 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003311 size_t iv_size = 16;
3312 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003313 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003314 size_t output1_size = 0;
3315 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003316 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003317 size_t output2_size = 0;
3318 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003319 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003320 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3321 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003322 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003323
Gilles Peskine8817f612018-12-18 00:18:46 +01003324 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003325
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003326 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003327 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003328 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003329
Gilles Peskine8817f612018-12-18 00:18:46 +01003330 PSA_ASSERT( psa_import_key( handle, key_type,
3331 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003332
Gilles Peskine8817f612018-12-18 00:18:46 +01003333 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3334 handle, alg ) );
3335 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3336 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003337
Gilles Peskine8817f612018-12-18 00:18:46 +01003338 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3339 iv, iv_size,
3340 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003341 output1_size = ( (size_t) input->len +
3342 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003343 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003344
Gilles Peskine8817f612018-12-18 00:18:46 +01003345 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3346 output1, output1_size,
3347 &output1_length ) );
3348 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003349 output1 + output1_length,
3350 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003351 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003352
Gilles Peskine048b7f02018-06-08 14:20:49 +02003353 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003354
Gilles Peskine8817f612018-12-18 00:18:46 +01003355 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003356
3357 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003358 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003359
Gilles Peskine8817f612018-12-18 00:18:46 +01003360 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3361 iv, iv_length ) );
3362 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3363 output2, output2_size,
3364 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003365 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003366 PSA_ASSERT( psa_cipher_finish( &operation2,
3367 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003368 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003369 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003370
Gilles Peskine048b7f02018-06-08 14:20:49 +02003371 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003372
Gilles Peskine8817f612018-12-18 00:18:46 +01003373 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003374
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003375 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003376
3377exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003378 mbedtls_free( output1 );
3379 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003380 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03003381 mbedtls_psa_crypto_free( );
3382}
3383/* END_CASE */
3384
3385/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003386void cipher_verify_output_multipart( int alg_arg,
3387 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003388 data_t *key,
3389 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003390 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003391{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003392 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003393 psa_key_type_t key_type = key_type_arg;
3394 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003395 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003396 unsigned char iv[16] = {0};
3397 size_t iv_size = 16;
3398 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003399 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003400 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003401 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003402 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003403 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003404 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003405 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003406 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3407 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003408 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003409
Gilles Peskine8817f612018-12-18 00:18:46 +01003410 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003411
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003412 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003413 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003414 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003415
Gilles Peskine8817f612018-12-18 00:18:46 +01003416 PSA_ASSERT( psa_import_key( handle, key_type,
3417 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003418
Gilles Peskine8817f612018-12-18 00:18:46 +01003419 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3420 handle, alg ) );
3421 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3422 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003423
Gilles Peskine8817f612018-12-18 00:18:46 +01003424 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3425 iv, iv_size,
3426 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003427 output1_buffer_size = ( (size_t) input->len +
3428 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003429 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003430
Gilles Peskinee0866522019-02-19 19:44:00 +01003431 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003432
Gilles Peskine8817f612018-12-18 00:18:46 +01003433 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3434 output1, output1_buffer_size,
3435 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003436 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003437
Gilles Peskine8817f612018-12-18 00:18:46 +01003438 PSA_ASSERT( psa_cipher_update( &operation1,
3439 input->x + first_part_size,
3440 input->len - first_part_size,
3441 output1, output1_buffer_size,
3442 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003443 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003444
Gilles Peskine8817f612018-12-18 00:18:46 +01003445 PSA_ASSERT( psa_cipher_finish( &operation1,
3446 output1 + output1_length,
3447 output1_buffer_size - output1_length,
3448 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003449 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003450
Gilles Peskine8817f612018-12-18 00:18:46 +01003451 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003452
Gilles Peskine048b7f02018-06-08 14:20:49 +02003453 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003454 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003455
Gilles Peskine8817f612018-12-18 00:18:46 +01003456 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3457 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003458
Gilles Peskine8817f612018-12-18 00:18:46 +01003459 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3460 output2, output2_buffer_size,
3461 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003462 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003463
Gilles Peskine8817f612018-12-18 00:18:46 +01003464 PSA_ASSERT( psa_cipher_update( &operation2,
3465 output1 + first_part_size,
3466 output1_length - first_part_size,
3467 output2, output2_buffer_size,
3468 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003469 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003470
Gilles Peskine8817f612018-12-18 00:18:46 +01003471 PSA_ASSERT( psa_cipher_finish( &operation2,
3472 output2 + output2_length,
3473 output2_buffer_size - output2_length,
3474 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003475 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003476
Gilles Peskine8817f612018-12-18 00:18:46 +01003477 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003478
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003479 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003480
3481exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003482 mbedtls_free( output1 );
3483 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003484 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003485 mbedtls_psa_crypto_free( );
3486}
3487/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003488
Gilles Peskine20035e32018-02-03 22:44:14 +01003489/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003490void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003491 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003492 data_t *nonce,
3493 data_t *additional_data,
3494 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003495 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003496{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003497 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003498 psa_key_type_t key_type = key_type_arg;
3499 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003500 unsigned char *output_data = NULL;
3501 size_t output_size = 0;
3502 size_t output_length = 0;
3503 unsigned char *output_data2 = NULL;
3504 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003505 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003506 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003507 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003508
Gilles Peskine4abf7412018-06-18 16:35:34 +02003509 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003510 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003511
Gilles Peskine8817f612018-12-18 00:18:46 +01003512 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003513
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003514 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003515 psa_key_policy_set_usage( &policy,
3516 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
3517 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003518 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003519
Gilles Peskine8817f612018-12-18 00:18:46 +01003520 PSA_ASSERT( psa_import_key( handle, key_type,
3521 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003522
Gilles Peskinefe11b722018-12-18 00:24:04 +01003523 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3524 nonce->x, nonce->len,
3525 additional_data->x,
3526 additional_data->len,
3527 input_data->x, input_data->len,
3528 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003529 &output_length ),
3530 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003531
3532 if( PSA_SUCCESS == expected_result )
3533 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003534 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003535
Gilles Peskinefe11b722018-12-18 00:24:04 +01003536 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3537 nonce->x, nonce->len,
3538 additional_data->x,
3539 additional_data->len,
3540 output_data, output_length,
3541 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003542 &output_length2 ),
3543 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003544
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003545 ASSERT_COMPARE( input_data->x, input_data->len,
3546 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003547 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003548
Gilles Peskinea1cac842018-06-11 19:33:02 +02003549exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003550 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003551 mbedtls_free( output_data );
3552 mbedtls_free( output_data2 );
3553 mbedtls_psa_crypto_free( );
3554}
3555/* END_CASE */
3556
3557/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003558void aead_encrypt( int key_type_arg, data_t *key_data,
3559 int alg_arg,
3560 data_t *nonce,
3561 data_t *additional_data,
3562 data_t *input_data,
3563 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003564{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003565 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003566 psa_key_type_t key_type = key_type_arg;
3567 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003568 unsigned char *output_data = NULL;
3569 size_t output_size = 0;
3570 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003571 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00003572 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003573
Gilles Peskine4abf7412018-06-18 16:35:34 +02003574 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003575 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003576
Gilles Peskine8817f612018-12-18 00:18:46 +01003577 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003578
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003579 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003580 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003581 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003582
Gilles Peskine8817f612018-12-18 00:18:46 +01003583 PSA_ASSERT( psa_import_key( handle, key_type,
3584 key_data->x,
3585 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003586
Gilles Peskine8817f612018-12-18 00:18:46 +01003587 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3588 nonce->x, nonce->len,
3589 additional_data->x, additional_data->len,
3590 input_data->x, input_data->len,
3591 output_data, output_size,
3592 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003593
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003594 ASSERT_COMPARE( expected_result->x, expected_result->len,
3595 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003596
Gilles Peskinea1cac842018-06-11 19:33:02 +02003597exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003598 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003599 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003600 mbedtls_psa_crypto_free( );
3601}
3602/* END_CASE */
3603
3604/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003605void aead_decrypt( int key_type_arg, data_t *key_data,
3606 int alg_arg,
3607 data_t *nonce,
3608 data_t *additional_data,
3609 data_t *input_data,
3610 data_t *expected_data,
3611 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003612{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003613 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003614 psa_key_type_t key_type = key_type_arg;
3615 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003616 unsigned char *output_data = NULL;
3617 size_t output_size = 0;
3618 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003619 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00003620 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003621 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003622
Gilles Peskine4abf7412018-06-18 16:35:34 +02003623 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003624 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003625
Gilles Peskine8817f612018-12-18 00:18:46 +01003626 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003627
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003628 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003629 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003630 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003631
Gilles Peskine8817f612018-12-18 00:18:46 +01003632 PSA_ASSERT( psa_import_key( handle, key_type,
3633 key_data->x,
3634 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003635
Gilles Peskinefe11b722018-12-18 00:24:04 +01003636 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3637 nonce->x, nonce->len,
3638 additional_data->x,
3639 additional_data->len,
3640 input_data->x, input_data->len,
3641 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003642 &output_length ),
3643 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003644
Gilles Peskine2d277862018-06-18 15:41:12 +02003645 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003646 ASSERT_COMPARE( expected_data->x, expected_data->len,
3647 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003648
Gilles Peskinea1cac842018-06-11 19:33:02 +02003649exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003650 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003651 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003652 mbedtls_psa_crypto_free( );
3653}
3654/* END_CASE */
3655
3656/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003657void signature_size( int type_arg,
3658 int bits,
3659 int alg_arg,
3660 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003661{
3662 psa_key_type_t type = type_arg;
3663 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003664 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003665 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003666exit:
3667 ;
3668}
3669/* END_CASE */
3670
3671/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003672void sign_deterministic( int key_type_arg, data_t *key_data,
3673 int alg_arg, data_t *input_data,
3674 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003675{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003676 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003677 psa_key_type_t key_type = key_type_arg;
3678 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003679 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003680 unsigned char *signature = NULL;
3681 size_t signature_size;
3682 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003683 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003684
Gilles Peskine8817f612018-12-18 00:18:46 +01003685 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003686
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003687 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003688 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003689 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003690
Gilles Peskine8817f612018-12-18 00:18:46 +01003691 PSA_ASSERT( psa_import_key( handle, key_type,
3692 key_data->x,
3693 key_data->len ) );
3694 PSA_ASSERT( psa_get_key_information( handle,
3695 NULL,
3696 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003697
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003698 /* Allocate a buffer which has the size advertized by the
3699 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003700 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3701 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003702 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003703 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003704 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003705
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003706 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003707 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3708 input_data->x, input_data->len,
3709 signature, signature_size,
3710 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003711 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003712 ASSERT_COMPARE( output_data->x, output_data->len,
3713 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003714
3715exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003716 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003717 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01003718 mbedtls_psa_crypto_free( );
3719}
3720/* END_CASE */
3721
3722/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003723void sign_fail( int key_type_arg, data_t *key_data,
3724 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003725 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003726{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003727 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003728 psa_key_type_t key_type = key_type_arg;
3729 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003730 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003731 psa_status_t actual_status;
3732 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003733 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003734 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003735 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003736
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003737 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003738
Gilles Peskine8817f612018-12-18 00:18:46 +01003739 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003740
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003741 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003742 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003743 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003744
Gilles Peskine8817f612018-12-18 00:18:46 +01003745 PSA_ASSERT( psa_import_key( handle, key_type,
3746 key_data->x,
3747 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003748
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003749 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003750 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003751 signature, signature_size,
3752 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003753 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003754 /* The value of *signature_length is unspecified on error, but
3755 * whatever it is, it should be less than signature_size, so that
3756 * if the caller tries to read *signature_length bytes without
3757 * checking the error code then they don't overflow a buffer. */
3758 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003759
3760exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003761 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003762 mbedtls_free( signature );
3763 mbedtls_psa_crypto_free( );
3764}
3765/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003766
3767/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003768void sign_verify( int key_type_arg, data_t *key_data,
3769 int alg_arg, data_t *input_data )
3770{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003771 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003772 psa_key_type_t key_type = key_type_arg;
3773 psa_algorithm_t alg = alg_arg;
3774 size_t key_bits;
3775 unsigned char *signature = NULL;
3776 size_t signature_size;
3777 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003778 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003779
Gilles Peskine8817f612018-12-18 00:18:46 +01003780 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003781
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003782 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003783 psa_key_policy_set_usage( &policy,
3784 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
3785 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003786 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003787
Gilles Peskine8817f612018-12-18 00:18:46 +01003788 PSA_ASSERT( psa_import_key( handle, key_type,
3789 key_data->x,
3790 key_data->len ) );
3791 PSA_ASSERT( psa_get_key_information( handle,
3792 NULL,
3793 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003794
3795 /* Allocate a buffer which has the size advertized by the
3796 * library. */
3797 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3798 key_bits, alg );
3799 TEST_ASSERT( signature_size != 0 );
3800 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003801 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003802
3803 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003804 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3805 input_data->x, input_data->len,
3806 signature, signature_size,
3807 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003808 /* Check that the signature length looks sensible. */
3809 TEST_ASSERT( signature_length <= signature_size );
3810 TEST_ASSERT( signature_length > 0 );
3811
3812 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003813 PSA_ASSERT( psa_asymmetric_verify(
3814 handle, alg,
3815 input_data->x, input_data->len,
3816 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003817
3818 if( input_data->len != 0 )
3819 {
3820 /* Flip a bit in the input and verify that the signature is now
3821 * detected as invalid. Flip a bit at the beginning, not at the end,
3822 * because ECDSA may ignore the last few bits of the input. */
3823 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003824 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3825 input_data->x, input_data->len,
3826 signature, signature_length ),
3827 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003828 }
3829
3830exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003831 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003832 mbedtls_free( signature );
3833 mbedtls_psa_crypto_free( );
3834}
3835/* END_CASE */
3836
3837/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003838void asymmetric_verify( int key_type_arg, data_t *key_data,
3839 int alg_arg, data_t *hash_data,
3840 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003841{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003842 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003843 psa_key_type_t key_type = key_type_arg;
3844 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003845 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003846
Gilles Peskine69c12672018-06-28 00:07:19 +02003847 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3848
Gilles Peskine8817f612018-12-18 00:18:46 +01003849 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003850
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003851 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003852 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003853 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03003854
Gilles Peskine8817f612018-12-18 00:18:46 +01003855 PSA_ASSERT( psa_import_key( handle, key_type,
3856 key_data->x,
3857 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003858
Gilles Peskine8817f612018-12-18 00:18:46 +01003859 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3860 hash_data->x, hash_data->len,
3861 signature_data->x,
3862 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003863exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003864 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003865 mbedtls_psa_crypto_free( );
3866}
3867/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003868
3869/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003870void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3871 int alg_arg, data_t *hash_data,
3872 data_t *signature_data,
3873 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003874{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003875 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003876 psa_key_type_t key_type = key_type_arg;
3877 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003878 psa_status_t actual_status;
3879 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003880 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003881
Gilles Peskine8817f612018-12-18 00:18:46 +01003882 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003883
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003884 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003885 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003886 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003887
Gilles Peskine8817f612018-12-18 00:18:46 +01003888 PSA_ASSERT( psa_import_key( handle, key_type,
3889 key_data->x,
3890 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003891
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003892 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003893 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003894 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003895 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003896
Gilles Peskinefe11b722018-12-18 00:24:04 +01003897 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003898
3899exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003900 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003901 mbedtls_psa_crypto_free( );
3902}
3903/* END_CASE */
3904
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003905/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003906void asymmetric_encrypt( int key_type_arg,
3907 data_t *key_data,
3908 int alg_arg,
3909 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003910 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003911 int expected_output_length_arg,
3912 int expected_status_arg )
3913{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003914 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003915 psa_key_type_t key_type = key_type_arg;
3916 psa_algorithm_t alg = alg_arg;
3917 size_t expected_output_length = expected_output_length_arg;
3918 size_t key_bits;
3919 unsigned char *output = NULL;
3920 size_t output_size;
3921 size_t output_length = ~0;
3922 psa_status_t actual_status;
3923 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003924 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003925
Gilles Peskine8817f612018-12-18 00:18:46 +01003926 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003927
Gilles Peskine656896e2018-06-29 19:12:28 +02003928 /* Import the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003929 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003930 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003931 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3932 PSA_ASSERT( psa_import_key( handle, key_type,
3933 key_data->x,
3934 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003935
3936 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003937 PSA_ASSERT( psa_get_key_information( handle,
3938 NULL,
3939 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003940 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003941 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003942
3943 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003944 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003945 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003946 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003947 output, output_size,
3948 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003949 TEST_EQUAL( actual_status, expected_status );
3950 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003951
Gilles Peskine68428122018-06-30 18:42:41 +02003952 /* If the label is empty, the test framework puts a non-null pointer
3953 * in label->x. Test that a null pointer works as well. */
3954 if( label->len == 0 )
3955 {
3956 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003957 if( output_size != 0 )
3958 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003959 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003960 input_data->x, input_data->len,
3961 NULL, label->len,
3962 output, output_size,
3963 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003964 TEST_EQUAL( actual_status, expected_status );
3965 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003966 }
3967
Gilles Peskine656896e2018-06-29 19:12:28 +02003968exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003969 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003970 mbedtls_free( output );
3971 mbedtls_psa_crypto_free( );
3972}
3973/* END_CASE */
3974
3975/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003976void asymmetric_encrypt_decrypt( int key_type_arg,
3977 data_t *key_data,
3978 int alg_arg,
3979 data_t *input_data,
3980 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003981{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003982 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003983 psa_key_type_t key_type = key_type_arg;
3984 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003985 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003986 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003987 size_t output_size;
3988 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003989 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003990 size_t output2_size;
3991 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003992 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003993
Gilles Peskine8817f612018-12-18 00:18:46 +01003994 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003995
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003996 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003997 psa_key_policy_set_usage( &policy,
3998 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003999 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004000 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004001
Gilles Peskine8817f612018-12-18 00:18:46 +01004002 PSA_ASSERT( psa_import_key( handle, key_type,
4003 key_data->x,
4004 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004005
4006 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01004007 PSA_ASSERT( psa_get_key_information( handle,
4008 NULL,
4009 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004010 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004011 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004012 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004013 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004014
Gilles Peskineeebd7382018-06-08 18:11:54 +02004015 /* We test encryption by checking that encrypt-then-decrypt gives back
4016 * the original plaintext because of the non-optional random
4017 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004018 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
4019 input_data->x, input_data->len,
4020 label->x, label->len,
4021 output, output_size,
4022 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004023 /* We don't know what ciphertext length to expect, but check that
4024 * it looks sensible. */
4025 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004026
Gilles Peskine8817f612018-12-18 00:18:46 +01004027 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4028 output, output_length,
4029 label->x, label->len,
4030 output2, output2_size,
4031 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004032 ASSERT_COMPARE( input_data->x, input_data->len,
4033 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004034
4035exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004036 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004037 mbedtls_free( output );
4038 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004039 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004040}
4041/* END_CASE */
4042
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004043/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004044void asymmetric_decrypt( int key_type_arg,
4045 data_t *key_data,
4046 int alg_arg,
4047 data_t *input_data,
4048 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004049 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004050{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004051 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004052 psa_key_type_t key_type = key_type_arg;
4053 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004054 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004055 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004056 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00004057 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004058
Jaeden Amero412654a2019-02-06 12:57:46 +00004059 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004060 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004061
Gilles Peskine8817f612018-12-18 00:18:46 +01004062 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004063
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004064 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02004065 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004066 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004067
Gilles Peskine8817f612018-12-18 00:18:46 +01004068 PSA_ASSERT( psa_import_key( handle, key_type,
4069 key_data->x,
4070 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004071
Gilles Peskine8817f612018-12-18 00:18:46 +01004072 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4073 input_data->x, input_data->len,
4074 label->x, label->len,
4075 output,
4076 output_size,
4077 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004078 ASSERT_COMPARE( expected_data->x, expected_data->len,
4079 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004080
Gilles Peskine68428122018-06-30 18:42:41 +02004081 /* If the label is empty, the test framework puts a non-null pointer
4082 * in label->x. Test that a null pointer works as well. */
4083 if( label->len == 0 )
4084 {
4085 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004086 if( output_size != 0 )
4087 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004088 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4089 input_data->x, input_data->len,
4090 NULL, label->len,
4091 output,
4092 output_size,
4093 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004094 ASSERT_COMPARE( expected_data->x, expected_data->len,
4095 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004096 }
4097
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004098exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004099 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004100 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004101 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004102}
4103/* END_CASE */
4104
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004105/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004106void asymmetric_decrypt_fail( int key_type_arg,
4107 data_t *key_data,
4108 int alg_arg,
4109 data_t *input_data,
4110 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004111 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004112 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004113{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004114 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004115 psa_key_type_t key_type = key_type_arg;
4116 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004117 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004118 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004119 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004120 psa_status_t actual_status;
4121 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004122 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004123
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004124 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004125
Gilles Peskine8817f612018-12-18 00:18:46 +01004126 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004127
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004128 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02004129 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004130 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004131
Gilles Peskine8817f612018-12-18 00:18:46 +01004132 PSA_ASSERT( psa_import_key( handle, key_type,
4133 key_data->x,
4134 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004135
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004136 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004137 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004138 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004139 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004140 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004141 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004142 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004143
Gilles Peskine68428122018-06-30 18:42:41 +02004144 /* If the label is empty, the test framework puts a non-null pointer
4145 * in label->x. Test that a null pointer works as well. */
4146 if( label->len == 0 )
4147 {
4148 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004149 if( output_size != 0 )
4150 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004151 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004152 input_data->x, input_data->len,
4153 NULL, label->len,
4154 output, output_size,
4155 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004156 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004157 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004158 }
4159
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004160exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004161 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004162 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004163 mbedtls_psa_crypto_free( );
4164}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004165/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004166
4167/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00004168void crypto_generator_init( )
4169{
4170 /* Test each valid way of initializing the object, except for `= {0}`, as
4171 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4172 * though it's OK by the C standard. We could test for this, but we'd need
4173 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004174 size_t capacity;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004175 psa_crypto_generator_t func = psa_crypto_generator_init( );
4176 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
4177 psa_crypto_generator_t zero;
4178
4179 memset( &zero, 0, sizeof( zero ) );
4180
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004181 /* A default generator should not be able to report its capacity. */
4182 TEST_EQUAL( psa_get_generator_capacity( &func, &capacity ),
4183 PSA_ERROR_BAD_STATE );
4184 TEST_EQUAL( psa_get_generator_capacity( &init, &capacity ),
4185 PSA_ERROR_BAD_STATE );
4186 TEST_EQUAL( psa_get_generator_capacity( &zero, &capacity ),
4187 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004188
4189 /* A default generator should be abortable without error. */
4190 PSA_ASSERT( psa_generator_abort(&func) );
4191 PSA_ASSERT( psa_generator_abort(&init) );
4192 PSA_ASSERT( psa_generator_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004193}
4194/* END_CASE */
4195
4196/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004197void derive_setup( int key_type_arg,
4198 data_t *key_data,
4199 int alg_arg,
4200 data_t *salt,
4201 data_t *label,
4202 int requested_capacity_arg,
4203 int expected_status_arg )
4204{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004205 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004206 size_t key_type = key_type_arg;
4207 psa_algorithm_t alg = alg_arg;
4208 size_t requested_capacity = requested_capacity_arg;
4209 psa_status_t expected_status = expected_status_arg;
4210 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004211 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004212
Gilles Peskine8817f612018-12-18 00:18:46 +01004213 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004214
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004215 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004216 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004217 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004218
Gilles Peskine8817f612018-12-18 00:18:46 +01004219 PSA_ASSERT( psa_import_key( handle, key_type,
4220 key_data->x,
4221 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004222
Gilles Peskinefe11b722018-12-18 00:24:04 +01004223 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4224 salt->x, salt->len,
4225 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004226 requested_capacity ),
4227 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004228
4229exit:
4230 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004231 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004232 mbedtls_psa_crypto_free( );
4233}
4234/* END_CASE */
4235
4236/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004237void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004238{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004239 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004240 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004241 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004242 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004243 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004244 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004245 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4246 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4247 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00004248 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004249
Gilles Peskine8817f612018-12-18 00:18:46 +01004250 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004251
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004252 PSA_ASSERT( psa_allocate_key( &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004253 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004254 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004255
Gilles Peskine8817f612018-12-18 00:18:46 +01004256 PSA_ASSERT( psa_import_key( handle, key_type,
4257 key_data,
4258 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004259
4260 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01004261 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4262 NULL, 0,
4263 NULL, 0,
4264 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004265
4266 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01004267 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4268 NULL, 0,
4269 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004270 capacity ),
4271 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004272
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004273 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004274
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004275 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004276 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004277
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004278exit:
4279 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004280 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004281 mbedtls_psa_crypto_free( );
4282}
4283/* END_CASE */
4284
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004285/* BEGIN_CASE */
4286void test_derive_invalid_generator_tests( )
4287{
4288 uint8_t output_buffer[16];
4289 size_t buffer_size = 16;
4290 size_t capacity = 0;
4291 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4292
Nir Sonnenschein50789302018-10-31 12:16:38 +02004293 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004294 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004295
4296 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004297 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004298
Gilles Peskine8817f612018-12-18 00:18:46 +01004299 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004300
Nir Sonnenschein50789302018-10-31 12:16:38 +02004301 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004302 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004303
Nir Sonnenschein50789302018-10-31 12:16:38 +02004304 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004305 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004306
4307exit:
4308 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004309}
4310/* END_CASE */
4311
4312/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004313void derive_output( int alg_arg,
4314 data_t *key_data,
4315 data_t *salt,
4316 data_t *label,
4317 int requested_capacity_arg,
4318 data_t *expected_output1,
4319 data_t *expected_output2 )
4320{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004321 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004322 psa_algorithm_t alg = alg_arg;
4323 size_t requested_capacity = requested_capacity_arg;
4324 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4325 uint8_t *expected_outputs[2] =
4326 {expected_output1->x, expected_output2->x};
4327 size_t output_sizes[2] =
4328 {expected_output1->len, expected_output2->len};
4329 size_t output_buffer_size = 0;
4330 uint8_t *output_buffer = NULL;
4331 size_t expected_capacity;
4332 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00004333 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004334 psa_status_t status;
4335 unsigned i;
4336
4337 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4338 {
4339 if( output_sizes[i] > output_buffer_size )
4340 output_buffer_size = output_sizes[i];
4341 if( output_sizes[i] == 0 )
4342 expected_outputs[i] = NULL;
4343 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004344 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004345 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004346
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004347 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004348 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004349 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004350
Gilles Peskine8817f612018-12-18 00:18:46 +01004351 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
4352 key_data->x,
4353 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004354
4355 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004356 if( PSA_ALG_IS_HKDF( alg ) )
4357 {
4358 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4359 PSA_ASSERT( psa_set_generator_capacity( &generator,
4360 requested_capacity ) );
4361 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4362 PSA_KDF_STEP_SALT,
4363 salt->x, salt->len ) );
4364 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4365 PSA_KDF_STEP_SECRET,
4366 handle ) );
4367 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4368 PSA_KDF_STEP_INFO,
4369 label->x, label->len ) );
4370 }
4371 else
4372 {
4373 // legacy
4374 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4375 salt->x, salt->len,
4376 label->x, label->len,
4377 requested_capacity ) );
4378 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004379 PSA_ASSERT( psa_get_generator_capacity( &generator,
4380 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004381 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004382 expected_capacity = requested_capacity;
4383
4384 /* Expansion phase. */
4385 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4386 {
4387 /* Read some bytes. */
4388 status = psa_generator_read( &generator,
4389 output_buffer, output_sizes[i] );
4390 if( expected_capacity == 0 && output_sizes[i] == 0 )
4391 {
4392 /* Reading 0 bytes when 0 bytes are available can go either way. */
4393 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004394 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004395 continue;
4396 }
4397 else if( expected_capacity == 0 ||
4398 output_sizes[i] > expected_capacity )
4399 {
4400 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004401 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004402 expected_capacity = 0;
4403 continue;
4404 }
4405 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004406 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004407 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004408 ASSERT_COMPARE( output_buffer, output_sizes[i],
4409 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004410 /* Check the generator status. */
4411 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01004412 PSA_ASSERT( psa_get_generator_capacity( &generator,
4413 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004414 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004415 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004416 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004417
4418exit:
4419 mbedtls_free( output_buffer );
4420 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004421 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004422 mbedtls_psa_crypto_free( );
4423}
4424/* END_CASE */
4425
4426/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004427void derive_full( int alg_arg,
4428 data_t *key_data,
4429 data_t *salt,
4430 data_t *label,
4431 int requested_capacity_arg )
4432{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004433 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004434 psa_algorithm_t alg = alg_arg;
4435 size_t requested_capacity = requested_capacity_arg;
4436 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4437 unsigned char output_buffer[16];
4438 size_t expected_capacity = requested_capacity;
4439 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00004440 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004441
Gilles Peskine8817f612018-12-18 00:18:46 +01004442 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004443
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004444 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004445 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004446 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004447
Gilles Peskine8817f612018-12-18 00:18:46 +01004448 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
4449 key_data->x,
4450 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004451
4452 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004453 if( PSA_ALG_IS_HKDF( alg ) )
4454 {
4455 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4456 PSA_ASSERT( psa_set_generator_capacity( &generator,
4457 requested_capacity ) );
4458 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4459 PSA_KDF_STEP_SALT,
4460 salt->x, salt->len ) );
4461 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4462 PSA_KDF_STEP_SECRET,
4463 handle ) );
4464 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4465 PSA_KDF_STEP_INFO,
4466 label->x, label->len ) );
4467 }
4468 else
4469 {
4470 // legacy
4471 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4472 salt->x, salt->len,
4473 label->x, label->len,
4474 requested_capacity ) );
4475 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004476 PSA_ASSERT( psa_get_generator_capacity( &generator,
4477 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004478 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004479
4480 /* Expansion phase. */
4481 while( current_capacity > 0 )
4482 {
4483 size_t read_size = sizeof( output_buffer );
4484 if( read_size > current_capacity )
4485 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01004486 PSA_ASSERT( psa_generator_read( &generator,
4487 output_buffer,
4488 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004489 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01004490 PSA_ASSERT( psa_get_generator_capacity( &generator,
4491 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004492 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004493 }
4494
4495 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004496 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004497 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004498
Gilles Peskine8817f612018-12-18 00:18:46 +01004499 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004500
4501exit:
4502 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004503 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02004504 mbedtls_psa_crypto_free( );
4505}
4506/* END_CASE */
4507
4508/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004509void derive_key_exercise( int alg_arg,
4510 data_t *key_data,
4511 data_t *salt,
4512 data_t *label,
4513 int derived_type_arg,
4514 int derived_bits_arg,
4515 int derived_usage_arg,
4516 int derived_alg_arg )
4517{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004518 psa_key_handle_t base_handle = 0;
4519 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004520 psa_algorithm_t alg = alg_arg;
4521 psa_key_type_t derived_type = derived_type_arg;
4522 size_t derived_bits = derived_bits_arg;
4523 psa_key_usage_t derived_usage = derived_usage_arg;
4524 psa_algorithm_t derived_alg = derived_alg_arg;
4525 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
4526 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004527 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004528 psa_key_type_t got_type;
4529 size_t got_bits;
4530
Gilles Peskine8817f612018-12-18 00:18:46 +01004531 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004532
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004533 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004534 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004535 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
4536 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
4537 key_data->x,
4538 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004539
4540 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004541 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4542 salt->x, salt->len,
4543 label->x, label->len,
4544 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004545 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004546 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004547 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4548 PSA_ASSERT( psa_generator_import_key( derived_handle,
4549 derived_type,
4550 derived_bits,
4551 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004552
4553 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01004554 PSA_ASSERT( psa_get_key_information( derived_handle,
4555 &got_type,
4556 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004557 TEST_EQUAL( got_type, derived_type );
4558 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004559
4560 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004561 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004562 goto exit;
4563
4564exit:
4565 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004566 psa_destroy_key( base_handle );
4567 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004568 mbedtls_psa_crypto_free( );
4569}
4570/* END_CASE */
4571
4572/* BEGIN_CASE */
4573void derive_key_export( int alg_arg,
4574 data_t *key_data,
4575 data_t *salt,
4576 data_t *label,
4577 int bytes1_arg,
4578 int bytes2_arg )
4579{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004580 psa_key_handle_t base_handle = 0;
4581 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004582 psa_algorithm_t alg = alg_arg;
4583 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004584 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004585 size_t bytes2 = bytes2_arg;
4586 size_t capacity = bytes1 + bytes2;
4587 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004588 uint8_t *output_buffer = NULL;
4589 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00004590 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004591 size_t length;
4592
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004593 ASSERT_ALLOC( output_buffer, capacity );
4594 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004595 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004596
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004597 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004598 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004599 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
4600 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
4601 key_data->x,
4602 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004603
4604 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004605 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4606 salt->x, salt->len,
4607 label->x, label->len,
4608 capacity ) );
4609 PSA_ASSERT( psa_generator_read( &generator,
4610 output_buffer,
4611 capacity ) );
4612 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004613
4614 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004615 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4616 salt->x, salt->len,
4617 label->x, label->len,
4618 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004619 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004620 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004621 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4622 PSA_ASSERT( psa_generator_import_key( derived_handle,
4623 PSA_KEY_TYPE_RAW_DATA,
4624 derived_bits,
4625 &generator ) );
4626 PSA_ASSERT( psa_export_key( derived_handle,
4627 export_buffer, bytes1,
4628 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004629 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004630 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004631 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004632 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4633 PSA_ASSERT( psa_generator_import_key( derived_handle,
4634 PSA_KEY_TYPE_RAW_DATA,
4635 PSA_BYTES_TO_BITS( bytes2 ),
4636 &generator ) );
4637 PSA_ASSERT( psa_export_key( derived_handle,
4638 export_buffer + bytes1, bytes2,
4639 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004640 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004641
4642 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004643 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4644 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004645
4646exit:
4647 mbedtls_free( output_buffer );
4648 mbedtls_free( export_buffer );
4649 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004650 psa_destroy_key( base_handle );
4651 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004652 mbedtls_psa_crypto_free( );
4653}
4654/* END_CASE */
4655
4656/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004657void key_agreement_setup( int alg_arg,
4658 int our_key_type_arg, data_t *our_key_data,
4659 data_t *peer_key_data,
4660 int expected_status_arg )
4661{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004662 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004663 psa_algorithm_t alg = alg_arg;
4664 psa_key_type_t our_key_type = our_key_type_arg;
4665 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004666 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004667 psa_status_t expected_status = expected_status_arg;
4668 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004669
Gilles Peskine8817f612018-12-18 00:18:46 +01004670 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004671
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004672 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004673 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004674 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4675 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4676 our_key_data->x,
4677 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004678
Gilles Peskine77f40d82019-04-11 21:27:06 +02004679 /* The tests currently include inputs that should fail at either step.
4680 * Test cases that fail at the setup step should be changed to call
4681 * key_derivation_setup instead, and this function should be renamed
4682 * to key_agreement_fail. */
4683 status = psa_key_derivation_setup( &generator, alg );
4684 if( status == PSA_SUCCESS )
4685 {
4686 TEST_EQUAL( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
4687 our_key,
4688 peer_key_data->x, peer_key_data->len ),
4689 expected_status );
4690 }
4691 else
4692 {
4693 TEST_ASSERT( status == expected_status );
4694 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004695
4696exit:
4697 psa_generator_abort( &generator );
4698 psa_destroy_key( our_key );
4699 mbedtls_psa_crypto_free( );
4700}
4701/* END_CASE */
4702
4703/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004704void key_agreement_capacity( int alg_arg,
4705 int our_key_type_arg, data_t *our_key_data,
4706 data_t *peer_key_data,
4707 int expected_capacity_arg )
4708{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004709 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004710 psa_algorithm_t alg = alg_arg;
4711 psa_key_type_t our_key_type = our_key_type_arg;
4712 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004713 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004714 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004715 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004716
Gilles Peskine8817f612018-12-18 00:18:46 +01004717 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004718
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004719 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004720 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004721 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4722 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4723 our_key_data->x,
4724 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004725
Gilles Peskine969c5d62019-01-16 15:53:06 +01004726 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4727 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004728 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004729 peer_key_data->x, peer_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004730
Gilles Peskinebf491972018-10-25 22:36:12 +02004731 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004732 PSA_ASSERT( psa_get_generator_capacity(
4733 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004734 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004735
Gilles Peskinebf491972018-10-25 22:36:12 +02004736 /* Test the actual capacity by reading the output. */
4737 while( actual_capacity > sizeof( output ) )
4738 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004739 PSA_ASSERT( psa_generator_read( &generator,
4740 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004741 actual_capacity -= sizeof( output );
4742 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004743 PSA_ASSERT( psa_generator_read( &generator,
4744 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004745 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004746 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004747
Gilles Peskine59685592018-09-18 12:11:34 +02004748exit:
4749 psa_generator_abort( &generator );
4750 psa_destroy_key( our_key );
4751 mbedtls_psa_crypto_free( );
4752}
4753/* END_CASE */
4754
4755/* BEGIN_CASE */
4756void key_agreement_output( int alg_arg,
4757 int our_key_type_arg, data_t *our_key_data,
4758 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004759 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004760{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004761 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004762 psa_algorithm_t alg = alg_arg;
4763 psa_key_type_t our_key_type = our_key_type_arg;
4764 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004765 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004766 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004767
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004768 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4769 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004770
Gilles Peskine8817f612018-12-18 00:18:46 +01004771 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004772
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004773 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004774 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004775 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4776 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4777 our_key_data->x,
4778 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004779
Gilles Peskine969c5d62019-01-16 15:53:06 +01004780 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4781 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004782 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004783 peer_key_data->x, peer_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004784
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004785 PSA_ASSERT( psa_generator_read( &generator,
4786 actual_output,
4787 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004788 ASSERT_COMPARE( actual_output, expected_output1->len,
4789 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004790 if( expected_output2->len != 0 )
4791 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004792 PSA_ASSERT( psa_generator_read( &generator,
4793 actual_output,
4794 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004795 ASSERT_COMPARE( actual_output, expected_output2->len,
4796 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004797 }
Gilles Peskine59685592018-09-18 12:11:34 +02004798
4799exit:
4800 psa_generator_abort( &generator );
4801 psa_destroy_key( our_key );
4802 mbedtls_psa_crypto_free( );
4803 mbedtls_free( actual_output );
4804}
4805/* END_CASE */
4806
4807/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004808void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004809{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004810 size_t bytes = bytes_arg;
4811 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004812 unsigned char *output = NULL;
4813 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004814 size_t i;
4815 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004816
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004817 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4818 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004819 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004820
Gilles Peskine8817f612018-12-18 00:18:46 +01004821 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004822
Gilles Peskinea50d7392018-06-21 10:22:13 +02004823 /* Run several times, to ensure that every output byte will be
4824 * nonzero at least once with overwhelming probability
4825 * (2^(-8*number_of_runs)). */
4826 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004827 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004828 if( bytes != 0 )
4829 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004830 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004831
4832 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004833 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4834 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004835
4836 for( i = 0; i < bytes; i++ )
4837 {
4838 if( output[i] != 0 )
4839 ++changed[i];
4840 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004841 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004842
4843 /* Check that every byte was changed to nonzero at least once. This
4844 * validates that psa_generate_random is overwriting every byte of
4845 * the output buffer. */
4846 for( i = 0; i < bytes; i++ )
4847 {
4848 TEST_ASSERT( changed[i] != 0 );
4849 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004850
4851exit:
4852 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004853 mbedtls_free( output );
4854 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004855}
4856/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004857
4858/* BEGIN_CASE */
4859void generate_key( int type_arg,
4860 int bits_arg,
4861 int usage_arg,
4862 int alg_arg,
4863 int expected_status_arg )
4864{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004865 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004866 psa_key_type_t type = type_arg;
4867 psa_key_usage_t usage = usage_arg;
4868 size_t bits = bits_arg;
4869 psa_algorithm_t alg = alg_arg;
4870 psa_status_t expected_status = expected_status_arg;
4871 psa_key_type_t got_type;
4872 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004873 psa_status_t expected_info_status =
David Saadab4ecc272019-02-14 13:48:10 +02004874 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Jaeden Amero70261c52019-01-04 11:47:20 +00004875 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004876
Gilles Peskine8817f612018-12-18 00:18:46 +01004877 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004878
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004879 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004880 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004881 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004882
4883 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004884 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
4885 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004886
4887 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004888 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
4889 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004890 if( expected_info_status != PSA_SUCCESS )
4891 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004892 TEST_EQUAL( got_type, type );
4893 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004894
Gilles Peskine818ca122018-06-20 18:16:48 +02004895 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004896 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004897 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004898
4899exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004900 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004901 mbedtls_psa_crypto_free( );
4902}
4903/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004904
Darryl Greend49a4992018-06-18 17:27:26 +01004905/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4906void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4907 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004908 int alg_arg, int generation_method,
4909 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004910{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004911 psa_key_handle_t handle = 0;
4912 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004913 psa_key_type_t type = (psa_key_type_t) type_arg;
4914 psa_key_type_t type_get;
4915 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00004916 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
4917 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004918 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4919 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004920 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00004921 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4922 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004923 unsigned char *first_export = NULL;
4924 unsigned char *second_export = NULL;
4925 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4926 size_t first_exported_length;
4927 size_t second_exported_length;
4928
4929 ASSERT_ALLOC( first_export, export_size );
4930 ASSERT_ALLOC( second_export, export_size );
4931
Gilles Peskine8817f612018-12-18 00:18:46 +01004932 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004933
Gilles Peskine8817f612018-12-18 00:18:46 +01004934 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
Gilles Peskine8817f612018-12-18 00:18:46 +01004935 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004936 psa_key_policy_set_usage( &policy_set, policy_usage,
4937 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004938 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004939
Darryl Green0c6575a2018-11-07 16:05:30 +00004940 switch( generation_method )
4941 {
4942 case IMPORT_KEY:
4943 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004944 PSA_ASSERT( psa_import_key( handle, type,
4945 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004946 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004947
Darryl Green0c6575a2018-11-07 16:05:30 +00004948 case GENERATE_KEY:
4949 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004950 PSA_ASSERT( psa_generate_key( handle, type, bits,
4951 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004952 break;
4953
4954 case DERIVE_KEY:
4955 /* Create base key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004956 PSA_ASSERT( psa_allocate_key( &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004957 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4958 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004959 PSA_ASSERT( psa_set_key_policy(
4960 base_key, &base_policy_set ) );
4961 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4962 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004963 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004964 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4965 base_policy_alg,
4966 NULL, 0, NULL, 0,
4967 export_size ) );
4968 PSA_ASSERT( psa_generator_import_key(
4969 handle, PSA_KEY_TYPE_RAW_DATA,
4970 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004971 break;
4972 }
Darryl Greend49a4992018-06-18 17:27:26 +01004973
4974 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004975 TEST_EQUAL( psa_export_key( handle,
4976 first_export, export_size,
4977 &first_exported_length ),
4978 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004979
4980 /* Shutdown and restart */
4981 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004982 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004983
Darryl Greend49a4992018-06-18 17:27:26 +01004984 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004985 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4986 &handle ) );
4987 PSA_ASSERT( psa_get_key_information(
4988 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004989 TEST_EQUAL( type_get, type );
4990 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004991
Gilles Peskine8817f612018-12-18 00:18:46 +01004992 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004993 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4994 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004995
4996 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004997 TEST_EQUAL( psa_export_key( handle,
4998 second_export, export_size,
4999 &second_exported_length ),
5000 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01005001
Darryl Green0c6575a2018-11-07 16:05:30 +00005002 if( export_status == PSA_SUCCESS )
5003 {
5004 ASSERT_COMPARE( first_export, first_exported_length,
5005 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01005006
Darryl Green0c6575a2018-11-07 16:05:30 +00005007 switch( generation_method )
5008 {
5009 case IMPORT_KEY:
5010 ASSERT_COMPARE( data->x, data->len,
5011 first_export, first_exported_length );
5012 break;
5013 default:
5014 break;
5015 }
5016 }
5017
5018 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005019 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005020 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005021
5022exit:
5023 mbedtls_free( first_export );
5024 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005025 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01005026 mbedtls_psa_crypto_free();
5027}
5028/* END_CASE */