blob: a049ee8cb0d99dafa7fdc9ab2b93c2d7f10a69e9 [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
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskine1838e822019-06-20 12:40:56 +02008#include "psa_crypto_helpers.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +03009
Jaeden Amerof24c7f82018-06-27 17:20:43 +010010/** An invalid export length that will never be set by psa_export_key(). */
11static const size_t INVALID_EXPORT_LENGTH = ~0U;
12
Gilles Peskinef426e0f2019-02-25 17:42:03 +010013/* A hash algorithm that is known to be supported.
14 *
15 * This is used in some smoke tests.
16 */
17#if defined(MBEDTLS_MD2_C)
18#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
19#elif defined(MBEDTLS_MD4_C)
20#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
21#elif defined(MBEDTLS_MD5_C)
22#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
23/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
24 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
25 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
26 * implausible anyway. */
27#elif defined(MBEDTLS_SHA1_C)
28#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
29#elif defined(MBEDTLS_SHA256_C)
30#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
31#elif defined(MBEDTLS_SHA512_C)
32#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
33#elif defined(MBEDTLS_SHA3_C)
34#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
35#else
36#undef KNOWN_SUPPORTED_HASH_ALG
37#endif
38
39/* A block cipher that is known to be supported.
40 *
41 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
42 */
43#if defined(MBEDTLS_AES_C)
44#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
45#elif defined(MBEDTLS_ARIA_C)
46#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
47#elif defined(MBEDTLS_CAMELLIA_C)
48#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
49#undef KNOWN_SUPPORTED_BLOCK_CIPHER
50#endif
51
52/* A MAC mode that is known to be supported.
53 *
54 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
55 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
56 *
57 * This is used in some smoke tests.
58 */
59#if defined(KNOWN_SUPPORTED_HASH_ALG)
60#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
61#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
62#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
63#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
64#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
65#else
66#undef KNOWN_SUPPORTED_MAC_ALG
67#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
68#endif
69
70/* A cipher algorithm and key type that are known to be supported.
71 *
72 * This is used in some smoke tests.
73 */
74#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
75#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
76#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
77#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
78#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
79#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
80#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
81#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
82#else
83#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
84#endif
85#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
86#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
87#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
88#elif defined(MBEDTLS_RC4_C)
89#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
90#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
91#else
92#undef KNOWN_SUPPORTED_CIPHER_ALG
93#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
94#endif
95
Gilles Peskinea7aa4422018-08-14 15:17:54 +020096/** Test if a buffer contains a constant byte value.
97 *
98 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020099 *
100 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200101 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200102 * \param size Size of the buffer in bytes.
103 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200104 * \return 1 if the buffer is all-bits-zero.
105 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200106 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200107static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200108{
109 size_t i;
110 for( i = 0; i < size; i++ )
111 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200112 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200113 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200114 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200115 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200116}
Gilles Peskine818ca122018-06-20 18:16:48 +0200117
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200118/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
119static int asn1_write_10x( unsigned char **p,
120 unsigned char *start,
121 size_t bits,
122 unsigned char x )
123{
124 int ret;
125 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200126 if( bits == 0 )
127 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
128 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200129 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300130 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200131 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
132 *p -= len;
133 ( *p )[len-1] = x;
134 if( bits % 8 == 0 )
135 ( *p )[1] |= 1;
136 else
137 ( *p )[0] |= 1 << ( bits % 8 );
138 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
139 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
140 MBEDTLS_ASN1_INTEGER ) );
141 return( len );
142}
143
144static int construct_fake_rsa_key( unsigned char *buffer,
145 size_t buffer_size,
146 unsigned char **p,
147 size_t bits,
148 int keypair )
149{
150 size_t half_bits = ( bits + 1 ) / 2;
151 int ret;
152 int len = 0;
153 /* Construct something that looks like a DER encoding of
154 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
155 * RSAPrivateKey ::= SEQUENCE {
156 * version Version,
157 * modulus INTEGER, -- n
158 * publicExponent INTEGER, -- e
159 * privateExponent INTEGER, -- d
160 * prime1 INTEGER, -- p
161 * prime2 INTEGER, -- q
162 * exponent1 INTEGER, -- d mod (p-1)
163 * exponent2 INTEGER, -- d mod (q-1)
164 * coefficient INTEGER, -- (inverse of q) mod p
165 * otherPrimeInfos OtherPrimeInfos OPTIONAL
166 * }
167 * Or, for a public key, the same structure with only
168 * version, modulus and publicExponent.
169 */
170 *p = buffer + buffer_size;
171 if( keypair )
172 {
173 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
174 asn1_write_10x( p, buffer, half_bits, 1 ) );
175 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
176 asn1_write_10x( p, buffer, half_bits, 1 ) );
177 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
178 asn1_write_10x( p, buffer, half_bits, 1 ) );
179 MBEDTLS_ASN1_CHK_ADD( len, /* q */
180 asn1_write_10x( p, buffer, half_bits, 1 ) );
181 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
182 asn1_write_10x( p, buffer, half_bits, 3 ) );
183 MBEDTLS_ASN1_CHK_ADD( len, /* d */
184 asn1_write_10x( p, buffer, bits, 1 ) );
185 }
186 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
187 asn1_write_10x( p, buffer, 17, 1 ) );
188 MBEDTLS_ASN1_CHK_ADD( len, /* n */
189 asn1_write_10x( p, buffer, bits, 1 ) );
190 if( keypair )
191 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
192 mbedtls_asn1_write_int( p, buffer, 0 ) );
193 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
194 {
195 const unsigned char tag =
196 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
197 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
198 }
199 return( len );
200}
201
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100202int exercise_mac_setup( psa_key_type_t key_type,
203 const unsigned char *key_bytes,
204 size_t key_length,
205 psa_algorithm_t alg,
206 psa_mac_operation_t *operation,
207 psa_status_t *status )
208{
209 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200210 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100211
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200212 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
213 psa_set_key_algorithm( &attributes, alg );
214 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +0200215 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length,
216 &handle ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100217
218 *status = psa_mac_sign_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100219 /* Whether setup succeeded or failed, abort must succeed. */
220 PSA_ASSERT( psa_mac_abort( operation ) );
221 /* If setup failed, reproduce the failure, so that the caller can
222 * test the resulting state of the operation object. */
223 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100224 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100225 TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ),
226 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100227 }
228
229 psa_destroy_key( handle );
230 return( 1 );
231
232exit:
233 psa_destroy_key( handle );
234 return( 0 );
235}
236
237int exercise_cipher_setup( psa_key_type_t key_type,
238 const unsigned char *key_bytes,
239 size_t key_length,
240 psa_algorithm_t alg,
241 psa_cipher_operation_t *operation,
242 psa_status_t *status )
243{
244 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200245 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100246
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200247 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
248 psa_set_key_algorithm( &attributes, alg );
249 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +0200250 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length,
251 &handle ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100252
253 *status = psa_cipher_encrypt_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100254 /* Whether setup succeeded or failed, abort must succeed. */
255 PSA_ASSERT( psa_cipher_abort( operation ) );
256 /* If setup failed, reproduce the failure, so that the caller can
257 * test the resulting state of the operation object. */
258 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100259 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100260 TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ),
261 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100262 }
263
264 psa_destroy_key( handle );
265 return( 1 );
266
267exit:
268 psa_destroy_key( handle );
269 return( 0 );
270}
271
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100272static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200273 psa_key_usage_t usage,
274 psa_algorithm_t alg )
275{
Jaeden Amero769ce272019-01-04 11:48:03 +0000276 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200277 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200278 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200279 size_t mac_length = sizeof( mac );
280
281 if( usage & PSA_KEY_USAGE_SIGN )
282 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100283 PSA_ASSERT( psa_mac_sign_setup( &operation,
284 handle, alg ) );
285 PSA_ASSERT( psa_mac_update( &operation,
286 input, sizeof( input ) ) );
287 PSA_ASSERT( psa_mac_sign_finish( &operation,
288 mac, sizeof( mac ),
289 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200290 }
291
292 if( usage & PSA_KEY_USAGE_VERIFY )
293 {
294 psa_status_t verify_status =
295 ( usage & PSA_KEY_USAGE_SIGN ?
296 PSA_SUCCESS :
297 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100298 PSA_ASSERT( psa_mac_verify_setup( &operation,
299 handle, alg ) );
300 PSA_ASSERT( psa_mac_update( &operation,
301 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100302 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
303 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200304 }
305
306 return( 1 );
307
308exit:
309 psa_mac_abort( &operation );
310 return( 0 );
311}
312
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100313static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200314 psa_key_usage_t usage,
315 psa_algorithm_t alg )
316{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000317 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200318 unsigned char iv[16] = {0};
319 size_t iv_length = sizeof( iv );
320 const unsigned char plaintext[16] = "Hello, world...";
321 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
322 size_t ciphertext_length = sizeof( ciphertext );
323 unsigned char decrypted[sizeof( ciphertext )];
324 size_t part_length;
325
326 if( usage & PSA_KEY_USAGE_ENCRYPT )
327 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100328 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
329 handle, alg ) );
330 PSA_ASSERT( psa_cipher_generate_iv( &operation,
331 iv, sizeof( iv ),
332 &iv_length ) );
333 PSA_ASSERT( psa_cipher_update( &operation,
334 plaintext, sizeof( plaintext ),
335 ciphertext, sizeof( ciphertext ),
336 &ciphertext_length ) );
337 PSA_ASSERT( psa_cipher_finish( &operation,
338 ciphertext + ciphertext_length,
339 sizeof( ciphertext ) - ciphertext_length,
340 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200341 ciphertext_length += part_length;
342 }
343
344 if( usage & PSA_KEY_USAGE_DECRYPT )
345 {
346 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200347 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200348 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
349 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
351 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
352 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
353 * have this macro yet. */
354 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE(
355 psa_get_key_type( &attributes ) );
356 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200357 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100358 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
359 handle, alg ) );
360 PSA_ASSERT( psa_cipher_set_iv( &operation,
361 iv, iv_length ) );
362 PSA_ASSERT( psa_cipher_update( &operation,
363 ciphertext, ciphertext_length,
364 decrypted, sizeof( decrypted ),
365 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200366 status = psa_cipher_finish( &operation,
367 decrypted + part_length,
368 sizeof( decrypted ) - part_length,
369 &part_length );
370 /* For a stream cipher, all inputs are valid. For a block cipher,
371 * if the input is some aribtrary data rather than an actual
372 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200373 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200374 TEST_ASSERT( status == PSA_SUCCESS ||
375 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200376 else
377 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200378 }
379
380 return( 1 );
381
382exit:
383 psa_cipher_abort( &operation );
384 return( 0 );
385}
386
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100387static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200388 psa_key_usage_t usage,
389 psa_algorithm_t alg )
390{
391 unsigned char nonce[16] = {0};
392 size_t nonce_length = sizeof( nonce );
393 unsigned char plaintext[16] = "Hello, world...";
394 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
395 size_t ciphertext_length = sizeof( ciphertext );
396 size_t plaintext_length = sizeof( ciphertext );
397
398 if( usage & PSA_KEY_USAGE_ENCRYPT )
399 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100400 PSA_ASSERT( psa_aead_encrypt( handle, alg,
401 nonce, nonce_length,
402 NULL, 0,
403 plaintext, sizeof( plaintext ),
404 ciphertext, sizeof( ciphertext ),
405 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200406 }
407
408 if( usage & PSA_KEY_USAGE_DECRYPT )
409 {
410 psa_status_t verify_status =
411 ( usage & PSA_KEY_USAGE_ENCRYPT ?
412 PSA_SUCCESS :
413 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100414 TEST_EQUAL( psa_aead_decrypt( handle, alg,
415 nonce, nonce_length,
416 NULL, 0,
417 ciphertext, ciphertext_length,
418 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100419 &plaintext_length ),
420 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200421 }
422
423 return( 1 );
424
425exit:
426 return( 0 );
427}
428
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100429static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200430 psa_key_usage_t usage,
431 psa_algorithm_t alg )
432{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200433 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
434 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200435 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200436 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100437 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
438
439 /* If the policy allows signing with any hash, just pick one. */
440 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
441 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100442#if defined(KNOWN_SUPPORTED_HASH_ALG)
443 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
444 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100445#else
446 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100447 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100448#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100449 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200450
451 if( usage & PSA_KEY_USAGE_SIGN )
452 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200453 /* Some algorithms require the payload to have the size of
454 * the hash encoded in the algorithm. Use this input size
455 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200456 if( hash_alg != 0 )
457 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100458 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
459 payload, payload_length,
460 signature, sizeof( signature ),
461 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200462 }
463
464 if( usage & PSA_KEY_USAGE_VERIFY )
465 {
466 psa_status_t verify_status =
467 ( usage & PSA_KEY_USAGE_SIGN ?
468 PSA_SUCCESS :
469 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100470 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
471 payload, payload_length,
472 signature, signature_length ),
473 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200474 }
475
476 return( 1 );
477
478exit:
479 return( 0 );
480}
481
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100482static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200483 psa_key_usage_t usage,
484 psa_algorithm_t alg )
485{
486 unsigned char plaintext[256] = "Hello, world...";
487 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
488 size_t ciphertext_length = sizeof( ciphertext );
489 size_t plaintext_length = 16;
490
491 if( usage & PSA_KEY_USAGE_ENCRYPT )
492 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100493 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
494 plaintext, plaintext_length,
495 NULL, 0,
496 ciphertext, sizeof( ciphertext ),
497 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200498 }
499
500 if( usage & PSA_KEY_USAGE_DECRYPT )
501 {
502 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100503 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200504 ciphertext, ciphertext_length,
505 NULL, 0,
506 plaintext, sizeof( plaintext ),
507 &plaintext_length );
508 TEST_ASSERT( status == PSA_SUCCESS ||
509 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
510 ( status == PSA_ERROR_INVALID_ARGUMENT ||
511 status == PSA_ERROR_INVALID_PADDING ) ) );
512 }
513
514 return( 1 );
515
516exit:
517 return( 0 );
518}
Gilles Peskine02b75072018-07-01 22:31:34 +0200519
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100520static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200521 psa_key_usage_t usage,
522 psa_algorithm_t alg )
523{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200524 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +0200525 unsigned char label[16] = "This is a label.";
526 size_t label_length = sizeof( label );
527 unsigned char seed[16] = "abcdefghijklmnop";
528 size_t seed_length = sizeof( seed );
529 unsigned char output[1];
530
531 if( usage & PSA_KEY_USAGE_DERIVE )
532 {
Gilles Peskineb70a0fd2019-01-07 22:59:38 +0100533 if( PSA_ALG_IS_HKDF( alg ) )
534 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200535 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
536 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +0200537 PSA_KEY_DERIVATION_INPUT_SALT,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +0100538 label,
539 label_length ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200540 PSA_ASSERT( psa_key_derivation_input_key( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +0200541 PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +0100542 handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200543 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +0200544 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +0100545 seed,
546 seed_length ) );
547 }
Janos Follath71a4c912019-06-11 09:14:47 +0100548#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +0100549 else
550 {
551 // legacy
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200552 PSA_ASSERT( psa_key_derivation( &operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +0100553 handle, alg,
554 label, label_length,
555 seed, seed_length,
556 sizeof( output ) ) );
557 }
Janos Follath71a4c912019-06-11 09:14:47 +0100558#endif
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200559 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200560 output,
561 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200562 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
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. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200573static psa_status_t key_agreement_with_self(
574 psa_key_derivation_operation_t *operation,
575 psa_key_handle_t handle )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100576{
577 psa_key_type_t private_key_type;
578 psa_key_type_t public_key_type;
579 size_t key_bits;
580 uint8_t *public_key = NULL;
581 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200582 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200583 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
584 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200585 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200586 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100587
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200588 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
589 private_key_type = psa_get_key_type( &attributes );
590 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200591 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100592 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
593 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100594 PSA_ASSERT( psa_export_public_key( handle,
595 public_key, public_key_length,
596 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100597
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200598 status = psa_key_derivation_key_agreement(
599 operation, PSA_KEY_DERIVATION_INPUT_SECRET, handle,
600 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100601exit:
602 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200603 psa_reset_key_attributes( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100604 return( status );
605}
606
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200607/* We need two keys to exercise key agreement. Exercise the
608 * private key against its own public key. */
609static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
610 psa_key_handle_t handle )
611{
612 psa_key_type_t private_key_type;
613 psa_key_type_t public_key_type;
614 size_t key_bits;
615 uint8_t *public_key = NULL;
616 size_t public_key_length;
617 uint8_t output[1024];
618 size_t output_length;
619 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200620 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
621 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200622 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200623 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200624
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200625 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
626 private_key_type = psa_get_key_type( &attributes );
627 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200628 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200629 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
630 ASSERT_ALLOC( public_key, public_key_length );
631 PSA_ASSERT( psa_export_public_key( handle,
632 public_key, public_key_length,
633 &public_key_length ) );
634
Gilles Peskinebe697d82019-05-16 18:00:41 +0200635 status = psa_raw_key_agreement( alg, handle,
636 public_key, public_key_length,
637 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200638exit:
639 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200640 psa_reset_key_attributes( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200641 return( status );
642}
643
644static int exercise_raw_key_agreement_key( psa_key_handle_t handle,
645 psa_key_usage_t usage,
646 psa_algorithm_t alg )
647{
648 int ok = 0;
649
650 if( usage & PSA_KEY_USAGE_DERIVE )
651 {
652 /* We need two keys to exercise key agreement. Exercise the
653 * private key against its own public key. */
654 PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) );
655 }
656 ok = 1;
657
658exit:
659 return( ok );
660}
661
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100662static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200663 psa_key_usage_t usage,
664 psa_algorithm_t alg )
665{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200666 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200667 unsigned char output[1];
668 int ok = 0;
669
670 if( usage & PSA_KEY_USAGE_DERIVE )
671 {
672 /* We need two keys to exercise key agreement. Exercise the
673 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200674 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
675 PSA_ASSERT( key_agreement_with_self( &operation, handle ) );
676 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200677 output,
678 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200679 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200680 }
681 ok = 1;
682
683exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200684 return( ok );
685}
686
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200687static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
688 size_t min_bits, size_t max_bits,
689 int must_be_odd )
690{
691 size_t len;
692 size_t actual_bits;
693 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100694 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100695 MBEDTLS_ASN1_INTEGER ),
696 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200697 /* Tolerate a slight departure from DER encoding:
698 * - 0 may be represented by an empty string or a 1-byte string.
699 * - The sign bit may be used as a value bit. */
700 if( ( len == 1 && ( *p )[0] == 0 ) ||
701 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
702 {
703 ++( *p );
704 --len;
705 }
706 if( min_bits == 0 && len == 0 )
707 return( 1 );
708 msb = ( *p )[0];
709 TEST_ASSERT( msb != 0 );
710 actual_bits = 8 * ( len - 1 );
711 while( msb != 0 )
712 {
713 msb >>= 1;
714 ++actual_bits;
715 }
716 TEST_ASSERT( actual_bits >= min_bits );
717 TEST_ASSERT( actual_bits <= max_bits );
718 if( must_be_odd )
719 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
720 *p += len;
721 return( 1 );
722exit:
723 return( 0 );
724}
725
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200726static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
727 uint8_t *exported, size_t exported_length )
728{
729 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100730 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200731 else
732 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200733
734#if defined(MBEDTLS_DES_C)
735 if( type == PSA_KEY_TYPE_DES )
736 {
737 /* Check the parity bits. */
738 unsigned i;
739 for( i = 0; i < bits / 8; i++ )
740 {
741 unsigned bit_count = 0;
742 unsigned m;
743 for( m = 1; m <= 0x100; m <<= 1 )
744 {
745 if( exported[i] & m )
746 ++bit_count;
747 }
748 TEST_ASSERT( bit_count % 2 != 0 );
749 }
750 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200751 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200752#endif
753
754#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200755 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200756 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200757 uint8_t *p = exported;
758 uint8_t *end = exported + exported_length;
759 size_t len;
760 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200761 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200762 * modulus INTEGER, -- n
763 * publicExponent INTEGER, -- e
764 * privateExponent INTEGER, -- d
765 * prime1 INTEGER, -- p
766 * prime2 INTEGER, -- q
767 * exponent1 INTEGER, -- d mod (p-1)
768 * exponent2 INTEGER, -- d mod (q-1)
769 * coefficient INTEGER, -- (inverse of q) mod p
770 * }
771 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100772 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
773 MBEDTLS_ASN1_SEQUENCE |
774 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
775 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200776 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
777 goto exit;
778 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
779 goto exit;
780 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
781 goto exit;
782 /* Require d to be at least half the size of n. */
783 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
784 goto exit;
785 /* Require p and q to be at most half the size of n, rounded up. */
786 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
787 goto exit;
788 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
789 goto exit;
790 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
791 goto exit;
792 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
793 goto exit;
794 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
795 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100796 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100797 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200798 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200799#endif /* MBEDTLS_RSA_C */
800
801#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200802 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200803 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100804 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100805 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100806 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200807 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200808#endif /* MBEDTLS_ECP_C */
809
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200810 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
811 {
812 uint8_t *p = exported;
813 uint8_t *end = exported + exported_length;
814 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200815#if defined(MBEDTLS_RSA_C)
816 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
817 {
818 /* RSAPublicKey ::= SEQUENCE {
819 * modulus INTEGER, -- n
820 * publicExponent INTEGER } -- e
821 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100822 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
823 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100824 MBEDTLS_ASN1_CONSTRUCTED ),
825 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100826 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200827 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
828 goto exit;
829 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
830 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100831 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200832 }
833 else
834#endif /* MBEDTLS_RSA_C */
835#if defined(MBEDTLS_ECP_C)
836 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
837 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000838 /* The representation of an ECC public key is:
839 * - The byte 0x04;
840 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
841 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
842 * - where m is the bit size associated with the curve.
Jaeden Amero6b196002019-01-10 10:23:21 +0000843 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100844 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
845 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200846 }
847 else
848#endif /* MBEDTLS_ECP_C */
849 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100850 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200851 mbedtls_snprintf( message, sizeof( message ),
852 "No sanity check for public key type=0x%08lx",
853 (unsigned long) type );
854 test_fail( message, __LINE__, __FILE__ );
855 return( 0 );
856 }
857 }
858 else
859
860 {
861 /* No sanity checks for other types */
862 }
863
864 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200865
866exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200867 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200868}
869
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100870static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200871 psa_key_usage_t usage )
872{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200873 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200874 uint8_t *exported = NULL;
875 size_t exported_size = 0;
876 size_t exported_length = 0;
877 int ok = 0;
878
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200879 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200880
881 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200882 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200883 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100884 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
885 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200886 ok = 1;
887 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +0200888 }
889
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200890 exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ),
891 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200892 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200893
Gilles Peskine8817f612018-12-18 00:18:46 +0100894 PSA_ASSERT( psa_export_key( handle,
895 exported, exported_size,
896 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200897 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
898 psa_get_key_bits( &attributes ),
899 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +0200900
901exit:
902 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200903 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +0200904 return( ok );
905}
906
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100907static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200908{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200909 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200910 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +0200911 uint8_t *exported = NULL;
912 size_t exported_size = 0;
913 size_t exported_length = 0;
914 int ok = 0;
915
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200916 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
917 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200918 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100919 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100920 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200921 return( 1 );
922 }
923
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200924 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200925 psa_get_key_type( &attributes ) );
926 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type,
927 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200928 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200929
Gilles Peskine8817f612018-12-18 00:18:46 +0100930 PSA_ASSERT( psa_export_public_key( handle,
931 exported, exported_size,
932 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200933 ok = exported_key_sanity_check( public_type,
934 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +0200935 exported, exported_length );
936
937exit:
938 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200939 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +0200940 return( ok );
941}
942
Gilles Peskinec9516fb2019-02-05 20:32:06 +0100943/** Do smoke tests on a key.
944 *
945 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
946 * sign/verify, or derivation) that is permitted according to \p usage.
947 * \p usage and \p alg should correspond to the expected policy on the
948 * key.
949 *
950 * Export the key if permitted by \p usage, and check that the output
951 * looks sensible. If \p usage forbids export, check that
952 * \p psa_export_key correctly rejects the attempt. If the key is
953 * asymmetric, also check \p psa_export_public_key.
954 *
955 * If the key fails the tests, this function calls the test framework's
956 * `test_fail` function and returns false. Otherwise this function returns
957 * true. Therefore it should be used as follows:
958 * ```
959 * if( ! exercise_key( ... ) ) goto exit;
960 * ```
961 *
962 * \param handle The key to exercise. It should be capable of performing
963 * \p alg.
964 * \param usage The usage flags to assume.
965 * \param alg The algorithm to exercise.
966 *
967 * \retval 0 The key failed the smoke tests.
968 * \retval 1 The key passed the smoke tests.
969 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100970static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200971 psa_key_usage_t usage,
972 psa_algorithm_t alg )
973{
974 int ok;
975 if( alg == 0 )
976 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
977 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100978 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200979 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100980 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200981 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100982 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200983 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100984 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200985 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100986 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200987 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100988 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200989 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
990 ok = exercise_raw_key_agreement_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200991 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100992 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200993 else
994 {
995 char message[40];
996 mbedtls_snprintf( message, sizeof( message ),
997 "No code to exercise alg=0x%08lx",
998 (unsigned long) alg );
999 test_fail( message, __LINE__, __FILE__ );
1000 ok = 0;
1001 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001002
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001003 ok = ok && exercise_export_key( handle, usage );
1004 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +02001005
Gilles Peskine02b75072018-07-01 22:31:34 +02001006 return( ok );
1007}
1008
Gilles Peskine10df3412018-10-25 22:35:43 +02001009static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1010 psa_algorithm_t alg )
1011{
1012 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1013 {
1014 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1015 PSA_KEY_USAGE_VERIFY :
1016 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
1017 }
1018 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1019 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1020 {
1021 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1022 PSA_KEY_USAGE_ENCRYPT :
1023 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1024 }
1025 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1026 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1027 {
1028 return( PSA_KEY_USAGE_DERIVE );
1029 }
1030 else
1031 {
1032 return( 0 );
1033 }
1034
1035}
Darryl Green0c6575a2018-11-07 16:05:30 +00001036
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001037static int test_operations_on_invalid_handle( psa_key_handle_t handle )
1038{
1039 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1040 uint8_t buffer[1];
1041 size_t length;
1042 int ok = 0;
1043
Gilles Peskinec87af662019-05-15 16:12:22 +02001044 psa_set_key_id( &attributes, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001045 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1046 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1047 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
1048 TEST_EQUAL( psa_get_key_attributes( handle, &attributes ),
1049 PSA_ERROR_INVALID_HANDLE );
1050 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001051 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001052 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1053 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1054 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1055 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1056
1057 TEST_EQUAL( psa_export_key( handle,
1058 buffer, sizeof( buffer ), &length ),
1059 PSA_ERROR_INVALID_HANDLE );
1060 TEST_EQUAL( psa_export_public_key( handle,
1061 buffer, sizeof( buffer ), &length ),
1062 PSA_ERROR_INVALID_HANDLE );
1063
1064 TEST_EQUAL( psa_close_key( handle ), PSA_ERROR_INVALID_HANDLE );
1065 TEST_EQUAL( psa_destroy_key( handle ), PSA_ERROR_INVALID_HANDLE );
1066
1067 ok = 1;
1068
1069exit:
1070 psa_reset_key_attributes( &attributes );
1071 return( ok );
1072}
1073
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001074/* An overapproximation of the amount of storage needed for a key of the
1075 * given type and with the given content. The API doesn't make it easy
1076 * to find a good value for the size. The current implementation doesn't
1077 * care about the value anyway. */
1078#define KEY_BITS_FROM_DATA( type, data ) \
1079 ( data )->len
1080
Darryl Green0c6575a2018-11-07 16:05:30 +00001081typedef enum {
1082 IMPORT_KEY = 0,
1083 GENERATE_KEY = 1,
1084 DERIVE_KEY = 2
1085} generate_method;
1086
Gilles Peskinee59236f2018-01-27 23:32:46 +01001087/* END_HEADER */
1088
1089/* BEGIN_DEPENDENCIES
1090 * depends_on:MBEDTLS_PSA_CRYPTO_C
1091 * END_DEPENDENCIES
1092 */
1093
1094/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001095void static_checks( )
1096{
1097 size_t max_truncated_mac_size =
1098 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1099
1100 /* Check that the length for a truncated MAC always fits in the algorithm
1101 * encoding. The shifted mask is the maximum truncated value. The
1102 * untruncated algorithm may be one byte larger. */
1103 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
1104}
1105/* END_CASE */
1106
1107/* BEGIN_CASE */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001108void attributes_set_get( int id_arg, int lifetime_arg,
1109 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001110 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001111{
Gilles Peskine4747d192019-04-17 15:05:45 +02001112 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001113 psa_key_id_t id = id_arg;
1114 psa_key_lifetime_t lifetime = lifetime_arg;
1115 psa_key_usage_t usage_flags = usage_flags_arg;
1116 psa_algorithm_t alg = alg_arg;
1117 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001118 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001119
1120 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1121 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1122 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1123 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1124 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001125 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001126
Gilles Peskinec87af662019-05-15 16:12:22 +02001127 psa_set_key_id( &attributes, id );
1128 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001129 psa_set_key_usage_flags( &attributes, usage_flags );
1130 psa_set_key_algorithm( &attributes, alg );
1131 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001132 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001133
1134 TEST_EQUAL( psa_get_key_id( &attributes ), id );
1135 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1136 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1137 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1138 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001139 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001140
1141 psa_reset_key_attributes( &attributes );
1142
1143 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1144 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1145 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1146 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1147 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001148 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001149}
1150/* END_CASE */
1151
1152/* BEGIN_CASE */
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001153void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg,
1154 int expected_id_arg, int expected_lifetime_arg )
1155{
1156 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1157 psa_key_id_t id1 = id1_arg;
1158 psa_key_lifetime_t lifetime = lifetime_arg;
1159 psa_key_id_t id2 = id2_arg;
1160 psa_key_id_t expected_id = expected_id_arg;
1161 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1162
1163 if( id1_arg != -1 )
1164 psa_set_key_id( &attributes, id1 );
1165 if( lifetime_arg != -1 )
1166 psa_set_key_lifetime( &attributes, lifetime );
1167 if( id2_arg != -1 )
1168 psa_set_key_id( &attributes, id2 );
1169
1170 TEST_EQUAL( psa_get_key_id( &attributes ), expected_id );
1171 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1172}
1173/* END_CASE */
1174
1175/* BEGIN_CASE */
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001176void import( data_t *data, int type_arg,
1177 int attr_bits_arg,
1178 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001179{
1180 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1181 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001182 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001183 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001184 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001185 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001186 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001187
Gilles Peskine8817f612018-12-18 00:18:46 +01001188 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001189
Gilles Peskine4747d192019-04-17 15:05:45 +02001190 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001191 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001192 status = psa_import_key( &attributes, data->x, data->len, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001193 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001194 if( status != PSA_SUCCESS )
1195 goto exit;
1196
1197 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1198 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001199 if( attr_bits != 0 )
1200 TEST_EQUAL( attr_bits, got_attributes.bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001201
1202 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001203 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001204
1205exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001206 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001207 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001208 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001209}
1210/* END_CASE */
1211
1212/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001213void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1214{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001215 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001216 size_t bits = bits_arg;
1217 psa_status_t expected_status = expected_status_arg;
1218 psa_status_t status;
1219 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001220 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001221 size_t buffer_size = /* Slight overapproximations */
1222 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001223 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001224 unsigned char *p;
1225 int ret;
1226 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001227 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001228
Gilles Peskine8817f612018-12-18 00:18:46 +01001229 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001230 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001231
1232 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1233 bits, keypair ) ) >= 0 );
1234 length = ret;
1235
1236 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001237 psa_set_key_type( &attributes, type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001238 status = psa_import_key( &attributes, p, length, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001239 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001240
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001241 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001242 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001243
1244exit:
1245 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001246 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001247}
1248/* END_CASE */
1249
1250/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001251void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001252 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001253 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001254 int expected_bits,
1255 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001256 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001257 int canonical_input )
1258{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001259 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001260 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001261 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001262 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001263 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001264 unsigned char *exported = NULL;
1265 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001266 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001267 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001268 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001269 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001270 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001271
Moran Pekercb088e72018-07-17 17:36:59 +03001272 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001273 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001274 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001275 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001276 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001277
Gilles Peskine4747d192019-04-17 15:05:45 +02001278 psa_set_key_usage_flags( &attributes, usage_arg );
1279 psa_set_key_algorithm( &attributes, alg );
1280 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001281
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001282 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001283 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001284
1285 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001286 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1287 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1288 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001289
1290 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001291 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001292 exported, export_size,
1293 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001294 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001295
1296 /* The exported length must be set by psa_export_key() to a value between 0
1297 * and export_size. On errors, the exported length must be 0. */
1298 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1299 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1300 TEST_ASSERT( exported_length <= export_size );
1301
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001302 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001303 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001304 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001305 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001306 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001307 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001308 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001309
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001310 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001311 goto exit;
1312
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001313 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001314 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001315 else
1316 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001317 psa_key_handle_t handle2;
Gilles Peskine049c7532019-05-15 20:22:09 +02001318 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
1319 &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001320 PSA_ASSERT( psa_export_key( handle2,
1321 reexported,
1322 export_size,
1323 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001324 ASSERT_COMPARE( exported, exported_length,
1325 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001326 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001327 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001328 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001329
1330destroy:
1331 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001332 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001333 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001334
1335exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001336 mbedtls_free( exported );
1337 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001338 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001339 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001340}
1341/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001342
Moran Pekerf709f4a2018-06-06 17:26:04 +03001343/* BEGIN_CASE */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001344void invalid_handle( int handle )
Moran Peker28a38e62018-11-07 16:18:24 +02001345{
Gilles Peskine8817f612018-12-18 00:18:46 +01001346 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001347 test_operations_on_invalid_handle( handle );
Moran Peker28a38e62018-11-07 16:18:24 +02001348
1349exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001350 PSA_DONE( );
Moran Peker28a38e62018-11-07 16:18:24 +02001351}
1352/* END_CASE */
1353
1354/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001355void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001356 int type_arg,
1357 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001358 int export_size_delta,
1359 int expected_export_status_arg,
1360 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001361{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001362 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001363 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001364 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001365 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001366 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001367 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001368 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001369 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001370 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001371
Gilles Peskine8817f612018-12-18 00:18:46 +01001372 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001373
Gilles Peskine4747d192019-04-17 15:05:45 +02001374 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1375 psa_set_key_algorithm( &attributes, alg );
1376 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001377
1378 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001379 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001380
Gilles Peskine49c25912018-10-29 15:15:31 +01001381 /* Export the public key */
1382 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001383 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001384 exported, export_size,
1385 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001386 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001387 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001388 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001389 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001390 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001391 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1392 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001393 TEST_ASSERT( expected_public_key->len <=
1394 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001395 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1396 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001397 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001398
1399exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001400 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001401 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001402 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001403 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001404}
1405/* END_CASE */
1406
Gilles Peskine20035e32018-02-03 22:44:14 +01001407/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001408void import_and_exercise_key( data_t *data,
1409 int type_arg,
1410 int bits_arg,
1411 int alg_arg )
1412{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001413 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001414 psa_key_type_t type = type_arg;
1415 size_t bits = bits_arg;
1416 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001417 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001418 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001419 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001420
Gilles Peskine8817f612018-12-18 00:18:46 +01001421 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001422
Gilles Peskine4747d192019-04-17 15:05:45 +02001423 psa_set_key_usage_flags( &attributes, usage );
1424 psa_set_key_algorithm( &attributes, alg );
1425 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001426
1427 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001428 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001429
1430 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001431 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1432 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1433 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001434
1435 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001436 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001437 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001438
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001439 PSA_ASSERT( psa_destroy_key( handle ) );
1440 test_operations_on_invalid_handle( handle );
1441
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001442exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001443 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001444 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001445 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001446}
1447/* END_CASE */
1448
1449/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001450void key_policy( int usage_arg, int alg_arg )
1451{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001452 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001453 psa_algorithm_t alg = alg_arg;
1454 psa_key_usage_t usage = usage_arg;
1455 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1456 unsigned char key[32] = {0};
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001457 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001458
1459 memset( key, 0x2a, sizeof( key ) );
1460
Gilles Peskine8817f612018-12-18 00:18:46 +01001461 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001462
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001463 psa_set_key_usage_flags( &attributes, usage );
1464 psa_set_key_algorithm( &attributes, alg );
1465 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001466
Gilles Peskine73676cb2019-05-15 20:15:10 +02001467 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001468
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001469 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1470 TEST_EQUAL( psa_get_key_type( &attributes ), key_type );
1471 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage );
1472 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001473
1474exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001475 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001476 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001477 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001478}
1479/* END_CASE */
1480
1481/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001482void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001483{
1484 /* Test each valid way of initializing the object, except for `= {0}`, as
1485 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1486 * though it's OK by the C standard. We could test for this, but we'd need
1487 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001488 psa_key_attributes_t func = psa_key_attributes_init( );
1489 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1490 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001491
1492 memset( &zero, 0, sizeof( zero ) );
1493
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001494 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1495 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1496 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001497
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001498 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1499 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1500 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1501
1502 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1503 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1504 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1505
1506 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1507 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1508 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1509
1510 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1511 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1512 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001513}
1514/* END_CASE */
1515
1516/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001517void mac_key_policy( int policy_usage,
1518 int policy_alg,
1519 int key_type,
1520 data_t *key_data,
1521 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001522{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001523 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001524 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001525 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001526 psa_status_t status;
1527 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001528
Gilles Peskine8817f612018-12-18 00:18:46 +01001529 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001530
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001531 psa_set_key_usage_flags( &attributes, policy_usage );
1532 psa_set_key_algorithm( &attributes, policy_alg );
1533 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001534
Gilles Peskine049c7532019-05-15 20:22:09 +02001535 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1536 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001537
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001538 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001539 if( policy_alg == exercise_alg &&
1540 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001541 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001542 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001543 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001544 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001545
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001546 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001547 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001548 if( policy_alg == exercise_alg &&
1549 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001550 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001551 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001552 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001553
1554exit:
1555 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001556 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001557 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001558}
1559/* END_CASE */
1560
1561/* BEGIN_CASE */
1562void cipher_key_policy( int policy_usage,
1563 int policy_alg,
1564 int key_type,
1565 data_t *key_data,
1566 int exercise_alg )
1567{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001568 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001569 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001570 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001571 psa_status_t status;
1572
Gilles Peskine8817f612018-12-18 00:18:46 +01001573 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001574
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001575 psa_set_key_usage_flags( &attributes, policy_usage );
1576 psa_set_key_algorithm( &attributes, policy_alg );
1577 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001578
Gilles Peskine049c7532019-05-15 20:22:09 +02001579 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1580 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001581
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001582 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001583 if( policy_alg == exercise_alg &&
1584 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001585 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001586 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001587 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001588 psa_cipher_abort( &operation );
1589
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001590 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001591 if( policy_alg == exercise_alg &&
1592 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001593 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001594 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001595 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001596
1597exit:
1598 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001599 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001600 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001601}
1602/* END_CASE */
1603
1604/* BEGIN_CASE */
1605void aead_key_policy( int policy_usage,
1606 int policy_alg,
1607 int key_type,
1608 data_t *key_data,
1609 int nonce_length_arg,
1610 int tag_length_arg,
1611 int exercise_alg )
1612{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001613 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001614 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001615 psa_status_t status;
1616 unsigned char nonce[16] = {0};
1617 size_t nonce_length = nonce_length_arg;
1618 unsigned char tag[16];
1619 size_t tag_length = tag_length_arg;
1620 size_t output_length;
1621
1622 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1623 TEST_ASSERT( tag_length <= sizeof( tag ) );
1624
Gilles Peskine8817f612018-12-18 00:18:46 +01001625 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001626
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001627 psa_set_key_usage_flags( &attributes, policy_usage );
1628 psa_set_key_algorithm( &attributes, policy_alg );
1629 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001630
Gilles Peskine049c7532019-05-15 20:22:09 +02001631 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1632 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001633
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001634 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001635 nonce, nonce_length,
1636 NULL, 0,
1637 NULL, 0,
1638 tag, tag_length,
1639 &output_length );
1640 if( policy_alg == exercise_alg &&
1641 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001642 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001643 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001644 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001645
1646 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001647 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001648 nonce, nonce_length,
1649 NULL, 0,
1650 tag, tag_length,
1651 NULL, 0,
1652 &output_length );
1653 if( policy_alg == exercise_alg &&
1654 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001655 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001656 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001657 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001658
1659exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001660 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001661 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001662}
1663/* END_CASE */
1664
1665/* BEGIN_CASE */
1666void asymmetric_encryption_key_policy( int policy_usage,
1667 int policy_alg,
1668 int key_type,
1669 data_t *key_data,
1670 int exercise_alg )
1671{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001672 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001673 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001674 psa_status_t status;
1675 size_t key_bits;
1676 size_t buffer_length;
1677 unsigned char *buffer = NULL;
1678 size_t output_length;
1679
Gilles Peskine8817f612018-12-18 00:18:46 +01001680 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001681
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001682 psa_set_key_usage_flags( &attributes, policy_usage );
1683 psa_set_key_algorithm( &attributes, policy_alg );
1684 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001685
Gilles Peskine049c7532019-05-15 20:22:09 +02001686 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1687 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001688
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001689 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1690 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001691 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1692 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001693 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001694
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001695 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001696 NULL, 0,
1697 NULL, 0,
1698 buffer, buffer_length,
1699 &output_length );
1700 if( policy_alg == exercise_alg &&
1701 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001702 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001703 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001704 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001705
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001706 if( buffer_length != 0 )
1707 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001708 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001709 buffer, buffer_length,
1710 NULL, 0,
1711 buffer, buffer_length,
1712 &output_length );
1713 if( policy_alg == exercise_alg &&
1714 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001715 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001716 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001717 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001718
1719exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001720 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001721 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001722 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001723 mbedtls_free( buffer );
1724}
1725/* END_CASE */
1726
1727/* BEGIN_CASE */
1728void asymmetric_signature_key_policy( int policy_usage,
1729 int policy_alg,
1730 int key_type,
1731 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001732 int exercise_alg,
1733 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001734{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001735 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001736 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001737 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001738 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1739 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1740 * compatible with the policy and `payload_length_arg` is supposed to be
1741 * a valid input length to sign. If `payload_length_arg <= 0`,
1742 * `exercise_alg` is supposed to be forbidden by the policy. */
1743 int compatible_alg = payload_length_arg > 0;
1744 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001745 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1746 size_t signature_length;
1747
Gilles Peskine8817f612018-12-18 00:18:46 +01001748 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001749
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001750 psa_set_key_usage_flags( &attributes, policy_usage );
1751 psa_set_key_algorithm( &attributes, policy_alg );
1752 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001753
Gilles Peskine049c7532019-05-15 20:22:09 +02001754 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1755 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001756
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001757 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001758 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001759 signature, sizeof( signature ),
1760 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001761 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001762 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001763 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001764 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001765
1766 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001767 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001768 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001769 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001770 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001771 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001772 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001773 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001774
1775exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001776 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001777 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001778}
1779/* END_CASE */
1780
Janos Follathba3fab92019-06-11 14:50:16 +01001781/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001782void derive_key_policy( int policy_usage,
1783 int policy_alg,
1784 int key_type,
1785 data_t *key_data,
1786 int exercise_alg )
1787{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001788 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001789 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001790 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001791 psa_status_t status;
1792
Gilles Peskine8817f612018-12-18 00:18:46 +01001793 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001794
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001795 psa_set_key_usage_flags( &attributes, policy_usage );
1796 psa_set_key_algorithm( &attributes, policy_alg );
1797 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001798
Gilles Peskine049c7532019-05-15 20:22:09 +02001799 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1800 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001801
Janos Follathba3fab92019-06-11 14:50:16 +01001802 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1803
1804 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1805 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
1806 PSA_ASSERT( psa_key_derivation_input_bytes(
1807 &operation,
1808 PSA_KEY_DERIVATION_INPUT_SEED,
1809 (const uint8_t*) "", 0) );
1810
1811 status = psa_key_derivation_input_key( &operation,
1812 PSA_KEY_DERIVATION_INPUT_SECRET,
1813 handle );
1814
Gilles Peskineea0fb492018-07-12 17:17:20 +02001815 if( policy_alg == exercise_alg &&
1816 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001817 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001818 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001819 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001820
1821exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001822 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001823 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001824 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001825}
1826/* END_CASE */
1827
1828/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001829void agreement_key_policy( int policy_usage,
1830 int policy_alg,
1831 int key_type_arg,
1832 data_t *key_data,
1833 int exercise_alg )
1834{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001835 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001836 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001837 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001838 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001839 psa_status_t status;
1840
Gilles Peskine8817f612018-12-18 00:18:46 +01001841 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001842
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001843 psa_set_key_usage_flags( &attributes, policy_usage );
1844 psa_set_key_algorithm( &attributes, policy_alg );
1845 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001846
Gilles Peskine049c7532019-05-15 20:22:09 +02001847 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1848 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001849
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001850 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1851 status = key_agreement_with_self( &operation, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001852
Gilles Peskine01d718c2018-09-18 12:01:02 +02001853 if( policy_alg == exercise_alg &&
1854 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001855 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001856 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001857 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001858
1859exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001860 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001861 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001862 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001863}
1864/* END_CASE */
1865
1866/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001867void key_policy_alg2( int key_type_arg, data_t *key_data,
1868 int usage_arg, int alg_arg, int alg2_arg )
1869{
1870 psa_key_handle_t handle = 0;
1871 psa_key_type_t key_type = key_type_arg;
1872 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1873 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1874 psa_key_usage_t usage = usage_arg;
1875 psa_algorithm_t alg = alg_arg;
1876 psa_algorithm_t alg2 = alg2_arg;
1877
1878 PSA_ASSERT( psa_crypto_init( ) );
1879
1880 psa_set_key_usage_flags( &attributes, usage );
1881 psa_set_key_algorithm( &attributes, alg );
1882 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1883 psa_set_key_type( &attributes, key_type );
1884 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1885 &handle ) );
1886
1887 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1888 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1889 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1890 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1891
1892 if( ! exercise_key( handle, usage, alg ) )
1893 goto exit;
1894 if( ! exercise_key( handle, usage, alg2 ) )
1895 goto exit;
1896
1897exit:
1898 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001899 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001900}
1901/* END_CASE */
1902
1903/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001904void raw_agreement_key_policy( int policy_usage,
1905 int policy_alg,
1906 int key_type_arg,
1907 data_t *key_data,
1908 int exercise_alg )
1909{
1910 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001911 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001912 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001913 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001914 psa_status_t status;
1915
1916 PSA_ASSERT( psa_crypto_init( ) );
1917
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001918 psa_set_key_usage_flags( &attributes, policy_usage );
1919 psa_set_key_algorithm( &attributes, policy_alg );
1920 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001921
Gilles Peskine049c7532019-05-15 20:22:09 +02001922 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1923 &handle ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001924
1925 status = raw_key_agreement_with_self( exercise_alg, handle );
1926
1927 if( policy_alg == exercise_alg &&
1928 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1929 PSA_ASSERT( status );
1930 else
1931 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1932
1933exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001934 psa_key_derivation_abort( &operation );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001935 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001936 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001937}
1938/* END_CASE */
1939
1940/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001941void copy_success( int source_usage_arg,
1942 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001943 int type_arg, data_t *material,
1944 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001945 int target_usage_arg,
1946 int target_alg_arg, int target_alg2_arg,
1947 int expected_usage_arg,
1948 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001949{
Gilles Peskineca25db92019-04-19 11:43:08 +02001950 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1951 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001952 psa_key_usage_t expected_usage = expected_usage_arg;
1953 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001954 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02001955 psa_key_handle_t source_handle = 0;
1956 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001957 uint8_t *export_buffer = NULL;
1958
Gilles Peskine57ab7212019-01-28 13:03:09 +01001959 PSA_ASSERT( psa_crypto_init( ) );
1960
Gilles Peskineca25db92019-04-19 11:43:08 +02001961 /* Prepare the source key. */
1962 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1963 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001964 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001965 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001966 PSA_ASSERT( psa_import_key( &source_attributes,
1967 material->x, material->len,
1968 &source_handle ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001969 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001970
Gilles Peskineca25db92019-04-19 11:43:08 +02001971 /* Prepare the target attributes. */
1972 if( copy_attributes )
1973 target_attributes = source_attributes;
1974 if( target_usage_arg != -1 )
1975 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1976 if( target_alg_arg != -1 )
1977 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001978 if( target_alg2_arg != -1 )
1979 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001980
1981 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02001982 PSA_ASSERT( psa_copy_key( source_handle,
1983 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001984
1985 /* Destroy the source to ensure that this doesn't affect the target. */
1986 PSA_ASSERT( psa_destroy_key( source_handle ) );
1987
1988 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02001989 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
1990 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1991 psa_get_key_type( &target_attributes ) );
1992 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1993 psa_get_key_bits( &target_attributes ) );
1994 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1995 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001996 TEST_EQUAL( expected_alg2,
1997 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001998 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1999 {
2000 size_t length;
2001 ASSERT_ALLOC( export_buffer, material->len );
2002 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2003 material->len, &length ) );
2004 ASSERT_COMPARE( material->x, material->len,
2005 export_buffer, length );
2006 }
2007 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2008 goto exit;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002009 if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) )
2010 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002011
2012 PSA_ASSERT( psa_close_key( target_handle ) );
2013
2014exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002015 psa_reset_key_attributes( &source_attributes );
2016 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002017 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002018 mbedtls_free( export_buffer );
2019}
2020/* END_CASE */
2021
2022/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002023void copy_fail( int source_usage_arg,
2024 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002025 int type_arg, data_t *material,
2026 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002027 int target_usage_arg,
2028 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002029 int expected_status_arg )
2030{
2031 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2032 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2033 psa_key_handle_t source_handle = 0;
2034 psa_key_handle_t target_handle = 0;
2035
2036 PSA_ASSERT( psa_crypto_init( ) );
2037
2038 /* Prepare the source key. */
2039 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2040 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002041 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002042 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002043 PSA_ASSERT( psa_import_key( &source_attributes,
2044 material->x, material->len,
2045 &source_handle ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002046
2047 /* Prepare the target attributes. */
2048 psa_set_key_type( &target_attributes, target_type_arg );
2049 psa_set_key_bits( &target_attributes, target_bits_arg );
2050 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2051 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002052 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002053
2054 /* Try to copy the key. */
2055 TEST_EQUAL( psa_copy_key( source_handle,
2056 &target_attributes, &target_handle ),
2057 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002058
2059 PSA_ASSERT( psa_destroy_key( source_handle ) );
2060
Gilles Peskine4a644642019-05-03 17:14:08 +02002061exit:
2062 psa_reset_key_attributes( &source_attributes );
2063 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002064 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002065}
2066/* END_CASE */
2067
2068/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002069void hash_operation_init( )
2070{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002071 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002072 /* Test each valid way of initializing the object, except for `= {0}`, as
2073 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2074 * though it's OK by the C standard. We could test for this, but we'd need
2075 * to supress the Clang warning for the test. */
2076 psa_hash_operation_t func = psa_hash_operation_init( );
2077 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2078 psa_hash_operation_t zero;
2079
2080 memset( &zero, 0, sizeof( zero ) );
2081
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002082 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002083 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2084 PSA_ERROR_BAD_STATE );
2085 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2086 PSA_ERROR_BAD_STATE );
2087 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2088 PSA_ERROR_BAD_STATE );
2089
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002090 /* A default hash operation should be abortable without error. */
2091 PSA_ASSERT( psa_hash_abort( &func ) );
2092 PSA_ASSERT( psa_hash_abort( &init ) );
2093 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002094}
2095/* END_CASE */
2096
2097/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002098void hash_setup( int alg_arg,
2099 int expected_status_arg )
2100{
2101 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002102 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002103 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002104 psa_status_t status;
2105
Gilles Peskine8817f612018-12-18 00:18:46 +01002106 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002107
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002108 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002109 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002110
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002111 /* Whether setup succeeded or failed, abort must succeed. */
2112 PSA_ASSERT( psa_hash_abort( &operation ) );
2113
2114 /* If setup failed, reproduce the failure, so as to
2115 * test the resulting state of the operation object. */
2116 if( status != PSA_SUCCESS )
2117 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2118
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002119 /* Now the operation object should be reusable. */
2120#if defined(KNOWN_SUPPORTED_HASH_ALG)
2121 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2122 PSA_ASSERT( psa_hash_abort( &operation ) );
2123#endif
2124
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002125exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002126 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002127}
2128/* END_CASE */
2129
2130/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002131void hash_bad_order( )
2132{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002133 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002134 unsigned char input[] = "";
2135 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002136 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002137 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2138 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2139 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002140 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002141 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002142 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002143
Gilles Peskine8817f612018-12-18 00:18:46 +01002144 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002145
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002146 /* Call setup twice in a row. */
2147 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2148 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2149 PSA_ERROR_BAD_STATE );
2150 PSA_ASSERT( psa_hash_abort( &operation ) );
2151
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002152 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002153 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002154 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002155 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002156
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002157 /* Call update after finish. */
2158 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2159 PSA_ASSERT( psa_hash_finish( &operation,
2160 hash, sizeof( hash ), &hash_len ) );
2161 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002162 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002163 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002164
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002165 /* Call verify without calling setup beforehand. */
2166 TEST_EQUAL( psa_hash_verify( &operation,
2167 valid_hash, sizeof( valid_hash ) ),
2168 PSA_ERROR_BAD_STATE );
2169 PSA_ASSERT( psa_hash_abort( &operation ) );
2170
2171 /* Call verify after finish. */
2172 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2173 PSA_ASSERT( psa_hash_finish( &operation,
2174 hash, sizeof( hash ), &hash_len ) );
2175 TEST_EQUAL( psa_hash_verify( &operation,
2176 valid_hash, sizeof( valid_hash ) ),
2177 PSA_ERROR_BAD_STATE );
2178 PSA_ASSERT( psa_hash_abort( &operation ) );
2179
2180 /* Call verify twice in a row. */
2181 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2182 PSA_ASSERT( psa_hash_verify( &operation,
2183 valid_hash, sizeof( valid_hash ) ) );
2184 TEST_EQUAL( psa_hash_verify( &operation,
2185 valid_hash, sizeof( valid_hash ) ),
2186 PSA_ERROR_BAD_STATE );
2187 PSA_ASSERT( psa_hash_abort( &operation ) );
2188
2189 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002190 TEST_EQUAL( psa_hash_finish( &operation,
2191 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002192 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002193 PSA_ASSERT( psa_hash_abort( &operation ) );
2194
2195 /* Call finish twice in a row. */
2196 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2197 PSA_ASSERT( psa_hash_finish( &operation,
2198 hash, sizeof( hash ), &hash_len ) );
2199 TEST_EQUAL( psa_hash_finish( &operation,
2200 hash, sizeof( hash ), &hash_len ),
2201 PSA_ERROR_BAD_STATE );
2202 PSA_ASSERT( psa_hash_abort( &operation ) );
2203
2204 /* Call finish after calling verify. */
2205 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2206 PSA_ASSERT( psa_hash_verify( &operation,
2207 valid_hash, sizeof( valid_hash ) ) );
2208 TEST_EQUAL( psa_hash_finish( &operation,
2209 hash, sizeof( hash ), &hash_len ),
2210 PSA_ERROR_BAD_STATE );
2211 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002212
2213exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002214 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002215}
2216/* END_CASE */
2217
itayzafrir27e69452018-11-01 14:26:34 +02002218/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2219void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002220{
2221 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002222 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2223 * appended to it */
2224 unsigned char hash[] = {
2225 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2226 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2227 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002228 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002229 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002230
Gilles Peskine8817f612018-12-18 00:18:46 +01002231 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002232
itayzafrir27e69452018-11-01 14:26:34 +02002233 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002234 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002235 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002236 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002237
itayzafrir27e69452018-11-01 14:26:34 +02002238 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002239 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002240 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002241 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002242
itayzafrir27e69452018-11-01 14:26:34 +02002243 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002244 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002245 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002246 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002247
itayzafrirec93d302018-10-18 18:01:10 +03002248exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002249 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002250}
2251/* END_CASE */
2252
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002253/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2254void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002255{
2256 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002257 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002258 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002259 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002260 size_t hash_len;
2261
Gilles Peskine8817f612018-12-18 00:18:46 +01002262 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002263
itayzafrir58028322018-10-25 10:22:01 +03002264 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002265 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002266 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002267 hash, expected_size - 1, &hash_len ),
2268 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002269
2270exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002271 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002272}
2273/* END_CASE */
2274
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002275/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2276void hash_clone_source_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_source = PSA_HASH_OPERATION_INIT;
2281 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2282 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2283 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2284 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2285 size_t hash_len;
2286
2287 PSA_ASSERT( psa_crypto_init( ) );
2288 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2289
2290 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2291 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2292 PSA_ASSERT( psa_hash_finish( &op_finished,
2293 hash, sizeof( hash ), &hash_len ) );
2294 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2295 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2296
2297 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2298 PSA_ERROR_BAD_STATE );
2299
2300 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2301 PSA_ASSERT( psa_hash_finish( &op_init,
2302 hash, sizeof( hash ), &hash_len ) );
2303 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2304 PSA_ASSERT( psa_hash_finish( &op_finished,
2305 hash, sizeof( hash ), &hash_len ) );
2306 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2307 PSA_ASSERT( psa_hash_finish( &op_aborted,
2308 hash, sizeof( hash ), &hash_len ) );
2309
2310exit:
2311 psa_hash_abort( &op_source );
2312 psa_hash_abort( &op_init );
2313 psa_hash_abort( &op_setup );
2314 psa_hash_abort( &op_finished );
2315 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002316 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002317}
2318/* END_CASE */
2319
2320/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2321void hash_clone_target_state( )
2322{
2323 psa_algorithm_t alg = PSA_ALG_SHA_256;
2324 unsigned char hash[PSA_HASH_MAX_SIZE];
2325 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2326 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2327 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2328 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2329 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2330 size_t hash_len;
2331
2332 PSA_ASSERT( psa_crypto_init( ) );
2333
2334 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2335 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2336 PSA_ASSERT( psa_hash_finish( &op_finished,
2337 hash, sizeof( hash ), &hash_len ) );
2338 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2339 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2340
2341 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2342 PSA_ASSERT( psa_hash_finish( &op_target,
2343 hash, sizeof( hash ), &hash_len ) );
2344
2345 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2346 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2347 PSA_ERROR_BAD_STATE );
2348 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2349 PSA_ERROR_BAD_STATE );
2350
2351exit:
2352 psa_hash_abort( &op_target );
2353 psa_hash_abort( &op_init );
2354 psa_hash_abort( &op_setup );
2355 psa_hash_abort( &op_finished );
2356 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002357 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002358}
2359/* END_CASE */
2360
itayzafrir58028322018-10-25 10:22:01 +03002361/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002362void mac_operation_init( )
2363{
Jaeden Amero252ef282019-02-15 14:05:35 +00002364 const uint8_t input[1] = { 0 };
2365
Jaeden Amero769ce272019-01-04 11:48:03 +00002366 /* Test each valid way of initializing the object, except for `= {0}`, as
2367 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2368 * though it's OK by the C standard. We could test for this, but we'd need
2369 * to supress the Clang warning for the test. */
2370 psa_mac_operation_t func = psa_mac_operation_init( );
2371 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2372 psa_mac_operation_t zero;
2373
2374 memset( &zero, 0, sizeof( zero ) );
2375
Jaeden Amero252ef282019-02-15 14:05:35 +00002376 /* A freshly-initialized MAC operation should not be usable. */
2377 TEST_EQUAL( psa_mac_update( &func,
2378 input, sizeof( input ) ),
2379 PSA_ERROR_BAD_STATE );
2380 TEST_EQUAL( psa_mac_update( &init,
2381 input, sizeof( input ) ),
2382 PSA_ERROR_BAD_STATE );
2383 TEST_EQUAL( psa_mac_update( &zero,
2384 input, sizeof( input ) ),
2385 PSA_ERROR_BAD_STATE );
2386
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002387 /* A default MAC operation should be abortable without error. */
2388 PSA_ASSERT( psa_mac_abort( &func ) );
2389 PSA_ASSERT( psa_mac_abort( &init ) );
2390 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002391}
2392/* END_CASE */
2393
2394/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002395void mac_setup( int key_type_arg,
2396 data_t *key,
2397 int alg_arg,
2398 int expected_status_arg )
2399{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002400 psa_key_type_t key_type = key_type_arg;
2401 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002402 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002403 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002404 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2405#if defined(KNOWN_SUPPORTED_MAC_ALG)
2406 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2407#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002408
Gilles Peskine8817f612018-12-18 00:18:46 +01002409 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002410
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002411 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2412 &operation, &status ) )
2413 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002414 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002415
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002416 /* The operation object should be reusable. */
2417#if defined(KNOWN_SUPPORTED_MAC_ALG)
2418 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2419 smoke_test_key_data,
2420 sizeof( smoke_test_key_data ),
2421 KNOWN_SUPPORTED_MAC_ALG,
2422 &operation, &status ) )
2423 goto exit;
2424 TEST_EQUAL( status, PSA_SUCCESS );
2425#endif
2426
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002427exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002428 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002429}
2430/* END_CASE */
2431
2432/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002433void mac_bad_order( )
2434{
2435 psa_key_handle_t handle = 0;
2436 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2437 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2438 const uint8_t key[] = {
2439 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2440 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2441 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002442 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002443 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2444 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2445 size_t sign_mac_length = 0;
2446 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2447 const uint8_t verify_mac[] = {
2448 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2449 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2450 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2451
2452 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002453 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
2454 psa_set_key_algorithm( &attributes, alg );
2455 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002456
Gilles Peskine73676cb2019-05-15 20:15:10 +02002457 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002458
Jaeden Amero252ef282019-02-15 14:05:35 +00002459 /* Call update without calling setup beforehand. */
2460 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2461 PSA_ERROR_BAD_STATE );
2462 PSA_ASSERT( psa_mac_abort( &operation ) );
2463
2464 /* Call sign finish without calling setup beforehand. */
2465 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2466 &sign_mac_length),
2467 PSA_ERROR_BAD_STATE );
2468 PSA_ASSERT( psa_mac_abort( &operation ) );
2469
2470 /* Call verify finish without calling setup beforehand. */
2471 TEST_EQUAL( psa_mac_verify_finish( &operation,
2472 verify_mac, sizeof( verify_mac ) ),
2473 PSA_ERROR_BAD_STATE );
2474 PSA_ASSERT( psa_mac_abort( &operation ) );
2475
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002476 /* Call setup twice in a row. */
2477 PSA_ASSERT( psa_mac_sign_setup( &operation,
2478 handle, alg ) );
2479 TEST_EQUAL( psa_mac_sign_setup( &operation,
2480 handle, alg ),
2481 PSA_ERROR_BAD_STATE );
2482 PSA_ASSERT( psa_mac_abort( &operation ) );
2483
Jaeden Amero252ef282019-02-15 14:05:35 +00002484 /* Call update after sign finish. */
2485 PSA_ASSERT( psa_mac_sign_setup( &operation,
2486 handle, alg ) );
2487 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2488 PSA_ASSERT( psa_mac_sign_finish( &operation,
2489 sign_mac, sizeof( sign_mac ),
2490 &sign_mac_length ) );
2491 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2492 PSA_ERROR_BAD_STATE );
2493 PSA_ASSERT( psa_mac_abort( &operation ) );
2494
2495 /* Call update after verify finish. */
2496 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002497 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002498 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2499 PSA_ASSERT( psa_mac_verify_finish( &operation,
2500 verify_mac, sizeof( verify_mac ) ) );
2501 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2502 PSA_ERROR_BAD_STATE );
2503 PSA_ASSERT( psa_mac_abort( &operation ) );
2504
2505 /* Call sign finish twice in a row. */
2506 PSA_ASSERT( psa_mac_sign_setup( &operation,
2507 handle, alg ) );
2508 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2509 PSA_ASSERT( psa_mac_sign_finish( &operation,
2510 sign_mac, sizeof( sign_mac ),
2511 &sign_mac_length ) );
2512 TEST_EQUAL( psa_mac_sign_finish( &operation,
2513 sign_mac, sizeof( sign_mac ),
2514 &sign_mac_length ),
2515 PSA_ERROR_BAD_STATE );
2516 PSA_ASSERT( psa_mac_abort( &operation ) );
2517
2518 /* Call verify finish twice in a row. */
2519 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002520 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002521 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2522 PSA_ASSERT( psa_mac_verify_finish( &operation,
2523 verify_mac, sizeof( verify_mac ) ) );
2524 TEST_EQUAL( psa_mac_verify_finish( &operation,
2525 verify_mac, sizeof( verify_mac ) ),
2526 PSA_ERROR_BAD_STATE );
2527 PSA_ASSERT( psa_mac_abort( &operation ) );
2528
2529 /* Setup sign but try verify. */
2530 PSA_ASSERT( psa_mac_sign_setup( &operation,
2531 handle, alg ) );
2532 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2533 TEST_EQUAL( psa_mac_verify_finish( &operation,
2534 verify_mac, sizeof( verify_mac ) ),
2535 PSA_ERROR_BAD_STATE );
2536 PSA_ASSERT( psa_mac_abort( &operation ) );
2537
2538 /* Setup verify but try sign. */
2539 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002540 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002541 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2542 TEST_EQUAL( psa_mac_sign_finish( &operation,
2543 sign_mac, sizeof( sign_mac ),
2544 &sign_mac_length ),
2545 PSA_ERROR_BAD_STATE );
2546 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002547
Gilles Peskine76b29a72019-05-28 14:08:50 +02002548 PSA_ASSERT( psa_destroy_key( handle ) );
2549
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002550exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002551 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002552}
2553/* END_CASE */
2554
2555/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002556void mac_sign( int key_type_arg,
2557 data_t *key,
2558 int alg_arg,
2559 data_t *input,
2560 data_t *expected_mac )
2561{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002562 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002563 psa_key_type_t key_type = key_type_arg;
2564 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002565 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002566 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002567 /* Leave a little extra room in the output buffer. At the end of the
2568 * test, we'll check that the implementation didn't overwrite onto
2569 * this extra room. */
2570 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2571 size_t mac_buffer_size =
2572 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2573 size_t mac_length = 0;
2574
2575 memset( actual_mac, '+', sizeof( actual_mac ) );
2576 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2577 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2578
Gilles Peskine8817f612018-12-18 00:18:46 +01002579 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002580
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002581 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
2582 psa_set_key_algorithm( &attributes, alg );
2583 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002584
Gilles Peskine73676cb2019-05-15 20:15:10 +02002585 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002586
2587 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002588 PSA_ASSERT( psa_mac_sign_setup( &operation,
2589 handle, alg ) );
2590 PSA_ASSERT( psa_mac_update( &operation,
2591 input->x, input->len ) );
2592 PSA_ASSERT( psa_mac_sign_finish( &operation,
2593 actual_mac, mac_buffer_size,
2594 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002595
2596 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002597 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2598 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002599
2600 /* Verify that the end of the buffer is untouched. */
2601 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2602 sizeof( actual_mac ) - mac_length ) );
2603
2604exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002605 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002606 PSA_DONE( );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002607}
2608/* END_CASE */
2609
2610/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002611void mac_verify( int key_type_arg,
2612 data_t *key,
2613 int alg_arg,
2614 data_t *input,
2615 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002616{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002617 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002618 psa_key_type_t key_type = key_type_arg;
2619 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002620 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002621 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002622
Gilles Peskine69c12672018-06-28 00:07:19 +02002623 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2624
Gilles Peskine8817f612018-12-18 00:18:46 +01002625 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002626
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002627 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
2628 psa_set_key_algorithm( &attributes, alg );
2629 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002630
Gilles Peskine73676cb2019-05-15 20:15:10 +02002631 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002632
Gilles Peskine8817f612018-12-18 00:18:46 +01002633 PSA_ASSERT( psa_mac_verify_setup( &operation,
2634 handle, alg ) );
2635 PSA_ASSERT( psa_destroy_key( handle ) );
2636 PSA_ASSERT( psa_mac_update( &operation,
2637 input->x, input->len ) );
2638 PSA_ASSERT( psa_mac_verify_finish( &operation,
2639 expected_mac->x,
2640 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002641
2642exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002643 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002644 PSA_DONE( );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002645}
2646/* END_CASE */
2647
2648/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002649void cipher_operation_init( )
2650{
Jaeden Ameroab439972019-02-15 14:12:05 +00002651 const uint8_t input[1] = { 0 };
2652 unsigned char output[1] = { 0 };
2653 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002654 /* Test each valid way of initializing the object, except for `= {0}`, as
2655 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2656 * though it's OK by the C standard. We could test for this, but we'd need
2657 * to supress the Clang warning for the test. */
2658 psa_cipher_operation_t func = psa_cipher_operation_init( );
2659 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2660 psa_cipher_operation_t zero;
2661
2662 memset( &zero, 0, sizeof( zero ) );
2663
Jaeden Ameroab439972019-02-15 14:12:05 +00002664 /* A freshly-initialized cipher operation should not be usable. */
2665 TEST_EQUAL( psa_cipher_update( &func,
2666 input, sizeof( input ),
2667 output, sizeof( output ),
2668 &output_length ),
2669 PSA_ERROR_BAD_STATE );
2670 TEST_EQUAL( psa_cipher_update( &init,
2671 input, sizeof( input ),
2672 output, sizeof( output ),
2673 &output_length ),
2674 PSA_ERROR_BAD_STATE );
2675 TEST_EQUAL( psa_cipher_update( &zero,
2676 input, sizeof( input ),
2677 output, sizeof( output ),
2678 &output_length ),
2679 PSA_ERROR_BAD_STATE );
2680
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002681 /* A default cipher operation should be abortable without error. */
2682 PSA_ASSERT( psa_cipher_abort( &func ) );
2683 PSA_ASSERT( psa_cipher_abort( &init ) );
2684 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002685}
2686/* END_CASE */
2687
2688/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002689void cipher_setup( int key_type_arg,
2690 data_t *key,
2691 int alg_arg,
2692 int expected_status_arg )
2693{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002694 psa_key_type_t key_type = key_type_arg;
2695 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002696 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002697 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002698 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002699#if defined(KNOWN_SUPPORTED_MAC_ALG)
2700 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2701#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002702
Gilles Peskine8817f612018-12-18 00:18:46 +01002703 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002704
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002705 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2706 &operation, &status ) )
2707 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002708 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002709
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002710 /* The operation object should be reusable. */
2711#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2712 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2713 smoke_test_key_data,
2714 sizeof( smoke_test_key_data ),
2715 KNOWN_SUPPORTED_CIPHER_ALG,
2716 &operation, &status ) )
2717 goto exit;
2718 TEST_EQUAL( status, PSA_SUCCESS );
2719#endif
2720
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002721exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002722 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002723}
2724/* END_CASE */
2725
2726/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002727void cipher_bad_order( )
2728{
2729 psa_key_handle_t handle = 0;
2730 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2731 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002732 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002733 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2734 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2735 const uint8_t key[] = {
2736 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2737 0xaa, 0xaa, 0xaa, 0xaa };
2738 const uint8_t text[] = {
2739 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2740 0xbb, 0xbb, 0xbb, 0xbb };
2741 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2742 size_t length = 0;
2743
2744 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002745 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2746 psa_set_key_algorithm( &attributes, alg );
2747 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02002748 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002749
2750
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002751 /* Call encrypt setup twice in a row. */
2752 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2753 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2754 PSA_ERROR_BAD_STATE );
2755 PSA_ASSERT( psa_cipher_abort( &operation ) );
2756
2757 /* Call decrypt setup twice in a row. */
2758 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2759 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2760 PSA_ERROR_BAD_STATE );
2761 PSA_ASSERT( psa_cipher_abort( &operation ) );
2762
Jaeden Ameroab439972019-02-15 14:12:05 +00002763 /* Generate an IV without calling setup beforehand. */
2764 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2765 buffer, sizeof( buffer ),
2766 &length ),
2767 PSA_ERROR_BAD_STATE );
2768 PSA_ASSERT( psa_cipher_abort( &operation ) );
2769
2770 /* Generate an IV twice in a row. */
2771 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2772 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2773 buffer, sizeof( buffer ),
2774 &length ) );
2775 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2776 buffer, sizeof( buffer ),
2777 &length ),
2778 PSA_ERROR_BAD_STATE );
2779 PSA_ASSERT( psa_cipher_abort( &operation ) );
2780
2781 /* Generate an IV after it's already set. */
2782 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2783 PSA_ASSERT( psa_cipher_set_iv( &operation,
2784 iv, sizeof( iv ) ) );
2785 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2786 buffer, sizeof( buffer ),
2787 &length ),
2788 PSA_ERROR_BAD_STATE );
2789 PSA_ASSERT( psa_cipher_abort( &operation ) );
2790
2791 /* Set an IV without calling setup beforehand. */
2792 TEST_EQUAL( psa_cipher_set_iv( &operation,
2793 iv, sizeof( iv ) ),
2794 PSA_ERROR_BAD_STATE );
2795 PSA_ASSERT( psa_cipher_abort( &operation ) );
2796
2797 /* Set an IV after it's already set. */
2798 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2799 PSA_ASSERT( psa_cipher_set_iv( &operation,
2800 iv, sizeof( iv ) ) );
2801 TEST_EQUAL( psa_cipher_set_iv( &operation,
2802 iv, sizeof( iv ) ),
2803 PSA_ERROR_BAD_STATE );
2804 PSA_ASSERT( psa_cipher_abort( &operation ) );
2805
2806 /* Set an IV after it's already generated. */
2807 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2808 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2809 buffer, sizeof( buffer ),
2810 &length ) );
2811 TEST_EQUAL( psa_cipher_set_iv( &operation,
2812 iv, sizeof( iv ) ),
2813 PSA_ERROR_BAD_STATE );
2814 PSA_ASSERT( psa_cipher_abort( &operation ) );
2815
2816 /* Call update without calling setup beforehand. */
2817 TEST_EQUAL( psa_cipher_update( &operation,
2818 text, sizeof( text ),
2819 buffer, sizeof( buffer ),
2820 &length ),
2821 PSA_ERROR_BAD_STATE );
2822 PSA_ASSERT( psa_cipher_abort( &operation ) );
2823
2824 /* Call update without an IV where an IV is required. */
2825 TEST_EQUAL( psa_cipher_update( &operation,
2826 text, sizeof( text ),
2827 buffer, sizeof( buffer ),
2828 &length ),
2829 PSA_ERROR_BAD_STATE );
2830 PSA_ASSERT( psa_cipher_abort( &operation ) );
2831
2832 /* Call update after finish. */
2833 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2834 PSA_ASSERT( psa_cipher_set_iv( &operation,
2835 iv, sizeof( iv ) ) );
2836 PSA_ASSERT( psa_cipher_finish( &operation,
2837 buffer, sizeof( buffer ), &length ) );
2838 TEST_EQUAL( psa_cipher_update( &operation,
2839 text, sizeof( text ),
2840 buffer, sizeof( buffer ),
2841 &length ),
2842 PSA_ERROR_BAD_STATE );
2843 PSA_ASSERT( psa_cipher_abort( &operation ) );
2844
2845 /* Call finish without calling setup beforehand. */
2846 TEST_EQUAL( psa_cipher_finish( &operation,
2847 buffer, sizeof( buffer ), &length ),
2848 PSA_ERROR_BAD_STATE );
2849 PSA_ASSERT( psa_cipher_abort( &operation ) );
2850
2851 /* Call finish without an IV where an IV is required. */
2852 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2853 /* Not calling update means we are encrypting an empty buffer, which is OK
2854 * for cipher modes with padding. */
2855 TEST_EQUAL( psa_cipher_finish( &operation,
2856 buffer, sizeof( buffer ), &length ),
2857 PSA_ERROR_BAD_STATE );
2858 PSA_ASSERT( psa_cipher_abort( &operation ) );
2859
2860 /* Call finish twice in a row. */
2861 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2862 PSA_ASSERT( psa_cipher_set_iv( &operation,
2863 iv, sizeof( iv ) ) );
2864 PSA_ASSERT( psa_cipher_finish( &operation,
2865 buffer, sizeof( buffer ), &length ) );
2866 TEST_EQUAL( psa_cipher_finish( &operation,
2867 buffer, sizeof( buffer ), &length ),
2868 PSA_ERROR_BAD_STATE );
2869 PSA_ASSERT( psa_cipher_abort( &operation ) );
2870
Gilles Peskine76b29a72019-05-28 14:08:50 +02002871 PSA_ASSERT( psa_destroy_key( handle ) );
2872
Jaeden Ameroab439972019-02-15 14:12:05 +00002873exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002874 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002875}
2876/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002877
Gilles Peskine50e586b2018-06-08 14:28:46 +02002878/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002879void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02002880 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002881 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002882 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002883{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002884 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002885 psa_status_t status;
2886 psa_key_type_t key_type = key_type_arg;
2887 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002888 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002889 unsigned char *output = NULL;
2890 size_t output_buffer_size = 0;
2891 size_t function_output_length = 0;
2892 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002893 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002894 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002895
Gilles Peskine8817f612018-12-18 00:18:46 +01002896 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002897
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002898 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2899 psa_set_key_algorithm( &attributes, alg );
2900 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002901
Gilles Peskine73676cb2019-05-15 20:15:10 +02002902 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002903
Gilles Peskine8817f612018-12-18 00:18:46 +01002904 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2905 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002906
Gilles Peskine423005e2019-05-06 15:22:57 +02002907 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002908 output_buffer_size = ( (size_t) input->len +
2909 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002910 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002911
Gilles Peskine8817f612018-12-18 00:18:46 +01002912 PSA_ASSERT( psa_cipher_update( &operation,
2913 input->x, input->len,
2914 output, output_buffer_size,
2915 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002916 total_output_length += function_output_length;
2917 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002918 output + total_output_length,
2919 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002920 &function_output_length );
2921 total_output_length += function_output_length;
2922
Gilles Peskinefe11b722018-12-18 00:24:04 +01002923 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002924 if( expected_status == PSA_SUCCESS )
2925 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002926 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002927 ASSERT_COMPARE( expected_output->x, expected_output->len,
2928 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002929 }
2930
2931exit:
2932 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002933 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002934 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002935}
2936/* END_CASE */
2937
2938/* BEGIN_CASE */
2939void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02002940 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002941 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002942 int first_part_size_arg,
2943 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002944 data_t *expected_output )
2945{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002946 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002947 psa_key_type_t key_type = key_type_arg;
2948 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002949 size_t first_part_size = first_part_size_arg;
2950 size_t output1_length = output1_length_arg;
2951 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002952 unsigned char *output = NULL;
2953 size_t output_buffer_size = 0;
2954 size_t function_output_length = 0;
2955 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002956 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002957 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002958
Gilles Peskine8817f612018-12-18 00:18:46 +01002959 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002960
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002961 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2962 psa_set_key_algorithm( &attributes, alg );
2963 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002964
Gilles Peskine73676cb2019-05-15 20:15:10 +02002965 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002966
Gilles Peskine8817f612018-12-18 00:18:46 +01002967 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2968 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002969
Gilles Peskine423005e2019-05-06 15:22:57 +02002970 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002971 output_buffer_size = ( (size_t) input->len +
2972 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002973 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002974
Gilles Peskinee0866522019-02-19 19:44:00 +01002975 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002976 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2977 output, output_buffer_size,
2978 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002979 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002980 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002981 PSA_ASSERT( psa_cipher_update( &operation,
2982 input->x + first_part_size,
2983 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002984 output + total_output_length,
2985 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002986 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002987 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002988 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002989 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002990 output + total_output_length,
2991 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002992 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002993 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002994 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002995
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002996 ASSERT_COMPARE( expected_output->x, expected_output->len,
2997 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002998
2999exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003000 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003001 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003002 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003003}
3004/* END_CASE */
3005
3006/* BEGIN_CASE */
3007void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003008 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003009 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003010 int first_part_size_arg,
3011 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003012 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003013{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003014 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003015
3016 psa_key_type_t key_type = key_type_arg;
3017 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003018 size_t first_part_size = first_part_size_arg;
3019 size_t output1_length = output1_length_arg;
3020 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003021 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003022 size_t output_buffer_size = 0;
3023 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003024 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003025 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003026 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003027
Gilles Peskine8817f612018-12-18 00:18:46 +01003028 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003029
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003030 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3031 psa_set_key_algorithm( &attributes, alg );
3032 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003033
Gilles Peskine73676cb2019-05-15 20:15:10 +02003034 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003035
Gilles Peskine8817f612018-12-18 00:18:46 +01003036 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3037 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003038
Gilles Peskine423005e2019-05-06 15:22:57 +02003039 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003040
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003041 output_buffer_size = ( (size_t) input->len +
3042 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003043 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003044
Gilles Peskinee0866522019-02-19 19:44:00 +01003045 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003046 PSA_ASSERT( psa_cipher_update( &operation,
3047 input->x, first_part_size,
3048 output, output_buffer_size,
3049 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003050 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003051 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003052 PSA_ASSERT( psa_cipher_update( &operation,
3053 input->x + first_part_size,
3054 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003055 output + total_output_length,
3056 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003057 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003058 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003059 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003060 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003061 output + total_output_length,
3062 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003063 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003064 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003065 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003066
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003067 ASSERT_COMPARE( expected_output->x, expected_output->len,
3068 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003069
3070exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003071 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003072 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003073 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003074}
3075/* END_CASE */
3076
Gilles Peskine50e586b2018-06-08 14:28:46 +02003077/* BEGIN_CASE */
3078void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003079 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003080 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003081 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003082{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003083 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003084 psa_status_t status;
3085 psa_key_type_t key_type = key_type_arg;
3086 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003087 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003088 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003089 size_t output_buffer_size = 0;
3090 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003091 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003092 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003093 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003094
Gilles Peskine8817f612018-12-18 00:18:46 +01003095 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003096
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003097 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3098 psa_set_key_algorithm( &attributes, alg );
3099 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003100
Gilles Peskine73676cb2019-05-15 20:15:10 +02003101 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003102
Gilles Peskine8817f612018-12-18 00:18:46 +01003103 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3104 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003105
Gilles Peskine423005e2019-05-06 15:22:57 +02003106 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003107
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003108 output_buffer_size = ( (size_t) input->len +
3109 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003110 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003111
Gilles Peskine8817f612018-12-18 00:18:46 +01003112 PSA_ASSERT( psa_cipher_update( &operation,
3113 input->x, input->len,
3114 output, output_buffer_size,
3115 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003116 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003117 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003118 output + total_output_length,
3119 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003120 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003121 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003122 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003123
3124 if( expected_status == PSA_SUCCESS )
3125 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003126 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003127 ASSERT_COMPARE( expected_output->x, expected_output->len,
3128 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003129 }
3130
Gilles Peskine50e586b2018-06-08 14:28:46 +02003131exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003132 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003133 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003134 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003135}
3136/* END_CASE */
3137
Gilles Peskine50e586b2018-06-08 14:28:46 +02003138/* BEGIN_CASE */
3139void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003140 data_t *key,
3141 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003142{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003143 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003144 psa_key_type_t key_type = key_type_arg;
3145 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003146 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003147 size_t iv_size = 16;
3148 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003149 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003150 size_t output1_size = 0;
3151 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003152 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003153 size_t output2_size = 0;
3154 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003155 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003156 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3157 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003158 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003159
Gilles Peskine8817f612018-12-18 00:18:46 +01003160 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003161
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003162 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3163 psa_set_key_algorithm( &attributes, alg );
3164 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003165
Gilles Peskine73676cb2019-05-15 20:15:10 +02003166 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003167
Gilles Peskine8817f612018-12-18 00:18:46 +01003168 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3169 handle, alg ) );
3170 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3171 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003172
Gilles Peskine8817f612018-12-18 00:18:46 +01003173 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3174 iv, iv_size,
3175 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003176 output1_size = ( (size_t) input->len +
3177 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003178 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003179
Gilles Peskine8817f612018-12-18 00:18:46 +01003180 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3181 output1, output1_size,
3182 &output1_length ) );
3183 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003184 output1 + output1_length,
3185 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003186 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003187
Gilles Peskine048b7f02018-06-08 14:20:49 +02003188 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003189
Gilles Peskine8817f612018-12-18 00:18:46 +01003190 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003191
3192 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003193 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003194
Gilles Peskine8817f612018-12-18 00:18:46 +01003195 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3196 iv, iv_length ) );
3197 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3198 output2, output2_size,
3199 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003200 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003201 PSA_ASSERT( psa_cipher_finish( &operation2,
3202 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003203 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003204 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003205
Gilles Peskine048b7f02018-06-08 14:20:49 +02003206 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003207
Gilles Peskine8817f612018-12-18 00:18:46 +01003208 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003209
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003210 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003211
3212exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003213 mbedtls_free( output1 );
3214 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003215 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003216 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003217}
3218/* END_CASE */
3219
3220/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003221void cipher_verify_output_multipart( int alg_arg,
3222 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003223 data_t *key,
3224 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003225 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003226{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003227 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003228 psa_key_type_t key_type = key_type_arg;
3229 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003230 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003231 unsigned char iv[16] = {0};
3232 size_t iv_size = 16;
3233 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003234 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003235 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003236 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003237 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003238 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003239 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003240 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003241 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3242 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003243 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003244
Gilles Peskine8817f612018-12-18 00:18:46 +01003245 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003246
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003247 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3248 psa_set_key_algorithm( &attributes, alg );
3249 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003250
Gilles Peskine73676cb2019-05-15 20:15:10 +02003251 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003252
Gilles Peskine8817f612018-12-18 00:18:46 +01003253 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3254 handle, alg ) );
3255 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3256 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003257
Gilles Peskine8817f612018-12-18 00:18:46 +01003258 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3259 iv, iv_size,
3260 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003261 output1_buffer_size = ( (size_t) input->len +
3262 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003263 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003264
Gilles Peskinee0866522019-02-19 19:44:00 +01003265 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003266
Gilles Peskine8817f612018-12-18 00:18:46 +01003267 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3268 output1, output1_buffer_size,
3269 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003270 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003271
Gilles Peskine8817f612018-12-18 00:18:46 +01003272 PSA_ASSERT( psa_cipher_update( &operation1,
3273 input->x + first_part_size,
3274 input->len - first_part_size,
3275 output1, output1_buffer_size,
3276 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003277 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003278
Gilles Peskine8817f612018-12-18 00:18:46 +01003279 PSA_ASSERT( psa_cipher_finish( &operation1,
3280 output1 + output1_length,
3281 output1_buffer_size - output1_length,
3282 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003283 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003284
Gilles Peskine8817f612018-12-18 00:18:46 +01003285 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003286
Gilles Peskine048b7f02018-06-08 14:20:49 +02003287 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003288 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003289
Gilles Peskine8817f612018-12-18 00:18:46 +01003290 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3291 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003292
Gilles Peskine8817f612018-12-18 00:18:46 +01003293 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3294 output2, output2_buffer_size,
3295 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003296 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003297
Gilles Peskine8817f612018-12-18 00:18:46 +01003298 PSA_ASSERT( psa_cipher_update( &operation2,
3299 output1 + first_part_size,
3300 output1_length - first_part_size,
3301 output2, output2_buffer_size,
3302 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003303 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003304
Gilles Peskine8817f612018-12-18 00:18:46 +01003305 PSA_ASSERT( psa_cipher_finish( &operation2,
3306 output2 + output2_length,
3307 output2_buffer_size - output2_length,
3308 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003309 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003310
Gilles Peskine8817f612018-12-18 00:18:46 +01003311 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003312
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003313 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003314
3315exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003316 mbedtls_free( output1 );
3317 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003318 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003319 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003320}
3321/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003322
Gilles Peskine20035e32018-02-03 22:44:14 +01003323/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003324void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003325 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003326 data_t *nonce,
3327 data_t *additional_data,
3328 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003329 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003330{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003331 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003332 psa_key_type_t key_type = key_type_arg;
3333 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003334 unsigned char *output_data = NULL;
3335 size_t output_size = 0;
3336 size_t output_length = 0;
3337 unsigned char *output_data2 = NULL;
3338 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003339 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003340 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003341 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003342
Gilles Peskine4abf7412018-06-18 16:35:34 +02003343 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003344 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3345 * should be exact. */
3346 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3347 TEST_EQUAL( output_size,
3348 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003349 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003350
Gilles Peskine8817f612018-12-18 00:18:46 +01003351 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003352
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003353 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3354 psa_set_key_algorithm( &attributes, alg );
3355 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003356
Gilles Peskine049c7532019-05-15 20:22:09 +02003357 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3358 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003359
Gilles Peskinefe11b722018-12-18 00:24:04 +01003360 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3361 nonce->x, nonce->len,
3362 additional_data->x,
3363 additional_data->len,
3364 input_data->x, input_data->len,
3365 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003366 &output_length ),
3367 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003368
3369 if( PSA_SUCCESS == expected_result )
3370 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003371 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003372
Gilles Peskine003a4a92019-05-14 16:09:40 +02003373 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3374 * should be exact. */
3375 TEST_EQUAL( input_data->len,
3376 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3377
Gilles Peskinefe11b722018-12-18 00:24:04 +01003378 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3379 nonce->x, nonce->len,
3380 additional_data->x,
3381 additional_data->len,
3382 output_data, output_length,
3383 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003384 &output_length2 ),
3385 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003386
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003387 ASSERT_COMPARE( input_data->x, input_data->len,
3388 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003389 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003390
Gilles Peskinea1cac842018-06-11 19:33:02 +02003391exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003392 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003393 mbedtls_free( output_data );
3394 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003395 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003396}
3397/* END_CASE */
3398
3399/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003400void aead_encrypt( int key_type_arg, data_t *key_data,
3401 int alg_arg,
3402 data_t *nonce,
3403 data_t *additional_data,
3404 data_t *input_data,
3405 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003406{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003407 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003408 psa_key_type_t key_type = key_type_arg;
3409 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003410 unsigned char *output_data = NULL;
3411 size_t output_size = 0;
3412 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003413 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003414 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003415
Gilles Peskine4abf7412018-06-18 16:35:34 +02003416 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003417 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3418 * should be exact. */
3419 TEST_EQUAL( output_size,
3420 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003421 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003422
Gilles Peskine8817f612018-12-18 00:18:46 +01003423 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003424
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003425 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3426 psa_set_key_algorithm( &attributes, alg );
3427 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003428
Gilles Peskine049c7532019-05-15 20:22:09 +02003429 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3430 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003431
Gilles Peskine8817f612018-12-18 00:18:46 +01003432 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3433 nonce->x, nonce->len,
3434 additional_data->x, additional_data->len,
3435 input_data->x, input_data->len,
3436 output_data, output_size,
3437 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003438
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003439 ASSERT_COMPARE( expected_result->x, expected_result->len,
3440 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003441
Gilles Peskinea1cac842018-06-11 19:33:02 +02003442exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003443 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003444 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003445 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003446}
3447/* END_CASE */
3448
3449/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003450void aead_decrypt( int key_type_arg, data_t *key_data,
3451 int alg_arg,
3452 data_t *nonce,
3453 data_t *additional_data,
3454 data_t *input_data,
3455 data_t *expected_data,
3456 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003457{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003458 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003459 psa_key_type_t key_type = key_type_arg;
3460 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003461 unsigned char *output_data = NULL;
3462 size_t output_size = 0;
3463 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003464 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003465 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003466 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003467
Gilles Peskine003a4a92019-05-14 16:09:40 +02003468 output_size = input_data->len - tag_length;
3469 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3470 * should be exact. */
3471 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3472 TEST_EQUAL( output_size,
3473 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003474 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003475
Gilles Peskine8817f612018-12-18 00:18:46 +01003476 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003477
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003478 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3479 psa_set_key_algorithm( &attributes, alg );
3480 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003481
Gilles Peskine049c7532019-05-15 20:22:09 +02003482 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3483 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003484
Gilles Peskinefe11b722018-12-18 00:24:04 +01003485 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3486 nonce->x, nonce->len,
3487 additional_data->x,
3488 additional_data->len,
3489 input_data->x, input_data->len,
3490 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003491 &output_length ),
3492 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003493
Gilles Peskine2d277862018-06-18 15:41:12 +02003494 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003495 ASSERT_COMPARE( expected_data->x, expected_data->len,
3496 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003497
Gilles Peskinea1cac842018-06-11 19:33:02 +02003498exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003499 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003500 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003501 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003502}
3503/* END_CASE */
3504
3505/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003506void signature_size( int type_arg,
3507 int bits,
3508 int alg_arg,
3509 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003510{
3511 psa_key_type_t type = type_arg;
3512 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003513 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003514 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003515exit:
3516 ;
3517}
3518/* END_CASE */
3519
3520/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003521void sign_deterministic( int key_type_arg, data_t *key_data,
3522 int alg_arg, data_t *input_data,
3523 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003524{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003525 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003526 psa_key_type_t key_type = key_type_arg;
3527 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003528 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003529 unsigned char *signature = NULL;
3530 size_t signature_size;
3531 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003532 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003533
Gilles Peskine8817f612018-12-18 00:18:46 +01003534 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003535
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003536 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3537 psa_set_key_algorithm( &attributes, alg );
3538 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003539
Gilles Peskine049c7532019-05-15 20:22:09 +02003540 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3541 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003542 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3543 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003544
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003545 /* Allocate a buffer which has the size advertized by the
3546 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003547 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3548 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003549 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003550 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003551 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003552
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003553 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003554 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3555 input_data->x, input_data->len,
3556 signature, signature_size,
3557 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003558 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003559 ASSERT_COMPARE( output_data->x, output_data->len,
3560 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003561
3562exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003563 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003564 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003565 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003566 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003567}
3568/* END_CASE */
3569
3570/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003571void sign_fail( int key_type_arg, data_t *key_data,
3572 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003573 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003574{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003575 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003576 psa_key_type_t key_type = key_type_arg;
3577 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003578 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003579 psa_status_t actual_status;
3580 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003581 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003582 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003583 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003584
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003585 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003586
Gilles Peskine8817f612018-12-18 00:18:46 +01003587 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003588
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003589 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3590 psa_set_key_algorithm( &attributes, alg );
3591 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003592
Gilles Peskine049c7532019-05-15 20:22:09 +02003593 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3594 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003595
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003596 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003597 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003598 signature, signature_size,
3599 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003600 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003601 /* The value of *signature_length is unspecified on error, but
3602 * whatever it is, it should be less than signature_size, so that
3603 * if the caller tries to read *signature_length bytes without
3604 * checking the error code then they don't overflow a buffer. */
3605 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003606
3607exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003608 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003609 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003610 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003611 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003612}
3613/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003614
3615/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003616void sign_verify( int key_type_arg, data_t *key_data,
3617 int alg_arg, data_t *input_data )
3618{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003619 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003620 psa_key_type_t key_type = key_type_arg;
3621 psa_algorithm_t alg = alg_arg;
3622 size_t key_bits;
3623 unsigned char *signature = NULL;
3624 size_t signature_size;
3625 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003626 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003627
Gilles Peskine8817f612018-12-18 00:18:46 +01003628 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003629
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003630 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
3631 psa_set_key_algorithm( &attributes, alg );
3632 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003633
Gilles Peskine049c7532019-05-15 20:22:09 +02003634 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3635 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003636 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3637 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003638
3639 /* Allocate a buffer which has the size advertized by the
3640 * library. */
3641 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3642 key_bits, alg );
3643 TEST_ASSERT( signature_size != 0 );
3644 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003645 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003646
3647 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003648 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3649 input_data->x, input_data->len,
3650 signature, signature_size,
3651 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003652 /* Check that the signature length looks sensible. */
3653 TEST_ASSERT( signature_length <= signature_size );
3654 TEST_ASSERT( signature_length > 0 );
3655
3656 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003657 PSA_ASSERT( psa_asymmetric_verify(
3658 handle, alg,
3659 input_data->x, input_data->len,
3660 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003661
3662 if( input_data->len != 0 )
3663 {
3664 /* Flip a bit in the input and verify that the signature is now
3665 * detected as invalid. Flip a bit at the beginning, not at the end,
3666 * because ECDSA may ignore the last few bits of the input. */
3667 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003668 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3669 input_data->x, input_data->len,
3670 signature, signature_length ),
3671 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003672 }
3673
3674exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003675 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003676 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003677 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003678 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003679}
3680/* END_CASE */
3681
3682/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003683void asymmetric_verify( int key_type_arg, data_t *key_data,
3684 int alg_arg, data_t *hash_data,
3685 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003686{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003687 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003688 psa_key_type_t key_type = key_type_arg;
3689 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003690 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003691
Gilles Peskine69c12672018-06-28 00:07:19 +02003692 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3693
Gilles Peskine8817f612018-12-18 00:18:46 +01003694 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003695
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003696 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3697 psa_set_key_algorithm( &attributes, alg );
3698 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003699
Gilles Peskine049c7532019-05-15 20:22:09 +02003700 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3701 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03003702
Gilles Peskine8817f612018-12-18 00:18:46 +01003703 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3704 hash_data->x, hash_data->len,
3705 signature_data->x,
3706 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003707exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003708 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003709 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003710 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003711}
3712/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003713
3714/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003715void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3716 int alg_arg, data_t *hash_data,
3717 data_t *signature_data,
3718 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003719{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003720 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003721 psa_key_type_t key_type = key_type_arg;
3722 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003723 psa_status_t actual_status;
3724 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003725 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003726
Gilles Peskine8817f612018-12-18 00:18:46 +01003727 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003728
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003729 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3730 psa_set_key_algorithm( &attributes, alg );
3731 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003732
Gilles Peskine049c7532019-05-15 20:22:09 +02003733 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3734 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003735
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003736 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003737 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003738 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003739 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003740
Gilles Peskinefe11b722018-12-18 00:24:04 +01003741 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003742
3743exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003744 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003745 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003746 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003747}
3748/* END_CASE */
3749
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003750/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003751void asymmetric_encrypt( int key_type_arg,
3752 data_t *key_data,
3753 int alg_arg,
3754 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003755 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003756 int expected_output_length_arg,
3757 int expected_status_arg )
3758{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003759 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003760 psa_key_type_t key_type = key_type_arg;
3761 psa_algorithm_t alg = alg_arg;
3762 size_t expected_output_length = expected_output_length_arg;
3763 size_t key_bits;
3764 unsigned char *output = NULL;
3765 size_t output_size;
3766 size_t output_length = ~0;
3767 psa_status_t actual_status;
3768 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003769 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003770
Gilles Peskine8817f612018-12-18 00:18:46 +01003771 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003772
Gilles Peskine656896e2018-06-29 19:12:28 +02003773 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003774 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3775 psa_set_key_algorithm( &attributes, alg );
3776 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003777 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3778 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003779
3780 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003781 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3782 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02003783 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003784 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003785
3786 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003787 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003788 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003789 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003790 output, output_size,
3791 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003792 TEST_EQUAL( actual_status, expected_status );
3793 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003794
Gilles Peskine68428122018-06-30 18:42:41 +02003795 /* If the label is empty, the test framework puts a non-null pointer
3796 * in label->x. Test that a null pointer works as well. */
3797 if( label->len == 0 )
3798 {
3799 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003800 if( output_size != 0 )
3801 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003802 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003803 input_data->x, input_data->len,
3804 NULL, label->len,
3805 output, output_size,
3806 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003807 TEST_EQUAL( actual_status, expected_status );
3808 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003809 }
3810
Gilles Peskine656896e2018-06-29 19:12:28 +02003811exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003812 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003813 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003814 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003815 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003816}
3817/* END_CASE */
3818
3819/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003820void asymmetric_encrypt_decrypt( int key_type_arg,
3821 data_t *key_data,
3822 int alg_arg,
3823 data_t *input_data,
3824 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003825{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003826 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003827 psa_key_type_t key_type = key_type_arg;
3828 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003829 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003830 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003831 size_t output_size;
3832 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003833 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003834 size_t output2_size;
3835 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003836 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003837
Gilles Peskine8817f612018-12-18 00:18:46 +01003838 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003839
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003840 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3841 psa_set_key_algorithm( &attributes, alg );
3842 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003843
Gilles Peskine049c7532019-05-15 20:22:09 +02003844 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3845 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003846
3847 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003848 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3849 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003850 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003851 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003852 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003853 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003854
Gilles Peskineeebd7382018-06-08 18:11:54 +02003855 /* We test encryption by checking that encrypt-then-decrypt gives back
3856 * the original plaintext because of the non-optional random
3857 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003858 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3859 input_data->x, input_data->len,
3860 label->x, label->len,
3861 output, output_size,
3862 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003863 /* We don't know what ciphertext length to expect, but check that
3864 * it looks sensible. */
3865 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003866
Gilles Peskine8817f612018-12-18 00:18:46 +01003867 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3868 output, output_length,
3869 label->x, label->len,
3870 output2, output2_size,
3871 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003872 ASSERT_COMPARE( input_data->x, input_data->len,
3873 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003874
3875exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003876 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003877 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003878 mbedtls_free( output );
3879 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003880 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003881}
3882/* END_CASE */
3883
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003884/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003885void asymmetric_decrypt( int key_type_arg,
3886 data_t *key_data,
3887 int alg_arg,
3888 data_t *input_data,
3889 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003890 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003891{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003892 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003893 psa_key_type_t key_type = key_type_arg;
3894 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003895 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003896 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003897 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003898 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003899
Jaeden Amero412654a2019-02-06 12:57:46 +00003900 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003901 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003902
Gilles Peskine8817f612018-12-18 00:18:46 +01003903 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003904
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003905 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3906 psa_set_key_algorithm( &attributes, alg );
3907 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003908
Gilles Peskine049c7532019-05-15 20:22:09 +02003909 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3910 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003911
Gilles Peskine8817f612018-12-18 00:18:46 +01003912 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3913 input_data->x, input_data->len,
3914 label->x, label->len,
3915 output,
3916 output_size,
3917 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003918 ASSERT_COMPARE( expected_data->x, expected_data->len,
3919 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003920
Gilles Peskine68428122018-06-30 18:42:41 +02003921 /* If the label is empty, the test framework puts a non-null pointer
3922 * in label->x. Test that a null pointer works as well. */
3923 if( label->len == 0 )
3924 {
3925 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003926 if( output_size != 0 )
3927 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003928 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3929 input_data->x, input_data->len,
3930 NULL, label->len,
3931 output,
3932 output_size,
3933 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003934 ASSERT_COMPARE( expected_data->x, expected_data->len,
3935 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003936 }
3937
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003938exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003939 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003940 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003941 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003942 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003943}
3944/* END_CASE */
3945
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003946/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003947void asymmetric_decrypt_fail( int key_type_arg,
3948 data_t *key_data,
3949 int alg_arg,
3950 data_t *input_data,
3951 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003952 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003953 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003954{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003955 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003956 psa_key_type_t key_type = key_type_arg;
3957 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003958 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003959 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003960 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003961 psa_status_t actual_status;
3962 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003963 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003964
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003965 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003966
Gilles Peskine8817f612018-12-18 00:18:46 +01003967 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003968
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003969 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3970 psa_set_key_algorithm( &attributes, alg );
3971 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003972
Gilles Peskine049c7532019-05-15 20:22:09 +02003973 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3974 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003975
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003976 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003977 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003978 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003979 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003980 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003981 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003982 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003983
Gilles Peskine68428122018-06-30 18:42:41 +02003984 /* If the label is empty, the test framework puts a non-null pointer
3985 * in label->x. Test that a null pointer works as well. */
3986 if( label->len == 0 )
3987 {
3988 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003989 if( output_size != 0 )
3990 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003991 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003992 input_data->x, input_data->len,
3993 NULL, label->len,
3994 output, output_size,
3995 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003996 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003997 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003998 }
3999
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004000exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004001 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004002 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004003 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004004 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004005}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004006/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004007
4008/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004009void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004010{
4011 /* Test each valid way of initializing the object, except for `= {0}`, as
4012 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4013 * though it's OK by the C standard. We could test for this, but we'd need
4014 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004015 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004016 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4017 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4018 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004019
4020 memset( &zero, 0, sizeof( zero ) );
4021
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004022 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004023 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004024 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004025 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004026 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004027 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004028 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004029
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004030 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004031 PSA_ASSERT( psa_key_derivation_abort(&func) );
4032 PSA_ASSERT( psa_key_derivation_abort(&init) );
4033 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004034}
4035/* END_CASE */
4036
Janos Follath71a4c912019-06-11 09:14:47 +01004037/* BEGIN_CASE depends_on:PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004038void derive_setup( int key_type_arg,
4039 data_t *key_data,
4040 int alg_arg,
4041 data_t *salt,
4042 data_t *label,
4043 int requested_capacity_arg,
4044 int expected_status_arg )
4045{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004046 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004047 size_t key_type = key_type_arg;
4048 psa_algorithm_t alg = alg_arg;
4049 size_t requested_capacity = requested_capacity_arg;
4050 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004051 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004052 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004053
Gilles Peskine8817f612018-12-18 00:18:46 +01004054 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004055
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004056 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4057 psa_set_key_algorithm( &attributes, alg );
4058 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004059
Gilles Peskine049c7532019-05-15 20:22:09 +02004060 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4061 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004062
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004063 TEST_EQUAL( psa_key_derivation( &operation, handle, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004064 salt->x, salt->len,
4065 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004066 requested_capacity ),
4067 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004068
4069exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004070 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004071 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004072 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004073}
4074/* END_CASE */
4075
Janos Follathaf3c2a02019-06-12 12:34:34 +01004076/* BEGIN_CASE */
4077void derive_input( int alg_arg,
4078 int key_type_arg,
4079 int step1_arg, data_t *input1,
4080 int step2_arg, data_t *input2,
4081 int step3_arg, data_t *input3,
4082 int expected_status_arg1,
4083 int expected_status_arg2,
4084 int expected_status_arg3 )
4085{
4086 psa_algorithm_t alg = alg_arg;
4087 size_t key_type = key_type_arg;
4088 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4089 psa_status_t expected_statuses[] = {expected_status_arg1,
4090 expected_status_arg2,
4091 expected_status_arg3};
4092 data_t *inputs[] = {input1, input2, input3};
4093 psa_key_handle_t handles[] = {0, 0, 0};
4094 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4095 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4096 size_t i;
4097
4098 PSA_ASSERT( psa_crypto_init( ) );
4099
4100 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4101 psa_set_key_algorithm( &attributes, alg );
4102 psa_set_key_type( &attributes, key_type );
4103
4104 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4105
4106 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4107 {
4108 switch( steps[i] )
4109 {
4110 case PSA_KEY_DERIVATION_INPUT_SECRET:
4111 PSA_ASSERT( psa_import_key( &attributes,
4112 inputs[i]->x, inputs[i]->len,
4113 &handles[i] ) );
4114 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4115 handles[i] ),
4116 expected_statuses[i] );
4117 break;
4118 default:
4119 TEST_EQUAL( psa_key_derivation_input_bytes(
4120 &operation, steps[i],
4121 inputs[i]->x, inputs[i]->len ),
4122 expected_statuses[i] );
4123 break;
4124 }
4125 }
4126
4127exit:
4128 psa_key_derivation_abort( &operation );
4129 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4130 psa_destroy_key( handles[i] );
4131 PSA_DONE( );
4132}
4133/* END_CASE */
4134
Janos Follath71a4c912019-06-11 09:14:47 +01004135/* BEGIN_CASE depends_on:PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004136void test_derive_invalid_key_derivation_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004137{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004138 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004139 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004140 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004141 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004142 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004143 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004144 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4145 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4146 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004147 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004148
Gilles Peskine8817f612018-12-18 00:18:46 +01004149 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004150
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004151 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4152 psa_set_key_algorithm( &attributes, alg );
4153 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004154
Gilles Peskine73676cb2019-05-15 20:15:10 +02004155 PSA_ASSERT( psa_import_key( &attributes,
4156 key_data, sizeof( key_data ),
4157 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004158
4159 /* valid key derivation */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004160 PSA_ASSERT( psa_key_derivation( &operation, handle, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004161 NULL, 0,
4162 NULL, 0,
4163 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004164
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004165 /* state of operation shouldn't allow additional generation */
4166 TEST_EQUAL( psa_key_derivation( &operation, handle, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004167 NULL, 0,
4168 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004169 capacity ),
4170 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004171
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004172 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004173
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004174 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004175 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004176
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004177exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004178 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004179 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004180 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004181}
4182/* END_CASE */
4183
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004184/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004185void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004186{
4187 uint8_t output_buffer[16];
4188 size_t buffer_size = 16;
4189 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004190 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004191
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004192 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4193 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004194 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004195
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004196 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004197 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004198
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004199 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004200
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004201 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4202 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004203 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004204
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004205 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004206 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004207
4208exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004209 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004210}
4211/* END_CASE */
4212
4213/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004214void derive_output( int alg_arg,
4215 data_t *key_data,
4216 data_t *salt,
4217 data_t *label,
4218 int requested_capacity_arg,
4219 data_t *expected_output1,
4220 data_t *expected_output2 )
4221{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004222 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004223 psa_algorithm_t alg = alg_arg;
4224 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004225 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004226 uint8_t *expected_outputs[2] =
4227 {expected_output1->x, expected_output2->x};
4228 size_t output_sizes[2] =
4229 {expected_output1->len, expected_output2->len};
4230 size_t output_buffer_size = 0;
4231 uint8_t *output_buffer = NULL;
4232 size_t expected_capacity;
4233 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004234 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004235 psa_status_t status;
4236 unsigned i;
4237
4238 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4239 {
4240 if( output_sizes[i] > output_buffer_size )
4241 output_buffer_size = output_sizes[i];
4242 if( output_sizes[i] == 0 )
4243 expected_outputs[i] = NULL;
4244 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004245 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004246 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004247
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004248 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4249 psa_set_key_algorithm( &attributes, alg );
4250 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004251
Gilles Peskine049c7532019-05-15 20:22:09 +02004252 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4253 &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004254
4255 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004256 if( PSA_ALG_IS_HKDF( alg ) )
4257 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004258 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4259 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004260 requested_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004261 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004262 PSA_KEY_DERIVATION_INPUT_SALT,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004263 salt->x, salt->len ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004264 PSA_ASSERT( psa_key_derivation_input_key( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004265 PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004266 handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004267 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004268 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004269 label->x, label->len ) );
4270 }
Janos Follath71a4c912019-06-11 09:14:47 +01004271#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004272 else
4273 {
4274 // legacy
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004275 PSA_ASSERT( psa_key_derivation( &operation, handle, alg,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004276 salt->x, salt->len,
4277 label->x, label->len,
4278 requested_capacity ) );
4279 }
Janos Follath71a4c912019-06-11 09:14:47 +01004280#endif
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004281 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004282 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004283 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004284 expected_capacity = requested_capacity;
4285
4286 /* Expansion phase. */
4287 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4288 {
4289 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004290 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004291 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004292 if( expected_capacity == 0 && output_sizes[i] == 0 )
4293 {
4294 /* Reading 0 bytes when 0 bytes are available can go either way. */
4295 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004296 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004297 continue;
4298 }
4299 else if( expected_capacity == 0 ||
4300 output_sizes[i] > expected_capacity )
4301 {
4302 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004303 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004304 expected_capacity = 0;
4305 continue;
4306 }
4307 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004308 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004309 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004310 ASSERT_COMPARE( output_buffer, output_sizes[i],
4311 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004312 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004313 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004314 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004315 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004316 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004317 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004318 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004319
4320exit:
4321 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004322 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004323 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004324 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004325}
4326/* END_CASE */
4327
4328/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004329void derive_full( int alg_arg,
4330 data_t *key_data,
4331 data_t *salt,
4332 data_t *label,
4333 int requested_capacity_arg )
4334{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004335 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004336 psa_algorithm_t alg = alg_arg;
4337 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004338 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004339 unsigned char output_buffer[16];
4340 size_t expected_capacity = requested_capacity;
4341 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004343
Gilles Peskine8817f612018-12-18 00:18:46 +01004344 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004345
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004346 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4347 psa_set_key_algorithm( &attributes, alg );
4348 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004349
Gilles Peskine049c7532019-05-15 20:22:09 +02004350 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4351 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004352
4353 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004354 if( PSA_ALG_IS_HKDF( alg ) )
4355 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004356 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4357 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004358 requested_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004359 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004360 PSA_KEY_DERIVATION_INPUT_SALT,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004361 salt->x, salt->len ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004362 PSA_ASSERT( psa_key_derivation_input_key( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004363 PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004364 handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004365 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004366 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004367 label->x, label->len ) );
4368 }
Janos Follath71a4c912019-06-11 09:14:47 +01004369
4370#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004371 else
4372 {
4373 // legacy
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004374 PSA_ASSERT( psa_key_derivation( &operation, handle, alg,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004375 salt->x, salt->len,
4376 label->x, label->len,
4377 requested_capacity ) );
4378 }
Janos Follath71a4c912019-06-11 09:14:47 +01004379#endif
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004380 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004381 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004382 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004383
4384 /* Expansion phase. */
4385 while( current_capacity > 0 )
4386 {
4387 size_t read_size = sizeof( output_buffer );
4388 if( read_size > current_capacity )
4389 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004390 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004391 output_buffer,
4392 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004393 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004394 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004395 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004396 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004397 }
4398
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004399 /* Check that the operation refuses to go over capacity. */
4400 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004401 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004402
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004403 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004404
4405exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004406 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004407 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004408 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004409}
4410/* END_CASE */
4411
Janos Follath71a4c912019-06-11 09:14:47 +01004412/* BEGIN_CASE depends_on:PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004413void derive_key_exercise( int alg_arg,
4414 data_t *key_data,
4415 data_t *salt,
4416 data_t *label,
4417 int derived_type_arg,
4418 int derived_bits_arg,
4419 int derived_usage_arg,
4420 int derived_alg_arg )
4421{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004422 psa_key_handle_t base_handle = 0;
4423 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004424 psa_algorithm_t alg = alg_arg;
4425 psa_key_type_t derived_type = derived_type_arg;
4426 size_t derived_bits = derived_bits_arg;
4427 psa_key_usage_t derived_usage = derived_usage_arg;
4428 psa_algorithm_t derived_alg = derived_alg_arg;
4429 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004430 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004431 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004432 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004433
Gilles Peskine8817f612018-12-18 00:18:46 +01004434 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004435
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004436 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4437 psa_set_key_algorithm( &attributes, alg );
4438 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004439 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4440 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004441
4442 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004443 PSA_ASSERT( psa_key_derivation( &operation, base_handle, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004444 salt->x, salt->len,
4445 label->x, label->len,
4446 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004447 psa_set_key_usage_flags( &attributes, derived_usage );
4448 psa_set_key_algorithm( &attributes, derived_alg );
4449 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004450 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004451 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004452 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004453
4454 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004455 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4456 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4457 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004458
4459 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004460 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004461 goto exit;
4462
4463exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004464 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004465 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004466 psa_destroy_key( base_handle );
4467 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004468 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004469}
4470/* END_CASE */
4471
Janos Follath71a4c912019-06-11 09:14:47 +01004472/* BEGIN_CASE depends_on:PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004473void derive_key_export( int alg_arg,
4474 data_t *key_data,
4475 data_t *salt,
4476 data_t *label,
4477 int bytes1_arg,
4478 int bytes2_arg )
4479{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004480 psa_key_handle_t base_handle = 0;
4481 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004482 psa_algorithm_t alg = alg_arg;
4483 size_t bytes1 = bytes1_arg;
4484 size_t bytes2 = bytes2_arg;
4485 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004486 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004487 uint8_t *output_buffer = NULL;
4488 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004489 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4490 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004491 size_t length;
4492
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004493 ASSERT_ALLOC( output_buffer, capacity );
4494 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004495 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004496
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004497 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4498 psa_set_key_algorithm( &base_attributes, alg );
4499 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004500 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4501 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004502
4503 /* Derive some material and output it. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004504 PSA_ASSERT( psa_key_derivation( &operation, base_handle, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004505 salt->x, salt->len,
4506 label->x, label->len,
4507 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004508 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004509 output_buffer,
4510 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004511 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004512
4513 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004514 PSA_ASSERT( psa_key_derivation( &operation, base_handle, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004515 salt->x, salt->len,
4516 label->x, label->len,
4517 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004518 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4519 psa_set_key_algorithm( &derived_attributes, 0 );
4520 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004521 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004522 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004523 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004524 PSA_ASSERT( psa_export_key( derived_handle,
4525 export_buffer, bytes1,
4526 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004527 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004528 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004529 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004530 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004531 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004532 PSA_ASSERT( psa_export_key( derived_handle,
4533 export_buffer + bytes1, bytes2,
4534 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004535 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004536
4537 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004538 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4539 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004540
4541exit:
4542 mbedtls_free( output_buffer );
4543 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004544 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004545 psa_destroy_key( base_handle );
4546 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004547 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004548}
4549/* END_CASE */
4550
4551/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004552void key_agreement_setup( int alg_arg,
4553 int our_key_type_arg, data_t *our_key_data,
4554 data_t *peer_key_data,
4555 int expected_status_arg )
4556{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004557 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004558 psa_algorithm_t alg = alg_arg;
4559 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004560 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004561 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004562 psa_status_t expected_status = expected_status_arg;
4563 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004564
Gilles Peskine8817f612018-12-18 00:18:46 +01004565 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004566
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004567 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4568 psa_set_key_algorithm( &attributes, alg );
4569 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004570 PSA_ASSERT( psa_import_key( &attributes,
4571 our_key_data->x, our_key_data->len,
4572 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004573
Gilles Peskine77f40d82019-04-11 21:27:06 +02004574 /* The tests currently include inputs that should fail at either step.
4575 * Test cases that fail at the setup step should be changed to call
4576 * key_derivation_setup instead, and this function should be renamed
4577 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004578 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004579 if( status == PSA_SUCCESS )
4580 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004581 TEST_EQUAL( psa_key_derivation_key_agreement(
4582 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4583 our_key,
4584 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004585 expected_status );
4586 }
4587 else
4588 {
4589 TEST_ASSERT( status == expected_status );
4590 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004591
4592exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004593 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004594 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004595 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004596}
4597/* END_CASE */
4598
4599/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004600void raw_key_agreement( int alg_arg,
4601 int our_key_type_arg, data_t *our_key_data,
4602 data_t *peer_key_data,
4603 data_t *expected_output )
4604{
4605 psa_key_handle_t our_key = 0;
4606 psa_algorithm_t alg = alg_arg;
4607 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004608 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004609 unsigned char *output = NULL;
4610 size_t output_length = ~0;
4611
4612 ASSERT_ALLOC( output, expected_output->len );
4613 PSA_ASSERT( psa_crypto_init( ) );
4614
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004615 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4616 psa_set_key_algorithm( &attributes, alg );
4617 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004618 PSA_ASSERT( psa_import_key( &attributes,
4619 our_key_data->x, our_key_data->len,
4620 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004621
Gilles Peskinebe697d82019-05-16 18:00:41 +02004622 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4623 peer_key_data->x, peer_key_data->len,
4624 output, expected_output->len,
4625 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004626 ASSERT_COMPARE( output, output_length,
4627 expected_output->x, expected_output->len );
4628
4629exit:
4630 mbedtls_free( output );
4631 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004632 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004633}
4634/* END_CASE */
4635
4636/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004637void key_agreement_capacity( int alg_arg,
4638 int our_key_type_arg, data_t *our_key_data,
4639 data_t *peer_key_data,
4640 int expected_capacity_arg )
4641{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004642 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004643 psa_algorithm_t alg = alg_arg;
4644 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004645 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004646 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004647 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004648 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004649
Gilles Peskine8817f612018-12-18 00:18:46 +01004650 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004651
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004652 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4653 psa_set_key_algorithm( &attributes, alg );
4654 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004655 PSA_ASSERT( psa_import_key( &attributes,
4656 our_key_data->x, our_key_data->len,
4657 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004658
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004659 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004660 PSA_ASSERT( psa_key_derivation_key_agreement(
4661 &operation,
4662 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4663 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004664 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4665 {
4666 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004667 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004668 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004669 NULL, 0 ) );
4670 }
Gilles Peskine59685592018-09-18 12:11:34 +02004671
Gilles Peskinebf491972018-10-25 22:36:12 +02004672 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004673 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004674 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004675 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004676
Gilles Peskinebf491972018-10-25 22:36:12 +02004677 /* Test the actual capacity by reading the output. */
4678 while( actual_capacity > sizeof( output ) )
4679 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004680 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004681 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004682 actual_capacity -= sizeof( output );
4683 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004684 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004685 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004686 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004687 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004688
Gilles Peskine59685592018-09-18 12:11:34 +02004689exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004690 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004691 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004692 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004693}
4694/* END_CASE */
4695
4696/* BEGIN_CASE */
4697void key_agreement_output( int alg_arg,
4698 int our_key_type_arg, data_t *our_key_data,
4699 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004700 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004701{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004702 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004703 psa_algorithm_t alg = alg_arg;
4704 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004705 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004706 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004707 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004708
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004709 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4710 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004711
Gilles Peskine8817f612018-12-18 00:18:46 +01004712 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004713
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004714 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4715 psa_set_key_algorithm( &attributes, alg );
4716 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004717 PSA_ASSERT( psa_import_key( &attributes,
4718 our_key_data->x, our_key_data->len,
4719 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004720
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004721 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004722 PSA_ASSERT( psa_key_derivation_key_agreement(
4723 &operation,
4724 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4725 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004726 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4727 {
4728 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004729 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004730 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004731 NULL, 0 ) );
4732 }
Gilles Peskine59685592018-09-18 12:11:34 +02004733
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004734 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004735 actual_output,
4736 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004737 ASSERT_COMPARE( actual_output, expected_output1->len,
4738 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004739 if( expected_output2->len != 0 )
4740 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004741 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004742 actual_output,
4743 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004744 ASSERT_COMPARE( actual_output, expected_output2->len,
4745 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004746 }
Gilles Peskine59685592018-09-18 12:11:34 +02004747
4748exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004749 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004750 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004751 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004752 mbedtls_free( actual_output );
4753}
4754/* END_CASE */
4755
4756/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004757void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004758{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004759 size_t bytes = bytes_arg;
4760 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004761 unsigned char *output = NULL;
4762 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004763 size_t i;
4764 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004765
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004766 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4767 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004768 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004769
Gilles Peskine8817f612018-12-18 00:18:46 +01004770 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004771
Gilles Peskinea50d7392018-06-21 10:22:13 +02004772 /* Run several times, to ensure that every output byte will be
4773 * nonzero at least once with overwhelming probability
4774 * (2^(-8*number_of_runs)). */
4775 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004776 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004777 if( bytes != 0 )
4778 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004779 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004780
4781 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004782 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4783 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004784
4785 for( i = 0; i < bytes; i++ )
4786 {
4787 if( output[i] != 0 )
4788 ++changed[i];
4789 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004790 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004791
4792 /* Check that every byte was changed to nonzero at least once. This
4793 * validates that psa_generate_random is overwriting every byte of
4794 * the output buffer. */
4795 for( i = 0; i < bytes; i++ )
4796 {
4797 TEST_ASSERT( changed[i] != 0 );
4798 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004799
4800exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004801 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004802 mbedtls_free( output );
4803 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004804}
4805/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004806
4807/* BEGIN_CASE */
4808void generate_key( int type_arg,
4809 int bits_arg,
4810 int usage_arg,
4811 int alg_arg,
4812 int expected_status_arg )
4813{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004814 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004815 psa_key_type_t type = type_arg;
4816 psa_key_usage_t usage = usage_arg;
4817 size_t bits = bits_arg;
4818 psa_algorithm_t alg = alg_arg;
4819 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004820 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004821 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004822
Gilles Peskine8817f612018-12-18 00:18:46 +01004823 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004824
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004825 psa_set_key_usage_flags( &attributes, usage );
4826 psa_set_key_algorithm( &attributes, alg );
4827 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004828 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004829
4830 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004831 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004832 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004833 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004834
4835 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004836 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
4837 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4838 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004839
Gilles Peskine818ca122018-06-20 18:16:48 +02004840 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004841 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004842 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004843
4844exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004845 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004846 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004847 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004848}
4849/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004850
Gilles Peskinee56e8782019-04-26 17:34:02 +02004851/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
4852void generate_key_rsa( int bits_arg,
4853 data_t *e_arg,
4854 int expected_status_arg )
4855{
4856 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004857 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02004858 size_t bits = bits_arg;
4859 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4860 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4861 psa_status_t expected_status = expected_status_arg;
4862 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4863 uint8_t *exported = NULL;
4864 size_t exported_size =
4865 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
4866 size_t exported_length = SIZE_MAX;
4867 uint8_t *e_read_buffer = NULL;
4868 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004869 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004870 size_t e_read_length = SIZE_MAX;
4871
4872 if( e_arg->len == 0 ||
4873 ( e_arg->len == 3 &&
4874 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4875 {
4876 is_default_public_exponent = 1;
4877 e_read_size = 0;
4878 }
4879 ASSERT_ALLOC( e_read_buffer, e_read_size );
4880 ASSERT_ALLOC( exported, exported_size );
4881
4882 PSA_ASSERT( psa_crypto_init( ) );
4883
4884 psa_set_key_usage_flags( &attributes, usage );
4885 psa_set_key_algorithm( &attributes, alg );
4886 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4887 e_arg->x, e_arg->len ) );
4888 psa_set_key_bits( &attributes, bits );
4889
4890 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004891 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004892 if( expected_status != PSA_SUCCESS )
4893 goto exit;
4894
4895 /* Test the key information */
4896 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4897 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4898 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4899 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4900 e_read_buffer, e_read_size,
4901 &e_read_length ) );
4902 if( is_default_public_exponent )
4903 TEST_EQUAL( e_read_length, 0 );
4904 else
4905 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4906
4907 /* Do something with the key according to its type and permitted usage. */
4908 if( ! exercise_key( handle, usage, alg ) )
4909 goto exit;
4910
4911 /* Export the key and check the public exponent. */
4912 PSA_ASSERT( psa_export_public_key( handle,
4913 exported, exported_size,
4914 &exported_length ) );
4915 {
4916 uint8_t *p = exported;
4917 uint8_t *end = exported + exported_length;
4918 size_t len;
4919 /* RSAPublicKey ::= SEQUENCE {
4920 * modulus INTEGER, -- n
4921 * publicExponent INTEGER } -- e
4922 */
4923 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004924 MBEDTLS_ASN1_SEQUENCE |
4925 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004926 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
4927 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4928 MBEDTLS_ASN1_INTEGER ) );
4929 if( len >= 1 && p[0] == 0 )
4930 {
4931 ++p;
4932 --len;
4933 }
4934 if( e_arg->len == 0 )
4935 {
4936 TEST_EQUAL( len, 3 );
4937 TEST_EQUAL( p[0], 1 );
4938 TEST_EQUAL( p[1], 0 );
4939 TEST_EQUAL( p[2], 1 );
4940 }
4941 else
4942 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4943 }
4944
4945exit:
4946 psa_reset_key_attributes( &attributes );
4947 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004948 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004949 mbedtls_free( e_read_buffer );
4950 mbedtls_free( exported );
4951}
4952/* END_CASE */
4953
Darryl Greend49a4992018-06-18 17:27:26 +01004954/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004955void persistent_key_load_key_from_storage( data_t *data,
4956 int type_arg, int bits_arg,
4957 int usage_flags_arg, int alg_arg,
4958 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004959{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004960 psa_key_id_t key_id = 1;
4961 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004962 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004963 psa_key_handle_t base_key = 0;
4964 psa_key_type_t type = type_arg;
4965 size_t bits = bits_arg;
4966 psa_key_usage_t usage_flags = usage_flags_arg;
4967 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004968 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004969 unsigned char *first_export = NULL;
4970 unsigned char *second_export = NULL;
4971 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4972 size_t first_exported_length;
4973 size_t second_exported_length;
4974
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004975 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4976 {
4977 ASSERT_ALLOC( first_export, export_size );
4978 ASSERT_ALLOC( second_export, export_size );
4979 }
Darryl Greend49a4992018-06-18 17:27:26 +01004980
Gilles Peskine8817f612018-12-18 00:18:46 +01004981 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004982
Gilles Peskinec87af662019-05-15 16:12:22 +02004983 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004984 psa_set_key_usage_flags( &attributes, usage_flags );
4985 psa_set_key_algorithm( &attributes, alg );
4986 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004987 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004988
Darryl Green0c6575a2018-11-07 16:05:30 +00004989 switch( generation_method )
4990 {
4991 case IMPORT_KEY:
4992 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02004993 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
4994 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004995 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004996
Darryl Green0c6575a2018-11-07 16:05:30 +00004997 case GENERATE_KEY:
4998 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004999 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005000 break;
5001
5002 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005003 {
5004 /* Create base key */
5005 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5006 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5007 psa_set_key_usage_flags( &base_attributes,
5008 PSA_KEY_USAGE_DERIVE );
5009 psa_set_key_algorithm( &base_attributes, derive_alg );
5010 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005011 PSA_ASSERT( psa_import_key( &base_attributes,
5012 data->x, data->len,
5013 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005014 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005015 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005016 PSA_ASSERT( psa_key_derivation_input_key(
5017 &operation,
5018 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005019 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005020 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005021 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005022 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5023 &operation,
5024 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005025 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005026 PSA_ASSERT( psa_destroy_key( base_key ) );
5027 base_key = 0;
5028 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005029 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005030 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005031 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005032
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005033 /* Export the key if permitted by the key policy. */
5034 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5035 {
5036 PSA_ASSERT( psa_export_key( handle,
5037 first_export, export_size,
5038 &first_exported_length ) );
5039 if( generation_method == IMPORT_KEY )
5040 ASSERT_COMPARE( data->x, data->len,
5041 first_export, first_exported_length );
5042 }
Darryl Greend49a4992018-06-18 17:27:26 +01005043
5044 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005045 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005046 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005047 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005048
Darryl Greend49a4992018-06-18 17:27:26 +01005049 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005050 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005051 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5052 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
5053 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5054 PSA_KEY_LIFETIME_PERSISTENT );
5055 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5056 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5057 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5058 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005059
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005060 /* Export the key again if permitted by the key policy. */
5061 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005062 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005063 PSA_ASSERT( psa_export_key( handle,
5064 second_export, export_size,
5065 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005066 ASSERT_COMPARE( first_export, first_exported_length,
5067 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005068 }
5069
5070 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005071 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005072 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005073
5074exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005075 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005076 mbedtls_free( first_export );
5077 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005078 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005079 psa_destroy_key( base_key );
5080 if( handle == 0 )
5081 {
5082 /* In case there was a test failure after creating the persistent key
5083 * but while it was not open, try to re-open the persistent key
5084 * to delete it. */
Gilles Peskine225010f2019-05-06 18:44:55 +02005085 psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005086 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005087 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005088 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005089}
5090/* END_CASE */