blob: f1fea3809aef86e6ab9f606cfc06ee85f69a12f7 [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;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100215
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200216 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
217 psa_set_key_algorithm( &attributes, alg );
218 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +0200219 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &handle ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100220
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;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200248 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100249
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200250 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
251 psa_set_key_algorithm( &attributes, alg );
252 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +0200253 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &handle ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100254
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;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200349 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200350 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
351 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200352 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
353 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
354 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
355 * have this macro yet. */
356 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE(
357 psa_get_key_type( &attributes ) );
358 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200359 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100360 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
361 handle, alg ) );
362 PSA_ASSERT( psa_cipher_set_iv( &operation,
363 iv, iv_length ) );
364 PSA_ASSERT( psa_cipher_update( &operation,
365 ciphertext, ciphertext_length,
366 decrypted, sizeof( decrypted ),
367 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200368 status = psa_cipher_finish( &operation,
369 decrypted + part_length,
370 sizeof( decrypted ) - part_length,
371 &part_length );
372 /* For a stream cipher, all inputs are valid. For a block cipher,
373 * if the input is some aribtrary data rather than an actual
374 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200375 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200376 TEST_ASSERT( status == PSA_SUCCESS ||
377 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200378 else
379 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200380 }
381
382 return( 1 );
383
384exit:
385 psa_cipher_abort( &operation );
386 return( 0 );
387}
388
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100389static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200390 psa_key_usage_t usage,
391 psa_algorithm_t alg )
392{
393 unsigned char nonce[16] = {0};
394 size_t nonce_length = sizeof( nonce );
395 unsigned char plaintext[16] = "Hello, world...";
396 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
397 size_t ciphertext_length = sizeof( ciphertext );
398 size_t plaintext_length = sizeof( ciphertext );
399
400 if( usage & PSA_KEY_USAGE_ENCRYPT )
401 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100402 PSA_ASSERT( psa_aead_encrypt( handle, alg,
403 nonce, nonce_length,
404 NULL, 0,
405 plaintext, sizeof( plaintext ),
406 ciphertext, sizeof( ciphertext ),
407 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200408 }
409
410 if( usage & PSA_KEY_USAGE_DECRYPT )
411 {
412 psa_status_t verify_status =
413 ( usage & PSA_KEY_USAGE_ENCRYPT ?
414 PSA_SUCCESS :
415 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100416 TEST_EQUAL( psa_aead_decrypt( handle, alg,
417 nonce, nonce_length,
418 NULL, 0,
419 ciphertext, ciphertext_length,
420 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100421 &plaintext_length ),
422 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200423 }
424
425 return( 1 );
426
427exit:
428 return( 0 );
429}
430
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100431static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200432 psa_key_usage_t usage,
433 psa_algorithm_t alg )
434{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200435 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
436 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200437 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200438 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100439 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
440
441 /* If the policy allows signing with any hash, just pick one. */
442 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
443 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100444#if defined(KNOWN_SUPPORTED_HASH_ALG)
445 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
446 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100447#else
448 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100449 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100450#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100451 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200452
453 if( usage & PSA_KEY_USAGE_SIGN )
454 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200455 /* Some algorithms require the payload to have the size of
456 * the hash encoded in the algorithm. Use this input size
457 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200458 if( hash_alg != 0 )
459 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100460 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
461 payload, payload_length,
462 signature, sizeof( signature ),
463 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200464 }
465
466 if( usage & PSA_KEY_USAGE_VERIFY )
467 {
468 psa_status_t verify_status =
469 ( usage & PSA_KEY_USAGE_SIGN ?
470 PSA_SUCCESS :
471 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100472 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
473 payload, payload_length,
474 signature, signature_length ),
475 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200476 }
477
478 return( 1 );
479
480exit:
481 return( 0 );
482}
483
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100484static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200485 psa_key_usage_t usage,
486 psa_algorithm_t alg )
487{
488 unsigned char plaintext[256] = "Hello, world...";
489 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
490 size_t ciphertext_length = sizeof( ciphertext );
491 size_t plaintext_length = 16;
492
493 if( usage & PSA_KEY_USAGE_ENCRYPT )
494 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100495 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
496 plaintext, plaintext_length,
497 NULL, 0,
498 ciphertext, sizeof( ciphertext ),
499 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200500 }
501
502 if( usage & PSA_KEY_USAGE_DECRYPT )
503 {
504 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100505 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200506 ciphertext, ciphertext_length,
507 NULL, 0,
508 plaintext, sizeof( plaintext ),
509 &plaintext_length );
510 TEST_ASSERT( status == PSA_SUCCESS ||
511 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
512 ( status == PSA_ERROR_INVALID_ARGUMENT ||
513 status == PSA_ERROR_INVALID_PADDING ) ) );
514 }
515
516 return( 1 );
517
518exit:
519 return( 0 );
520}
Gilles Peskine02b75072018-07-01 22:31:34 +0200521
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100522static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200523 psa_key_usage_t usage,
524 psa_algorithm_t alg )
525{
526 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
527 unsigned char label[16] = "This is a label.";
528 size_t label_length = sizeof( label );
529 unsigned char seed[16] = "abcdefghijklmnop";
530 size_t seed_length = sizeof( seed );
531 unsigned char output[1];
532
533 if( usage & PSA_KEY_USAGE_DERIVE )
534 {
Gilles Peskineb70a0fd2019-01-07 22:59:38 +0100535 if( PSA_ALG_IS_HKDF( alg ) )
536 {
537 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
538 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
539 PSA_KDF_STEP_SALT,
540 label,
541 label_length ) );
542 PSA_ASSERT( psa_key_derivation_input_key( &generator,
543 PSA_KDF_STEP_SECRET,
544 handle ) );
545 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
546 PSA_KDF_STEP_INFO,
547 seed,
548 seed_length ) );
549 }
550 else
551 {
552 // legacy
553 PSA_ASSERT( psa_key_derivation( &generator,
554 handle, alg,
555 label, label_length,
556 seed, seed_length,
557 sizeof( output ) ) );
558 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100559 PSA_ASSERT( psa_generator_read( &generator,
560 output,
561 sizeof( output ) ) );
562 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200563 }
564
565 return( 1 );
566
567exit:
568 return( 0 );
569}
570
Gilles Peskinec7998b72018-11-07 18:45:02 +0100571/* We need two keys to exercise key agreement. Exercise the
572 * private key against its own public key. */
573static psa_status_t key_agreement_with_self( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +0100574 psa_key_handle_t handle )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100575{
576 psa_key_type_t private_key_type;
577 psa_key_type_t public_key_type;
578 size_t key_bits;
579 uint8_t *public_key = NULL;
580 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200581 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinec7998b72018-11-07 18:45:02 +0100582 * psa_key_agreement fails. This isn't fully satisfactory, but it's
583 * good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200584 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200585 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100586
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200587 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
588 private_key_type = psa_get_key_type( &attributes );
589 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100590 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
591 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
592 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100593 PSA_ASSERT( psa_export_public_key( handle,
594 public_key, public_key_length,
595 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100596
Gilles Peskine969c5d62019-01-16 15:53:06 +0100597 status = psa_key_agreement( generator, PSA_KDF_STEP_SECRET, handle,
598 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100599exit:
600 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200601 psa_reset_key_attributes( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100602 return( status );
603}
604
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200605/* We need two keys to exercise key agreement. Exercise the
606 * private key against its own public key. */
607static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
608 psa_key_handle_t handle )
609{
610 psa_key_type_t private_key_type;
611 psa_key_type_t public_key_type;
612 size_t key_bits;
613 uint8_t *public_key = NULL;
614 size_t public_key_length;
615 uint8_t output[1024];
616 size_t output_length;
617 /* Return GENERIC_ERROR if something other than the final call to
618 * psa_key_agreement fails. This isn't fully satisfactory, but it's
619 * good enough: callers will report it as a failed test anyway. */
620 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200621 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200622
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200623 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
624 private_key_type = psa_get_key_type( &attributes );
625 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200626 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( private_key_type );
627 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
628 ASSERT_ALLOC( public_key, public_key_length );
629 PSA_ASSERT( psa_export_public_key( handle,
630 public_key, public_key_length,
631 &public_key_length ) );
632
633 status = psa_key_agreement_raw_shared_secret(
634 alg, handle,
635 public_key, public_key_length,
636 output, sizeof( output ), &output_length );
637exit:
638 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200639 psa_reset_key_attributes( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200640 return( status );
641}
642
643static int exercise_raw_key_agreement_key( psa_key_handle_t handle,
644 psa_key_usage_t usage,
645 psa_algorithm_t alg )
646{
647 int ok = 0;
648
649 if( usage & PSA_KEY_USAGE_DERIVE )
650 {
651 /* We need two keys to exercise key agreement. Exercise the
652 * private key against its own public key. */
653 PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) );
654 }
655 ok = 1;
656
657exit:
658 return( ok );
659}
660
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100661static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200662 psa_key_usage_t usage,
663 psa_algorithm_t alg )
664{
665 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200666 unsigned char output[1];
667 int ok = 0;
668
669 if( usage & PSA_KEY_USAGE_DERIVE )
670 {
671 /* We need two keys to exercise key agreement. Exercise the
672 * private key against its own public key. */
Gilles Peskine969c5d62019-01-16 15:53:06 +0100673 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
674 PSA_ASSERT( key_agreement_with_self( &generator, handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +0100675 PSA_ASSERT( psa_generator_read( &generator,
676 output,
677 sizeof( output ) ) );
678 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200679 }
680 ok = 1;
681
682exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200683 return( ok );
684}
685
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200686static int is_oid_of_key_type( psa_key_type_t type,
687 const uint8_t *oid, size_t oid_length )
688{
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200689 const uint8_t *expected_oid = NULL;
690 size_t expected_oid_length = 0;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200691#if defined(MBEDTLS_RSA_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200692 if( PSA_KEY_TYPE_IS_RSA( type ) )
693 {
694 expected_oid = (uint8_t *) MBEDTLS_OID_PKCS1_RSA;
695 expected_oid_length = sizeof( MBEDTLS_OID_PKCS1_RSA ) - 1;
696 }
697 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200698#endif /* MBEDTLS_RSA_C */
699#if defined(MBEDTLS_ECP_C)
Gilles Peskineae3d2a22018-08-13 14:14:22 +0200700 if( PSA_KEY_TYPE_IS_ECC( type ) )
701 {
702 expected_oid = (uint8_t *) MBEDTLS_OID_EC_ALG_UNRESTRICTED;
703 expected_oid_length = sizeof( MBEDTLS_OID_EC_ALG_UNRESTRICTED ) - 1;
704 }
705 else
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200706#endif /* MBEDTLS_ECP_C */
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200707 {
708 char message[40];
709 mbedtls_snprintf( message, sizeof( message ),
710 "OID not known for key type=0x%08lx",
711 (unsigned long) type );
712 test_fail( message, __LINE__, __FILE__ );
713 return( 0 );
714 }
715
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200716 ASSERT_COMPARE( expected_oid, expected_oid_length, oid, oid_length );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200717 return( 1 );
718
719exit:
720 return( 0 );
721}
722
723static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
724 size_t min_bits, size_t max_bits,
725 int must_be_odd )
726{
727 size_t len;
728 size_t actual_bits;
729 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100730 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100731 MBEDTLS_ASN1_INTEGER ),
732 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200733 /* Tolerate a slight departure from DER encoding:
734 * - 0 may be represented by an empty string or a 1-byte string.
735 * - The sign bit may be used as a value bit. */
736 if( ( len == 1 && ( *p )[0] == 0 ) ||
737 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
738 {
739 ++( *p );
740 --len;
741 }
742 if( min_bits == 0 && len == 0 )
743 return( 1 );
744 msb = ( *p )[0];
745 TEST_ASSERT( msb != 0 );
746 actual_bits = 8 * ( len - 1 );
747 while( msb != 0 )
748 {
749 msb >>= 1;
750 ++actual_bits;
751 }
752 TEST_ASSERT( actual_bits >= min_bits );
753 TEST_ASSERT( actual_bits <= max_bits );
754 if( must_be_odd )
755 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
756 *p += len;
757 return( 1 );
758exit:
759 return( 0 );
760}
761
762static int asn1_get_implicit_tag( unsigned char **p, const unsigned char *end,
763 size_t *len,
764 unsigned char n, unsigned char tag )
765{
766 int ret;
767 ret = mbedtls_asn1_get_tag( p, end, len,
768 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
769 MBEDTLS_ASN1_CONSTRUCTED | ( n ) );
770 if( ret != 0 )
771 return( ret );
772 end = *p + *len;
773 ret = mbedtls_asn1_get_tag( p, end, len, tag );
774 if( ret != 0 )
775 return( ret );
776 if( *p + *len != end )
777 return( MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
778 return( 0 );
779}
780
781static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
782 uint8_t *exported, size_t exported_length )
783{
784 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100785 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200786 else
787 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200788
789#if defined(MBEDTLS_DES_C)
790 if( type == PSA_KEY_TYPE_DES )
791 {
792 /* Check the parity bits. */
793 unsigned i;
794 for( i = 0; i < bits / 8; i++ )
795 {
796 unsigned bit_count = 0;
797 unsigned m;
798 for( m = 1; m <= 0x100; m <<= 1 )
799 {
800 if( exported[i] & m )
801 ++bit_count;
802 }
803 TEST_ASSERT( bit_count % 2 != 0 );
804 }
805 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200806 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200807#endif
808
809#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
810 if( type == PSA_KEY_TYPE_RSA_KEYPAIR )
811 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200812 uint8_t *p = exported;
813 uint8_t *end = exported + exported_length;
814 size_t len;
815 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200816 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200817 * modulus INTEGER, -- n
818 * publicExponent INTEGER, -- e
819 * privateExponent INTEGER, -- d
820 * prime1 INTEGER, -- p
821 * prime2 INTEGER, -- q
822 * exponent1 INTEGER, -- d mod (p-1)
823 * exponent2 INTEGER, -- d mod (q-1)
824 * coefficient INTEGER, -- (inverse of q) mod p
825 * }
826 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100827 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
828 MBEDTLS_ASN1_SEQUENCE |
829 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
830 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200831 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
832 goto exit;
833 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
834 goto exit;
835 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
836 goto exit;
837 /* Require d to be at least half the size of n. */
838 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
839 goto exit;
840 /* Require p and q to be at most half the size of n, rounded up. */
841 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
842 goto exit;
843 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
844 goto exit;
845 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
846 goto exit;
847 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
848 goto exit;
849 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
850 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100851 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100852 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200853 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200854#endif /* MBEDTLS_RSA_C */
855
856#if defined(MBEDTLS_ECP_C)
857 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( type ) )
858 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100859 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100860 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100861 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200862 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200863#endif /* MBEDTLS_ECP_C */
864
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200865 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
866 {
867 uint8_t *p = exported;
868 uint8_t *end = exported + exported_length;
869 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200870#if defined(MBEDTLS_RSA_C)
871 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
872 {
873 /* RSAPublicKey ::= SEQUENCE {
874 * modulus INTEGER, -- n
875 * publicExponent INTEGER } -- e
876 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100877 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
878 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100879 MBEDTLS_ASN1_CONSTRUCTED ),
880 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100881 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200882 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
883 goto exit;
884 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
885 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100886 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200887 }
888 else
889#endif /* MBEDTLS_RSA_C */
890#if defined(MBEDTLS_ECP_C)
891 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
892 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000893 /* The representation of an ECC public key is:
894 * - The byte 0x04;
895 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
896 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
897 * - where m is the bit size associated with the curve.
Jaeden Amero6b196002019-01-10 10:23:21 +0000898 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100899 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
900 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200901 }
902 else
903#endif /* MBEDTLS_ECP_C */
904 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100905 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200906 mbedtls_snprintf( message, sizeof( message ),
907 "No sanity check for public key type=0x%08lx",
908 (unsigned long) type );
909 test_fail( message, __LINE__, __FILE__ );
910 return( 0 );
911 }
912 }
913 else
914
915 {
916 /* No sanity checks for other types */
917 }
918
919 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200920
921exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200922 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200923}
924
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100925static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200926 psa_key_usage_t usage )
927{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200928 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200929 uint8_t *exported = NULL;
930 size_t exported_size = 0;
931 size_t exported_length = 0;
932 int ok = 0;
933
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200934 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200935
936 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200937 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200938 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100939 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
940 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200941 ok = 1;
942 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +0200943 }
944
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200945 exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ),
946 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200947 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200948
Gilles Peskine8817f612018-12-18 00:18:46 +0100949 PSA_ASSERT( psa_export_key( handle,
950 exported, exported_size,
951 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200952 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
953 psa_get_key_bits( &attributes ),
954 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +0200955
956exit:
957 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200958 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +0200959 return( ok );
960}
961
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100962static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200963{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200964 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200965 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +0200966 uint8_t *exported = NULL;
967 size_t exported_size = 0;
968 size_t exported_length = 0;
969 int ok = 0;
970
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200971 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
972 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200973 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100974 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100975 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200976 return( 1 );
977 }
978
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200979 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(
980 psa_get_key_type( &attributes ) );
981 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type,
982 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200983 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200984
Gilles Peskine8817f612018-12-18 00:18:46 +0100985 PSA_ASSERT( psa_export_public_key( handle,
986 exported, exported_size,
987 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200988 ok = exported_key_sanity_check( public_type,
989 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +0200990 exported, exported_length );
991
992exit:
993 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200994 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +0200995 return( ok );
996}
997
Gilles Peskinec9516fb2019-02-05 20:32:06 +0100998/** Do smoke tests on a key.
999 *
1000 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1001 * sign/verify, or derivation) that is permitted according to \p usage.
1002 * \p usage and \p alg should correspond to the expected policy on the
1003 * key.
1004 *
1005 * Export the key if permitted by \p usage, and check that the output
1006 * looks sensible. If \p usage forbids export, check that
1007 * \p psa_export_key correctly rejects the attempt. If the key is
1008 * asymmetric, also check \p psa_export_public_key.
1009 *
1010 * If the key fails the tests, this function calls the test framework's
1011 * `test_fail` function and returns false. Otherwise this function returns
1012 * true. Therefore it should be used as follows:
1013 * ```
1014 * if( ! exercise_key( ... ) ) goto exit;
1015 * ```
1016 *
1017 * \param handle The key to exercise. It should be capable of performing
1018 * \p alg.
1019 * \param usage The usage flags to assume.
1020 * \param alg The algorithm to exercise.
1021 *
1022 * \retval 0 The key failed the smoke tests.
1023 * \retval 1 The key passed the smoke tests.
1024 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001025static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +02001026 psa_key_usage_t usage,
1027 psa_algorithm_t alg )
1028{
1029 int ok;
1030 if( alg == 0 )
1031 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1032 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001033 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001034 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001035 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001036 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001037 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001038 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001039 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001040 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001041 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001042 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001043 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001044 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
1045 ok = exercise_raw_key_agreement_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001046 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001047 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001048 else
1049 {
1050 char message[40];
1051 mbedtls_snprintf( message, sizeof( message ),
1052 "No code to exercise alg=0x%08lx",
1053 (unsigned long) alg );
1054 test_fail( message, __LINE__, __FILE__ );
1055 ok = 0;
1056 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001057
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001058 ok = ok && exercise_export_key( handle, usage );
1059 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +02001060
Gilles Peskine02b75072018-07-01 22:31:34 +02001061 return( ok );
1062}
1063
Gilles Peskine10df3412018-10-25 22:35:43 +02001064static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1065 psa_algorithm_t alg )
1066{
1067 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1068 {
1069 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1070 PSA_KEY_USAGE_VERIFY :
1071 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
1072 }
1073 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1074 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1075 {
1076 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1077 PSA_KEY_USAGE_ENCRYPT :
1078 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1079 }
1080 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1081 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1082 {
1083 return( PSA_KEY_USAGE_DERIVE );
1084 }
1085 else
1086 {
1087 return( 0 );
1088 }
1089
1090}
Darryl Green0c6575a2018-11-07 16:05:30 +00001091
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001092static int test_operations_on_invalid_handle( psa_key_handle_t handle )
1093{
1094 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1095 uint8_t buffer[1];
1096 size_t length;
1097 int ok = 0;
1098
1099 psa_make_key_persistent( &attributes, 0x6964, PSA_KEY_LIFETIME_PERSISTENT );
1100 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1101 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1102 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
1103 TEST_EQUAL( psa_get_key_attributes( handle, &attributes ),
1104 PSA_ERROR_INVALID_HANDLE );
1105 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001106 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001107 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1108 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1109 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1110 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1111
1112 TEST_EQUAL( psa_export_key( handle,
1113 buffer, sizeof( buffer ), &length ),
1114 PSA_ERROR_INVALID_HANDLE );
1115 TEST_EQUAL( psa_export_public_key( handle,
1116 buffer, sizeof( buffer ), &length ),
1117 PSA_ERROR_INVALID_HANDLE );
1118
1119 TEST_EQUAL( psa_close_key( handle ), PSA_ERROR_INVALID_HANDLE );
1120 TEST_EQUAL( psa_destroy_key( handle ), PSA_ERROR_INVALID_HANDLE );
1121
1122 ok = 1;
1123
1124exit:
1125 psa_reset_key_attributes( &attributes );
1126 return( ok );
1127}
1128
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001129/* An overapproximation of the amount of storage needed for a key of the
1130 * given type and with the given content. The API doesn't make it easy
1131 * to find a good value for the size. The current implementation doesn't
1132 * care about the value anyway. */
1133#define KEY_BITS_FROM_DATA( type, data ) \
1134 ( data )->len
1135
Darryl Green0c6575a2018-11-07 16:05:30 +00001136typedef enum {
1137 IMPORT_KEY = 0,
1138 GENERATE_KEY = 1,
1139 DERIVE_KEY = 2
1140} generate_method;
1141
Gilles Peskinee59236f2018-01-27 23:32:46 +01001142/* END_HEADER */
1143
1144/* BEGIN_DEPENDENCIES
1145 * depends_on:MBEDTLS_PSA_CRYPTO_C
1146 * END_DEPENDENCIES
1147 */
1148
1149/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001150void static_checks( )
1151{
1152 size_t max_truncated_mac_size =
1153 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1154
1155 /* Check that the length for a truncated MAC always fits in the algorithm
1156 * encoding. The shifted mask is the maximum truncated value. The
1157 * untruncated algorithm may be one byte larger. */
1158 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
1159}
1160/* END_CASE */
1161
1162/* BEGIN_CASE */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001163void attributes_set_get( int id_arg, int lifetime_arg,
1164 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001165 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001166{
Gilles Peskine4747d192019-04-17 15:05:45 +02001167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001168 psa_key_id_t id = id_arg;
1169 psa_key_lifetime_t lifetime = lifetime_arg;
1170 psa_key_usage_t usage_flags = usage_flags_arg;
1171 psa_algorithm_t alg = alg_arg;
1172 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001173 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001174
1175 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1176 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1177 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1178 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1179 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001180 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001181
1182 psa_make_key_persistent( &attributes, id, lifetime );
1183 psa_set_key_usage_flags( &attributes, usage_flags );
1184 psa_set_key_algorithm( &attributes, alg );
1185 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001186 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001187
1188 TEST_EQUAL( psa_get_key_id( &attributes ), id );
1189 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1190 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1191 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1192 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001193 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001194
1195 psa_reset_key_attributes( &attributes );
1196
1197 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1198 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1199 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1200 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1201 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001202 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001203}
1204/* END_CASE */
1205
1206/* BEGIN_CASE */
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001207void import( data_t *data, int type_arg,
1208 int attr_bits_arg,
1209 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001210{
1211 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1212 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001213 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001214 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001215 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001216 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001217 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001218
Gilles Peskine8817f612018-12-18 00:18:46 +01001219 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001220
Gilles Peskine4747d192019-04-17 15:05:45 +02001221 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001222 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001223 status = psa_import_key( &attributes, data->x, data->len, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001224 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001225 if( status != PSA_SUCCESS )
1226 goto exit;
1227
1228 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1229 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001230 if( attr_bits != 0 )
1231 TEST_EQUAL( attr_bits, got_attributes.bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001232
1233 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001234 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001235
1236exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001237 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001238 psa_reset_key_attributes( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001239 mbedtls_psa_crypto_free( );
1240}
1241/* END_CASE */
1242
1243/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001244void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1245{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001246 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001247 size_t bits = bits_arg;
1248 psa_status_t expected_status = expected_status_arg;
1249 psa_status_t status;
1250 psa_key_type_t type =
1251 keypair ? PSA_KEY_TYPE_RSA_KEYPAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
1252 size_t buffer_size = /* Slight overapproximations */
1253 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001254 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001255 unsigned char *p;
1256 int ret;
1257 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001258 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001259
Gilles Peskine8817f612018-12-18 00:18:46 +01001260 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001261 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001262
1263 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1264 bits, keypair ) ) >= 0 );
1265 length = ret;
1266
1267 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001268 psa_set_key_type( &attributes, type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001269 status = psa_import_key( &attributes, p, length, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001270 TEST_EQUAL( status, expected_status );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001271 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001272 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001273
1274exit:
1275 mbedtls_free( buffer );
1276 mbedtls_psa_crypto_free( );
1277}
1278/* END_CASE */
1279
1280/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001281void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001282 int type_arg,
1283 int alg_arg,
1284 int usage_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001285 int expected_bits,
1286 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001287 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001288 int canonical_input )
1289{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001290 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001291 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001292 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001293 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001294 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001295 unsigned char *exported = NULL;
1296 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001297 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001298 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001299 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001300 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001301 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001302
Moran Pekercb088e72018-07-17 17:36:59 +03001303 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001304 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001305 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001306 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001307 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001308
Gilles Peskine4747d192019-04-17 15:05:45 +02001309 psa_set_key_usage_flags( &attributes, usage_arg );
1310 psa_set_key_algorithm( &attributes, alg );
1311 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001312
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001313 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001314 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001315
1316 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001317 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1318 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1319 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001320
1321 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001322 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001323 exported, export_size,
1324 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001325 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001326
1327 /* The exported length must be set by psa_export_key() to a value between 0
1328 * and export_size. On errors, the exported length must be 0. */
1329 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1330 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1331 TEST_ASSERT( exported_length <= export_size );
1332
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001333 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001334 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001335 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001336 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001337 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001338 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001339 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001340
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001341 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001342 goto exit;
1343
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001344 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001345 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001346 else
1347 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001348 psa_key_handle_t handle2;
Gilles Peskine73676cb2019-05-15 20:15:10 +02001349 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001350 PSA_ASSERT( psa_export_key( handle2,
1351 reexported,
1352 export_size,
1353 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001354 ASSERT_COMPARE( exported, exported_length,
1355 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001356 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001357 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001358 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001359
1360destroy:
1361 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001362 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001363 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001364
1365exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001366 mbedtls_free( exported );
1367 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001368 psa_reset_key_attributes( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001369 mbedtls_psa_crypto_free( );
1370}
1371/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001372
Moran Pekerf709f4a2018-06-06 17:26:04 +03001373/* BEGIN_CASE */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001374void invalid_handle( int handle )
Moran Peker28a38e62018-11-07 16:18:24 +02001375{
Gilles Peskine8817f612018-12-18 00:18:46 +01001376 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001377 test_operations_on_invalid_handle( handle );
Moran Peker28a38e62018-11-07 16:18:24 +02001378
1379exit:
1380 mbedtls_psa_crypto_free( );
1381}
1382/* END_CASE */
1383
1384/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001385void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001386 int type_arg,
1387 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001388 int export_size_delta,
1389 int expected_export_status_arg,
1390 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001391{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001392 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001393 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001394 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001395 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001396 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001397 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001398 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001399 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001400 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001401
Gilles Peskine8817f612018-12-18 00:18:46 +01001402 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001403
Gilles Peskine4747d192019-04-17 15:05:45 +02001404 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1405 psa_set_key_algorithm( &attributes, alg );
1406 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001407
1408 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001409 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001410
Gilles Peskine49c25912018-10-29 15:15:31 +01001411 /* Export the public key */
1412 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001413 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001414 exported, export_size,
1415 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001416 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001417 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001418 {
1419 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR( type );
1420 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001421 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1422 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001423 TEST_ASSERT( expected_public_key->len <=
1424 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001425 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1426 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001427 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001428
1429exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001430 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001431 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001432 psa_reset_key_attributes( &attributes );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001433 mbedtls_psa_crypto_free( );
1434}
1435/* END_CASE */
1436
Gilles Peskine20035e32018-02-03 22:44:14 +01001437/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001438void import_and_exercise_key( data_t *data,
1439 int type_arg,
1440 int bits_arg,
1441 int alg_arg )
1442{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001443 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001444 psa_key_type_t type = type_arg;
1445 size_t bits = bits_arg;
1446 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001447 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001448 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001449 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001450
Gilles Peskine8817f612018-12-18 00:18:46 +01001451 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001452
Gilles Peskine4747d192019-04-17 15:05:45 +02001453 psa_set_key_usage_flags( &attributes, usage );
1454 psa_set_key_algorithm( &attributes, alg );
1455 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001456
1457 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001458 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001459
1460 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001461 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1462 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1463 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001464
1465 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001466 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001467 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001468
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001469 PSA_ASSERT( psa_destroy_key( handle ) );
1470 test_operations_on_invalid_handle( handle );
1471
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001472exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001473 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001474 psa_reset_key_attributes( &got_attributes );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001475 mbedtls_psa_crypto_free( );
1476}
1477/* END_CASE */
1478
1479/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001480void key_policy( int usage_arg, int alg_arg )
1481{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001482 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001483 psa_algorithm_t alg = alg_arg;
1484 psa_key_usage_t usage = usage_arg;
1485 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1486 unsigned char key[32] = {0};
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001487 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001488
1489 memset( key, 0x2a, sizeof( key ) );
1490
Gilles Peskine8817f612018-12-18 00:18:46 +01001491 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001492
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001493 psa_set_key_usage_flags( &attributes, usage );
1494 psa_set_key_algorithm( &attributes, alg );
1495 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001496
Gilles Peskine73676cb2019-05-15 20:15:10 +02001497 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001498
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001499 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1500 TEST_EQUAL( psa_get_key_type( &attributes ), key_type );
1501 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage );
1502 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001503
1504exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001505 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001506 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001507 mbedtls_psa_crypto_free( );
1508}
1509/* END_CASE */
1510
1511/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001512void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001513{
1514 /* Test each valid way of initializing the object, except for `= {0}`, as
1515 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1516 * though it's OK by the C standard. We could test for this, but we'd need
1517 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001518 psa_key_attributes_t func = psa_key_attributes_init( );
1519 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1520 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001521
1522 memset( &zero, 0, sizeof( zero ) );
1523
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001524 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1525 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1526 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001527
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001528 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1529 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1530 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1531
1532 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1533 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1534 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1535
1536 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1537 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1538 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1539
1540 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1541 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1542 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001543}
1544/* END_CASE */
1545
1546/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001547void mac_key_policy( int policy_usage,
1548 int policy_alg,
1549 int key_type,
1550 data_t *key_data,
1551 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001552{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001553 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001554 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001555 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001556 psa_status_t status;
1557 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001558
Gilles Peskine8817f612018-12-18 00:18:46 +01001559 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001560
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001561 psa_set_key_usage_flags( &attributes, policy_usage );
1562 psa_set_key_algorithm( &attributes, policy_alg );
1563 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001564
Gilles Peskine73676cb2019-05-15 20:15:10 +02001565 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001566
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001567 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001568 if( policy_alg == exercise_alg &&
1569 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001570 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001571 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001572 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001573 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001574
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001575 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001576 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001577 if( policy_alg == exercise_alg &&
1578 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001579 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001580 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001581 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001582
1583exit:
1584 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001585 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001586 mbedtls_psa_crypto_free( );
1587}
1588/* END_CASE */
1589
1590/* BEGIN_CASE */
1591void cipher_key_policy( int policy_usage,
1592 int policy_alg,
1593 int key_type,
1594 data_t *key_data,
1595 int exercise_alg )
1596{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001597 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001598 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001599 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001600 psa_status_t status;
1601
Gilles Peskine8817f612018-12-18 00:18:46 +01001602 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001603
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001604 psa_set_key_usage_flags( &attributes, policy_usage );
1605 psa_set_key_algorithm( &attributes, policy_alg );
1606 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001607
Gilles Peskine73676cb2019-05-15 20:15:10 +02001608 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001609
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001610 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001611 if( policy_alg == exercise_alg &&
1612 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001613 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001614 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001615 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001616 psa_cipher_abort( &operation );
1617
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001618 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001619 if( policy_alg == exercise_alg &&
1620 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001621 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001622 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001623 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001624
1625exit:
1626 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001627 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001628 mbedtls_psa_crypto_free( );
1629}
1630/* END_CASE */
1631
1632/* BEGIN_CASE */
1633void aead_key_policy( int policy_usage,
1634 int policy_alg,
1635 int key_type,
1636 data_t *key_data,
1637 int nonce_length_arg,
1638 int tag_length_arg,
1639 int exercise_alg )
1640{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001641 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001642 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001643 psa_status_t status;
1644 unsigned char nonce[16] = {0};
1645 size_t nonce_length = nonce_length_arg;
1646 unsigned char tag[16];
1647 size_t tag_length = tag_length_arg;
1648 size_t output_length;
1649
1650 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1651 TEST_ASSERT( tag_length <= sizeof( tag ) );
1652
Gilles Peskine8817f612018-12-18 00:18:46 +01001653 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001654
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001655 psa_set_key_usage_flags( &attributes, policy_usage );
1656 psa_set_key_algorithm( &attributes, policy_alg );
1657 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001658
Gilles Peskine73676cb2019-05-15 20:15:10 +02001659 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001660
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001661 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001662 nonce, nonce_length,
1663 NULL, 0,
1664 NULL, 0,
1665 tag, tag_length,
1666 &output_length );
1667 if( policy_alg == exercise_alg &&
1668 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001669 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001670 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001671 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001672
1673 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001674 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001675 nonce, nonce_length,
1676 NULL, 0,
1677 tag, tag_length,
1678 NULL, 0,
1679 &output_length );
1680 if( policy_alg == exercise_alg &&
1681 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001682 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001683 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001684 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001685
1686exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001687 psa_destroy_key( handle );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001688 mbedtls_psa_crypto_free( );
1689}
1690/* END_CASE */
1691
1692/* BEGIN_CASE */
1693void asymmetric_encryption_key_policy( int policy_usage,
1694 int policy_alg,
1695 int key_type,
1696 data_t *key_data,
1697 int exercise_alg )
1698{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001699 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001700 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001701 psa_status_t status;
1702 size_t key_bits;
1703 size_t buffer_length;
1704 unsigned char *buffer = NULL;
1705 size_t output_length;
1706
Gilles Peskine8817f612018-12-18 00:18:46 +01001707 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001708
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001709 psa_set_key_usage_flags( &attributes, policy_usage );
1710 psa_set_key_algorithm( &attributes, policy_alg );
1711 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001712
Gilles Peskine73676cb2019-05-15 20:15:10 +02001713 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001714
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001715 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1716 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001717 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1718 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001719 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001720
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001721 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001722 NULL, 0,
1723 NULL, 0,
1724 buffer, buffer_length,
1725 &output_length );
1726 if( policy_alg == exercise_alg &&
1727 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001728 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001729 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001730 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001731
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001732 if( buffer_length != 0 )
1733 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001734 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001735 buffer, buffer_length,
1736 NULL, 0,
1737 buffer, buffer_length,
1738 &output_length );
1739 if( policy_alg == exercise_alg &&
1740 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001741 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001742 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001743 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001744
1745exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001746 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001747 psa_reset_key_attributes( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001748 mbedtls_psa_crypto_free( );
1749 mbedtls_free( buffer );
1750}
1751/* END_CASE */
1752
1753/* BEGIN_CASE */
1754void asymmetric_signature_key_policy( int policy_usage,
1755 int policy_alg,
1756 int key_type,
1757 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001758 int exercise_alg,
1759 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001760{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001761 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001762 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001763 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001764 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1765 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1766 * compatible with the policy and `payload_length_arg` is supposed to be
1767 * a valid input length to sign. If `payload_length_arg <= 0`,
1768 * `exercise_alg` is supposed to be forbidden by the policy. */
1769 int compatible_alg = payload_length_arg > 0;
1770 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001771 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1772 size_t signature_length;
1773
Gilles Peskine8817f612018-12-18 00:18:46 +01001774 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001775
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001776 psa_set_key_usage_flags( &attributes, policy_usage );
1777 psa_set_key_algorithm( &attributes, policy_alg );
1778 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001779
Gilles Peskine73676cb2019-05-15 20:15:10 +02001780 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001781
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001782 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001783 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001784 signature, sizeof( signature ),
1785 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001786 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001787 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001788 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001789 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001790
1791 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001792 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001793 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001794 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001795 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001796 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001797 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001798 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001799
1800exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001801 psa_destroy_key( handle );
Gilles Peskined5b33222018-06-18 22:20:03 +02001802 mbedtls_psa_crypto_free( );
1803}
1804/* END_CASE */
1805
1806/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001807void derive_key_policy( int policy_usage,
1808 int policy_alg,
1809 int key_type,
1810 data_t *key_data,
1811 int exercise_alg )
1812{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001813 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001814 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001815 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1816 psa_status_t status;
1817
Gilles Peskine8817f612018-12-18 00:18:46 +01001818 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001819
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001820 psa_set_key_usage_flags( &attributes, policy_usage );
1821 psa_set_key_algorithm( &attributes, policy_alg );
1822 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001823
Gilles Peskine73676cb2019-05-15 20:15:10 +02001824 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001825
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001826 status = psa_key_derivation( &generator, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001827 exercise_alg,
1828 NULL, 0,
1829 NULL, 0,
1830 1 );
1831 if( policy_alg == exercise_alg &&
1832 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001833 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001834 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001835 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001836
1837exit:
1838 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001839 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001840 mbedtls_psa_crypto_free( );
1841}
1842/* END_CASE */
1843
1844/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001845void agreement_key_policy( int policy_usage,
1846 int policy_alg,
1847 int key_type_arg,
1848 data_t *key_data,
1849 int exercise_alg )
1850{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001851 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001852 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001853 psa_key_type_t key_type = key_type_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001854 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1855 psa_status_t status;
1856
Gilles Peskine8817f612018-12-18 00:18:46 +01001857 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001858
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001859 psa_set_key_usage_flags( &attributes, policy_usage );
1860 psa_set_key_algorithm( &attributes, policy_alg );
1861 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001862
Gilles Peskine73676cb2019-05-15 20:15:10 +02001863 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001864
Gilles Peskine969c5d62019-01-16 15:53:06 +01001865 PSA_ASSERT( psa_key_derivation_setup( &generator, exercise_alg ) );
1866 status = key_agreement_with_self( &generator, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001867
Gilles Peskine01d718c2018-09-18 12:01:02 +02001868 if( policy_alg == exercise_alg &&
1869 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001870 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001871 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001872 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001873
1874exit:
1875 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001876 psa_destroy_key( handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001877 mbedtls_psa_crypto_free( );
1878}
1879/* END_CASE */
1880
1881/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001882void raw_agreement_key_policy( int policy_usage,
1883 int policy_alg,
1884 int key_type_arg,
1885 data_t *key_data,
1886 int exercise_alg )
1887{
1888 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001889 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001890 psa_key_type_t key_type = key_type_arg;
1891 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
1892 psa_status_t status;
1893
1894 PSA_ASSERT( psa_crypto_init( ) );
1895
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001896 psa_set_key_usage_flags( &attributes, policy_usage );
1897 psa_set_key_algorithm( &attributes, policy_alg );
1898 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001899
Gilles Peskine73676cb2019-05-15 20:15:10 +02001900 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001901
1902 status = raw_key_agreement_with_self( exercise_alg, handle );
1903
1904 if( policy_alg == exercise_alg &&
1905 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1906 PSA_ASSERT( status );
1907 else
1908 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1909
1910exit:
1911 psa_generator_abort( &generator );
1912 psa_destroy_key( handle );
1913 mbedtls_psa_crypto_free( );
1914}
1915/* END_CASE */
1916
1917/* BEGIN_CASE */
Gilles Peskine4a644642019-05-03 17:14:08 +02001918void copy_success( int source_usage_arg, int source_alg_arg,
1919 int type_arg, data_t *material,
1920 int copy_attributes,
1921 int target_usage_arg, int target_alg_arg,
1922 int expected_usage_arg, int expected_alg_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001923{
Gilles Peskineca25db92019-04-19 11:43:08 +02001924 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1925 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001926 psa_key_usage_t expected_usage = expected_usage_arg;
1927 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02001928 psa_key_handle_t source_handle = 0;
1929 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001930 uint8_t *export_buffer = NULL;
1931
Gilles Peskine57ab7212019-01-28 13:03:09 +01001932 PSA_ASSERT( psa_crypto_init( ) );
1933
Gilles Peskineca25db92019-04-19 11:43:08 +02001934 /* Prepare the source key. */
1935 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1936 psa_set_key_algorithm( &source_attributes, source_alg_arg );
1937 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001938 PSA_ASSERT( psa_import_key( &source_attributes, material->x, material->len, &source_handle ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001939 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001940
Gilles Peskineca25db92019-04-19 11:43:08 +02001941 /* Prepare the target attributes. */
1942 if( copy_attributes )
1943 target_attributes = source_attributes;
1944 if( target_usage_arg != -1 )
1945 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1946 if( target_alg_arg != -1 )
1947 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001948
1949 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02001950 PSA_ASSERT( psa_copy_key( source_handle,
1951 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001952
1953 /* Destroy the source to ensure that this doesn't affect the target. */
1954 PSA_ASSERT( psa_destroy_key( source_handle ) );
1955
1956 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02001957 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
1958 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1959 psa_get_key_type( &target_attributes ) );
1960 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1961 psa_get_key_bits( &target_attributes ) );
1962 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1963 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001964 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1965 {
1966 size_t length;
1967 ASSERT_ALLOC( export_buffer, material->len );
1968 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
1969 material->len, &length ) );
1970 ASSERT_COMPARE( material->x, material->len,
1971 export_buffer, length );
1972 }
1973 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
1974 goto exit;
1975
1976 PSA_ASSERT( psa_close_key( target_handle ) );
1977
1978exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001979 psa_reset_key_attributes( &source_attributes );
1980 psa_reset_key_attributes( &target_attributes );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001981 mbedtls_psa_crypto_free( );
1982 mbedtls_free( export_buffer );
1983}
1984/* END_CASE */
1985
1986/* BEGIN_CASE */
Gilles Peskine4a644642019-05-03 17:14:08 +02001987void copy_fail( int source_usage_arg, int source_alg_arg,
1988 int type_arg, data_t *material,
1989 int target_type_arg, int target_bits_arg,
1990 int target_usage_arg, int target_alg_arg,
1991 int expected_status_arg )
1992{
1993 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1994 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
1995 psa_key_handle_t source_handle = 0;
1996 psa_key_handle_t target_handle = 0;
1997
1998 PSA_ASSERT( psa_crypto_init( ) );
1999
2000 /* Prepare the source key. */
2001 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2002 psa_set_key_algorithm( &source_attributes, source_alg_arg );
2003 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine73676cb2019-05-15 20:15:10 +02002004 PSA_ASSERT( psa_import_key( &source_attributes, material->x, material->len, &source_handle ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002005
2006 /* Prepare the target attributes. */
2007 psa_set_key_type( &target_attributes, target_type_arg );
2008 psa_set_key_bits( &target_attributes, target_bits_arg );
2009 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2010 psa_set_key_algorithm( &target_attributes, target_alg_arg );
2011
2012 /* Try to copy the key. */
2013 TEST_EQUAL( psa_copy_key( source_handle,
2014 &target_attributes, &target_handle ),
2015 expected_status_arg );
2016exit:
2017 psa_reset_key_attributes( &source_attributes );
2018 psa_reset_key_attributes( &target_attributes );
2019 mbedtls_psa_crypto_free( );
2020}
2021/* END_CASE */
2022
2023/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002024void hash_operation_init( )
2025{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002026 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002027 /* Test each valid way of initializing the object, except for `= {0}`, as
2028 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2029 * though it's OK by the C standard. We could test for this, but we'd need
2030 * to supress the Clang warning for the test. */
2031 psa_hash_operation_t func = psa_hash_operation_init( );
2032 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2033 psa_hash_operation_t zero;
2034
2035 memset( &zero, 0, sizeof( zero ) );
2036
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002037 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002038 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2039 PSA_ERROR_BAD_STATE );
2040 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2041 PSA_ERROR_BAD_STATE );
2042 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2043 PSA_ERROR_BAD_STATE );
2044
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002045 /* A default hash operation should be abortable without error. */
2046 PSA_ASSERT( psa_hash_abort( &func ) );
2047 PSA_ASSERT( psa_hash_abort( &init ) );
2048 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002049}
2050/* END_CASE */
2051
2052/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002053void hash_setup( int alg_arg,
2054 int expected_status_arg )
2055{
2056 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002057 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002058 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002059 psa_status_t status;
2060
Gilles Peskine8817f612018-12-18 00:18:46 +01002061 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002062
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002063 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002064 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002065
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002066 /* Whether setup succeeded or failed, abort must succeed. */
2067 PSA_ASSERT( psa_hash_abort( &operation ) );
2068
2069 /* If setup failed, reproduce the failure, so as to
2070 * test the resulting state of the operation object. */
2071 if( status != PSA_SUCCESS )
2072 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2073
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002074 /* Now the operation object should be reusable. */
2075#if defined(KNOWN_SUPPORTED_HASH_ALG)
2076 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2077 PSA_ASSERT( psa_hash_abort( &operation ) );
2078#endif
2079
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002080exit:
2081 mbedtls_psa_crypto_free( );
2082}
2083/* END_CASE */
2084
2085/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002086void hash_bad_order( )
2087{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002088 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002089 unsigned char input[] = "";
2090 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002091 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002092 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2093 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2094 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002095 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002096 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002097 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002098
Gilles Peskine8817f612018-12-18 00:18:46 +01002099 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002100
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002101 /* Call setup twice in a row. */
2102 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2103 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2104 PSA_ERROR_BAD_STATE );
2105 PSA_ASSERT( psa_hash_abort( &operation ) );
2106
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002107 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002108 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002109 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002110 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002111
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002112 /* Call update after finish. */
2113 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2114 PSA_ASSERT( psa_hash_finish( &operation,
2115 hash, sizeof( hash ), &hash_len ) );
2116 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002117 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002118 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002119
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002120 /* Call verify without calling setup beforehand. */
2121 TEST_EQUAL( psa_hash_verify( &operation,
2122 valid_hash, sizeof( valid_hash ) ),
2123 PSA_ERROR_BAD_STATE );
2124 PSA_ASSERT( psa_hash_abort( &operation ) );
2125
2126 /* Call verify after finish. */
2127 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2128 PSA_ASSERT( psa_hash_finish( &operation,
2129 hash, sizeof( hash ), &hash_len ) );
2130 TEST_EQUAL( psa_hash_verify( &operation,
2131 valid_hash, sizeof( valid_hash ) ),
2132 PSA_ERROR_BAD_STATE );
2133 PSA_ASSERT( psa_hash_abort( &operation ) );
2134
2135 /* Call verify twice in a row. */
2136 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2137 PSA_ASSERT( psa_hash_verify( &operation,
2138 valid_hash, sizeof( valid_hash ) ) );
2139 TEST_EQUAL( psa_hash_verify( &operation,
2140 valid_hash, sizeof( valid_hash ) ),
2141 PSA_ERROR_BAD_STATE );
2142 PSA_ASSERT( psa_hash_abort( &operation ) );
2143
2144 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002145 TEST_EQUAL( psa_hash_finish( &operation,
2146 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002147 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002148 PSA_ASSERT( psa_hash_abort( &operation ) );
2149
2150 /* Call finish twice in a row. */
2151 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2152 PSA_ASSERT( psa_hash_finish( &operation,
2153 hash, sizeof( hash ), &hash_len ) );
2154 TEST_EQUAL( psa_hash_finish( &operation,
2155 hash, sizeof( hash ), &hash_len ),
2156 PSA_ERROR_BAD_STATE );
2157 PSA_ASSERT( psa_hash_abort( &operation ) );
2158
2159 /* Call finish after calling verify. */
2160 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2161 PSA_ASSERT( psa_hash_verify( &operation,
2162 valid_hash, sizeof( valid_hash ) ) );
2163 TEST_EQUAL( psa_hash_finish( &operation,
2164 hash, sizeof( hash ), &hash_len ),
2165 PSA_ERROR_BAD_STATE );
2166 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002167
2168exit:
2169 mbedtls_psa_crypto_free( );
2170}
2171/* END_CASE */
2172
itayzafrir27e69452018-11-01 14:26:34 +02002173/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2174void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002175{
2176 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002177 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2178 * appended to it */
2179 unsigned char hash[] = {
2180 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2181 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2182 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002183 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002184 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002185
Gilles Peskine8817f612018-12-18 00:18:46 +01002186 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002187
itayzafrir27e69452018-11-01 14:26:34 +02002188 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002189 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002190 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002191 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002192
itayzafrir27e69452018-11-01 14:26:34 +02002193 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002194 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002195 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002196 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002197
itayzafrir27e69452018-11-01 14:26:34 +02002198 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002199 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002200 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002201 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002202
itayzafrirec93d302018-10-18 18:01:10 +03002203exit:
2204 mbedtls_psa_crypto_free( );
2205}
2206/* END_CASE */
2207
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002208/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2209void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002210{
2211 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002212 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002213 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002214 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002215 size_t hash_len;
2216
Gilles Peskine8817f612018-12-18 00:18:46 +01002217 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002218
itayzafrir58028322018-10-25 10:22:01 +03002219 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002220 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002221 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002222 hash, expected_size - 1, &hash_len ),
2223 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002224
2225exit:
2226 mbedtls_psa_crypto_free( );
2227}
2228/* END_CASE */
2229
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002230/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2231void hash_clone_source_state( )
2232{
2233 psa_algorithm_t alg = PSA_ALG_SHA_256;
2234 unsigned char hash[PSA_HASH_MAX_SIZE];
2235 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2236 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2237 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2238 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2239 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2240 size_t hash_len;
2241
2242 PSA_ASSERT( psa_crypto_init( ) );
2243 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2244
2245 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2246 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2247 PSA_ASSERT( psa_hash_finish( &op_finished,
2248 hash, sizeof( hash ), &hash_len ) );
2249 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2250 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2251
2252 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2253 PSA_ERROR_BAD_STATE );
2254
2255 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2256 PSA_ASSERT( psa_hash_finish( &op_init,
2257 hash, sizeof( hash ), &hash_len ) );
2258 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2259 PSA_ASSERT( psa_hash_finish( &op_finished,
2260 hash, sizeof( hash ), &hash_len ) );
2261 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2262 PSA_ASSERT( psa_hash_finish( &op_aborted,
2263 hash, sizeof( hash ), &hash_len ) );
2264
2265exit:
2266 psa_hash_abort( &op_source );
2267 psa_hash_abort( &op_init );
2268 psa_hash_abort( &op_setup );
2269 psa_hash_abort( &op_finished );
2270 psa_hash_abort( &op_aborted );
2271 mbedtls_psa_crypto_free( );
2272}
2273/* END_CASE */
2274
2275/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2276void hash_clone_target_state( )
2277{
2278 psa_algorithm_t alg = PSA_ALG_SHA_256;
2279 unsigned char hash[PSA_HASH_MAX_SIZE];
2280 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2281 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2282 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2283 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2284 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2285 size_t hash_len;
2286
2287 PSA_ASSERT( psa_crypto_init( ) );
2288
2289 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2290 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2291 PSA_ASSERT( psa_hash_finish( &op_finished,
2292 hash, sizeof( hash ), &hash_len ) );
2293 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2294 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2295
2296 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2297 PSA_ASSERT( psa_hash_finish( &op_target,
2298 hash, sizeof( hash ), &hash_len ) );
2299
2300 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2301 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2302 PSA_ERROR_BAD_STATE );
2303 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2304 PSA_ERROR_BAD_STATE );
2305
2306exit:
2307 psa_hash_abort( &op_target );
2308 psa_hash_abort( &op_init );
2309 psa_hash_abort( &op_setup );
2310 psa_hash_abort( &op_finished );
2311 psa_hash_abort( &op_aborted );
2312 mbedtls_psa_crypto_free( );
2313}
2314/* END_CASE */
2315
itayzafrir58028322018-10-25 10:22:01 +03002316/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002317void mac_operation_init( )
2318{
Jaeden Amero252ef282019-02-15 14:05:35 +00002319 const uint8_t input[1] = { 0 };
2320
Jaeden Amero769ce272019-01-04 11:48:03 +00002321 /* Test each valid way of initializing the object, except for `= {0}`, as
2322 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2323 * though it's OK by the C standard. We could test for this, but we'd need
2324 * to supress the Clang warning for the test. */
2325 psa_mac_operation_t func = psa_mac_operation_init( );
2326 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2327 psa_mac_operation_t zero;
2328
2329 memset( &zero, 0, sizeof( zero ) );
2330
Jaeden Amero252ef282019-02-15 14:05:35 +00002331 /* A freshly-initialized MAC operation should not be usable. */
2332 TEST_EQUAL( psa_mac_update( &func,
2333 input, sizeof( input ) ),
2334 PSA_ERROR_BAD_STATE );
2335 TEST_EQUAL( psa_mac_update( &init,
2336 input, sizeof( input ) ),
2337 PSA_ERROR_BAD_STATE );
2338 TEST_EQUAL( psa_mac_update( &zero,
2339 input, sizeof( input ) ),
2340 PSA_ERROR_BAD_STATE );
2341
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002342 /* A default MAC operation should be abortable without error. */
2343 PSA_ASSERT( psa_mac_abort( &func ) );
2344 PSA_ASSERT( psa_mac_abort( &init ) );
2345 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002346}
2347/* END_CASE */
2348
2349/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002350void mac_setup( int key_type_arg,
2351 data_t *key,
2352 int alg_arg,
2353 int expected_status_arg )
2354{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002355 psa_key_type_t key_type = key_type_arg;
2356 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002357 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002358 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002359 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2360#if defined(KNOWN_SUPPORTED_MAC_ALG)
2361 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2362#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002363
Gilles Peskine8817f612018-12-18 00:18:46 +01002364 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002365
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002366 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2367 &operation, &status ) )
2368 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002369 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002370
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002371 /* The operation object should be reusable. */
2372#if defined(KNOWN_SUPPORTED_MAC_ALG)
2373 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2374 smoke_test_key_data,
2375 sizeof( smoke_test_key_data ),
2376 KNOWN_SUPPORTED_MAC_ALG,
2377 &operation, &status ) )
2378 goto exit;
2379 TEST_EQUAL( status, PSA_SUCCESS );
2380#endif
2381
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002382exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002383 mbedtls_psa_crypto_free( );
2384}
2385/* END_CASE */
2386
2387/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002388void mac_bad_order( )
2389{
2390 psa_key_handle_t handle = 0;
2391 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2392 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2393 const uint8_t key[] = {
2394 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2395 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2396 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002397 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002398 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2399 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2400 size_t sign_mac_length = 0;
2401 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2402 const uint8_t verify_mac[] = {
2403 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2404 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2405 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2406
2407 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002408 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
2409 psa_set_key_algorithm( &attributes, alg );
2410 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002411
Gilles Peskine73676cb2019-05-15 20:15:10 +02002412 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002413
Jaeden Amero252ef282019-02-15 14:05:35 +00002414 /* Call update without calling setup beforehand. */
2415 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2416 PSA_ERROR_BAD_STATE );
2417 PSA_ASSERT( psa_mac_abort( &operation ) );
2418
2419 /* Call sign finish without calling setup beforehand. */
2420 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2421 &sign_mac_length),
2422 PSA_ERROR_BAD_STATE );
2423 PSA_ASSERT( psa_mac_abort( &operation ) );
2424
2425 /* Call verify finish without calling setup beforehand. */
2426 TEST_EQUAL( psa_mac_verify_finish( &operation,
2427 verify_mac, sizeof( verify_mac ) ),
2428 PSA_ERROR_BAD_STATE );
2429 PSA_ASSERT( psa_mac_abort( &operation ) );
2430
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002431 /* Call setup twice in a row. */
2432 PSA_ASSERT( psa_mac_sign_setup( &operation,
2433 handle, alg ) );
2434 TEST_EQUAL( psa_mac_sign_setup( &operation,
2435 handle, alg ),
2436 PSA_ERROR_BAD_STATE );
2437 PSA_ASSERT( psa_mac_abort( &operation ) );
2438
Jaeden Amero252ef282019-02-15 14:05:35 +00002439 /* Call update after sign finish. */
2440 PSA_ASSERT( psa_mac_sign_setup( &operation,
2441 handle, alg ) );
2442 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2443 PSA_ASSERT( psa_mac_sign_finish( &operation,
2444 sign_mac, sizeof( sign_mac ),
2445 &sign_mac_length ) );
2446 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2447 PSA_ERROR_BAD_STATE );
2448 PSA_ASSERT( psa_mac_abort( &operation ) );
2449
2450 /* Call update after verify finish. */
2451 PSA_ASSERT( psa_mac_verify_setup( &operation,
2452 handle, alg ) );
2453 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2454 PSA_ASSERT( psa_mac_verify_finish( &operation,
2455 verify_mac, sizeof( verify_mac ) ) );
2456 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2457 PSA_ERROR_BAD_STATE );
2458 PSA_ASSERT( psa_mac_abort( &operation ) );
2459
2460 /* Call sign finish twice in a row. */
2461 PSA_ASSERT( psa_mac_sign_setup( &operation,
2462 handle, alg ) );
2463 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2464 PSA_ASSERT( psa_mac_sign_finish( &operation,
2465 sign_mac, sizeof( sign_mac ),
2466 &sign_mac_length ) );
2467 TEST_EQUAL( psa_mac_sign_finish( &operation,
2468 sign_mac, sizeof( sign_mac ),
2469 &sign_mac_length ),
2470 PSA_ERROR_BAD_STATE );
2471 PSA_ASSERT( psa_mac_abort( &operation ) );
2472
2473 /* Call verify finish twice in a row. */
2474 PSA_ASSERT( psa_mac_verify_setup( &operation,
2475 handle, alg ) );
2476 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2477 PSA_ASSERT( psa_mac_verify_finish( &operation,
2478 verify_mac, sizeof( verify_mac ) ) );
2479 TEST_EQUAL( psa_mac_verify_finish( &operation,
2480 verify_mac, sizeof( verify_mac ) ),
2481 PSA_ERROR_BAD_STATE );
2482 PSA_ASSERT( psa_mac_abort( &operation ) );
2483
2484 /* Setup sign but try verify. */
2485 PSA_ASSERT( psa_mac_sign_setup( &operation,
2486 handle, alg ) );
2487 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2488 TEST_EQUAL( psa_mac_verify_finish( &operation,
2489 verify_mac, sizeof( verify_mac ) ),
2490 PSA_ERROR_BAD_STATE );
2491 PSA_ASSERT( psa_mac_abort( &operation ) );
2492
2493 /* Setup verify but try sign. */
2494 PSA_ASSERT( psa_mac_verify_setup( &operation,
2495 handle, alg ) );
2496 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2497 TEST_EQUAL( psa_mac_sign_finish( &operation,
2498 sign_mac, sizeof( sign_mac ),
2499 &sign_mac_length ),
2500 PSA_ERROR_BAD_STATE );
2501 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002502
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002503exit:
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002504 mbedtls_psa_crypto_free( );
2505}
2506/* END_CASE */
2507
2508/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002509void mac_sign( int key_type_arg,
2510 data_t *key,
2511 int alg_arg,
2512 data_t *input,
2513 data_t *expected_mac )
2514{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002515 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002516 psa_key_type_t key_type = key_type_arg;
2517 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002518 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002519 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002520 /* Leave a little extra room in the output buffer. At the end of the
2521 * test, we'll check that the implementation didn't overwrite onto
2522 * this extra room. */
2523 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2524 size_t mac_buffer_size =
2525 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2526 size_t mac_length = 0;
2527
2528 memset( actual_mac, '+', sizeof( actual_mac ) );
2529 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2530 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2531
Gilles Peskine8817f612018-12-18 00:18:46 +01002532 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002533
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002534 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
2535 psa_set_key_algorithm( &attributes, alg );
2536 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002537
Gilles Peskine73676cb2019-05-15 20:15:10 +02002538 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002539
2540 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002541 PSA_ASSERT( psa_mac_sign_setup( &operation,
2542 handle, alg ) );
2543 PSA_ASSERT( psa_mac_update( &operation,
2544 input->x, input->len ) );
2545 PSA_ASSERT( psa_mac_sign_finish( &operation,
2546 actual_mac, mac_buffer_size,
2547 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002548
2549 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002550 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2551 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002552
2553 /* Verify that the end of the buffer is untouched. */
2554 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2555 sizeof( actual_mac ) - mac_length ) );
2556
2557exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002558 psa_destroy_key( handle );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002559 mbedtls_psa_crypto_free( );
2560}
2561/* END_CASE */
2562
2563/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002564void mac_verify( int key_type_arg,
2565 data_t *key,
2566 int alg_arg,
2567 data_t *input,
2568 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002569{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002570 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002571 psa_key_type_t key_type = key_type_arg;
2572 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002573 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002574 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002575
Gilles Peskine69c12672018-06-28 00:07:19 +02002576 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2577
Gilles Peskine8817f612018-12-18 00:18:46 +01002578 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002579
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002580 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
2581 psa_set_key_algorithm( &attributes, alg );
2582 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002583
Gilles Peskine73676cb2019-05-15 20:15:10 +02002584 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002585
Gilles Peskine8817f612018-12-18 00:18:46 +01002586 PSA_ASSERT( psa_mac_verify_setup( &operation,
2587 handle, alg ) );
2588 PSA_ASSERT( psa_destroy_key( handle ) );
2589 PSA_ASSERT( psa_mac_update( &operation,
2590 input->x, input->len ) );
2591 PSA_ASSERT( psa_mac_verify_finish( &operation,
2592 expected_mac->x,
2593 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002594
2595exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002596 psa_destroy_key( handle );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002597 mbedtls_psa_crypto_free( );
2598}
2599/* END_CASE */
2600
2601/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002602void cipher_operation_init( )
2603{
Jaeden Ameroab439972019-02-15 14:12:05 +00002604 const uint8_t input[1] = { 0 };
2605 unsigned char output[1] = { 0 };
2606 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002607 /* Test each valid way of initializing the object, except for `= {0}`, as
2608 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2609 * though it's OK by the C standard. We could test for this, but we'd need
2610 * to supress the Clang warning for the test. */
2611 psa_cipher_operation_t func = psa_cipher_operation_init( );
2612 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2613 psa_cipher_operation_t zero;
2614
2615 memset( &zero, 0, sizeof( zero ) );
2616
Jaeden Ameroab439972019-02-15 14:12:05 +00002617 /* A freshly-initialized cipher operation should not be usable. */
2618 TEST_EQUAL( psa_cipher_update( &func,
2619 input, sizeof( input ),
2620 output, sizeof( output ),
2621 &output_length ),
2622 PSA_ERROR_BAD_STATE );
2623 TEST_EQUAL( psa_cipher_update( &init,
2624 input, sizeof( input ),
2625 output, sizeof( output ),
2626 &output_length ),
2627 PSA_ERROR_BAD_STATE );
2628 TEST_EQUAL( psa_cipher_update( &zero,
2629 input, sizeof( input ),
2630 output, sizeof( output ),
2631 &output_length ),
2632 PSA_ERROR_BAD_STATE );
2633
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002634 /* A default cipher operation should be abortable without error. */
2635 PSA_ASSERT( psa_cipher_abort( &func ) );
2636 PSA_ASSERT( psa_cipher_abort( &init ) );
2637 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002638}
2639/* END_CASE */
2640
2641/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002642void cipher_setup( int key_type_arg,
2643 data_t *key,
2644 int alg_arg,
2645 int expected_status_arg )
2646{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002647 psa_key_type_t key_type = key_type_arg;
2648 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002649 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002650 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002651 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002652#if defined(KNOWN_SUPPORTED_MAC_ALG)
2653 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2654#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002655
Gilles Peskine8817f612018-12-18 00:18:46 +01002656 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002657
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002658 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2659 &operation, &status ) )
2660 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002661 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002662
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002663 /* The operation object should be reusable. */
2664#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2665 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2666 smoke_test_key_data,
2667 sizeof( smoke_test_key_data ),
2668 KNOWN_SUPPORTED_CIPHER_ALG,
2669 &operation, &status ) )
2670 goto exit;
2671 TEST_EQUAL( status, PSA_SUCCESS );
2672#endif
2673
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002674exit:
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002675 mbedtls_psa_crypto_free( );
2676}
2677/* END_CASE */
2678
2679/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002680void cipher_bad_order( )
2681{
2682 psa_key_handle_t handle = 0;
2683 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2684 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002685 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002686 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2687 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2688 const uint8_t key[] = {
2689 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2690 0xaa, 0xaa, 0xaa, 0xaa };
2691 const uint8_t text[] = {
2692 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2693 0xbb, 0xbb, 0xbb, 0xbb };
2694 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2695 size_t length = 0;
2696
2697 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002698 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2699 psa_set_key_algorithm( &attributes, alg );
2700 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02002701 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002702
2703
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002704 /* Call encrypt setup twice in a row. */
2705 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2706 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2707 PSA_ERROR_BAD_STATE );
2708 PSA_ASSERT( psa_cipher_abort( &operation ) );
2709
2710 /* Call decrypt setup twice in a row. */
2711 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2712 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2713 PSA_ERROR_BAD_STATE );
2714 PSA_ASSERT( psa_cipher_abort( &operation ) );
2715
Jaeden Ameroab439972019-02-15 14:12:05 +00002716 /* Generate an IV without calling setup beforehand. */
2717 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2718 buffer, sizeof( buffer ),
2719 &length ),
2720 PSA_ERROR_BAD_STATE );
2721 PSA_ASSERT( psa_cipher_abort( &operation ) );
2722
2723 /* Generate an IV twice in a row. */
2724 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2725 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2726 buffer, sizeof( buffer ),
2727 &length ) );
2728 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2729 buffer, sizeof( buffer ),
2730 &length ),
2731 PSA_ERROR_BAD_STATE );
2732 PSA_ASSERT( psa_cipher_abort( &operation ) );
2733
2734 /* Generate an IV after it's already set. */
2735 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2736 PSA_ASSERT( psa_cipher_set_iv( &operation,
2737 iv, sizeof( iv ) ) );
2738 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2739 buffer, sizeof( buffer ),
2740 &length ),
2741 PSA_ERROR_BAD_STATE );
2742 PSA_ASSERT( psa_cipher_abort( &operation ) );
2743
2744 /* Set an IV without calling setup beforehand. */
2745 TEST_EQUAL( psa_cipher_set_iv( &operation,
2746 iv, sizeof( iv ) ),
2747 PSA_ERROR_BAD_STATE );
2748 PSA_ASSERT( psa_cipher_abort( &operation ) );
2749
2750 /* Set an IV after it's already set. */
2751 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2752 PSA_ASSERT( psa_cipher_set_iv( &operation,
2753 iv, sizeof( iv ) ) );
2754 TEST_EQUAL( psa_cipher_set_iv( &operation,
2755 iv, sizeof( iv ) ),
2756 PSA_ERROR_BAD_STATE );
2757 PSA_ASSERT( psa_cipher_abort( &operation ) );
2758
2759 /* Set an IV after it's already generated. */
2760 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2761 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2762 buffer, sizeof( buffer ),
2763 &length ) );
2764 TEST_EQUAL( psa_cipher_set_iv( &operation,
2765 iv, sizeof( iv ) ),
2766 PSA_ERROR_BAD_STATE );
2767 PSA_ASSERT( psa_cipher_abort( &operation ) );
2768
2769 /* Call update without calling setup beforehand. */
2770 TEST_EQUAL( psa_cipher_update( &operation,
2771 text, sizeof( text ),
2772 buffer, sizeof( buffer ),
2773 &length ),
2774 PSA_ERROR_BAD_STATE );
2775 PSA_ASSERT( psa_cipher_abort( &operation ) );
2776
2777 /* Call update without an IV where an IV is required. */
2778 TEST_EQUAL( psa_cipher_update( &operation,
2779 text, sizeof( text ),
2780 buffer, sizeof( buffer ),
2781 &length ),
2782 PSA_ERROR_BAD_STATE );
2783 PSA_ASSERT( psa_cipher_abort( &operation ) );
2784
2785 /* Call update after finish. */
2786 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2787 PSA_ASSERT( psa_cipher_set_iv( &operation,
2788 iv, sizeof( iv ) ) );
2789 PSA_ASSERT( psa_cipher_finish( &operation,
2790 buffer, sizeof( buffer ), &length ) );
2791 TEST_EQUAL( psa_cipher_update( &operation,
2792 text, sizeof( text ),
2793 buffer, sizeof( buffer ),
2794 &length ),
2795 PSA_ERROR_BAD_STATE );
2796 PSA_ASSERT( psa_cipher_abort( &operation ) );
2797
2798 /* Call finish without calling setup beforehand. */
2799 TEST_EQUAL( psa_cipher_finish( &operation,
2800 buffer, sizeof( buffer ), &length ),
2801 PSA_ERROR_BAD_STATE );
2802 PSA_ASSERT( psa_cipher_abort( &operation ) );
2803
2804 /* Call finish without an IV where an IV is required. */
2805 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2806 /* Not calling update means we are encrypting an empty buffer, which is OK
2807 * for cipher modes with padding. */
2808 TEST_EQUAL( psa_cipher_finish( &operation,
2809 buffer, sizeof( buffer ), &length ),
2810 PSA_ERROR_BAD_STATE );
2811 PSA_ASSERT( psa_cipher_abort( &operation ) );
2812
2813 /* Call finish twice in a row. */
2814 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2815 PSA_ASSERT( psa_cipher_set_iv( &operation,
2816 iv, sizeof( iv ) ) );
2817 PSA_ASSERT( psa_cipher_finish( &operation,
2818 buffer, sizeof( buffer ), &length ) );
2819 TEST_EQUAL( psa_cipher_finish( &operation,
2820 buffer, sizeof( buffer ), &length ),
2821 PSA_ERROR_BAD_STATE );
2822 PSA_ASSERT( psa_cipher_abort( &operation ) );
2823
2824exit:
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002825 mbedtls_psa_crypto_free( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002826}
2827/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002828
Gilles Peskine50e586b2018-06-08 14:28:46 +02002829/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002830void cipher_encrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002831 data_t *key,
2832 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002833 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002834{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002835 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002836 psa_status_t status;
2837 psa_key_type_t key_type = key_type_arg;
2838 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002839 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002840 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002841 size_t iv_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002842 unsigned char *output = NULL;
2843 size_t output_buffer_size = 0;
2844 size_t function_output_length = 0;
2845 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002846 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002847 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002848
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002849 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2850 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002851
Gilles Peskine8817f612018-12-18 00:18:46 +01002852 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002853
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002854 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2855 psa_set_key_algorithm( &attributes, alg );
2856 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002857
Gilles Peskine73676cb2019-05-15 20:15:10 +02002858 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002859
Gilles Peskine8817f612018-12-18 00:18:46 +01002860 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2861 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002862
Gilles Peskine8817f612018-12-18 00:18:46 +01002863 PSA_ASSERT( psa_cipher_set_iv( &operation,
2864 iv, iv_size ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002865 output_buffer_size = ( (size_t) input->len +
2866 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002867 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002868
Gilles Peskine8817f612018-12-18 00:18:46 +01002869 PSA_ASSERT( psa_cipher_update( &operation,
2870 input->x, input->len,
2871 output, output_buffer_size,
2872 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002873 total_output_length += function_output_length;
2874 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002875 output + total_output_length,
2876 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002877 &function_output_length );
2878 total_output_length += function_output_length;
2879
Gilles Peskinefe11b722018-12-18 00:24:04 +01002880 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002881 if( expected_status == PSA_SUCCESS )
2882 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002883 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002884 ASSERT_COMPARE( expected_output->x, expected_output->len,
2885 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002886 }
2887
2888exit:
2889 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002890 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002891 mbedtls_psa_crypto_free( );
2892}
2893/* END_CASE */
2894
2895/* BEGIN_CASE */
2896void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
2897 data_t *key,
2898 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002899 int first_part_size_arg,
2900 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002901 data_t *expected_output )
2902{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002903 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002904 psa_key_type_t key_type = key_type_arg;
2905 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002906 size_t first_part_size = first_part_size_arg;
2907 size_t output1_length = output1_length_arg;
2908 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002909 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002910 size_t iv_size;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002911 unsigned char *output = NULL;
2912 size_t output_buffer_size = 0;
2913 size_t function_output_length = 0;
2914 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002915 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002916 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002917
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002918 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2919 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002920
Gilles Peskine8817f612018-12-18 00:18:46 +01002921 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002922
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002923 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2924 psa_set_key_algorithm( &attributes, alg );
2925 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002926
Gilles Peskine73676cb2019-05-15 20:15:10 +02002927 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002928
Gilles Peskine8817f612018-12-18 00:18:46 +01002929 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2930 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002931
Gilles Peskine8817f612018-12-18 00:18:46 +01002932 PSA_ASSERT( psa_cipher_set_iv( &operation,
2933 iv, sizeof( iv ) ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002934 output_buffer_size = ( (size_t) input->len +
2935 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002936 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002937
Gilles Peskinee0866522019-02-19 19:44:00 +01002938 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002939 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2940 output, output_buffer_size,
2941 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002942 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002943 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002944 PSA_ASSERT( psa_cipher_update( &operation,
2945 input->x + first_part_size,
2946 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002947 output + total_output_length,
2948 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002949 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002950 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002951 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002952 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002953 output + total_output_length,
2954 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002955 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002956 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002957 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002958
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002959 ASSERT_COMPARE( expected_output->x, expected_output->len,
2960 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002961
2962exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002963 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002964 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002965 mbedtls_psa_crypto_free( );
2966}
2967/* END_CASE */
2968
2969/* BEGIN_CASE */
2970void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002971 data_t *key,
2972 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002973 int first_part_size_arg,
2974 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002975 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002976{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002977 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002978
2979 psa_key_type_t key_type = key_type_arg;
2980 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002981 size_t first_part_size = first_part_size_arg;
2982 size_t output1_length = output1_length_arg;
2983 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002984 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002985 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002986 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002987 size_t output_buffer_size = 0;
2988 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002989 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002990 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002991 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002992
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002993 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
2994 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002995
Gilles Peskine8817f612018-12-18 00:18:46 +01002996 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002997
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002998 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2999 psa_set_key_algorithm( &attributes, alg );
3000 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003001
Gilles Peskine73676cb2019-05-15 20:15:10 +02003002 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003003
Gilles Peskine8817f612018-12-18 00:18:46 +01003004 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3005 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003006
Gilles Peskine8817f612018-12-18 00:18:46 +01003007 PSA_ASSERT( psa_cipher_set_iv( &operation,
3008 iv, sizeof( iv ) ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003009
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003010 output_buffer_size = ( (size_t) input->len +
3011 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003012 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003013
Gilles Peskinee0866522019-02-19 19:44:00 +01003014 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003015 PSA_ASSERT( psa_cipher_update( &operation,
3016 input->x, first_part_size,
3017 output, output_buffer_size,
3018 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003019 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003020 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003021 PSA_ASSERT( psa_cipher_update( &operation,
3022 input->x + first_part_size,
3023 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003024 output + total_output_length,
3025 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003026 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003027 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003028 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003029 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003030 output + total_output_length,
3031 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003032 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003033 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003034 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003035
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003036 ASSERT_COMPARE( expected_output->x, expected_output->len,
3037 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003038
3039exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003040 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003041 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003042 mbedtls_psa_crypto_free( );
3043}
3044/* END_CASE */
3045
Gilles Peskine50e586b2018-06-08 14:28:46 +02003046/* BEGIN_CASE */
3047void cipher_decrypt( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003048 data_t *key,
3049 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003050 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003051{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003052 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003053 psa_status_t status;
3054 psa_key_type_t key_type = key_type_arg;
3055 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003056 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003057 unsigned char iv[16] = {0};
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003058 size_t iv_size;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003059 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003060 size_t output_buffer_size = 0;
3061 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003062 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003063 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003064 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003065
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003066 iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
3067 memset( iv, 0x2a, iv_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003068
Gilles Peskine8817f612018-12-18 00:18:46 +01003069 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003070
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003071 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3072 psa_set_key_algorithm( &attributes, alg );
3073 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003074
Gilles Peskine73676cb2019-05-15 20:15:10 +02003075 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003076
Gilles Peskine8817f612018-12-18 00:18:46 +01003077 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3078 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003079
Gilles Peskine8817f612018-12-18 00:18:46 +01003080 PSA_ASSERT( psa_cipher_set_iv( &operation,
3081 iv, iv_size ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003082
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003083 output_buffer_size = ( (size_t) input->len +
3084 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003085 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003086
Gilles Peskine8817f612018-12-18 00:18:46 +01003087 PSA_ASSERT( psa_cipher_update( &operation,
3088 input->x, input->len,
3089 output, output_buffer_size,
3090 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003091 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003092 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003093 output + total_output_length,
3094 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003095 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003096 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003097 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003098
3099 if( expected_status == PSA_SUCCESS )
3100 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003101 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003102 ASSERT_COMPARE( expected_output->x, expected_output->len,
3103 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003104 }
3105
Gilles Peskine50e586b2018-06-08 14:28:46 +02003106exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003107 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003108 psa_destroy_key( handle );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003109 mbedtls_psa_crypto_free( );
3110}
3111/* END_CASE */
3112
Gilles Peskine50e586b2018-06-08 14:28:46 +02003113/* BEGIN_CASE */
3114void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003115 data_t *key,
3116 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003117{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003118 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003119 psa_key_type_t key_type = key_type_arg;
3120 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003121 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003122 size_t iv_size = 16;
3123 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003124 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003125 size_t output1_size = 0;
3126 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003127 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003128 size_t output2_size = 0;
3129 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003130 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003131 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3132 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003133 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003134
Gilles Peskine8817f612018-12-18 00:18:46 +01003135 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003136
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003137 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3138 psa_set_key_algorithm( &attributes, alg );
3139 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003140
Gilles Peskine73676cb2019-05-15 20:15:10 +02003141 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003142
Gilles Peskine8817f612018-12-18 00:18:46 +01003143 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3144 handle, alg ) );
3145 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3146 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003147
Gilles Peskine8817f612018-12-18 00:18:46 +01003148 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3149 iv, iv_size,
3150 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003151 output1_size = ( (size_t) input->len +
3152 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003153 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003154
Gilles Peskine8817f612018-12-18 00:18:46 +01003155 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3156 output1, output1_size,
3157 &output1_length ) );
3158 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003159 output1 + output1_length,
3160 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003161 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003162
Gilles Peskine048b7f02018-06-08 14:20:49 +02003163 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003164
Gilles Peskine8817f612018-12-18 00:18:46 +01003165 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003166
3167 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003168 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003169
Gilles Peskine8817f612018-12-18 00:18:46 +01003170 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3171 iv, iv_length ) );
3172 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3173 output2, output2_size,
3174 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003175 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003176 PSA_ASSERT( psa_cipher_finish( &operation2,
3177 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003178 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003179 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003180
Gilles Peskine048b7f02018-06-08 14:20:49 +02003181 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003182
Gilles Peskine8817f612018-12-18 00:18:46 +01003183 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003184
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003185 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003186
3187exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003188 mbedtls_free( output1 );
3189 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003190 psa_destroy_key( handle );
Moran Pekerded84402018-06-06 16:36:50 +03003191 mbedtls_psa_crypto_free( );
3192}
3193/* END_CASE */
3194
3195/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003196void cipher_verify_output_multipart( int alg_arg,
3197 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003198 data_t *key,
3199 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003200 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003201{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003202 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003203 psa_key_type_t key_type = key_type_arg;
3204 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003205 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003206 unsigned char iv[16] = {0};
3207 size_t iv_size = 16;
3208 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003209 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003210 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003211 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003212 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003213 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003214 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003215 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003216 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3217 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003218 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003219
Gilles Peskine8817f612018-12-18 00:18:46 +01003220 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003221
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003222 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3223 psa_set_key_algorithm( &attributes, alg );
3224 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003225
Gilles Peskine73676cb2019-05-15 20:15:10 +02003226 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003227
Gilles Peskine8817f612018-12-18 00:18:46 +01003228 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3229 handle, alg ) );
3230 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3231 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003232
Gilles Peskine8817f612018-12-18 00:18:46 +01003233 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3234 iv, iv_size,
3235 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003236 output1_buffer_size = ( (size_t) input->len +
3237 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003238 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003239
Gilles Peskinee0866522019-02-19 19:44:00 +01003240 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003241
Gilles Peskine8817f612018-12-18 00:18:46 +01003242 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3243 output1, output1_buffer_size,
3244 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003245 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003246
Gilles Peskine8817f612018-12-18 00:18:46 +01003247 PSA_ASSERT( psa_cipher_update( &operation1,
3248 input->x + first_part_size,
3249 input->len - first_part_size,
3250 output1, output1_buffer_size,
3251 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003252 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003253
Gilles Peskine8817f612018-12-18 00:18:46 +01003254 PSA_ASSERT( psa_cipher_finish( &operation1,
3255 output1 + output1_length,
3256 output1_buffer_size - output1_length,
3257 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003258 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003259
Gilles Peskine8817f612018-12-18 00:18:46 +01003260 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003261
Gilles Peskine048b7f02018-06-08 14:20:49 +02003262 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003263 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003264
Gilles Peskine8817f612018-12-18 00:18:46 +01003265 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3266 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003267
Gilles Peskine8817f612018-12-18 00:18:46 +01003268 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3269 output2, output2_buffer_size,
3270 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003271 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003272
Gilles Peskine8817f612018-12-18 00:18:46 +01003273 PSA_ASSERT( psa_cipher_update( &operation2,
3274 output1 + first_part_size,
3275 output1_length - first_part_size,
3276 output2, output2_buffer_size,
3277 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003278 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003279
Gilles Peskine8817f612018-12-18 00:18:46 +01003280 PSA_ASSERT( psa_cipher_finish( &operation2,
3281 output2 + output2_length,
3282 output2_buffer_size - output2_length,
3283 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003284 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003285
Gilles Peskine8817f612018-12-18 00:18:46 +01003286 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003287
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003288 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003289
3290exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003291 mbedtls_free( output1 );
3292 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003293 psa_destroy_key( handle );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003294 mbedtls_psa_crypto_free( );
3295}
3296/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003297
Gilles Peskine20035e32018-02-03 22:44:14 +01003298/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003299void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003300 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003301 data_t *nonce,
3302 data_t *additional_data,
3303 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003304 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003305{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003306 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003307 psa_key_type_t key_type = key_type_arg;
3308 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003309 unsigned char *output_data = NULL;
3310 size_t output_size = 0;
3311 size_t output_length = 0;
3312 unsigned char *output_data2 = NULL;
3313 size_t output_length2 = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003314 size_t tag_length = 16;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003315 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003316 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003317
Gilles Peskine4abf7412018-06-18 16:35:34 +02003318 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003319 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003320
Gilles Peskine8817f612018-12-18 00:18:46 +01003321 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003322
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003323 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3324 psa_set_key_algorithm( &attributes, alg );
3325 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003326
Gilles Peskine73676cb2019-05-15 20:15:10 +02003327 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003328
Gilles Peskinefe11b722018-12-18 00:24:04 +01003329 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3330 nonce->x, nonce->len,
3331 additional_data->x,
3332 additional_data->len,
3333 input_data->x, input_data->len,
3334 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003335 &output_length ),
3336 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003337
3338 if( PSA_SUCCESS == expected_result )
3339 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003340 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003341
Gilles Peskinefe11b722018-12-18 00:24:04 +01003342 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3343 nonce->x, nonce->len,
3344 additional_data->x,
3345 additional_data->len,
3346 output_data, output_length,
3347 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003348 &output_length2 ),
3349 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003350
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003351 ASSERT_COMPARE( input_data->x, input_data->len,
3352 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003353 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003354
Gilles Peskinea1cac842018-06-11 19:33:02 +02003355exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003356 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003357 mbedtls_free( output_data );
3358 mbedtls_free( output_data2 );
3359 mbedtls_psa_crypto_free( );
3360}
3361/* END_CASE */
3362
3363/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003364void aead_encrypt( int key_type_arg, data_t *key_data,
3365 int alg_arg,
3366 data_t *nonce,
3367 data_t *additional_data,
3368 data_t *input_data,
3369 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003370{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003371 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003372 psa_key_type_t key_type = key_type_arg;
3373 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003374 unsigned char *output_data = NULL;
3375 size_t output_size = 0;
3376 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003377 size_t tag_length = 16;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003378 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003379
Gilles Peskine4abf7412018-06-18 16:35:34 +02003380 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003381 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003382
Gilles Peskine8817f612018-12-18 00:18:46 +01003383 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003384
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003385 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3386 psa_set_key_algorithm( &attributes, alg );
3387 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003388
Gilles Peskine73676cb2019-05-15 20:15:10 +02003389 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003390
Gilles Peskine8817f612018-12-18 00:18:46 +01003391 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3392 nonce->x, nonce->len,
3393 additional_data->x, additional_data->len,
3394 input_data->x, input_data->len,
3395 output_data, output_size,
3396 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003397
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003398 ASSERT_COMPARE( expected_result->x, expected_result->len,
3399 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003400
Gilles Peskinea1cac842018-06-11 19:33:02 +02003401exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003402 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003403 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003404 mbedtls_psa_crypto_free( );
3405}
3406/* END_CASE */
3407
3408/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003409void aead_decrypt( int key_type_arg, data_t *key_data,
3410 int alg_arg,
3411 data_t *nonce,
3412 data_t *additional_data,
3413 data_t *input_data,
3414 data_t *expected_data,
3415 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003416{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003417 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003418 psa_key_type_t key_type = key_type_arg;
3419 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003420 unsigned char *output_data = NULL;
3421 size_t output_size = 0;
3422 size_t output_length = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003423 size_t tag_length = 16;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003424 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003425 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003426
Gilles Peskine4abf7412018-06-18 16:35:34 +02003427 output_size = input_data->len + tag_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003428 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003429
Gilles Peskine8817f612018-12-18 00:18:46 +01003430 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003431
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003432 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3433 psa_set_key_algorithm( &attributes, alg );
3434 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003435
Gilles Peskine73676cb2019-05-15 20:15:10 +02003436 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003437
Gilles Peskinefe11b722018-12-18 00:24:04 +01003438 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3439 nonce->x, nonce->len,
3440 additional_data->x,
3441 additional_data->len,
3442 input_data->x, input_data->len,
3443 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003444 &output_length ),
3445 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003446
Gilles Peskine2d277862018-06-18 15:41:12 +02003447 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003448 ASSERT_COMPARE( expected_data->x, expected_data->len,
3449 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003450
Gilles Peskinea1cac842018-06-11 19:33:02 +02003451exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003452 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003453 mbedtls_free( output_data );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003454 mbedtls_psa_crypto_free( );
3455}
3456/* END_CASE */
3457
3458/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003459void signature_size( int type_arg,
3460 int bits,
3461 int alg_arg,
3462 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003463{
3464 psa_key_type_t type = type_arg;
3465 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003466 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003467 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003468exit:
3469 ;
3470}
3471/* END_CASE */
3472
3473/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003474void sign_deterministic( int key_type_arg, data_t *key_data,
3475 int alg_arg, data_t *input_data,
3476 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003477{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003478 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003479 psa_key_type_t key_type = key_type_arg;
3480 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003481 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003482 unsigned char *signature = NULL;
3483 size_t signature_size;
3484 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003485 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003486
Gilles Peskine8817f612018-12-18 00:18:46 +01003487 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003488
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003489 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3490 psa_set_key_algorithm( &attributes, alg );
3491 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003492
Gilles Peskine73676cb2019-05-15 20:15:10 +02003493 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003494 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3495 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003496
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003497 /* Allocate a buffer which has the size advertized by the
3498 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003499 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3500 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003501 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003502 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003503 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003504
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003505 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003506 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3507 input_data->x, input_data->len,
3508 signature, signature_size,
3509 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003510 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003511 ASSERT_COMPARE( output_data->x, output_data->len,
3512 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003513
3514exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003515 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003516 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003517 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +01003518 mbedtls_psa_crypto_free( );
3519}
3520/* END_CASE */
3521
3522/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003523void sign_fail( int key_type_arg, data_t *key_data,
3524 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003525 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003526{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003527 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003528 psa_key_type_t key_type = key_type_arg;
3529 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003530 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003531 psa_status_t actual_status;
3532 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003533 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003534 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003535 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003536
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003537 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003538
Gilles Peskine8817f612018-12-18 00:18:46 +01003539 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003540
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003541 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3542 psa_set_key_algorithm( &attributes, alg );
3543 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003544
Gilles Peskine73676cb2019-05-15 20:15:10 +02003545 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003546
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003547 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003548 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003549 signature, signature_size,
3550 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003551 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003552 /* The value of *signature_length is unspecified on error, but
3553 * whatever it is, it should be less than signature_size, so that
3554 * if the caller tries to read *signature_length bytes without
3555 * checking the error code then they don't overflow a buffer. */
3556 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003557
3558exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003559 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003560 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003561 mbedtls_free( signature );
3562 mbedtls_psa_crypto_free( );
3563}
3564/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003565
3566/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003567void sign_verify( int key_type_arg, data_t *key_data,
3568 int alg_arg, data_t *input_data )
3569{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003570 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003571 psa_key_type_t key_type = key_type_arg;
3572 psa_algorithm_t alg = alg_arg;
3573 size_t key_bits;
3574 unsigned char *signature = NULL;
3575 size_t signature_size;
3576 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003577 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003578
Gilles Peskine8817f612018-12-18 00:18:46 +01003579 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003580
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003581 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
3582 psa_set_key_algorithm( &attributes, alg );
3583 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003584
Gilles Peskine73676cb2019-05-15 20:15:10 +02003585 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003586 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3587 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003588
3589 /* Allocate a buffer which has the size advertized by the
3590 * library. */
3591 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3592 key_bits, alg );
3593 TEST_ASSERT( signature_size != 0 );
3594 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003595 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003596
3597 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003598 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3599 input_data->x, input_data->len,
3600 signature, signature_size,
3601 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003602 /* Check that the signature length looks sensible. */
3603 TEST_ASSERT( signature_length <= signature_size );
3604 TEST_ASSERT( signature_length > 0 );
3605
3606 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003607 PSA_ASSERT( psa_asymmetric_verify(
3608 handle, alg,
3609 input_data->x, input_data->len,
3610 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003611
3612 if( input_data->len != 0 )
3613 {
3614 /* Flip a bit in the input and verify that the signature is now
3615 * detected as invalid. Flip a bit at the beginning, not at the end,
3616 * because ECDSA may ignore the last few bits of the input. */
3617 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003618 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3619 input_data->x, input_data->len,
3620 signature, signature_length ),
3621 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003622 }
3623
3624exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003625 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003626 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003627 mbedtls_free( signature );
3628 mbedtls_psa_crypto_free( );
3629}
3630/* END_CASE */
3631
3632/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003633void asymmetric_verify( int key_type_arg, data_t *key_data,
3634 int alg_arg, data_t *hash_data,
3635 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003636{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003637 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003638 psa_key_type_t key_type = key_type_arg;
3639 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003640 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003641
Gilles Peskine69c12672018-06-28 00:07:19 +02003642 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3643
Gilles Peskine8817f612018-12-18 00:18:46 +01003644 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003645
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003646 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3647 psa_set_key_algorithm( &attributes, alg );
3648 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003649
Gilles Peskine73676cb2019-05-15 20:15:10 +02003650 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03003651
Gilles Peskine8817f612018-12-18 00:18:46 +01003652 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3653 hash_data->x, hash_data->len,
3654 signature_data->x,
3655 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003656exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003657 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003658 psa_destroy_key( handle );
itayzafrir5c753392018-05-08 11:18:38 +03003659 mbedtls_psa_crypto_free( );
3660}
3661/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003662
3663/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003664void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3665 int alg_arg, data_t *hash_data,
3666 data_t *signature_data,
3667 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003668{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003669 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003670 psa_key_type_t key_type = key_type_arg;
3671 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003672 psa_status_t actual_status;
3673 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003674 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003675
Gilles Peskine8817f612018-12-18 00:18:46 +01003676 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003677
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003678 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3679 psa_set_key_algorithm( &attributes, alg );
3680 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003681
Gilles Peskine73676cb2019-05-15 20:15:10 +02003682 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003683
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003684 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003685 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003686 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003687 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003688
Gilles Peskinefe11b722018-12-18 00:24:04 +01003689 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003690
3691exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003692 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003693 psa_destroy_key( handle );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003694 mbedtls_psa_crypto_free( );
3695}
3696/* END_CASE */
3697
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003698/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003699void asymmetric_encrypt( int key_type_arg,
3700 data_t *key_data,
3701 int alg_arg,
3702 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003703 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003704 int expected_output_length_arg,
3705 int expected_status_arg )
3706{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003707 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003708 psa_key_type_t key_type = key_type_arg;
3709 psa_algorithm_t alg = alg_arg;
3710 size_t expected_output_length = expected_output_length_arg;
3711 size_t key_bits;
3712 unsigned char *output = NULL;
3713 size_t output_size;
3714 size_t output_length = ~0;
3715 psa_status_t actual_status;
3716 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003717 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003718
Gilles Peskine8817f612018-12-18 00:18:46 +01003719 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003720
Gilles Peskine656896e2018-06-29 19:12:28 +02003721 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003722 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3723 psa_set_key_algorithm( &attributes, alg );
3724 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02003725 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003726
3727 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003728 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3729 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02003730 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003731 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003732
3733 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003734 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003735 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003736 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003737 output, output_size,
3738 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003739 TEST_EQUAL( actual_status, expected_status );
3740 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003741
Gilles Peskine68428122018-06-30 18:42:41 +02003742 /* If the label is empty, the test framework puts a non-null pointer
3743 * in label->x. Test that a null pointer works as well. */
3744 if( label->len == 0 )
3745 {
3746 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003747 if( output_size != 0 )
3748 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003749 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003750 input_data->x, input_data->len,
3751 NULL, label->len,
3752 output, output_size,
3753 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003754 TEST_EQUAL( actual_status, expected_status );
3755 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003756 }
3757
Gilles Peskine656896e2018-06-29 19:12:28 +02003758exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003759 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003760 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003761 mbedtls_free( output );
3762 mbedtls_psa_crypto_free( );
3763}
3764/* END_CASE */
3765
3766/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003767void asymmetric_encrypt_decrypt( int key_type_arg,
3768 data_t *key_data,
3769 int alg_arg,
3770 data_t *input_data,
3771 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003772{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003773 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003774 psa_key_type_t key_type = key_type_arg;
3775 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003776 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003777 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003778 size_t output_size;
3779 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003780 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003781 size_t output2_size;
3782 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003783 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003784
Gilles Peskine8817f612018-12-18 00:18:46 +01003785 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003786
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003787 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3788 psa_set_key_algorithm( &attributes, alg );
3789 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003790
Gilles Peskine73676cb2019-05-15 20:15:10 +02003791 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003792
3793 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003794 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3795 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003796 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003797 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003798 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003799 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003800
Gilles Peskineeebd7382018-06-08 18:11:54 +02003801 /* We test encryption by checking that encrypt-then-decrypt gives back
3802 * the original plaintext because of the non-optional random
3803 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003804 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3805 input_data->x, input_data->len,
3806 label->x, label->len,
3807 output, output_size,
3808 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003809 /* We don't know what ciphertext length to expect, but check that
3810 * it looks sensible. */
3811 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003812
Gilles Peskine8817f612018-12-18 00:18:46 +01003813 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3814 output, output_length,
3815 label->x, label->len,
3816 output2, output2_size,
3817 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003818 ASSERT_COMPARE( input_data->x, input_data->len,
3819 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003820
3821exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003822 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003823 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003824 mbedtls_free( output );
3825 mbedtls_free( output2 );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003826 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003827}
3828/* END_CASE */
3829
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003830/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003831void asymmetric_decrypt( int key_type_arg,
3832 data_t *key_data,
3833 int alg_arg,
3834 data_t *input_data,
3835 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003836 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003837{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003838 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003839 psa_key_type_t key_type = key_type_arg;
3840 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003841 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003842 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003843 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003844 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003845
Jaeden Amero412654a2019-02-06 12:57:46 +00003846 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003847 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003848
Gilles Peskine8817f612018-12-18 00:18:46 +01003849 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003850
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003851 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3852 psa_set_key_algorithm( &attributes, alg );
3853 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003854
Gilles Peskine73676cb2019-05-15 20:15:10 +02003855 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003856
Gilles Peskine8817f612018-12-18 00:18:46 +01003857 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3858 input_data->x, input_data->len,
3859 label->x, label->len,
3860 output,
3861 output_size,
3862 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003863 ASSERT_COMPARE( expected_data->x, expected_data->len,
3864 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003865
Gilles Peskine68428122018-06-30 18:42:41 +02003866 /* If the label is empty, the test framework puts a non-null pointer
3867 * in label->x. Test that a null pointer works as well. */
3868 if( label->len == 0 )
3869 {
3870 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003871 if( output_size != 0 )
3872 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003873 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3874 input_data->x, input_data->len,
3875 NULL, label->len,
3876 output,
3877 output_size,
3878 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003879 ASSERT_COMPARE( expected_data->x, expected_data->len,
3880 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003881 }
3882
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003883exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003884 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003885 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003886 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003887 mbedtls_psa_crypto_free( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003888}
3889/* END_CASE */
3890
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003891/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003892void asymmetric_decrypt_fail( int key_type_arg,
3893 data_t *key_data,
3894 int alg_arg,
3895 data_t *input_data,
3896 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003897 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003898 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003899{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003900 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003901 psa_key_type_t key_type = key_type_arg;
3902 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003903 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003904 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003905 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003906 psa_status_t actual_status;
3907 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003908 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003909
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003910 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003911
Gilles Peskine8817f612018-12-18 00:18:46 +01003912 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003913
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003914 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3915 psa_set_key_algorithm( &attributes, alg );
3916 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003917
Gilles Peskine73676cb2019-05-15 20:15:10 +02003918 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003919
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003920 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003921 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003922 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003923 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003924 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003925 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003926 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003927
Gilles Peskine68428122018-06-30 18:42:41 +02003928 /* If the label is empty, the test framework puts a non-null pointer
3929 * in label->x. Test that a null pointer works as well. */
3930 if( label->len == 0 )
3931 {
3932 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003933 if( output_size != 0 )
3934 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003935 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003936 input_data->x, input_data->len,
3937 NULL, label->len,
3938 output, output_size,
3939 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003940 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003941 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003942 }
3943
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003944exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003945 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003946 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003947 mbedtls_free( output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003948 mbedtls_psa_crypto_free( );
3949}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003950/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003951
3952/* BEGIN_CASE */
Jaeden Amerod94d6712019-01-04 14:11:48 +00003953void crypto_generator_init( )
3954{
3955 /* Test each valid way of initializing the object, except for `= {0}`, as
3956 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3957 * though it's OK by the C standard. We could test for this, but we'd need
3958 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003959 size_t capacity;
Jaeden Amerod94d6712019-01-04 14:11:48 +00003960 psa_crypto_generator_t func = psa_crypto_generator_init( );
3961 psa_crypto_generator_t init = PSA_CRYPTO_GENERATOR_INIT;
3962 psa_crypto_generator_t zero;
3963
3964 memset( &zero, 0, sizeof( zero ) );
3965
Jaeden Amerocf2010c2019-02-15 13:05:49 +00003966 /* A default generator should not be able to report its capacity. */
3967 TEST_EQUAL( psa_get_generator_capacity( &func, &capacity ),
3968 PSA_ERROR_BAD_STATE );
3969 TEST_EQUAL( psa_get_generator_capacity( &init, &capacity ),
3970 PSA_ERROR_BAD_STATE );
3971 TEST_EQUAL( psa_get_generator_capacity( &zero, &capacity ),
3972 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003973
3974 /* A default generator should be abortable without error. */
3975 PSA_ASSERT( psa_generator_abort(&func) );
3976 PSA_ASSERT( psa_generator_abort(&init) );
3977 PSA_ASSERT( psa_generator_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00003978}
3979/* END_CASE */
3980
3981/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02003982void derive_setup( int key_type_arg,
3983 data_t *key_data,
3984 int alg_arg,
3985 data_t *salt,
3986 data_t *label,
3987 int requested_capacity_arg,
3988 int expected_status_arg )
3989{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003990 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003991 size_t key_type = key_type_arg;
3992 psa_algorithm_t alg = alg_arg;
3993 size_t requested_capacity = requested_capacity_arg;
3994 psa_status_t expected_status = expected_status_arg;
3995 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003996 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02003997
Gilles Peskine8817f612018-12-18 00:18:46 +01003998 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02003999
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004000 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4001 psa_set_key_algorithm( &attributes, alg );
4002 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004003
Gilles Peskine73676cb2019-05-15 20:15:10 +02004004 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004005
Gilles Peskinefe11b722018-12-18 00:24:04 +01004006 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4007 salt->x, salt->len,
4008 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004009 requested_capacity ),
4010 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004011
4012exit:
4013 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004014 psa_destroy_key( handle );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004015 mbedtls_psa_crypto_free( );
4016}
4017/* END_CASE */
4018
4019/* BEGIN_CASE */
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004020void test_derive_invalid_generator_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004021{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004022 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004023 size_t key_type = PSA_KEY_TYPE_DERIVE;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004024 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004025 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004026 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004027 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004028 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4029 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4030 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004031 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004032
Gilles Peskine8817f612018-12-18 00:18:46 +01004033 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004034
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004035 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4036 psa_set_key_algorithm( &attributes, alg );
4037 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004038
Gilles Peskine73676cb2019-05-15 20:15:10 +02004039 PSA_ASSERT( psa_import_key( &attributes,
4040 key_data, sizeof( key_data ),
4041 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004042
4043 /* valid key derivation */
Gilles Peskine8817f612018-12-18 00:18:46 +01004044 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4045 NULL, 0,
4046 NULL, 0,
4047 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004048
4049 /* state of generator shouldn't allow additional generation */
Gilles Peskinefe11b722018-12-18 00:24:04 +01004050 TEST_EQUAL( psa_key_derivation( &generator, handle, alg,
4051 NULL, 0,
4052 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004053 capacity ),
4054 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004055
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004056 PSA_ASSERT( psa_generator_read( &generator, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004057
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004058 TEST_EQUAL( psa_generator_read( &generator, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004059 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004060
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004061exit:
4062 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004063 psa_destroy_key( handle );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004064 mbedtls_psa_crypto_free( );
4065}
4066/* END_CASE */
4067
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004068/* BEGIN_CASE */
4069void test_derive_invalid_generator_tests( )
4070{
4071 uint8_t output_buffer[16];
4072 size_t buffer_size = 16;
4073 size_t capacity = 0;
4074 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4075
Nir Sonnenschein50789302018-10-31 12:16:38 +02004076 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004077 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004078
4079 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004080 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004081
Gilles Peskine8817f612018-12-18 00:18:46 +01004082 PSA_ASSERT( psa_generator_abort( &generator ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004083
Nir Sonnenschein50789302018-10-31 12:16:38 +02004084 TEST_ASSERT( psa_generator_read( &generator, output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004085 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004086
Nir Sonnenschein50789302018-10-31 12:16:38 +02004087 TEST_ASSERT( psa_get_generator_capacity( &generator, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004088 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004089
4090exit:
4091 psa_generator_abort( &generator );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004092}
4093/* END_CASE */
4094
4095/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004096void derive_output( int alg_arg,
4097 data_t *key_data,
4098 data_t *salt,
4099 data_t *label,
4100 int requested_capacity_arg,
4101 data_t *expected_output1,
4102 data_t *expected_output2 )
4103{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004104 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004105 psa_algorithm_t alg = alg_arg;
4106 size_t requested_capacity = requested_capacity_arg;
4107 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4108 uint8_t *expected_outputs[2] =
4109 {expected_output1->x, expected_output2->x};
4110 size_t output_sizes[2] =
4111 {expected_output1->len, expected_output2->len};
4112 size_t output_buffer_size = 0;
4113 uint8_t *output_buffer = NULL;
4114 size_t expected_capacity;
4115 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004116 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004117 psa_status_t status;
4118 unsigned i;
4119
4120 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4121 {
4122 if( output_sizes[i] > output_buffer_size )
4123 output_buffer_size = output_sizes[i];
4124 if( output_sizes[i] == 0 )
4125 expected_outputs[i] = NULL;
4126 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004127 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004128 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004129
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004130 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4131 psa_set_key_algorithm( &attributes, alg );
4132 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004133
Gilles Peskine73676cb2019-05-15 20:15:10 +02004134 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004135
4136 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004137 if( PSA_ALG_IS_HKDF( alg ) )
4138 {
4139 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4140 PSA_ASSERT( psa_set_generator_capacity( &generator,
4141 requested_capacity ) );
4142 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4143 PSA_KDF_STEP_SALT,
4144 salt->x, salt->len ) );
4145 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4146 PSA_KDF_STEP_SECRET,
4147 handle ) );
4148 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4149 PSA_KDF_STEP_INFO,
4150 label->x, label->len ) );
4151 }
4152 else
4153 {
4154 // legacy
4155 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4156 salt->x, salt->len,
4157 label->x, label->len,
4158 requested_capacity ) );
4159 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004160 PSA_ASSERT( psa_get_generator_capacity( &generator,
4161 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004162 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004163 expected_capacity = requested_capacity;
4164
4165 /* Expansion phase. */
4166 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4167 {
4168 /* Read some bytes. */
4169 status = psa_generator_read( &generator,
4170 output_buffer, output_sizes[i] );
4171 if( expected_capacity == 0 && output_sizes[i] == 0 )
4172 {
4173 /* Reading 0 bytes when 0 bytes are available can go either way. */
4174 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004175 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004176 continue;
4177 }
4178 else if( expected_capacity == 0 ||
4179 output_sizes[i] > expected_capacity )
4180 {
4181 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004182 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004183 expected_capacity = 0;
4184 continue;
4185 }
4186 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004187 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004188 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004189 ASSERT_COMPARE( output_buffer, output_sizes[i],
4190 expected_outputs[i], output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004191 /* Check the generator status. */
4192 expected_capacity -= output_sizes[i];
Gilles Peskine8817f612018-12-18 00:18:46 +01004193 PSA_ASSERT( psa_get_generator_capacity( &generator,
4194 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004195 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004196 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004197 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004198
4199exit:
4200 mbedtls_free( output_buffer );
4201 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004202 psa_destroy_key( handle );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004203 mbedtls_psa_crypto_free( );
4204}
4205/* END_CASE */
4206
4207/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004208void derive_full( int alg_arg,
4209 data_t *key_data,
4210 data_t *salt,
4211 data_t *label,
4212 int requested_capacity_arg )
4213{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004214 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004215 psa_algorithm_t alg = alg_arg;
4216 size_t requested_capacity = requested_capacity_arg;
4217 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
4218 unsigned char output_buffer[16];
4219 size_t expected_capacity = requested_capacity;
4220 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004221 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004222
Gilles Peskine8817f612018-12-18 00:18:46 +01004223 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004224
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004225 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4226 psa_set_key_algorithm( &attributes, alg );
4227 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004228
Gilles Peskine73676cb2019-05-15 20:15:10 +02004229 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004230
4231 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004232 if( PSA_ALG_IS_HKDF( alg ) )
4233 {
4234 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4235 PSA_ASSERT( psa_set_generator_capacity( &generator,
4236 requested_capacity ) );
4237 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4238 PSA_KDF_STEP_SALT,
4239 salt->x, salt->len ) );
4240 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4241 PSA_KDF_STEP_SECRET,
4242 handle ) );
4243 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4244 PSA_KDF_STEP_INFO,
4245 label->x, label->len ) );
4246 }
4247 else
4248 {
4249 // legacy
4250 PSA_ASSERT( psa_key_derivation( &generator, handle, alg,
4251 salt->x, salt->len,
4252 label->x, label->len,
4253 requested_capacity ) );
4254 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004255 PSA_ASSERT( psa_get_generator_capacity( &generator,
4256 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004257 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004258
4259 /* Expansion phase. */
4260 while( current_capacity > 0 )
4261 {
4262 size_t read_size = sizeof( output_buffer );
4263 if( read_size > current_capacity )
4264 read_size = current_capacity;
Gilles Peskine8817f612018-12-18 00:18:46 +01004265 PSA_ASSERT( psa_generator_read( &generator,
4266 output_buffer,
4267 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004268 expected_capacity -= read_size;
Gilles Peskine8817f612018-12-18 00:18:46 +01004269 PSA_ASSERT( psa_get_generator_capacity( &generator,
4270 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004271 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004272 }
4273
4274 /* Check that the generator refuses to go over capacity. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004275 TEST_EQUAL( psa_generator_read( &generator, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004276 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004277
Gilles Peskine8817f612018-12-18 00:18:46 +01004278 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004279
4280exit:
4281 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004282 psa_destroy_key( handle );
Gilles Peskined54931c2018-07-17 21:06:59 +02004283 mbedtls_psa_crypto_free( );
4284}
4285/* END_CASE */
4286
4287/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004288void derive_key_exercise( int alg_arg,
4289 data_t *key_data,
4290 data_t *salt,
4291 data_t *label,
4292 int derived_type_arg,
4293 int derived_bits_arg,
4294 int derived_usage_arg,
4295 int derived_alg_arg )
4296{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004297 psa_key_handle_t base_handle = 0;
4298 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004299 psa_algorithm_t alg = alg_arg;
4300 psa_key_type_t derived_type = derived_type_arg;
4301 size_t derived_bits = derived_bits_arg;
4302 psa_key_usage_t derived_usage = derived_usage_arg;
4303 psa_algorithm_t derived_alg = derived_alg_arg;
4304 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
4305 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004306 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004307 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004308
Gilles Peskine8817f612018-12-18 00:18:46 +01004309 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004310
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004311 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4312 psa_set_key_algorithm( &attributes, alg );
4313 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine73676cb2019-05-15 20:15:10 +02004314 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004315
4316 /* Derive a key. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004317 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4318 salt->x, salt->len,
4319 label->x, label->len,
4320 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004321 psa_set_key_usage_flags( &attributes, derived_usage );
4322 psa_set_key_algorithm( &attributes, derived_alg );
4323 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004324 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine98dd7792019-05-15 19:43:49 +02004325 PSA_ASSERT( psa_generate_derived_key( &attributes, &generator,
4326 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004327
4328 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004329 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4330 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4331 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004332
4333 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004334 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004335 goto exit;
4336
4337exit:
4338 psa_generator_abort( &generator );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004339 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004340 psa_destroy_key( base_handle );
4341 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004342 mbedtls_psa_crypto_free( );
4343}
4344/* END_CASE */
4345
4346/* BEGIN_CASE */
4347void derive_key_export( int alg_arg,
4348 data_t *key_data,
4349 data_t *salt,
4350 data_t *label,
4351 int bytes1_arg,
4352 int bytes2_arg )
4353{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004354 psa_key_handle_t base_handle = 0;
4355 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004356 psa_algorithm_t alg = alg_arg;
4357 size_t bytes1 = bytes1_arg;
4358 size_t bytes2 = bytes2_arg;
4359 size_t capacity = bytes1 + bytes2;
4360 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004361 uint8_t *output_buffer = NULL;
4362 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004363 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4364 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004365 size_t length;
4366
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004367 ASSERT_ALLOC( output_buffer, capacity );
4368 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004369 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004370
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004371 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4372 psa_set_key_algorithm( &base_attributes, alg );
4373 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine73676cb2019-05-15 20:15:10 +02004374 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004375
4376 /* Derive some material and output it. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004377 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4378 salt->x, salt->len,
4379 label->x, label->len,
4380 capacity ) );
4381 PSA_ASSERT( psa_generator_read( &generator,
4382 output_buffer,
4383 capacity ) );
4384 PSA_ASSERT( psa_generator_abort( &generator ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004385
4386 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004387 PSA_ASSERT( psa_key_derivation( &generator, base_handle, alg,
4388 salt->x, salt->len,
4389 label->x, label->len,
4390 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004391 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4392 psa_set_key_algorithm( &derived_attributes, 0 );
4393 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004394 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine98dd7792019-05-15 19:43:49 +02004395 PSA_ASSERT( psa_generate_derived_key( &derived_attributes, &generator,
4396 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004397 PSA_ASSERT( psa_export_key( derived_handle,
4398 export_buffer, bytes1,
4399 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004400 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004401 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004402 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine98dd7792019-05-15 19:43:49 +02004403 PSA_ASSERT( psa_generate_derived_key( &derived_attributes, &generator,
4404 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004405 PSA_ASSERT( psa_export_key( derived_handle,
4406 export_buffer + bytes1, bytes2,
4407 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004408 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004409
4410 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004411 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4412 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004413
4414exit:
4415 mbedtls_free( output_buffer );
4416 mbedtls_free( export_buffer );
4417 psa_generator_abort( &generator );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004418 psa_destroy_key( base_handle );
4419 psa_destroy_key( derived_handle );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004420 mbedtls_psa_crypto_free( );
4421}
4422/* END_CASE */
4423
4424/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004425void key_agreement_setup( int alg_arg,
4426 int our_key_type_arg, data_t *our_key_data,
4427 data_t *peer_key_data,
4428 int expected_status_arg )
4429{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004430 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004431 psa_algorithm_t alg = alg_arg;
4432 psa_key_type_t our_key_type = our_key_type_arg;
4433 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004434 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004435 psa_status_t expected_status = expected_status_arg;
4436 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004437
Gilles Peskine8817f612018-12-18 00:18:46 +01004438 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004439
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004440 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4441 psa_set_key_algorithm( &attributes, alg );
4442 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02004443 PSA_ASSERT( psa_import_key( &attributes, our_key_data->x, our_key_data->len, &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004444
Gilles Peskine77f40d82019-04-11 21:27:06 +02004445 /* The tests currently include inputs that should fail at either step.
4446 * Test cases that fail at the setup step should be changed to call
4447 * key_derivation_setup instead, and this function should be renamed
4448 * to key_agreement_fail. */
4449 status = psa_key_derivation_setup( &generator, alg );
4450 if( status == PSA_SUCCESS )
4451 {
4452 TEST_EQUAL( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
4453 our_key,
4454 peer_key_data->x, peer_key_data->len ),
4455 expected_status );
4456 }
4457 else
4458 {
4459 TEST_ASSERT( status == expected_status );
4460 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004461
4462exit:
4463 psa_generator_abort( &generator );
4464 psa_destroy_key( our_key );
4465 mbedtls_psa_crypto_free( );
4466}
4467/* END_CASE */
4468
4469/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004470void raw_key_agreement( int alg_arg,
4471 int our_key_type_arg, data_t *our_key_data,
4472 data_t *peer_key_data,
4473 data_t *expected_output )
4474{
4475 psa_key_handle_t our_key = 0;
4476 psa_algorithm_t alg = alg_arg;
4477 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004478 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004479 unsigned char *output = NULL;
4480 size_t output_length = ~0;
4481
4482 ASSERT_ALLOC( output, expected_output->len );
4483 PSA_ASSERT( psa_crypto_init( ) );
4484
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004485 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4486 psa_set_key_algorithm( &attributes, alg );
4487 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02004488 PSA_ASSERT( psa_import_key( &attributes, our_key_data->x, our_key_data->len, &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004489
4490 PSA_ASSERT( psa_key_agreement_raw_shared_secret(
4491 alg, our_key,
4492 peer_key_data->x, peer_key_data->len,
4493 output, expected_output->len, &output_length ) );
4494 ASSERT_COMPARE( output, output_length,
4495 expected_output->x, expected_output->len );
4496
4497exit:
4498 mbedtls_free( output );
4499 psa_destroy_key( our_key );
4500 mbedtls_psa_crypto_free( );
4501}
4502/* END_CASE */
4503
4504/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004505void key_agreement_capacity( int alg_arg,
4506 int our_key_type_arg, data_t *our_key_data,
4507 data_t *peer_key_data,
4508 int expected_capacity_arg )
4509{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004510 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004511 psa_algorithm_t alg = alg_arg;
4512 psa_key_type_t our_key_type = our_key_type_arg;
4513 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004514 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004515 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004516 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004517
Gilles Peskine8817f612018-12-18 00:18:46 +01004518 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004519
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004520 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4521 psa_set_key_algorithm( &attributes, alg );
4522 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02004523 PSA_ASSERT( psa_import_key( &attributes, our_key_data->x, our_key_data->len, &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004524
Gilles Peskine969c5d62019-01-16 15:53:06 +01004525 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4526 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004527 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004528 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004529 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4530 {
4531 /* The test data is for info="" */
4532 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4533 PSA_KDF_STEP_INFO,
4534 NULL, 0 ) );
4535 }
Gilles Peskine59685592018-09-18 12:11:34 +02004536
Gilles Peskinebf491972018-10-25 22:36:12 +02004537 /* Test the advertized capacity. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004538 PSA_ASSERT( psa_get_generator_capacity(
4539 &generator, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004540 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004541
Gilles Peskinebf491972018-10-25 22:36:12 +02004542 /* Test the actual capacity by reading the output. */
4543 while( actual_capacity > sizeof( output ) )
4544 {
Gilles Peskine8817f612018-12-18 00:18:46 +01004545 PSA_ASSERT( psa_generator_read( &generator,
4546 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004547 actual_capacity -= sizeof( output );
4548 }
Gilles Peskine8817f612018-12-18 00:18:46 +01004549 PSA_ASSERT( psa_generator_read( &generator,
4550 output, actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004551 TEST_EQUAL( psa_generator_read( &generator, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004552 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004553
Gilles Peskine59685592018-09-18 12:11:34 +02004554exit:
4555 psa_generator_abort( &generator );
4556 psa_destroy_key( our_key );
4557 mbedtls_psa_crypto_free( );
4558}
4559/* END_CASE */
4560
4561/* BEGIN_CASE */
4562void key_agreement_output( int alg_arg,
4563 int our_key_type_arg, data_t *our_key_data,
4564 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004565 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004566{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004567 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004568 psa_algorithm_t alg = alg_arg;
4569 psa_key_type_t our_key_type = our_key_type_arg;
4570 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004571 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004572 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004573
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004574 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4575 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004576
Gilles Peskine8817f612018-12-18 00:18:46 +01004577 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004578
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004579 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4580 psa_set_key_algorithm( &attributes, alg );
4581 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02004582 PSA_ASSERT( psa_import_key( &attributes, our_key_data->x, our_key_data->len, &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004583
Gilles Peskine969c5d62019-01-16 15:53:06 +01004584 PSA_ASSERT( psa_key_derivation_setup( &generator, alg ) );
4585 PSA_ASSERT( psa_key_agreement( &generator, PSA_KDF_STEP_SECRET,
Gilles Peskine8817f612018-12-18 00:18:46 +01004586 our_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004587 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004588 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4589 {
4590 /* The test data is for info="" */
4591 PSA_ASSERT( psa_key_derivation_input_bytes( &generator,
4592 PSA_KDF_STEP_INFO,
4593 NULL, 0 ) );
4594 }
Gilles Peskine59685592018-09-18 12:11:34 +02004595
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004596 PSA_ASSERT( psa_generator_read( &generator,
4597 actual_output,
4598 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004599 ASSERT_COMPARE( actual_output, expected_output1->len,
4600 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004601 if( expected_output2->len != 0 )
4602 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004603 PSA_ASSERT( psa_generator_read( &generator,
4604 actual_output,
4605 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004606 ASSERT_COMPARE( actual_output, expected_output2->len,
4607 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004608 }
Gilles Peskine59685592018-09-18 12:11:34 +02004609
4610exit:
4611 psa_generator_abort( &generator );
4612 psa_destroy_key( our_key );
4613 mbedtls_psa_crypto_free( );
4614 mbedtls_free( actual_output );
4615}
4616/* END_CASE */
4617
4618/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004619void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004620{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004621 size_t bytes = bytes_arg;
4622 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004623 unsigned char *output = NULL;
4624 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004625 size_t i;
4626 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004627
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004628 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4629 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004630 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004631
Gilles Peskine8817f612018-12-18 00:18:46 +01004632 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004633
Gilles Peskinea50d7392018-06-21 10:22:13 +02004634 /* Run several times, to ensure that every output byte will be
4635 * nonzero at least once with overwhelming probability
4636 * (2^(-8*number_of_runs)). */
4637 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004638 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004639 if( bytes != 0 )
4640 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004641 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004642
4643 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004644 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4645 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004646
4647 for( i = 0; i < bytes; i++ )
4648 {
4649 if( output[i] != 0 )
4650 ++changed[i];
4651 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004652 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004653
4654 /* Check that every byte was changed to nonzero at least once. This
4655 * validates that psa_generate_random is overwriting every byte of
4656 * the output buffer. */
4657 for( i = 0; i < bytes; i++ )
4658 {
4659 TEST_ASSERT( changed[i] != 0 );
4660 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004661
4662exit:
4663 mbedtls_psa_crypto_free( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004664 mbedtls_free( output );
4665 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004666}
4667/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004668
4669/* BEGIN_CASE */
4670void generate_key( int type_arg,
4671 int bits_arg,
4672 int usage_arg,
4673 int alg_arg,
4674 int expected_status_arg )
4675{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004676 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004677 psa_key_type_t type = type_arg;
4678 psa_key_usage_t usage = usage_arg;
4679 size_t bits = bits_arg;
4680 psa_algorithm_t alg = alg_arg;
4681 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004682 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004683 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004684
Gilles Peskine8817f612018-12-18 00:18:46 +01004685 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004686
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004687 psa_set_key_usage_flags( &attributes, usage );
4688 psa_set_key_algorithm( &attributes, alg );
4689 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004690 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004691
4692 /* Generate a key */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004693 TEST_EQUAL( psa_generate_random_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004694 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004695 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004696
4697 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004698 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
4699 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4700 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004701
Gilles Peskine818ca122018-06-20 18:16:48 +02004702 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004703 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004704 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004705
4706exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004707 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004708 psa_destroy_key( handle );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004709 mbedtls_psa_crypto_free( );
4710}
4711/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004712
Gilles Peskinee56e8782019-04-26 17:34:02 +02004713/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
4714void generate_key_rsa( int bits_arg,
4715 data_t *e_arg,
4716 int expected_status_arg )
4717{
4718 psa_key_handle_t handle = 0;
4719 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEYPAIR;
4720 size_t bits = bits_arg;
4721 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4722 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4723 psa_status_t expected_status = expected_status_arg;
4724 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4725 uint8_t *exported = NULL;
4726 size_t exported_size =
4727 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
4728 size_t exported_length = SIZE_MAX;
4729 uint8_t *e_read_buffer = NULL;
4730 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004731 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004732 size_t e_read_length = SIZE_MAX;
4733
4734 if( e_arg->len == 0 ||
4735 ( e_arg->len == 3 &&
4736 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4737 {
4738 is_default_public_exponent = 1;
4739 e_read_size = 0;
4740 }
4741 ASSERT_ALLOC( e_read_buffer, e_read_size );
4742 ASSERT_ALLOC( exported, exported_size );
4743
4744 PSA_ASSERT( psa_crypto_init( ) );
4745
4746 psa_set_key_usage_flags( &attributes, usage );
4747 psa_set_key_algorithm( &attributes, alg );
4748 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4749 e_arg->x, e_arg->len ) );
4750 psa_set_key_bits( &attributes, bits );
4751
4752 /* Generate a key */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004753 TEST_EQUAL( psa_generate_random_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004754 if( expected_status != PSA_SUCCESS )
4755 goto exit;
4756
4757 /* Test the key information */
4758 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4759 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4760 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4761 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4762 e_read_buffer, e_read_size,
4763 &e_read_length ) );
4764 if( is_default_public_exponent )
4765 TEST_EQUAL( e_read_length, 0 );
4766 else
4767 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4768
4769 /* Do something with the key according to its type and permitted usage. */
4770 if( ! exercise_key( handle, usage, alg ) )
4771 goto exit;
4772
4773 /* Export the key and check the public exponent. */
4774 PSA_ASSERT( psa_export_public_key( handle,
4775 exported, exported_size,
4776 &exported_length ) );
4777 {
4778 uint8_t *p = exported;
4779 uint8_t *end = exported + exported_length;
4780 size_t len;
4781 /* RSAPublicKey ::= SEQUENCE {
4782 * modulus INTEGER, -- n
4783 * publicExponent INTEGER } -- e
4784 */
4785 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4786 MBEDTLS_ASN1_SEQUENCE |
4787 MBEDTLS_ASN1_CONSTRUCTED ) );
4788 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
4789 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4790 MBEDTLS_ASN1_INTEGER ) );
4791 if( len >= 1 && p[0] == 0 )
4792 {
4793 ++p;
4794 --len;
4795 }
4796 if( e_arg->len == 0 )
4797 {
4798 TEST_EQUAL( len, 3 );
4799 TEST_EQUAL( p[0], 1 );
4800 TEST_EQUAL( p[1], 0 );
4801 TEST_EQUAL( p[2], 1 );
4802 }
4803 else
4804 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4805 }
4806
4807exit:
4808 psa_reset_key_attributes( &attributes );
4809 psa_destroy_key( handle );
4810 mbedtls_psa_crypto_free( );
4811 mbedtls_free( e_read_buffer );
4812 mbedtls_free( exported );
4813}
4814/* END_CASE */
4815
Darryl Greend49a4992018-06-18 17:27:26 +01004816/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004817void persistent_key_load_key_from_storage( data_t *data,
4818 int type_arg, int bits_arg,
4819 int usage_flags_arg, int alg_arg,
4820 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004821{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004822 psa_key_id_t key_id = 1;
4823 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004824 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004825 psa_key_handle_t base_key = 0;
4826 psa_key_type_t type = type_arg;
4827 size_t bits = bits_arg;
4828 psa_key_usage_t usage_flags = usage_flags_arg;
4829 psa_algorithm_t alg = alg_arg;
Darryl Green0c6575a2018-11-07 16:05:30 +00004830 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004831 unsigned char *first_export = NULL;
4832 unsigned char *second_export = NULL;
4833 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4834 size_t first_exported_length;
4835 size_t second_exported_length;
4836
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004837 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4838 {
4839 ASSERT_ALLOC( first_export, export_size );
4840 ASSERT_ALLOC( second_export, export_size );
4841 }
Darryl Greend49a4992018-06-18 17:27:26 +01004842
Gilles Peskine8817f612018-12-18 00:18:46 +01004843 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004844
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004845 psa_make_key_persistent( &attributes, key_id, PSA_KEY_LIFETIME_PERSISTENT );
4846 psa_set_key_usage_flags( &attributes, usage_flags );
4847 psa_set_key_algorithm( &attributes, alg );
4848 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004849 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004850
Darryl Green0c6575a2018-11-07 16:05:30 +00004851 switch( generation_method )
4852 {
4853 case IMPORT_KEY:
4854 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02004855 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004856 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004857
Darryl Green0c6575a2018-11-07 16:05:30 +00004858 case GENERATE_KEY:
4859 /* Generate a key */
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004860 PSA_ASSERT( psa_generate_random_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004861 break;
4862
4863 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004864 {
4865 /* Create base key */
4866 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
4867 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4868 psa_set_key_usage_flags( &base_attributes,
4869 PSA_KEY_USAGE_DERIVE );
4870 psa_set_key_algorithm( &base_attributes, derive_alg );
4871 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine73676cb2019-05-15 20:15:10 +02004872 PSA_ASSERT( psa_import_key( &base_attributes, data->x, data->len, &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004873 /* Derive a key. */
4874 PSA_ASSERT( psa_key_derivation_setup( &generator, derive_alg ) );
4875 PSA_ASSERT( psa_key_derivation_input_key( &generator,
4876 PSA_KDF_STEP_SECRET,
4877 base_key ) );
4878 PSA_ASSERT( psa_key_derivation_input_bytes(
4879 &generator, PSA_KDF_STEP_INFO,
4880 NULL, 0 ) );
Gilles Peskine98dd7792019-05-15 19:43:49 +02004881 PSA_ASSERT( psa_generate_derived_key( &attributes, &generator,
4882 &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004883 PSA_ASSERT( psa_generator_abort( &generator ) );
4884 PSA_ASSERT( psa_destroy_key( base_key ) );
4885 base_key = 0;
4886 }
Darryl Green0c6575a2018-11-07 16:05:30 +00004887 break;
4888 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004889 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004890
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004891 /* Export the key if permitted by the key policy. */
4892 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4893 {
4894 PSA_ASSERT( psa_export_key( handle,
4895 first_export, export_size,
4896 &first_exported_length ) );
4897 if( generation_method == IMPORT_KEY )
4898 ASSERT_COMPARE( data->x, data->len,
4899 first_export, first_exported_length );
4900 }
Darryl Greend49a4992018-06-18 17:27:26 +01004901
4902 /* Shutdown and restart */
4903 mbedtls_psa_crypto_free();
Gilles Peskine8817f612018-12-18 00:18:46 +01004904 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004905
Darryl Greend49a4992018-06-18 17:27:26 +01004906 /* Check key slot still contains key data */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004907 PSA_ASSERT( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
Gilles Peskine8817f612018-12-18 00:18:46 +01004908 &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004909 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4910 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
4911 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
4912 PSA_KEY_LIFETIME_PERSISTENT );
4913 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4914 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4915 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
4916 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004917
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004918 /* Export the key again if permitted by the key policy. */
4919 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00004920 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004921 PSA_ASSERT( psa_export_key( handle,
4922 second_export, export_size,
4923 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004924 ASSERT_COMPARE( first_export, first_exported_length,
4925 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00004926 }
4927
4928 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004929 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00004930 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01004931
4932exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004933 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004934 mbedtls_free( first_export );
4935 mbedtls_free( second_export );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004936 psa_generator_abort( &generator );
4937 psa_destroy_key( base_key );
4938 if( handle == 0 )
4939 {
4940 /* In case there was a test failure after creating the persistent key
4941 * but while it was not open, try to re-open the persistent key
4942 * to delete it. */
4943 psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id, &handle );
4944 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004945 psa_destroy_key( handle );
Darryl Greend49a4992018-06-18 17:27:26 +01004946 mbedtls_psa_crypto_free();
4947}
4948/* END_CASE */