blob: b21a8f16d97052985dec4e2b2b64ebc074642f38 [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 Follath71a4c912019-06-11 09:14:47 +01001781/* BEGIN_CASE depends_on:PSA_PRE_1_0_KEY_DERIVATION */
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
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001802 status = psa_key_derivation( &operation, handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02001803 exercise_alg,
1804 NULL, 0,
1805 NULL, 0,
1806 1 );
1807 if( policy_alg == exercise_alg &&
1808 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001809 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001810 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001811 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001812
1813exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001814 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001815 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001816 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001817}
1818/* END_CASE */
1819
1820/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001821void agreement_key_policy( int policy_usage,
1822 int policy_alg,
1823 int key_type_arg,
1824 data_t *key_data,
1825 int exercise_alg )
1826{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001827 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001828 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001829 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001830 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001831 psa_status_t status;
1832
Gilles Peskine8817f612018-12-18 00:18:46 +01001833 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001834
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001835 psa_set_key_usage_flags( &attributes, policy_usage );
1836 psa_set_key_algorithm( &attributes, policy_alg );
1837 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001838
Gilles Peskine049c7532019-05-15 20:22:09 +02001839 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1840 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001841
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001842 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1843 status = key_agreement_with_self( &operation, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001844
Gilles Peskine01d718c2018-09-18 12:01:02 +02001845 if( policy_alg == exercise_alg &&
1846 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001847 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001848 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001849 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001850
1851exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001852 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001853 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001854 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001855}
1856/* END_CASE */
1857
1858/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001859void key_policy_alg2( int key_type_arg, data_t *key_data,
1860 int usage_arg, int alg_arg, int alg2_arg )
1861{
1862 psa_key_handle_t handle = 0;
1863 psa_key_type_t key_type = key_type_arg;
1864 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1865 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1866 psa_key_usage_t usage = usage_arg;
1867 psa_algorithm_t alg = alg_arg;
1868 psa_algorithm_t alg2 = alg2_arg;
1869
1870 PSA_ASSERT( psa_crypto_init( ) );
1871
1872 psa_set_key_usage_flags( &attributes, usage );
1873 psa_set_key_algorithm( &attributes, alg );
1874 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1875 psa_set_key_type( &attributes, key_type );
1876 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1877 &handle ) );
1878
1879 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1880 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1881 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1882 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1883
1884 if( ! exercise_key( handle, usage, alg ) )
1885 goto exit;
1886 if( ! exercise_key( handle, usage, alg2 ) )
1887 goto exit;
1888
1889exit:
1890 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001891 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001892}
1893/* END_CASE */
1894
1895/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001896void raw_agreement_key_policy( int policy_usage,
1897 int policy_alg,
1898 int key_type_arg,
1899 data_t *key_data,
1900 int exercise_alg )
1901{
1902 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001903 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001904 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001905 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001906 psa_status_t status;
1907
1908 PSA_ASSERT( psa_crypto_init( ) );
1909
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001910 psa_set_key_usage_flags( &attributes, policy_usage );
1911 psa_set_key_algorithm( &attributes, policy_alg );
1912 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001913
Gilles Peskine049c7532019-05-15 20:22:09 +02001914 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1915 &handle ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001916
1917 status = raw_key_agreement_with_self( exercise_alg, handle );
1918
1919 if( policy_alg == exercise_alg &&
1920 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1921 PSA_ASSERT( status );
1922 else
1923 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1924
1925exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001926 psa_key_derivation_abort( &operation );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001927 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001928 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001929}
1930/* END_CASE */
1931
1932/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001933void copy_success( int source_usage_arg,
1934 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001935 int type_arg, data_t *material,
1936 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001937 int target_usage_arg,
1938 int target_alg_arg, int target_alg2_arg,
1939 int expected_usage_arg,
1940 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001941{
Gilles Peskineca25db92019-04-19 11:43:08 +02001942 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1943 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001944 psa_key_usage_t expected_usage = expected_usage_arg;
1945 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001946 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02001947 psa_key_handle_t source_handle = 0;
1948 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001949 uint8_t *export_buffer = NULL;
1950
Gilles Peskine57ab7212019-01-28 13:03:09 +01001951 PSA_ASSERT( psa_crypto_init( ) );
1952
Gilles Peskineca25db92019-04-19 11:43:08 +02001953 /* Prepare the source key. */
1954 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1955 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001956 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001957 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001958 PSA_ASSERT( psa_import_key( &source_attributes,
1959 material->x, material->len,
1960 &source_handle ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001961 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001962
Gilles Peskineca25db92019-04-19 11:43:08 +02001963 /* Prepare the target attributes. */
1964 if( copy_attributes )
1965 target_attributes = source_attributes;
1966 if( target_usage_arg != -1 )
1967 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1968 if( target_alg_arg != -1 )
1969 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001970 if( target_alg2_arg != -1 )
1971 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001972
1973 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02001974 PSA_ASSERT( psa_copy_key( source_handle,
1975 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001976
1977 /* Destroy the source to ensure that this doesn't affect the target. */
1978 PSA_ASSERT( psa_destroy_key( source_handle ) );
1979
1980 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02001981 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
1982 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1983 psa_get_key_type( &target_attributes ) );
1984 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1985 psa_get_key_bits( &target_attributes ) );
1986 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1987 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001988 TEST_EQUAL( expected_alg2,
1989 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001990 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1991 {
1992 size_t length;
1993 ASSERT_ALLOC( export_buffer, material->len );
1994 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
1995 material->len, &length ) );
1996 ASSERT_COMPARE( material->x, material->len,
1997 export_buffer, length );
1998 }
1999 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2000 goto exit;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002001 if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) )
2002 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002003
2004 PSA_ASSERT( psa_close_key( target_handle ) );
2005
2006exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002007 psa_reset_key_attributes( &source_attributes );
2008 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002009 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002010 mbedtls_free( export_buffer );
2011}
2012/* END_CASE */
2013
2014/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002015void copy_fail( int source_usage_arg,
2016 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002017 int type_arg, data_t *material,
2018 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002019 int target_usage_arg,
2020 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002021 int expected_status_arg )
2022{
2023 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2024 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2025 psa_key_handle_t source_handle = 0;
2026 psa_key_handle_t target_handle = 0;
2027
2028 PSA_ASSERT( psa_crypto_init( ) );
2029
2030 /* Prepare the source key. */
2031 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2032 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002033 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002034 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002035 PSA_ASSERT( psa_import_key( &source_attributes,
2036 material->x, material->len,
2037 &source_handle ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002038
2039 /* Prepare the target attributes. */
2040 psa_set_key_type( &target_attributes, target_type_arg );
2041 psa_set_key_bits( &target_attributes, target_bits_arg );
2042 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2043 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002044 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002045
2046 /* Try to copy the key. */
2047 TEST_EQUAL( psa_copy_key( source_handle,
2048 &target_attributes, &target_handle ),
2049 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002050
2051 PSA_ASSERT( psa_destroy_key( source_handle ) );
2052
Gilles Peskine4a644642019-05-03 17:14:08 +02002053exit:
2054 psa_reset_key_attributes( &source_attributes );
2055 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002056 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002057}
2058/* END_CASE */
2059
2060/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002061void hash_operation_init( )
2062{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002063 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002064 /* Test each valid way of initializing the object, except for `= {0}`, as
2065 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2066 * though it's OK by the C standard. We could test for this, but we'd need
2067 * to supress the Clang warning for the test. */
2068 psa_hash_operation_t func = psa_hash_operation_init( );
2069 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2070 psa_hash_operation_t zero;
2071
2072 memset( &zero, 0, sizeof( zero ) );
2073
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002074 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002075 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2076 PSA_ERROR_BAD_STATE );
2077 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2078 PSA_ERROR_BAD_STATE );
2079 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2080 PSA_ERROR_BAD_STATE );
2081
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002082 /* A default hash operation should be abortable without error. */
2083 PSA_ASSERT( psa_hash_abort( &func ) );
2084 PSA_ASSERT( psa_hash_abort( &init ) );
2085 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002086}
2087/* END_CASE */
2088
2089/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002090void hash_setup( int alg_arg,
2091 int expected_status_arg )
2092{
2093 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002094 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002095 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002096 psa_status_t status;
2097
Gilles Peskine8817f612018-12-18 00:18:46 +01002098 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002099
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002100 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002101 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002102
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002103 /* Whether setup succeeded or failed, abort must succeed. */
2104 PSA_ASSERT( psa_hash_abort( &operation ) );
2105
2106 /* If setup failed, reproduce the failure, so as to
2107 * test the resulting state of the operation object. */
2108 if( status != PSA_SUCCESS )
2109 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2110
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002111 /* Now the operation object should be reusable. */
2112#if defined(KNOWN_SUPPORTED_HASH_ALG)
2113 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2114 PSA_ASSERT( psa_hash_abort( &operation ) );
2115#endif
2116
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002117exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002118 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002119}
2120/* END_CASE */
2121
2122/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002123void hash_bad_order( )
2124{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002125 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002126 unsigned char input[] = "";
2127 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002128 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002129 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2130 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2131 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002132 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002133 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002134 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002135
Gilles Peskine8817f612018-12-18 00:18:46 +01002136 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002137
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002138 /* Call setup twice in a row. */
2139 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2140 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2141 PSA_ERROR_BAD_STATE );
2142 PSA_ASSERT( psa_hash_abort( &operation ) );
2143
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002144 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002145 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002146 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002147 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002148
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002149 /* Call update after finish. */
2150 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2151 PSA_ASSERT( psa_hash_finish( &operation,
2152 hash, sizeof( hash ), &hash_len ) );
2153 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 verify without calling setup beforehand. */
2158 TEST_EQUAL( psa_hash_verify( &operation,
2159 valid_hash, sizeof( valid_hash ) ),
2160 PSA_ERROR_BAD_STATE );
2161 PSA_ASSERT( psa_hash_abort( &operation ) );
2162
2163 /* Call verify after finish. */
2164 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2165 PSA_ASSERT( psa_hash_finish( &operation,
2166 hash, sizeof( hash ), &hash_len ) );
2167 TEST_EQUAL( psa_hash_verify( &operation,
2168 valid_hash, sizeof( valid_hash ) ),
2169 PSA_ERROR_BAD_STATE );
2170 PSA_ASSERT( psa_hash_abort( &operation ) );
2171
2172 /* Call verify twice in a row. */
2173 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2174 PSA_ASSERT( psa_hash_verify( &operation,
2175 valid_hash, sizeof( valid_hash ) ) );
2176 TEST_EQUAL( psa_hash_verify( &operation,
2177 valid_hash, sizeof( valid_hash ) ),
2178 PSA_ERROR_BAD_STATE );
2179 PSA_ASSERT( psa_hash_abort( &operation ) );
2180
2181 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002182 TEST_EQUAL( psa_hash_finish( &operation,
2183 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002184 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002185 PSA_ASSERT( psa_hash_abort( &operation ) );
2186
2187 /* Call finish twice in a row. */
2188 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2189 PSA_ASSERT( psa_hash_finish( &operation,
2190 hash, sizeof( hash ), &hash_len ) );
2191 TEST_EQUAL( psa_hash_finish( &operation,
2192 hash, sizeof( hash ), &hash_len ),
2193 PSA_ERROR_BAD_STATE );
2194 PSA_ASSERT( psa_hash_abort( &operation ) );
2195
2196 /* Call finish after calling verify. */
2197 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2198 PSA_ASSERT( psa_hash_verify( &operation,
2199 valid_hash, sizeof( valid_hash ) ) );
2200 TEST_EQUAL( psa_hash_finish( &operation,
2201 hash, sizeof( hash ), &hash_len ),
2202 PSA_ERROR_BAD_STATE );
2203 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002204
2205exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002206 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002207}
2208/* END_CASE */
2209
itayzafrir27e69452018-11-01 14:26:34 +02002210/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2211void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002212{
2213 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002214 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2215 * appended to it */
2216 unsigned char hash[] = {
2217 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2218 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2219 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002220 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002221 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002222
Gilles Peskine8817f612018-12-18 00:18:46 +01002223 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002224
itayzafrir27e69452018-11-01 14:26:34 +02002225 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002226 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002227 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002228 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002229
itayzafrir27e69452018-11-01 14:26:34 +02002230 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002231 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002232 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002233 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002234
itayzafrir27e69452018-11-01 14:26:34 +02002235 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002236 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002237 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002238 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002239
itayzafrirec93d302018-10-18 18:01:10 +03002240exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002241 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002242}
2243/* END_CASE */
2244
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002245/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2246void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002247{
2248 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002249 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002250 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002251 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002252 size_t hash_len;
2253
Gilles Peskine8817f612018-12-18 00:18:46 +01002254 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002255
itayzafrir58028322018-10-25 10:22:01 +03002256 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002257 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002258 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002259 hash, expected_size - 1, &hash_len ),
2260 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002261
2262exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002263 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002264}
2265/* END_CASE */
2266
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002267/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2268void hash_clone_source_state( )
2269{
2270 psa_algorithm_t alg = PSA_ALG_SHA_256;
2271 unsigned char hash[PSA_HASH_MAX_SIZE];
2272 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2273 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2274 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2275 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2276 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2277 size_t hash_len;
2278
2279 PSA_ASSERT( psa_crypto_init( ) );
2280 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2281
2282 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2283 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2284 PSA_ASSERT( psa_hash_finish( &op_finished,
2285 hash, sizeof( hash ), &hash_len ) );
2286 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2287 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2288
2289 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2290 PSA_ERROR_BAD_STATE );
2291
2292 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2293 PSA_ASSERT( psa_hash_finish( &op_init,
2294 hash, sizeof( hash ), &hash_len ) );
2295 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2296 PSA_ASSERT( psa_hash_finish( &op_finished,
2297 hash, sizeof( hash ), &hash_len ) );
2298 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2299 PSA_ASSERT( psa_hash_finish( &op_aborted,
2300 hash, sizeof( hash ), &hash_len ) );
2301
2302exit:
2303 psa_hash_abort( &op_source );
2304 psa_hash_abort( &op_init );
2305 psa_hash_abort( &op_setup );
2306 psa_hash_abort( &op_finished );
2307 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002308 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002309}
2310/* END_CASE */
2311
2312/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2313void hash_clone_target_state( )
2314{
2315 psa_algorithm_t alg = PSA_ALG_SHA_256;
2316 unsigned char hash[PSA_HASH_MAX_SIZE];
2317 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2318 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2319 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2320 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2321 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2322 size_t hash_len;
2323
2324 PSA_ASSERT( psa_crypto_init( ) );
2325
2326 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2327 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2328 PSA_ASSERT( psa_hash_finish( &op_finished,
2329 hash, sizeof( hash ), &hash_len ) );
2330 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2331 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2332
2333 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2334 PSA_ASSERT( psa_hash_finish( &op_target,
2335 hash, sizeof( hash ), &hash_len ) );
2336
2337 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2338 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2339 PSA_ERROR_BAD_STATE );
2340 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2341 PSA_ERROR_BAD_STATE );
2342
2343exit:
2344 psa_hash_abort( &op_target );
2345 psa_hash_abort( &op_init );
2346 psa_hash_abort( &op_setup );
2347 psa_hash_abort( &op_finished );
2348 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002349 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002350}
2351/* END_CASE */
2352
itayzafrir58028322018-10-25 10:22:01 +03002353/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002354void mac_operation_init( )
2355{
Jaeden Amero252ef282019-02-15 14:05:35 +00002356 const uint8_t input[1] = { 0 };
2357
Jaeden Amero769ce272019-01-04 11:48:03 +00002358 /* Test each valid way of initializing the object, except for `= {0}`, as
2359 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2360 * though it's OK by the C standard. We could test for this, but we'd need
2361 * to supress the Clang warning for the test. */
2362 psa_mac_operation_t func = psa_mac_operation_init( );
2363 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2364 psa_mac_operation_t zero;
2365
2366 memset( &zero, 0, sizeof( zero ) );
2367
Jaeden Amero252ef282019-02-15 14:05:35 +00002368 /* A freshly-initialized MAC operation should not be usable. */
2369 TEST_EQUAL( psa_mac_update( &func,
2370 input, sizeof( input ) ),
2371 PSA_ERROR_BAD_STATE );
2372 TEST_EQUAL( psa_mac_update( &init,
2373 input, sizeof( input ) ),
2374 PSA_ERROR_BAD_STATE );
2375 TEST_EQUAL( psa_mac_update( &zero,
2376 input, sizeof( input ) ),
2377 PSA_ERROR_BAD_STATE );
2378
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002379 /* A default MAC operation should be abortable without error. */
2380 PSA_ASSERT( psa_mac_abort( &func ) );
2381 PSA_ASSERT( psa_mac_abort( &init ) );
2382 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002383}
2384/* END_CASE */
2385
2386/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002387void mac_setup( int key_type_arg,
2388 data_t *key,
2389 int alg_arg,
2390 int expected_status_arg )
2391{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002392 psa_key_type_t key_type = key_type_arg;
2393 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002394 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002395 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002396 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2397#if defined(KNOWN_SUPPORTED_MAC_ALG)
2398 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2399#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002400
Gilles Peskine8817f612018-12-18 00:18:46 +01002401 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002402
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002403 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2404 &operation, &status ) )
2405 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002406 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002407
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002408 /* The operation object should be reusable. */
2409#if defined(KNOWN_SUPPORTED_MAC_ALG)
2410 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2411 smoke_test_key_data,
2412 sizeof( smoke_test_key_data ),
2413 KNOWN_SUPPORTED_MAC_ALG,
2414 &operation, &status ) )
2415 goto exit;
2416 TEST_EQUAL( status, PSA_SUCCESS );
2417#endif
2418
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002419exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002420 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002421}
2422/* END_CASE */
2423
2424/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002425void mac_bad_order( )
2426{
2427 psa_key_handle_t handle = 0;
2428 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2429 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2430 const uint8_t key[] = {
2431 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2432 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2433 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002434 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002435 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2436 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2437 size_t sign_mac_length = 0;
2438 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2439 const uint8_t verify_mac[] = {
2440 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2441 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2442 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2443
2444 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002445 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
2446 psa_set_key_algorithm( &attributes, alg );
2447 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002448
Gilles Peskine73676cb2019-05-15 20:15:10 +02002449 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002450
Jaeden Amero252ef282019-02-15 14:05:35 +00002451 /* Call update without calling setup beforehand. */
2452 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2453 PSA_ERROR_BAD_STATE );
2454 PSA_ASSERT( psa_mac_abort( &operation ) );
2455
2456 /* Call sign finish without calling setup beforehand. */
2457 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2458 &sign_mac_length),
2459 PSA_ERROR_BAD_STATE );
2460 PSA_ASSERT( psa_mac_abort( &operation ) );
2461
2462 /* Call verify finish without calling setup beforehand. */
2463 TEST_EQUAL( psa_mac_verify_finish( &operation,
2464 verify_mac, sizeof( verify_mac ) ),
2465 PSA_ERROR_BAD_STATE );
2466 PSA_ASSERT( psa_mac_abort( &operation ) );
2467
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002468 /* Call setup twice in a row. */
2469 PSA_ASSERT( psa_mac_sign_setup( &operation,
2470 handle, alg ) );
2471 TEST_EQUAL( psa_mac_sign_setup( &operation,
2472 handle, alg ),
2473 PSA_ERROR_BAD_STATE );
2474 PSA_ASSERT( psa_mac_abort( &operation ) );
2475
Jaeden Amero252ef282019-02-15 14:05:35 +00002476 /* Call update after sign finish. */
2477 PSA_ASSERT( psa_mac_sign_setup( &operation,
2478 handle, alg ) );
2479 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2480 PSA_ASSERT( psa_mac_sign_finish( &operation,
2481 sign_mac, sizeof( sign_mac ),
2482 &sign_mac_length ) );
2483 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2484 PSA_ERROR_BAD_STATE );
2485 PSA_ASSERT( psa_mac_abort( &operation ) );
2486
2487 /* Call update after verify finish. */
2488 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002489 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002490 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2491 PSA_ASSERT( psa_mac_verify_finish( &operation,
2492 verify_mac, sizeof( verify_mac ) ) );
2493 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2494 PSA_ERROR_BAD_STATE );
2495 PSA_ASSERT( psa_mac_abort( &operation ) );
2496
2497 /* Call sign finish twice in a row. */
2498 PSA_ASSERT( psa_mac_sign_setup( &operation,
2499 handle, alg ) );
2500 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2501 PSA_ASSERT( psa_mac_sign_finish( &operation,
2502 sign_mac, sizeof( sign_mac ),
2503 &sign_mac_length ) );
2504 TEST_EQUAL( psa_mac_sign_finish( &operation,
2505 sign_mac, sizeof( sign_mac ),
2506 &sign_mac_length ),
2507 PSA_ERROR_BAD_STATE );
2508 PSA_ASSERT( psa_mac_abort( &operation ) );
2509
2510 /* Call verify finish twice in a row. */
2511 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002512 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002513 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2514 PSA_ASSERT( psa_mac_verify_finish( &operation,
2515 verify_mac, sizeof( verify_mac ) ) );
2516 TEST_EQUAL( psa_mac_verify_finish( &operation,
2517 verify_mac, sizeof( verify_mac ) ),
2518 PSA_ERROR_BAD_STATE );
2519 PSA_ASSERT( psa_mac_abort( &operation ) );
2520
2521 /* Setup sign but try verify. */
2522 PSA_ASSERT( psa_mac_sign_setup( &operation,
2523 handle, alg ) );
2524 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2525 TEST_EQUAL( psa_mac_verify_finish( &operation,
2526 verify_mac, sizeof( verify_mac ) ),
2527 PSA_ERROR_BAD_STATE );
2528 PSA_ASSERT( psa_mac_abort( &operation ) );
2529
2530 /* Setup verify but try sign. */
2531 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002532 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002533 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2534 TEST_EQUAL( psa_mac_sign_finish( &operation,
2535 sign_mac, sizeof( sign_mac ),
2536 &sign_mac_length ),
2537 PSA_ERROR_BAD_STATE );
2538 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002539
Gilles Peskine76b29a72019-05-28 14:08:50 +02002540 PSA_ASSERT( psa_destroy_key( handle ) );
2541
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002542exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002543 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002544}
2545/* END_CASE */
2546
2547/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002548void mac_sign( int key_type_arg,
2549 data_t *key,
2550 int alg_arg,
2551 data_t *input,
2552 data_t *expected_mac )
2553{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002554 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002555 psa_key_type_t key_type = key_type_arg;
2556 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002557 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002558 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002559 /* Leave a little extra room in the output buffer. At the end of the
2560 * test, we'll check that the implementation didn't overwrite onto
2561 * this extra room. */
2562 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2563 size_t mac_buffer_size =
2564 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2565 size_t mac_length = 0;
2566
2567 memset( actual_mac, '+', sizeof( actual_mac ) );
2568 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2569 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2570
Gilles Peskine8817f612018-12-18 00:18:46 +01002571 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002572
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002573 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
2574 psa_set_key_algorithm( &attributes, alg );
2575 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002576
Gilles Peskine73676cb2019-05-15 20:15:10 +02002577 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002578
2579 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002580 PSA_ASSERT( psa_mac_sign_setup( &operation,
2581 handle, alg ) );
2582 PSA_ASSERT( psa_mac_update( &operation,
2583 input->x, input->len ) );
2584 PSA_ASSERT( psa_mac_sign_finish( &operation,
2585 actual_mac, mac_buffer_size,
2586 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002587
2588 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002589 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2590 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002591
2592 /* Verify that the end of the buffer is untouched. */
2593 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2594 sizeof( actual_mac ) - mac_length ) );
2595
2596exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002597 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002598 PSA_DONE( );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002599}
2600/* END_CASE */
2601
2602/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002603void mac_verify( int key_type_arg,
2604 data_t *key,
2605 int alg_arg,
2606 data_t *input,
2607 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002608{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002609 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002610 psa_key_type_t key_type = key_type_arg;
2611 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002612 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002613 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002614
Gilles Peskine69c12672018-06-28 00:07:19 +02002615 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2616
Gilles Peskine8817f612018-12-18 00:18:46 +01002617 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002618
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002619 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
2620 psa_set_key_algorithm( &attributes, alg );
2621 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002622
Gilles Peskine73676cb2019-05-15 20:15:10 +02002623 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002624
Gilles Peskine8817f612018-12-18 00:18:46 +01002625 PSA_ASSERT( psa_mac_verify_setup( &operation,
2626 handle, alg ) );
2627 PSA_ASSERT( psa_destroy_key( handle ) );
2628 PSA_ASSERT( psa_mac_update( &operation,
2629 input->x, input->len ) );
2630 PSA_ASSERT( psa_mac_verify_finish( &operation,
2631 expected_mac->x,
2632 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002633
2634exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002635 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002636 PSA_DONE( );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002637}
2638/* END_CASE */
2639
2640/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002641void cipher_operation_init( )
2642{
Jaeden Ameroab439972019-02-15 14:12:05 +00002643 const uint8_t input[1] = { 0 };
2644 unsigned char output[1] = { 0 };
2645 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002646 /* Test each valid way of initializing the object, except for `= {0}`, as
2647 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2648 * though it's OK by the C standard. We could test for this, but we'd need
2649 * to supress the Clang warning for the test. */
2650 psa_cipher_operation_t func = psa_cipher_operation_init( );
2651 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2652 psa_cipher_operation_t zero;
2653
2654 memset( &zero, 0, sizeof( zero ) );
2655
Jaeden Ameroab439972019-02-15 14:12:05 +00002656 /* A freshly-initialized cipher operation should not be usable. */
2657 TEST_EQUAL( psa_cipher_update( &func,
2658 input, sizeof( input ),
2659 output, sizeof( output ),
2660 &output_length ),
2661 PSA_ERROR_BAD_STATE );
2662 TEST_EQUAL( psa_cipher_update( &init,
2663 input, sizeof( input ),
2664 output, sizeof( output ),
2665 &output_length ),
2666 PSA_ERROR_BAD_STATE );
2667 TEST_EQUAL( psa_cipher_update( &zero,
2668 input, sizeof( input ),
2669 output, sizeof( output ),
2670 &output_length ),
2671 PSA_ERROR_BAD_STATE );
2672
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002673 /* A default cipher operation should be abortable without error. */
2674 PSA_ASSERT( psa_cipher_abort( &func ) );
2675 PSA_ASSERT( psa_cipher_abort( &init ) );
2676 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002677}
2678/* END_CASE */
2679
2680/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002681void cipher_setup( int key_type_arg,
2682 data_t *key,
2683 int alg_arg,
2684 int expected_status_arg )
2685{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002686 psa_key_type_t key_type = key_type_arg;
2687 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002688 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002689 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002690 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002691#if defined(KNOWN_SUPPORTED_MAC_ALG)
2692 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2693#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002694
Gilles Peskine8817f612018-12-18 00:18:46 +01002695 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002696
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002697 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2698 &operation, &status ) )
2699 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002700 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002701
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002702 /* The operation object should be reusable. */
2703#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2704 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2705 smoke_test_key_data,
2706 sizeof( smoke_test_key_data ),
2707 KNOWN_SUPPORTED_CIPHER_ALG,
2708 &operation, &status ) )
2709 goto exit;
2710 TEST_EQUAL( status, PSA_SUCCESS );
2711#endif
2712
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002713exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002714 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002715}
2716/* END_CASE */
2717
2718/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002719void cipher_bad_order( )
2720{
2721 psa_key_handle_t handle = 0;
2722 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2723 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002724 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002725 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2726 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2727 const uint8_t key[] = {
2728 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2729 0xaa, 0xaa, 0xaa, 0xaa };
2730 const uint8_t text[] = {
2731 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2732 0xbb, 0xbb, 0xbb, 0xbb };
2733 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2734 size_t length = 0;
2735
2736 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002737 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2738 psa_set_key_algorithm( &attributes, alg );
2739 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02002740 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002741
2742
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002743 /* Call encrypt setup twice in a row. */
2744 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2745 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2746 PSA_ERROR_BAD_STATE );
2747 PSA_ASSERT( psa_cipher_abort( &operation ) );
2748
2749 /* Call decrypt setup twice in a row. */
2750 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2751 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2752 PSA_ERROR_BAD_STATE );
2753 PSA_ASSERT( psa_cipher_abort( &operation ) );
2754
Jaeden Ameroab439972019-02-15 14:12:05 +00002755 /* Generate an IV without calling setup beforehand. */
2756 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2757 buffer, sizeof( buffer ),
2758 &length ),
2759 PSA_ERROR_BAD_STATE );
2760 PSA_ASSERT( psa_cipher_abort( &operation ) );
2761
2762 /* Generate an IV twice in a row. */
2763 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2764 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2765 buffer, sizeof( buffer ),
2766 &length ) );
2767 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2768 buffer, sizeof( buffer ),
2769 &length ),
2770 PSA_ERROR_BAD_STATE );
2771 PSA_ASSERT( psa_cipher_abort( &operation ) );
2772
2773 /* Generate an IV after it's already set. */
2774 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2775 PSA_ASSERT( psa_cipher_set_iv( &operation,
2776 iv, sizeof( iv ) ) );
2777 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2778 buffer, sizeof( buffer ),
2779 &length ),
2780 PSA_ERROR_BAD_STATE );
2781 PSA_ASSERT( psa_cipher_abort( &operation ) );
2782
2783 /* Set an IV without calling setup beforehand. */
2784 TEST_EQUAL( psa_cipher_set_iv( &operation,
2785 iv, sizeof( iv ) ),
2786 PSA_ERROR_BAD_STATE );
2787 PSA_ASSERT( psa_cipher_abort( &operation ) );
2788
2789 /* Set an IV after it's already set. */
2790 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2791 PSA_ASSERT( psa_cipher_set_iv( &operation,
2792 iv, sizeof( iv ) ) );
2793 TEST_EQUAL( psa_cipher_set_iv( &operation,
2794 iv, sizeof( iv ) ),
2795 PSA_ERROR_BAD_STATE );
2796 PSA_ASSERT( psa_cipher_abort( &operation ) );
2797
2798 /* Set an IV after it's already generated. */
2799 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2800 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2801 buffer, sizeof( buffer ),
2802 &length ) );
2803 TEST_EQUAL( psa_cipher_set_iv( &operation,
2804 iv, sizeof( iv ) ),
2805 PSA_ERROR_BAD_STATE );
2806 PSA_ASSERT( psa_cipher_abort( &operation ) );
2807
2808 /* Call update without calling setup beforehand. */
2809 TEST_EQUAL( psa_cipher_update( &operation,
2810 text, sizeof( text ),
2811 buffer, sizeof( buffer ),
2812 &length ),
2813 PSA_ERROR_BAD_STATE );
2814 PSA_ASSERT( psa_cipher_abort( &operation ) );
2815
2816 /* Call update without an IV where an IV is required. */
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 after finish. */
2825 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2826 PSA_ASSERT( psa_cipher_set_iv( &operation,
2827 iv, sizeof( iv ) ) );
2828 PSA_ASSERT( psa_cipher_finish( &operation,
2829 buffer, sizeof( buffer ), &length ) );
2830 TEST_EQUAL( psa_cipher_update( &operation,
2831 text, sizeof( text ),
2832 buffer, sizeof( buffer ),
2833 &length ),
2834 PSA_ERROR_BAD_STATE );
2835 PSA_ASSERT( psa_cipher_abort( &operation ) );
2836
2837 /* Call finish without calling setup beforehand. */
2838 TEST_EQUAL( psa_cipher_finish( &operation,
2839 buffer, sizeof( buffer ), &length ),
2840 PSA_ERROR_BAD_STATE );
2841 PSA_ASSERT( psa_cipher_abort( &operation ) );
2842
2843 /* Call finish without an IV where an IV is required. */
2844 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2845 /* Not calling update means we are encrypting an empty buffer, which is OK
2846 * for cipher modes with padding. */
2847 TEST_EQUAL( psa_cipher_finish( &operation,
2848 buffer, sizeof( buffer ), &length ),
2849 PSA_ERROR_BAD_STATE );
2850 PSA_ASSERT( psa_cipher_abort( &operation ) );
2851
2852 /* Call finish twice in a row. */
2853 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2854 PSA_ASSERT( psa_cipher_set_iv( &operation,
2855 iv, sizeof( iv ) ) );
2856 PSA_ASSERT( psa_cipher_finish( &operation,
2857 buffer, sizeof( buffer ), &length ) );
2858 TEST_EQUAL( psa_cipher_finish( &operation,
2859 buffer, sizeof( buffer ), &length ),
2860 PSA_ERROR_BAD_STATE );
2861 PSA_ASSERT( psa_cipher_abort( &operation ) );
2862
Gilles Peskine76b29a72019-05-28 14:08:50 +02002863 PSA_ASSERT( psa_destroy_key( handle ) );
2864
Jaeden Ameroab439972019-02-15 14:12:05 +00002865exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002866 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002867}
2868/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002869
Gilles Peskine50e586b2018-06-08 14:28:46 +02002870/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002871void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02002872 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002873 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002874 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002875{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002876 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002877 psa_status_t status;
2878 psa_key_type_t key_type = key_type_arg;
2879 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002880 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002881 unsigned char *output = NULL;
2882 size_t output_buffer_size = 0;
2883 size_t function_output_length = 0;
2884 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002885 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002886 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002887
Gilles Peskine8817f612018-12-18 00:18:46 +01002888 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002889
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002890 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2891 psa_set_key_algorithm( &attributes, alg );
2892 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002893
Gilles Peskine73676cb2019-05-15 20:15:10 +02002894 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002895
Gilles Peskine8817f612018-12-18 00:18:46 +01002896 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2897 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002898
Gilles Peskine423005e2019-05-06 15:22:57 +02002899 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002900 output_buffer_size = ( (size_t) input->len +
2901 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002902 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002903
Gilles Peskine8817f612018-12-18 00:18:46 +01002904 PSA_ASSERT( psa_cipher_update( &operation,
2905 input->x, input->len,
2906 output, output_buffer_size,
2907 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002908 total_output_length += function_output_length;
2909 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002910 output + total_output_length,
2911 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002912 &function_output_length );
2913 total_output_length += function_output_length;
2914
Gilles Peskinefe11b722018-12-18 00:24:04 +01002915 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002916 if( expected_status == PSA_SUCCESS )
2917 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002918 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002919 ASSERT_COMPARE( expected_output->x, expected_output->len,
2920 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002921 }
2922
2923exit:
2924 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002925 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002926 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002927}
2928/* END_CASE */
2929
2930/* BEGIN_CASE */
2931void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02002932 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002933 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002934 int first_part_size_arg,
2935 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002936 data_t *expected_output )
2937{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002938 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002939 psa_key_type_t key_type = key_type_arg;
2940 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002941 size_t first_part_size = first_part_size_arg;
2942 size_t output1_length = output1_length_arg;
2943 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002944 unsigned char *output = NULL;
2945 size_t output_buffer_size = 0;
2946 size_t function_output_length = 0;
2947 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002948 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002949 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002950
Gilles Peskine8817f612018-12-18 00:18:46 +01002951 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002952
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002953 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2954 psa_set_key_algorithm( &attributes, alg );
2955 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002956
Gilles Peskine73676cb2019-05-15 20:15:10 +02002957 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002958
Gilles Peskine8817f612018-12-18 00:18:46 +01002959 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2960 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002961
Gilles Peskine423005e2019-05-06 15:22:57 +02002962 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002963 output_buffer_size = ( (size_t) input->len +
2964 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002965 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002966
Gilles Peskinee0866522019-02-19 19:44:00 +01002967 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002968 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2969 output, output_buffer_size,
2970 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002971 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002972 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002973 PSA_ASSERT( psa_cipher_update( &operation,
2974 input->x + first_part_size,
2975 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002976 output + total_output_length,
2977 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002978 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002979 TEST_ASSERT( function_output_length == output2_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_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002982 output + total_output_length,
2983 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002984 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002985 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002986 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002987
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002988 ASSERT_COMPARE( expected_output->x, expected_output->len,
2989 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002990
2991exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03002992 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002993 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002994 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002995}
2996/* END_CASE */
2997
2998/* BEGIN_CASE */
2999void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003000 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003001 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003002 int first_part_size_arg,
3003 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003004 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003005{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003006 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003007
3008 psa_key_type_t key_type = key_type_arg;
3009 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003010 size_t first_part_size = first_part_size_arg;
3011 size_t output1_length = output1_length_arg;
3012 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003013 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003014 size_t output_buffer_size = 0;
3015 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003016 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003017 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003018 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003019
Gilles Peskine8817f612018-12-18 00:18:46 +01003020 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003021
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003022 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3023 psa_set_key_algorithm( &attributes, alg );
3024 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003025
Gilles Peskine73676cb2019-05-15 20:15:10 +02003026 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003027
Gilles Peskine8817f612018-12-18 00:18:46 +01003028 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3029 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003030
Gilles Peskine423005e2019-05-06 15:22:57 +02003031 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003032
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003033 output_buffer_size = ( (size_t) input->len +
3034 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003035 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003036
Gilles Peskinee0866522019-02-19 19:44:00 +01003037 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003038 PSA_ASSERT( psa_cipher_update( &operation,
3039 input->x, first_part_size,
3040 output, output_buffer_size,
3041 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003042 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003043 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003044 PSA_ASSERT( psa_cipher_update( &operation,
3045 input->x + first_part_size,
3046 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003047 output + total_output_length,
3048 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003049 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003050 TEST_ASSERT( function_output_length == output2_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_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003053 output + total_output_length,
3054 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003055 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003056 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003057 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003058
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003059 ASSERT_COMPARE( expected_output->x, expected_output->len,
3060 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003061
3062exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003063 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003064 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003065 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003066}
3067/* END_CASE */
3068
Gilles Peskine50e586b2018-06-08 14:28:46 +02003069/* BEGIN_CASE */
3070void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003071 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003072 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003073 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003074{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003075 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003076 psa_status_t status;
3077 psa_key_type_t key_type = key_type_arg;
3078 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003079 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003080 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003081 size_t output_buffer_size = 0;
3082 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003083 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003084 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003085 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003086
Gilles Peskine8817f612018-12-18 00:18:46 +01003087 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003088
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003089 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3090 psa_set_key_algorithm( &attributes, alg );
3091 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003092
Gilles Peskine73676cb2019-05-15 20:15:10 +02003093 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003094
Gilles Peskine8817f612018-12-18 00:18:46 +01003095 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3096 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003097
Gilles Peskine423005e2019-05-06 15:22:57 +02003098 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003099
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003100 output_buffer_size = ( (size_t) input->len +
3101 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003102 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003103
Gilles Peskine8817f612018-12-18 00:18:46 +01003104 PSA_ASSERT( psa_cipher_update( &operation,
3105 input->x, input->len,
3106 output, output_buffer_size,
3107 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003108 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003109 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003110 output + total_output_length,
3111 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003112 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003113 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003114 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003115
3116 if( expected_status == PSA_SUCCESS )
3117 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003118 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003119 ASSERT_COMPARE( expected_output->x, expected_output->len,
3120 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003121 }
3122
Gilles Peskine50e586b2018-06-08 14:28:46 +02003123exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003124 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003125 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003126 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003127}
3128/* END_CASE */
3129
Gilles Peskine50e586b2018-06-08 14:28:46 +02003130/* BEGIN_CASE */
3131void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003132 data_t *key,
3133 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003134{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003135 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003136 psa_key_type_t key_type = key_type_arg;
3137 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003138 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003139 size_t iv_size = 16;
3140 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003141 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003142 size_t output1_size = 0;
3143 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003144 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003145 size_t output2_size = 0;
3146 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003147 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003148 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3149 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003150 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003151
Gilles Peskine8817f612018-12-18 00:18:46 +01003152 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003153
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003154 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3155 psa_set_key_algorithm( &attributes, alg );
3156 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003157
Gilles Peskine73676cb2019-05-15 20:15:10 +02003158 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003159
Gilles Peskine8817f612018-12-18 00:18:46 +01003160 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3161 handle, alg ) );
3162 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3163 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003164
Gilles Peskine8817f612018-12-18 00:18:46 +01003165 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3166 iv, iv_size,
3167 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003168 output1_size = ( (size_t) input->len +
3169 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003170 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003171
Gilles Peskine8817f612018-12-18 00:18:46 +01003172 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3173 output1, output1_size,
3174 &output1_length ) );
3175 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003176 output1 + output1_length,
3177 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003178 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003179
Gilles Peskine048b7f02018-06-08 14:20:49 +02003180 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003181
Gilles Peskine8817f612018-12-18 00:18:46 +01003182 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003183
3184 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003185 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003186
Gilles Peskine8817f612018-12-18 00:18:46 +01003187 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3188 iv, iv_length ) );
3189 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3190 output2, output2_size,
3191 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003192 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003193 PSA_ASSERT( psa_cipher_finish( &operation2,
3194 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003195 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003196 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003197
Gilles Peskine048b7f02018-06-08 14:20:49 +02003198 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003199
Gilles Peskine8817f612018-12-18 00:18:46 +01003200 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003201
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003202 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003203
3204exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003205 mbedtls_free( output1 );
3206 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003207 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003208 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003209}
3210/* END_CASE */
3211
3212/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003213void cipher_verify_output_multipart( int alg_arg,
3214 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003215 data_t *key,
3216 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003217 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003218{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003219 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003220 psa_key_type_t key_type = key_type_arg;
3221 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003222 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003223 unsigned char iv[16] = {0};
3224 size_t iv_size = 16;
3225 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003226 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003227 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003228 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003229 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003230 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003231 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003232 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003233 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3234 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003235 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003236
Gilles Peskine8817f612018-12-18 00:18:46 +01003237 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003238
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003239 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3240 psa_set_key_algorithm( &attributes, alg );
3241 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003242
Gilles Peskine73676cb2019-05-15 20:15:10 +02003243 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003244
Gilles Peskine8817f612018-12-18 00:18:46 +01003245 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3246 handle, alg ) );
3247 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3248 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003249
Gilles Peskine8817f612018-12-18 00:18:46 +01003250 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3251 iv, iv_size,
3252 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003253 output1_buffer_size = ( (size_t) input->len +
3254 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003255 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003256
Gilles Peskinee0866522019-02-19 19:44:00 +01003257 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003258
Gilles Peskine8817f612018-12-18 00:18:46 +01003259 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3260 output1, output1_buffer_size,
3261 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003262 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003263
Gilles Peskine8817f612018-12-18 00:18:46 +01003264 PSA_ASSERT( psa_cipher_update( &operation1,
3265 input->x + first_part_size,
3266 input->len - first_part_size,
3267 output1, output1_buffer_size,
3268 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003269 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003270
Gilles Peskine8817f612018-12-18 00:18:46 +01003271 PSA_ASSERT( psa_cipher_finish( &operation1,
3272 output1 + output1_length,
3273 output1_buffer_size - output1_length,
3274 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003275 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003276
Gilles Peskine8817f612018-12-18 00:18:46 +01003277 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003278
Gilles Peskine048b7f02018-06-08 14:20:49 +02003279 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003280 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003281
Gilles Peskine8817f612018-12-18 00:18:46 +01003282 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3283 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003284
Gilles Peskine8817f612018-12-18 00:18:46 +01003285 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3286 output2, output2_buffer_size,
3287 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003288 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003289
Gilles Peskine8817f612018-12-18 00:18:46 +01003290 PSA_ASSERT( psa_cipher_update( &operation2,
3291 output1 + first_part_size,
3292 output1_length - first_part_size,
3293 output2, output2_buffer_size,
3294 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003295 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003296
Gilles Peskine8817f612018-12-18 00:18:46 +01003297 PSA_ASSERT( psa_cipher_finish( &operation2,
3298 output2 + output2_length,
3299 output2_buffer_size - output2_length,
3300 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003301 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003302
Gilles Peskine8817f612018-12-18 00:18:46 +01003303 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003304
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003305 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003306
3307exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003308 mbedtls_free( output1 );
3309 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003310 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003311 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003312}
3313/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003314
Gilles Peskine20035e32018-02-03 22:44:14 +01003315/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003316void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003317 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003318 data_t *nonce,
3319 data_t *additional_data,
3320 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003321 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003322{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003323 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003324 psa_key_type_t key_type = key_type_arg;
3325 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003326 unsigned char *output_data = NULL;
3327 size_t output_size = 0;
3328 size_t output_length = 0;
3329 unsigned char *output_data2 = NULL;
3330 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003331 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003332 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003333 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003334
Gilles Peskine4abf7412018-06-18 16:35:34 +02003335 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003336 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3337 * should be exact. */
3338 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3339 TEST_EQUAL( output_size,
3340 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003341 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003342
Gilles Peskine8817f612018-12-18 00:18:46 +01003343 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003344
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003345 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3346 psa_set_key_algorithm( &attributes, alg );
3347 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003348
Gilles Peskine049c7532019-05-15 20:22:09 +02003349 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3350 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003351
Gilles Peskinefe11b722018-12-18 00:24:04 +01003352 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3353 nonce->x, nonce->len,
3354 additional_data->x,
3355 additional_data->len,
3356 input_data->x, input_data->len,
3357 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003358 &output_length ),
3359 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003360
3361 if( PSA_SUCCESS == expected_result )
3362 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003363 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003364
Gilles Peskine003a4a92019-05-14 16:09:40 +02003365 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3366 * should be exact. */
3367 TEST_EQUAL( input_data->len,
3368 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3369
Gilles Peskinefe11b722018-12-18 00:24:04 +01003370 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3371 nonce->x, nonce->len,
3372 additional_data->x,
3373 additional_data->len,
3374 output_data, output_length,
3375 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003376 &output_length2 ),
3377 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003378
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003379 ASSERT_COMPARE( input_data->x, input_data->len,
3380 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003381 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003382
Gilles Peskinea1cac842018-06-11 19:33:02 +02003383exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003384 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003385 mbedtls_free( output_data );
3386 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003387 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003388}
3389/* END_CASE */
3390
3391/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003392void aead_encrypt( int key_type_arg, data_t *key_data,
3393 int alg_arg,
3394 data_t *nonce,
3395 data_t *additional_data,
3396 data_t *input_data,
3397 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003398{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003399 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003400 psa_key_type_t key_type = key_type_arg;
3401 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003402 unsigned char *output_data = NULL;
3403 size_t output_size = 0;
3404 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003405 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003406 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003407
Gilles Peskine4abf7412018-06-18 16:35:34 +02003408 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003409 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3410 * should be exact. */
3411 TEST_EQUAL( output_size,
3412 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003413 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003414
Gilles Peskine8817f612018-12-18 00:18:46 +01003415 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003416
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003417 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3418 psa_set_key_algorithm( &attributes, alg );
3419 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003420
Gilles Peskine049c7532019-05-15 20:22:09 +02003421 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3422 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003423
Gilles Peskine8817f612018-12-18 00:18:46 +01003424 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3425 nonce->x, nonce->len,
3426 additional_data->x, additional_data->len,
3427 input_data->x, input_data->len,
3428 output_data, output_size,
3429 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003430
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003431 ASSERT_COMPARE( expected_result->x, expected_result->len,
3432 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003433
Gilles Peskinea1cac842018-06-11 19:33:02 +02003434exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003435 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003436 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003437 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003438}
3439/* END_CASE */
3440
3441/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003442void aead_decrypt( int key_type_arg, data_t *key_data,
3443 int alg_arg,
3444 data_t *nonce,
3445 data_t *additional_data,
3446 data_t *input_data,
3447 data_t *expected_data,
3448 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003449{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003450 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003451 psa_key_type_t key_type = key_type_arg;
3452 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003453 unsigned char *output_data = NULL;
3454 size_t output_size = 0;
3455 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003456 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003457 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003458 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003459
Gilles Peskine003a4a92019-05-14 16:09:40 +02003460 output_size = input_data->len - tag_length;
3461 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3462 * should be exact. */
3463 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3464 TEST_EQUAL( output_size,
3465 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003466 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003467
Gilles Peskine8817f612018-12-18 00:18:46 +01003468 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003469
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003470 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3471 psa_set_key_algorithm( &attributes, alg );
3472 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003473
Gilles Peskine049c7532019-05-15 20:22:09 +02003474 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3475 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003476
Gilles Peskinefe11b722018-12-18 00:24:04 +01003477 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3478 nonce->x, nonce->len,
3479 additional_data->x,
3480 additional_data->len,
3481 input_data->x, input_data->len,
3482 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003483 &output_length ),
3484 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003485
Gilles Peskine2d277862018-06-18 15:41:12 +02003486 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003487 ASSERT_COMPARE( expected_data->x, expected_data->len,
3488 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003489
Gilles Peskinea1cac842018-06-11 19:33:02 +02003490exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003491 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003492 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003493 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003494}
3495/* END_CASE */
3496
3497/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003498void signature_size( int type_arg,
3499 int bits,
3500 int alg_arg,
3501 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003502{
3503 psa_key_type_t type = type_arg;
3504 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003505 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003506 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003507exit:
3508 ;
3509}
3510/* END_CASE */
3511
3512/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003513void sign_deterministic( int key_type_arg, data_t *key_data,
3514 int alg_arg, data_t *input_data,
3515 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003516{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003517 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003518 psa_key_type_t key_type = key_type_arg;
3519 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003520 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003521 unsigned char *signature = NULL;
3522 size_t signature_size;
3523 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003524 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003525
Gilles Peskine8817f612018-12-18 00:18:46 +01003526 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003527
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003528 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3529 psa_set_key_algorithm( &attributes, alg );
3530 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003531
Gilles Peskine049c7532019-05-15 20:22:09 +02003532 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3533 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003534 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3535 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003536
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003537 /* Allocate a buffer which has the size advertized by the
3538 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003539 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3540 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003541 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003542 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003543 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003544
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003545 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003546 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3547 input_data->x, input_data->len,
3548 signature, signature_size,
3549 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003550 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003551 ASSERT_COMPARE( output_data->x, output_data->len,
3552 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003553
3554exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003555 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003556 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003557 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003558 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003559}
3560/* END_CASE */
3561
3562/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003563void sign_fail( int key_type_arg, data_t *key_data,
3564 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003565 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003566{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003567 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003568 psa_key_type_t key_type = key_type_arg;
3569 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003570 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003571 psa_status_t actual_status;
3572 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003573 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003574 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003575 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003576
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003577 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003578
Gilles Peskine8817f612018-12-18 00:18:46 +01003579 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003580
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003581 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3582 psa_set_key_algorithm( &attributes, alg );
3583 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003584
Gilles Peskine049c7532019-05-15 20:22:09 +02003585 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3586 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003587
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003588 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003589 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003590 signature, signature_size,
3591 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003592 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003593 /* The value of *signature_length is unspecified on error, but
3594 * whatever it is, it should be less than signature_size, so that
3595 * if the caller tries to read *signature_length bytes without
3596 * checking the error code then they don't overflow a buffer. */
3597 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003598
3599exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003600 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003601 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003602 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003603 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003604}
3605/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003606
3607/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003608void sign_verify( int key_type_arg, data_t *key_data,
3609 int alg_arg, data_t *input_data )
3610{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003611 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003612 psa_key_type_t key_type = key_type_arg;
3613 psa_algorithm_t alg = alg_arg;
3614 size_t key_bits;
3615 unsigned char *signature = NULL;
3616 size_t signature_size;
3617 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003618 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003619
Gilles Peskine8817f612018-12-18 00:18:46 +01003620 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003621
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003622 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
3623 psa_set_key_algorithm( &attributes, alg );
3624 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003625
Gilles Peskine049c7532019-05-15 20:22:09 +02003626 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3627 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003628 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3629 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003630
3631 /* Allocate a buffer which has the size advertized by the
3632 * library. */
3633 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3634 key_bits, alg );
3635 TEST_ASSERT( signature_size != 0 );
3636 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003637 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003638
3639 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003640 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3641 input_data->x, input_data->len,
3642 signature, signature_size,
3643 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003644 /* Check that the signature length looks sensible. */
3645 TEST_ASSERT( signature_length <= signature_size );
3646 TEST_ASSERT( signature_length > 0 );
3647
3648 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003649 PSA_ASSERT( psa_asymmetric_verify(
3650 handle, alg,
3651 input_data->x, input_data->len,
3652 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003653
3654 if( input_data->len != 0 )
3655 {
3656 /* Flip a bit in the input and verify that the signature is now
3657 * detected as invalid. Flip a bit at the beginning, not at the end,
3658 * because ECDSA may ignore the last few bits of the input. */
3659 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003660 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3661 input_data->x, input_data->len,
3662 signature, signature_length ),
3663 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003664 }
3665
3666exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003667 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003668 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003669 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003670 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003671}
3672/* END_CASE */
3673
3674/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003675void asymmetric_verify( int key_type_arg, data_t *key_data,
3676 int alg_arg, data_t *hash_data,
3677 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003678{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003679 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003680 psa_key_type_t key_type = key_type_arg;
3681 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003682 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003683
Gilles Peskine69c12672018-06-28 00:07:19 +02003684 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3685
Gilles Peskine8817f612018-12-18 00:18:46 +01003686 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003687
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003688 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3689 psa_set_key_algorithm( &attributes, alg );
3690 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003691
Gilles Peskine049c7532019-05-15 20:22:09 +02003692 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3693 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03003694
Gilles Peskine8817f612018-12-18 00:18:46 +01003695 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3696 hash_data->x, hash_data->len,
3697 signature_data->x,
3698 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003699exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003700 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003701 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003702 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003703}
3704/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003705
3706/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003707void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3708 int alg_arg, data_t *hash_data,
3709 data_t *signature_data,
3710 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003711{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003712 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003713 psa_key_type_t key_type = key_type_arg;
3714 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003715 psa_status_t actual_status;
3716 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003717 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003718
Gilles Peskine8817f612018-12-18 00:18:46 +01003719 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003720
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003721 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3722 psa_set_key_algorithm( &attributes, alg );
3723 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003724
Gilles Peskine049c7532019-05-15 20:22:09 +02003725 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3726 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003727
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003728 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003729 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003730 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003731 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003732
Gilles Peskinefe11b722018-12-18 00:24:04 +01003733 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003734
3735exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003736 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003737 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003738 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003739}
3740/* END_CASE */
3741
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003742/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003743void asymmetric_encrypt( int key_type_arg,
3744 data_t *key_data,
3745 int alg_arg,
3746 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003747 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003748 int expected_output_length_arg,
3749 int expected_status_arg )
3750{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003751 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003752 psa_key_type_t key_type = key_type_arg;
3753 psa_algorithm_t alg = alg_arg;
3754 size_t expected_output_length = expected_output_length_arg;
3755 size_t key_bits;
3756 unsigned char *output = NULL;
3757 size_t output_size;
3758 size_t output_length = ~0;
3759 psa_status_t actual_status;
3760 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003761 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003762
Gilles Peskine8817f612018-12-18 00:18:46 +01003763 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003764
Gilles Peskine656896e2018-06-29 19:12:28 +02003765 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003766 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3767 psa_set_key_algorithm( &attributes, alg );
3768 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003769 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3770 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003771
3772 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003773 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3774 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02003775 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003776 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003777
3778 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003779 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003780 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003781 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003782 output, output_size,
3783 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003784 TEST_EQUAL( actual_status, expected_status );
3785 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003786
Gilles Peskine68428122018-06-30 18:42:41 +02003787 /* If the label is empty, the test framework puts a non-null pointer
3788 * in label->x. Test that a null pointer works as well. */
3789 if( label->len == 0 )
3790 {
3791 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003792 if( output_size != 0 )
3793 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003794 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003795 input_data->x, input_data->len,
3796 NULL, label->len,
3797 output, output_size,
3798 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003799 TEST_EQUAL( actual_status, expected_status );
3800 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003801 }
3802
Gilles Peskine656896e2018-06-29 19:12:28 +02003803exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003804 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003805 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003806 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003807 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003808}
3809/* END_CASE */
3810
3811/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003812void asymmetric_encrypt_decrypt( int key_type_arg,
3813 data_t *key_data,
3814 int alg_arg,
3815 data_t *input_data,
3816 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003817{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003818 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003819 psa_key_type_t key_type = key_type_arg;
3820 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003821 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003822 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003823 size_t output_size;
3824 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003825 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003826 size_t output2_size;
3827 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003828 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003829
Gilles Peskine8817f612018-12-18 00:18:46 +01003830 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003831
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003832 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3833 psa_set_key_algorithm( &attributes, alg );
3834 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003835
Gilles Peskine049c7532019-05-15 20:22:09 +02003836 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3837 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003838
3839 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003840 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3841 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003842 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003843 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003844 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003845 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003846
Gilles Peskineeebd7382018-06-08 18:11:54 +02003847 /* We test encryption by checking that encrypt-then-decrypt gives back
3848 * the original plaintext because of the non-optional random
3849 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003850 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3851 input_data->x, input_data->len,
3852 label->x, label->len,
3853 output, output_size,
3854 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003855 /* We don't know what ciphertext length to expect, but check that
3856 * it looks sensible. */
3857 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003858
Gilles Peskine8817f612018-12-18 00:18:46 +01003859 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3860 output, output_length,
3861 label->x, label->len,
3862 output2, output2_size,
3863 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003864 ASSERT_COMPARE( input_data->x, input_data->len,
3865 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003866
3867exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003868 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003869 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003870 mbedtls_free( output );
3871 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003872 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003873}
3874/* END_CASE */
3875
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003876/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003877void asymmetric_decrypt( int key_type_arg,
3878 data_t *key_data,
3879 int alg_arg,
3880 data_t *input_data,
3881 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003882 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003883{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003884 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003885 psa_key_type_t key_type = key_type_arg;
3886 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003887 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003888 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003889 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003890 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003891
Jaeden Amero412654a2019-02-06 12:57:46 +00003892 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003893 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003894
Gilles Peskine8817f612018-12-18 00:18:46 +01003895 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003896
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003897 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3898 psa_set_key_algorithm( &attributes, alg );
3899 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003900
Gilles Peskine049c7532019-05-15 20:22:09 +02003901 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3902 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003903
Gilles Peskine8817f612018-12-18 00:18:46 +01003904 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3905 input_data->x, input_data->len,
3906 label->x, label->len,
3907 output,
3908 output_size,
3909 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003910 ASSERT_COMPARE( expected_data->x, expected_data->len,
3911 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003912
Gilles Peskine68428122018-06-30 18:42:41 +02003913 /* If the label is empty, the test framework puts a non-null pointer
3914 * in label->x. Test that a null pointer works as well. */
3915 if( label->len == 0 )
3916 {
3917 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003918 if( output_size != 0 )
3919 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003920 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3921 input_data->x, input_data->len,
3922 NULL, label->len,
3923 output,
3924 output_size,
3925 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003926 ASSERT_COMPARE( expected_data->x, expected_data->len,
3927 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003928 }
3929
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003930exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003931 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003932 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003933 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003934 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003935}
3936/* END_CASE */
3937
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003938/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003939void asymmetric_decrypt_fail( int key_type_arg,
3940 data_t *key_data,
3941 int alg_arg,
3942 data_t *input_data,
3943 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003944 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003945 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003946{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003947 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003948 psa_key_type_t key_type = key_type_arg;
3949 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003950 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003951 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003952 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003953 psa_status_t actual_status;
3954 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003955 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003956
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003957 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003958
Gilles Peskine8817f612018-12-18 00:18:46 +01003959 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003960
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003961 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3962 psa_set_key_algorithm( &attributes, alg );
3963 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003964
Gilles Peskine049c7532019-05-15 20:22:09 +02003965 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3966 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003967
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003968 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003969 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003970 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003971 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003972 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003973 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003974 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003975
Gilles Peskine68428122018-06-30 18:42:41 +02003976 /* If the label is empty, the test framework puts a non-null pointer
3977 * in label->x. Test that a null pointer works as well. */
3978 if( label->len == 0 )
3979 {
3980 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003981 if( output_size != 0 )
3982 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003983 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003984 input_data->x, input_data->len,
3985 NULL, label->len,
3986 output, output_size,
3987 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003988 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003989 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02003990 }
3991
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003992exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003993 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003994 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02003995 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003996 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003997}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003998/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02003999
4000/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004001void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004002{
4003 /* Test each valid way of initializing the object, except for `= {0}`, as
4004 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4005 * though it's OK by the C standard. We could test for this, but we'd need
4006 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004007 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004008 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4009 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4010 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004011
4012 memset( &zero, 0, sizeof( zero ) );
4013
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004014 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004015 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004016 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004017 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004018 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004019 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004020 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004021
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004022 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004023 PSA_ASSERT( psa_key_derivation_abort(&func) );
4024 PSA_ASSERT( psa_key_derivation_abort(&init) );
4025 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004026}
4027/* END_CASE */
4028
Janos Follath71a4c912019-06-11 09:14:47 +01004029/* BEGIN_CASE depends_on:PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004030void derive_setup( int key_type_arg,
4031 data_t *key_data,
4032 int alg_arg,
4033 data_t *salt,
4034 data_t *label,
4035 int requested_capacity_arg,
4036 int expected_status_arg )
4037{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004038 psa_key_handle_t handle = 0;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004039 size_t key_type = key_type_arg;
4040 psa_algorithm_t alg = alg_arg;
4041 size_t requested_capacity = requested_capacity_arg;
4042 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004043 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004044 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004045
Gilles Peskine8817f612018-12-18 00:18:46 +01004046 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004047
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004048 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4049 psa_set_key_algorithm( &attributes, alg );
4050 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004051
Gilles Peskine049c7532019-05-15 20:22:09 +02004052 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4053 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004054
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004055 TEST_EQUAL( psa_key_derivation( &operation, handle, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004056 salt->x, salt->len,
4057 label->x, label->len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004058 requested_capacity ),
4059 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004060
4061exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004062 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004063 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004064 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004065}
4066/* END_CASE */
4067
Janos Follath71a4c912019-06-11 09:14:47 +01004068/* BEGIN_CASE depends_on:PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004069void test_derive_invalid_key_derivation_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004070{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004071 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004072 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004073 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004074 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004075 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004076 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004077 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4078 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4079 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004080 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004081
Gilles Peskine8817f612018-12-18 00:18:46 +01004082 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004083
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004084 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4085 psa_set_key_algorithm( &attributes, alg );
4086 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004087
Gilles Peskine73676cb2019-05-15 20:15:10 +02004088 PSA_ASSERT( psa_import_key( &attributes,
4089 key_data, sizeof( key_data ),
4090 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004091
4092 /* valid key derivation */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004093 PSA_ASSERT( psa_key_derivation( &operation, handle, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004094 NULL, 0,
4095 NULL, 0,
4096 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004097
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004098 /* state of operation shouldn't allow additional generation */
4099 TEST_EQUAL( psa_key_derivation( &operation, handle, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004100 NULL, 0,
4101 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004102 capacity ),
4103 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004104
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004105 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004106
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004107 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004108 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004109
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004110exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004111 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004112 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004113 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004114}
4115/* END_CASE */
4116
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004117/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004118void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004119{
4120 uint8_t output_buffer[16];
4121 size_t buffer_size = 16;
4122 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004123 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004124
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004125 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4126 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004127 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004128
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004129 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004130 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004131
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004132 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004133
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004134 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4135 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004136 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004137
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004138 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004139 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004140
4141exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004142 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004143}
4144/* END_CASE */
4145
4146/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004147void derive_output( int alg_arg,
4148 data_t *key_data,
4149 data_t *salt,
4150 data_t *label,
4151 int requested_capacity_arg,
4152 data_t *expected_output1,
4153 data_t *expected_output2 )
4154{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004155 psa_key_handle_t handle = 0;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004156 psa_algorithm_t alg = alg_arg;
4157 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004158 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004159 uint8_t *expected_outputs[2] =
4160 {expected_output1->x, expected_output2->x};
4161 size_t output_sizes[2] =
4162 {expected_output1->len, expected_output2->len};
4163 size_t output_buffer_size = 0;
4164 uint8_t *output_buffer = NULL;
4165 size_t expected_capacity;
4166 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004168 psa_status_t status;
4169 unsigned i;
4170
4171 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4172 {
4173 if( output_sizes[i] > output_buffer_size )
4174 output_buffer_size = output_sizes[i];
4175 if( output_sizes[i] == 0 )
4176 expected_outputs[i] = NULL;
4177 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004178 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004179 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004180
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004181 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4182 psa_set_key_algorithm( &attributes, alg );
4183 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004184
Gilles Peskine049c7532019-05-15 20:22:09 +02004185 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4186 &handle ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004187
4188 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004189 if( PSA_ALG_IS_HKDF( alg ) )
4190 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004191 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4192 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004193 requested_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004194 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004195 PSA_KEY_DERIVATION_INPUT_SALT,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004196 salt->x, salt->len ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004197 PSA_ASSERT( psa_key_derivation_input_key( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004198 PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004199 handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004200 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004201 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004202 label->x, label->len ) );
4203 }
Janos Follath71a4c912019-06-11 09:14:47 +01004204#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004205 else
4206 {
4207 // legacy
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004208 PSA_ASSERT( psa_key_derivation( &operation, handle, alg,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004209 salt->x, salt->len,
4210 label->x, label->len,
4211 requested_capacity ) );
4212 }
Janos Follath71a4c912019-06-11 09:14:47 +01004213#endif
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004214 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004215 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004216 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004217 expected_capacity = requested_capacity;
4218
4219 /* Expansion phase. */
4220 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4221 {
4222 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004223 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004224 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004225 if( expected_capacity == 0 && output_sizes[i] == 0 )
4226 {
4227 /* Reading 0 bytes when 0 bytes are available can go either way. */
4228 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004229 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004230 continue;
4231 }
4232 else if( expected_capacity == 0 ||
4233 output_sizes[i] > expected_capacity )
4234 {
4235 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004236 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004237 expected_capacity = 0;
4238 continue;
4239 }
4240 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004241 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004242 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004243 ASSERT_COMPARE( output_buffer, output_sizes[i],
4244 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004245 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004246 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004247 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004248 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004249 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004250 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004251 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004252
4253exit:
4254 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004255 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004256 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004257 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004258}
4259/* END_CASE */
4260
4261/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004262void derive_full( int alg_arg,
4263 data_t *key_data,
4264 data_t *salt,
4265 data_t *label,
4266 int requested_capacity_arg )
4267{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004268 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004269 psa_algorithm_t alg = alg_arg;
4270 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004271 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004272 unsigned char output_buffer[16];
4273 size_t expected_capacity = requested_capacity;
4274 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004275 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004276
Gilles Peskine8817f612018-12-18 00:18:46 +01004277 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004278
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004279 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4280 psa_set_key_algorithm( &attributes, alg );
4281 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004282
Gilles Peskine049c7532019-05-15 20:22:09 +02004283 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4284 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004285
4286 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004287 if( PSA_ALG_IS_HKDF( alg ) )
4288 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004289 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4290 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004291 requested_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004292 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004293 PSA_KEY_DERIVATION_INPUT_SALT,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004294 salt->x, salt->len ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004295 PSA_ASSERT( psa_key_derivation_input_key( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004296 PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004297 handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004298 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004299 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004300 label->x, label->len ) );
4301 }
Janos Follath71a4c912019-06-11 09:14:47 +01004302
4303#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004304 else
4305 {
4306 // legacy
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004307 PSA_ASSERT( psa_key_derivation( &operation, handle, alg,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004308 salt->x, salt->len,
4309 label->x, label->len,
4310 requested_capacity ) );
4311 }
Janos Follath71a4c912019-06-11 09:14:47 +01004312#endif
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004313 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004314 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004315 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004316
4317 /* Expansion phase. */
4318 while( current_capacity > 0 )
4319 {
4320 size_t read_size = sizeof( output_buffer );
4321 if( read_size > current_capacity )
4322 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004323 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004324 output_buffer,
4325 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004326 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004327 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004328 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004329 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004330 }
4331
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004332 /* Check that the operation refuses to go over capacity. */
4333 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004334 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004335
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004336 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004337
4338exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004339 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004340 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004341 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004342}
4343/* END_CASE */
4344
Janos Follath71a4c912019-06-11 09:14:47 +01004345/* BEGIN_CASE depends_on:PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004346void derive_key_exercise( int alg_arg,
4347 data_t *key_data,
4348 data_t *salt,
4349 data_t *label,
4350 int derived_type_arg,
4351 int derived_bits_arg,
4352 int derived_usage_arg,
4353 int derived_alg_arg )
4354{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004355 psa_key_handle_t base_handle = 0;
4356 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004357 psa_algorithm_t alg = alg_arg;
4358 psa_key_type_t derived_type = derived_type_arg;
4359 size_t derived_bits = derived_bits_arg;
4360 psa_key_usage_t derived_usage = derived_usage_arg;
4361 psa_algorithm_t derived_alg = derived_alg_arg;
4362 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004363 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004364 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004365 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004366
Gilles Peskine8817f612018-12-18 00:18:46 +01004367 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004368
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004369 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4370 psa_set_key_algorithm( &attributes, alg );
4371 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004372 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4373 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004374
4375 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004376 PSA_ASSERT( psa_key_derivation( &operation, base_handle, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004377 salt->x, salt->len,
4378 label->x, label->len,
4379 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004380 psa_set_key_usage_flags( &attributes, derived_usage );
4381 psa_set_key_algorithm( &attributes, derived_alg );
4382 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004383 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004384 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004385 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004386
4387 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004388 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4389 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4390 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004391
4392 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004393 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004394 goto exit;
4395
4396exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004397 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004398 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004399 psa_destroy_key( base_handle );
4400 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004401 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004402}
4403/* END_CASE */
4404
Janos Follath71a4c912019-06-11 09:14:47 +01004405/* BEGIN_CASE depends_on:PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004406void derive_key_export( int alg_arg,
4407 data_t *key_data,
4408 data_t *salt,
4409 data_t *label,
4410 int bytes1_arg,
4411 int bytes2_arg )
4412{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004413 psa_key_handle_t base_handle = 0;
4414 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004415 psa_algorithm_t alg = alg_arg;
4416 size_t bytes1 = bytes1_arg;
4417 size_t bytes2 = bytes2_arg;
4418 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004419 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004420 uint8_t *output_buffer = NULL;
4421 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004422 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4423 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004424 size_t length;
4425
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004426 ASSERT_ALLOC( output_buffer, capacity );
4427 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004428 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004429
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004430 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4431 psa_set_key_algorithm( &base_attributes, alg );
4432 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004433 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4434 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004435
4436 /* Derive some material and output it. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004437 PSA_ASSERT( psa_key_derivation( &operation, base_handle, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004438 salt->x, salt->len,
4439 label->x, label->len,
4440 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004441 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004442 output_buffer,
4443 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004444 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004445
4446 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004447 PSA_ASSERT( psa_key_derivation( &operation, base_handle, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004448 salt->x, salt->len,
4449 label->x, label->len,
4450 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004451 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4452 psa_set_key_algorithm( &derived_attributes, 0 );
4453 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004454 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004455 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004456 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004457 PSA_ASSERT( psa_export_key( derived_handle,
4458 export_buffer, bytes1,
4459 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004460 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004461 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004462 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004463 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004464 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004465 PSA_ASSERT( psa_export_key( derived_handle,
4466 export_buffer + bytes1, bytes2,
4467 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004468 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004469
4470 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004471 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4472 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004473
4474exit:
4475 mbedtls_free( output_buffer );
4476 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004477 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004478 psa_destroy_key( base_handle );
4479 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004480 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004481}
4482/* END_CASE */
4483
4484/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004485void key_agreement_setup( int alg_arg,
4486 int our_key_type_arg, data_t *our_key_data,
4487 data_t *peer_key_data,
4488 int expected_status_arg )
4489{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004490 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004491 psa_algorithm_t alg = alg_arg;
4492 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004493 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004494 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004495 psa_status_t expected_status = expected_status_arg;
4496 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004497
Gilles Peskine8817f612018-12-18 00:18:46 +01004498 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004499
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004500 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4501 psa_set_key_algorithm( &attributes, alg );
4502 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004503 PSA_ASSERT( psa_import_key( &attributes,
4504 our_key_data->x, our_key_data->len,
4505 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004506
Gilles Peskine77f40d82019-04-11 21:27:06 +02004507 /* The tests currently include inputs that should fail at either step.
4508 * Test cases that fail at the setup step should be changed to call
4509 * key_derivation_setup instead, and this function should be renamed
4510 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004511 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004512 if( status == PSA_SUCCESS )
4513 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004514 TEST_EQUAL( psa_key_derivation_key_agreement(
4515 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4516 our_key,
4517 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004518 expected_status );
4519 }
4520 else
4521 {
4522 TEST_ASSERT( status == expected_status );
4523 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004524
4525exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004526 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004527 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004528 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004529}
4530/* END_CASE */
4531
4532/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004533void raw_key_agreement( int alg_arg,
4534 int our_key_type_arg, data_t *our_key_data,
4535 data_t *peer_key_data,
4536 data_t *expected_output )
4537{
4538 psa_key_handle_t our_key = 0;
4539 psa_algorithm_t alg = alg_arg;
4540 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004541 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004542 unsigned char *output = NULL;
4543 size_t output_length = ~0;
4544
4545 ASSERT_ALLOC( output, expected_output->len );
4546 PSA_ASSERT( psa_crypto_init( ) );
4547
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004548 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4549 psa_set_key_algorithm( &attributes, alg );
4550 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004551 PSA_ASSERT( psa_import_key( &attributes,
4552 our_key_data->x, our_key_data->len,
4553 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004554
Gilles Peskinebe697d82019-05-16 18:00:41 +02004555 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4556 peer_key_data->x, peer_key_data->len,
4557 output, expected_output->len,
4558 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004559 ASSERT_COMPARE( output, output_length,
4560 expected_output->x, expected_output->len );
4561
4562exit:
4563 mbedtls_free( output );
4564 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004565 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004566}
4567/* END_CASE */
4568
4569/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004570void key_agreement_capacity( int alg_arg,
4571 int our_key_type_arg, data_t *our_key_data,
4572 data_t *peer_key_data,
4573 int expected_capacity_arg )
4574{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004575 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004576 psa_algorithm_t alg = alg_arg;
4577 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004578 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004579 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004580 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004581 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004582
Gilles Peskine8817f612018-12-18 00:18:46 +01004583 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004584
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004585 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4586 psa_set_key_algorithm( &attributes, alg );
4587 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004588 PSA_ASSERT( psa_import_key( &attributes,
4589 our_key_data->x, our_key_data->len,
4590 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004591
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004592 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004593 PSA_ASSERT( psa_key_derivation_key_agreement(
4594 &operation,
4595 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4596 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004597 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4598 {
4599 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004600 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004601 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004602 NULL, 0 ) );
4603 }
Gilles Peskine59685592018-09-18 12:11:34 +02004604
Gilles Peskinebf491972018-10-25 22:36:12 +02004605 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004606 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004607 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004608 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004609
Gilles Peskinebf491972018-10-25 22:36:12 +02004610 /* Test the actual capacity by reading the output. */
4611 while( actual_capacity > sizeof( output ) )
4612 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004613 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004614 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004615 actual_capacity -= sizeof( output );
4616 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004617 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004618 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004619 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004620 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004621
Gilles Peskine59685592018-09-18 12:11:34 +02004622exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004623 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004624 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004625 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004626}
4627/* END_CASE */
4628
4629/* BEGIN_CASE */
4630void key_agreement_output( int alg_arg,
4631 int our_key_type_arg, data_t *our_key_data,
4632 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004633 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004634{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004635 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004636 psa_algorithm_t alg = alg_arg;
4637 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004638 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004639 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004640 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004641
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004642 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4643 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004644
Gilles Peskine8817f612018-12-18 00:18:46 +01004645 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004646
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004647 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4648 psa_set_key_algorithm( &attributes, alg );
4649 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004650 PSA_ASSERT( psa_import_key( &attributes,
4651 our_key_data->x, our_key_data->len,
4652 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004653
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004654 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004655 PSA_ASSERT( psa_key_derivation_key_agreement(
4656 &operation,
4657 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4658 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004659 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4660 {
4661 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004662 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004663 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004664 NULL, 0 ) );
4665 }
Gilles Peskine59685592018-09-18 12:11:34 +02004666
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004667 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004668 actual_output,
4669 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004670 ASSERT_COMPARE( actual_output, expected_output1->len,
4671 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004672 if( expected_output2->len != 0 )
4673 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004674 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004675 actual_output,
4676 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004677 ASSERT_COMPARE( actual_output, expected_output2->len,
4678 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004679 }
Gilles Peskine59685592018-09-18 12:11:34 +02004680
4681exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004682 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004683 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004684 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004685 mbedtls_free( actual_output );
4686}
4687/* END_CASE */
4688
4689/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004690void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004691{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004692 size_t bytes = bytes_arg;
4693 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004694 unsigned char *output = NULL;
4695 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004696 size_t i;
4697 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004698
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004699 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4700 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004701 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004702
Gilles Peskine8817f612018-12-18 00:18:46 +01004703 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004704
Gilles Peskinea50d7392018-06-21 10:22:13 +02004705 /* Run several times, to ensure that every output byte will be
4706 * nonzero at least once with overwhelming probability
4707 * (2^(-8*number_of_runs)). */
4708 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004709 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004710 if( bytes != 0 )
4711 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004712 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004713
4714 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004715 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4716 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004717
4718 for( i = 0; i < bytes; i++ )
4719 {
4720 if( output[i] != 0 )
4721 ++changed[i];
4722 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004723 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004724
4725 /* Check that every byte was changed to nonzero at least once. This
4726 * validates that psa_generate_random is overwriting every byte of
4727 * the output buffer. */
4728 for( i = 0; i < bytes; i++ )
4729 {
4730 TEST_ASSERT( changed[i] != 0 );
4731 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004732
4733exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004734 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004735 mbedtls_free( output );
4736 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004737}
4738/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004739
4740/* BEGIN_CASE */
4741void generate_key( int type_arg,
4742 int bits_arg,
4743 int usage_arg,
4744 int alg_arg,
4745 int expected_status_arg )
4746{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004747 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004748 psa_key_type_t type = type_arg;
4749 psa_key_usage_t usage = usage_arg;
4750 size_t bits = bits_arg;
4751 psa_algorithm_t alg = alg_arg;
4752 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004753 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004754 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004755
Gilles Peskine8817f612018-12-18 00:18:46 +01004756 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004757
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004758 psa_set_key_usage_flags( &attributes, usage );
4759 psa_set_key_algorithm( &attributes, alg );
4760 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004761 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004762
4763 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004764 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004765 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004766 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004767
4768 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004769 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
4770 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4771 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004772
Gilles Peskine818ca122018-06-20 18:16:48 +02004773 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004774 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004775 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004776
4777exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004778 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004779 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004780 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004781}
4782/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004783
Gilles Peskinee56e8782019-04-26 17:34:02 +02004784/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
4785void generate_key_rsa( int bits_arg,
4786 data_t *e_arg,
4787 int expected_status_arg )
4788{
4789 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004790 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02004791 size_t bits = bits_arg;
4792 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4793 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4794 psa_status_t expected_status = expected_status_arg;
4795 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4796 uint8_t *exported = NULL;
4797 size_t exported_size =
4798 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
4799 size_t exported_length = SIZE_MAX;
4800 uint8_t *e_read_buffer = NULL;
4801 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004802 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004803 size_t e_read_length = SIZE_MAX;
4804
4805 if( e_arg->len == 0 ||
4806 ( e_arg->len == 3 &&
4807 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4808 {
4809 is_default_public_exponent = 1;
4810 e_read_size = 0;
4811 }
4812 ASSERT_ALLOC( e_read_buffer, e_read_size );
4813 ASSERT_ALLOC( exported, exported_size );
4814
4815 PSA_ASSERT( psa_crypto_init( ) );
4816
4817 psa_set_key_usage_flags( &attributes, usage );
4818 psa_set_key_algorithm( &attributes, alg );
4819 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4820 e_arg->x, e_arg->len ) );
4821 psa_set_key_bits( &attributes, bits );
4822
4823 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004824 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004825 if( expected_status != PSA_SUCCESS )
4826 goto exit;
4827
4828 /* Test the key information */
4829 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4830 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4831 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4832 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4833 e_read_buffer, e_read_size,
4834 &e_read_length ) );
4835 if( is_default_public_exponent )
4836 TEST_EQUAL( e_read_length, 0 );
4837 else
4838 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4839
4840 /* Do something with the key according to its type and permitted usage. */
4841 if( ! exercise_key( handle, usage, alg ) )
4842 goto exit;
4843
4844 /* Export the key and check the public exponent. */
4845 PSA_ASSERT( psa_export_public_key( handle,
4846 exported, exported_size,
4847 &exported_length ) );
4848 {
4849 uint8_t *p = exported;
4850 uint8_t *end = exported + exported_length;
4851 size_t len;
4852 /* RSAPublicKey ::= SEQUENCE {
4853 * modulus INTEGER, -- n
4854 * publicExponent INTEGER } -- e
4855 */
4856 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004857 MBEDTLS_ASN1_SEQUENCE |
4858 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004859 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
4860 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4861 MBEDTLS_ASN1_INTEGER ) );
4862 if( len >= 1 && p[0] == 0 )
4863 {
4864 ++p;
4865 --len;
4866 }
4867 if( e_arg->len == 0 )
4868 {
4869 TEST_EQUAL( len, 3 );
4870 TEST_EQUAL( p[0], 1 );
4871 TEST_EQUAL( p[1], 0 );
4872 TEST_EQUAL( p[2], 1 );
4873 }
4874 else
4875 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4876 }
4877
4878exit:
4879 psa_reset_key_attributes( &attributes );
4880 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004881 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004882 mbedtls_free( e_read_buffer );
4883 mbedtls_free( exported );
4884}
4885/* END_CASE */
4886
Darryl Greend49a4992018-06-18 17:27:26 +01004887/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004888void persistent_key_load_key_from_storage( data_t *data,
4889 int type_arg, int bits_arg,
4890 int usage_flags_arg, int alg_arg,
4891 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004892{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004893 psa_key_id_t key_id = 1;
4894 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004895 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004896 psa_key_handle_t base_key = 0;
4897 psa_key_type_t type = type_arg;
4898 size_t bits = bits_arg;
4899 psa_key_usage_t usage_flags = usage_flags_arg;
4900 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004901 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004902 unsigned char *first_export = NULL;
4903 unsigned char *second_export = NULL;
4904 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4905 size_t first_exported_length;
4906 size_t second_exported_length;
4907
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004908 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4909 {
4910 ASSERT_ALLOC( first_export, export_size );
4911 ASSERT_ALLOC( second_export, export_size );
4912 }
Darryl Greend49a4992018-06-18 17:27:26 +01004913
Gilles Peskine8817f612018-12-18 00:18:46 +01004914 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004915
Gilles Peskinec87af662019-05-15 16:12:22 +02004916 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004917 psa_set_key_usage_flags( &attributes, usage_flags );
4918 psa_set_key_algorithm( &attributes, alg );
4919 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004920 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004921
Darryl Green0c6575a2018-11-07 16:05:30 +00004922 switch( generation_method )
4923 {
4924 case IMPORT_KEY:
4925 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02004926 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
4927 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004928 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004929
Darryl Green0c6575a2018-11-07 16:05:30 +00004930 case GENERATE_KEY:
4931 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004932 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004933 break;
4934
4935 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004936 {
4937 /* Create base key */
4938 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
4939 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4940 psa_set_key_usage_flags( &base_attributes,
4941 PSA_KEY_USAGE_DERIVE );
4942 psa_set_key_algorithm( &base_attributes, derive_alg );
4943 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004944 PSA_ASSERT( psa_import_key( &base_attributes,
4945 data->x, data->len,
4946 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004947 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004948 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004949 PSA_ASSERT( psa_key_derivation_input_key(
4950 &operation,
4951 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004952 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004953 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004954 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004955 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
4956 &operation,
4957 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004958 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004959 PSA_ASSERT( psa_destroy_key( base_key ) );
4960 base_key = 0;
4961 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004962 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00004963 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004964 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01004965
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004966 /* Export the key if permitted by the key policy. */
4967 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4968 {
4969 PSA_ASSERT( psa_export_key( handle,
4970 first_export, export_size,
4971 &first_exported_length ) );
4972 if( generation_method == IMPORT_KEY )
4973 ASSERT_COMPARE( data->x, data->len,
4974 first_export, first_exported_length );
4975 }
Darryl Greend49a4992018-06-18 17:27:26 +01004976
4977 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02004978 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004979 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01004980 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004981
Darryl Greend49a4992018-06-18 17:27:26 +01004982 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02004983 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004984 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4985 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
4986 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
4987 PSA_KEY_LIFETIME_PERSISTENT );
4988 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4989 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4990 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
4991 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01004992
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004993 /* Export the key again if permitted by the key policy. */
4994 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00004995 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004996 PSA_ASSERT( psa_export_key( handle,
4997 second_export, export_size,
4998 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004999 ASSERT_COMPARE( first_export, first_exported_length,
5000 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005001 }
5002
5003 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005004 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005005 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005006
5007exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005008 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005009 mbedtls_free( first_export );
5010 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005011 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005012 psa_destroy_key( base_key );
5013 if( handle == 0 )
5014 {
5015 /* In case there was a test failure after creating the persistent key
5016 * but while it was not open, try to re-open the persistent key
5017 * to delete it. */
Gilles Peskine225010f2019-05-06 18:44:55 +02005018 psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005019 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005020 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005021 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005022}
5023/* END_CASE */