blob: a79b73834020852fb233dcc58066ce7964afb25c [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
4#if defined(MBEDTLS_PSA_CRYPTO_SPM)
5#include "spm/psa_defs.h"
6#endif
7
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02008#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02009#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +020010#include "mbedtls/oid.h"
11
Gilles Peskinee59236f2018-01-27 23:32:46 +010012#include "psa/crypto.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Jaeden Amerof24c7f82018-06-27 17:20:43 +010014/** An invalid export length that will never be set by psa_export_key(). */
15static const size_t INVALID_EXPORT_LENGTH = ~0U;
16
Gilles Peskinef426e0f2019-02-25 17:42:03 +010017/* A hash algorithm that is known to be supported.
18 *
19 * This is used in some smoke tests.
20 */
21#if defined(MBEDTLS_MD2_C)
22#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
23#elif defined(MBEDTLS_MD4_C)
24#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
25#elif defined(MBEDTLS_MD5_C)
26#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
27/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
28 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
29 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
30 * implausible anyway. */
31#elif defined(MBEDTLS_SHA1_C)
32#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
33#elif defined(MBEDTLS_SHA256_C)
34#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
35#elif defined(MBEDTLS_SHA512_C)
36#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
37#elif defined(MBEDTLS_SHA3_C)
38#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
39#else
40#undef KNOWN_SUPPORTED_HASH_ALG
41#endif
42
43/* A block cipher that is known to be supported.
44 *
45 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
46 */
47#if defined(MBEDTLS_AES_C)
48#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
49#elif defined(MBEDTLS_ARIA_C)
50#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
51#elif defined(MBEDTLS_CAMELLIA_C)
52#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
53#undef KNOWN_SUPPORTED_BLOCK_CIPHER
54#endif
55
56/* A MAC mode that is known to be supported.
57 *
58 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
59 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
60 *
61 * This is used in some smoke tests.
62 */
63#if defined(KNOWN_SUPPORTED_HASH_ALG)
64#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
65#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
66#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
67#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
68#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
69#else
70#undef KNOWN_SUPPORTED_MAC_ALG
71#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
72#endif
73
74/* A cipher algorithm and key type that are known to be supported.
75 *
76 * This is used in some smoke tests.
77 */
78#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
79#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
80#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
81#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
82#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
83#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
84#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
85#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
86#else
87#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
88#endif
89#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
90#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
91#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
92#elif defined(MBEDTLS_RC4_C)
93#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
94#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
95#else
96#undef KNOWN_SUPPORTED_CIPHER_ALG
97#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
98#endif
99
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200100/** Test if a buffer contains a constant byte value.
101 *
102 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200103 *
104 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200105 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200106 * \param size Size of the buffer in bytes.
107 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200108 * \return 1 if the buffer is all-bits-zero.
109 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200110 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200111static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200112{
113 size_t i;
114 for( i = 0; i < size; i++ )
115 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200116 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200117 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200118 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200119 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200120}
Gilles Peskine818ca122018-06-20 18:16:48 +0200121
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200122/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
123static int asn1_write_10x( unsigned char **p,
124 unsigned char *start,
125 size_t bits,
126 unsigned char x )
127{
128 int ret;
129 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200130 if( bits == 0 )
131 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
132 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200133 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300134 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200135 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
136 *p -= len;
137 ( *p )[len-1] = x;
138 if( bits % 8 == 0 )
139 ( *p )[1] |= 1;
140 else
141 ( *p )[0] |= 1 << ( bits % 8 );
142 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
143 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
144 MBEDTLS_ASN1_INTEGER ) );
145 return( len );
146}
147
148static int construct_fake_rsa_key( unsigned char *buffer,
149 size_t buffer_size,
150 unsigned char **p,
151 size_t bits,
152 int keypair )
153{
154 size_t half_bits = ( bits + 1 ) / 2;
155 int ret;
156 int len = 0;
157 /* Construct something that looks like a DER encoding of
158 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
159 * RSAPrivateKey ::= SEQUENCE {
160 * version Version,
161 * modulus INTEGER, -- n
162 * publicExponent INTEGER, -- e
163 * privateExponent INTEGER, -- d
164 * prime1 INTEGER, -- p
165 * prime2 INTEGER, -- q
166 * exponent1 INTEGER, -- d mod (p-1)
167 * exponent2 INTEGER, -- d mod (q-1)
168 * coefficient INTEGER, -- (inverse of q) mod p
169 * otherPrimeInfos OtherPrimeInfos OPTIONAL
170 * }
171 * Or, for a public key, the same structure with only
172 * version, modulus and publicExponent.
173 */
174 *p = buffer + buffer_size;
175 if( keypair )
176 {
177 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
178 asn1_write_10x( p, buffer, half_bits, 1 ) );
179 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
180 asn1_write_10x( p, buffer, half_bits, 1 ) );
181 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
182 asn1_write_10x( p, buffer, half_bits, 1 ) );
183 MBEDTLS_ASN1_CHK_ADD( len, /* q */
184 asn1_write_10x( p, buffer, half_bits, 1 ) );
185 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
186 asn1_write_10x( p, buffer, half_bits, 3 ) );
187 MBEDTLS_ASN1_CHK_ADD( len, /* d */
188 asn1_write_10x( p, buffer, bits, 1 ) );
189 }
190 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
191 asn1_write_10x( p, buffer, 17, 1 ) );
192 MBEDTLS_ASN1_CHK_ADD( len, /* n */
193 asn1_write_10x( p, buffer, bits, 1 ) );
194 if( keypair )
195 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
196 mbedtls_asn1_write_int( p, buffer, 0 ) );
197 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
198 {
199 const unsigned char tag =
200 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
201 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
202 }
203 return( len );
204}
205
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100206int exercise_mac_setup( psa_key_type_t key_type,
207 const unsigned char *key_bytes,
208 size_t key_length,
209 psa_algorithm_t alg,
210 psa_mac_operation_t *operation,
211 psa_status_t *status )
212{
213 psa_key_handle_t handle = 0;
214 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
215
216 PSA_ASSERT( psa_allocate_key( &handle ) );
217 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
218 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
219 PSA_ASSERT( psa_import_key( handle, key_type, key_bytes, key_length ) );
220
221 *status = psa_mac_sign_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100222 /* Whether setup succeeded or failed, abort must succeed. */
223 PSA_ASSERT( psa_mac_abort( operation ) );
224 /* If setup failed, reproduce the failure, so that the caller can
225 * test the resulting state of the operation object. */
226 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100227 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100228 TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ),
229 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100230 }
231
232 psa_destroy_key( handle );
233 return( 1 );
234
235exit:
236 psa_destroy_key( handle );
237 return( 0 );
238}
239
240int exercise_cipher_setup( psa_key_type_t key_type,
241 const unsigned char *key_bytes,
242 size_t key_length,
243 psa_algorithm_t alg,
244 psa_cipher_operation_t *operation,
245 psa_status_t *status )
246{
247 psa_key_handle_t handle = 0;
248 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
249
250 PSA_ASSERT( psa_allocate_key( &handle ) );
251 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
252 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
253 PSA_ASSERT( psa_import_key( handle, key_type, key_bytes, key_length ) );
254
255 *status = psa_cipher_encrypt_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100256 /* Whether setup succeeded or failed, abort must succeed. */
257 PSA_ASSERT( psa_cipher_abort( operation ) );
258 /* If setup failed, reproduce the failure, so that the caller can
259 * test the resulting state of the operation object. */
260 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100261 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100262 TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ),
263 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100264 }
265
266 psa_destroy_key( handle );
267 return( 1 );
268
269exit:
270 psa_destroy_key( handle );
271 return( 0 );
272}
273
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100274static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200275 psa_key_usage_t usage,
276 psa_algorithm_t alg )
277{
Jaeden Amero769ce272019-01-04 11:48:03 +0000278 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200279 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200280 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200281 size_t mac_length = sizeof( mac );
282
283 if( usage & PSA_KEY_USAGE_SIGN )
284 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100285 PSA_ASSERT( psa_mac_sign_setup( &operation,
286 handle, alg ) );
287 PSA_ASSERT( psa_mac_update( &operation,
288 input, sizeof( input ) ) );
289 PSA_ASSERT( psa_mac_sign_finish( &operation,
290 mac, sizeof( mac ),
291 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200292 }
293
294 if( usage & PSA_KEY_USAGE_VERIFY )
295 {
296 psa_status_t verify_status =
297 ( usage & PSA_KEY_USAGE_SIGN ?
298 PSA_SUCCESS :
299 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100300 PSA_ASSERT( psa_mac_verify_setup( &operation,
301 handle, alg ) );
302 PSA_ASSERT( psa_mac_update( &operation,
303 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100304 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
305 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200306 }
307
308 return( 1 );
309
310exit:
311 psa_mac_abort( &operation );
312 return( 0 );
313}
314
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100315static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200316 psa_key_usage_t usage,
317 psa_algorithm_t alg )
318{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000319 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200320 unsigned char iv[16] = {0};
321 size_t iv_length = sizeof( iv );
322 const unsigned char plaintext[16] = "Hello, world...";
323 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
324 size_t ciphertext_length = sizeof( ciphertext );
325 unsigned char decrypted[sizeof( ciphertext )];
326 size_t part_length;
327
328 if( usage & PSA_KEY_USAGE_ENCRYPT )
329 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100330 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
331 handle, alg ) );
332 PSA_ASSERT( psa_cipher_generate_iv( &operation,
333 iv, sizeof( iv ),
334 &iv_length ) );
335 PSA_ASSERT( psa_cipher_update( &operation,
336 plaintext, sizeof( plaintext ),
337 ciphertext, sizeof( ciphertext ),
338 &ciphertext_length ) );
339 PSA_ASSERT( psa_cipher_finish( &operation,
340 ciphertext + ciphertext_length,
341 sizeof( ciphertext ) - ciphertext_length,
342 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200343 ciphertext_length += part_length;
344 }
345
346 if( usage & PSA_KEY_USAGE_DECRYPT )
347 {
348 psa_status_t status;
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700349 psa_key_type_t type = PSA_KEY_TYPE_NONE;
Gilles Peskine818ca122018-06-20 18:16:48 +0200350 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
351 {
Gilles Peskine818ca122018-06-20 18:16:48 +0200352 size_t bits;
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100353 TEST_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200354 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE( type );
355 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100356 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
357 handle, alg ) );
358 PSA_ASSERT( psa_cipher_set_iv( &operation,
359 iv, iv_length ) );
360 PSA_ASSERT( psa_cipher_update( &operation,
361 ciphertext, ciphertext_length,
362 decrypted, sizeof( decrypted ),
363 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200364 status = psa_cipher_finish( &operation,
365 decrypted + part_length,
366 sizeof( decrypted ) - part_length,
367 &part_length );
368 /* For a stream cipher, all inputs are valid. For a block cipher,
369 * if the input is some aribtrary data rather than an actual
370 ciphertext, a padding error is likely. */
Mohammad AboMokh65fa0b82018-06-28 02:14:00 -0700371 if( ( usage & PSA_KEY_USAGE_ENCRYPT ) ||
Mohammad AboMokhadb9b232018-06-28 01:52:54 -0700372 PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) == 1 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100373 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200374 else
375 TEST_ASSERT( status == PSA_SUCCESS ||
376 status == PSA_ERROR_INVALID_PADDING );
377 }
378
379 return( 1 );
380
381exit:
382 psa_cipher_abort( &operation );
383 return( 0 );
384}
385
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100386static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200387 psa_key_usage_t usage,
388 psa_algorithm_t alg )
389{
390 unsigned char nonce[16] = {0};
391 size_t nonce_length = sizeof( nonce );
392 unsigned char plaintext[16] = "Hello, world...";
393 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
394 size_t ciphertext_length = sizeof( ciphertext );
395 size_t plaintext_length = sizeof( ciphertext );
396
397 if( usage & PSA_KEY_USAGE_ENCRYPT )
398 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100399 PSA_ASSERT( psa_aead_encrypt( handle, alg,
400 nonce, nonce_length,
401 NULL, 0,
402 plaintext, sizeof( plaintext ),
403 ciphertext, sizeof( ciphertext ),
404 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200405 }
406
407 if( usage & PSA_KEY_USAGE_DECRYPT )
408 {
409 psa_status_t verify_status =
410 ( usage & PSA_KEY_USAGE_ENCRYPT ?
411 PSA_SUCCESS :
412 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100413 TEST_EQUAL( psa_aead_decrypt( handle, alg,
414 nonce, nonce_length,
415 NULL, 0,
416 ciphertext, ciphertext_length,
417 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100418 &plaintext_length ),
419 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200420 }
421
422 return( 1 );
423
424exit:
425 return( 0 );
426}
427
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100428static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200429 psa_key_usage_t usage,
430 psa_algorithm_t alg )
431{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200432 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
433 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200434 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200435 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100436 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
437
438 /* If the policy allows signing with any hash, just pick one. */
439 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
440 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100441#if defined(KNOWN_SUPPORTED_HASH_ALG)
442 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
443 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100444#else
445 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100446 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100447#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100448 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200449
450 if( usage & PSA_KEY_USAGE_SIGN )
451 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200452 /* Some algorithms require the payload to have the size of
453 * the hash encoded in the algorithm. Use this input size
454 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200455 if( hash_alg != 0 )
456 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100457 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
458 payload, payload_length,
459 signature, sizeof( signature ),
460 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200461 }
462
463 if( usage & PSA_KEY_USAGE_VERIFY )
464 {
465 psa_status_t verify_status =
466 ( usage & PSA_KEY_USAGE_SIGN ?
467 PSA_SUCCESS :
468 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100469 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
470 payload, payload_length,
471 signature, signature_length ),
472 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200473 }
474
475 return( 1 );
476
477exit:
478 return( 0 );
479}
480
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100481static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200482 psa_key_usage_t usage,
483 psa_algorithm_t alg )
484{
485 unsigned char plaintext[256] = "Hello, world...";
486 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
487 size_t ciphertext_length = sizeof( ciphertext );
488 size_t plaintext_length = 16;
489
490 if( usage & PSA_KEY_USAGE_ENCRYPT )
491 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100492 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
493 plaintext, plaintext_length,
494 NULL, 0,
495 ciphertext, sizeof( ciphertext ),
496 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200497 }
498
499 if( usage & PSA_KEY_USAGE_DECRYPT )
500 {
501 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100502 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200503 ciphertext, ciphertext_length,
504 NULL, 0,
505 plaintext, sizeof( plaintext ),
506 &plaintext_length );
507 TEST_ASSERT( status == PSA_SUCCESS ||
508 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
509 ( status == PSA_ERROR_INVALID_ARGUMENT ||
510 status == PSA_ERROR_INVALID_PADDING ) ) );
511 }
512
513 return( 1 );
514
515exit:
516 return( 0 );
517}
Gilles Peskine02b75072018-07-01 22:31:34 +0200518
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100519static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200520 psa_key_usage_t usage,
521 psa_algorithm_t alg )
522{
523 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
524 unsigned char label[16] = "This is a label.";
525 size_t label_length = sizeof( label );
526 unsigned char seed[16] = "abcdefghijklmnop";
527 size_t seed_length = sizeof( seed );
528 unsigned char output[1];
529
530 if( usage & PSA_KEY_USAGE_DERIVE )
531 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100532 PSA_ASSERT( psa_key_derivation( &generator,
533 handle, alg,
534 label, label_length,
535 seed, seed_length,
536 sizeof( output ) ) );
537 PSA_ASSERT( psa_generator_read( &generator,
538 output,
539 sizeof( output ) ) );
540 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200541 }
542
543 return( 1 );
544
545exit:
546 return( 0 );
547}
548
Gilles Peskinec7998b72018-11-07 18:45:02 +0100549/* We need two keys to exercise key agreement. Exercise the
550 * private key against its own public key. */
551static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100552 psa_key_handle_t handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100553 psa_algorithm_t alg )
554{
555 psa_key_type_t private_key_type;
556 psa_key_type_t public_key_type;
557 size_t key_bits;
558 uint8_t *public_key = NULL;
559 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200560 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinec7998b72018-11-07 18:45:02 +0100561 * psa_key_agreement fails. This isn't fully satisfactory, but it's
562 * good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200563 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100564
Gilles Peskine8817f612018-12-18 00:18:46 +0100565 PSA_ASSERT( psa_get_key_information( handle,
566 &private_key_type,
567 &key_bits ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100568 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
569 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
570 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100571 PSA_ASSERT( psa_export_public_key( handle,
572 public_key, public_key_length,
573 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100574
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100575 status = psa_key_agreement( generator, handle,
Gilles Peskinec7998b72018-11-07 18:45:02 +0100576 public_key, public_key_length,
577 alg );
578exit:
579 mbedtls_free( public_key );
580 return( status );
581}
582
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100583static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200584 psa_key_usage_t usage,
585 psa_algorithm_t alg )
586{
587 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200588 unsigned char output[1];
589 int ok = 0;
590
591 if( usage & PSA_KEY_USAGE_DERIVE )
592 {
593 /* We need two keys to exercise key agreement. Exercise the
594 * private key against its own public key. */
Gilles Peskine8817f612018-12-18 00:18:46 +0100595 PSA_ASSERT( key_agreement_with_self( &generator, handle, alg ) );
596 PSA_ASSERT( psa_generator_read( &generator,
597 output,
598 sizeof( output ) ) );
599 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200600 }
601 ok = 1;
602
603exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200604 return( ok );
605}
606
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200607static int is_oid_of_key_type( psa_key_type_t type,
608 const uint8_t *oid, size_t oid_length )
609{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200610 const uint8_t *expected_oid = NULL;
611 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200612#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200613 if( PSA_KEY_TYPE_IS_RSA( type ) )
614 {
615 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
616 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
617 }
618 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200619#endif /* MBEDTLS_RSA_C */
620#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200621 if( PSA_KEY_TYPE_IS_ECC( type ) )
622 {
623 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
624 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
625 }
626 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200627#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200628 {
629 char message[40];
630 mbedtls_snprintf( message, sizeof( message ),
631 "OID not known for key type=0x%08lx",
632 (unsigned long) type );
633 test_fail( message, __LINE__, __FILE__ );
634 return( 0 );
635 }
636
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200637 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200638 return( 1 );
639
640exit:
641 return( 0 );
642}
643
644static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
645 size_t min_bits, size_t max_bits,
646 int must_be_odd )
647{
648 size_t len;
649 size_t actual_bits;
650 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100651 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100652 MBEDTLS_ASN1_INTEGER ),
653 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200654 /* Tolerate a slight departure from DER encoding:
655 * - 0 may be represented by an empty string or a 1-byte string.
656 * - The sign bit may be used as a value bit. */
657 if( ( len == 1 && ( *p )[0] == 0 ) ||
658 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
659 {
660 ++( *p );
661 --len;
662 }
663 if( min_bits == 0 && len == 0 )
664 return( 1 );
665 msb = ( *p )[0];
666 TEST_ASSERT( msb != 0 );
667 actual_bits = 8 * ( len - 1 );
668 while( msb != 0 )
669 {
670 msb >>= 1;
671 ++actual_bits;
672 }
673 TEST_ASSERT( actual_bits >= min_bits );
674 TEST_ASSERT( actual_bits <= max_bits );
675 if( must_be_odd )
676 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
677 *p += len;
678 return( 1 );
679exit:
680 return( 0 );
681}
682
683static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
684 size_t *len,
685 unsigned char n, unsigned char tag )
686{
687 int ret;
688 ret = mbedtls_asn1_get_tag( p, end, len,
689 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
690 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
691 if( ret != 0 )
692 return( ret );
693 end = *p + *len;
694 ret = mbedtls_asn1_get_tag( p, end, len, tag );
695 if( ret != 0 )
696 return( ret );
697 if( *p + *len != end )
698 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
699 return( 0 );
700}
701
702static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
703 uint8_t *exported, size_t exported_length )
704{
705 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100706 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200707 else
708 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200709
710#if defined(MBEDTLS_DES_C)
711 if( type == PSA_KEY_TYPE_DES )
712 {
713 /* Check the parity bits. */
714 unsigned i;
715 for( i = 0; i < bits / 8; i++ )
716 {
717 unsigned bit_count = 0;
718 unsigned m;
719 for( m = 1; m <= 0x100; m <<= 1 )
720 {
721 if( exported[i] & m )
722 ++bit_count;
723 }
724 TEST_ASSERT( bit_count % 2 != 0 );
725 }
726 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200727 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200728#endif
729
730#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
731 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
732 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200733 uint8_t *p = exported;
734 uint8_t *end = exported + exported_length;
735 size_t len;
736 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200737 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200738 * modulus INTEGER, -- n
739 * publicExponent INTEGER, -- e
740 * privateExponent INTEGER, -- d
741 * prime1 INTEGER, -- p
742 * prime2 INTEGER, -- q
743 * exponent1 INTEGER, -- d mod (p-1)
744 * exponent2 INTEGER, -- d mod (q-1)
745 * coefficient INTEGER, -- (inverse of q) mod p
746 * }
747 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100748 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
749 MBEDTLS_ASN1_SEQUENCE |
750 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
751 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200752 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
753 goto exit;
754 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
755 goto exit;
756 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
757 goto exit;
758 /* Require d to be at least half the size of n. */
759 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
760 goto exit;
761 /* Require p and q to be at most half the size of n, rounded up. */
762 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
763 goto exit;
764 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
765 goto exit;
766 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
767 goto exit;
768 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
769 goto exit;
770 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
771 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100772 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100773 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200774 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200775#endif /* MBEDTLS_RSA_C */
776
777#if defined(MBEDTLS_ECP_C)
778 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
779 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100780 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100781 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100782 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200783 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200784#endif /* MBEDTLS_ECP_C */
785
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200786 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
787 {
788 uint8_t *p = exported;
789 uint8_t *end = exported + exported_length;
790 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200791#if defined(MBEDTLS_RSA_C)
792 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
793 {
794 /* RSAPublicKey ::= SEQUENCE {
795 * modulus INTEGER, -- n
796 * publicExponent INTEGER } -- e
797 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100798 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
799 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100800 MBEDTLS_ASN1_CONSTRUCTED ),
801 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100802 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200803 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
804 goto exit;
805 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
806 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100807 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200808 }
809 else
810#endif /* MBEDTLS_RSA_C */
811#if defined(MBEDTLS_ECP_C)
812 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
813 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000814 /* The representation of an ECC public key is:
815 * - The byte 0x04;
816 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
817 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
818 * - where m is the bit size associated with the curve.
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200819 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100820 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
821 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200822 }
823 else
824#endif /* MBEDTLS_ECP_C */
825 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100826 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200827 mbedtls_snprintf( message, sizeof( message ),
828 "No sanity check for public key type=0x%08lx",
829 (unsigned long) type );
830 test_fail( message, __LINE__, __FILE__ );
831 return( 0 );
832 }
833 }
834 else
835
836 {
837 /* No sanity checks for other types */
838 }
839
840 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200841
842exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200843 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200844}
845
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100846static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200847 psa_key_usage_t usage )
848{
849 psa_key_type_t type;
850 size_t bits;
851 uint8_t *exported = NULL;
852 size_t exported_size = 0;
853 size_t exported_length = 0;
854 int ok = 0;
855
Gilles Peskine8817f612018-12-18 00:18:46 +0100856 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200857
858 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
859 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200860 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100861 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
862 PSA_ERROR_NOT_PERMITTED );
Gilles Peskined14664a2018-08-10 19:07:32 +0200863 return( 1 );
864 }
865
Gilles Peskined14664a2018-08-10 19:07:32 +0200866 exported_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200867 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200868
Gilles Peskine8817f612018-12-18 00:18:46 +0100869 PSA_ASSERT( psa_export_key( handle,
870 exported, exported_size,
871 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200872 ok = exported_key_sanity_check( type, bits, exported, exported_length );
873
874exit:
875 mbedtls_free( exported );
876 return( ok );
877}
878
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100879static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200880{
881 psa_key_type_t type;
882 psa_key_type_t public_type;
883 size_t bits;
884 uint8_t *exported = NULL;
885 size_t exported_size = 0;
886 size_t exported_length = 0;
887 int ok = 0;
888
Gilles Peskine8817f612018-12-18 00:18:46 +0100889 PSA_ASSERT( psa_get_key_information( handle, &type, &bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200890 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
891 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100892 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100893 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200894 return( 1 );
895 }
896
897 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
898 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type, bits );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200899 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200900
Gilles Peskine8817f612018-12-18 00:18:46 +0100901 PSA_ASSERT( psa_export_public_key( handle,
902 exported, exported_size,
903 &exported_length ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200904 ok = exported_key_sanity_check( public_type, bits,
905 exported, exported_length );
906
907exit:
908 mbedtls_free( exported );
909 return( ok );
910}
911
Gilles Peskinec9516fb2019-02-05 20:32:06 +0100912/** Do smoke tests on a key.
913 *
914 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
915 * sign/verify, or derivation) that is permitted according to \p usage.
916 * \p usage and \p alg should correspond to the expected policy on the
917 * key.
918 *
919 * Export the key if permitted by \p usage, and check that the output
920 * looks sensible. If \p usage forbids export, check that
921 * \p psa_export_key correctly rejects the attempt. If the key is
922 * asymmetric, also check \p psa_export_public_key.
923 *
924 * If the key fails the tests, this function calls the test framework's
925 * `test_fail` function and returns false. Otherwise this function returns
926 * true. Therefore it should be used as follows:
927 * ```
928 * if( ! exercise_key( ... ) ) goto exit;
929 * ```
930 *
931 * \param handle The key to exercise. It should be capable of performing
932 * \p alg.
933 * \param usage The usage flags to assume.
934 * \param alg The algorithm to exercise.
935 *
936 * \retval 0 The key failed the smoke tests.
937 * \retval 1 The key passed the smoke tests.
938 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100939static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200940 psa_key_usage_t usage,
941 psa_algorithm_t alg )
942{
943 int ok;
944 if( alg == 0 )
945 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
946 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100947 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200948 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100949 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200950 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100951 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200952 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100953 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200954 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100955 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200956 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100957 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200958 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100959 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200960 else
961 {
962 char message[40];
963 mbedtls_snprintf( message, sizeof( message ),
964 "No code to exercise alg=0x%08lx",
965 (unsigned long) alg );
966 test_fail( message, __LINE__, __FILE__ );
967 ok = 0;
968 }
Gilles Peskined14664a2018-08-10 19:07:32 +0200969
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100970 ok = ok && exercise_export_key( handle, usage );
971 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +0200972
Gilles Peskine02b75072018-07-01 22:31:34 +0200973 return( ok );
974}
975
Gilles Peskine10df3412018-10-25 22:35:43 +0200976static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
977 psa_algorithm_t alg )
978{
979 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
980 {
981 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
982 PSA_KEY_USAGE_VERIFY :
983 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
984 }
985 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
986 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
987 {
988 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
989 PSA_KEY_USAGE_ENCRYPT :
990 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
991 }
992 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
993 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
994 {
995 return( PSA_KEY_USAGE_DERIVE );
996 }
997 else
998 {
999 return( 0 );
1000 }
1001
1002}
Darryl Green0c6575a2018-11-07 16:05:30 +00001003
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001004/* An overapproximation of the amount of storage needed for a key of the
1005 * given type and with the given content. The API doesn't make it easy
1006 * to find a good value for the size. The current implementation doesn't
1007 * care about the value anyway. */
1008#define KEY_BITS_FROM_DATA( type, data ) \
1009 ( data )->len
1010
Darryl Green0c6575a2018-11-07 16:05:30 +00001011typedef enum {
1012 IMPORT_KEY = 0,
1013 GENERATE_KEY = 1,
1014 DERIVE_KEY = 2
1015} generate_method;
1016
Gilles Peskinee59236f2018-01-27 23:32:46 +01001017/* END_HEADER */
1018
1019/* BEGIN_DEPENDENCIES
1020 * depends_on:MBEDTLS_PSA_CRYPTO_C
1021 * END_DEPENDENCIES
1022 */
1023
1024/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001025void static_checks( )
1026{
1027 size_t max_truncated_mac_size =
1028 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1029
1030 /* Check that the length for a truncated MAC always fits in the algorithm
1031 * encoding. The shifted mask is the maximum truncated value. The
1032 * untruncated algorithm may be one byte larger. */
1033 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
1034}
1035/* END_CASE */
1036
1037/* BEGIN_CASE */
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001038void import( data_t *data, int type, int expected_status_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001039{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001040 psa_key_handle_t handle = 0;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001041 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001042 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001043
Gilles Peskine8817f612018-12-18 00:18:46 +01001044 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001045
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001046 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001047 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001048 TEST_EQUAL( status, expected_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001049 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001050 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001051
1052exit:
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001053 mbedtls_psa_crypto_free( );
1054}
1055/* END_CASE */
1056
1057/* BEGIN_CASE */
Gilles Peskinea4261682018-12-03 11:34:01 +01001058void import_twice( int alg_arg, int usage_arg,
1059 int type1_arg, data_t *data1,
1060 int expected_import1_status_arg,
1061 int type2_arg, data_t *data2,
1062 int expected_import2_status_arg )
1063{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001064 psa_key_handle_t handle = 0;
Gilles Peskinea4261682018-12-03 11:34:01 +01001065 psa_algorithm_t alg = alg_arg;
1066 psa_key_usage_t usage = usage_arg;
1067 psa_key_type_t type1 = type1_arg;
1068 psa_status_t expected_import1_status = expected_import1_status_arg;
1069 psa_key_type_t type2 = type2_arg;
1070 psa_status_t expected_import2_status = expected_import2_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00001071 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea4261682018-12-03 11:34:01 +01001072 psa_status_t status;
1073
Gilles Peskine8817f612018-12-18 00:18:46 +01001074 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea4261682018-12-03 11:34:01 +01001075
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001076 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea4261682018-12-03 11:34:01 +01001077 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001078 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea4261682018-12-03 11:34:01 +01001079
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001080 status = psa_import_key( handle, type1, data1->x, data1->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001081 TEST_EQUAL( status, expected_import1_status );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001082 status = psa_import_key( handle, type2, data2->x, data2->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001083 TEST_EQUAL( status, expected_import2_status );
Gilles Peskinea4261682018-12-03 11:34:01 +01001084
1085 if( expected_import1_status == PSA_SUCCESS ||
1086 expected_import2_status == PSA_SUCCESS )
1087 {
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001088 if( ! exercise_key( handle, usage, alg ) )
1089 goto exit;
Gilles Peskinea4261682018-12-03 11:34:01 +01001090 }
1091
1092exit:
1093 mbedtls_psa_crypto_free( );
1094}
1095/* END_CASE */
1096
1097/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001098void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1099{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001100 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001101 size_t bits = bits_arg;
1102 psa_status_t expected_status = expected_status_arg;
1103 psa_status_t status;
1104 psa_key_type_t type =
1105 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
1106 size_t buffer_size = /* Slight overapproximations */
1107 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001108 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001109 unsigned char *p;
1110 int ret;
1111 size_t length;
1112
Gilles Peskine8817f612018-12-18 00:18:46 +01001113 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001114 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001115
1116 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1117 bits, keypair ) ) >= 0 );
1118 length = ret;
1119
1120 /* Try importing the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001121 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001122 status = psa_import_key( handle, type, p, length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001123 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001124 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001125 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001126
1127exit:
1128 mbedtls_free( buffer );
1129 mbedtls_psa_crypto_free( );
1130}
1131/* END_CASE */
1132
1133/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001134void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001135 int type_arg,
1136 int alg_arg,
1137 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001138 int expected_bits,
1139 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001140 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001141 int canonical_input )
1142{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001143 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001144 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001145 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001146 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001147 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001148 unsigned char *exported = NULL;
1149 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001150 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001151 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001152 size_t reexported_length;
1153 psa_key_type_t got_type;
1154 size_t got_bits;
Jaeden Amero70261c52019-01-04 11:47:20 +00001155 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001156
Moran Pekercb088e72018-07-17 17:36:59 +03001157 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001158 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001159 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001160 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001161 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001162
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001163 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001164 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001165 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001166
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001167 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
David Saadab4ecc272019-02-14 13:48:10 +02001168 PSA_ERROR_DOES_NOT_EXIST );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001169
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001170 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001171 PSA_ASSERT( psa_import_key( handle, type,
1172 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001173
1174 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001175 PSA_ASSERT( psa_get_key_information( handle,
1176 &got_type,
1177 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001178 TEST_EQUAL( got_type, type );
1179 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001180
1181 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001182 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001183 exported, export_size,
1184 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001185 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001186
1187 /* The exported length must be set by psa_export_key() to a value between 0
1188 * and export_size. On errors, the exported length must be 0. */
1189 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1190 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1191 TEST_ASSERT( exported_length <= export_size );
1192
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001193 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001194 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001195 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001196 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001197 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001198 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001199 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001200
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001201 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001202 goto exit;
1203
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001204 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001205 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001206 else
1207 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001208 psa_key_handle_t handle2;
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001209 PSA_ASSERT( psa_allocate_key( &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001210 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001211
Gilles Peskine8817f612018-12-18 00:18:46 +01001212 PSA_ASSERT( psa_import_key( handle2, type,
1213 exported,
1214 exported_length ) );
1215 PSA_ASSERT( psa_export_key( handle2,
1216 reexported,
1217 export_size,
1218 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001219 ASSERT_COMPARE( exported, exported_length,
1220 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001221 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001222 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001223 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001224
1225destroy:
1226 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001227 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001228 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1229 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001230
1231exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001232 mbedtls_free( exported );
1233 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001234 mbedtls_psa_crypto_free( );
1235}
1236/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001237
Moran Pekerf709f4a2018-06-06 17:26:04 +03001238/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001239void import_key_nonempty_slot( )
1240{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001241 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001242 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1243 psa_status_t status;
1244 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001245 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001246
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001247 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001248
Moran Peker28a38e62018-11-07 16:18:24 +02001249 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001250 PSA_ASSERT( psa_import_key( handle, type,
1251 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001252
1253 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001254 status = psa_import_key( handle, type, data, sizeof( data ) );
David Saadab4ecc272019-02-14 13:48:10 +02001255 TEST_EQUAL( status, PSA_ERROR_ALREADY_EXISTS );
Moran Peker28a38e62018-11-07 16:18:24 +02001256
1257exit:
1258 mbedtls_psa_crypto_free( );
1259}
1260/* END_CASE */
1261
1262/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001263void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001264{
1265 psa_status_t status;
1266 unsigned char *exported = NULL;
1267 size_t export_size = 0;
1268 size_t exported_length = INVALID_EXPORT_LENGTH;
1269 psa_status_t expected_export_status = expected_export_status_arg;
1270
Gilles Peskine8817f612018-12-18 00:18:46 +01001271 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001272
1273 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001274 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001275 exported, export_size,
1276 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001277 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001278
1279exit:
1280 mbedtls_psa_crypto_free( );
1281}
1282/* END_CASE */
1283
1284/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001285void export_with_no_key_activity( )
1286{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001287 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001288 psa_algorithm_t alg = PSA_ALG_CTR;
1289 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001290 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001291 unsigned char *exported = NULL;
1292 size_t export_size = 0;
1293 size_t exported_length = INVALID_EXPORT_LENGTH;
1294
Gilles Peskine8817f612018-12-18 00:18:46 +01001295 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001296
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001297 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001298 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001299 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001300
1301 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001302 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001303 exported, export_size,
1304 &exported_length );
David Saadab4ecc272019-02-14 13:48:10 +02001305 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Peker34550092018-11-07 16:19:34 +02001306
1307exit:
1308 mbedtls_psa_crypto_free( );
1309}
1310/* END_CASE */
1311
1312/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001313void cipher_with_no_key_activity( )
1314{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001315 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001316 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001317 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001318 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001319 int exercise_alg = PSA_ALG_CTR;
1320
Gilles Peskine8817f612018-12-18 00:18:46 +01001321 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001322
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001323 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001324 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001325 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001326
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001327 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
David Saadab4ecc272019-02-14 13:48:10 +02001328 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Pekerce500072018-11-07 16:20:07 +02001329
1330exit:
1331 psa_cipher_abort( &operation );
1332 mbedtls_psa_crypto_free( );
1333}
1334/* END_CASE */
1335
1336/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001337void export_after_import_failure( data_t *data, int type_arg,
1338 int expected_import_status_arg )
1339{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001340 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001341 psa_key_type_t type = type_arg;
1342 psa_status_t status;
1343 unsigned char *exported = NULL;
1344 size_t export_size = 0;
1345 psa_status_t expected_import_status = expected_import_status_arg;
1346 size_t exported_length = INVALID_EXPORT_LENGTH;
1347
Gilles Peskine8817f612018-12-18 00:18:46 +01001348 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001349
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001350 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001351
Moran Peker34550092018-11-07 16:19:34 +02001352 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001353 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001354 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001355 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001356
1357 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001358 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001359 exported, export_size,
1360 &exported_length );
David Saadab4ecc272019-02-14 13:48:10 +02001361 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Peker34550092018-11-07 16:19:34 +02001362
1363exit:
1364 mbedtls_psa_crypto_free( );
1365}
1366/* END_CASE */
1367
1368/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001369void cipher_after_import_failure( data_t *data, int type_arg,
1370 int expected_import_status_arg )
1371{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001372 psa_key_handle_t handle = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001373 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001374 psa_key_type_t type = type_arg;
1375 psa_status_t status;
1376 psa_status_t expected_import_status = expected_import_status_arg;
1377 int exercise_alg = PSA_ALG_CTR;
1378
Gilles Peskine8817f612018-12-18 00:18:46 +01001379 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001380
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001381 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001382
Moran Pekerce500072018-11-07 16:20:07 +02001383 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001384 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001385 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001386 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001387
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001388 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
David Saadab4ecc272019-02-14 13:48:10 +02001389 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Pekerce500072018-11-07 16:20:07 +02001390
1391exit:
1392 psa_cipher_abort( &operation );
1393 mbedtls_psa_crypto_free( );
1394}
1395/* END_CASE */
1396
1397/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001398void export_after_destroy_key( data_t *data, int type_arg )
1399{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001400 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001401 psa_key_type_t type = type_arg;
1402 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001403 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001404 psa_algorithm_t alg = PSA_ALG_CTR;
1405 unsigned char *exported = NULL;
1406 size_t export_size = 0;
1407 size_t exported_length = INVALID_EXPORT_LENGTH;
1408
Gilles Peskine8817f612018-12-18 00:18:46 +01001409 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001410
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001411 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001412 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001413 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001414 export_size = (ptrdiff_t) data->len;
1415 ASSERT_ALLOC( exported, export_size );
1416
1417 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001418 PSA_ASSERT( psa_import_key( handle, type,
1419 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001420
Gilles Peskine8817f612018-12-18 00:18:46 +01001421 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1422 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001423
1424 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001425 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001426
1427 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001428 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001429 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001430 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001431
1432exit:
1433 mbedtls_free( exported );
1434 mbedtls_psa_crypto_free( );
1435}
1436/* END_CASE */
1437
1438/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001439void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001440 int type_arg,
1441 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001442 int export_size_delta,
1443 int expected_export_status_arg,
1444 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001445{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001446 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001447 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001448 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001449 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001450 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001451 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001452 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001453 size_t exported_length = INVALID_EXPORT_LENGTH;
Jaeden Amero70261c52019-01-04 11:47:20 +00001454 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001455
Gilles Peskine8817f612018-12-18 00:18:46 +01001456 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001457
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001458 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001459 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001460 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001461
1462 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001463 PSA_ASSERT( psa_import_key( handle, type,
1464 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001465
Gilles Peskine49c25912018-10-29 15:15:31 +01001466 /* Export the public key */
1467 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001468 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001469 exported, export_size,
1470 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001471 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001472 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001473 {
1474 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1475 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001476 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001477 TEST_ASSERT( expected_public_key->len <=
1478 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001479 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1480 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001481 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001482
1483exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001484 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001485 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001486 mbedtls_psa_crypto_free( );
1487}
1488/* END_CASE */
1489
Gilles Peskine20035e32018-02-03 22:44:14 +01001490/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001491void import_and_exercise_key( data_t *data,
1492 int type_arg,
1493 int bits_arg,
1494 int alg_arg )
1495{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001496 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001497 psa_key_type_t type = type_arg;
1498 size_t bits = bits_arg;
1499 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001500 psa_key_usage_t usage = usage_to_exercise( type, alg );
Jaeden Amero70261c52019-01-04 11:47:20 +00001501 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001502 psa_key_type_t got_type;
1503 size_t got_bits;
1504 psa_status_t status;
1505
Gilles Peskine8817f612018-12-18 00:18:46 +01001506 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001507
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001508 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001509 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001510 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001511
1512 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001513 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001514 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001515
1516 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001517 PSA_ASSERT( psa_get_key_information( handle,
1518 &got_type,
1519 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001520 TEST_EQUAL( got_type, type );
1521 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001522
1523 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001524 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001525 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001526
1527exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001528 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001529 mbedtls_psa_crypto_free( );
1530}
1531/* END_CASE */
1532
1533/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001534void key_policy( int usage_arg, int alg_arg )
1535{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001536 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001537 psa_algorithm_t alg = alg_arg;
1538 psa_key_usage_t usage = usage_arg;
1539 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1540 unsigned char key[32] = {0};
Jaeden Amero70261c52019-01-04 11:47:20 +00001541 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
1542 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001543
1544 memset( key, 0x2a, sizeof( key ) );
1545
Gilles Peskine8817f612018-12-18 00:18:46 +01001546 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001547
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001548 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001549 psa_key_policy_set_usage( &policy_set, usage, alg );
1550
Gilles Peskinefe11b722018-12-18 00:24:04 +01001551 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1552 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001553 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001554
Gilles Peskine8817f612018-12-18 00:18:46 +01001555 PSA_ASSERT( psa_import_key( handle, key_type,
1556 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001557
Gilles Peskine8817f612018-12-18 00:18:46 +01001558 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001559
Gilles Peskinefe11b722018-12-18 00:24:04 +01001560 TEST_EQUAL( policy_get.usage, policy_set.usage );
1561 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001562
1563exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001564 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001565 mbedtls_psa_crypto_free( );
1566}
1567/* END_CASE */
1568
1569/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001570void key_policy_init( )
1571{
1572 /* Test each valid way of initializing the object, except for `= {0}`, as
1573 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1574 * though it's OK by the C standard. We could test for this, but we'd need
1575 * to supress the Clang warning for the test. */
1576 psa_key_policy_t func = psa_key_policy_init( );
1577 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1578 psa_key_policy_t zero;
1579
1580 memset( &zero, 0, sizeof( zero ) );
1581
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001582 /* A default key policy should not permit any usage. */
1583 TEST_EQUAL( psa_key_policy_get_usage( &func ), 0 );
1584 TEST_EQUAL( psa_key_policy_get_usage( &init ), 0 );
1585 TEST_EQUAL( psa_key_policy_get_usage( &zero ), 0 );
1586
1587 /* A default key policy should not permit any algorithm. */
1588 TEST_EQUAL( psa_key_policy_get_algorithm( &func ), 0 );
1589 TEST_EQUAL( psa_key_policy_get_algorithm( &init ), 0 );
1590 TEST_EQUAL( psa_key_policy_get_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001591}
1592/* END_CASE */
1593
1594/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001595void mac_key_policy( int policy_usage,
1596 int policy_alg,
1597 int key_type,
1598 data_t *key_data,
1599 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001600{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001601 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001602 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001603 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001604 psa_status_t status;
1605 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001606
Gilles Peskine8817f612018-12-18 00:18:46 +01001607 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001608
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001609 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001610 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001611 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001612
Gilles Peskine8817f612018-12-18 00:18:46 +01001613 PSA_ASSERT( psa_import_key( handle, key_type,
1614 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001615
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001616 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001617 if( policy_alg == exercise_alg &&
1618 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001619 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001620 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001621 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001622 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001623
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001624 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001625 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001626 if( policy_alg == exercise_alg &&
1627 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001628 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001629 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001630 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001631
1632exit:
1633 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001634 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001635 mbedtls_psa_crypto_free( );
1636}
1637/* END_CASE */
1638
1639/* BEGIN_CASE */
1640void cipher_key_policy( int policy_usage,
1641 int policy_alg,
1642 int key_type,
1643 data_t *key_data,
1644 int exercise_alg )
1645{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001646 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001647 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001648 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001649 psa_status_t status;
1650
Gilles Peskine8817f612018-12-18 00:18:46 +01001651 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001652
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001653 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001654 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001655 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001656
Gilles Peskine8817f612018-12-18 00:18:46 +01001657 PSA_ASSERT( psa_import_key( handle, key_type,
1658 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001659
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001660 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001661 if( policy_alg == exercise_alg &&
1662 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001663 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001664 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001665 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001666 psa_cipher_abort( &operation );
1667
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001668 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001669 if( policy_alg == exercise_alg &&
1670 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001671 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001672 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001673 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001674
1675exit:
1676 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001677 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001678 mbedtls_psa_crypto_free( );
1679}
1680/* END_CASE */
1681
1682/* BEGIN_CASE */
1683void aead_key_policy( int policy_usage,
1684 int policy_alg,
1685 int key_type,
1686 data_t *key_data,
1687 int nonce_length_arg,
1688 int tag_length_arg,
1689 int exercise_alg )
1690{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001691 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001692 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001693 psa_status_t status;
1694 unsigned char nonce[16] = {0};
1695 size_t nonce_length = nonce_length_arg;
1696 unsigned char tag[16];
1697 size_t tag_length = tag_length_arg;
1698 size_t output_length;
1699
1700 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1701 TEST_ASSERT( tag_length <= sizeof( tag ) );
1702
Gilles Peskine8817f612018-12-18 00:18:46 +01001703 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001704
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001705 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001706 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001707 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001708
Gilles Peskine8817f612018-12-18 00:18:46 +01001709 PSA_ASSERT( psa_import_key( handle, key_type,
1710 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001711
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001712 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001713 nonce, nonce_length,
1714 NULL, 0,
1715 NULL, 0,
1716 tag, tag_length,
1717 &output_length );
1718 if( policy_alg == exercise_alg &&
1719 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001720 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001721 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001722 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001723
1724 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001725 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001726 nonce, nonce_length,
1727 NULL, 0,
1728 tag, tag_length,
1729 NULL, 0,
1730 &output_length );
1731 if( policy_alg == exercise_alg &&
1732 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001733 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001734 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001735 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001736
1737exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001738 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001739 mbedtls_psa_crypto_free( );
1740}
1741/* END_CASE */
1742
1743/* BEGIN_CASE */
1744void asymmetric_encryption_key_policy( int policy_usage,
1745 int policy_alg,
1746 int key_type,
1747 data_t *key_data,
1748 int exercise_alg )
1749{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001750 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001751 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001752 psa_status_t status;
1753 size_t key_bits;
1754 size_t buffer_length;
1755 unsigned char *buffer = NULL;
1756 size_t output_length;
1757
Gilles Peskine8817f612018-12-18 00:18:46 +01001758 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001759
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001760 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001761 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001762 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001763
Gilles Peskine8817f612018-12-18 00:18:46 +01001764 PSA_ASSERT( psa_import_key( handle, key_type,
1765 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001766
Gilles Peskine8817f612018-12-18 00:18:46 +01001767 PSA_ASSERT( psa_get_key_information( handle,
1768 NULL,
1769 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001770 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1771 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001772 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001773
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001774 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001775 NULL, 0,
1776 NULL, 0,
1777 buffer, buffer_length,
1778 &output_length );
1779 if( policy_alg == exercise_alg &&
1780 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001781 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001782 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001783 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001784
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001785 if( buffer_length != 0 )
1786 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001787 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001788 buffer, buffer_length,
1789 NULL, 0,
1790 buffer, buffer_length,
1791 &output_length );
1792 if( policy_alg == exercise_alg &&
1793 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001794 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
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
1798exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001799 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001800 mbedtls_psa_crypto_free( );
1801 mbedtls_free( buffer );
1802}
1803/* END_CASE */
1804
1805/* BEGIN_CASE */
1806void asymmetric_signature_key_policy( int policy_usage,
1807 int policy_alg,
1808 int key_type,
1809 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001810 int exercise_alg,
1811 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001812{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001813 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001814 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001815 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001816 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1817 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1818 * compatible with the policy and `payload_length_arg` is supposed to be
1819 * a valid input length to sign. If `payload_length_arg <= 0`,
1820 * `exercise_alg` is supposed to be forbidden by the policy. */
1821 int compatible_alg = payload_length_arg > 0;
1822 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001823 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1824 size_t signature_length;
1825
Gilles Peskine8817f612018-12-18 00:18:46 +01001826 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001827
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001828 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001829 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001830 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001831
Gilles Peskine8817f612018-12-18 00:18:46 +01001832 PSA_ASSERT( psa_import_key( handle, key_type,
1833 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001834
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001835 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001836 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001837 signature, sizeof( signature ),
1838 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001839 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001840 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001841 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001842 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001843
1844 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001845 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001846 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001847 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001848 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001849 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001850 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001851 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001852
1853exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001854 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001855 mbedtls_psa_crypto_free( );
1856}
1857/* END_CASE */
1858
1859/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001860void derive_key_policy( int policy_usage,
1861 int policy_alg,
1862 int key_type,
1863 data_t *key_data,
1864 int exercise_alg )
1865{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001866 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001867 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001868 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1869 psa_status_t status;
1870
Gilles Peskine8817f612018-12-18 00:18:46 +01001871 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001872
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001873 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001874 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001875 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001876
Gilles Peskine8817f612018-12-18 00:18:46 +01001877 PSA_ASSERT( psa_import_key( handle, key_type,
1878 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001879
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001880 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001881 exercise_alg,
1882 NULL, 0,
1883 NULL, 0,
1884 1 );
1885 if( policy_alg == exercise_alg &&
1886 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001887 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001888 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001889 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001890
1891exit:
1892 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001893 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001894 mbedtls_psa_crypto_free( );
1895}
1896/* END_CASE */
1897
1898/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001899void agreement_key_policy( int policy_usage,
1900 int policy_alg,
1901 int key_type_arg,
1902 data_t *key_data,
1903 int exercise_alg )
1904{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001905 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001906 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001907 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001908 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1909 psa_status_t status;
1910
Gilles Peskine8817f612018-12-18 00:18:46 +01001911 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001912
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001913 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001914 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001915 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001916
Gilles Peskine8817f612018-12-18 00:18:46 +01001917 PSA_ASSERT( psa_import_key( handle, key_type,
1918 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001919
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001920 status = key_agreement_with_self( &generator, handle, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001921
Gilles Peskine01d718c2018-09-18 12:01:02 +02001922 if( policy_alg == exercise_alg &&
1923 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001924 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001925 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001926 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001927
1928exit:
1929 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001930 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001931 mbedtls_psa_crypto_free( );
1932}
1933/* END_CASE */
1934
1935/* BEGIN_CASE */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001936void key_policy_alg2( int key_type_arg, data_t *key_data,
1937 int usage_arg, int alg_arg, int alg2_arg )
1938{
1939 psa_key_handle_t handle = 0;
1940 psa_key_type_t key_type = key_type_arg;
1941 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
1942 psa_key_policy_t got_policy = PSA_KEY_POLICY_INIT;
1943 psa_key_usage_t usage = usage_arg;
1944 psa_algorithm_t alg = alg_arg;
1945 psa_algorithm_t alg2 = alg2_arg;
1946
1947 PSA_ASSERT( psa_crypto_init( ) );
1948
1949 PSA_ASSERT( psa_allocate_key( &handle ) );
1950 psa_key_policy_set_usage( &policy, usage, alg );
1951 psa_key_policy_set_enrollment_algorithm( &policy, alg2 );
1952 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
1953 PSA_ASSERT( psa_import_key( handle, key_type,
1954 key_data->x, key_data->len ) );
1955
1956 PSA_ASSERT( psa_get_key_policy( handle, &got_policy ) );
1957 TEST_EQUAL( psa_key_policy_get_usage( &got_policy ), usage );
1958 TEST_EQUAL( psa_key_policy_get_algorithm( &got_policy ), alg );
1959 TEST_EQUAL( psa_key_policy_get_enrollment_algorithm( &got_policy ), alg2 );
1960
1961 if( ! exercise_key( handle, usage, alg ) )
1962 goto exit;
1963 if( ! exercise_key( handle, usage, alg2 ) )
1964 goto exit;
1965
1966exit:
1967 psa_destroy_key( handle );
1968 mbedtls_psa_crypto_free( );
1969}
1970/* END_CASE */
1971
1972/* BEGIN_CASE */
Gilles Peskine57ab7212019-01-28 13:03:09 +01001973void copy_key_policy( int source_usage_arg, int source_alg_arg,
1974 int type_arg, data_t *material,
1975 int target_usage_arg, int target_alg_arg,
1976 int constraint_usage_arg, int constraint_alg_arg,
1977 int expected_usage_arg, int expected_alg_arg )
1978{
1979 psa_key_usage_t source_usage = source_usage_arg;
1980 psa_algorithm_t source_alg = source_alg_arg;
1981 psa_key_handle_t source_handle = 0;
1982 psa_key_policy_t source_policy = PSA_KEY_POLICY_INIT;
1983 psa_key_type_t source_type = type_arg;
1984 size_t source_bits;
1985 psa_key_usage_t target_usage = target_usage_arg;
1986 psa_algorithm_t target_alg = target_alg_arg;
1987 psa_key_handle_t target_handle = 0;
1988 psa_key_policy_t target_policy = PSA_KEY_POLICY_INIT;
1989 psa_key_type_t target_type;
1990 size_t target_bits;
1991 psa_key_usage_t constraint_usage = constraint_usage_arg;
1992 psa_algorithm_t constraint_alg = constraint_alg_arg;
1993 psa_key_policy_t constraint = PSA_KEY_POLICY_INIT;
1994 psa_key_policy_t *p_constraint = NULL;
1995 psa_key_usage_t expected_usage = expected_usage_arg;
1996 psa_algorithm_t expected_alg = expected_alg_arg;
1997 uint8_t *export_buffer = NULL;
1998
1999 if( constraint_usage_arg != -1 )
2000 {
2001 p_constraint = &constraint;
2002 psa_key_policy_set_usage( p_constraint,
2003 constraint_usage, constraint_alg );
2004 }
2005
2006 PSA_ASSERT( psa_crypto_init( ) );
2007
2008 /* Populate the source slot. */
2009 PSA_ASSERT( psa_allocate_key( &source_handle ) );
2010 psa_key_policy_set_usage( &source_policy, source_usage, source_alg );
2011 PSA_ASSERT( psa_set_key_policy( source_handle, &source_policy ) );
2012 PSA_ASSERT( psa_import_key( source_handle, source_type,
2013 material->x, material->len ) );
2014 PSA_ASSERT( psa_get_key_information( source_handle, NULL, &source_bits ) );
2015
2016 /* Prepare the target slot. */
2017 PSA_ASSERT( psa_allocate_key( &target_handle ) );
2018 psa_key_policy_set_usage( &target_policy, target_usage, target_alg );
2019 PSA_ASSERT( psa_set_key_policy( target_handle, &target_policy ) );
2020 target_policy = psa_key_policy_init();
2021
2022 /* Copy the key. */
2023 PSA_ASSERT( psa_copy_key( source_handle, target_handle, p_constraint ) );
2024
2025 /* Destroy the source to ensure that this doesn't affect the target. */
2026 PSA_ASSERT( psa_destroy_key( source_handle ) );
2027
2028 /* Test that the target slot has the expected content and policy. */
2029 PSA_ASSERT( psa_get_key_information( target_handle,
2030 &target_type, &target_bits ) );
2031 TEST_EQUAL( source_type, target_type );
2032 TEST_EQUAL( source_bits, target_bits );
2033 PSA_ASSERT( psa_get_key_policy( target_handle, &target_policy ) );
2034 TEST_EQUAL( expected_usage, psa_key_policy_get_usage( &target_policy ) );
2035 TEST_EQUAL( expected_alg, psa_key_policy_get_algorithm( &target_policy ) );
2036 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2037 {
2038 size_t length;
2039 ASSERT_ALLOC( export_buffer, material->len );
2040 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2041 material->len, &length ) );
2042 ASSERT_COMPARE( material->x, material->len,
2043 export_buffer, length );
2044 }
2045 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2046 goto exit;
2047
2048 PSA_ASSERT( psa_close_key( target_handle ) );
2049
2050exit:
2051 mbedtls_psa_crypto_free( );
2052 mbedtls_free( export_buffer );
2053}
2054/* END_CASE */
2055
2056/* BEGIN_CASE */
2057void copy_fail( int source_usage_arg, int source_alg_arg,
2058 int type_arg, data_t *material,
2059 int target_usage_arg, int target_alg_arg,
2060 int constraint_usage_arg, int constraint_alg_arg,
2061 int expected_status_arg )
2062{
2063 /* Test copy failure into an empty slot. There is a test for copy failure
2064 * into an occupied slot in
2065 * test_suite_psa_crypto_slot_management.function. */
2066
2067 psa_key_usage_t source_usage = source_usage_arg;
2068 psa_algorithm_t source_alg = source_alg_arg;
2069 psa_key_handle_t source_handle = 0;
2070 psa_key_policy_t source_policy = PSA_KEY_POLICY_INIT;
2071 psa_key_type_t source_type = type_arg;
2072 size_t source_bits;
2073 psa_key_usage_t target_usage = target_usage_arg;
2074 psa_algorithm_t target_alg = target_alg_arg;
2075 psa_key_handle_t target_handle = 0;
2076 psa_key_policy_t target_policy = PSA_KEY_POLICY_INIT;
2077 psa_key_type_t target_type;
2078 size_t target_bits;
2079 psa_key_usage_t constraint_usage = constraint_usage_arg;
2080 psa_algorithm_t constraint_alg = constraint_alg_arg;
2081 psa_key_policy_t constraint = PSA_KEY_POLICY_INIT;
2082 psa_key_policy_t *p_constraint = NULL;
2083 psa_status_t expected_status = expected_status_arg;
2084
2085 if( constraint_usage_arg != -1 )
2086 {
2087 p_constraint = &constraint;
2088 psa_key_policy_set_usage( p_constraint,
2089 constraint_usage, constraint_alg );
2090 }
2091
2092 PSA_ASSERT( psa_crypto_init( ) );
2093
2094 /* Populate the source slot. */
2095 PSA_ASSERT( psa_allocate_key( &source_handle ) );
2096 psa_key_policy_set_usage( &source_policy, source_usage, source_alg );
2097 PSA_ASSERT( psa_set_key_policy( source_handle, &source_policy ) );
2098 PSA_ASSERT( psa_import_key( source_handle, source_type,
2099 material->x, material->len ) );
2100 PSA_ASSERT( psa_get_key_information( source_handle, NULL, &source_bits ) );
2101
2102 /* Prepare the target slot. */
2103 PSA_ASSERT( psa_allocate_key( &target_handle ) );
2104 psa_key_policy_set_usage( &target_policy, target_usage, target_alg );
2105 PSA_ASSERT( psa_set_key_policy( target_handle, &target_policy ) );
2106 target_policy = psa_key_policy_init();
2107
2108 /* Copy the key. */
2109 TEST_EQUAL( psa_copy_key( source_handle, target_handle, p_constraint ),
2110 expected_status );
2111
2112 /* Test that the target slot is unaffected. */
2113 TEST_EQUAL( psa_get_key_information( target_handle,
2114 &target_type, &target_bits ),
David Saadab4ecc272019-02-14 13:48:10 +02002115 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002116 PSA_ASSERT( psa_get_key_policy( target_handle, &target_policy ) );
2117 TEST_EQUAL( target_usage, psa_key_policy_get_usage( &target_policy ) );
2118 TEST_EQUAL( target_alg, psa_key_policy_get_algorithm( &target_policy ) );
2119
2120exit:
2121 mbedtls_psa_crypto_free( );
2122}
2123/* END_CASE */
2124
2125/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002126void hash_operation_init( )
2127{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002128 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002129 /* Test each valid way of initializing the object, except for `= {0}`, as
2130 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2131 * though it's OK by the C standard. We could test for this, but we'd need
2132 * to supress the Clang warning for the test. */
2133 psa_hash_operation_t func = psa_hash_operation_init( );
2134 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2135 psa_hash_operation_t zero;
2136
2137 memset( &zero, 0, sizeof( zero ) );
2138
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002139 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002140 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2141 PSA_ERROR_BAD_STATE );
2142 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2143 PSA_ERROR_BAD_STATE );
2144 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2145 PSA_ERROR_BAD_STATE );
2146
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002147 /* A default hash operation should be abortable without error. */
2148 PSA_ASSERT( psa_hash_abort( &func ) );
2149 PSA_ASSERT( psa_hash_abort( &init ) );
2150 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002151}
2152/* END_CASE */
2153
2154/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002155void hash_setup( int alg_arg,
2156 int expected_status_arg )
2157{
2158 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002159 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002160 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002161 psa_status_t status;
2162
Gilles Peskine8817f612018-12-18 00:18:46 +01002163 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002164
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002165 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002166 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002167
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002168 /* Whether setup succeeded or failed, abort must succeed. */
2169 PSA_ASSERT( psa_hash_abort( &operation ) );
2170
2171 /* If setup failed, reproduce the failure, so as to
2172 * test the resulting state of the operation object. */
2173 if( status != PSA_SUCCESS )
2174 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2175
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002176 /* Now the operation object should be reusable. */
2177#if defined(KNOWN_SUPPORTED_HASH_ALG)
2178 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2179 PSA_ASSERT( psa_hash_abort( &operation ) );
2180#endif
2181
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002182exit:
2183 mbedtls_psa_crypto_free( );
2184}
2185/* END_CASE */
2186
2187/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002188void hash_bad_order( )
2189{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002190 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002191 unsigned char input[] = "";
2192 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002193 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002194 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2195 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2196 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002197 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002198 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002199 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002200
Gilles Peskine8817f612018-12-18 00:18:46 +01002201 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002202
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002203 /* Call setup twice in a row. */
2204 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2205 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2206 PSA_ERROR_BAD_STATE );
2207 PSA_ASSERT( psa_hash_abort( &operation ) );
2208
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002209 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002210 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002211 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002212 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002213
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002214 /* Call update after finish. */
2215 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2216 PSA_ASSERT( psa_hash_finish( &operation,
2217 hash, sizeof( hash ), &hash_len ) );
2218 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002219 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002220 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002221
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002222 /* Call verify without calling setup beforehand. */
2223 TEST_EQUAL( psa_hash_verify( &operation,
2224 valid_hash, sizeof( valid_hash ) ),
2225 PSA_ERROR_BAD_STATE );
2226 PSA_ASSERT( psa_hash_abort( &operation ) );
2227
2228 /* Call verify after finish. */
2229 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2230 PSA_ASSERT( psa_hash_finish( &operation,
2231 hash, sizeof( hash ), &hash_len ) );
2232 TEST_EQUAL( psa_hash_verify( &operation,
2233 valid_hash, sizeof( valid_hash ) ),
2234 PSA_ERROR_BAD_STATE );
2235 PSA_ASSERT( psa_hash_abort( &operation ) );
2236
2237 /* Call verify twice in a row. */
2238 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2239 PSA_ASSERT( psa_hash_verify( &operation,
2240 valid_hash, sizeof( valid_hash ) ) );
2241 TEST_EQUAL( psa_hash_verify( &operation,
2242 valid_hash, sizeof( valid_hash ) ),
2243 PSA_ERROR_BAD_STATE );
2244 PSA_ASSERT( psa_hash_abort( &operation ) );
2245
2246 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002247 TEST_EQUAL( psa_hash_finish( &operation,
2248 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002249 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002250 PSA_ASSERT( psa_hash_abort( &operation ) );
2251
2252 /* Call finish twice in a row. */
2253 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2254 PSA_ASSERT( psa_hash_finish( &operation,
2255 hash, sizeof( hash ), &hash_len ) );
2256 TEST_EQUAL( psa_hash_finish( &operation,
2257 hash, sizeof( hash ), &hash_len ),
2258 PSA_ERROR_BAD_STATE );
2259 PSA_ASSERT( psa_hash_abort( &operation ) );
2260
2261 /* Call finish after calling verify. */
2262 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2263 PSA_ASSERT( psa_hash_verify( &operation,
2264 valid_hash, sizeof( valid_hash ) ) );
2265 TEST_EQUAL( psa_hash_finish( &operation,
2266 hash, sizeof( hash ), &hash_len ),
2267 PSA_ERROR_BAD_STATE );
2268 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002269
2270exit:
2271 mbedtls_psa_crypto_free( );
2272}
2273/* END_CASE */
2274
itayzafrir27e69452018-11-01 14:26:34 +02002275/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2276void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002277{
2278 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002279 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2280 * appended to it */
2281 unsigned char hash[] = {
2282 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2283 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2284 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002285 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002286 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002287
Gilles Peskine8817f612018-12-18 00:18:46 +01002288 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002289
itayzafrir27e69452018-11-01 14:26:34 +02002290 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002291 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002292 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002293 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002294
itayzafrir27e69452018-11-01 14:26:34 +02002295 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002296 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002297 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002298 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002299
itayzafrir27e69452018-11-01 14:26:34 +02002300 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002301 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002302 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002303 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002304
itayzafrirec93d302018-10-18 18:01:10 +03002305exit:
2306 mbedtls_psa_crypto_free( );
2307}
2308/* END_CASE */
2309
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002310/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2311void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002312{
2313 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002314 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002315 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002316 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002317 size_t hash_len;
2318
Gilles Peskine8817f612018-12-18 00:18:46 +01002319 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002320
itayzafrir58028322018-10-25 10:22:01 +03002321 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002322 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002323 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002324 hash, expected_size - 1, &hash_len ),
2325 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002326
2327exit:
2328 mbedtls_psa_crypto_free( );
2329}
2330/* END_CASE */
2331
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002332/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2333void hash_clone_source_state( )
2334{
2335 psa_algorithm_t alg = PSA_ALG_SHA_256;
2336 unsigned char hash[PSA_HASH_MAX_SIZE];
2337 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2338 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2339 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2340 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2341 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2342 size_t hash_len;
2343
2344 PSA_ASSERT( psa_crypto_init( ) );
2345 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2346
2347 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2348 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2349 PSA_ASSERT( psa_hash_finish( &op_finished,
2350 hash, sizeof( hash ), &hash_len ) );
2351 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2352 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2353
2354 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2355 PSA_ERROR_BAD_STATE );
2356
2357 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2358 PSA_ASSERT( psa_hash_finish( &op_init,
2359 hash, sizeof( hash ), &hash_len ) );
2360 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2361 PSA_ASSERT( psa_hash_finish( &op_finished,
2362 hash, sizeof( hash ), &hash_len ) );
2363 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2364 PSA_ASSERT( psa_hash_finish( &op_aborted,
2365 hash, sizeof( hash ), &hash_len ) );
2366
2367exit:
2368 psa_hash_abort( &op_source );
2369 psa_hash_abort( &op_init );
2370 psa_hash_abort( &op_setup );
2371 psa_hash_abort( &op_finished );
2372 psa_hash_abort( &op_aborted );
2373 mbedtls_psa_crypto_free( );
2374}
2375/* END_CASE */
2376
2377/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2378void hash_clone_target_state( )
2379{
2380 psa_algorithm_t alg = PSA_ALG_SHA_256;
2381 unsigned char hash[PSA_HASH_MAX_SIZE];
2382 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2383 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2384 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2385 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2386 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2387 size_t hash_len;
2388
2389 PSA_ASSERT( psa_crypto_init( ) );
2390
2391 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2392 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2393 PSA_ASSERT( psa_hash_finish( &op_finished,
2394 hash, sizeof( hash ), &hash_len ) );
2395 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2396 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2397
2398 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2399 PSA_ASSERT( psa_hash_finish( &op_target,
2400 hash, sizeof( hash ), &hash_len ) );
2401
2402 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2403 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2404 PSA_ERROR_BAD_STATE );
2405 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2406 PSA_ERROR_BAD_STATE );
2407
2408exit:
2409 psa_hash_abort( &op_target );
2410 psa_hash_abort( &op_init );
2411 psa_hash_abort( &op_setup );
2412 psa_hash_abort( &op_finished );
2413 psa_hash_abort( &op_aborted );
2414 mbedtls_psa_crypto_free( );
2415}
2416/* END_CASE */
2417
itayzafrir58028322018-10-25 10:22:01 +03002418/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002419void mac_operation_init( )
2420{
Jaeden Amero252ef282019-02-15 14:05:35 +00002421 const uint8_t input[1] = { 0 };
2422
Jaeden Amero769ce272019-01-04 11:48:03 +00002423 /* Test each valid way of initializing the object, except for `= {0}`, as
2424 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2425 * though it's OK by the C standard. We could test for this, but we'd need
2426 * to supress the Clang warning for the test. */
2427 psa_mac_operation_t func = psa_mac_operation_init( );
2428 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2429 psa_mac_operation_t zero;
2430
2431 memset( &zero, 0, sizeof( zero ) );
2432
Jaeden Amero252ef282019-02-15 14:05:35 +00002433 /* A freshly-initialized MAC operation should not be usable. */
2434 TEST_EQUAL( psa_mac_update( &func,
2435 input, sizeof( input ) ),
2436 PSA_ERROR_BAD_STATE );
2437 TEST_EQUAL( psa_mac_update( &init,
2438 input, sizeof( input ) ),
2439 PSA_ERROR_BAD_STATE );
2440 TEST_EQUAL( psa_mac_update( &zero,
2441 input, sizeof( input ) ),
2442 PSA_ERROR_BAD_STATE );
2443
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002444 /* A default MAC operation should be abortable without error. */
2445 PSA_ASSERT( psa_mac_abort( &func ) );
2446 PSA_ASSERT( psa_mac_abort( &init ) );
2447 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002448}
2449/* END_CASE */
2450
2451/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002452void mac_setup( int key_type_arg,
2453 data_t *key,
2454 int alg_arg,
2455 int expected_status_arg )
2456{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002457 psa_key_type_t key_type = key_type_arg;
2458 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002459 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002460 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002461 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2462#if defined(KNOWN_SUPPORTED_MAC_ALG)
2463 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2464#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002465
Gilles Peskine8817f612018-12-18 00:18:46 +01002466 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002467
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002468 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2469 &operation, &status ) )
2470 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002471 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002472
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002473 /* The operation object should be reusable. */
2474#if defined(KNOWN_SUPPORTED_MAC_ALG)
2475 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2476 smoke_test_key_data,
2477 sizeof( smoke_test_key_data ),
2478 KNOWN_SUPPORTED_MAC_ALG,
2479 &operation, &status ) )
2480 goto exit;
2481 TEST_EQUAL( status, PSA_SUCCESS );
2482#endif
2483
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002484exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002485 mbedtls_psa_crypto_free( );
2486}
2487/* END_CASE */
2488
2489/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002490void mac_bad_order( )
2491{
2492 psa_key_handle_t handle = 0;
2493 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2494 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2495 const uint8_t key[] = {
2496 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2497 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2498 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
2499 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2500 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2501 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2502 size_t sign_mac_length = 0;
2503 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2504 const uint8_t verify_mac[] = {
2505 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2506 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2507 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2508
2509 PSA_ASSERT( psa_crypto_init( ) );
2510 PSA_ASSERT( psa_allocate_key( &handle ) );
2511 psa_key_policy_set_usage( &policy,
2512 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2513 alg );
2514 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2515
2516 PSA_ASSERT( psa_import_key( handle, key_type,
2517 key, sizeof(key) ) );
2518
2519 /* Call update without calling setup beforehand. */
2520 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2521 PSA_ERROR_BAD_STATE );
2522 PSA_ASSERT( psa_mac_abort( &operation ) );
2523
2524 /* Call sign finish without calling setup beforehand. */
2525 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2526 &sign_mac_length),
2527 PSA_ERROR_BAD_STATE );
2528 PSA_ASSERT( psa_mac_abort( &operation ) );
2529
2530 /* Call verify finish without calling setup beforehand. */
2531 TEST_EQUAL( psa_mac_verify_finish( &operation,
2532 verify_mac, sizeof( verify_mac ) ),
2533 PSA_ERROR_BAD_STATE );
2534 PSA_ASSERT( psa_mac_abort( &operation ) );
2535
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002536 /* Call setup twice in a row. */
2537 PSA_ASSERT( psa_mac_sign_setup( &operation,
2538 handle, alg ) );
2539 TEST_EQUAL( psa_mac_sign_setup( &operation,
2540 handle, alg ),
2541 PSA_ERROR_BAD_STATE );
2542 PSA_ASSERT( psa_mac_abort( &operation ) );
2543
Jaeden Amero252ef282019-02-15 14:05:35 +00002544 /* Call update after sign finish. */
2545 PSA_ASSERT( psa_mac_sign_setup( &operation,
2546 handle, alg ) );
2547 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2548 PSA_ASSERT( psa_mac_sign_finish( &operation,
2549 sign_mac, sizeof( sign_mac ),
2550 &sign_mac_length ) );
2551 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2552 PSA_ERROR_BAD_STATE );
2553 PSA_ASSERT( psa_mac_abort( &operation ) );
2554
2555 /* Call update after verify finish. */
2556 PSA_ASSERT( psa_mac_verify_setup( &operation,
2557 handle, alg ) );
2558 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2559 PSA_ASSERT( psa_mac_verify_finish( &operation,
2560 verify_mac, sizeof( verify_mac ) ) );
2561 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2562 PSA_ERROR_BAD_STATE );
2563 PSA_ASSERT( psa_mac_abort( &operation ) );
2564
2565 /* Call sign finish twice in a row. */
2566 PSA_ASSERT( psa_mac_sign_setup( &operation,
2567 handle, alg ) );
2568 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2569 PSA_ASSERT( psa_mac_sign_finish( &operation,
2570 sign_mac, sizeof( sign_mac ),
2571 &sign_mac_length ) );
2572 TEST_EQUAL( psa_mac_sign_finish( &operation,
2573 sign_mac, sizeof( sign_mac ),
2574 &sign_mac_length ),
2575 PSA_ERROR_BAD_STATE );
2576 PSA_ASSERT( psa_mac_abort( &operation ) );
2577
2578 /* Call verify finish twice in a row. */
2579 PSA_ASSERT( psa_mac_verify_setup( &operation,
2580 handle, alg ) );
2581 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2582 PSA_ASSERT( psa_mac_verify_finish( &operation,
2583 verify_mac, sizeof( verify_mac ) ) );
2584 TEST_EQUAL( psa_mac_verify_finish( &operation,
2585 verify_mac, sizeof( verify_mac ) ),
2586 PSA_ERROR_BAD_STATE );
2587 PSA_ASSERT( psa_mac_abort( &operation ) );
2588
2589 /* Setup sign but try verify. */
2590 PSA_ASSERT( psa_mac_sign_setup( &operation,
2591 handle, alg ) );
2592 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2593 TEST_EQUAL( psa_mac_verify_finish( &operation,
2594 verify_mac, sizeof( verify_mac ) ),
2595 PSA_ERROR_BAD_STATE );
2596 PSA_ASSERT( psa_mac_abort( &operation ) );
2597
2598 /* Setup verify but try sign. */
2599 PSA_ASSERT( psa_mac_verify_setup( &operation,
2600 handle, alg ) );
2601 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2602 TEST_EQUAL( psa_mac_sign_finish( &operation,
2603 sign_mac, sizeof( sign_mac ),
2604 &sign_mac_length ),
2605 PSA_ERROR_BAD_STATE );
2606 PSA_ASSERT( psa_mac_abort( &operation ) );
2607
2608exit:
2609 mbedtls_psa_crypto_free( );
2610}
2611/* END_CASE */
2612
2613/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002614void mac_sign( int key_type_arg,
2615 data_t *key,
2616 int alg_arg,
2617 data_t *input,
2618 data_t *expected_mac )
2619{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002620 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002621 psa_key_type_t key_type = key_type_arg;
2622 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002623 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002624 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002625 /* Leave a little extra room in the output buffer. At the end of the
2626 * test, we'll check that the implementation didn't overwrite onto
2627 * this extra room. */
2628 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2629 size_t mac_buffer_size =
2630 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2631 size_t mac_length = 0;
2632
2633 memset( actual_mac, '+', sizeof( actual_mac ) );
2634 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2635 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2636
Gilles Peskine8817f612018-12-18 00:18:46 +01002637 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002638
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002639 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002640 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002641 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002642
Gilles Peskine8817f612018-12-18 00:18:46 +01002643 PSA_ASSERT( psa_import_key( handle, key_type,
2644 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002645
2646 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002647 PSA_ASSERT( psa_mac_sign_setup( &operation,
2648 handle, alg ) );
2649 PSA_ASSERT( psa_mac_update( &operation,
2650 input->x, input->len ) );
2651 PSA_ASSERT( psa_mac_sign_finish( &operation,
2652 actual_mac, mac_buffer_size,
2653 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002654
2655 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002656 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2657 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002658
2659 /* Verify that the end of the buffer is untouched. */
2660 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2661 sizeof( actual_mac ) - mac_length ) );
2662
2663exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002664 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002665 mbedtls_psa_crypto_free( );
2666}
2667/* END_CASE */
2668
2669/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002670void mac_verify( int key_type_arg,
2671 data_t *key,
2672 int alg_arg,
2673 data_t *input,
2674 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002675{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002676 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002677 psa_key_type_t key_type = key_type_arg;
2678 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002679 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002680 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002681
Gilles Peskine69c12672018-06-28 00:07:19 +02002682 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2683
Gilles Peskine8817f612018-12-18 00:18:46 +01002684 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002685
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002686 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002687 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002688 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002689
Gilles Peskine8817f612018-12-18 00:18:46 +01002690 PSA_ASSERT( psa_import_key( handle, key_type,
2691 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002692
Gilles Peskine8817f612018-12-18 00:18:46 +01002693 PSA_ASSERT( psa_mac_verify_setup( &operation,
2694 handle, alg ) );
2695 PSA_ASSERT( psa_destroy_key( handle ) );
2696 PSA_ASSERT( psa_mac_update( &operation,
2697 input->x, input->len ) );
2698 PSA_ASSERT( psa_mac_verify_finish( &operation,
2699 expected_mac->x,
2700 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002701
2702exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002703 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002704 mbedtls_psa_crypto_free( );
2705}
2706/* END_CASE */
2707
2708/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002709void cipher_operation_init( )
2710{
Jaeden Ameroab439972019-02-15 14:12:05 +00002711 const uint8_t input[1] = { 0 };
2712 unsigned char output[1] = { 0 };
2713 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002714 /* Test each valid way of initializing the object, except for `= {0}`, as
2715 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2716 * though it's OK by the C standard. We could test for this, but we'd need
2717 * to supress the Clang warning for the test. */
2718 psa_cipher_operation_t func = psa_cipher_operation_init( );
2719 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2720 psa_cipher_operation_t zero;
2721
2722 memset( &zero, 0, sizeof( zero ) );
2723
Jaeden Ameroab439972019-02-15 14:12:05 +00002724 /* A freshly-initialized cipher operation should not be usable. */
2725 TEST_EQUAL( psa_cipher_update( &func,
2726 input, sizeof( input ),
2727 output, sizeof( output ),
2728 &output_length ),
2729 PSA_ERROR_BAD_STATE );
2730 TEST_EQUAL( psa_cipher_update( &init,
2731 input, sizeof( input ),
2732 output, sizeof( output ),
2733 &output_length ),
2734 PSA_ERROR_BAD_STATE );
2735 TEST_EQUAL( psa_cipher_update( &zero,
2736 input, sizeof( input ),
2737 output, sizeof( output ),
2738 &output_length ),
2739 PSA_ERROR_BAD_STATE );
2740
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002741 /* A default cipher operation should be abortable without error. */
2742 PSA_ASSERT( psa_cipher_abort( &func ) );
2743 PSA_ASSERT( psa_cipher_abort( &init ) );
2744 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002745}
2746/* END_CASE */
2747
2748/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002749void cipher_setup( int key_type_arg,
2750 data_t *key,
2751 int alg_arg,
2752 int expected_status_arg )
2753{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002754 psa_key_type_t key_type = key_type_arg;
2755 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002756 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002757 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002758 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002759#if defined(KNOWN_SUPPORTED_MAC_ALG)
2760 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2761#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002762
Gilles Peskine8817f612018-12-18 00:18:46 +01002763 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002764
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002765 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2766 &operation, &status ) )
2767 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002768 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002769
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002770 /* The operation object should be reusable. */
2771#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2772 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2773 smoke_test_key_data,
2774 sizeof( smoke_test_key_data ),
2775 KNOWN_SUPPORTED_CIPHER_ALG,
2776 &operation, &status ) )
2777 goto exit;
2778 TEST_EQUAL( status, PSA_SUCCESS );
2779#endif
2780
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002781exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002782 mbedtls_psa_crypto_free( );
2783}
2784/* END_CASE */
2785
2786/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002787void cipher_bad_order( )
2788{
2789 psa_key_handle_t handle = 0;
2790 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2791 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
2792 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2793 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2794 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2795 const uint8_t key[] = {
2796 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2797 0xaa, 0xaa, 0xaa, 0xaa };
2798 const uint8_t text[] = {
2799 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2800 0xbb, 0xbb, 0xbb, 0xbb };
2801 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2802 size_t length = 0;
2803
2804 PSA_ASSERT( psa_crypto_init( ) );
2805 PSA_ASSERT( psa_allocate_key( &handle ) );
2806 psa_key_policy_set_usage( &policy,
2807 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2808 alg );
2809 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2810 PSA_ASSERT( psa_import_key( handle, key_type,
2811 key, sizeof(key) ) );
2812
2813
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002814 /* Call encrypt setup twice in a row. */
2815 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2816 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2817 PSA_ERROR_BAD_STATE );
2818 PSA_ASSERT( psa_cipher_abort( &operation ) );
2819
2820 /* Call decrypt setup twice in a row. */
2821 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2822 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2823 PSA_ERROR_BAD_STATE );
2824 PSA_ASSERT( psa_cipher_abort( &operation ) );
2825
Jaeden Ameroab439972019-02-15 14:12:05 +00002826 /* Generate an IV without calling setup beforehand. */
2827 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2828 buffer, sizeof( buffer ),
2829 &length ),
2830 PSA_ERROR_BAD_STATE );
2831 PSA_ASSERT( psa_cipher_abort( &operation ) );
2832
2833 /* Generate an IV twice in a row. */
2834 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2835 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2836 buffer, sizeof( buffer ),
2837 &length ) );
2838 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2839 buffer, sizeof( buffer ),
2840 &length ),
2841 PSA_ERROR_BAD_STATE );
2842 PSA_ASSERT( psa_cipher_abort( &operation ) );
2843
2844 /* Generate an IV after it's already set. */
2845 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2846 PSA_ASSERT( psa_cipher_set_iv( &operation,
2847 iv, sizeof( iv ) ) );
2848 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2849 buffer, sizeof( buffer ),
2850 &length ),
2851 PSA_ERROR_BAD_STATE );
2852 PSA_ASSERT( psa_cipher_abort( &operation ) );
2853
2854 /* Set an IV without calling setup beforehand. */
2855 TEST_EQUAL( psa_cipher_set_iv( &operation,
2856 iv, sizeof( iv ) ),
2857 PSA_ERROR_BAD_STATE );
2858 PSA_ASSERT( psa_cipher_abort( &operation ) );
2859
2860 /* Set an IV after it's already set. */
2861 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2862 PSA_ASSERT( psa_cipher_set_iv( &operation,
2863 iv, sizeof( iv ) ) );
2864 TEST_EQUAL( psa_cipher_set_iv( &operation,
2865 iv, sizeof( iv ) ),
2866 PSA_ERROR_BAD_STATE );
2867 PSA_ASSERT( psa_cipher_abort( &operation ) );
2868
2869 /* Set an IV after it's already generated. */
2870 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2871 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2872 buffer, sizeof( buffer ),
2873 &length ) );
2874 TEST_EQUAL( psa_cipher_set_iv( &operation,
2875 iv, sizeof( iv ) ),
2876 PSA_ERROR_BAD_STATE );
2877 PSA_ASSERT( psa_cipher_abort( &operation ) );
2878
2879 /* Call update without calling setup beforehand. */
2880 TEST_EQUAL( psa_cipher_update( &operation,
2881 text, sizeof( text ),
2882 buffer, sizeof( buffer ),
2883 &length ),
2884 PSA_ERROR_BAD_STATE );
2885 PSA_ASSERT( psa_cipher_abort( &operation ) );
2886
2887 /* Call update without an IV where an IV is required. */
2888 TEST_EQUAL( psa_cipher_update( &operation,
2889 text, sizeof( text ),
2890 buffer, sizeof( buffer ),
2891 &length ),
2892 PSA_ERROR_BAD_STATE );
2893 PSA_ASSERT( psa_cipher_abort( &operation ) );
2894
2895 /* Call update after finish. */
2896 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2897 PSA_ASSERT( psa_cipher_set_iv( &operation,
2898 iv, sizeof( iv ) ) );
2899 PSA_ASSERT( psa_cipher_finish( &operation,
2900 buffer, sizeof( buffer ), &length ) );
2901 TEST_EQUAL( psa_cipher_update( &operation,
2902 text, sizeof( text ),
2903 buffer, sizeof( buffer ),
2904 &length ),
2905 PSA_ERROR_BAD_STATE );
2906 PSA_ASSERT( psa_cipher_abort( &operation ) );
2907
2908 /* Call finish without calling setup beforehand. */
2909 TEST_EQUAL( psa_cipher_finish( &operation,
2910 buffer, sizeof( buffer ), &length ),
2911 PSA_ERROR_BAD_STATE );
2912 PSA_ASSERT( psa_cipher_abort( &operation ) );
2913
2914 /* Call finish without an IV where an IV is required. */
2915 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2916 /* Not calling update means we are encrypting an empty buffer, which is OK
2917 * for cipher modes with padding. */
2918 TEST_EQUAL( psa_cipher_finish( &operation,
2919 buffer, sizeof( buffer ), &length ),
2920 PSA_ERROR_BAD_STATE );
2921 PSA_ASSERT( psa_cipher_abort( &operation ) );
2922
2923 /* Call finish twice in a row. */
2924 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2925 PSA_ASSERT( psa_cipher_set_iv( &operation,
2926 iv, sizeof( iv ) ) );
2927 PSA_ASSERT( psa_cipher_finish( &operation,
2928 buffer, sizeof( buffer ), &length ) );
2929 TEST_EQUAL( psa_cipher_finish( &operation,
2930 buffer, sizeof( buffer ), &length ),
2931 PSA_ERROR_BAD_STATE );
2932 PSA_ASSERT( psa_cipher_abort( &operation ) );
2933
2934exit:
2935 mbedtls_psa_crypto_free( );
2936}
2937/* END_CASE */
2938
2939/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002940void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002941 data_t *key,
2942 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002943 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002944{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002945 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002946 psa_status_t status;
2947 psa_key_type_t key_type = key_type_arg;
2948 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002949 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002950 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002951 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002952 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002953 size_t output_buffer_size = 0;
2954 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002955 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002956 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002957 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002958
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002959 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2960 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002961
Gilles Peskine8817f612018-12-18 00:18:46 +01002962 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002963
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002964 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002965 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002966 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002967
Gilles Peskine8817f612018-12-18 00:18:46 +01002968 PSA_ASSERT( psa_import_key( handle, key_type,
2969 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002970
Gilles Peskine8817f612018-12-18 00:18:46 +01002971 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2972 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002973
Gilles Peskine8817f612018-12-18 00:18:46 +01002974 PSA_ASSERT( psa_cipher_set_iv( &operation,
2975 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002976 output_buffer_size = ( (size_t) input->len +
2977 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002978 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002979
Gilles Peskine8817f612018-12-18 00:18:46 +01002980 PSA_ASSERT( psa_cipher_update( &operation,
2981 input->x, input->len,
2982 output, output_buffer_size,
2983 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002984 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002985 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002986 output + total_output_length,
2987 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002988 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002989 total_output_length += function_output_length;
2990
Gilles Peskinefe11b722018-12-18 00:24:04 +01002991 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002992 if( expected_status == PSA_SUCCESS )
2993 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002994 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002995 ASSERT_COMPARE( expected_output->x, expected_output->len,
2996 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002997 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002998
Gilles Peskine50e586b2018-06-08 14:28:46 +02002999exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003000 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003001 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003002 mbedtls_psa_crypto_free( );
3003}
3004/* END_CASE */
3005
3006/* BEGIN_CASE */
3007void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003008 data_t *key,
3009 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003010 int first_part_size_arg,
3011 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003012 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003013{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003014 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003015 psa_key_type_t key_type = key_type_arg;
3016 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003017 size_t first_part_size = first_part_size_arg;
3018 size_t output1_length = output1_length_arg;
3019 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003020 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003021 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003022 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003023 size_t output_buffer_size = 0;
3024 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003025 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003026 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003027 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003028
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003029 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3030 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003031
Gilles Peskine8817f612018-12-18 00:18:46 +01003032 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003033
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003034 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003035 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003036 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003037
Gilles Peskine8817f612018-12-18 00:18:46 +01003038 PSA_ASSERT( psa_import_key( handle, key_type,
3039 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003040
Gilles Peskine8817f612018-12-18 00:18:46 +01003041 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3042 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003043
Gilles Peskine8817f612018-12-18 00:18:46 +01003044 PSA_ASSERT( psa_cipher_set_iv( &operation,
3045 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003046 output_buffer_size = ( (size_t) input->len +
3047 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003048 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003049
Gilles Peskinee0866522019-02-19 19:44:00 +01003050 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003051 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3052 output, output_buffer_size,
3053 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003054 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003055 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003056 PSA_ASSERT( psa_cipher_update( &operation,
3057 input->x + first_part_size,
3058 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003059 output + total_output_length,
3060 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003061 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003062 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003063 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003064 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003065 output + total_output_length,
3066 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003067 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003068 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003069 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003070
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003071 ASSERT_COMPARE( expected_output->x, expected_output->len,
3072 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003073
3074exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003075 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_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003083 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,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003087 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003088{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003089 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003090
3091 psa_key_type_t key_type = key_type_arg;
3092 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003093 size_t first_part_size = first_part_size_arg;
3094 size_t output1_length = output1_length_arg;
3095 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003096 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003097 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003098 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003099 size_t output_buffer_size = 0;
3100 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003101 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003102 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003103 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003104
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003105 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3106 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003107
Gilles Peskine8817f612018-12-18 00:18:46 +01003108 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003109
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003110 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003111 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003112 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003113
Gilles Peskine8817f612018-12-18 00:18:46 +01003114 PSA_ASSERT( psa_import_key( handle, key_type,
3115 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003116
Gilles Peskine8817f612018-12-18 00:18:46 +01003117 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3118 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003119
Gilles Peskine8817f612018-12-18 00:18:46 +01003120 PSA_ASSERT( psa_cipher_set_iv( &operation,
3121 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003122
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003123 output_buffer_size = ( (size_t) input->len +
3124 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003125 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003126
Gilles Peskinee0866522019-02-19 19:44:00 +01003127 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003128 PSA_ASSERT( psa_cipher_update( &operation,
3129 input->x, first_part_size,
3130 output, output_buffer_size,
3131 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003132 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003133 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003134 PSA_ASSERT( psa_cipher_update( &operation,
3135 input->x + first_part_size,
3136 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003137 output + total_output_length,
3138 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003139 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003140 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003141 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003142 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003143 output + total_output_length,
3144 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003145 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003146 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003147 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003148
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003149 ASSERT_COMPARE( expected_output->x, expected_output->len,
3150 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003151
3152exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003153 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003154 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003155 mbedtls_psa_crypto_free( );
3156}
3157/* END_CASE */
3158
Gilles Peskine50e586b2018-06-08 14:28:46 +02003159/* BEGIN_CASE */
3160void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003161 data_t *key,
3162 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003163 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003164{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003165 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003166 psa_status_t status;
3167 psa_key_type_t key_type = key_type_arg;
3168 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003169 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003170 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003171 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003172 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003173 size_t output_buffer_size = 0;
3174 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003175 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003176 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003177 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003178
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003179 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3180 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003181
Gilles Peskine8817f612018-12-18 00:18:46 +01003182 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003183
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003184 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003185 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003186 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003187
Gilles Peskine8817f612018-12-18 00:18:46 +01003188 PSA_ASSERT( psa_import_key( handle, key_type,
3189 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003190
Gilles Peskine8817f612018-12-18 00:18:46 +01003191 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3192 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003193
Gilles Peskine8817f612018-12-18 00:18:46 +01003194 PSA_ASSERT( psa_cipher_set_iv( &operation,
3195 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003196
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003197 output_buffer_size = ( (size_t) input->len +
3198 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003199 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003200
Gilles Peskine8817f612018-12-18 00:18:46 +01003201 PSA_ASSERT( psa_cipher_update( &operation,
3202 input->x, input->len,
3203 output, output_buffer_size,
3204 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003205 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003206 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003207 output + total_output_length,
3208 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003209 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003210 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003211 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003212
3213 if( expected_status == PSA_SUCCESS )
3214 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003215 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003216 ASSERT_COMPARE( expected_output->x, expected_output->len,
3217 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003218 }
3219
Gilles Peskine50e586b2018-06-08 14:28:46 +02003220exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003221 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003222 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003223 mbedtls_psa_crypto_free( );
3224}
3225/* END_CASE */
3226
Gilles Peskine50e586b2018-06-08 14:28:46 +02003227/* BEGIN_CASE */
3228void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003229 data_t *key,
3230 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003231{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003232 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003233 psa_key_type_t key_type = key_type_arg;
3234 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003235 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003236 size_t iv_size = 16;
3237 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003238 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003239 size_t output1_size = 0;
3240 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003241 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003242 size_t output2_size = 0;
3243 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003244 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003245 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3246 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003247 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003248
Gilles Peskine8817f612018-12-18 00:18:46 +01003249 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003250
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003251 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003252 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003253 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003254
Gilles Peskine8817f612018-12-18 00:18:46 +01003255 PSA_ASSERT( psa_import_key( handle, key_type,
3256 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003257
Gilles Peskine8817f612018-12-18 00:18:46 +01003258 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3259 handle, alg ) );
3260 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3261 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003262
Gilles Peskine8817f612018-12-18 00:18:46 +01003263 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3264 iv, iv_size,
3265 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003266 output1_size = ( (size_t) input->len +
3267 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003268 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003269
Gilles Peskine8817f612018-12-18 00:18:46 +01003270 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3271 output1, output1_size,
3272 &output1_length ) );
3273 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003274 output1 + output1_length,
3275 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003276 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003277
Gilles Peskine048b7f02018-06-08 14:20:49 +02003278 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003279
Gilles Peskine8817f612018-12-18 00:18:46 +01003280 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003281
3282 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003283 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003284
Gilles Peskine8817f612018-12-18 00:18:46 +01003285 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3286 iv, iv_length ) );
3287 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3288 output2, output2_size,
3289 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003290 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003291 PSA_ASSERT( psa_cipher_finish( &operation2,
3292 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003293 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003294 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003295
Gilles Peskine048b7f02018-06-08 14:20:49 +02003296 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003297
Gilles Peskine8817f612018-12-18 00:18:46 +01003298 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003299
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003300 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003301
3302exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003303 mbedtls_free( output1 );
3304 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003305 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03003306 mbedtls_psa_crypto_free( );
3307}
3308/* END_CASE */
3309
3310/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003311void cipher_verify_output_multipart( int alg_arg,
3312 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003313 data_t *key,
3314 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003315 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003316{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003317 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003318 psa_key_type_t key_type = key_type_arg;
3319 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003320 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003321 unsigned char iv[16] = {0};
3322 size_t iv_size = 16;
3323 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003324 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003325 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003326 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003327 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003328 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003329 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003330 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003331 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3332 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003333 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003334
Gilles Peskine8817f612018-12-18 00:18:46 +01003335 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003336
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003337 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003338 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003339 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003340
Gilles Peskine8817f612018-12-18 00:18:46 +01003341 PSA_ASSERT( psa_import_key( handle, key_type,
3342 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003343
Gilles Peskine8817f612018-12-18 00:18:46 +01003344 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3345 handle, alg ) );
3346 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3347 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003348
Gilles Peskine8817f612018-12-18 00:18:46 +01003349 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3350 iv, iv_size,
3351 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003352 output1_buffer_size = ( (size_t) input->len +
3353 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003354 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003355
Gilles Peskinee0866522019-02-19 19:44:00 +01003356 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003357
Gilles Peskine8817f612018-12-18 00:18:46 +01003358 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3359 output1, output1_buffer_size,
3360 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003361 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003362
Gilles Peskine8817f612018-12-18 00:18:46 +01003363 PSA_ASSERT( psa_cipher_update( &operation1,
3364 input->x + first_part_size,
3365 input->len - first_part_size,
3366 output1, output1_buffer_size,
3367 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003368 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003369
Gilles Peskine8817f612018-12-18 00:18:46 +01003370 PSA_ASSERT( psa_cipher_finish( &operation1,
3371 output1 + output1_length,
3372 output1_buffer_size - output1_length,
3373 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003374 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003375
Gilles Peskine8817f612018-12-18 00:18:46 +01003376 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003377
Gilles Peskine048b7f02018-06-08 14:20:49 +02003378 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003379 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003380
Gilles Peskine8817f612018-12-18 00:18:46 +01003381 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3382 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003383
Gilles Peskine8817f612018-12-18 00:18:46 +01003384 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3385 output2, output2_buffer_size,
3386 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003387 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003388
Gilles Peskine8817f612018-12-18 00:18:46 +01003389 PSA_ASSERT( psa_cipher_update( &operation2,
3390 output1 + first_part_size,
3391 output1_length - first_part_size,
3392 output2, output2_buffer_size,
3393 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003394 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003395
Gilles Peskine8817f612018-12-18 00:18:46 +01003396 PSA_ASSERT( psa_cipher_finish( &operation2,
3397 output2 + output2_length,
3398 output2_buffer_size - output2_length,
3399 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003400 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003401
Gilles Peskine8817f612018-12-18 00:18:46 +01003402 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003403
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003404 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003405
3406exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003407 mbedtls_free( output1 );
3408 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003409 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003410 mbedtls_psa_crypto_free( );
3411}
3412/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003413
Gilles Peskine20035e32018-02-03 22:44:14 +01003414/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003415void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003416 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003417 data_t *nonce,
3418 data_t *additional_data,
3419 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003420 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003421{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003422 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003423 psa_key_type_t key_type = key_type_arg;
3424 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003425 unsigned char *output_data = NULL;
3426 size_t output_size = 0;
3427 size_t output_length = 0;
3428 unsigned char *output_data2 = NULL;
3429 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003430 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003431 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003432 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003433
Gilles Peskine4abf7412018-06-18 16:35:34 +02003434 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003435 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003436
Gilles Peskine8817f612018-12-18 00:18:46 +01003437 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003438
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003439 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003440 psa_key_policy_set_usage( &policy,
3441 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
3442 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003443 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003444
Gilles Peskine8817f612018-12-18 00:18:46 +01003445 PSA_ASSERT( psa_import_key( handle, key_type,
3446 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003447
Gilles Peskinefe11b722018-12-18 00:24:04 +01003448 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3449 nonce->x, nonce->len,
3450 additional_data->x,
3451 additional_data->len,
3452 input_data->x, input_data->len,
3453 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003454 &output_length ),
3455 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003456
3457 if( PSA_SUCCESS == expected_result )
3458 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003459 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003460
Gilles Peskinefe11b722018-12-18 00:24:04 +01003461 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3462 nonce->x, nonce->len,
3463 additional_data->x,
3464 additional_data->len,
3465 output_data, output_length,
3466 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003467 &output_length2 ),
3468 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003469
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003470 ASSERT_COMPARE( input_data->x, input_data->len,
3471 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003472 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003473
Gilles Peskinea1cac842018-06-11 19:33:02 +02003474exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003475 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003476 mbedtls_free( output_data );
3477 mbedtls_free( output_data2 );
3478 mbedtls_psa_crypto_free( );
3479}
3480/* END_CASE */
3481
3482/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003483void aead_encrypt( int key_type_arg, data_t *key_data,
3484 int alg_arg,
3485 data_t *nonce,
3486 data_t *additional_data,
3487 data_t *input_data,
3488 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003489{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003490 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003491 psa_key_type_t key_type = key_type_arg;
3492 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003493 unsigned char *output_data = NULL;
3494 size_t output_size = 0;
3495 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003496 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00003497 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003498
Gilles Peskine4abf7412018-06-18 16:35:34 +02003499 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003500 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003501
Gilles Peskine8817f612018-12-18 00:18:46 +01003502 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003503
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003504 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003505 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003506 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003507
Gilles Peskine8817f612018-12-18 00:18:46 +01003508 PSA_ASSERT( psa_import_key( handle, key_type,
3509 key_data->x,
3510 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003511
Gilles Peskine8817f612018-12-18 00:18:46 +01003512 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3513 nonce->x, nonce->len,
3514 additional_data->x, additional_data->len,
3515 input_data->x, input_data->len,
3516 output_data, output_size,
3517 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003518
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003519 ASSERT_COMPARE( expected_result->x, expected_result->len,
3520 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003521
Gilles Peskinea1cac842018-06-11 19:33:02 +02003522exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003523 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003524 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003525 mbedtls_psa_crypto_free( );
3526}
3527/* END_CASE */
3528
3529/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003530void aead_decrypt( int key_type_arg, data_t *key_data,
3531 int alg_arg,
3532 data_t *nonce,
3533 data_t *additional_data,
3534 data_t *input_data,
3535 data_t *expected_data,
3536 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003537{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003538 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003539 psa_key_type_t key_type = key_type_arg;
3540 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003541 unsigned char *output_data = NULL;
3542 size_t output_size = 0;
3543 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003544 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00003545 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003546 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003547
Gilles Peskine4abf7412018-06-18 16:35:34 +02003548 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003549 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003550
Gilles Peskine8817f612018-12-18 00:18:46 +01003551 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003552
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003553 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003554 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003555 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003556
Gilles Peskine8817f612018-12-18 00:18:46 +01003557 PSA_ASSERT( psa_import_key( handle, key_type,
3558 key_data->x,
3559 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003560
Gilles Peskinefe11b722018-12-18 00:24:04 +01003561 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3562 nonce->x, nonce->len,
3563 additional_data->x,
3564 additional_data->len,
3565 input_data->x, input_data->len,
3566 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003567 &output_length ),
3568 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003569
Gilles Peskine2d277862018-06-18 15:41:12 +02003570 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003571 ASSERT_COMPARE( expected_data->x, expected_data->len,
3572 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003573
Gilles Peskinea1cac842018-06-11 19:33:02 +02003574exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003575 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003576 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003577 mbedtls_psa_crypto_free( );
3578}
3579/* END_CASE */
3580
3581/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003582void signature_size( int type_arg,
3583 int bits,
3584 int alg_arg,
3585 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003586{
3587 psa_key_type_t type = type_arg;
3588 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003589 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003590 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003591exit:
3592 ;
3593}
3594/* END_CASE */
3595
3596/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003597void sign_deterministic( int key_type_arg, data_t *key_data,
3598 int alg_arg, data_t *input_data,
3599 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003600{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003601 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003602 psa_key_type_t key_type = key_type_arg;
3603 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003604 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003605 unsigned char *signature = NULL;
3606 size_t signature_size;
3607 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003608 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003609
Gilles Peskine8817f612018-12-18 00:18:46 +01003610 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003611
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003612 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003613 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003614 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003615
Gilles Peskine8817f612018-12-18 00:18:46 +01003616 PSA_ASSERT( psa_import_key( handle, key_type,
3617 key_data->x,
3618 key_data->len ) );
3619 PSA_ASSERT( psa_get_key_information( handle,
3620 NULL,
3621 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003622
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003623 /* Allocate a buffer which has the size advertized by the
3624 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003625 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3626 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003627 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003628 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003629 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003630
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003631 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003632 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3633 input_data->x, input_data->len,
3634 signature, signature_size,
3635 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003636 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003637 ASSERT_COMPARE( output_data->x, output_data->len,
3638 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003639
3640exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003641 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003642 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01003643 mbedtls_psa_crypto_free( );
3644}
3645/* END_CASE */
3646
3647/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003648void sign_fail( int key_type_arg, data_t *key_data,
3649 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003650 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003651{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003652 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003653 psa_key_type_t key_type = key_type_arg;
3654 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003655 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003656 psa_status_t actual_status;
3657 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003658 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003659 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003660 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003661
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003662 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003663
Gilles Peskine8817f612018-12-18 00:18:46 +01003664 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003665
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003666 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003667 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003668 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003669
Gilles Peskine8817f612018-12-18 00:18:46 +01003670 PSA_ASSERT( psa_import_key( handle, key_type,
3671 key_data->x,
3672 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003673
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003674 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003675 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003676 signature, signature_size,
3677 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003678 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003679 /* The value of *signature_length is unspecified on error, but
3680 * whatever it is, it should be less than signature_size, so that
3681 * if the caller tries to read *signature_length bytes without
3682 * checking the error code then they don't overflow a buffer. */
3683 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003684
3685exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003686 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003687 mbedtls_free( signature );
3688 mbedtls_psa_crypto_free( );
3689}
3690/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003691
3692/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003693void sign_verify( int key_type_arg, data_t *key_data,
3694 int alg_arg, data_t *input_data )
3695{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003696 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003697 psa_key_type_t key_type = key_type_arg;
3698 psa_algorithm_t alg = alg_arg;
3699 size_t key_bits;
3700 unsigned char *signature = NULL;
3701 size_t signature_size;
3702 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003703 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003704
Gilles Peskine8817f612018-12-18 00:18:46 +01003705 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003706
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003707 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003708 psa_key_policy_set_usage( &policy,
3709 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
3710 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003711 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003712
Gilles Peskine8817f612018-12-18 00:18:46 +01003713 PSA_ASSERT( psa_import_key( handle, key_type,
3714 key_data->x,
3715 key_data->len ) );
3716 PSA_ASSERT( psa_get_key_information( handle,
3717 NULL,
3718 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003719
3720 /* Allocate a buffer which has the size advertized by the
3721 * library. */
3722 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3723 key_bits, alg );
3724 TEST_ASSERT( signature_size != 0 );
3725 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003726 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003727
3728 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003729 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3730 input_data->x, input_data->len,
3731 signature, signature_size,
3732 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003733 /* Check that the signature length looks sensible. */
3734 TEST_ASSERT( signature_length <= signature_size );
3735 TEST_ASSERT( signature_length > 0 );
3736
3737 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003738 PSA_ASSERT( psa_asymmetric_verify(
3739 handle, alg,
3740 input_data->x, input_data->len,
3741 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003742
3743 if( input_data->len != 0 )
3744 {
3745 /* Flip a bit in the input and verify that the signature is now
3746 * detected as invalid. Flip a bit at the beginning, not at the end,
3747 * because ECDSA may ignore the last few bits of the input. */
3748 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003749 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3750 input_data->x, input_data->len,
3751 signature, signature_length ),
3752 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003753 }
3754
3755exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003756 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003757 mbedtls_free( signature );
3758 mbedtls_psa_crypto_free( );
3759}
3760/* END_CASE */
3761
3762/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003763void asymmetric_verify( int key_type_arg, data_t *key_data,
3764 int alg_arg, data_t *hash_data,
3765 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003766{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003767 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003768 psa_key_type_t key_type = key_type_arg;
3769 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003770 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003771
Gilles Peskine69c12672018-06-28 00:07:19 +02003772 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3773
Gilles Peskine8817f612018-12-18 00:18:46 +01003774 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003775
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003776 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003777 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003778 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03003779
Gilles Peskine8817f612018-12-18 00:18:46 +01003780 PSA_ASSERT( psa_import_key( handle, key_type,
3781 key_data->x,
3782 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003783
Gilles Peskine8817f612018-12-18 00:18:46 +01003784 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3785 hash_data->x, hash_data->len,
3786 signature_data->x,
3787 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003788exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003789 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003790 mbedtls_psa_crypto_free( );
3791}
3792/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003793
3794/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003795void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3796 int alg_arg, data_t *hash_data,
3797 data_t *signature_data,
3798 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003799{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003800 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003801 psa_key_type_t key_type = key_type_arg;
3802 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003803 psa_status_t actual_status;
3804 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003805 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003806
Gilles Peskine8817f612018-12-18 00:18:46 +01003807 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003808
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003809 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003810 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003811 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003812
Gilles Peskine8817f612018-12-18 00:18:46 +01003813 PSA_ASSERT( psa_import_key( handle, key_type,
3814 key_data->x,
3815 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003816
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003817 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003818 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003819 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003820 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003821
Gilles Peskinefe11b722018-12-18 00:24:04 +01003822 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003823
3824exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003825 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003826 mbedtls_psa_crypto_free( );
3827}
3828/* END_CASE */
3829
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003830/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003831void asymmetric_encrypt( int key_type_arg,
3832 data_t *key_data,
3833 int alg_arg,
3834 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003835 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003836 int expected_output_length_arg,
3837 int expected_status_arg )
3838{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003839 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003840 psa_key_type_t key_type = key_type_arg;
3841 psa_algorithm_t alg = alg_arg;
3842 size_t expected_output_length = expected_output_length_arg;
3843 size_t key_bits;
3844 unsigned char *output = NULL;
3845 size_t output_size;
3846 size_t output_length = ~0;
3847 psa_status_t actual_status;
3848 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003849 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003850
Gilles Peskine8817f612018-12-18 00:18:46 +01003851 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003852
Gilles Peskine656896e2018-06-29 19:12:28 +02003853 /* Import the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003854 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003855 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003856 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3857 PSA_ASSERT( psa_import_key( handle, key_type,
3858 key_data->x,
3859 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003860
3861 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003862 PSA_ASSERT( psa_get_key_information( handle,
3863 NULL,
3864 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003865 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003866 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003867
3868 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003869 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003870 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003871 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003872 output, output_size,
3873 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003874 TEST_EQUAL( actual_status, expected_status );
3875 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003876
Gilles Peskine68428122018-06-30 18:42:41 +02003877 /* If the label is empty, the test framework puts a non-null pointer
3878 * in label->x. Test that a null pointer works as well. */
3879 if( label->len == 0 )
3880 {
3881 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003882 if( output_size != 0 )
3883 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003884 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003885 input_data->x, input_data->len,
3886 NULL, label->len,
3887 output, output_size,
3888 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003889 TEST_EQUAL( actual_status, expected_status );
3890 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003891 }
3892
Gilles Peskine656896e2018-06-29 19:12:28 +02003893exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003894 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003895 mbedtls_free( output );
3896 mbedtls_psa_crypto_free( );
3897}
3898/* END_CASE */
3899
3900/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003901void asymmetric_encrypt_decrypt( int key_type_arg,
3902 data_t *key_data,
3903 int alg_arg,
3904 data_t *input_data,
3905 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003906{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003907 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003908 psa_key_type_t key_type = key_type_arg;
3909 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003910 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003911 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003912 size_t output_size;
3913 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003914 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003915 size_t output2_size;
3916 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003917 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003918
Gilles Peskine8817f612018-12-18 00:18:46 +01003919 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003920
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003921 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003922 psa_key_policy_set_usage( &policy,
3923 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003924 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003925 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003926
Gilles Peskine8817f612018-12-18 00:18:46 +01003927 PSA_ASSERT( psa_import_key( handle, key_type,
3928 key_data->x,
3929 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003930
3931 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003932 PSA_ASSERT( psa_get_key_information( handle,
3933 NULL,
3934 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003935 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003936 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003937 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003938 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003939
Gilles Peskineeebd7382018-06-08 18:11:54 +02003940 /* We test encryption by checking that encrypt-then-decrypt gives back
3941 * the original plaintext because of the non-optional random
3942 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003943 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3944 input_data->x, input_data->len,
3945 label->x, label->len,
3946 output, output_size,
3947 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003948 /* We don't know what ciphertext length to expect, but check that
3949 * it looks sensible. */
3950 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003951
Gilles Peskine8817f612018-12-18 00:18:46 +01003952 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3953 output, output_length,
3954 label->x, label->len,
3955 output2, output2_size,
3956 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003957 ASSERT_COMPARE( input_data->x, input_data->len,
3958 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003959
3960exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003961 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003962 mbedtls_free( output );
3963 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003964 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003965}
3966/* END_CASE */
3967
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003968/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003969void asymmetric_decrypt( int key_type_arg,
3970 data_t *key_data,
3971 int alg_arg,
3972 data_t *input_data,
3973 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003974 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003975{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003976 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003977 psa_key_type_t key_type = key_type_arg;
3978 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003979 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003980 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003981 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003982 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003983
Jaeden Amero412654a2019-02-06 12:57:46 +00003984 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003985 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003986
Gilles Peskine8817f612018-12-18 00:18:46 +01003987 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003988
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003989 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003990 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003991 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003992
Gilles Peskine8817f612018-12-18 00:18:46 +01003993 PSA_ASSERT( psa_import_key( handle, key_type,
3994 key_data->x,
3995 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003996
Gilles Peskine8817f612018-12-18 00:18:46 +01003997 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3998 input_data->x, input_data->len,
3999 label->x, label->len,
4000 output,
4001 output_size,
4002 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004003 ASSERT_COMPARE( expected_data->x, expected_data->len,
4004 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004005
Gilles Peskine68428122018-06-30 18:42:41 +02004006 /* If the label is empty, the test framework puts a non-null pointer
4007 * in label->x. Test that a null pointer works as well. */
4008 if( label->len == 0 )
4009 {
4010 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004011 if( output_size != 0 )
4012 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004013 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4014 input_data->x, input_data->len,
4015 NULL, label->len,
4016 output,
4017 output_size,
4018 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004019 ASSERT_COMPARE( expected_data->x, expected_data->len,
4020 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004021 }
4022
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004023exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004024 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004025 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004026 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004027}
4028/* END_CASE */
4029
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004030/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004031void asymmetric_decrypt_fail( int key_type_arg,
4032 data_t *key_data,
4033 int alg_arg,
4034 data_t *input_data,
4035 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004036 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004037 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004038{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004039 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004040 psa_key_type_t key_type = key_type_arg;
4041 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004042 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004043 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004044 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004045 psa_status_t actual_status;
4046 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004047 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004048
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004049 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004050
Gilles Peskine8817f612018-12-18 00:18:46 +01004051 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004052
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004053 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02004054 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004055 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004056
Gilles Peskine8817f612018-12-18 00:18:46 +01004057 PSA_ASSERT( psa_import_key( handle, key_type,
4058 key_data->x,
4059 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004060
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004061 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004062 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004063 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004064 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004065 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004066 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004067 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004068
Gilles Peskine68428122018-06-30 18:42:41 +02004069 /* If the label is empty, the test framework puts a non-null pointer
4070 * in label->x. Test that a null pointer works as well. */
4071 if( label->len == 0 )
4072 {
4073 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004074 if( output_size != 0 )
4075 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004076 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004077 input_data->x, input_data->len,
4078 NULL, label->len,
4079 output, output_size,
4080 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004081 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004082 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004083 }
4084
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004085exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004086 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004087 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004088 mbedtls_psa_crypto_free( );
4089}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004090/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004091
4092/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00004093void crypto_generator_init( )
4094{
4095 /* Test each valid way of initializing the object, except for `= {0}`, as
4096 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4097 * though it's OK by the C standard. We could test for this, but we'd need
4098 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004099 size_t capacity;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004100 psa_crypto_generator_t func = psa_crypto_generator_init( );
4101 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
4102 psa_crypto_generator_t zero;
4103
4104 memset( &zero, 0, sizeof( zero ) );
4105
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004106 /* A default generator should not be able to report its capacity. */
4107 TEST_EQUAL( psa_get_generator_capacity( &func, &capacity ),
4108 PSA_ERROR_BAD_STATE );
4109 TEST_EQUAL( psa_get_generator_capacity( &init, &capacity ),
4110 PSA_ERROR_BAD_STATE );
4111 TEST_EQUAL( psa_get_generator_capacity( &zero, &capacity ),
4112 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004113
4114 /* A default generator should be abortable without error. */
4115 PSA_ASSERT( psa_generator_abort(&func) );
4116 PSA_ASSERT( psa_generator_abort(&init) );
4117 PSA_ASSERT( psa_generator_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004118}
4119/* END_CASE */
4120
4121/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004122void derive_setup( int key_type_arg,
4123 data_t *key_data,
4124 int alg_arg,
4125 data_t *salt,
4126 data_t *label,
4127 int requested_capacity_arg,
4128 int expected_status_arg )
4129{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004130 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004131 size_t key_type = key_type_arg;
4132 psa_algorithm_t alg = alg_arg;
4133 size_t requested_capacity = requested_capacity_arg;
4134 psa_status_t expected_status = expected_status_arg;
4135 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004136 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004137
Gilles Peskine8817f612018-12-18 00:18:46 +01004138 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004139
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004140 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004141 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004142 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004143
Gilles Peskine8817f612018-12-18 00:18:46 +01004144 PSA_ASSERT( psa_import_key( handle, key_type,
4145 key_data->x,
4146 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004147
Gilles Peskinefe11b722018-12-18 00:24:04 +01004148 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4149 salt->x, salt->len,
4150 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004151 requested_capacity ),
4152 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004153
4154exit:
4155 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004156 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004157 mbedtls_psa_crypto_free( );
4158}
4159/* END_CASE */
4160
4161/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004162void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004163{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004164 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004165 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004166 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004167 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004168 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004169 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004170 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4171 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4172 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00004173 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004174
Gilles Peskine8817f612018-12-18 00:18:46 +01004175 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004176
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004177 PSA_ASSERT( psa_allocate_key( &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004178 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004179 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004180
Gilles Peskine8817f612018-12-18 00:18:46 +01004181 PSA_ASSERT( psa_import_key( handle, key_type,
4182 key_data,
4183 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004184
4185 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01004186 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4187 NULL, 0,
4188 NULL, 0,
4189 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004190
4191 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01004192 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4193 NULL, 0,
4194 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004195 capacity ),
4196 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004197
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004198 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004199
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004200 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004201 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004202
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004203exit:
4204 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004205 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004206 mbedtls_psa_crypto_free( );
4207}
4208/* END_CASE */
4209
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004210/* BEGIN_CASE */
4211void test_derive_invalid_generator_tests( )
4212{
4213 uint8_t output_buffer[16];
4214 size_t buffer_size = 16;
4215 size_t capacity = 0;
4216 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4217
Nir Sonnenschein50789302018-10-31 12:16:38 +02004218 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004219 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004220
4221 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004222 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004223
Gilles Peskine8817f612018-12-18 00:18:46 +01004224 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004225
Nir Sonnenschein50789302018-10-31 12:16:38 +02004226 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004227 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004228
Nir Sonnenschein50789302018-10-31 12:16:38 +02004229 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004230 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004231
4232exit:
4233 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004234}
4235/* END_CASE */
4236
4237/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004238void derive_output( int alg_arg,
4239 data_t *key_data,
4240 data_t *salt,
4241 data_t *label,
4242 int requested_capacity_arg,
4243 data_t *expected_output1,
4244 data_t *expected_output2 )
4245{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004246 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004247 psa_algorithm_t alg = alg_arg;
4248 size_t requested_capacity = requested_capacity_arg;
4249 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4250 uint8_t *expected_outputs[2] =
4251 {expected_output1->x, expected_output2->x};
4252 size_t output_sizes[2] =
4253 {expected_output1->len, expected_output2->len};
4254 size_t output_buffer_size = 0;
4255 uint8_t *output_buffer = NULL;
4256 size_t expected_capacity;
4257 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00004258 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004259 psa_status_t status;
4260 unsigned i;
4261
4262 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4263 {
4264 if( output_sizes[i] > output_buffer_size )
4265 output_buffer_size = output_sizes[i];
4266 if( output_sizes[i] == 0 )
4267 expected_outputs[i] = NULL;
4268 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004269 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004270 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004271
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004272 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004273 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004274 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004275
Gilles Peskine8817f612018-12-18 00:18:46 +01004276 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
4277 key_data->x,
4278 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004279
4280 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004281 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4282 salt->x, salt->len,
4283 label->x, label->len,
4284 requested_capacity ) );
4285 PSA_ASSERT( psa_get_generator_capacity( &generator,
4286 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004287 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004288 expected_capacity = requested_capacity;
4289
4290 /* Expansion phase. */
4291 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4292 {
4293 /* Read some bytes. */
4294 status = psa_generator_read( &generator,
4295 output_buffer, output_sizes[i] );
4296 if( expected_capacity == 0 && output_sizes[i] == 0 )
4297 {
4298 /* Reading 0 bytes when 0 bytes are available can go either way. */
4299 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004300 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004301 continue;
4302 }
4303 else if( expected_capacity == 0 ||
4304 output_sizes[i] > expected_capacity )
4305 {
4306 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004307 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004308 expected_capacity = 0;
4309 continue;
4310 }
4311 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004312 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004313 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004314 ASSERT_COMPARE( output_buffer, output_sizes[i],
4315 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004316 /* Check the generator status. */
4317 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01004318 PSA_ASSERT( psa_get_generator_capacity( &generator,
4319 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004320 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004321 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004322 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004323
4324exit:
4325 mbedtls_free( output_buffer );
4326 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004327 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004328 mbedtls_psa_crypto_free( );
4329}
4330/* END_CASE */
4331
4332/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004333void derive_full( int alg_arg,
4334 data_t *key_data,
4335 data_t *salt,
4336 data_t *label,
4337 int requested_capacity_arg )
4338{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004339 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004340 psa_algorithm_t alg = alg_arg;
4341 size_t requested_capacity = requested_capacity_arg;
4342 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4343 unsigned char output_buffer[16];
4344 size_t expected_capacity = requested_capacity;
4345 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00004346 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004347
Gilles Peskine8817f612018-12-18 00:18:46 +01004348 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004349
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004350 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004351 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004352 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004353
Gilles Peskine8817f612018-12-18 00:18:46 +01004354 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
4355 key_data->x,
4356 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004357
4358 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004359 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4360 salt->x, salt->len,
4361 label->x, label->len,
4362 requested_capacity ) );
4363 PSA_ASSERT( psa_get_generator_capacity( &generator,
4364 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004365 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004366
4367 /* Expansion phase. */
4368 while( current_capacity > 0 )
4369 {
4370 size_t read_size = sizeof( output_buffer );
4371 if( read_size > current_capacity )
4372 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01004373 PSA_ASSERT( psa_generator_read( &generator,
4374 output_buffer,
4375 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004376 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01004377 PSA_ASSERT( psa_get_generator_capacity( &generator,
4378 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004379 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004380 }
4381
4382 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004383 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004384 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004385
Gilles Peskine8817f612018-12-18 00:18:46 +01004386 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004387
4388exit:
4389 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004390 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02004391 mbedtls_psa_crypto_free( );
4392}
4393/* END_CASE */
4394
4395/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004396void derive_key_exercise( int alg_arg,
4397 data_t *key_data,
4398 data_t *salt,
4399 data_t *label,
4400 int derived_type_arg,
4401 int derived_bits_arg,
4402 int derived_usage_arg,
4403 int derived_alg_arg )
4404{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004405 psa_key_handle_t base_handle = 0;
4406 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004407 psa_algorithm_t alg = alg_arg;
4408 psa_key_type_t derived_type = derived_type_arg;
4409 size_t derived_bits = derived_bits_arg;
4410 psa_key_usage_t derived_usage = derived_usage_arg;
4411 psa_algorithm_t derived_alg = derived_alg_arg;
4412 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
4413 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004414 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004415 psa_key_type_t got_type;
4416 size_t got_bits;
4417
Gilles Peskine8817f612018-12-18 00:18:46 +01004418 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004419
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004420 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004421 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004422 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
4423 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
4424 key_data->x,
4425 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004426
4427 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004428 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4429 salt->x, salt->len,
4430 label->x, label->len,
4431 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004432 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004433 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004434 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4435 PSA_ASSERT( psa_generator_import_key( derived_handle,
4436 derived_type,
4437 derived_bits,
4438 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004439
4440 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01004441 PSA_ASSERT( psa_get_key_information( derived_handle,
4442 &got_type,
4443 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004444 TEST_EQUAL( got_type, derived_type );
4445 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004446
4447 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004448 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004449 goto exit;
4450
4451exit:
4452 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004453 psa_destroy_key( base_handle );
4454 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004455 mbedtls_psa_crypto_free( );
4456}
4457/* END_CASE */
4458
4459/* BEGIN_CASE */
4460void derive_key_export( int alg_arg,
4461 data_t *key_data,
4462 data_t *salt,
4463 data_t *label,
4464 int bytes1_arg,
4465 int bytes2_arg )
4466{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004467 psa_key_handle_t base_handle = 0;
4468 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004469 psa_algorithm_t alg = alg_arg;
4470 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004471 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004472 size_t bytes2 = bytes2_arg;
4473 size_t capacity = bytes1 + bytes2;
4474 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004475 uint8_t *output_buffer = NULL;
4476 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00004477 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004478 size_t length;
4479
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004480 ASSERT_ALLOC( output_buffer, capacity );
4481 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004482 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004483
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004484 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004485 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004486 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
4487 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
4488 key_data->x,
4489 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004490
4491 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004492 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4493 salt->x, salt->len,
4494 label->x, label->len,
4495 capacity ) );
4496 PSA_ASSERT( psa_generator_read( &generator,
4497 output_buffer,
4498 capacity ) );
4499 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004500
4501 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004502 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4503 salt->x, salt->len,
4504 label->x, label->len,
4505 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004506 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004507 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004508 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4509 PSA_ASSERT( psa_generator_import_key( derived_handle,
4510 PSA_KEY_TYPE_RAW_DATA,
4511 derived_bits,
4512 &generator ) );
4513 PSA_ASSERT( psa_export_key( derived_handle,
4514 export_buffer, bytes1,
4515 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004516 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004517 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004518 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004519 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4520 PSA_ASSERT( psa_generator_import_key( derived_handle,
4521 PSA_KEY_TYPE_RAW_DATA,
4522 PSA_BYTES_TO_BITS( bytes2 ),
4523 &generator ) );
4524 PSA_ASSERT( psa_export_key( derived_handle,
4525 export_buffer + bytes1, bytes2,
4526 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004527 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004528
4529 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004530 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4531 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004532
4533exit:
4534 mbedtls_free( output_buffer );
4535 mbedtls_free( export_buffer );
4536 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004537 psa_destroy_key( base_handle );
4538 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004539 mbedtls_psa_crypto_free( );
4540}
4541/* END_CASE */
4542
4543/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004544void key_agreement_setup( int alg_arg,
4545 int our_key_type_arg, data_t *our_key_data,
4546 data_t *peer_key_data,
4547 int expected_status_arg )
4548{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004549 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004550 psa_algorithm_t alg = alg_arg;
4551 psa_key_type_t our_key_type = our_key_type_arg;
4552 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004553 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004554
Gilles Peskine8817f612018-12-18 00:18:46 +01004555 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004556
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004557 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004558 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004559 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4560 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4561 our_key_data->x,
4562 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004563
Gilles Peskinefe11b722018-12-18 00:24:04 +01004564 TEST_EQUAL( psa_key_agreement( &generator,
4565 our_key,
4566 peer_key_data->x, peer_key_data->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004567 alg ),
4568 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004569
4570exit:
4571 psa_generator_abort( &generator );
4572 psa_destroy_key( our_key );
4573 mbedtls_psa_crypto_free( );
4574}
4575/* END_CASE */
4576
4577/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004578void key_agreement_capacity( int alg_arg,
4579 int our_key_type_arg, data_t *our_key_data,
4580 data_t *peer_key_data,
4581 int expected_capacity_arg )
4582{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004583 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004584 psa_algorithm_t alg = alg_arg;
4585 psa_key_type_t our_key_type = our_key_type_arg;
4586 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004587 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004588 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004589 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004590
Gilles Peskine8817f612018-12-18 00:18:46 +01004591 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004592
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004593 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004594 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004595 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4596 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4597 our_key_data->x,
4598 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004599
Gilles Peskine8817f612018-12-18 00:18:46 +01004600 PSA_ASSERT( psa_key_agreement( &generator,
4601 our_key,
4602 peer_key_data->x, peer_key_data->len,
4603 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004604
Gilles Peskinebf491972018-10-25 22:36:12 +02004605 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004606 PSA_ASSERT( psa_get_generator_capacity(
4607 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004608 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004609
Gilles Peskinebf491972018-10-25 22:36:12 +02004610 /* Test the actual capacity by reading the output. */
4611 while( actual_capacity > sizeof( output ) )
4612 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004613 PSA_ASSERT( psa_generator_read( &generator,
4614 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004615 actual_capacity -= sizeof( output );
4616 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004617 PSA_ASSERT( psa_generator_read( &generator,
4618 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004619 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004620 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004621
Gilles Peskine59685592018-09-18 12:11:34 +02004622exit:
4623 psa_generator_abort( &generator );
4624 psa_destroy_key( our_key );
4625 mbedtls_psa_crypto_free( );
4626}
4627/* END_CASE */
4628
4629/* BEGIN_CASE */
4630void key_agreement_output( int alg_arg,
4631 int our_key_type_arg, data_t *our_key_data,
4632 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004633 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004634{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004635 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004636 psa_algorithm_t alg = alg_arg;
4637 psa_key_type_t our_key_type = our_key_type_arg;
4638 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004639 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004640 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004641
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004642 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4643 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004644
Gilles Peskine8817f612018-12-18 00:18:46 +01004645 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004646
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004647 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004648 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004649 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4650 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4651 our_key_data->x,
4652 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004653
Gilles Peskine8817f612018-12-18 00:18:46 +01004654 PSA_ASSERT( psa_key_agreement( &generator,
4655 our_key,
4656 peer_key_data->x, peer_key_data->len,
4657 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004658
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004659 PSA_ASSERT( psa_generator_read( &generator,
4660 actual_output,
4661 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004662 ASSERT_COMPARE( actual_output, expected_output1->len,
4663 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004664 if( expected_output2->len != 0 )
4665 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004666 PSA_ASSERT( psa_generator_read( &generator,
4667 actual_output,
4668 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004669 ASSERT_COMPARE( actual_output, expected_output2->len,
4670 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004671 }
Gilles Peskine59685592018-09-18 12:11:34 +02004672
4673exit:
4674 psa_generator_abort( &generator );
4675 psa_destroy_key( our_key );
4676 mbedtls_psa_crypto_free( );
4677 mbedtls_free( actual_output );
4678}
4679/* END_CASE */
4680
4681/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004682void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004683{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004684 size_t bytes = bytes_arg;
4685 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004686 unsigned char *output = NULL;
4687 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004688 size_t i;
4689 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004690
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004691 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4692 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004693 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004694
Gilles Peskine8817f612018-12-18 00:18:46 +01004695 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004696
Gilles Peskinea50d7392018-06-21 10:22:13 +02004697 /* Run several times, to ensure that every output byte will be
4698 * nonzero at least once with overwhelming probability
4699 * (2^(-8*number_of_runs)). */
4700 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004701 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004702 if( bytes != 0 )
4703 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004704 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004705
4706 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004707 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4708 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004709
4710 for( i = 0; i < bytes; i++ )
4711 {
4712 if( output[i] != 0 )
4713 ++changed[i];
4714 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004715 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004716
4717 /* Check that every byte was changed to nonzero at least once. This
4718 * validates that psa_generate_random is overwriting every byte of
4719 * the output buffer. */
4720 for( i = 0; i < bytes; i++ )
4721 {
4722 TEST_ASSERT( changed[i] != 0 );
4723 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004724
4725exit:
4726 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004727 mbedtls_free( output );
4728 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004729}
4730/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004731
4732/* BEGIN_CASE */
4733void generate_key( int type_arg,
4734 int bits_arg,
4735 int usage_arg,
4736 int alg_arg,
4737 int expected_status_arg )
4738{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004739 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004740 psa_key_type_t type = type_arg;
4741 psa_key_usage_t usage = usage_arg;
4742 size_t bits = bits_arg;
4743 psa_algorithm_t alg = alg_arg;
4744 psa_status_t expected_status = expected_status_arg;
4745 psa_key_type_t got_type;
4746 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004747 psa_status_t expected_info_status =
David Saadab4ecc272019-02-14 13:48:10 +02004748 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Jaeden Amero70261c52019-01-04 11:47:20 +00004749 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004750
Gilles Peskine8817f612018-12-18 00:18:46 +01004751 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004752
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004753 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004754 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004755 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004756
4757 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004758 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
4759 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004760
4761 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004762 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
4763 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004764 if( expected_info_status != PSA_SUCCESS )
4765 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004766 TEST_EQUAL( got_type, type );
4767 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004768
Gilles Peskine818ca122018-06-20 18:16:48 +02004769 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004770 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004771 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004772
4773exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004774 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004775 mbedtls_psa_crypto_free( );
4776}
4777/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004778
Darryl Greend49a4992018-06-18 17:27:26 +01004779/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4780void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4781 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004782 int alg_arg, int generation_method,
4783 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004784{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004785 psa_key_handle_t handle = 0;
4786 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004787 psa_key_type_t type = (psa_key_type_t) type_arg;
4788 psa_key_type_t type_get;
4789 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00004790 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
4791 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004792 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4793 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004794 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00004795 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4796 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004797 unsigned char *first_export = NULL;
4798 unsigned char *second_export = NULL;
4799 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4800 size_t first_exported_length;
4801 size_t second_exported_length;
4802
4803 ASSERT_ALLOC( first_export, export_size );
4804 ASSERT_ALLOC( second_export, export_size );
4805
Gilles Peskine8817f612018-12-18 00:18:46 +01004806 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004807
Gilles Peskine8817f612018-12-18 00:18:46 +01004808 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
Gilles Peskine8817f612018-12-18 00:18:46 +01004809 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004810 psa_key_policy_set_usage( &policy_set, policy_usage,
4811 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004812 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004813
Darryl Green0c6575a2018-11-07 16:05:30 +00004814 switch( generation_method )
4815 {
4816 case IMPORT_KEY:
4817 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004818 PSA_ASSERT( psa_import_key( handle, type,
4819 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004820 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004821
Darryl Green0c6575a2018-11-07 16:05:30 +00004822 case GENERATE_KEY:
4823 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004824 PSA_ASSERT( psa_generate_key( handle, type, bits,
4825 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004826 break;
4827
4828 case DERIVE_KEY:
4829 /* Create base key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004830 PSA_ASSERT( psa_allocate_key( &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004831 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4832 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004833 PSA_ASSERT( psa_set_key_policy(
4834 base_key, &base_policy_set ) );
4835 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4836 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004837 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004838 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4839 base_policy_alg,
4840 NULL, 0, NULL, 0,
4841 export_size ) );
4842 PSA_ASSERT( psa_generator_import_key(
4843 handle, PSA_KEY_TYPE_RAW_DATA,
4844 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004845 break;
4846 }
Darryl Greend49a4992018-06-18 17:27:26 +01004847
4848 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004849 TEST_EQUAL( psa_export_key( handle,
4850 first_export, export_size,
4851 &first_exported_length ),
4852 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004853
4854 /* Shutdown and restart */
4855 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004856 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004857
Darryl Greend49a4992018-06-18 17:27:26 +01004858 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004859 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4860 &handle ) );
4861 PSA_ASSERT( psa_get_key_information(
4862 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004863 TEST_EQUAL( type_get, type );
4864 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004865
Gilles Peskine8817f612018-12-18 00:18:46 +01004866 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004867 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4868 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004869
4870 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004871 TEST_EQUAL( psa_export_key( handle,
4872 second_export, export_size,
4873 &second_exported_length ),
4874 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004875
Darryl Green0c6575a2018-11-07 16:05:30 +00004876 if( export_status == PSA_SUCCESS )
4877 {
4878 ASSERT_COMPARE( first_export, first_exported_length,
4879 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004880
Darryl Green0c6575a2018-11-07 16:05:30 +00004881 switch( generation_method )
4882 {
4883 case IMPORT_KEY:
4884 ASSERT_COMPARE( data->x, data->len,
4885 first_export, first_exported_length );
4886 break;
4887 default:
4888 break;
4889 }
4890 }
4891
4892 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004893 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004894 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004895
4896exit:
4897 mbedtls_free( first_export );
4898 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004899 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004900 mbedtls_psa_crypto_free();
4901}
4902/* END_CASE */