blob: 316f2edcd77035a978b64dfb50f7dcbf4993dd7b [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 Follath16de4a42019-06-13 16:32:24 +01004037/* BEGIN_CASE */
4038void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004039{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004040 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004041 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004042 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004043
Gilles Peskine8817f612018-12-18 00:18:46 +01004044 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004045
Janos Follath16de4a42019-06-13 16:32:24 +01004046 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004047 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004048
4049exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004050 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004051 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004052}
4053/* END_CASE */
4054
Janos Follathaf3c2a02019-06-12 12:34:34 +01004055/* BEGIN_CASE */
4056void derive_input( int alg_arg,
4057 int key_type_arg,
4058 int step1_arg, data_t *input1,
4059 int step2_arg, data_t *input2,
4060 int step3_arg, data_t *input3,
4061 int expected_status_arg1,
4062 int expected_status_arg2,
4063 int expected_status_arg3 )
4064{
4065 psa_algorithm_t alg = alg_arg;
4066 size_t key_type = key_type_arg;
4067 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4068 psa_status_t expected_statuses[] = {expected_status_arg1,
4069 expected_status_arg2,
4070 expected_status_arg3};
4071 data_t *inputs[] = {input1, input2, input3};
4072 psa_key_handle_t handles[] = {0, 0, 0};
4073 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4074 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4075 size_t i;
4076
4077 PSA_ASSERT( psa_crypto_init( ) );
4078
4079 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4080 psa_set_key_algorithm( &attributes, alg );
4081 psa_set_key_type( &attributes, key_type );
4082
4083 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4084
4085 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4086 {
4087 switch( steps[i] )
4088 {
4089 case PSA_KEY_DERIVATION_INPUT_SECRET:
4090 PSA_ASSERT( psa_import_key( &attributes,
4091 inputs[i]->x, inputs[i]->len,
4092 &handles[i] ) );
4093 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4094 handles[i] ),
4095 expected_statuses[i] );
4096 break;
4097 default:
4098 TEST_EQUAL( psa_key_derivation_input_bytes(
4099 &operation, steps[i],
4100 inputs[i]->x, inputs[i]->len ),
4101 expected_statuses[i] );
4102 break;
4103 }
4104 }
4105
4106exit:
4107 psa_key_derivation_abort( &operation );
4108 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4109 psa_destroy_key( handles[i] );
4110 PSA_DONE( );
4111}
4112/* END_CASE */
4113
Janos Follath71a4c912019-06-11 09:14:47 +01004114/* BEGIN_CASE depends_on:PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004115void test_derive_invalid_key_derivation_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004116{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004117 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004118 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004119 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004120 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004121 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004122 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004123 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4124 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4125 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004126 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004127
Gilles Peskine8817f612018-12-18 00:18:46 +01004128 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004129
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004130 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4131 psa_set_key_algorithm( &attributes, alg );
4132 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004133
Gilles Peskine73676cb2019-05-15 20:15:10 +02004134 PSA_ASSERT( psa_import_key( &attributes,
4135 key_data, sizeof( key_data ),
4136 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004137
4138 /* valid key derivation */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004139 PSA_ASSERT( psa_key_derivation( &operation, handle, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004140 NULL, 0,
4141 NULL, 0,
4142 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004143
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004144 /* state of operation shouldn't allow additional generation */
4145 TEST_EQUAL( psa_key_derivation( &operation, handle, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004146 NULL, 0,
4147 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004148 capacity ),
4149 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004150
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004151 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004152
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004153 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004154 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004155
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004156exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004157 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004158 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004159 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004160}
4161/* END_CASE */
4162
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004163/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004164void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004165{
4166 uint8_t output_buffer[16];
4167 size_t buffer_size = 16;
4168 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004169 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004170
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004171 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4172 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004173 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004174
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004175 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004176 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004177
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004178 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004179
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004180 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4181 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004182 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004183
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004184 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004185 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004186
4187exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004188 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004189}
4190/* END_CASE */
4191
4192/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004193void derive_output( int alg_arg,
4194 data_t *key_data,
4195 data_t *salt,
4196 data_t *label,
4197 int requested_capacity_arg,
4198 data_t *expected_output1,
4199 data_t *expected_output2 )
4200{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004201 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004202 psa_algorithm_t alg = alg_arg;
4203 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004204 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004205 uint8_t *expected_outputs[2] =
4206 {expected_output1->x, expected_output2->x};
4207 size_t output_sizes[2] =
4208 {expected_output1->len, expected_output2->len};
4209 size_t output_buffer_size = 0;
4210 uint8_t *output_buffer = NULL;
4211 size_t expected_capacity;
4212 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004213 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004214 psa_status_t status;
4215 unsigned i;
4216
4217 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4218 {
4219 if( output_sizes[i] > output_buffer_size )
4220 output_buffer_size = output_sizes[i];
4221 if( output_sizes[i] == 0 )
4222 expected_outputs[i] = NULL;
4223 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004224 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004225 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004226
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004227 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4228 psa_set_key_algorithm( &attributes, alg );
4229 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004230
Gilles Peskine049c7532019-05-15 20:22:09 +02004231 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4232 &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004233
4234 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004235 if( PSA_ALG_IS_HKDF( alg ) )
4236 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004237 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4238 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004239 requested_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004240 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004241 PSA_KEY_DERIVATION_INPUT_SALT,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004242 salt->x, salt->len ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004243 PSA_ASSERT( psa_key_derivation_input_key( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004244 PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004245 handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004246 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004247 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004248 label->x, label->len ) );
4249 }
Janos Follath71a4c912019-06-11 09:14:47 +01004250#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004251 else
4252 {
4253 // legacy
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004254 PSA_ASSERT( psa_key_derivation( &operation, handle, alg,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004255 salt->x, salt->len,
4256 label->x, label->len,
4257 requested_capacity ) );
4258 }
Janos Follath71a4c912019-06-11 09:14:47 +01004259#endif
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004260 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004261 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004262 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004263 expected_capacity = requested_capacity;
4264
4265 /* Expansion phase. */
4266 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4267 {
4268 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004269 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004270 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004271 if( expected_capacity == 0 && output_sizes[i] == 0 )
4272 {
4273 /* Reading 0 bytes when 0 bytes are available can go either way. */
4274 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004275 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004276 continue;
4277 }
4278 else if( expected_capacity == 0 ||
4279 output_sizes[i] > expected_capacity )
4280 {
4281 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004282 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004283 expected_capacity = 0;
4284 continue;
4285 }
4286 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004287 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004288 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004289 ASSERT_COMPARE( output_buffer, output_sizes[i],
4290 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004291 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004292 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004293 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004294 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004295 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004296 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004297 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004298
4299exit:
4300 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004301 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004302 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004303 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004304}
4305/* END_CASE */
4306
4307/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004308void derive_full( int alg_arg,
4309 data_t *key_data,
4310 data_t *salt,
4311 data_t *label,
4312 int requested_capacity_arg )
4313{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004314 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004315 psa_algorithm_t alg = alg_arg;
4316 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004317 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004318 unsigned char output_buffer[16];
4319 size_t expected_capacity = requested_capacity;
4320 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004321 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004322
Gilles Peskine8817f612018-12-18 00:18:46 +01004323 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004324
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004325 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4326 psa_set_key_algorithm( &attributes, alg );
4327 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004328
Gilles Peskine049c7532019-05-15 20:22:09 +02004329 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4330 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004331
4332 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004333 if( PSA_ALG_IS_HKDF( alg ) )
4334 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004335 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4336 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004337 requested_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004338 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004339 PSA_KEY_DERIVATION_INPUT_SALT,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004340 salt->x, salt->len ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004341 PSA_ASSERT( psa_key_derivation_input_key( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004342 PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004343 handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004344 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004345 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004346 label->x, label->len ) );
4347 }
Janos Follath71a4c912019-06-11 09:14:47 +01004348
4349#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004350 else
4351 {
4352 // legacy
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004353 PSA_ASSERT( psa_key_derivation( &operation, handle, alg,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004354 salt->x, salt->len,
4355 label->x, label->len,
4356 requested_capacity ) );
4357 }
Janos Follath71a4c912019-06-11 09:14:47 +01004358#endif
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004359 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004360 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004361 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004362
4363 /* Expansion phase. */
4364 while( current_capacity > 0 )
4365 {
4366 size_t read_size = sizeof( output_buffer );
4367 if( read_size > current_capacity )
4368 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004369 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004370 output_buffer,
4371 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004372 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004373 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004374 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004375 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004376 }
4377
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004378 /* Check that the operation refuses to go over capacity. */
4379 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004380 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004381
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004382 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004383
4384exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004385 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004386 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004387 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004388}
4389/* END_CASE */
4390
Janos Follath71a4c912019-06-11 09:14:47 +01004391/* BEGIN_CASE depends_on:PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004392void derive_key_exercise( int alg_arg,
4393 data_t *key_data,
4394 data_t *salt,
4395 data_t *label,
4396 int derived_type_arg,
4397 int derived_bits_arg,
4398 int derived_usage_arg,
4399 int derived_alg_arg )
4400{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004401 psa_key_handle_t base_handle = 0;
4402 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004403 psa_algorithm_t alg = alg_arg;
4404 psa_key_type_t derived_type = derived_type_arg;
4405 size_t derived_bits = derived_bits_arg;
4406 psa_key_usage_t derived_usage = derived_usage_arg;
4407 psa_algorithm_t derived_alg = derived_alg_arg;
4408 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004409 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004410 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004411 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004412
Gilles Peskine8817f612018-12-18 00:18:46 +01004413 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004414
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004415 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4416 psa_set_key_algorithm( &attributes, alg );
4417 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004418 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4419 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004420
4421 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004422 PSA_ASSERT( psa_key_derivation( &operation, base_handle, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004423 salt->x, salt->len,
4424 label->x, label->len,
4425 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004426 psa_set_key_usage_flags( &attributes, derived_usage );
4427 psa_set_key_algorithm( &attributes, derived_alg );
4428 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004429 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004430 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004431 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004432
4433 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004434 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4435 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4436 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004437
4438 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004439 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004440 goto exit;
4441
4442exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004443 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004444 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004445 psa_destroy_key( base_handle );
4446 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004447 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004448}
4449/* END_CASE */
4450
Janos Follath71a4c912019-06-11 09:14:47 +01004451/* BEGIN_CASE depends_on:PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004452void derive_key_export( int alg_arg,
4453 data_t *key_data,
4454 data_t *salt,
4455 data_t *label,
4456 int bytes1_arg,
4457 int bytes2_arg )
4458{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004459 psa_key_handle_t base_handle = 0;
4460 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004461 psa_algorithm_t alg = alg_arg;
4462 size_t bytes1 = bytes1_arg;
4463 size_t bytes2 = bytes2_arg;
4464 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004465 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004466 uint8_t *output_buffer = NULL;
4467 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004468 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4469 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004470 size_t length;
4471
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004472 ASSERT_ALLOC( output_buffer, capacity );
4473 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004474 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004475
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004476 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4477 psa_set_key_algorithm( &base_attributes, alg );
4478 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004479 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4480 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004481
4482 /* Derive some material and output it. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004483 PSA_ASSERT( psa_key_derivation( &operation, base_handle, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004484 salt->x, salt->len,
4485 label->x, label->len,
4486 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004487 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004488 output_buffer,
4489 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004490 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004491
4492 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004493 PSA_ASSERT( psa_key_derivation( &operation, base_handle, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004494 salt->x, salt->len,
4495 label->x, label->len,
4496 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004497 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4498 psa_set_key_algorithm( &derived_attributes, 0 );
4499 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004500 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004501 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004502 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004503 PSA_ASSERT( psa_export_key( derived_handle,
4504 export_buffer, bytes1,
4505 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004506 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004507 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004508 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004509 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004510 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004511 PSA_ASSERT( psa_export_key( derived_handle,
4512 export_buffer + bytes1, bytes2,
4513 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004514 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004515
4516 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004517 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4518 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004519
4520exit:
4521 mbedtls_free( output_buffer );
4522 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004523 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004524 psa_destroy_key( base_handle );
4525 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004526 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004527}
4528/* END_CASE */
4529
4530/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004531void key_agreement_setup( int alg_arg,
4532 int our_key_type_arg, data_t *our_key_data,
4533 data_t *peer_key_data,
4534 int expected_status_arg )
4535{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004536 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004537 psa_algorithm_t alg = alg_arg;
4538 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004539 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004540 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004541 psa_status_t expected_status = expected_status_arg;
4542 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004543
Gilles Peskine8817f612018-12-18 00:18:46 +01004544 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004545
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004546 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4547 psa_set_key_algorithm( &attributes, alg );
4548 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004549 PSA_ASSERT( psa_import_key( &attributes,
4550 our_key_data->x, our_key_data->len,
4551 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004552
Gilles Peskine77f40d82019-04-11 21:27:06 +02004553 /* The tests currently include inputs that should fail at either step.
4554 * Test cases that fail at the setup step should be changed to call
4555 * key_derivation_setup instead, and this function should be renamed
4556 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004557 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004558 if( status == PSA_SUCCESS )
4559 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004560 TEST_EQUAL( psa_key_derivation_key_agreement(
4561 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4562 our_key,
4563 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004564 expected_status );
4565 }
4566 else
4567 {
4568 TEST_ASSERT( status == expected_status );
4569 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004570
4571exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004572 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004573 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004574 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004575}
4576/* END_CASE */
4577
4578/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004579void raw_key_agreement( int alg_arg,
4580 int our_key_type_arg, data_t *our_key_data,
4581 data_t *peer_key_data,
4582 data_t *expected_output )
4583{
4584 psa_key_handle_t our_key = 0;
4585 psa_algorithm_t alg = alg_arg;
4586 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004587 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004588 unsigned char *output = NULL;
4589 size_t output_length = ~0;
4590
4591 ASSERT_ALLOC( output, expected_output->len );
4592 PSA_ASSERT( psa_crypto_init( ) );
4593
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004594 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4595 psa_set_key_algorithm( &attributes, alg );
4596 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004597 PSA_ASSERT( psa_import_key( &attributes,
4598 our_key_data->x, our_key_data->len,
4599 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004600
Gilles Peskinebe697d82019-05-16 18:00:41 +02004601 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4602 peer_key_data->x, peer_key_data->len,
4603 output, expected_output->len,
4604 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004605 ASSERT_COMPARE( output, output_length,
4606 expected_output->x, expected_output->len );
4607
4608exit:
4609 mbedtls_free( output );
4610 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004611 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004612}
4613/* END_CASE */
4614
4615/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004616void key_agreement_capacity( int alg_arg,
4617 int our_key_type_arg, data_t *our_key_data,
4618 data_t *peer_key_data,
4619 int expected_capacity_arg )
4620{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004621 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004622 psa_algorithm_t alg = alg_arg;
4623 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004624 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004625 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004626 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004627 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004628
Gilles Peskine8817f612018-12-18 00:18:46 +01004629 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004630
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004631 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4632 psa_set_key_algorithm( &attributes, alg );
4633 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004634 PSA_ASSERT( psa_import_key( &attributes,
4635 our_key_data->x, our_key_data->len,
4636 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004637
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004638 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004639 PSA_ASSERT( psa_key_derivation_key_agreement(
4640 &operation,
4641 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4642 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004643 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4644 {
4645 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004646 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004647 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004648 NULL, 0 ) );
4649 }
Gilles Peskine59685592018-09-18 12:11:34 +02004650
Gilles Peskinebf491972018-10-25 22:36:12 +02004651 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004652 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004653 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004654 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004655
Gilles Peskinebf491972018-10-25 22:36:12 +02004656 /* Test the actual capacity by reading the output. */
4657 while( actual_capacity > sizeof( output ) )
4658 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004659 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004660 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004661 actual_capacity -= sizeof( output );
4662 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004663 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004664 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004665 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004666 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004667
Gilles Peskine59685592018-09-18 12:11:34 +02004668exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004669 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004670 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004671 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004672}
4673/* END_CASE */
4674
4675/* BEGIN_CASE */
4676void key_agreement_output( int alg_arg,
4677 int our_key_type_arg, data_t *our_key_data,
4678 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004679 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004680{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004681 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004682 psa_algorithm_t alg = alg_arg;
4683 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004684 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004685 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004686 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004687
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004688 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4689 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004690
Gilles Peskine8817f612018-12-18 00:18:46 +01004691 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004692
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004693 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4694 psa_set_key_algorithm( &attributes, alg );
4695 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004696 PSA_ASSERT( psa_import_key( &attributes,
4697 our_key_data->x, our_key_data->len,
4698 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004699
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004700 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004701 PSA_ASSERT( psa_key_derivation_key_agreement(
4702 &operation,
4703 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4704 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004705 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4706 {
4707 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004708 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004709 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004710 NULL, 0 ) );
4711 }
Gilles Peskine59685592018-09-18 12:11:34 +02004712
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004713 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004714 actual_output,
4715 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004716 ASSERT_COMPARE( actual_output, expected_output1->len,
4717 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004718 if( expected_output2->len != 0 )
4719 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004720 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004721 actual_output,
4722 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004723 ASSERT_COMPARE( actual_output, expected_output2->len,
4724 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004725 }
Gilles Peskine59685592018-09-18 12:11:34 +02004726
4727exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004728 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004729 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004730 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004731 mbedtls_free( actual_output );
4732}
4733/* END_CASE */
4734
4735/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004736void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004737{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004738 size_t bytes = bytes_arg;
4739 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004740 unsigned char *output = NULL;
4741 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004742 size_t i;
4743 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004744
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004745 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4746 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004747 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004748
Gilles Peskine8817f612018-12-18 00:18:46 +01004749 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004750
Gilles Peskinea50d7392018-06-21 10:22:13 +02004751 /* Run several times, to ensure that every output byte will be
4752 * nonzero at least once with overwhelming probability
4753 * (2^(-8*number_of_runs)). */
4754 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004755 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004756 if( bytes != 0 )
4757 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004758 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004759
4760 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004761 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4762 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004763
4764 for( i = 0; i < bytes; i++ )
4765 {
4766 if( output[i] != 0 )
4767 ++changed[i];
4768 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004769 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004770
4771 /* Check that every byte was changed to nonzero at least once. This
4772 * validates that psa_generate_random is overwriting every byte of
4773 * the output buffer. */
4774 for( i = 0; i < bytes; i++ )
4775 {
4776 TEST_ASSERT( changed[i] != 0 );
4777 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004778
4779exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004780 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004781 mbedtls_free( output );
4782 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004783}
4784/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004785
4786/* BEGIN_CASE */
4787void generate_key( int type_arg,
4788 int bits_arg,
4789 int usage_arg,
4790 int alg_arg,
4791 int expected_status_arg )
4792{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004793 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004794 psa_key_type_t type = type_arg;
4795 psa_key_usage_t usage = usage_arg;
4796 size_t bits = bits_arg;
4797 psa_algorithm_t alg = alg_arg;
4798 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004799 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004800 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004801
Gilles Peskine8817f612018-12-18 00:18:46 +01004802 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004803
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004804 psa_set_key_usage_flags( &attributes, usage );
4805 psa_set_key_algorithm( &attributes, alg );
4806 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004807 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004808
4809 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004810 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004811 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004812 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004813
4814 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004815 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
4816 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4817 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004818
Gilles Peskine818ca122018-06-20 18:16:48 +02004819 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004820 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004821 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004822
4823exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004824 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004825 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004826 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004827}
4828/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004829
Gilles Peskinee56e8782019-04-26 17:34:02 +02004830/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
4831void generate_key_rsa( int bits_arg,
4832 data_t *e_arg,
4833 int expected_status_arg )
4834{
4835 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004836 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02004837 size_t bits = bits_arg;
4838 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4839 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4840 psa_status_t expected_status = expected_status_arg;
4841 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4842 uint8_t *exported = NULL;
4843 size_t exported_size =
4844 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
4845 size_t exported_length = SIZE_MAX;
4846 uint8_t *e_read_buffer = NULL;
4847 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004848 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004849 size_t e_read_length = SIZE_MAX;
4850
4851 if( e_arg->len == 0 ||
4852 ( e_arg->len == 3 &&
4853 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4854 {
4855 is_default_public_exponent = 1;
4856 e_read_size = 0;
4857 }
4858 ASSERT_ALLOC( e_read_buffer, e_read_size );
4859 ASSERT_ALLOC( exported, exported_size );
4860
4861 PSA_ASSERT( psa_crypto_init( ) );
4862
4863 psa_set_key_usage_flags( &attributes, usage );
4864 psa_set_key_algorithm( &attributes, alg );
4865 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4866 e_arg->x, e_arg->len ) );
4867 psa_set_key_bits( &attributes, bits );
4868
4869 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004870 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004871 if( expected_status != PSA_SUCCESS )
4872 goto exit;
4873
4874 /* Test the key information */
4875 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4876 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4877 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4878 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4879 e_read_buffer, e_read_size,
4880 &e_read_length ) );
4881 if( is_default_public_exponent )
4882 TEST_EQUAL( e_read_length, 0 );
4883 else
4884 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4885
4886 /* Do something with the key according to its type and permitted usage. */
4887 if( ! exercise_key( handle, usage, alg ) )
4888 goto exit;
4889
4890 /* Export the key and check the public exponent. */
4891 PSA_ASSERT( psa_export_public_key( handle,
4892 exported, exported_size,
4893 &exported_length ) );
4894 {
4895 uint8_t *p = exported;
4896 uint8_t *end = exported + exported_length;
4897 size_t len;
4898 /* RSAPublicKey ::= SEQUENCE {
4899 * modulus INTEGER, -- n
4900 * publicExponent INTEGER } -- e
4901 */
4902 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004903 MBEDTLS_ASN1_SEQUENCE |
4904 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004905 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
4906 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4907 MBEDTLS_ASN1_INTEGER ) );
4908 if( len >= 1 && p[0] == 0 )
4909 {
4910 ++p;
4911 --len;
4912 }
4913 if( e_arg->len == 0 )
4914 {
4915 TEST_EQUAL( len, 3 );
4916 TEST_EQUAL( p[0], 1 );
4917 TEST_EQUAL( p[1], 0 );
4918 TEST_EQUAL( p[2], 1 );
4919 }
4920 else
4921 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4922 }
4923
4924exit:
4925 psa_reset_key_attributes( &attributes );
4926 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004927 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004928 mbedtls_free( e_read_buffer );
4929 mbedtls_free( exported );
4930}
4931/* END_CASE */
4932
Darryl Greend49a4992018-06-18 17:27:26 +01004933/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004934void persistent_key_load_key_from_storage( data_t *data,
4935 int type_arg, int bits_arg,
4936 int usage_flags_arg, int alg_arg,
4937 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004938{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004939 psa_key_id_t key_id = 1;
4940 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004941 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004942 psa_key_handle_t base_key = 0;
4943 psa_key_type_t type = type_arg;
4944 size_t bits = bits_arg;
4945 psa_key_usage_t usage_flags = usage_flags_arg;
4946 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004947 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004948 unsigned char *first_export = NULL;
4949 unsigned char *second_export = NULL;
4950 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4951 size_t first_exported_length;
4952 size_t second_exported_length;
4953
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004954 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4955 {
4956 ASSERT_ALLOC( first_export, export_size );
4957 ASSERT_ALLOC( second_export, export_size );
4958 }
Darryl Greend49a4992018-06-18 17:27:26 +01004959
Gilles Peskine8817f612018-12-18 00:18:46 +01004960 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004961
Gilles Peskinec87af662019-05-15 16:12:22 +02004962 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004963 psa_set_key_usage_flags( &attributes, usage_flags );
4964 psa_set_key_algorithm( &attributes, alg );
4965 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004966 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004967
Darryl Green0c6575a2018-11-07 16:05:30 +00004968 switch( generation_method )
4969 {
4970 case IMPORT_KEY:
4971 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02004972 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
4973 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004974 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004975
Darryl Green0c6575a2018-11-07 16:05:30 +00004976 case GENERATE_KEY:
4977 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004978 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004979 break;
4980
4981 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004982 {
4983 /* Create base key */
4984 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
4985 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4986 psa_set_key_usage_flags( &base_attributes,
4987 PSA_KEY_USAGE_DERIVE );
4988 psa_set_key_algorithm( &base_attributes, derive_alg );
4989 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004990 PSA_ASSERT( psa_import_key( &base_attributes,
4991 data->x, data->len,
4992 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004993 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004994 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004995 PSA_ASSERT( psa_key_derivation_input_key(
4996 &operation,
4997 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004998 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004999 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005000 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005001 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5002 &operation,
5003 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005004 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005005 PSA_ASSERT( psa_destroy_key( base_key ) );
5006 base_key = 0;
5007 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005008 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005009 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005010 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005011
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005012 /* Export the key if permitted by the key policy. */
5013 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5014 {
5015 PSA_ASSERT( psa_export_key( handle,
5016 first_export, export_size,
5017 &first_exported_length ) );
5018 if( generation_method == IMPORT_KEY )
5019 ASSERT_COMPARE( data->x, data->len,
5020 first_export, first_exported_length );
5021 }
Darryl Greend49a4992018-06-18 17:27:26 +01005022
5023 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005024 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005025 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005026 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005027
Darryl Greend49a4992018-06-18 17:27:26 +01005028 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005029 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005030 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5031 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
5032 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5033 PSA_KEY_LIFETIME_PERSISTENT );
5034 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5035 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5036 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5037 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005038
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005039 /* Export the key again if permitted by the key policy. */
5040 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005041 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005042 PSA_ASSERT( psa_export_key( handle,
5043 second_export, export_size,
5044 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005045 ASSERT_COMPARE( first_export, first_exported_length,
5046 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005047 }
5048
5049 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005050 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005051 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005052
5053exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005054 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005055 mbedtls_free( first_export );
5056 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005057 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005058 psa_destroy_key( base_key );
5059 if( handle == 0 )
5060 {
5061 /* In case there was a test failure after creating the persistent key
5062 * but while it was not open, try to re-open the persistent key
5063 * to delete it. */
Gilles Peskine225010f2019-05-06 18:44:55 +02005064 psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005065 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005066 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005067 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005068}
5069/* END_CASE */