blob: b5bf7de9cdd85e9e21a6069d84d1d039cab6e618 [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 Peskineca5bed72019-05-13 11:29:51 +02001058void import_twice( int usage_arg, int alg_arg,
Gilles Peskinea4261682018-12-03 11:34:01 +01001059 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,
Gilles Peskineca5bed72019-05-13 11:29:51 +02001136 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001137 int expected_bits,
1138 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001139 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001140 int canonical_input )
1141{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001142 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001143 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001144 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001145 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001146 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001147 unsigned char *exported = NULL;
1148 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001149 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001150 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001151 size_t reexported_length;
1152 psa_key_type_t got_type;
1153 size_t got_bits;
Jaeden Amero70261c52019-01-04 11:47:20 +00001154 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001155
Moran Pekercb088e72018-07-17 17:36:59 +03001156 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001157 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001158 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001159 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001160 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001161
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001162 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001163 psa_key_policy_set_usage( &policy, usage_arg, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001164 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001165
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001166 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
David Saadab4ecc272019-02-14 13:48:10 +02001167 PSA_ERROR_DOES_NOT_EXIST );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001168
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001169 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001170 PSA_ASSERT( psa_import_key( handle, type,
1171 data->x, data->len ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001172
1173 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001174 PSA_ASSERT( psa_get_key_information( handle,
1175 &got_type,
1176 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001177 TEST_EQUAL( got_type, type );
1178 TEST_EQUAL( got_bits, (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001179
1180 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001181 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001182 exported, export_size,
1183 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001184 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001185
1186 /* The exported length must be set by psa_export_key() to a value between 0
1187 * and export_size. On errors, the exported length must be 0. */
1188 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1189 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1190 TEST_ASSERT( exported_length <= export_size );
1191
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001192 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001193 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001194 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001195 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001196 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001197 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001198 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001199
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001200 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001201 goto exit;
1202
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001203 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001204 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001205 else
1206 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001207 psa_key_handle_t handle2;
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001208 PSA_ASSERT( psa_allocate_key( &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001209 PSA_ASSERT( psa_set_key_policy( handle2, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001210
Gilles Peskine8817f612018-12-18 00:18:46 +01001211 PSA_ASSERT( psa_import_key( handle2, type,
1212 exported,
1213 exported_length ) );
1214 PSA_ASSERT( psa_export_key( handle2,
1215 reexported,
1216 export_size,
1217 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001218 ASSERT_COMPARE( exported, exported_length,
1219 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001220 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001221 }
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001222 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, got_bits ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001223
1224destroy:
1225 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001226 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001227 TEST_EQUAL( psa_get_key_information( handle, NULL, NULL ),
1228 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001229
1230exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001231 mbedtls_free( exported );
1232 mbedtls_free( reexported );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001233 mbedtls_psa_crypto_free( );
1234}
1235/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001236
Moran Pekerf709f4a2018-06-06 17:26:04 +03001237/* BEGIN_CASE */
Moran Peker28a38e62018-11-07 16:18:24 +02001238void import_key_nonempty_slot( )
1239{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001240 psa_key_handle_t handle = 0;
Moran Peker28a38e62018-11-07 16:18:24 +02001241 psa_key_type_t type = PSA_KEY_TYPE_RAW_DATA;
1242 psa_status_t status;
1243 const uint8_t data[] = { 0x1, 0x2, 0x3, 0x4, 0x5 };
Gilles Peskine8817f612018-12-18 00:18:46 +01001244 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001245
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001246 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001247
Moran Peker28a38e62018-11-07 16:18:24 +02001248 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001249 PSA_ASSERT( psa_import_key( handle, type,
1250 data, sizeof( data ) ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001251
1252 /* Import the key again */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001253 status = psa_import_key( handle, type, data, sizeof( data ) );
David Saadab4ecc272019-02-14 13:48:10 +02001254 TEST_EQUAL( status, PSA_ERROR_ALREADY_EXISTS );
Moran Peker28a38e62018-11-07 16:18:24 +02001255
1256exit:
1257 mbedtls_psa_crypto_free( );
1258}
1259/* END_CASE */
1260
1261/* BEGIN_CASE */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001262void export_invalid_handle( int handle, int expected_export_status_arg )
Moran Peker28a38e62018-11-07 16:18:24 +02001263{
1264 psa_status_t status;
1265 unsigned char *exported = NULL;
1266 size_t export_size = 0;
1267 size_t exported_length = INVALID_EXPORT_LENGTH;
1268 psa_status_t expected_export_status = expected_export_status_arg;
1269
Gilles Peskine8817f612018-12-18 00:18:46 +01001270 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker28a38e62018-11-07 16:18:24 +02001271
1272 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001273 status = psa_export_key( (psa_key_handle_t) handle,
Moran Peker28a38e62018-11-07 16:18:24 +02001274 exported, export_size,
1275 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001276 TEST_EQUAL( status, expected_export_status );
Moran Peker28a38e62018-11-07 16:18:24 +02001277
1278exit:
1279 mbedtls_psa_crypto_free( );
1280}
1281/* END_CASE */
1282
1283/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001284void export_with_no_key_activity( )
1285{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001286 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001287 psa_algorithm_t alg = PSA_ALG_CTR;
1288 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001289 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001290 unsigned char *exported = NULL;
1291 size_t export_size = 0;
1292 size_t exported_length = INVALID_EXPORT_LENGTH;
1293
Gilles Peskine8817f612018-12-18 00:18:46 +01001294 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001295
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001296 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001297 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001298 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001299
1300 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001301 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001302 exported, export_size,
1303 &exported_length );
David Saadab4ecc272019-02-14 13:48:10 +02001304 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Peker34550092018-11-07 16:19:34 +02001305
1306exit:
1307 mbedtls_psa_crypto_free( );
1308}
1309/* END_CASE */
1310
1311/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001312void cipher_with_no_key_activity( )
1313{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001314 psa_key_handle_t handle = 0;
Moran Pekerce500072018-11-07 16:20:07 +02001315 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001316 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001317 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001318 int exercise_alg = PSA_ALG_CTR;
1319
Gilles Peskine8817f612018-12-18 00:18:46 +01001320 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001321
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001322 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekerce500072018-11-07 16:20:07 +02001323 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, exercise_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001324 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerce500072018-11-07 16:20:07 +02001325
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001326 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
David Saadab4ecc272019-02-14 13:48:10 +02001327 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Pekerce500072018-11-07 16:20:07 +02001328
1329exit:
1330 psa_cipher_abort( &operation );
1331 mbedtls_psa_crypto_free( );
1332}
1333/* END_CASE */
1334
1335/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001336void export_after_import_failure( data_t *data, int type_arg,
1337 int expected_import_status_arg )
1338{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001339 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001340 psa_key_type_t type = type_arg;
1341 psa_status_t status;
1342 unsigned char *exported = NULL;
1343 size_t export_size = 0;
1344 psa_status_t expected_import_status = expected_import_status_arg;
1345 size_t exported_length = INVALID_EXPORT_LENGTH;
1346
Gilles Peskine8817f612018-12-18 00:18:46 +01001347 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001348
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001349 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001350
Moran Peker34550092018-11-07 16:19:34 +02001351 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001352 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001353 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001354 TEST_EQUAL( status, expected_import_status );
Moran Peker34550092018-11-07 16:19:34 +02001355
1356 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001357 status = psa_export_key( handle,
Moran Peker34550092018-11-07 16:19:34 +02001358 exported, export_size,
1359 &exported_length );
David Saadab4ecc272019-02-14 13:48:10 +02001360 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Peker34550092018-11-07 16:19:34 +02001361
1362exit:
1363 mbedtls_psa_crypto_free( );
1364}
1365/* END_CASE */
1366
1367/* BEGIN_CASE */
Moran Pekerce500072018-11-07 16:20:07 +02001368void cipher_after_import_failure( data_t *data, int type_arg,
1369 int expected_import_status_arg )
1370{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001371 psa_key_handle_t handle = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001372 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Moran Pekerce500072018-11-07 16:20:07 +02001373 psa_key_type_t type = type_arg;
1374 psa_status_t status;
1375 psa_status_t expected_import_status = expected_import_status_arg;
1376 int exercise_alg = PSA_ALG_CTR;
1377
Gilles Peskine8817f612018-12-18 00:18:46 +01001378 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerce500072018-11-07 16:20:07 +02001379
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001380 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001381
Moran Pekerce500072018-11-07 16:20:07 +02001382 /* Import the key - expect failure */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001383 status = psa_import_key( handle, type,
Gilles Peskine0f915f12018-12-17 23:35:42 +01001384 data->x, data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001385 TEST_EQUAL( status, expected_import_status );
Moran Pekerce500072018-11-07 16:20:07 +02001386
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001387 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
David Saadab4ecc272019-02-14 13:48:10 +02001388 TEST_EQUAL( status, PSA_ERROR_DOES_NOT_EXIST );
Moran Pekerce500072018-11-07 16:20:07 +02001389
1390exit:
1391 psa_cipher_abort( &operation );
1392 mbedtls_psa_crypto_free( );
1393}
1394/* END_CASE */
1395
1396/* BEGIN_CASE */
Moran Peker34550092018-11-07 16:19:34 +02001397void export_after_destroy_key( data_t *data, int type_arg )
1398{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001399 psa_key_handle_t handle = 0;
Moran Peker34550092018-11-07 16:19:34 +02001400 psa_key_type_t type = type_arg;
1401 psa_status_t status;
Jaeden Amero70261c52019-01-04 11:47:20 +00001402 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Peker34550092018-11-07 16:19:34 +02001403 psa_algorithm_t alg = PSA_ALG_CTR;
1404 unsigned char *exported = NULL;
1405 size_t export_size = 0;
1406 size_t exported_length = INVALID_EXPORT_LENGTH;
1407
Gilles Peskine8817f612018-12-18 00:18:46 +01001408 PSA_ASSERT( psa_crypto_init( ) );
Moran Peker34550092018-11-07 16:19:34 +02001409
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001410 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001411 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001412 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Peker34550092018-11-07 16:19:34 +02001413 export_size = (ptrdiff_t) data->len;
1414 ASSERT_ALLOC( exported, export_size );
1415
1416 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001417 PSA_ASSERT( psa_import_key( handle, type,
1418 data->x, data->len ) );
Moran Peker34550092018-11-07 16:19:34 +02001419
Gilles Peskine8817f612018-12-18 00:18:46 +01001420 PSA_ASSERT( psa_export_key( handle, exported, export_size,
1421 &exported_length ) );
Moran Peker34550092018-11-07 16:19:34 +02001422
1423 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001424 PSA_ASSERT( psa_destroy_key( handle ) );
Moran Peker34550092018-11-07 16:19:34 +02001425
1426 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001427 status = psa_export_key( handle, exported, export_size,
Moran Peker34550092018-11-07 16:19:34 +02001428 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001429 TEST_EQUAL( status, PSA_ERROR_INVALID_HANDLE );
Moran Peker34550092018-11-07 16:19:34 +02001430
1431exit:
1432 mbedtls_free( exported );
1433 mbedtls_psa_crypto_free( );
1434}
1435/* END_CASE */
1436
1437/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001438void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001439 int type_arg,
1440 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001441 int export_size_delta,
1442 int expected_export_status_arg,
1443 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001444{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001445 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001446 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001447 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001448 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001449 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001450 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001451 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001452 size_t exported_length = INVALID_EXPORT_LENGTH;
Jaeden Amero70261c52019-01-04 11:47:20 +00001453 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001454
Gilles Peskine8817f612018-12-18 00:18:46 +01001455 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001456
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001457 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02001458 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001459 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001460
1461 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001462 PSA_ASSERT( psa_import_key( handle, type,
1463 data->x, data->len ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001464
Gilles Peskine49c25912018-10-29 15:15:31 +01001465 /* Export the public key */
1466 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001467 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001468 exported, export_size,
1469 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001470 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001471 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001472 {
1473 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1474 size_t bits;
Gilles Peskine8817f612018-12-18 00:18:46 +01001475 PSA_ASSERT( psa_get_key_information( handle, NULL, &bits ) );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001476 TEST_ASSERT( expected_public_key->len <=
1477 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001478 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1479 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001480 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001481
1482exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001483 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001484 psa_destroy_key( handle );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001485 mbedtls_psa_crypto_free( );
1486}
1487/* END_CASE */
1488
Gilles Peskine20035e32018-02-03 22:44:14 +01001489/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001490void import_and_exercise_key( data_t *data,
1491 int type_arg,
1492 int bits_arg,
1493 int alg_arg )
1494{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001495 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001496 psa_key_type_t type = type_arg;
1497 size_t bits = bits_arg;
1498 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001499 psa_key_usage_t usage = usage_to_exercise( type, alg );
Jaeden Amero70261c52019-01-04 11:47:20 +00001500 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001501 psa_key_type_t got_type;
1502 size_t got_bits;
1503 psa_status_t status;
1504
Gilles Peskine8817f612018-12-18 00:18:46 +01001505 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001506
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001507 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001508 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001509 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001510
1511 /* Import the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001512 status = psa_import_key( handle, type, data->x, data->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01001513 PSA_ASSERT( status );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001514
1515 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01001516 PSA_ASSERT( psa_get_key_information( handle,
1517 &got_type,
1518 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001519 TEST_EQUAL( got_type, type );
1520 TEST_EQUAL( got_bits, bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001521
1522 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001523 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001524 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001525
1526exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001527 psa_destroy_key( handle );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001528 mbedtls_psa_crypto_free( );
1529}
1530/* END_CASE */
1531
1532/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001533void key_policy( int usage_arg, int alg_arg )
1534{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001535 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001536 psa_algorithm_t alg = alg_arg;
1537 psa_key_usage_t usage = usage_arg;
1538 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1539 unsigned char key[32] = {0};
Jaeden Amero70261c52019-01-04 11:47:20 +00001540 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
1541 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001542
1543 memset( key, 0x2a, sizeof( key ) );
1544
Gilles Peskine8817f612018-12-18 00:18:46 +01001545 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001546
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001547 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001548 psa_key_policy_set_usage( &policy_set, usage, alg );
1549
Gilles Peskinefe11b722018-12-18 00:24:04 +01001550 TEST_EQUAL( psa_key_policy_get_usage( &policy_set ), usage );
1551 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_set ), alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001552 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001553
Gilles Peskine8817f612018-12-18 00:18:46 +01001554 PSA_ASSERT( psa_import_key( handle, key_type,
1555 key, sizeof( key ) ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001556
Gilles Peskine8817f612018-12-18 00:18:46 +01001557 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001558
Gilles Peskinefe11b722018-12-18 00:24:04 +01001559 TEST_EQUAL( policy_get.usage, policy_set.usage );
1560 TEST_EQUAL( policy_get.alg, policy_set.alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001561
1562exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001563 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001564 mbedtls_psa_crypto_free( );
1565}
1566/* END_CASE */
1567
1568/* BEGIN_CASE */
Jaeden Amero70261c52019-01-04 11:47:20 +00001569void key_policy_init( )
1570{
1571 /* Test each valid way of initializing the object, except for `= {0}`, as
1572 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1573 * though it's OK by the C standard. We could test for this, but we'd need
1574 * to supress the Clang warning for the test. */
1575 psa_key_policy_t func = psa_key_policy_init( );
1576 psa_key_policy_t init = PSA_KEY_POLICY_INIT;
1577 psa_key_policy_t zero;
1578
1579 memset( &zero, 0, sizeof( zero ) );
1580
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001581 /* A default key policy should not permit any usage. */
1582 TEST_EQUAL( psa_key_policy_get_usage( &func ), 0 );
1583 TEST_EQUAL( psa_key_policy_get_usage( &init ), 0 );
1584 TEST_EQUAL( psa_key_policy_get_usage( &zero ), 0 );
1585
1586 /* A default key policy should not permit any algorithm. */
1587 TEST_EQUAL( psa_key_policy_get_algorithm( &func ), 0 );
1588 TEST_EQUAL( psa_key_policy_get_algorithm( &init ), 0 );
1589 TEST_EQUAL( psa_key_policy_get_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001590}
1591/* END_CASE */
1592
1593/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001594void mac_key_policy( int policy_usage,
1595 int policy_alg,
1596 int key_type,
1597 data_t *key_data,
1598 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001599{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001600 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001601 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001602 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001603 psa_status_t status;
1604 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001605
Gilles Peskine8817f612018-12-18 00:18:46 +01001606 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001607
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001608 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001609 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001610 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001611
Gilles Peskine8817f612018-12-18 00:18:46 +01001612 PSA_ASSERT( psa_import_key( handle, key_type,
1613 key_data->x, key_data->len ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001614
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001615 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001616 if( policy_alg == exercise_alg &&
1617 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001618 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001619 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001620 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001621 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001622
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001623 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001624 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001625 if( policy_alg == exercise_alg &&
1626 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001627 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001628 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001629 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001630
1631exit:
1632 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001633 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001634 mbedtls_psa_crypto_free( );
1635}
1636/* END_CASE */
1637
1638/* BEGIN_CASE */
1639void cipher_key_policy( int policy_usage,
1640 int policy_alg,
1641 int key_type,
1642 data_t *key_data,
1643 int exercise_alg )
1644{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001645 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001646 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001647 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001648 psa_status_t status;
1649
Gilles Peskine8817f612018-12-18 00:18:46 +01001650 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001651
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001652 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001653 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001654 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001655
Gilles Peskine8817f612018-12-18 00:18:46 +01001656 PSA_ASSERT( psa_import_key( handle, key_type,
1657 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001658
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001659 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001660 if( policy_alg == exercise_alg &&
1661 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001662 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001663 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001664 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001665 psa_cipher_abort( &operation );
1666
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001667 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001668 if( policy_alg == exercise_alg &&
1669 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001670 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001671 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001672 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001673
1674exit:
1675 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001676 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001677 mbedtls_psa_crypto_free( );
1678}
1679/* END_CASE */
1680
1681/* BEGIN_CASE */
1682void aead_key_policy( int policy_usage,
1683 int policy_alg,
1684 int key_type,
1685 data_t *key_data,
1686 int nonce_length_arg,
1687 int tag_length_arg,
1688 int exercise_alg )
1689{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001690 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001691 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001692 psa_status_t status;
1693 unsigned char nonce[16] = {0};
1694 size_t nonce_length = nonce_length_arg;
1695 unsigned char tag[16];
1696 size_t tag_length = tag_length_arg;
1697 size_t output_length;
1698
1699 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1700 TEST_ASSERT( tag_length <= sizeof( tag ) );
1701
Gilles Peskine8817f612018-12-18 00:18:46 +01001702 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001703
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001704 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001705 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001706 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001707
Gilles Peskine8817f612018-12-18 00:18:46 +01001708 PSA_ASSERT( psa_import_key( handle, key_type,
1709 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001710
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001711 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001712 nonce, nonce_length,
1713 NULL, 0,
1714 NULL, 0,
1715 tag, tag_length,
1716 &output_length );
1717 if( policy_alg == exercise_alg &&
1718 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001719 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001720 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001721 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001722
1723 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001724 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001725 nonce, nonce_length,
1726 NULL, 0,
1727 tag, tag_length,
1728 NULL, 0,
1729 &output_length );
1730 if( policy_alg == exercise_alg &&
1731 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001732 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001733 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001734 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001735
1736exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001737 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001738 mbedtls_psa_crypto_free( );
1739}
1740/* END_CASE */
1741
1742/* BEGIN_CASE */
1743void asymmetric_encryption_key_policy( int policy_usage,
1744 int policy_alg,
1745 int key_type,
1746 data_t *key_data,
1747 int exercise_alg )
1748{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001749 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001750 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001751 psa_status_t status;
1752 size_t key_bits;
1753 size_t buffer_length;
1754 unsigned char *buffer = NULL;
1755 size_t output_length;
1756
Gilles Peskine8817f612018-12-18 00:18:46 +01001757 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001758
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001759 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001760 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001761 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001762
Gilles Peskine8817f612018-12-18 00:18:46 +01001763 PSA_ASSERT( psa_import_key( handle, key_type,
1764 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001765
Gilles Peskine8817f612018-12-18 00:18:46 +01001766 PSA_ASSERT( psa_get_key_information( handle,
1767 NULL,
1768 &key_bits ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001769 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1770 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001771 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001772
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001773 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001774 NULL, 0,
1775 NULL, 0,
1776 buffer, buffer_length,
1777 &output_length );
1778 if( policy_alg == exercise_alg &&
1779 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001780 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001781 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001782 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001783
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001784 if( buffer_length != 0 )
1785 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001786 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001787 buffer, buffer_length,
1788 NULL, 0,
1789 buffer, buffer_length,
1790 &output_length );
1791 if( policy_alg == exercise_alg &&
1792 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001793 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001794 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001795 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001796
1797exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001798 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001799 mbedtls_psa_crypto_free( );
1800 mbedtls_free( buffer );
1801}
1802/* END_CASE */
1803
1804/* BEGIN_CASE */
1805void asymmetric_signature_key_policy( int policy_usage,
1806 int policy_alg,
1807 int key_type,
1808 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001809 int exercise_alg,
1810 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001811{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001812 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001813 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001814 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001815 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1816 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1817 * compatible with the policy and `payload_length_arg` is supposed to be
1818 * a valid input length to sign. If `payload_length_arg <= 0`,
1819 * `exercise_alg` is supposed to be forbidden by the policy. */
1820 int compatible_alg = payload_length_arg > 0;
1821 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001822 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1823 size_t signature_length;
1824
Gilles Peskine8817f612018-12-18 00:18:46 +01001825 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001826
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001827 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001828 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001829 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001830
Gilles Peskine8817f612018-12-18 00:18:46 +01001831 PSA_ASSERT( psa_import_key( handle, key_type,
1832 key_data->x, key_data->len ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001833
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001834 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001835 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001836 signature, sizeof( signature ),
1837 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001838 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001839 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001840 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001841 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001842
1843 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001844 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001845 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001846 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001847 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001848 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001849 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001850 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001851
1852exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001853 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001854 mbedtls_psa_crypto_free( );
1855}
1856/* END_CASE */
1857
1858/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001859void derive_key_policy( int policy_usage,
1860 int policy_alg,
1861 int key_type,
1862 data_t *key_data,
1863 int exercise_alg )
1864{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001865 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001866 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001867 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1868 psa_status_t status;
1869
Gilles Peskine8817f612018-12-18 00:18:46 +01001870 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001871
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001872 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001873 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001874 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001875
Gilles Peskine8817f612018-12-18 00:18:46 +01001876 PSA_ASSERT( psa_import_key( handle, key_type,
1877 key_data->x, key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001878
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001879 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001880 exercise_alg,
1881 NULL, 0,
1882 NULL, 0,
1883 1 );
1884 if( policy_alg == exercise_alg &&
1885 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001886 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001887 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001888 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001889
1890exit:
1891 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001892 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001893 mbedtls_psa_crypto_free( );
1894}
1895/* END_CASE */
1896
1897/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001898void agreement_key_policy( int policy_usage,
1899 int policy_alg,
1900 int key_type_arg,
1901 data_t *key_data,
1902 int exercise_alg )
1903{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001904 psa_key_handle_t handle = 0;
Jaeden Amero70261c52019-01-04 11:47:20 +00001905 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001906 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001907 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1908 psa_status_t status;
1909
Gilles Peskine8817f612018-12-18 00:18:46 +01001910 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001911
Gilles Peskined40c1fb2019-01-19 12:20:52 +01001912 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001913 psa_key_policy_set_usage( &policy, policy_usage, policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01001914 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001915
Gilles Peskine8817f612018-12-18 00:18:46 +01001916 PSA_ASSERT( psa_import_key( handle, key_type,
1917 key_data->x, key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001918
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001919 status = key_agreement_with_self( &generator, handle, exercise_alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001920
Gilles Peskine01d718c2018-09-18 12:01:02 +02001921 if( policy_alg == exercise_alg &&
1922 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001923 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001924 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001925 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001926
1927exit:
1928 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001929 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001930 mbedtls_psa_crypto_free( );
1931}
1932/* END_CASE */
1933
1934/* BEGIN_CASE */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001935void key_policy_alg2( int key_type_arg, data_t *key_data,
1936 int usage_arg, int alg_arg, int alg2_arg )
1937{
1938 psa_key_handle_t handle = 0;
1939 psa_key_type_t key_type = key_type_arg;
1940 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
1941 psa_key_policy_t got_policy = PSA_KEY_POLICY_INIT;
1942 psa_key_usage_t usage = usage_arg;
1943 psa_algorithm_t alg = alg_arg;
1944 psa_algorithm_t alg2 = alg2_arg;
1945
1946 PSA_ASSERT( psa_crypto_init( ) );
1947
1948 PSA_ASSERT( psa_allocate_key( &handle ) );
1949 psa_key_policy_set_usage( &policy, usage, alg );
1950 psa_key_policy_set_enrollment_algorithm( &policy, alg2 );
1951 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
1952 PSA_ASSERT( psa_import_key( handle, key_type,
1953 key_data->x, key_data->len ) );
1954
1955 PSA_ASSERT( psa_get_key_policy( handle, &got_policy ) );
1956 TEST_EQUAL( psa_key_policy_get_usage( &got_policy ), usage );
1957 TEST_EQUAL( psa_key_policy_get_algorithm( &got_policy ), alg );
1958 TEST_EQUAL( psa_key_policy_get_enrollment_algorithm( &got_policy ), alg2 );
1959
1960 if( ! exercise_key( handle, usage, alg ) )
1961 goto exit;
1962 if( ! exercise_key( handle, usage, alg2 ) )
1963 goto exit;
1964
1965exit:
1966 psa_destroy_key( handle );
1967 mbedtls_psa_crypto_free( );
1968}
1969/* END_CASE */
1970
1971/* BEGIN_CASE */
Gilles Peskine705cbfd2019-05-20 17:28:11 +02001972void copy_key_policy( int source_usage_arg,
1973 int source_alg_arg, int source_alg2_arg,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001974 int type_arg, data_t *material,
Gilles Peskine705cbfd2019-05-20 17:28:11 +02001975 int target_usage_arg,
1976 int target_alg_arg, int target_alg2_arg,
1977 int constraint_usage_arg,
1978 int constraint_alg_arg, int constraint_alg2_arg,
1979 int expected_usage_arg,
1980 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001981{
1982 psa_key_usage_t source_usage = source_usage_arg;
1983 psa_algorithm_t source_alg = source_alg_arg;
Gilles Peskine705cbfd2019-05-20 17:28:11 +02001984 psa_algorithm_t source_alg2 = source_alg2_arg;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001985 psa_key_handle_t source_handle = 0;
1986 psa_key_policy_t source_policy = PSA_KEY_POLICY_INIT;
1987 psa_key_type_t source_type = type_arg;
1988 size_t source_bits;
1989 psa_key_usage_t target_usage = target_usage_arg;
1990 psa_algorithm_t target_alg = target_alg_arg;
Gilles Peskine705cbfd2019-05-20 17:28:11 +02001991 psa_algorithm_t target_alg2 = target_alg2_arg;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001992 psa_key_handle_t target_handle = 0;
1993 psa_key_policy_t target_policy = PSA_KEY_POLICY_INIT;
1994 psa_key_type_t target_type;
1995 size_t target_bits;
1996 psa_key_usage_t constraint_usage = constraint_usage_arg;
1997 psa_algorithm_t constraint_alg = constraint_alg_arg;
Gilles Peskine705cbfd2019-05-20 17:28:11 +02001998 psa_algorithm_t constraint_alg2 = constraint_alg2_arg;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001999 psa_key_policy_t constraint = PSA_KEY_POLICY_INIT;
2000 psa_key_policy_t *p_constraint = NULL;
2001 psa_key_usage_t expected_usage = expected_usage_arg;
2002 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002003 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002004 uint8_t *export_buffer = NULL;
2005
2006 if( constraint_usage_arg != -1 )
2007 {
2008 p_constraint = &constraint;
2009 psa_key_policy_set_usage( p_constraint,
2010 constraint_usage, constraint_alg );
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002011 psa_key_policy_set_enrollment_algorithm( p_constraint,
2012 constraint_alg2 );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002013 }
2014
2015 PSA_ASSERT( psa_crypto_init( ) );
2016
2017 /* Populate the source slot. */
2018 PSA_ASSERT( psa_allocate_key( &source_handle ) );
2019 psa_key_policy_set_usage( &source_policy, source_usage, source_alg );
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002020 psa_key_policy_set_enrollment_algorithm( &source_policy, source_alg2 );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002021 PSA_ASSERT( psa_set_key_policy( source_handle, &source_policy ) );
2022 PSA_ASSERT( psa_import_key( source_handle, source_type,
2023 material->x, material->len ) );
2024 PSA_ASSERT( psa_get_key_information( source_handle, NULL, &source_bits ) );
2025
2026 /* Prepare the target slot. */
2027 PSA_ASSERT( psa_allocate_key( &target_handle ) );
2028 psa_key_policy_set_usage( &target_policy, target_usage, target_alg );
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002029 psa_key_policy_set_enrollment_algorithm( &target_policy, target_alg2 );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002030 PSA_ASSERT( psa_set_key_policy( target_handle, &target_policy ) );
2031 target_policy = psa_key_policy_init();
2032
2033 /* Copy the key. */
2034 PSA_ASSERT( psa_copy_key( source_handle, target_handle, p_constraint ) );
2035
2036 /* Destroy the source to ensure that this doesn't affect the target. */
2037 PSA_ASSERT( psa_destroy_key( source_handle ) );
2038
2039 /* Test that the target slot has the expected content and policy. */
2040 PSA_ASSERT( psa_get_key_information( target_handle,
2041 &target_type, &target_bits ) );
2042 TEST_EQUAL( source_type, target_type );
2043 TEST_EQUAL( source_bits, target_bits );
2044 PSA_ASSERT( psa_get_key_policy( target_handle, &target_policy ) );
2045 TEST_EQUAL( expected_usage, psa_key_policy_get_usage( &target_policy ) );
2046 TEST_EQUAL( expected_alg, psa_key_policy_get_algorithm( &target_policy ) );
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002047 TEST_EQUAL( expected_alg2,
2048 psa_key_policy_get_enrollment_algorithm( &target_policy ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002049 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2050 {
2051 size_t length;
2052 ASSERT_ALLOC( export_buffer, material->len );
2053 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2054 material->len, &length ) );
2055 ASSERT_COMPARE( material->x, material->len,
2056 export_buffer, length );
2057 }
2058 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2059 goto exit;
2060
2061 PSA_ASSERT( psa_close_key( target_handle ) );
2062
2063exit:
2064 mbedtls_psa_crypto_free( );
2065 mbedtls_free( export_buffer );
2066}
2067/* END_CASE */
2068
2069/* BEGIN_CASE */
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002070void copy_fail( int source_usage_arg, int source_alg_arg, int source_alg2_arg,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002071 int type_arg, data_t *material,
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002072 int target_usage_arg, int target_alg_arg, int target_alg2_arg,
2073 int constraint_usage_arg,
2074 int constraint_alg_arg, int constraint_alg2_arg,
Gilles Peskine57ab7212019-01-28 13:03:09 +01002075 int expected_status_arg )
2076{
2077 /* Test copy failure into an empty slot. There is a test for copy failure
2078 * into an occupied slot in
2079 * test_suite_psa_crypto_slot_management.function. */
2080
2081 psa_key_usage_t source_usage = source_usage_arg;
2082 psa_algorithm_t source_alg = source_alg_arg;
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002083 psa_algorithm_t source_alg2 = source_alg2_arg;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002084 psa_key_handle_t source_handle = 0;
2085 psa_key_policy_t source_policy = PSA_KEY_POLICY_INIT;
2086 psa_key_type_t source_type = type_arg;
2087 size_t source_bits;
2088 psa_key_usage_t target_usage = target_usage_arg;
2089 psa_algorithm_t target_alg = target_alg_arg;
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002090 psa_algorithm_t target_alg2 = target_alg2_arg;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002091 psa_key_handle_t target_handle = 0;
2092 psa_key_policy_t target_policy = PSA_KEY_POLICY_INIT;
2093 psa_key_type_t target_type;
2094 size_t target_bits;
2095 psa_key_usage_t constraint_usage = constraint_usage_arg;
2096 psa_algorithm_t constraint_alg = constraint_alg_arg;
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002097 psa_algorithm_t constraint_alg2 = constraint_alg2_arg;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002098 psa_key_policy_t constraint = PSA_KEY_POLICY_INIT;
2099 psa_key_policy_t *p_constraint = NULL;
2100 psa_status_t expected_status = expected_status_arg;
2101
2102 if( constraint_usage_arg != -1 )
2103 {
2104 p_constraint = &constraint;
2105 psa_key_policy_set_usage( p_constraint,
2106 constraint_usage, constraint_alg );
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002107 psa_key_policy_set_enrollment_algorithm( p_constraint,
2108 constraint_alg2 );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002109 }
2110
2111 PSA_ASSERT( psa_crypto_init( ) );
2112
2113 /* Populate the source slot. */
2114 PSA_ASSERT( psa_allocate_key( &source_handle ) );
2115 psa_key_policy_set_usage( &source_policy, source_usage, source_alg );
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002116 psa_key_policy_set_enrollment_algorithm( &source_policy, source_alg2 );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002117 PSA_ASSERT( psa_set_key_policy( source_handle, &source_policy ) );
2118 PSA_ASSERT( psa_import_key( source_handle, source_type,
2119 material->x, material->len ) );
2120 PSA_ASSERT( psa_get_key_information( source_handle, NULL, &source_bits ) );
2121
2122 /* Prepare the target slot. */
2123 PSA_ASSERT( psa_allocate_key( &target_handle ) );
2124 psa_key_policy_set_usage( &target_policy, target_usage, target_alg );
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002125 psa_key_policy_set_enrollment_algorithm( &target_policy, target_alg2 );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002126 PSA_ASSERT( psa_set_key_policy( target_handle, &target_policy ) );
2127 target_policy = psa_key_policy_init();
2128
2129 /* Copy the key. */
2130 TEST_EQUAL( psa_copy_key( source_handle, target_handle, p_constraint ),
2131 expected_status );
2132
2133 /* Test that the target slot is unaffected. */
2134 TEST_EQUAL( psa_get_key_information( target_handle,
2135 &target_type, &target_bits ),
David Saadab4ecc272019-02-14 13:48:10 +02002136 PSA_ERROR_DOES_NOT_EXIST );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002137 PSA_ASSERT( psa_get_key_policy( target_handle, &target_policy ) );
2138 TEST_EQUAL( target_usage, psa_key_policy_get_usage( &target_policy ) );
2139 TEST_EQUAL( target_alg, psa_key_policy_get_algorithm( &target_policy ) );
Gilles Peskine705cbfd2019-05-20 17:28:11 +02002140 TEST_EQUAL( target_alg2,
2141 psa_key_policy_get_enrollment_algorithm( &target_policy ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002142
2143exit:
2144 mbedtls_psa_crypto_free( );
2145}
2146/* END_CASE */
2147
2148/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002149void hash_operation_init( )
2150{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002151 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002152 /* Test each valid way of initializing the object, except for `= {0}`, as
2153 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2154 * though it's OK by the C standard. We could test for this, but we'd need
2155 * to supress the Clang warning for the test. */
2156 psa_hash_operation_t func = psa_hash_operation_init( );
2157 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2158 psa_hash_operation_t zero;
2159
2160 memset( &zero, 0, sizeof( zero ) );
2161
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002162 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002163 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2164 PSA_ERROR_BAD_STATE );
2165 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2166 PSA_ERROR_BAD_STATE );
2167 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2168 PSA_ERROR_BAD_STATE );
2169
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002170 /* A default hash operation should be abortable without error. */
2171 PSA_ASSERT( psa_hash_abort( &func ) );
2172 PSA_ASSERT( psa_hash_abort( &init ) );
2173 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002174}
2175/* END_CASE */
2176
2177/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002178void hash_setup( int alg_arg,
2179 int expected_status_arg )
2180{
2181 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002182 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002183 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002184 psa_status_t status;
2185
Gilles Peskine8817f612018-12-18 00:18:46 +01002186 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002187
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002188 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002189 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002190
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002191 /* Whether setup succeeded or failed, abort must succeed. */
2192 PSA_ASSERT( psa_hash_abort( &operation ) );
2193
2194 /* If setup failed, reproduce the failure, so as to
2195 * test the resulting state of the operation object. */
2196 if( status != PSA_SUCCESS )
2197 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2198
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002199 /* Now the operation object should be reusable. */
2200#if defined(KNOWN_SUPPORTED_HASH_ALG)
2201 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2202 PSA_ASSERT( psa_hash_abort( &operation ) );
2203#endif
2204
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002205exit:
2206 mbedtls_psa_crypto_free( );
2207}
2208/* END_CASE */
2209
2210/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002211void hash_bad_order( )
2212{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002213 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002214 unsigned char input[] = "";
2215 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002216 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002217 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2218 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2219 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002220 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002221 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002222 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002223
Gilles Peskine8817f612018-12-18 00:18:46 +01002224 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002225
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002226 /* Call setup twice in a row. */
2227 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2228 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2229 PSA_ERROR_BAD_STATE );
2230 PSA_ASSERT( psa_hash_abort( &operation ) );
2231
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002232 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002233 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002234 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002235 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002236
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002237 /* Call update after finish. */
2238 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2239 PSA_ASSERT( psa_hash_finish( &operation,
2240 hash, sizeof( hash ), &hash_len ) );
2241 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002242 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002243 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002244
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002245 /* Call verify without calling setup beforehand. */
2246 TEST_EQUAL( psa_hash_verify( &operation,
2247 valid_hash, sizeof( valid_hash ) ),
2248 PSA_ERROR_BAD_STATE );
2249 PSA_ASSERT( psa_hash_abort( &operation ) );
2250
2251 /* Call verify after finish. */
2252 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2253 PSA_ASSERT( psa_hash_finish( &operation,
2254 hash, sizeof( hash ), &hash_len ) );
2255 TEST_EQUAL( psa_hash_verify( &operation,
2256 valid_hash, sizeof( valid_hash ) ),
2257 PSA_ERROR_BAD_STATE );
2258 PSA_ASSERT( psa_hash_abort( &operation ) );
2259
2260 /* Call verify twice in a row. */
2261 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2262 PSA_ASSERT( psa_hash_verify( &operation,
2263 valid_hash, sizeof( valid_hash ) ) );
2264 TEST_EQUAL( psa_hash_verify( &operation,
2265 valid_hash, sizeof( valid_hash ) ),
2266 PSA_ERROR_BAD_STATE );
2267 PSA_ASSERT( psa_hash_abort( &operation ) );
2268
2269 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002270 TEST_EQUAL( psa_hash_finish( &operation,
2271 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002272 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002273 PSA_ASSERT( psa_hash_abort( &operation ) );
2274
2275 /* Call finish twice in a row. */
2276 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2277 PSA_ASSERT( psa_hash_finish( &operation,
2278 hash, sizeof( hash ), &hash_len ) );
2279 TEST_EQUAL( psa_hash_finish( &operation,
2280 hash, sizeof( hash ), &hash_len ),
2281 PSA_ERROR_BAD_STATE );
2282 PSA_ASSERT( psa_hash_abort( &operation ) );
2283
2284 /* Call finish after calling verify. */
2285 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2286 PSA_ASSERT( psa_hash_verify( &operation,
2287 valid_hash, sizeof( valid_hash ) ) );
2288 TEST_EQUAL( psa_hash_finish( &operation,
2289 hash, sizeof( hash ), &hash_len ),
2290 PSA_ERROR_BAD_STATE );
2291 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002292
2293exit:
2294 mbedtls_psa_crypto_free( );
2295}
2296/* END_CASE */
2297
itayzafrir27e69452018-11-01 14:26:34 +02002298/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2299void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002300{
2301 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002302 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2303 * appended to it */
2304 unsigned char hash[] = {
2305 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2306 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2307 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002308 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002309 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002310
Gilles Peskine8817f612018-12-18 00:18:46 +01002311 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002312
itayzafrir27e69452018-11-01 14:26:34 +02002313 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002314 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002315 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002316 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002317
itayzafrir27e69452018-11-01 14:26:34 +02002318 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002319 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002320 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002321 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002322
itayzafrir27e69452018-11-01 14:26:34 +02002323 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002324 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002325 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002326 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002327
itayzafrirec93d302018-10-18 18:01:10 +03002328exit:
2329 mbedtls_psa_crypto_free( );
2330}
2331/* END_CASE */
2332
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002333/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2334void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002335{
2336 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002337 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002338 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002339 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002340 size_t hash_len;
2341
Gilles Peskine8817f612018-12-18 00:18:46 +01002342 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002343
itayzafrir58028322018-10-25 10:22:01 +03002344 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002345 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002346 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002347 hash, expected_size - 1, &hash_len ),
2348 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002349
2350exit:
2351 mbedtls_psa_crypto_free( );
2352}
2353/* END_CASE */
2354
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002355/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2356void hash_clone_source_state( )
2357{
2358 psa_algorithm_t alg = PSA_ALG_SHA_256;
2359 unsigned char hash[PSA_HASH_MAX_SIZE];
2360 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2361 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2362 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2363 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2364 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2365 size_t hash_len;
2366
2367 PSA_ASSERT( psa_crypto_init( ) );
2368 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2369
2370 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2371 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2372 PSA_ASSERT( psa_hash_finish( &op_finished,
2373 hash, sizeof( hash ), &hash_len ) );
2374 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2375 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2376
2377 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2378 PSA_ERROR_BAD_STATE );
2379
2380 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2381 PSA_ASSERT( psa_hash_finish( &op_init,
2382 hash, sizeof( hash ), &hash_len ) );
2383 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2384 PSA_ASSERT( psa_hash_finish( &op_finished,
2385 hash, sizeof( hash ), &hash_len ) );
2386 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2387 PSA_ASSERT( psa_hash_finish( &op_aborted,
2388 hash, sizeof( hash ), &hash_len ) );
2389
2390exit:
2391 psa_hash_abort( &op_source );
2392 psa_hash_abort( &op_init );
2393 psa_hash_abort( &op_setup );
2394 psa_hash_abort( &op_finished );
2395 psa_hash_abort( &op_aborted );
2396 mbedtls_psa_crypto_free( );
2397}
2398/* END_CASE */
2399
2400/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2401void hash_clone_target_state( )
2402{
2403 psa_algorithm_t alg = PSA_ALG_SHA_256;
2404 unsigned char hash[PSA_HASH_MAX_SIZE];
2405 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2406 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2407 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2408 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2409 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2410 size_t hash_len;
2411
2412 PSA_ASSERT( psa_crypto_init( ) );
2413
2414 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2415 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2416 PSA_ASSERT( psa_hash_finish( &op_finished,
2417 hash, sizeof( hash ), &hash_len ) );
2418 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2419 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2420
2421 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2422 PSA_ASSERT( psa_hash_finish( &op_target,
2423 hash, sizeof( hash ), &hash_len ) );
2424
2425 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2426 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2427 PSA_ERROR_BAD_STATE );
2428 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2429 PSA_ERROR_BAD_STATE );
2430
2431exit:
2432 psa_hash_abort( &op_target );
2433 psa_hash_abort( &op_init );
2434 psa_hash_abort( &op_setup );
2435 psa_hash_abort( &op_finished );
2436 psa_hash_abort( &op_aborted );
2437 mbedtls_psa_crypto_free( );
2438}
2439/* END_CASE */
2440
itayzafrir58028322018-10-25 10:22:01 +03002441/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002442void mac_operation_init( )
2443{
Jaeden Amero252ef282019-02-15 14:05:35 +00002444 const uint8_t input[1] = { 0 };
2445
Jaeden Amero769ce272019-01-04 11:48:03 +00002446 /* Test each valid way of initializing the object, except for `= {0}`, as
2447 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2448 * though it's OK by the C standard. We could test for this, but we'd need
2449 * to supress the Clang warning for the test. */
2450 psa_mac_operation_t func = psa_mac_operation_init( );
2451 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2452 psa_mac_operation_t zero;
2453
2454 memset( &zero, 0, sizeof( zero ) );
2455
Jaeden Amero252ef282019-02-15 14:05:35 +00002456 /* A freshly-initialized MAC operation should not be usable. */
2457 TEST_EQUAL( psa_mac_update( &func,
2458 input, sizeof( input ) ),
2459 PSA_ERROR_BAD_STATE );
2460 TEST_EQUAL( psa_mac_update( &init,
2461 input, sizeof( input ) ),
2462 PSA_ERROR_BAD_STATE );
2463 TEST_EQUAL( psa_mac_update( &zero,
2464 input, sizeof( input ) ),
2465 PSA_ERROR_BAD_STATE );
2466
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002467 /* A default MAC operation should be abortable without error. */
2468 PSA_ASSERT( psa_mac_abort( &func ) );
2469 PSA_ASSERT( psa_mac_abort( &init ) );
2470 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002471}
2472/* END_CASE */
2473
2474/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002475void mac_setup( int key_type_arg,
2476 data_t *key,
2477 int alg_arg,
2478 int expected_status_arg )
2479{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002480 psa_key_type_t key_type = key_type_arg;
2481 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002482 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002483 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002484 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2485#if defined(KNOWN_SUPPORTED_MAC_ALG)
2486 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2487#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002488
Gilles Peskine8817f612018-12-18 00:18:46 +01002489 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002490
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002491 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2492 &operation, &status ) )
2493 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002494 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002495
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002496 /* The operation object should be reusable. */
2497#if defined(KNOWN_SUPPORTED_MAC_ALG)
2498 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2499 smoke_test_key_data,
2500 sizeof( smoke_test_key_data ),
2501 KNOWN_SUPPORTED_MAC_ALG,
2502 &operation, &status ) )
2503 goto exit;
2504 TEST_EQUAL( status, PSA_SUCCESS );
2505#endif
2506
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002507exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002508 mbedtls_psa_crypto_free( );
2509}
2510/* END_CASE */
2511
2512/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002513void mac_bad_order( )
2514{
2515 psa_key_handle_t handle = 0;
2516 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2517 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2518 const uint8_t key[] = {
2519 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2520 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2521 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
2522 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2523 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2524 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2525 size_t sign_mac_length = 0;
2526 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2527 const uint8_t verify_mac[] = {
2528 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2529 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2530 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2531
2532 PSA_ASSERT( psa_crypto_init( ) );
2533 PSA_ASSERT( psa_allocate_key( &handle ) );
2534 psa_key_policy_set_usage( &policy,
2535 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
2536 alg );
2537 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2538
2539 PSA_ASSERT( psa_import_key( handle, key_type,
2540 key, sizeof(key) ) );
2541
2542 /* Call update without calling setup beforehand. */
2543 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2544 PSA_ERROR_BAD_STATE );
2545 PSA_ASSERT( psa_mac_abort( &operation ) );
2546
2547 /* Call sign finish without calling setup beforehand. */
2548 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2549 &sign_mac_length),
2550 PSA_ERROR_BAD_STATE );
2551 PSA_ASSERT( psa_mac_abort( &operation ) );
2552
2553 /* Call verify finish without calling setup beforehand. */
2554 TEST_EQUAL( psa_mac_verify_finish( &operation,
2555 verify_mac, sizeof( verify_mac ) ),
2556 PSA_ERROR_BAD_STATE );
2557 PSA_ASSERT( psa_mac_abort( &operation ) );
2558
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002559 /* Call setup twice in a row. */
2560 PSA_ASSERT( psa_mac_sign_setup( &operation,
2561 handle, alg ) );
2562 TEST_EQUAL( psa_mac_sign_setup( &operation,
2563 handle, alg ),
2564 PSA_ERROR_BAD_STATE );
2565 PSA_ASSERT( psa_mac_abort( &operation ) );
2566
Jaeden Amero252ef282019-02-15 14:05:35 +00002567 /* Call update after sign finish. */
2568 PSA_ASSERT( psa_mac_sign_setup( &operation,
2569 handle, alg ) );
2570 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2571 PSA_ASSERT( psa_mac_sign_finish( &operation,
2572 sign_mac, sizeof( sign_mac ),
2573 &sign_mac_length ) );
2574 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2575 PSA_ERROR_BAD_STATE );
2576 PSA_ASSERT( psa_mac_abort( &operation ) );
2577
2578 /* Call update after verify finish. */
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_update( &operation, input, sizeof( input ) ),
2585 PSA_ERROR_BAD_STATE );
2586 PSA_ASSERT( psa_mac_abort( &operation ) );
2587
2588 /* Call sign finish twice in a row. */
2589 PSA_ASSERT( psa_mac_sign_setup( &operation,
2590 handle, alg ) );
2591 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2592 PSA_ASSERT( psa_mac_sign_finish( &operation,
2593 sign_mac, sizeof( sign_mac ),
2594 &sign_mac_length ) );
2595 TEST_EQUAL( psa_mac_sign_finish( &operation,
2596 sign_mac, sizeof( sign_mac ),
2597 &sign_mac_length ),
2598 PSA_ERROR_BAD_STATE );
2599 PSA_ASSERT( psa_mac_abort( &operation ) );
2600
2601 /* Call verify finish twice in a row. */
2602 PSA_ASSERT( psa_mac_verify_setup( &operation,
2603 handle, alg ) );
2604 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2605 PSA_ASSERT( psa_mac_verify_finish( &operation,
2606 verify_mac, sizeof( verify_mac ) ) );
2607 TEST_EQUAL( psa_mac_verify_finish( &operation,
2608 verify_mac, sizeof( verify_mac ) ),
2609 PSA_ERROR_BAD_STATE );
2610 PSA_ASSERT( psa_mac_abort( &operation ) );
2611
2612 /* Setup sign but try verify. */
2613 PSA_ASSERT( psa_mac_sign_setup( &operation,
2614 handle, alg ) );
2615 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2616 TEST_EQUAL( psa_mac_verify_finish( &operation,
2617 verify_mac, sizeof( verify_mac ) ),
2618 PSA_ERROR_BAD_STATE );
2619 PSA_ASSERT( psa_mac_abort( &operation ) );
2620
2621 /* Setup verify but try sign. */
2622 PSA_ASSERT( psa_mac_verify_setup( &operation,
2623 handle, alg ) );
2624 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2625 TEST_EQUAL( psa_mac_sign_finish( &operation,
2626 sign_mac, sizeof( sign_mac ),
2627 &sign_mac_length ),
2628 PSA_ERROR_BAD_STATE );
2629 PSA_ASSERT( psa_mac_abort( &operation ) );
2630
2631exit:
2632 mbedtls_psa_crypto_free( );
2633}
2634/* END_CASE */
2635
2636/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002637void mac_sign( int key_type_arg,
2638 data_t *key,
2639 int alg_arg,
2640 data_t *input,
2641 data_t *expected_mac )
2642{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002643 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002644 psa_key_type_t key_type = key_type_arg;
2645 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002646 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002647 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002648 /* Leave a little extra room in the output buffer. At the end of the
2649 * test, we'll check that the implementation didn't overwrite onto
2650 * this extra room. */
2651 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2652 size_t mac_buffer_size =
2653 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2654 size_t mac_length = 0;
2655
2656 memset( actual_mac, '+', sizeof( actual_mac ) );
2657 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2658 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2659
Gilles Peskine8817f612018-12-18 00:18:46 +01002660 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002661
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002662 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002663 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002664 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002665
Gilles Peskine8817f612018-12-18 00:18:46 +01002666 PSA_ASSERT( psa_import_key( handle, key_type,
2667 key->x, key->len ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002668
2669 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002670 PSA_ASSERT( psa_mac_sign_setup( &operation,
2671 handle, alg ) );
2672 PSA_ASSERT( psa_mac_update( &operation,
2673 input->x, input->len ) );
2674 PSA_ASSERT( psa_mac_sign_finish( &operation,
2675 actual_mac, mac_buffer_size,
2676 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002677
2678 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002679 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2680 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002681
2682 /* Verify that the end of the buffer is untouched. */
2683 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2684 sizeof( actual_mac ) - mac_length ) );
2685
2686exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002687 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002688 mbedtls_psa_crypto_free( );
2689}
2690/* END_CASE */
2691
2692/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002693void mac_verify( int key_type_arg,
2694 data_t *key,
2695 int alg_arg,
2696 data_t *input,
2697 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002698{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002699 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002700 psa_key_type_t key_type = key_type_arg;
2701 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002702 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002703 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002704
Gilles Peskine69c12672018-06-28 00:07:19 +02002705 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2706
Gilles Peskine8817f612018-12-18 00:18:46 +01002707 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002708
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002709 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002710 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002711 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad16036df908f2018-04-02 08:34:15 -07002712
Gilles Peskine8817f612018-12-18 00:18:46 +01002713 PSA_ASSERT( psa_import_key( handle, key_type,
2714 key->x, key->len ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002715
Gilles Peskine8817f612018-12-18 00:18:46 +01002716 PSA_ASSERT( psa_mac_verify_setup( &operation,
2717 handle, alg ) );
2718 PSA_ASSERT( psa_destroy_key( handle ) );
2719 PSA_ASSERT( psa_mac_update( &operation,
2720 input->x, input->len ) );
2721 PSA_ASSERT( psa_mac_verify_finish( &operation,
2722 expected_mac->x,
2723 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002724
2725exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002726 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002727 mbedtls_psa_crypto_free( );
2728}
2729/* END_CASE */
2730
2731/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002732void cipher_operation_init( )
2733{
Jaeden Ameroab439972019-02-15 14:12:05 +00002734 const uint8_t input[1] = { 0 };
2735 unsigned char output[1] = { 0 };
2736 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002737 /* Test each valid way of initializing the object, except for `= {0}`, as
2738 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2739 * though it's OK by the C standard. We could test for this, but we'd need
2740 * to supress the Clang warning for the test. */
2741 psa_cipher_operation_t func = psa_cipher_operation_init( );
2742 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2743 psa_cipher_operation_t zero;
2744
2745 memset( &zero, 0, sizeof( zero ) );
2746
Jaeden Ameroab439972019-02-15 14:12:05 +00002747 /* A freshly-initialized cipher operation should not be usable. */
2748 TEST_EQUAL( psa_cipher_update( &func,
2749 input, sizeof( input ),
2750 output, sizeof( output ),
2751 &output_length ),
2752 PSA_ERROR_BAD_STATE );
2753 TEST_EQUAL( psa_cipher_update( &init,
2754 input, sizeof( input ),
2755 output, sizeof( output ),
2756 &output_length ),
2757 PSA_ERROR_BAD_STATE );
2758 TEST_EQUAL( psa_cipher_update( &zero,
2759 input, sizeof( input ),
2760 output, sizeof( output ),
2761 &output_length ),
2762 PSA_ERROR_BAD_STATE );
2763
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002764 /* A default cipher operation should be abortable without error. */
2765 PSA_ASSERT( psa_cipher_abort( &func ) );
2766 PSA_ASSERT( psa_cipher_abort( &init ) );
2767 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002768}
2769/* END_CASE */
2770
2771/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002772void cipher_setup( int key_type_arg,
2773 data_t *key,
2774 int alg_arg,
2775 int expected_status_arg )
2776{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002777 psa_key_type_t key_type = key_type_arg;
2778 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002779 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002780 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002781 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002782#if defined(KNOWN_SUPPORTED_MAC_ALG)
2783 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2784#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002785
Gilles Peskine8817f612018-12-18 00:18:46 +01002786 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002787
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002788 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2789 &operation, &status ) )
2790 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002791 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002792
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002793 /* The operation object should be reusable. */
2794#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2795 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2796 smoke_test_key_data,
2797 sizeof( smoke_test_key_data ),
2798 KNOWN_SUPPORTED_CIPHER_ALG,
2799 &operation, &status ) )
2800 goto exit;
2801 TEST_EQUAL( status, PSA_SUCCESS );
2802#endif
2803
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002804exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002805 mbedtls_psa_crypto_free( );
2806}
2807/* END_CASE */
2808
2809/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002810void cipher_bad_order( )
2811{
2812 psa_key_handle_t handle = 0;
2813 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2814 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
2815 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
2816 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2817 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2818 const uint8_t key[] = {
2819 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2820 0xaa, 0xaa, 0xaa, 0xaa };
2821 const uint8_t text[] = {
2822 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2823 0xbb, 0xbb, 0xbb, 0xbb };
2824 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2825 size_t length = 0;
2826
2827 PSA_ASSERT( psa_crypto_init( ) );
2828 PSA_ASSERT( psa_allocate_key( &handle ) );
2829 psa_key_policy_set_usage( &policy,
2830 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
2831 alg );
2832 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
2833 PSA_ASSERT( psa_import_key( handle, key_type,
2834 key, sizeof(key) ) );
2835
2836
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002837 /* Call encrypt setup twice in a row. */
2838 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2839 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2840 PSA_ERROR_BAD_STATE );
2841 PSA_ASSERT( psa_cipher_abort( &operation ) );
2842
2843 /* Call decrypt setup twice in a row. */
2844 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2845 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2846 PSA_ERROR_BAD_STATE );
2847 PSA_ASSERT( psa_cipher_abort( &operation ) );
2848
Jaeden Ameroab439972019-02-15 14:12:05 +00002849 /* Generate an IV without calling setup beforehand. */
2850 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2851 buffer, sizeof( buffer ),
2852 &length ),
2853 PSA_ERROR_BAD_STATE );
2854 PSA_ASSERT( psa_cipher_abort( &operation ) );
2855
2856 /* Generate an IV twice in a row. */
2857 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2858 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2859 buffer, sizeof( buffer ),
2860 &length ) );
2861 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2862 buffer, sizeof( buffer ),
2863 &length ),
2864 PSA_ERROR_BAD_STATE );
2865 PSA_ASSERT( psa_cipher_abort( &operation ) );
2866
2867 /* Generate an IV after it's already set. */
2868 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2869 PSA_ASSERT( psa_cipher_set_iv( &operation,
2870 iv, sizeof( iv ) ) );
2871 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2872 buffer, sizeof( buffer ),
2873 &length ),
2874 PSA_ERROR_BAD_STATE );
2875 PSA_ASSERT( psa_cipher_abort( &operation ) );
2876
2877 /* Set an IV without calling setup beforehand. */
2878 TEST_EQUAL( psa_cipher_set_iv( &operation,
2879 iv, sizeof( iv ) ),
2880 PSA_ERROR_BAD_STATE );
2881 PSA_ASSERT( psa_cipher_abort( &operation ) );
2882
2883 /* Set an IV after it's already set. */
2884 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2885 PSA_ASSERT( psa_cipher_set_iv( &operation,
2886 iv, sizeof( iv ) ) );
2887 TEST_EQUAL( psa_cipher_set_iv( &operation,
2888 iv, sizeof( iv ) ),
2889 PSA_ERROR_BAD_STATE );
2890 PSA_ASSERT( psa_cipher_abort( &operation ) );
2891
2892 /* Set an IV after it's already generated. */
2893 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2894 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2895 buffer, sizeof( buffer ),
2896 &length ) );
2897 TEST_EQUAL( psa_cipher_set_iv( &operation,
2898 iv, sizeof( iv ) ),
2899 PSA_ERROR_BAD_STATE );
2900 PSA_ASSERT( psa_cipher_abort( &operation ) );
2901
2902 /* Call update without calling setup beforehand. */
2903 TEST_EQUAL( psa_cipher_update( &operation,
2904 text, sizeof( text ),
2905 buffer, sizeof( buffer ),
2906 &length ),
2907 PSA_ERROR_BAD_STATE );
2908 PSA_ASSERT( psa_cipher_abort( &operation ) );
2909
2910 /* Call update without an IV where an IV is required. */
2911 TEST_EQUAL( psa_cipher_update( &operation,
2912 text, sizeof( text ),
2913 buffer, sizeof( buffer ),
2914 &length ),
2915 PSA_ERROR_BAD_STATE );
2916 PSA_ASSERT( psa_cipher_abort( &operation ) );
2917
2918 /* Call update after finish. */
2919 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2920 PSA_ASSERT( psa_cipher_set_iv( &operation,
2921 iv, sizeof( iv ) ) );
2922 PSA_ASSERT( psa_cipher_finish( &operation,
2923 buffer, sizeof( buffer ), &length ) );
2924 TEST_EQUAL( psa_cipher_update( &operation,
2925 text, sizeof( text ),
2926 buffer, sizeof( buffer ),
2927 &length ),
2928 PSA_ERROR_BAD_STATE );
2929 PSA_ASSERT( psa_cipher_abort( &operation ) );
2930
2931 /* Call finish without calling setup beforehand. */
2932 TEST_EQUAL( psa_cipher_finish( &operation,
2933 buffer, sizeof( buffer ), &length ),
2934 PSA_ERROR_BAD_STATE );
2935 PSA_ASSERT( psa_cipher_abort( &operation ) );
2936
2937 /* Call finish without an IV where an IV is required. */
2938 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2939 /* Not calling update means we are encrypting an empty buffer, which is OK
2940 * for cipher modes with padding. */
2941 TEST_EQUAL( psa_cipher_finish( &operation,
2942 buffer, sizeof( buffer ), &length ),
2943 PSA_ERROR_BAD_STATE );
2944 PSA_ASSERT( psa_cipher_abort( &operation ) );
2945
2946 /* Call finish twice in a row. */
2947 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2948 PSA_ASSERT( psa_cipher_set_iv( &operation,
2949 iv, sizeof( iv ) ) );
2950 PSA_ASSERT( psa_cipher_finish( &operation,
2951 buffer, sizeof( buffer ), &length ) );
2952 TEST_EQUAL( psa_cipher_finish( &operation,
2953 buffer, sizeof( buffer ), &length ),
2954 PSA_ERROR_BAD_STATE );
2955 PSA_ASSERT( psa_cipher_abort( &operation ) );
2956
2957exit:
2958 mbedtls_psa_crypto_free( );
2959}
2960/* END_CASE */
2961
2962/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002963void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002964 data_t *key,
2965 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002966 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002967{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002968 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002969 psa_status_t status;
2970 psa_key_type_t key_type = key_type_arg;
2971 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002972 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002973 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002974 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002975 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002976 size_t output_buffer_size = 0;
2977 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002978 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002979 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00002980 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002981
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002982 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2983 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002984
Gilles Peskine8817f612018-12-18 00:18:46 +01002985 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002986
Gilles Peskined40c1fb2019-01-19 12:20:52 +01002987 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03002988 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01002989 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03002990
Gilles Peskine8817f612018-12-18 00:18:46 +01002991 PSA_ASSERT( psa_import_key( handle, key_type,
2992 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002993
Gilles Peskine8817f612018-12-18 00:18:46 +01002994 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2995 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002996
Gilles Peskine8817f612018-12-18 00:18:46 +01002997 PSA_ASSERT( psa_cipher_set_iv( &operation,
2998 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002999 output_buffer_size = ( (size_t) input->len +
3000 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003001 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003002
Gilles Peskine8817f612018-12-18 00:18:46 +01003003 PSA_ASSERT( psa_cipher_update( &operation,
3004 input->x, input->len,
3005 output, output_buffer_size,
3006 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003007 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003008 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003009 output + total_output_length,
3010 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003011 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003012 total_output_length += function_output_length;
3013
Gilles Peskinefe11b722018-12-18 00:24:04 +01003014 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003015 if( expected_status == PSA_SUCCESS )
3016 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003017 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003018 ASSERT_COMPARE( expected_output->x, expected_output->len,
3019 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003020 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003021
Gilles Peskine50e586b2018-06-08 14:28:46 +02003022exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003023 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003024 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003025 mbedtls_psa_crypto_free( );
3026}
3027/* END_CASE */
3028
3029/* BEGIN_CASE */
3030void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003031 data_t *key,
3032 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003033 int first_part_size_arg,
3034 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003035 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003036{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003037 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003038 psa_key_type_t key_type = key_type_arg;
3039 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003040 size_t first_part_size = first_part_size_arg;
3041 size_t output1_length = output1_length_arg;
3042 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003043 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003044 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003045 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003046 size_t output_buffer_size = 0;
3047 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003048 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003049 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003050 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003051
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003052 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3053 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003054
Gilles Peskine8817f612018-12-18 00:18:46 +01003055 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003056
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003057 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003058 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003059 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003060
Gilles Peskine8817f612018-12-18 00:18:46 +01003061 PSA_ASSERT( psa_import_key( handle, key_type,
3062 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003063
Gilles Peskine8817f612018-12-18 00:18:46 +01003064 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3065 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003066
Gilles Peskine8817f612018-12-18 00:18:46 +01003067 PSA_ASSERT( psa_cipher_set_iv( &operation,
3068 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003069 output_buffer_size = ( (size_t) input->len +
3070 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003071 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003072
Gilles Peskinee0866522019-02-19 19:44:00 +01003073 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003074 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3075 output, output_buffer_size,
3076 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003077 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003078 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003079 PSA_ASSERT( psa_cipher_update( &operation,
3080 input->x + first_part_size,
3081 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003082 output + total_output_length,
3083 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003084 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003085 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003086 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003087 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003088 output + total_output_length,
3089 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003090 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003091 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003092 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003093
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003094 ASSERT_COMPARE( expected_output->x, expected_output->len,
3095 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003096
3097exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003098 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003099 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003100 mbedtls_psa_crypto_free( );
3101}
3102/* END_CASE */
3103
3104/* BEGIN_CASE */
3105void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003106 data_t *key,
3107 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003108 int first_part_size_arg,
3109 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003110 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003111{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003112 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003113
3114 psa_key_type_t key_type = key_type_arg;
3115 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003116 size_t first_part_size = first_part_size_arg;
3117 size_t output1_length = output1_length_arg;
3118 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003119 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003120 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003121 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003122 size_t output_buffer_size = 0;
3123 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003124 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003125 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003126 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003127
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003128 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3129 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003130
Gilles Peskine8817f612018-12-18 00:18:46 +01003131 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003132
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003133 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003134 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003135 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003136
Gilles Peskine8817f612018-12-18 00:18:46 +01003137 PSA_ASSERT( psa_import_key( handle, key_type,
3138 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003139
Gilles Peskine8817f612018-12-18 00:18:46 +01003140 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3141 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003142
Gilles Peskine8817f612018-12-18 00:18:46 +01003143 PSA_ASSERT( psa_cipher_set_iv( &operation,
3144 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003145
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003146 output_buffer_size = ( (size_t) input->len +
3147 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003148 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003149
Gilles Peskinee0866522019-02-19 19:44:00 +01003150 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003151 PSA_ASSERT( psa_cipher_update( &operation,
3152 input->x, first_part_size,
3153 output, output_buffer_size,
3154 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003155 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003156 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003157 PSA_ASSERT( psa_cipher_update( &operation,
3158 input->x + first_part_size,
3159 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003160 output + total_output_length,
3161 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003162 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003163 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003164 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003165 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003166 output + total_output_length,
3167 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003168 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003169 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003170 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003171
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003172 ASSERT_COMPARE( expected_output->x, expected_output->len,
3173 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003174
3175exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003176 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003177 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003178 mbedtls_psa_crypto_free( );
3179}
3180/* END_CASE */
3181
Gilles Peskine50e586b2018-06-08 14:28:46 +02003182/* BEGIN_CASE */
3183void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003184 data_t *key,
3185 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003186 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003187{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003188 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003189 psa_status_t status;
3190 psa_key_type_t key_type = key_type_arg;
3191 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003192 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003193 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003194 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003195 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003196 size_t output_buffer_size = 0;
3197 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003198 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003199 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003200 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003201
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003202 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3203 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003204
Gilles Peskine8817f612018-12-18 00:18:46 +01003205 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003206
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003207 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003208 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003209 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003210
Gilles Peskine8817f612018-12-18 00:18:46 +01003211 PSA_ASSERT( psa_import_key( handle, key_type,
3212 key->x, key->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003213
Gilles Peskine8817f612018-12-18 00:18:46 +01003214 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3215 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003216
Gilles Peskine8817f612018-12-18 00:18:46 +01003217 PSA_ASSERT( psa_cipher_set_iv( &operation,
3218 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003219
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003220 output_buffer_size = ( (size_t) input->len +
3221 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003222 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003223
Gilles Peskine8817f612018-12-18 00:18:46 +01003224 PSA_ASSERT( psa_cipher_update( &operation,
3225 input->x, input->len,
3226 output, output_buffer_size,
3227 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003228 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003229 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003230 output + total_output_length,
3231 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003232 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003233 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003234 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003235
3236 if( expected_status == PSA_SUCCESS )
3237 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003238 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003239 ASSERT_COMPARE( expected_output->x, expected_output->len,
3240 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003241 }
3242
Gilles Peskine50e586b2018-06-08 14:28:46 +02003243exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003244 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003245 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003246 mbedtls_psa_crypto_free( );
3247}
3248/* END_CASE */
3249
Gilles Peskine50e586b2018-06-08 14:28:46 +02003250/* BEGIN_CASE */
3251void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003252 data_t *key,
3253 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003254{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003255 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003256 psa_key_type_t key_type = key_type_arg;
3257 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003258 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003259 size_t iv_size = 16;
3260 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003261 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003262 size_t output1_size = 0;
3263 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003264 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003265 size_t output2_size = 0;
3266 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003267 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003268 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3269 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003270 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003271
Gilles Peskine8817f612018-12-18 00:18:46 +01003272 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003273
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003274 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003275 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003276 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003277
Gilles Peskine8817f612018-12-18 00:18:46 +01003278 PSA_ASSERT( psa_import_key( handle, key_type,
3279 key->x, key->len ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003280
Gilles Peskine8817f612018-12-18 00:18:46 +01003281 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3282 handle, alg ) );
3283 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3284 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003285
Gilles Peskine8817f612018-12-18 00:18:46 +01003286 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3287 iv, iv_size,
3288 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003289 output1_size = ( (size_t) input->len +
3290 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003291 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003292
Gilles Peskine8817f612018-12-18 00:18:46 +01003293 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3294 output1, output1_size,
3295 &output1_length ) );
3296 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003297 output1 + output1_length,
3298 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003299 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003300
Gilles Peskine048b7f02018-06-08 14:20:49 +02003301 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003302
Gilles Peskine8817f612018-12-18 00:18:46 +01003303 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003304
3305 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003306 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003307
Gilles Peskine8817f612018-12-18 00:18:46 +01003308 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3309 iv, iv_length ) );
3310 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3311 output2, output2_size,
3312 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003313 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003314 PSA_ASSERT( psa_cipher_finish( &operation2,
3315 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003316 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003317 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003318
Gilles Peskine048b7f02018-06-08 14:20:49 +02003319 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003320
Gilles Peskine8817f612018-12-18 00:18:46 +01003321 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003322
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003323 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003324
3325exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003326 mbedtls_free( output1 );
3327 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003328 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03003329 mbedtls_psa_crypto_free( );
3330}
3331/* END_CASE */
3332
3333/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003334void cipher_verify_output_multipart( int alg_arg,
3335 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003336 data_t *key,
3337 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003338 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003339{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003340 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003341 psa_key_type_t key_type = key_type_arg;
3342 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003343 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003344 unsigned char iv[16] = {0};
3345 size_t iv_size = 16;
3346 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003347 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003348 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003349 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003350 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003351 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003352 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003353 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003354 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3355 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00003356 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003357
Gilles Peskine8817f612018-12-18 00:18:46 +01003358 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003359
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003360 PSA_ASSERT( psa_allocate_key( &handle ) );
Moran Pekered346952018-07-05 15:22:45 +03003361 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003362 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Moran Pekered346952018-07-05 15:22:45 +03003363
Gilles Peskine8817f612018-12-18 00:18:46 +01003364 PSA_ASSERT( psa_import_key( handle, key_type,
3365 key->x, key->len ) );
Moran Pekerded84402018-06-06 16:36:50 +03003366
Gilles Peskine8817f612018-12-18 00:18:46 +01003367 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3368 handle, alg ) );
3369 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3370 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003371
Gilles Peskine8817f612018-12-18 00:18:46 +01003372 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3373 iv, iv_size,
3374 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003375 output1_buffer_size = ( (size_t) input->len +
3376 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003377 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003378
Gilles Peskinee0866522019-02-19 19:44:00 +01003379 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003380
Gilles Peskine8817f612018-12-18 00:18:46 +01003381 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3382 output1, output1_buffer_size,
3383 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003384 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003385
Gilles Peskine8817f612018-12-18 00:18:46 +01003386 PSA_ASSERT( psa_cipher_update( &operation1,
3387 input->x + first_part_size,
3388 input->len - first_part_size,
3389 output1, output1_buffer_size,
3390 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003391 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003392
Gilles Peskine8817f612018-12-18 00:18:46 +01003393 PSA_ASSERT( psa_cipher_finish( &operation1,
3394 output1 + output1_length,
3395 output1_buffer_size - output1_length,
3396 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003397 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003398
Gilles Peskine8817f612018-12-18 00:18:46 +01003399 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003400
Gilles Peskine048b7f02018-06-08 14:20:49 +02003401 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003402 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003403
Gilles Peskine8817f612018-12-18 00:18:46 +01003404 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3405 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003406
Gilles Peskine8817f612018-12-18 00:18:46 +01003407 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3408 output2, output2_buffer_size,
3409 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003410 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003411
Gilles Peskine8817f612018-12-18 00:18:46 +01003412 PSA_ASSERT( psa_cipher_update( &operation2,
3413 output1 + first_part_size,
3414 output1_length - first_part_size,
3415 output2, output2_buffer_size,
3416 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003417 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003418
Gilles Peskine8817f612018-12-18 00:18:46 +01003419 PSA_ASSERT( psa_cipher_finish( &operation2,
3420 output2 + output2_length,
3421 output2_buffer_size - output2_length,
3422 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003423 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003424
Gilles Peskine8817f612018-12-18 00:18:46 +01003425 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003426
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003427 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003428
3429exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003430 mbedtls_free( output1 );
3431 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003432 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003433 mbedtls_psa_crypto_free( );
3434}
3435/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003436
Gilles Peskine20035e32018-02-03 22:44:14 +01003437/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003438void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003439 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003440 data_t *nonce,
3441 data_t *additional_data,
3442 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003443 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003444{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003445 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003446 psa_key_type_t key_type = key_type_arg;
3447 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003448 unsigned char *output_data = NULL;
3449 size_t output_size = 0;
3450 size_t output_length = 0;
3451 unsigned char *output_data2 = NULL;
3452 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003453 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003454 psa_status_t expected_result = expected_result_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003455 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003456
Gilles Peskine4abf7412018-06-18 16:35:34 +02003457 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003458 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003459
Gilles Peskine8817f612018-12-18 00:18:46 +01003460 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003461
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003462 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003463 psa_key_policy_set_usage( &policy,
3464 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
3465 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003466 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003467
Gilles Peskine8817f612018-12-18 00:18:46 +01003468 PSA_ASSERT( psa_import_key( handle, key_type,
3469 key_data->x, key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003470
Gilles Peskinefe11b722018-12-18 00:24:04 +01003471 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3472 nonce->x, nonce->len,
3473 additional_data->x,
3474 additional_data->len,
3475 input_data->x, input_data->len,
3476 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003477 &output_length ),
3478 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003479
3480 if( PSA_SUCCESS == expected_result )
3481 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003482 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003483
Gilles Peskinefe11b722018-12-18 00:24:04 +01003484 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3485 nonce->x, nonce->len,
3486 additional_data->x,
3487 additional_data->len,
3488 output_data, output_length,
3489 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003490 &output_length2 ),
3491 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003492
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003493 ASSERT_COMPARE( input_data->x, input_data->len,
3494 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003495 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003496
Gilles Peskinea1cac842018-06-11 19:33:02 +02003497exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003498 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003499 mbedtls_free( output_data );
3500 mbedtls_free( output_data2 );
3501 mbedtls_psa_crypto_free( );
3502}
3503/* END_CASE */
3504
3505/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003506void aead_encrypt( int key_type_arg, data_t *key_data,
3507 int alg_arg,
3508 data_t *nonce,
3509 data_t *additional_data,
3510 data_t *input_data,
3511 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003512{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003513 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003514 psa_key_type_t key_type = key_type_arg;
3515 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003516 unsigned char *output_data = NULL;
3517 size_t output_size = 0;
3518 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003519 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00003520 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003521
Gilles Peskine4abf7412018-06-18 16:35:34 +02003522 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003523 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003524
Gilles Peskine8817f612018-12-18 00:18:46 +01003525 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003526
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003527 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003528 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003529 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003530
Gilles Peskine8817f612018-12-18 00:18:46 +01003531 PSA_ASSERT( psa_import_key( handle, key_type,
3532 key_data->x,
3533 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003534
Gilles Peskine8817f612018-12-18 00:18:46 +01003535 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3536 nonce->x, nonce->len,
3537 additional_data->x, additional_data->len,
3538 input_data->x, input_data->len,
3539 output_data, output_size,
3540 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003541
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003542 ASSERT_COMPARE( expected_result->x, expected_result->len,
3543 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003544
Gilles Peskinea1cac842018-06-11 19:33:02 +02003545exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003546 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003547 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003548 mbedtls_psa_crypto_free( );
3549}
3550/* END_CASE */
3551
3552/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003553void aead_decrypt( int key_type_arg, data_t *key_data,
3554 int alg_arg,
3555 data_t *nonce,
3556 data_t *additional_data,
3557 data_t *input_data,
3558 data_t *expected_data,
3559 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003560{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003561 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003562 psa_key_type_t key_type = key_type_arg;
3563 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003564 unsigned char *output_data = NULL;
3565 size_t output_size = 0;
3566 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003567 size_t tag_length = 16;
Jaeden Amero70261c52019-01-04 11:47:20 +00003568 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003569 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003570
Gilles Peskine4abf7412018-06-18 16:35:34 +02003571 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003572 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003573
Gilles Peskine8817f612018-12-18 00:18:46 +01003574 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003575
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003576 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003577 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT , alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003578 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003579
Gilles Peskine8817f612018-12-18 00:18:46 +01003580 PSA_ASSERT( psa_import_key( handle, key_type,
3581 key_data->x,
3582 key_data->len ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003583
Gilles Peskinefe11b722018-12-18 00:24:04 +01003584 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3585 nonce->x, nonce->len,
3586 additional_data->x,
3587 additional_data->len,
3588 input_data->x, input_data->len,
3589 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003590 &output_length ),
3591 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003592
Gilles Peskine2d277862018-06-18 15:41:12 +02003593 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003594 ASSERT_COMPARE( expected_data->x, expected_data->len,
3595 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003596
Gilles Peskinea1cac842018-06-11 19:33:02 +02003597exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003598 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003599 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003600 mbedtls_psa_crypto_free( );
3601}
3602/* END_CASE */
3603
3604/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003605void signature_size( int type_arg,
3606 int bits,
3607 int alg_arg,
3608 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003609{
3610 psa_key_type_t type = type_arg;
3611 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003612 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003613 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003614exit:
3615 ;
3616}
3617/* END_CASE */
3618
3619/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003620void sign_deterministic( int key_type_arg, data_t *key_data,
3621 int alg_arg, data_t *input_data,
3622 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003623{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003624 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003625 psa_key_type_t key_type = key_type_arg;
3626 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003627 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003628 unsigned char *signature = NULL;
3629 size_t signature_size;
3630 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003631 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003632
Gilles Peskine8817f612018-12-18 00:18:46 +01003633 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003634
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003635 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003636 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003637 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003638
Gilles Peskine8817f612018-12-18 00:18:46 +01003639 PSA_ASSERT( psa_import_key( handle, key_type,
3640 key_data->x,
3641 key_data->len ) );
3642 PSA_ASSERT( psa_get_key_information( handle,
3643 NULL,
3644 &key_bits ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003645
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003646 /* Allocate a buffer which has the size advertized by the
3647 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003648 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3649 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003650 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003651 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003652 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003653
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003654 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003655 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3656 input_data->x, input_data->len,
3657 signature, signature_size,
3658 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003659 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003660 ASSERT_COMPARE( output_data->x, output_data->len,
3661 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003662
3663exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003664 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003665 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01003666 mbedtls_psa_crypto_free( );
3667}
3668/* END_CASE */
3669
3670/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003671void sign_fail( int key_type_arg, data_t *key_data,
3672 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003673 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003674{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003675 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003676 psa_key_type_t key_type = key_type_arg;
3677 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003678 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003679 psa_status_t actual_status;
3680 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003681 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003682 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003683 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003684
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003685 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003686
Gilles Peskine8817f612018-12-18 00:18:46 +01003687 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003688
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003689 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003690 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003691 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003692
Gilles Peskine8817f612018-12-18 00:18:46 +01003693 PSA_ASSERT( psa_import_key( handle, key_type,
3694 key_data->x,
3695 key_data->len ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003696
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003697 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003698 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003699 signature, signature_size,
3700 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003701 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003702 /* The value of *signature_length is unspecified on error, but
3703 * whatever it is, it should be less than signature_size, so that
3704 * if the caller tries to read *signature_length bytes without
3705 * checking the error code then they don't overflow a buffer. */
3706 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003707
3708exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003709 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003710 mbedtls_free( signature );
3711 mbedtls_psa_crypto_free( );
3712}
3713/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003714
3715/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003716void sign_verify( int key_type_arg, data_t *key_data,
3717 int alg_arg, data_t *input_data )
3718{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003719 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003720 psa_key_type_t key_type = key_type_arg;
3721 psa_algorithm_t alg = alg_arg;
3722 size_t key_bits;
3723 unsigned char *signature = NULL;
3724 size_t signature_size;
3725 size_t signature_length = 0xdeadbeef;
Jaeden Amero70261c52019-01-04 11:47:20 +00003726 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003727
Gilles Peskine8817f612018-12-18 00:18:46 +01003728 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003729
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003730 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003731 psa_key_policy_set_usage( &policy,
3732 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY,
3733 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003734 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003735
Gilles Peskine8817f612018-12-18 00:18:46 +01003736 PSA_ASSERT( psa_import_key( handle, key_type,
3737 key_data->x,
3738 key_data->len ) );
3739 PSA_ASSERT( psa_get_key_information( handle,
3740 NULL,
3741 &key_bits ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003742
3743 /* Allocate a buffer which has the size advertized by the
3744 * library. */
3745 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3746 key_bits, alg );
3747 TEST_ASSERT( signature_size != 0 );
3748 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003749 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003750
3751 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003752 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3753 input_data->x, input_data->len,
3754 signature, signature_size,
3755 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003756 /* Check that the signature length looks sensible. */
3757 TEST_ASSERT( signature_length <= signature_size );
3758 TEST_ASSERT( signature_length > 0 );
3759
3760 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003761 PSA_ASSERT( psa_asymmetric_verify(
3762 handle, alg,
3763 input_data->x, input_data->len,
3764 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003765
3766 if( input_data->len != 0 )
3767 {
3768 /* Flip a bit in the input and verify that the signature is now
3769 * detected as invalid. Flip a bit at the beginning, not at the end,
3770 * because ECDSA may ignore the last few bits of the input. */
3771 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003772 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3773 input_data->x, input_data->len,
3774 signature, signature_length ),
3775 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003776 }
3777
3778exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003779 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003780 mbedtls_free( signature );
3781 mbedtls_psa_crypto_free( );
3782}
3783/* END_CASE */
3784
3785/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003786void asymmetric_verify( int key_type_arg, data_t *key_data,
3787 int alg_arg, data_t *hash_data,
3788 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003789{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003790 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003791 psa_key_type_t key_type = key_type_arg;
3792 psa_algorithm_t alg = alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003793 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003794
Gilles Peskine69c12672018-06-28 00:07:19 +02003795 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3796
Gilles Peskine8817f612018-12-18 00:18:46 +01003797 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003798
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003799 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003800 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003801 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
itayzafrir5c753392018-05-08 11:18:38 +03003802
Gilles Peskine8817f612018-12-18 00:18:46 +01003803 PSA_ASSERT( psa_import_key( handle, key_type,
3804 key_data->x,
3805 key_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003806
Gilles Peskine8817f612018-12-18 00:18:46 +01003807 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3808 hash_data->x, hash_data->len,
3809 signature_data->x,
3810 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003811exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003812 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003813 mbedtls_psa_crypto_free( );
3814}
3815/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003816
3817/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003818void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3819 int alg_arg, data_t *hash_data,
3820 data_t *signature_data,
3821 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003822{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003823 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003824 psa_key_type_t key_type = key_type_arg;
3825 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003826 psa_status_t actual_status;
3827 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003828 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003829
Gilles Peskine8817f612018-12-18 00:18:46 +01003830 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003831
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003832 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003833 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003834 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003835
Gilles Peskine8817f612018-12-18 00:18:46 +01003836 PSA_ASSERT( psa_import_key( handle, key_type,
3837 key_data->x,
3838 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003839
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003840 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003841 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003842 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003843 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003844
Gilles Peskinefe11b722018-12-18 00:24:04 +01003845 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003846
3847exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003848 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003849 mbedtls_psa_crypto_free( );
3850}
3851/* END_CASE */
3852
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003853/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003854void asymmetric_encrypt( int key_type_arg,
3855 data_t *key_data,
3856 int alg_arg,
3857 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003858 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003859 int expected_output_length_arg,
3860 int expected_status_arg )
3861{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003862 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003863 psa_key_type_t key_type = key_type_arg;
3864 psa_algorithm_t alg = alg_arg;
3865 size_t expected_output_length = expected_output_length_arg;
3866 size_t key_bits;
3867 unsigned char *output = NULL;
3868 size_t output_size;
3869 size_t output_length = ~0;
3870 psa_status_t actual_status;
3871 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00003872 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003873
Gilles Peskine8817f612018-12-18 00:18:46 +01003874 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003875
Gilles Peskine656896e2018-06-29 19:12:28 +02003876 /* Import the key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003877 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003878 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003879 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
3880 PSA_ASSERT( psa_import_key( handle, key_type,
3881 key_data->x,
3882 key_data->len ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003883
3884 /* Determine the maximum output length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003885 PSA_ASSERT( psa_get_key_information( handle,
3886 NULL,
3887 &key_bits ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003888 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003889 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003890
3891 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003892 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003893 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003894 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003895 output, output_size,
3896 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003897 TEST_EQUAL( actual_status, expected_status );
3898 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003899
Gilles Peskine68428122018-06-30 18:42:41 +02003900 /* If the label is empty, the test framework puts a non-null pointer
3901 * in label->x. Test that a null pointer works as well. */
3902 if( label->len == 0 )
3903 {
3904 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003905 if( output_size != 0 )
3906 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003907 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003908 input_data->x, input_data->len,
3909 NULL, label->len,
3910 output, output_size,
3911 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003912 TEST_EQUAL( actual_status, expected_status );
3913 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003914 }
3915
Gilles Peskine656896e2018-06-29 19:12:28 +02003916exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003917 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003918 mbedtls_free( output );
3919 mbedtls_psa_crypto_free( );
3920}
3921/* END_CASE */
3922
3923/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003924void asymmetric_encrypt_decrypt( int key_type_arg,
3925 data_t *key_data,
3926 int alg_arg,
3927 data_t *input_data,
3928 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003929{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003930 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003931 psa_key_type_t key_type = key_type_arg;
3932 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003933 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003934 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003935 size_t output_size;
3936 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003937 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003938 size_t output2_size;
3939 size_t output2_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00003940 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003941
Gilles Peskine8817f612018-12-18 00:18:46 +01003942 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003943
Gilles Peskined40c1fb2019-01-19 12:20:52 +01003944 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003945 psa_key_policy_set_usage( &policy,
3946 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003947 alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01003948 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003949
Gilles Peskine8817f612018-12-18 00:18:46 +01003950 PSA_ASSERT( psa_import_key( handle, key_type,
3951 key_data->x,
3952 key_data->len ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003953
3954 /* Determine the maximum ciphertext length */
Gilles Peskine8817f612018-12-18 00:18:46 +01003955 PSA_ASSERT( psa_get_key_information( handle,
3956 NULL,
3957 &key_bits ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003958 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003959 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003960 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003961 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003962
Gilles Peskineeebd7382018-06-08 18:11:54 +02003963 /* We test encryption by checking that encrypt-then-decrypt gives back
3964 * the original plaintext because of the non-optional random
3965 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003966 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3967 input_data->x, input_data->len,
3968 label->x, label->len,
3969 output, output_size,
3970 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003971 /* We don't know what ciphertext length to expect, but check that
3972 * it looks sensible. */
3973 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003974
Gilles Peskine8817f612018-12-18 00:18:46 +01003975 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3976 output, output_length,
3977 label->x, label->len,
3978 output2, output2_size,
3979 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003980 ASSERT_COMPARE( input_data->x, input_data->len,
3981 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003982
3983exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003984 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003985 mbedtls_free( output );
3986 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003987 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003988}
3989/* END_CASE */
3990
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003991/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003992void asymmetric_decrypt( int key_type_arg,
3993 data_t *key_data,
3994 int alg_arg,
3995 data_t *input_data,
3996 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003997 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003998{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003999 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004000 psa_key_type_t key_type = key_type_arg;
4001 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004002 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004003 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004004 size_t output_length = ~0;
Jaeden Amero70261c52019-01-04 11:47:20 +00004005 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004006
Jaeden Amero412654a2019-02-06 12:57:46 +00004007 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004008 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004009
Gilles Peskine8817f612018-12-18 00:18:46 +01004010 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004011
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004012 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02004013 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004014 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004015
Gilles Peskine8817f612018-12-18 00:18:46 +01004016 PSA_ASSERT( psa_import_key( handle, key_type,
4017 key_data->x,
4018 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004019
Gilles Peskine8817f612018-12-18 00:18:46 +01004020 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4021 input_data->x, input_data->len,
4022 label->x, label->len,
4023 output,
4024 output_size,
4025 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004026 ASSERT_COMPARE( expected_data->x, expected_data->len,
4027 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004028
Gilles Peskine68428122018-06-30 18:42:41 +02004029 /* If the label is empty, the test framework puts a non-null pointer
4030 * in label->x. Test that a null pointer works as well. */
4031 if( label->len == 0 )
4032 {
4033 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004034 if( output_size != 0 )
4035 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004036 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4037 input_data->x, input_data->len,
4038 NULL, label->len,
4039 output,
4040 output_size,
4041 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004042 ASSERT_COMPARE( expected_data->x, expected_data->len,
4043 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004044 }
4045
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004046exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004047 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004048 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004049 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004050}
4051/* END_CASE */
4052
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004053/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004054void asymmetric_decrypt_fail( int key_type_arg,
4055 data_t *key_data,
4056 int alg_arg,
4057 data_t *input_data,
4058 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004059 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004060 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004061{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004062 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004063 psa_key_type_t key_type = key_type_arg;
4064 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004065 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004066 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004067 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004068 psa_status_t actual_status;
4069 psa_status_t expected_status = expected_status_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004070 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004071
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004072 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004073
Gilles Peskine8817f612018-12-18 00:18:46 +01004074 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004075
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004076 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02004077 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004078 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004079
Gilles Peskine8817f612018-12-18 00:18:46 +01004080 PSA_ASSERT( psa_import_key( handle, key_type,
4081 key_data->x,
4082 key_data->len ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004083
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004084 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004085 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004086 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004087 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004088 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004089 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004090 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004091
Gilles Peskine68428122018-06-30 18:42:41 +02004092 /* If the label is empty, the test framework puts a non-null pointer
4093 * in label->x. Test that a null pointer works as well. */
4094 if( label->len == 0 )
4095 {
4096 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004097 if( output_size != 0 )
4098 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004099 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004100 input_data->x, input_data->len,
4101 NULL, label->len,
4102 output, output_size,
4103 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004104 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004105 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004106 }
4107
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004108exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004109 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004110 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004111 mbedtls_psa_crypto_free( );
4112}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004113/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004114
4115/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00004116void crypto_generator_init( )
4117{
4118 /* Test each valid way of initializing the object, except for `= {0}`, as
4119 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4120 * though it's OK by the C standard. We could test for this, but we'd need
4121 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004122 size_t capacity;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004123 psa_crypto_generator_t func = psa_crypto_generator_init( );
4124 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
4125 psa_crypto_generator_t zero;
4126
4127 memset( &zero, 0, sizeof( zero ) );
4128
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004129 /* A default generator should not be able to report its capacity. */
4130 TEST_EQUAL( psa_get_generator_capacity( &func, &capacity ),
4131 PSA_ERROR_BAD_STATE );
4132 TEST_EQUAL( psa_get_generator_capacity( &init, &capacity ),
4133 PSA_ERROR_BAD_STATE );
4134 TEST_EQUAL( psa_get_generator_capacity( &zero, &capacity ),
4135 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004136
4137 /* A default generator should be abortable without error. */
4138 PSA_ASSERT( psa_generator_abort(&func) );
4139 PSA_ASSERT( psa_generator_abort(&init) );
4140 PSA_ASSERT( psa_generator_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004141}
4142/* END_CASE */
4143
4144/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004145void derive_setup( int key_type_arg,
4146 data_t *key_data,
4147 int alg_arg,
4148 data_t *salt,
4149 data_t *label,
4150 int requested_capacity_arg,
4151 int expected_status_arg )
4152{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004153 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004154 size_t key_type = key_type_arg;
4155 psa_algorithm_t alg = alg_arg;
4156 size_t requested_capacity = requested_capacity_arg;
4157 psa_status_t expected_status = expected_status_arg;
4158 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004159 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004160
Gilles Peskine8817f612018-12-18 00:18:46 +01004161 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004162
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004163 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004164 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004165 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004166
Gilles Peskine8817f612018-12-18 00:18:46 +01004167 PSA_ASSERT( psa_import_key( handle, key_type,
4168 key_data->x,
4169 key_data->len ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004170
Gilles Peskinefe11b722018-12-18 00:24:04 +01004171 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4172 salt->x, salt->len,
4173 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004174 requested_capacity ),
4175 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004176
4177exit:
4178 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004179 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004180 mbedtls_psa_crypto_free( );
4181}
4182/* END_CASE */
4183
4184/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004185void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004186{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004187 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004188 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004189 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004190 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004191 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004192 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004193 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4194 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4195 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Jaeden Amero70261c52019-01-04 11:47:20 +00004196 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004197
Gilles Peskine8817f612018-12-18 00:18:46 +01004198 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004199
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004200 PSA_ASSERT( psa_allocate_key( &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004201 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004202 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004203
Gilles Peskine8817f612018-12-18 00:18:46 +01004204 PSA_ASSERT( psa_import_key( handle, key_type,
4205 key_data,
4206 sizeof( key_data ) ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004207
4208 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01004209 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4210 NULL, 0,
4211 NULL, 0,
4212 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004213
4214 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01004215 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4216 NULL, 0,
4217 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004218 capacity ),
4219 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004220
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004221 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004222
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004223 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004224 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004225
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004226exit:
4227 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004228 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004229 mbedtls_psa_crypto_free( );
4230}
4231/* END_CASE */
4232
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004233/* BEGIN_CASE */
4234void test_derive_invalid_generator_tests( )
4235{
4236 uint8_t output_buffer[16];
4237 size_t buffer_size = 16;
4238 size_t capacity = 0;
4239 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4240
Nir Sonnenschein50789302018-10-31 12:16:38 +02004241 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004242 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004243
4244 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004245 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004246
Gilles Peskine8817f612018-12-18 00:18:46 +01004247 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004248
Nir Sonnenschein50789302018-10-31 12:16:38 +02004249 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004250 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004251
Nir Sonnenschein50789302018-10-31 12:16:38 +02004252 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004253 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004254
4255exit:
4256 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004257}
4258/* END_CASE */
4259
4260/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004261void derive_output( int alg_arg,
4262 data_t *key_data,
4263 data_t *salt,
4264 data_t *label,
4265 int requested_capacity_arg,
4266 data_t *expected_output1,
4267 data_t *expected_output2 )
4268{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004269 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004270 psa_algorithm_t alg = alg_arg;
4271 size_t requested_capacity = requested_capacity_arg;
4272 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4273 uint8_t *expected_outputs[2] =
4274 {expected_output1->x, expected_output2->x};
4275 size_t output_sizes[2] =
4276 {expected_output1->len, expected_output2->len};
4277 size_t output_buffer_size = 0;
4278 uint8_t *output_buffer = NULL;
4279 size_t expected_capacity;
4280 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00004281 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004282 psa_status_t status;
4283 unsigned i;
4284
4285 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4286 {
4287 if( output_sizes[i] > output_buffer_size )
4288 output_buffer_size = output_sizes[i];
4289 if( output_sizes[i] == 0 )
4290 expected_outputs[i] = NULL;
4291 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004292 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004293 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004294
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004295 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004296 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004297 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004298
Gilles Peskine8817f612018-12-18 00:18:46 +01004299 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
4300 key_data->x,
4301 key_data->len ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004302
4303 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004304 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4305 salt->x, salt->len,
4306 label->x, label->len,
4307 requested_capacity ) );
4308 PSA_ASSERT( psa_get_generator_capacity( &generator,
4309 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004310 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004311 expected_capacity = requested_capacity;
4312
4313 /* Expansion phase. */
4314 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4315 {
4316 /* Read some bytes. */
4317 status = psa_generator_read( &generator,
4318 output_buffer, output_sizes[i] );
4319 if( expected_capacity == 0 && output_sizes[i] == 0 )
4320 {
4321 /* Reading 0 bytes when 0 bytes are available can go either way. */
4322 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004323 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004324 continue;
4325 }
4326 else if( expected_capacity == 0 ||
4327 output_sizes[i] > expected_capacity )
4328 {
4329 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004330 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004331 expected_capacity = 0;
4332 continue;
4333 }
4334 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004335 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004336 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004337 ASSERT_COMPARE( output_buffer, output_sizes[i],
4338 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004339 /* Check the generator status. */
4340 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01004341 PSA_ASSERT( psa_get_generator_capacity( &generator,
4342 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004343 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004344 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004345 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004346
4347exit:
4348 mbedtls_free( output_buffer );
4349 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004350 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004351 mbedtls_psa_crypto_free( );
4352}
4353/* END_CASE */
4354
4355/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004356void derive_full( int alg_arg,
4357 data_t *key_data,
4358 data_t *salt,
4359 data_t *label,
4360 int requested_capacity_arg )
4361{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004362 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004363 psa_algorithm_t alg = alg_arg;
4364 size_t requested_capacity = requested_capacity_arg;
4365 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4366 unsigned char output_buffer[16];
4367 size_t expected_capacity = requested_capacity;
4368 size_t current_capacity;
Jaeden Amero70261c52019-01-04 11:47:20 +00004369 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004370
Gilles Peskine8817f612018-12-18 00:18:46 +01004371 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004372
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004373 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004374 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004375 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004376
Gilles Peskine8817f612018-12-18 00:18:46 +01004377 PSA_ASSERT( psa_import_key( handle, PSA_KEY_TYPE_DERIVE,
4378 key_data->x,
4379 key_data->len ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004380
4381 /* Extraction phase. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004382 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4383 salt->x, salt->len,
4384 label->x, label->len,
4385 requested_capacity ) );
4386 PSA_ASSERT( psa_get_generator_capacity( &generator,
4387 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004388 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004389
4390 /* Expansion phase. */
4391 while( current_capacity > 0 )
4392 {
4393 size_t read_size = sizeof( output_buffer );
4394 if( read_size > current_capacity )
4395 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01004396 PSA_ASSERT( psa_generator_read( &generator,
4397 output_buffer,
4398 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004399 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01004400 PSA_ASSERT( psa_get_generator_capacity( &generator,
4401 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004402 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004403 }
4404
4405 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004406 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004407 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004408
Gilles Peskine8817f612018-12-18 00:18:46 +01004409 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004410
4411exit:
4412 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004413 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02004414 mbedtls_psa_crypto_free( );
4415}
4416/* END_CASE */
4417
4418/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004419void derive_key_exercise( int alg_arg,
4420 data_t *key_data,
4421 data_t *salt,
4422 data_t *label,
4423 int derived_type_arg,
4424 int derived_bits_arg,
4425 int derived_usage_arg,
4426 int derived_alg_arg )
4427{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004428 psa_key_handle_t base_handle = 0;
4429 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004430 psa_algorithm_t alg = alg_arg;
4431 psa_key_type_t derived_type = derived_type_arg;
4432 size_t derived_bits = derived_bits_arg;
4433 psa_key_usage_t derived_usage = derived_usage_arg;
4434 psa_algorithm_t derived_alg = derived_alg_arg;
4435 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
4436 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004437 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004438 psa_key_type_t got_type;
4439 size_t got_bits;
4440
Gilles Peskine8817f612018-12-18 00:18:46 +01004441 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004442
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004443 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004444 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004445 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
4446 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
4447 key_data->x,
4448 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004449
4450 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004451 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4452 salt->x, salt->len,
4453 label->x, label->len,
4454 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004455 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004456 psa_key_policy_set_usage( &policy, derived_usage, derived_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004457 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4458 PSA_ASSERT( psa_generator_import_key( derived_handle,
4459 derived_type,
4460 derived_bits,
4461 &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004462
4463 /* Test the key information */
Gilles Peskine8817f612018-12-18 00:18:46 +01004464 PSA_ASSERT( psa_get_key_information( derived_handle,
4465 &got_type,
4466 &got_bits ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004467 TEST_EQUAL( got_type, derived_type );
4468 TEST_EQUAL( got_bits, derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004469
4470 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004471 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004472 goto exit;
4473
4474exit:
4475 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004476 psa_destroy_key( base_handle );
4477 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004478 mbedtls_psa_crypto_free( );
4479}
4480/* END_CASE */
4481
4482/* BEGIN_CASE */
4483void derive_key_export( int alg_arg,
4484 data_t *key_data,
4485 data_t *salt,
4486 data_t *label,
4487 int bytes1_arg,
4488 int bytes2_arg )
4489{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004490 psa_key_handle_t base_handle = 0;
4491 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004492 psa_algorithm_t alg = alg_arg;
4493 size_t bytes1 = bytes1_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004494 size_t derived_bits = PSA_BYTES_TO_BITS( bytes1 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004495 size_t bytes2 = bytes2_arg;
4496 size_t capacity = bytes1 + bytes2;
4497 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004498 uint8_t *output_buffer = NULL;
4499 uint8_t *export_buffer = NULL;
Jaeden Amero70261c52019-01-04 11:47:20 +00004500 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004501 size_t length;
4502
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004503 ASSERT_ALLOC( output_buffer, capacity );
4504 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004505 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004506
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004507 PSA_ASSERT( psa_allocate_key( &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004508 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004509 PSA_ASSERT( psa_set_key_policy( base_handle, &policy ) );
4510 PSA_ASSERT( psa_import_key( base_handle, PSA_KEY_TYPE_DERIVE,
4511 key_data->x,
4512 key_data->len ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004513
4514 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004515 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4516 salt->x, salt->len,
4517 label->x, label->len,
4518 capacity ) );
4519 PSA_ASSERT( psa_generator_read( &generator,
4520 output_buffer,
4521 capacity ) );
4522 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004523
4524 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004525 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4526 salt->x, salt->len,
4527 label->x, label->len,
4528 capacity ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004529 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004530 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT, 0 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004531 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4532 PSA_ASSERT( psa_generator_import_key( derived_handle,
4533 PSA_KEY_TYPE_RAW_DATA,
4534 derived_bits,
4535 &generator ) );
4536 PSA_ASSERT( psa_export_key( derived_handle,
4537 export_buffer, bytes1,
4538 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004539 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004540 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004541 PSA_ASSERT( psa_allocate_key( &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004542 PSA_ASSERT( psa_set_key_policy( derived_handle, &policy ) );
4543 PSA_ASSERT( psa_generator_import_key( derived_handle,
4544 PSA_KEY_TYPE_RAW_DATA,
4545 PSA_BYTES_TO_BITS( bytes2 ),
4546 &generator ) );
4547 PSA_ASSERT( psa_export_key( derived_handle,
4548 export_buffer + bytes1, bytes2,
4549 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004550 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004551
4552 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004553 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4554 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004555
4556exit:
4557 mbedtls_free( output_buffer );
4558 mbedtls_free( export_buffer );
4559 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004560 psa_destroy_key( base_handle );
4561 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004562 mbedtls_psa_crypto_free( );
4563}
4564/* END_CASE */
4565
4566/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004567void key_agreement_setup( int alg_arg,
4568 int our_key_type_arg, data_t *our_key_data,
4569 data_t *peer_key_data,
4570 int expected_status_arg )
4571{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004572 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004573 psa_algorithm_t alg = alg_arg;
4574 psa_key_type_t our_key_type = our_key_type_arg;
4575 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004576 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004577
Gilles Peskine8817f612018-12-18 00:18:46 +01004578 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004579
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004580 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004581 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004582 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4583 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4584 our_key_data->x,
4585 our_key_data->len ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004586
Gilles Peskinefe11b722018-12-18 00:24:04 +01004587 TEST_EQUAL( psa_key_agreement( &generator,
4588 our_key,
4589 peer_key_data->x, peer_key_data->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004590 alg ),
4591 expected_status_arg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004592
4593exit:
4594 psa_generator_abort( &generator );
4595 psa_destroy_key( our_key );
4596 mbedtls_psa_crypto_free( );
4597}
4598/* END_CASE */
4599
4600/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004601void key_agreement_capacity( int alg_arg,
4602 int our_key_type_arg, data_t *our_key_data,
4603 data_t *peer_key_data,
4604 int expected_capacity_arg )
4605{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004606 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004607 psa_algorithm_t alg = alg_arg;
4608 psa_key_type_t our_key_type = our_key_type_arg;
4609 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004610 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004611 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004612 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004613
Gilles Peskine8817f612018-12-18 00:18:46 +01004614 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004615
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004616 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004617 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004618 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4619 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4620 our_key_data->x,
4621 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004622
Gilles Peskine8817f612018-12-18 00:18:46 +01004623 PSA_ASSERT( psa_key_agreement( &generator,
4624 our_key,
4625 peer_key_data->x, peer_key_data->len,
4626 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004627
Gilles Peskinebf491972018-10-25 22:36:12 +02004628 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004629 PSA_ASSERT( psa_get_generator_capacity(
4630 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004631 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004632
Gilles Peskinebf491972018-10-25 22:36:12 +02004633 /* Test the actual capacity by reading the output. */
4634 while( actual_capacity > sizeof( output ) )
4635 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004636 PSA_ASSERT( psa_generator_read( &generator,
4637 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004638 actual_capacity -= sizeof( output );
4639 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004640 PSA_ASSERT( psa_generator_read( &generator,
4641 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004642 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004643 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004644
Gilles Peskine59685592018-09-18 12:11:34 +02004645exit:
4646 psa_generator_abort( &generator );
4647 psa_destroy_key( our_key );
4648 mbedtls_psa_crypto_free( );
4649}
4650/* END_CASE */
4651
4652/* BEGIN_CASE */
4653void key_agreement_output( int alg_arg,
4654 int our_key_type_arg, data_t *our_key_data,
4655 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004656 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004657{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004658 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004659 psa_algorithm_t alg = alg_arg;
4660 psa_key_type_t our_key_type = our_key_type_arg;
4661 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Jaeden Amero70261c52019-01-04 11:47:20 +00004662 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004663 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004664
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004665 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4666 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004667
Gilles Peskine8817f612018-12-18 00:18:46 +01004668 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004669
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004670 PSA_ASSERT( psa_allocate_key( &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004671 psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004672 PSA_ASSERT( psa_set_key_policy( our_key, &policy ) );
4673 PSA_ASSERT( psa_import_key( our_key, our_key_type,
4674 our_key_data->x,
4675 our_key_data->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004676
Gilles Peskine8817f612018-12-18 00:18:46 +01004677 PSA_ASSERT( psa_key_agreement( &generator,
4678 our_key,
4679 peer_key_data->x, peer_key_data->len,
4680 alg ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004681
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004682 PSA_ASSERT( psa_generator_read( &generator,
4683 actual_output,
4684 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004685 ASSERT_COMPARE( actual_output, expected_output1->len,
4686 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004687 if( expected_output2->len != 0 )
4688 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004689 PSA_ASSERT( psa_generator_read( &generator,
4690 actual_output,
4691 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004692 ASSERT_COMPARE( actual_output, expected_output2->len,
4693 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004694 }
Gilles Peskine59685592018-09-18 12:11:34 +02004695
4696exit:
4697 psa_generator_abort( &generator );
4698 psa_destroy_key( our_key );
4699 mbedtls_psa_crypto_free( );
4700 mbedtls_free( actual_output );
4701}
4702/* END_CASE */
4703
4704/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004705void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004706{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004707 size_t bytes = bytes_arg;
4708 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004709 unsigned char *output = NULL;
4710 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004711 size_t i;
4712 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004713
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004714 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4715 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004716 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004717
Gilles Peskine8817f612018-12-18 00:18:46 +01004718 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004719
Gilles Peskinea50d7392018-06-21 10:22:13 +02004720 /* Run several times, to ensure that every output byte will be
4721 * nonzero at least once with overwhelming probability
4722 * (2^(-8*number_of_runs)). */
4723 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004724 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004725 if( bytes != 0 )
4726 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004727 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004728
4729 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004730 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4731 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004732
4733 for( i = 0; i < bytes; i++ )
4734 {
4735 if( output[i] != 0 )
4736 ++changed[i];
4737 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004738 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004739
4740 /* Check that every byte was changed to nonzero at least once. This
4741 * validates that psa_generate_random is overwriting every byte of
4742 * the output buffer. */
4743 for( i = 0; i < bytes; i++ )
4744 {
4745 TEST_ASSERT( changed[i] != 0 );
4746 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004747
4748exit:
4749 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004750 mbedtls_free( output );
4751 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004752}
4753/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004754
4755/* BEGIN_CASE */
4756void generate_key( int type_arg,
4757 int bits_arg,
4758 int usage_arg,
4759 int alg_arg,
4760 int expected_status_arg )
4761{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004762 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004763 psa_key_type_t type = type_arg;
4764 psa_key_usage_t usage = usage_arg;
4765 size_t bits = bits_arg;
4766 psa_algorithm_t alg = alg_arg;
4767 psa_status_t expected_status = expected_status_arg;
4768 psa_key_type_t got_type;
4769 size_t got_bits;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004770 psa_status_t expected_info_status =
David Saadab4ecc272019-02-14 13:48:10 +02004771 expected_status == PSA_SUCCESS ? PSA_SUCCESS : PSA_ERROR_DOES_NOT_EXIST;
Jaeden Amero70261c52019-01-04 11:47:20 +00004772 psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004773
Gilles Peskine8817f612018-12-18 00:18:46 +01004774 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004775
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004776 PSA_ASSERT( psa_allocate_key( &handle ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004777 psa_key_policy_set_usage( &policy, usage, alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004778 PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004779
4780 /* Generate a key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004781 TEST_EQUAL( psa_generate_key( handle, type, bits, NULL, 0 ),
4782 expected_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004783
4784 /* Test the key information */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004785 TEST_EQUAL( psa_get_key_information( handle, &got_type, &got_bits ),
4786 expected_info_status );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004787 if( expected_info_status != PSA_SUCCESS )
4788 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01004789 TEST_EQUAL( got_type, type );
4790 TEST_EQUAL( got_bits, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004791
Gilles Peskine818ca122018-06-20 18:16:48 +02004792 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004793 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004794 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004795
4796exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004797 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004798 mbedtls_psa_crypto_free( );
4799}
4800/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004801
Darryl Greend49a4992018-06-18 17:27:26 +01004802/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
4803void persistent_key_load_key_from_storage( data_t *data, int type_arg,
4804 int bits, int usage_arg,
Darryl Green0c6575a2018-11-07 16:05:30 +00004805 int alg_arg, int generation_method,
4806 int export_status )
Darryl Greend49a4992018-06-18 17:27:26 +01004807{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004808 psa_key_handle_t handle = 0;
4809 psa_key_handle_t base_key;
Darryl Greend49a4992018-06-18 17:27:26 +01004810 psa_key_type_t type = (psa_key_type_t) type_arg;
4811 psa_key_type_t type_get;
4812 size_t bits_get;
Jaeden Amero70261c52019-01-04 11:47:20 +00004813 psa_key_policy_t policy_set = PSA_KEY_POLICY_INIT;
4814 psa_key_policy_t policy_get = PSA_KEY_POLICY_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004815 psa_key_usage_t policy_usage = (psa_key_usage_t) usage_arg;
4816 psa_algorithm_t policy_alg = (psa_algorithm_t) alg_arg;
Jaeden Amero70261c52019-01-04 11:47:20 +00004817 psa_key_policy_t base_policy_set = PSA_KEY_POLICY_INIT;
Darryl Green0c6575a2018-11-07 16:05:30 +00004818 psa_algorithm_t base_policy_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
4819 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004820 unsigned char *first_export = NULL;
4821 unsigned char *second_export = NULL;
4822 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4823 size_t first_exported_length;
4824 size_t second_exported_length;
4825
4826 ASSERT_ALLOC( first_export, export_size );
4827 ASSERT_ALLOC( second_export, export_size );
4828
Gilles Peskine8817f612018-12-18 00:18:46 +01004829 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004830
Gilles Peskine8817f612018-12-18 00:18:46 +01004831 PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
Gilles Peskine8817f612018-12-18 00:18:46 +01004832 &handle ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004833 psa_key_policy_set_usage( &policy_set, policy_usage,
4834 policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004835 PSA_ASSERT( psa_set_key_policy( handle, &policy_set ) );
Darryl Greend49a4992018-06-18 17:27:26 +01004836
Darryl Green0c6575a2018-11-07 16:05:30 +00004837 switch( generation_method )
4838 {
4839 case IMPORT_KEY:
4840 /* Import the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004841 PSA_ASSERT( psa_import_key( handle, type,
4842 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004843 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004844
Darryl Green0c6575a2018-11-07 16:05:30 +00004845 case GENERATE_KEY:
4846 /* Generate a key */
Gilles Peskine8817f612018-12-18 00:18:46 +01004847 PSA_ASSERT( psa_generate_key( handle, type, bits,
4848 NULL, 0 ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004849 break;
4850
4851 case DERIVE_KEY:
4852 /* Create base key */
Gilles Peskined40c1fb2019-01-19 12:20:52 +01004853 PSA_ASSERT( psa_allocate_key( &base_key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004854 psa_key_policy_set_usage( &base_policy_set, PSA_KEY_USAGE_DERIVE,
4855 base_policy_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +01004856 PSA_ASSERT( psa_set_key_policy(
4857 base_key, &base_policy_set ) );
4858 PSA_ASSERT( psa_import_key( base_key, PSA_KEY_TYPE_DERIVE,
4859 data->x, data->len ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004860 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004861 PSA_ASSERT( psa_key_derivation( &generator, base_key,
4862 base_policy_alg,
4863 NULL, 0, NULL, 0,
4864 export_size ) );
4865 PSA_ASSERT( psa_generator_import_key(
4866 handle, PSA_KEY_TYPE_RAW_DATA,
4867 bits, &generator ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004868 break;
4869 }
Darryl Greend49a4992018-06-18 17:27:26 +01004870
4871 /* Export the key */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004872 TEST_EQUAL( psa_export_key( handle,
4873 first_export, export_size,
4874 &first_exported_length ),
4875 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004876
4877 /* Shutdown and restart */
4878 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004879 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004880
Darryl Greend49a4992018-06-18 17:27:26 +01004881 /* Check key slot still contains key data */
Gilles Peskine8817f612018-12-18 00:18:46 +01004882 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, 1,
4883 &handle ) );
4884 PSA_ASSERT( psa_get_key_information(
4885 handle, &type_get, &bits_get ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004886 TEST_EQUAL( type_get, type );
4887 TEST_EQUAL( bits_get, (size_t) bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004888
Gilles Peskine8817f612018-12-18 00:18:46 +01004889 PSA_ASSERT( psa_get_key_policy( handle, &policy_get ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004890 TEST_EQUAL( psa_key_policy_get_usage( &policy_get ), policy_usage );
4891 TEST_EQUAL( psa_key_policy_get_algorithm( &policy_get ), policy_alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004892
4893 /* Export the key again */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004894 TEST_EQUAL( psa_export_key( handle,
4895 second_export, export_size,
4896 &second_exported_length ),
4897 export_status );
Darryl Greend49a4992018-06-18 17:27:26 +01004898
Darryl Green0c6575a2018-11-07 16:05:30 +00004899 if( export_status == PSA_SUCCESS )
4900 {
4901 ASSERT_COMPARE( first_export, first_exported_length,
4902 second_export, second_exported_length );
Darryl Greend49a4992018-06-18 17:27:26 +01004903
Darryl Green0c6575a2018-11-07 16:05:30 +00004904 switch( generation_method )
4905 {
4906 case IMPORT_KEY:
4907 ASSERT_COMPARE( data->x, data->len,
4908 first_export, first_exported_length );
4909 break;
4910 default:
4911 break;
4912 }
4913 }
4914
4915 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004916 if( ! exercise_key( handle, policy_usage, policy_alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004917 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004918
4919exit:
4920 mbedtls_free( first_export );
4921 mbedtls_free( second_export );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004922 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004923 mbedtls_psa_crypto_free();
4924}
4925/* END_CASE */