blob: 48f533764e49ffa9cb3271bdbaddf7b26d11a652 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
itayzafrir3e02b3b2018-06-12 17:06:52 +03002#include <stdint.h>
mohammad160327010052018-07-03 13:16:15 +03003
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02004#include "mbedtls/asn1.h"
Gilles Peskine0b352bc2018-06-28 00:16:11 +02005#include "mbedtls/asn1write.h"
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02006#include "mbedtls/oid.h"
7
Gilles Peskine1838e822019-06-20 12:40:56 +02008#include "psa_crypto_helpers.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +03009
Jaeden Amerof24c7f82018-06-27 17:20:43 +010010/** An invalid export length that will never be set by psa_export_key(). */
11static const size_t INVALID_EXPORT_LENGTH = ~0U;
12
Gilles Peskinef426e0f2019-02-25 17:42:03 +010013/* A hash algorithm that is known to be supported.
14 *
15 * This is used in some smoke tests.
16 */
17#if defined(MBEDTLS_MD2_C)
18#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
19#elif defined(MBEDTLS_MD4_C)
20#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
21#elif defined(MBEDTLS_MD5_C)
22#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
23/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
24 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
25 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
26 * implausible anyway. */
27#elif defined(MBEDTLS_SHA1_C)
28#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
29#elif defined(MBEDTLS_SHA256_C)
30#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
31#elif defined(MBEDTLS_SHA512_C)
32#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
33#elif defined(MBEDTLS_SHA3_C)
34#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
35#else
36#undef KNOWN_SUPPORTED_HASH_ALG
37#endif
38
39/* A block cipher that is known to be supported.
40 *
41 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
42 */
43#if defined(MBEDTLS_AES_C)
44#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
45#elif defined(MBEDTLS_ARIA_C)
46#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
47#elif defined(MBEDTLS_CAMELLIA_C)
48#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
49#undef KNOWN_SUPPORTED_BLOCK_CIPHER
50#endif
51
52/* A MAC mode that is known to be supported.
53 *
54 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
55 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
56 *
57 * This is used in some smoke tests.
58 */
59#if defined(KNOWN_SUPPORTED_HASH_ALG)
60#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
61#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
62#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
63#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
64#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
65#else
66#undef KNOWN_SUPPORTED_MAC_ALG
67#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
68#endif
69
70/* A cipher algorithm and key type that are known to be supported.
71 *
72 * This is used in some smoke tests.
73 */
74#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
75#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
76#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
77#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
78#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
79#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
80#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
81#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
82#else
83#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
84#endif
85#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
86#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
87#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
88#elif defined(MBEDTLS_RC4_C)
89#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
90#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
91#else
92#undef KNOWN_SUPPORTED_CIPHER_ALG
93#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
94#endif
95
Gilles Peskinea7aa4422018-08-14 15:17:54 +020096/** Test if a buffer contains a constant byte value.
97 *
98 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020099 *
100 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200101 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200102 * \param size Size of the buffer in bytes.
103 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200104 * \return 1 if the buffer is all-bits-zero.
105 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200106 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200107static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200108{
109 size_t i;
110 for( i = 0; i < size; i++ )
111 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200112 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200113 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200114 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200115 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200116}
Gilles Peskine818ca122018-06-20 18:16:48 +0200117
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200118/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
119static int asn1_write_10x( unsigned char **p,
120 unsigned char *start,
121 size_t bits,
122 unsigned char x )
123{
124 int ret;
125 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200126 if( bits == 0 )
127 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
128 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200129 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300130 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200131 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
132 *p -= len;
133 ( *p )[len-1] = x;
134 if( bits % 8 == 0 )
135 ( *p )[1] |= 1;
136 else
137 ( *p )[0] |= 1 << ( bits % 8 );
138 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
139 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
140 MBEDTLS_ASN1_INTEGER ) );
141 return( len );
142}
143
144static int construct_fake_rsa_key( unsigned char *buffer,
145 size_t buffer_size,
146 unsigned char **p,
147 size_t bits,
148 int keypair )
149{
150 size_t half_bits = ( bits + 1 ) / 2;
151 int ret;
152 int len = 0;
153 /* Construct something that looks like a DER encoding of
154 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
155 * RSAPrivateKey ::= SEQUENCE {
156 * version Version,
157 * modulus INTEGER, -- n
158 * publicExponent INTEGER, -- e
159 * privateExponent INTEGER, -- d
160 * prime1 INTEGER, -- p
161 * prime2 INTEGER, -- q
162 * exponent1 INTEGER, -- d mod (p-1)
163 * exponent2 INTEGER, -- d mod (q-1)
164 * coefficient INTEGER, -- (inverse of q) mod p
165 * otherPrimeInfos OtherPrimeInfos OPTIONAL
166 * }
167 * Or, for a public key, the same structure with only
168 * version, modulus and publicExponent.
169 */
170 *p = buffer + buffer_size;
171 if( keypair )
172 {
173 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
174 asn1_write_10x( p, buffer, half_bits, 1 ) );
175 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
176 asn1_write_10x( p, buffer, half_bits, 1 ) );
177 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
178 asn1_write_10x( p, buffer, half_bits, 1 ) );
179 MBEDTLS_ASN1_CHK_ADD( len, /* q */
180 asn1_write_10x( p, buffer, half_bits, 1 ) );
181 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
182 asn1_write_10x( p, buffer, half_bits, 3 ) );
183 MBEDTLS_ASN1_CHK_ADD( len, /* d */
184 asn1_write_10x( p, buffer, bits, 1 ) );
185 }
186 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
187 asn1_write_10x( p, buffer, 17, 1 ) );
188 MBEDTLS_ASN1_CHK_ADD( len, /* n */
189 asn1_write_10x( p, buffer, bits, 1 ) );
190 if( keypair )
191 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
192 mbedtls_asn1_write_int( p, buffer, 0 ) );
193 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
194 {
195 const unsigned char tag =
196 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
197 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
198 }
199 return( len );
200}
201
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100202int exercise_mac_setup( psa_key_type_t key_type,
203 const unsigned char *key_bytes,
204 size_t key_length,
205 psa_algorithm_t alg,
206 psa_mac_operation_t *operation,
207 psa_status_t *status )
208{
209 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200210 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100211
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200212 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
213 psa_set_key_algorithm( &attributes, alg );
214 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +0200215 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length,
216 &handle ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100217
218 *status = psa_mac_sign_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100219 /* Whether setup succeeded or failed, abort must succeed. */
220 PSA_ASSERT( psa_mac_abort( operation ) );
221 /* If setup failed, reproduce the failure, so that the caller can
222 * test the resulting state of the operation object. */
223 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100224 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100225 TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ),
226 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100227 }
228
229 psa_destroy_key( handle );
230 return( 1 );
231
232exit:
233 psa_destroy_key( handle );
234 return( 0 );
235}
236
237int exercise_cipher_setup( psa_key_type_t key_type,
238 const unsigned char *key_bytes,
239 size_t key_length,
240 psa_algorithm_t alg,
241 psa_cipher_operation_t *operation,
242 psa_status_t *status )
243{
244 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200245 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100246
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200247 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
248 psa_set_key_algorithm( &attributes, alg );
249 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +0200250 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length,
251 &handle ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100252
253 *status = psa_cipher_encrypt_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100254 /* Whether setup succeeded or failed, abort must succeed. */
255 PSA_ASSERT( psa_cipher_abort( operation ) );
256 /* If setup failed, reproduce the failure, so that the caller can
257 * test the resulting state of the operation object. */
258 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100259 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100260 TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ),
261 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100262 }
263
264 psa_destroy_key( handle );
265 return( 1 );
266
267exit:
268 psa_destroy_key( handle );
269 return( 0 );
270}
271
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100272static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200273 psa_key_usage_t usage,
274 psa_algorithm_t alg )
275{
Jaeden Amero769ce272019-01-04 11:48:03 +0000276 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200277 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200278 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200279 size_t mac_length = sizeof( mac );
280
281 if( usage & PSA_KEY_USAGE_SIGN )
282 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100283 PSA_ASSERT( psa_mac_sign_setup( &operation,
284 handle, alg ) );
285 PSA_ASSERT( psa_mac_update( &operation,
286 input, sizeof( input ) ) );
287 PSA_ASSERT( psa_mac_sign_finish( &operation,
288 mac, sizeof( mac ),
289 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200290 }
291
292 if( usage & PSA_KEY_USAGE_VERIFY )
293 {
294 psa_status_t verify_status =
295 ( usage & PSA_KEY_USAGE_SIGN ?
296 PSA_SUCCESS :
297 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100298 PSA_ASSERT( psa_mac_verify_setup( &operation,
299 handle, alg ) );
300 PSA_ASSERT( psa_mac_update( &operation,
301 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100302 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
303 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200304 }
305
306 return( 1 );
307
308exit:
309 psa_mac_abort( &operation );
310 return( 0 );
311}
312
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100313static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200314 psa_key_usage_t usage,
315 psa_algorithm_t alg )
316{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000317 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200318 unsigned char iv[16] = {0};
319 size_t iv_length = sizeof( iv );
320 const unsigned char plaintext[16] = "Hello, world...";
321 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
322 size_t ciphertext_length = sizeof( ciphertext );
323 unsigned char decrypted[sizeof( ciphertext )];
324 size_t part_length;
325
326 if( usage & PSA_KEY_USAGE_ENCRYPT )
327 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100328 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
329 handle, alg ) );
330 PSA_ASSERT( psa_cipher_generate_iv( &operation,
331 iv, sizeof( iv ),
332 &iv_length ) );
333 PSA_ASSERT( psa_cipher_update( &operation,
334 plaintext, sizeof( plaintext ),
335 ciphertext, sizeof( ciphertext ),
336 &ciphertext_length ) );
337 PSA_ASSERT( psa_cipher_finish( &operation,
338 ciphertext + ciphertext_length,
339 sizeof( ciphertext ) - ciphertext_length,
340 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200341 ciphertext_length += part_length;
342 }
343
344 if( usage & PSA_KEY_USAGE_DECRYPT )
345 {
346 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200347 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200348 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
349 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
351 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
352 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
353 * have this macro yet. */
354 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE(
355 psa_get_key_type( &attributes ) );
356 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200357 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100358 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
359 handle, alg ) );
360 PSA_ASSERT( psa_cipher_set_iv( &operation,
361 iv, iv_length ) );
362 PSA_ASSERT( psa_cipher_update( &operation,
363 ciphertext, ciphertext_length,
364 decrypted, sizeof( decrypted ),
365 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200366 status = psa_cipher_finish( &operation,
367 decrypted + part_length,
368 sizeof( decrypted ) - part_length,
369 &part_length );
370 /* For a stream cipher, all inputs are valid. For a block cipher,
371 * if the input is some aribtrary data rather than an actual
372 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200373 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200374 TEST_ASSERT( status == PSA_SUCCESS ||
375 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200376 else
377 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200378 }
379
380 return( 1 );
381
382exit:
383 psa_cipher_abort( &operation );
384 return( 0 );
385}
386
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100387static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200388 psa_key_usage_t usage,
389 psa_algorithm_t alg )
390{
391 unsigned char nonce[16] = {0};
392 size_t nonce_length = sizeof( nonce );
393 unsigned char plaintext[16] = "Hello, world...";
394 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
395 size_t ciphertext_length = sizeof( ciphertext );
396 size_t plaintext_length = sizeof( ciphertext );
397
398 if( usage & PSA_KEY_USAGE_ENCRYPT )
399 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100400 PSA_ASSERT( psa_aead_encrypt( handle, alg,
401 nonce, nonce_length,
402 NULL, 0,
403 plaintext, sizeof( plaintext ),
404 ciphertext, sizeof( ciphertext ),
405 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200406 }
407
408 if( usage & PSA_KEY_USAGE_DECRYPT )
409 {
410 psa_status_t verify_status =
411 ( usage & PSA_KEY_USAGE_ENCRYPT ?
412 PSA_SUCCESS :
413 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100414 TEST_EQUAL( psa_aead_decrypt( handle, alg,
415 nonce, nonce_length,
416 NULL, 0,
417 ciphertext, ciphertext_length,
418 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100419 &plaintext_length ),
420 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200421 }
422
423 return( 1 );
424
425exit:
426 return( 0 );
427}
428
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100429static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200430 psa_key_usage_t usage,
431 psa_algorithm_t alg )
432{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200433 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
434 size_t payload_length = 16;
Gilles Peskine69c12672018-06-28 00:07:19 +0200435 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200436 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100437 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
438
439 /* If the policy allows signing with any hash, just pick one. */
440 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
441 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100442#if defined(KNOWN_SUPPORTED_HASH_ALG)
443 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
444 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100445#else
446 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100447 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100448#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100449 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200450
451 if( usage & PSA_KEY_USAGE_SIGN )
452 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200453 /* Some algorithms require the payload to have the size of
454 * the hash encoded in the algorithm. Use this input size
455 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200456 if( hash_alg != 0 )
457 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine8817f612018-12-18 00:18:46 +0100458 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
459 payload, payload_length,
460 signature, sizeof( signature ),
461 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200462 }
463
464 if( usage & PSA_KEY_USAGE_VERIFY )
465 {
466 psa_status_t verify_status =
467 ( usage & PSA_KEY_USAGE_SIGN ?
468 PSA_SUCCESS :
469 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100470 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
471 payload, payload_length,
472 signature, signature_length ),
473 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200474 }
475
476 return( 1 );
477
478exit:
479 return( 0 );
480}
481
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100482static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200483 psa_key_usage_t usage,
484 psa_algorithm_t alg )
485{
486 unsigned char plaintext[256] = "Hello, world...";
487 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
488 size_t ciphertext_length = sizeof( ciphertext );
489 size_t plaintext_length = 16;
490
491 if( usage & PSA_KEY_USAGE_ENCRYPT )
492 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100493 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
494 plaintext, plaintext_length,
495 NULL, 0,
496 ciphertext, sizeof( ciphertext ),
497 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200498 }
499
500 if( usage & PSA_KEY_USAGE_DECRYPT )
501 {
502 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100503 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200504 ciphertext, ciphertext_length,
505 NULL, 0,
506 plaintext, sizeof( plaintext ),
507 &plaintext_length );
508 TEST_ASSERT( status == PSA_SUCCESS ||
509 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
510 ( status == PSA_ERROR_INVALID_ARGUMENT ||
511 status == PSA_ERROR_INVALID_PADDING ) ) );
512 }
513
514 return( 1 );
515
516exit:
517 return( 0 );
518}
Gilles Peskine02b75072018-07-01 22:31:34 +0200519
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100520static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200521 psa_key_usage_t usage,
522 psa_algorithm_t alg )
523{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200524 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +0200525 unsigned char label[16] = "This is a label.";
526 size_t label_length = sizeof( label );
527 unsigned char seed[16] = "abcdefghijklmnop";
528 size_t seed_length = sizeof( seed );
529 unsigned char output[1];
530
531 if( usage & PSA_KEY_USAGE_DERIVE )
532 {
Gilles Peskineb70a0fd2019-01-07 22:59:38 +0100533 if( PSA_ALG_IS_HKDF( alg ) )
534 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200535 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
536 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +0200537 PSA_KEY_DERIVATION_INPUT_SALT,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +0100538 label,
539 label_length ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200540 PSA_ASSERT( psa_key_derivation_input_key( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +0200541 PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +0100542 handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200543 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +0200544 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +0100545 seed,
546 seed_length ) );
547 }
Janos Follath71a4c912019-06-11 09:14:47 +0100548#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +0100549 else
550 {
551 // legacy
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200552 PSA_ASSERT( psa_key_derivation( &operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +0100553 handle, alg,
554 label, label_length,
555 seed, seed_length,
556 sizeof( output ) ) );
557 }
Janos Follath71a4c912019-06-11 09:14:47 +0100558#endif
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200559 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200560 output,
561 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200562 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200563 }
564
565 return( 1 );
566
567exit:
568 return( 0 );
569}
570
Gilles Peskinec7998b72018-11-07 18:45:02 +0100571/* We need two keys to exercise key agreement. Exercise the
572 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200573static psa_status_t key_agreement_with_self(
574 psa_key_derivation_operation_t *operation,
575 psa_key_handle_t handle )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100576{
577 psa_key_type_t private_key_type;
578 psa_key_type_t public_key_type;
579 size_t key_bits;
580 uint8_t *public_key = NULL;
581 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200582 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200583 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
584 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200585 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200586 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100587
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200588 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
589 private_key_type = psa_get_key_type( &attributes );
590 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200591 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100592 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
593 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100594 PSA_ASSERT( psa_export_public_key( handle,
595 public_key, public_key_length,
596 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100597
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200598 status = psa_key_derivation_key_agreement(
599 operation, PSA_KEY_DERIVATION_INPUT_SECRET, handle,
600 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100601exit:
602 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200603 psa_reset_key_attributes( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100604 return( status );
605}
606
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200607/* We need two keys to exercise key agreement. Exercise the
608 * private key against its own public key. */
609static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
610 psa_key_handle_t handle )
611{
612 psa_key_type_t private_key_type;
613 psa_key_type_t public_key_type;
614 size_t key_bits;
615 uint8_t *public_key = NULL;
616 size_t public_key_length;
617 uint8_t output[1024];
618 size_t output_length;
619 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200620 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
621 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200622 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200623 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200624
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200625 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
626 private_key_type = psa_get_key_type( &attributes );
627 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200628 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200629 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
630 ASSERT_ALLOC( public_key, public_key_length );
631 PSA_ASSERT( psa_export_public_key( handle,
632 public_key, public_key_length,
633 &public_key_length ) );
634
Gilles Peskinebe697d82019-05-16 18:00:41 +0200635 status = psa_raw_key_agreement( alg, handle,
636 public_key, public_key_length,
637 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200638exit:
639 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200640 psa_reset_key_attributes( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200641 return( status );
642}
643
644static int exercise_raw_key_agreement_key( psa_key_handle_t handle,
645 psa_key_usage_t usage,
646 psa_algorithm_t alg )
647{
648 int ok = 0;
649
650 if( usage & PSA_KEY_USAGE_DERIVE )
651 {
652 /* We need two keys to exercise key agreement. Exercise the
653 * private key against its own public key. */
654 PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) );
655 }
656 ok = 1;
657
658exit:
659 return( ok );
660}
661
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100662static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200663 psa_key_usage_t usage,
664 psa_algorithm_t alg )
665{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200666 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200667 unsigned char output[1];
668 int ok = 0;
669
670 if( usage & PSA_KEY_USAGE_DERIVE )
671 {
672 /* We need two keys to exercise key agreement. Exercise the
673 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200674 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
675 PSA_ASSERT( key_agreement_with_self( &operation, handle ) );
676 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200677 output,
678 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200679 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200680 }
681 ok = 1;
682
683exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200684 return( ok );
685}
686
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200687static int asn1_skip_integer( unsigned char **p, const unsigned char *end,
688 size_t min_bits, size_t max_bits,
689 int must_be_odd )
690{
691 size_t len;
692 size_t actual_bits;
693 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100694 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100695 MBEDTLS_ASN1_INTEGER ),
696 0 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200697 /* Tolerate a slight departure from DER encoding:
698 * - 0 may be represented by an empty string or a 1-byte string.
699 * - The sign bit may be used as a value bit. */
700 if( ( len == 1 && ( *p )[0] == 0 ) ||
701 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
702 {
703 ++( *p );
704 --len;
705 }
706 if( min_bits == 0 && len == 0 )
707 return( 1 );
708 msb = ( *p )[0];
709 TEST_ASSERT( msb != 0 );
710 actual_bits = 8 * ( len - 1 );
711 while( msb != 0 )
712 {
713 msb >>= 1;
714 ++actual_bits;
715 }
716 TEST_ASSERT( actual_bits >= min_bits );
717 TEST_ASSERT( actual_bits <= max_bits );
718 if( must_be_odd )
719 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
720 *p += len;
721 return( 1 );
722exit:
723 return( 0 );
724}
725
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200726static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
727 uint8_t *exported, size_t exported_length )
728{
729 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100730 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200731 else
732 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200733
734#if defined(MBEDTLS_DES_C)
735 if( type == PSA_KEY_TYPE_DES )
736 {
737 /* Check the parity bits. */
738 unsigned i;
739 for( i = 0; i < bits / 8; i++ )
740 {
741 unsigned bit_count = 0;
742 unsigned m;
743 for( m = 1; m <= 0x100; m <<= 1 )
744 {
745 if( exported[i] & m )
746 ++bit_count;
747 }
748 TEST_ASSERT( bit_count % 2 != 0 );
749 }
750 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200751 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200752#endif
753
754#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200755 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200756 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200757 uint8_t *p = exported;
758 uint8_t *end = exported + exported_length;
759 size_t len;
760 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200761 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200762 * modulus INTEGER, -- n
763 * publicExponent INTEGER, -- e
764 * privateExponent INTEGER, -- d
765 * prime1 INTEGER, -- p
766 * prime2 INTEGER, -- q
767 * exponent1 INTEGER, -- d mod (p-1)
768 * exponent2 INTEGER, -- d mod (q-1)
769 * coefficient INTEGER, -- (inverse of q) mod p
770 * }
771 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100772 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
773 MBEDTLS_ASN1_SEQUENCE |
774 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
775 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200776 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
777 goto exit;
778 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
779 goto exit;
780 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
781 goto exit;
782 /* Require d to be at least half the size of n. */
783 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
784 goto exit;
785 /* Require p and q to be at most half the size of n, rounded up. */
786 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
787 goto exit;
788 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
789 goto exit;
790 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
791 goto exit;
792 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
793 goto exit;
794 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
795 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100796 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100797 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200798 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200799#endif /* MBEDTLS_RSA_C */
800
801#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200802 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200803 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100804 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100805 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100806 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200807 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200808#endif /* MBEDTLS_ECP_C */
809
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200810 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
811 {
812 uint8_t *p = exported;
813 uint8_t *end = exported + exported_length;
814 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200815#if defined(MBEDTLS_RSA_C)
816 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
817 {
818 /* RSAPublicKey ::= SEQUENCE {
819 * modulus INTEGER, -- n
820 * publicExponent INTEGER } -- e
821 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100822 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
823 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100824 MBEDTLS_ASN1_CONSTRUCTED ),
825 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100826 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200827 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
828 goto exit;
829 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
830 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100831 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200832 }
833 else
834#endif /* MBEDTLS_RSA_C */
835#if defined(MBEDTLS_ECP_C)
836 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
837 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000838 /* The representation of an ECC public key is:
839 * - The byte 0x04;
840 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
841 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
842 * - where m is the bit size associated with the curve.
Jaeden Amero6b196002019-01-10 10:23:21 +0000843 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100844 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
845 TEST_EQUAL( p[0], 4 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200846 }
847 else
848#endif /* MBEDTLS_ECP_C */
849 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100850 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200851 mbedtls_snprintf( message, sizeof( message ),
852 "No sanity check for public key type=0x%08lx",
853 (unsigned long) type );
854 test_fail( message, __LINE__, __FILE__ );
855 return( 0 );
856 }
857 }
858 else
859
860 {
861 /* No sanity checks for other types */
862 }
863
864 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200865
866exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200867 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +0200868}
869
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100870static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +0200871 psa_key_usage_t usage )
872{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200873 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200874 uint8_t *exported = NULL;
875 size_t exported_size = 0;
876 size_t exported_length = 0;
877 int ok = 0;
878
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200879 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +0200880
881 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200882 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200883 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100884 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
885 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200886 ok = 1;
887 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +0200888 }
889
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200890 exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ),
891 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200892 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200893
Gilles Peskine8817f612018-12-18 00:18:46 +0100894 PSA_ASSERT( psa_export_key( handle,
895 exported, exported_size,
896 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200897 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
898 psa_get_key_bits( &attributes ),
899 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +0200900
901exit:
902 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200903 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +0200904 return( ok );
905}
906
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100907static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +0200908{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200909 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +0200910 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +0200911 uint8_t *exported = NULL;
912 size_t exported_size = 0;
913 size_t exported_length = 0;
914 int ok = 0;
915
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200916 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
917 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200918 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100919 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100920 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +0200921 return( 1 );
922 }
923
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200924 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200925 psa_get_key_type( &attributes ) );
926 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type,
927 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200928 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +0200929
Gilles Peskine8817f612018-12-18 00:18:46 +0100930 PSA_ASSERT( psa_export_public_key( handle,
931 exported, exported_size,
932 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200933 ok = exported_key_sanity_check( public_type,
934 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +0200935 exported, exported_length );
936
937exit:
938 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200939 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +0200940 return( ok );
941}
942
Gilles Peskinec9516fb2019-02-05 20:32:06 +0100943/** Do smoke tests on a key.
944 *
945 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
946 * sign/verify, or derivation) that is permitted according to \p usage.
947 * \p usage and \p alg should correspond to the expected policy on the
948 * key.
949 *
950 * Export the key if permitted by \p usage, and check that the output
951 * looks sensible. If \p usage forbids export, check that
952 * \p psa_export_key correctly rejects the attempt. If the key is
953 * asymmetric, also check \p psa_export_public_key.
954 *
955 * If the key fails the tests, this function calls the test framework's
956 * `test_fail` function and returns false. Otherwise this function returns
957 * true. Therefore it should be used as follows:
958 * ```
959 * if( ! exercise_key( ... ) ) goto exit;
960 * ```
961 *
962 * \param handle The key to exercise. It should be capable of performing
963 * \p alg.
964 * \param usage The usage flags to assume.
965 * \param alg The algorithm to exercise.
966 *
967 * \retval 0 The key failed the smoke tests.
968 * \retval 1 The key passed the smoke tests.
969 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100970static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +0200971 psa_key_usage_t usage,
972 psa_algorithm_t alg )
973{
974 int ok;
975 if( alg == 0 )
976 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
977 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100978 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200979 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100980 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200981 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100982 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200983 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100984 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200985 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100986 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200987 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100988 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200989 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
990 ok = exercise_raw_key_agreement_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200991 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100992 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +0200993 else
994 {
995 char message[40];
996 mbedtls_snprintf( message, sizeof( message ),
997 "No code to exercise alg=0x%08lx",
998 (unsigned long) alg );
999 test_fail( message, __LINE__, __FILE__ );
1000 ok = 0;
1001 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001002
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001003 ok = ok && exercise_export_key( handle, usage );
1004 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +02001005
Gilles Peskine02b75072018-07-01 22:31:34 +02001006 return( ok );
1007}
1008
Gilles Peskine10df3412018-10-25 22:35:43 +02001009static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1010 psa_algorithm_t alg )
1011{
1012 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1013 {
1014 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1015 PSA_KEY_USAGE_VERIFY :
1016 PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
1017 }
1018 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1019 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1020 {
1021 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1022 PSA_KEY_USAGE_ENCRYPT :
1023 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1024 }
1025 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1026 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1027 {
1028 return( PSA_KEY_USAGE_DERIVE );
1029 }
1030 else
1031 {
1032 return( 0 );
1033 }
1034
1035}
Darryl Green0c6575a2018-11-07 16:05:30 +00001036
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001037static int test_operations_on_invalid_handle( psa_key_handle_t handle )
1038{
1039 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1040 uint8_t buffer[1];
1041 size_t length;
1042 int ok = 0;
1043
Gilles Peskinec87af662019-05-15 16:12:22 +02001044 psa_set_key_id( &attributes, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001045 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1046 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1047 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
1048 TEST_EQUAL( psa_get_key_attributes( handle, &attributes ),
1049 PSA_ERROR_INVALID_HANDLE );
1050 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001051 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001052 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1053 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1054 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1055 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1056
1057 TEST_EQUAL( psa_export_key( handle,
1058 buffer, sizeof( buffer ), &length ),
1059 PSA_ERROR_INVALID_HANDLE );
1060 TEST_EQUAL( psa_export_public_key( handle,
1061 buffer, sizeof( buffer ), &length ),
1062 PSA_ERROR_INVALID_HANDLE );
1063
1064 TEST_EQUAL( psa_close_key( handle ), PSA_ERROR_INVALID_HANDLE );
1065 TEST_EQUAL( psa_destroy_key( handle ), PSA_ERROR_INVALID_HANDLE );
1066
1067 ok = 1;
1068
1069exit:
1070 psa_reset_key_attributes( &attributes );
1071 return( ok );
1072}
1073
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001074/* An overapproximation of the amount of storage needed for a key of the
1075 * given type and with the given content. The API doesn't make it easy
1076 * to find a good value for the size. The current implementation doesn't
1077 * care about the value anyway. */
1078#define KEY_BITS_FROM_DATA( type, data ) \
1079 ( data )->len
1080
Darryl Green0c6575a2018-11-07 16:05:30 +00001081typedef enum {
1082 IMPORT_KEY = 0,
1083 GENERATE_KEY = 1,
1084 DERIVE_KEY = 2
1085} generate_method;
1086
Gilles Peskinee59236f2018-01-27 23:32:46 +01001087/* END_HEADER */
1088
1089/* BEGIN_DEPENDENCIES
1090 * depends_on:MBEDTLS_PSA_CRYPTO_C
1091 * END_DEPENDENCIES
1092 */
1093
1094/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001095void static_checks( )
1096{
1097 size_t max_truncated_mac_size =
1098 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1099
1100 /* Check that the length for a truncated MAC always fits in the algorithm
1101 * encoding. The shifted mask is the maximum truncated value. The
1102 * untruncated algorithm may be one byte larger. */
1103 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
1104}
1105/* END_CASE */
1106
1107/* BEGIN_CASE */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001108void attributes_set_get( int id_arg, int lifetime_arg,
1109 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001110 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001111{
Gilles Peskine4747d192019-04-17 15:05:45 +02001112 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001113 psa_key_id_t id = id_arg;
1114 psa_key_lifetime_t lifetime = lifetime_arg;
1115 psa_key_usage_t usage_flags = usage_flags_arg;
1116 psa_algorithm_t alg = alg_arg;
1117 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001118 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001119
1120 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1121 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1122 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1123 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1124 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001125 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001126
Gilles Peskinec87af662019-05-15 16:12:22 +02001127 psa_set_key_id( &attributes, id );
1128 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001129 psa_set_key_usage_flags( &attributes, usage_flags );
1130 psa_set_key_algorithm( &attributes, alg );
1131 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001132 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001133
1134 TEST_EQUAL( psa_get_key_id( &attributes ), id );
1135 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1136 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1137 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1138 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001139 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001140
1141 psa_reset_key_attributes( &attributes );
1142
1143 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1144 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1145 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1146 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1147 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001148 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001149}
1150/* END_CASE */
1151
1152/* BEGIN_CASE */
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001153void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg,
1154 int expected_id_arg, int expected_lifetime_arg )
1155{
1156 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1157 psa_key_id_t id1 = id1_arg;
1158 psa_key_lifetime_t lifetime = lifetime_arg;
1159 psa_key_id_t id2 = id2_arg;
1160 psa_key_id_t expected_id = expected_id_arg;
1161 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1162
1163 if( id1_arg != -1 )
1164 psa_set_key_id( &attributes, id1 );
1165 if( lifetime_arg != -1 )
1166 psa_set_key_lifetime( &attributes, lifetime );
1167 if( id2_arg != -1 )
1168 psa_set_key_id( &attributes, id2 );
1169
1170 TEST_EQUAL( psa_get_key_id( &attributes ), expected_id );
1171 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1172}
1173/* END_CASE */
1174
1175/* BEGIN_CASE */
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001176void import( data_t *data, int type_arg,
1177 int attr_bits_arg,
1178 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001179{
1180 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1181 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001182 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001183 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001184 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001185 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001186 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001187
Gilles Peskine8817f612018-12-18 00:18:46 +01001188 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001189
Gilles Peskine4747d192019-04-17 15:05:45 +02001190 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001191 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001192 status = psa_import_key( &attributes, data->x, data->len, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001193 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001194 if( status != PSA_SUCCESS )
1195 goto exit;
1196
1197 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1198 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001199 if( attr_bits != 0 )
1200 TEST_EQUAL( attr_bits, got_attributes.bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001201
1202 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001203 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001204
1205exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001206 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001207 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001208 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001209}
1210/* END_CASE */
1211
1212/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001213void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1214{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001215 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001216 size_t bits = bits_arg;
1217 psa_status_t expected_status = expected_status_arg;
1218 psa_status_t status;
1219 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001220 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001221 size_t buffer_size = /* Slight overapproximations */
1222 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001223 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001224 unsigned char *p;
1225 int ret;
1226 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001227 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001228
Gilles Peskine8817f612018-12-18 00:18:46 +01001229 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001230 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001231
1232 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1233 bits, keypair ) ) >= 0 );
1234 length = ret;
1235
1236 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001237 psa_set_key_type( &attributes, type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001238 status = psa_import_key( &attributes, p, length, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001239 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001240
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001241 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001242 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001243
1244exit:
1245 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001246 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001247}
1248/* END_CASE */
1249
1250/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001251void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001252 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001253 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001254 int expected_bits,
1255 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001256 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001257 int canonical_input )
1258{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001259 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001260 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001261 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001262 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001263 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001264 unsigned char *exported = NULL;
1265 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001266 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001267 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001268 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001269 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001270 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001271
Moran Pekercb088e72018-07-17 17:36:59 +03001272 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001273 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001274 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001275 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001276 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001277
Gilles Peskine4747d192019-04-17 15:05:45 +02001278 psa_set_key_usage_flags( &attributes, usage_arg );
1279 psa_set_key_algorithm( &attributes, alg );
1280 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001281
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001282 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001283 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001284
1285 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001286 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1287 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1288 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001289
1290 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001291 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001292 exported, export_size,
1293 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001294 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001295
1296 /* The exported length must be set by psa_export_key() to a value between 0
1297 * and export_size. On errors, the exported length must be 0. */
1298 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1299 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1300 TEST_ASSERT( exported_length <= export_size );
1301
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001302 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001303 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001304 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001305 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001306 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001307 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001308 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001309
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001310 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001311 goto exit;
1312
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001313 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001314 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001315 else
1316 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001317 psa_key_handle_t handle2;
Gilles Peskine049c7532019-05-15 20:22:09 +02001318 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
1319 &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001320 PSA_ASSERT( psa_export_key( handle2,
1321 reexported,
1322 export_size,
1323 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001324 ASSERT_COMPARE( exported, exported_length,
1325 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001326 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001327 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001328 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001329
1330destroy:
1331 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001332 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001333 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001334
1335exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001336 mbedtls_free( exported );
1337 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001338 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001339 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001340}
1341/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001342
Moran Pekerf709f4a2018-06-06 17:26:04 +03001343/* BEGIN_CASE */
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001344void invalid_handle( int handle )
Moran Peker28a38e62018-11-07 16:18:24 +02001345{
Gilles Peskine8817f612018-12-18 00:18:46 +01001346 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001347 test_operations_on_invalid_handle( handle );
Moran Peker28a38e62018-11-07 16:18:24 +02001348
1349exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001350 PSA_DONE( );
Moran Peker28a38e62018-11-07 16:18:24 +02001351}
1352/* END_CASE */
1353
1354/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001355void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001356 int type_arg,
1357 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001358 int export_size_delta,
1359 int expected_export_status_arg,
1360 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001361{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001362 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001363 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001364 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001365 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001366 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001367 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001368 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001369 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001370 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001371
Gilles Peskine8817f612018-12-18 00:18:46 +01001372 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001373
Gilles Peskine4747d192019-04-17 15:05:45 +02001374 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1375 psa_set_key_algorithm( &attributes, alg );
1376 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001377
1378 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001379 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001380
Gilles Peskine49c25912018-10-29 15:15:31 +01001381 /* Export the public key */
1382 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001383 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001384 exported, export_size,
1385 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001386 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001387 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001388 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001389 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001390 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001391 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1392 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001393 TEST_ASSERT( expected_public_key->len <=
1394 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001395 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1396 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001397 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001398
1399exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001400 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001401 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001402 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001403 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001404}
1405/* END_CASE */
1406
Gilles Peskine20035e32018-02-03 22:44:14 +01001407/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001408void import_and_exercise_key( data_t *data,
1409 int type_arg,
1410 int bits_arg,
1411 int alg_arg )
1412{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001413 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001414 psa_key_type_t type = type_arg;
1415 size_t bits = bits_arg;
1416 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001417 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001418 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001419 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001420
Gilles Peskine8817f612018-12-18 00:18:46 +01001421 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001422
Gilles Peskine4747d192019-04-17 15:05:45 +02001423 psa_set_key_usage_flags( &attributes, usage );
1424 psa_set_key_algorithm( &attributes, alg );
1425 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001426
1427 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001428 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001429
1430 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001431 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1432 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1433 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001434
1435 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001436 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001437 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001438
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001439 PSA_ASSERT( psa_destroy_key( handle ) );
1440 test_operations_on_invalid_handle( handle );
1441
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001442exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001443 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001444 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001445 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001446}
1447/* END_CASE */
1448
1449/* BEGIN_CASE */
Gilles Peskined5b33222018-06-18 22:20:03 +02001450void key_policy( int usage_arg, int alg_arg )
1451{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001452 psa_key_handle_t handle = 0;
Gilles Peskined5b33222018-06-18 22:20:03 +02001453 psa_algorithm_t alg = alg_arg;
1454 psa_key_usage_t usage = usage_arg;
1455 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
1456 unsigned char key[32] = {0};
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001457 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001458
1459 memset( key, 0x2a, sizeof( key ) );
1460
Gilles Peskine8817f612018-12-18 00:18:46 +01001461 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001462
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001463 psa_set_key_usage_flags( &attributes, usage );
1464 psa_set_key_algorithm( &attributes, alg );
1465 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001466
Gilles Peskine73676cb2019-05-15 20:15:10 +02001467 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001468
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001469 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1470 TEST_EQUAL( psa_get_key_type( &attributes ), key_type );
1471 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage );
1472 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001473
1474exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001475 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001476 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001477 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001478}
1479/* END_CASE */
1480
1481/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001482void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001483{
1484 /* Test each valid way of initializing the object, except for `= {0}`, as
1485 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1486 * though it's OK by the C standard. We could test for this, but we'd need
1487 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001488 psa_key_attributes_t func = psa_key_attributes_init( );
1489 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1490 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001491
1492 memset( &zero, 0, sizeof( zero ) );
1493
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001494 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1495 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1496 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001497
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001498 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1499 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1500 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1501
1502 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1503 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1504 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1505
1506 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1507 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1508 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1509
1510 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1511 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1512 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001513}
1514/* END_CASE */
1515
1516/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001517void mac_key_policy( int policy_usage,
1518 int policy_alg,
1519 int key_type,
1520 data_t *key_data,
1521 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001522{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001523 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001524 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001525 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001526 psa_status_t status;
1527 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001528
Gilles Peskine8817f612018-12-18 00:18:46 +01001529 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001530
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001531 psa_set_key_usage_flags( &attributes, policy_usage );
1532 psa_set_key_algorithm( &attributes, policy_alg );
1533 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001534
Gilles Peskine049c7532019-05-15 20:22:09 +02001535 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1536 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001537
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001538 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001539 if( policy_alg == exercise_alg &&
1540 ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001541 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001542 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001543 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001544 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001545
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001546 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001547 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001548 if( policy_alg == exercise_alg &&
1549 ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001550 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001551 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001552 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001553
1554exit:
1555 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001556 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001557 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001558}
1559/* END_CASE */
1560
1561/* BEGIN_CASE */
1562void cipher_key_policy( int policy_usage,
1563 int policy_alg,
1564 int key_type,
1565 data_t *key_data,
1566 int exercise_alg )
1567{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001568 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001569 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001570 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001571 psa_status_t status;
1572
Gilles Peskine8817f612018-12-18 00:18:46 +01001573 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001574
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001575 psa_set_key_usage_flags( &attributes, policy_usage );
1576 psa_set_key_algorithm( &attributes, policy_alg );
1577 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001578
Gilles Peskine049c7532019-05-15 20:22:09 +02001579 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1580 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001581
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001582 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001583 if( policy_alg == exercise_alg &&
1584 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001585 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001586 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001587 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001588 psa_cipher_abort( &operation );
1589
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001590 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001591 if( policy_alg == exercise_alg &&
1592 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001593 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001594 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001595 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001596
1597exit:
1598 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001599 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001600 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001601}
1602/* END_CASE */
1603
1604/* BEGIN_CASE */
1605void aead_key_policy( int policy_usage,
1606 int policy_alg,
1607 int key_type,
1608 data_t *key_data,
1609 int nonce_length_arg,
1610 int tag_length_arg,
1611 int exercise_alg )
1612{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001613 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001614 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001615 psa_status_t status;
1616 unsigned char nonce[16] = {0};
1617 size_t nonce_length = nonce_length_arg;
1618 unsigned char tag[16];
1619 size_t tag_length = tag_length_arg;
1620 size_t output_length;
1621
1622 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1623 TEST_ASSERT( tag_length <= sizeof( tag ) );
1624
Gilles Peskine8817f612018-12-18 00:18:46 +01001625 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001626
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001627 psa_set_key_usage_flags( &attributes, policy_usage );
1628 psa_set_key_algorithm( &attributes, policy_alg );
1629 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001630
Gilles Peskine049c7532019-05-15 20:22:09 +02001631 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1632 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001633
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001634 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001635 nonce, nonce_length,
1636 NULL, 0,
1637 NULL, 0,
1638 tag, tag_length,
1639 &output_length );
1640 if( policy_alg == exercise_alg &&
1641 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001642 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001643 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001644 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001645
1646 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001647 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001648 nonce, nonce_length,
1649 NULL, 0,
1650 tag, tag_length,
1651 NULL, 0,
1652 &output_length );
1653 if( policy_alg == exercise_alg &&
1654 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001655 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001656 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001657 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001658
1659exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001660 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001661 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001662}
1663/* END_CASE */
1664
1665/* BEGIN_CASE */
1666void asymmetric_encryption_key_policy( int policy_usage,
1667 int policy_alg,
1668 int key_type,
1669 data_t *key_data,
1670 int exercise_alg )
1671{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001672 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001673 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001674 psa_status_t status;
1675 size_t key_bits;
1676 size_t buffer_length;
1677 unsigned char *buffer = NULL;
1678 size_t output_length;
1679
Gilles Peskine8817f612018-12-18 00:18:46 +01001680 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001681
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001682 psa_set_key_usage_flags( &attributes, policy_usage );
1683 psa_set_key_algorithm( &attributes, policy_alg );
1684 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001685
Gilles Peskine049c7532019-05-15 20:22:09 +02001686 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1687 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001688
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001689 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1690 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001691 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
1692 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001693 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001694
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001695 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001696 NULL, 0,
1697 NULL, 0,
1698 buffer, buffer_length,
1699 &output_length );
1700 if( policy_alg == exercise_alg &&
1701 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001702 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001703 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001704 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001705
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02001706 if( buffer_length != 0 )
1707 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001708 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001709 buffer, buffer_length,
1710 NULL, 0,
1711 buffer, buffer_length,
1712 &output_length );
1713 if( policy_alg == exercise_alg &&
1714 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001715 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001716 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001717 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001718
1719exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001720 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001721 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001722 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001723 mbedtls_free( buffer );
1724}
1725/* END_CASE */
1726
1727/* BEGIN_CASE */
1728void asymmetric_signature_key_policy( int policy_usage,
1729 int policy_alg,
1730 int key_type,
1731 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001732 int exercise_alg,
1733 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001734{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001735 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001736 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001737 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001738 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1739 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1740 * compatible with the policy and `payload_length_arg` is supposed to be
1741 * a valid input length to sign. If `payload_length_arg <= 0`,
1742 * `exercise_alg` is supposed to be forbidden by the policy. */
1743 int compatible_alg = payload_length_arg > 0;
1744 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001745 unsigned char signature[PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE] = {0};
1746 size_t signature_length;
1747
Gilles Peskine8817f612018-12-18 00:18:46 +01001748 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001749
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001750 psa_set_key_usage_flags( &attributes, policy_usage );
1751 psa_set_key_algorithm( &attributes, policy_alg );
1752 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001753
Gilles Peskine049c7532019-05-15 20:22:09 +02001754 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1755 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001756
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001757 status = psa_asymmetric_sign( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001758 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001759 signature, sizeof( signature ),
1760 &signature_length );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001761 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001762 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001763 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001764 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001765
1766 memset( signature, 0, sizeof( signature ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001767 status = psa_asymmetric_verify( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001768 payload, payload_length,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001769 signature, sizeof( signature ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001770 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001771 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001772 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001773 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001774
1775exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001776 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001777 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001778}
1779/* END_CASE */
1780
Janos Follathba3fab92019-06-11 14:50:16 +01001781/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001782void derive_key_policy( int policy_usage,
1783 int policy_alg,
1784 int key_type,
1785 data_t *key_data,
1786 int exercise_alg )
1787{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001788 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001789 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001790 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001791 psa_status_t status;
1792
Gilles Peskine8817f612018-12-18 00:18:46 +01001793 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001794
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001795 psa_set_key_usage_flags( &attributes, policy_usage );
1796 psa_set_key_algorithm( &attributes, policy_alg );
1797 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001798
Gilles Peskine049c7532019-05-15 20:22:09 +02001799 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1800 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001801
Janos Follathba3fab92019-06-11 14:50:16 +01001802 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1803
1804 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1805 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001806 {
Janos Follathba3fab92019-06-11 14:50:16 +01001807 PSA_ASSERT( psa_key_derivation_input_bytes(
1808 &operation,
1809 PSA_KEY_DERIVATION_INPUT_SEED,
1810 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001811 }
Janos Follathba3fab92019-06-11 14:50:16 +01001812
1813 status = psa_key_derivation_input_key( &operation,
1814 PSA_KEY_DERIVATION_INPUT_SECRET,
1815 handle );
1816
Gilles Peskineea0fb492018-07-12 17:17:20 +02001817 if( policy_alg == exercise_alg &&
1818 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001819 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001820 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001821 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001822
1823exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001824 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001825 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001826 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001827}
1828/* END_CASE */
1829
1830/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001831void agreement_key_policy( int policy_usage,
1832 int policy_alg,
1833 int key_type_arg,
1834 data_t *key_data,
1835 int exercise_alg )
1836{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001837 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001838 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001839 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001840 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001841 psa_status_t status;
1842
Gilles Peskine8817f612018-12-18 00:18:46 +01001843 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001844
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001845 psa_set_key_usage_flags( &attributes, policy_usage );
1846 psa_set_key_algorithm( &attributes, policy_alg );
1847 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001848
Gilles Peskine049c7532019-05-15 20:22:09 +02001849 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1850 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001851
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001852 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1853 status = key_agreement_with_self( &operation, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001854
Gilles Peskine01d718c2018-09-18 12:01:02 +02001855 if( policy_alg == exercise_alg &&
1856 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001857 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001858 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001859 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001860
1861exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001862 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001863 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001864 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001865}
1866/* END_CASE */
1867
1868/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001869void key_policy_alg2( int key_type_arg, data_t *key_data,
1870 int usage_arg, int alg_arg, int alg2_arg )
1871{
1872 psa_key_handle_t handle = 0;
1873 psa_key_type_t key_type = key_type_arg;
1874 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1875 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1876 psa_key_usage_t usage = usage_arg;
1877 psa_algorithm_t alg = alg_arg;
1878 psa_algorithm_t alg2 = alg2_arg;
1879
1880 PSA_ASSERT( psa_crypto_init( ) );
1881
1882 psa_set_key_usage_flags( &attributes, usage );
1883 psa_set_key_algorithm( &attributes, alg );
1884 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1885 psa_set_key_type( &attributes, key_type );
1886 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1887 &handle ) );
1888
1889 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1890 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1891 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1892 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1893
1894 if( ! exercise_key( handle, usage, alg ) )
1895 goto exit;
1896 if( ! exercise_key( handle, usage, alg2 ) )
1897 goto exit;
1898
1899exit:
1900 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001901 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001902}
1903/* END_CASE */
1904
1905/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001906void raw_agreement_key_policy( int policy_usage,
1907 int policy_alg,
1908 int key_type_arg,
1909 data_t *key_data,
1910 int exercise_alg )
1911{
1912 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001913 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001914 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001915 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001916 psa_status_t status;
1917
1918 PSA_ASSERT( psa_crypto_init( ) );
1919
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001920 psa_set_key_usage_flags( &attributes, policy_usage );
1921 psa_set_key_algorithm( &attributes, policy_alg );
1922 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001923
Gilles Peskine049c7532019-05-15 20:22:09 +02001924 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1925 &handle ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001926
1927 status = raw_key_agreement_with_self( exercise_alg, handle );
1928
1929 if( policy_alg == exercise_alg &&
1930 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
1931 PSA_ASSERT( status );
1932 else
1933 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
1934
1935exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001936 psa_key_derivation_abort( &operation );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001937 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001938 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001939}
1940/* END_CASE */
1941
1942/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001943void copy_success( int source_usage_arg,
1944 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001945 int type_arg, data_t *material,
1946 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001947 int target_usage_arg,
1948 int target_alg_arg, int target_alg2_arg,
1949 int expected_usage_arg,
1950 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001951{
Gilles Peskineca25db92019-04-19 11:43:08 +02001952 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1953 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001954 psa_key_usage_t expected_usage = expected_usage_arg;
1955 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001956 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02001957 psa_key_handle_t source_handle = 0;
1958 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001959 uint8_t *export_buffer = NULL;
1960
Gilles Peskine57ab7212019-01-28 13:03:09 +01001961 PSA_ASSERT( psa_crypto_init( ) );
1962
Gilles Peskineca25db92019-04-19 11:43:08 +02001963 /* Prepare the source key. */
1964 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1965 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001966 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001967 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001968 PSA_ASSERT( psa_import_key( &source_attributes,
1969 material->x, material->len,
1970 &source_handle ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001971 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001972
Gilles Peskineca25db92019-04-19 11:43:08 +02001973 /* Prepare the target attributes. */
1974 if( copy_attributes )
1975 target_attributes = source_attributes;
1976 if( target_usage_arg != -1 )
1977 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1978 if( target_alg_arg != -1 )
1979 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001980 if( target_alg2_arg != -1 )
1981 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001982
1983 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02001984 PSA_ASSERT( psa_copy_key( source_handle,
1985 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001986
1987 /* Destroy the source to ensure that this doesn't affect the target. */
1988 PSA_ASSERT( psa_destroy_key( source_handle ) );
1989
1990 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02001991 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
1992 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1993 psa_get_key_type( &target_attributes ) );
1994 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1995 psa_get_key_bits( &target_attributes ) );
1996 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1997 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001998 TEST_EQUAL( expected_alg2,
1999 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002000 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2001 {
2002 size_t length;
2003 ASSERT_ALLOC( export_buffer, material->len );
2004 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2005 material->len, &length ) );
2006 ASSERT_COMPARE( material->x, material->len,
2007 export_buffer, length );
2008 }
2009 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2010 goto exit;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002011 if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) )
2012 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002013
2014 PSA_ASSERT( psa_close_key( target_handle ) );
2015
2016exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002017 psa_reset_key_attributes( &source_attributes );
2018 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002019 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002020 mbedtls_free( export_buffer );
2021}
2022/* END_CASE */
2023
2024/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002025void copy_fail( int source_usage_arg,
2026 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002027 int type_arg, data_t *material,
2028 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002029 int target_usage_arg,
2030 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002031 int expected_status_arg )
2032{
2033 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2034 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2035 psa_key_handle_t source_handle = 0;
2036 psa_key_handle_t target_handle = 0;
2037
2038 PSA_ASSERT( psa_crypto_init( ) );
2039
2040 /* Prepare the source key. */
2041 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2042 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002043 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002044 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002045 PSA_ASSERT( psa_import_key( &source_attributes,
2046 material->x, material->len,
2047 &source_handle ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002048
2049 /* Prepare the target attributes. */
2050 psa_set_key_type( &target_attributes, target_type_arg );
2051 psa_set_key_bits( &target_attributes, target_bits_arg );
2052 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2053 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002054 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002055
2056 /* Try to copy the key. */
2057 TEST_EQUAL( psa_copy_key( source_handle,
2058 &target_attributes, &target_handle ),
2059 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002060
2061 PSA_ASSERT( psa_destroy_key( source_handle ) );
2062
Gilles Peskine4a644642019-05-03 17:14:08 +02002063exit:
2064 psa_reset_key_attributes( &source_attributes );
2065 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002066 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002067}
2068/* END_CASE */
2069
2070/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002071void hash_operation_init( )
2072{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002073 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002074 /* Test each valid way of initializing the object, except for `= {0}`, as
2075 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2076 * though it's OK by the C standard. We could test for this, but we'd need
2077 * to supress the Clang warning for the test. */
2078 psa_hash_operation_t func = psa_hash_operation_init( );
2079 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2080 psa_hash_operation_t zero;
2081
2082 memset( &zero, 0, sizeof( zero ) );
2083
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002084 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002085 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2086 PSA_ERROR_BAD_STATE );
2087 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2088 PSA_ERROR_BAD_STATE );
2089 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2090 PSA_ERROR_BAD_STATE );
2091
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002092 /* A default hash operation should be abortable without error. */
2093 PSA_ASSERT( psa_hash_abort( &func ) );
2094 PSA_ASSERT( psa_hash_abort( &init ) );
2095 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002096}
2097/* END_CASE */
2098
2099/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002100void hash_setup( int alg_arg,
2101 int expected_status_arg )
2102{
2103 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002104 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002105 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002106 psa_status_t status;
2107
Gilles Peskine8817f612018-12-18 00:18:46 +01002108 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002109
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002110 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002111 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002112
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002113 /* Whether setup succeeded or failed, abort must succeed. */
2114 PSA_ASSERT( psa_hash_abort( &operation ) );
2115
2116 /* If setup failed, reproduce the failure, so as to
2117 * test the resulting state of the operation object. */
2118 if( status != PSA_SUCCESS )
2119 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2120
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002121 /* Now the operation object should be reusable. */
2122#if defined(KNOWN_SUPPORTED_HASH_ALG)
2123 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2124 PSA_ASSERT( psa_hash_abort( &operation ) );
2125#endif
2126
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002127exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002128 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002129}
2130/* END_CASE */
2131
2132/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002133void hash_bad_order( )
2134{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002135 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002136 unsigned char input[] = "";
2137 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002138 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002139 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2140 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2141 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002142 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002143 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002144 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002145
Gilles Peskine8817f612018-12-18 00:18:46 +01002146 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002147
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002148 /* Call setup twice in a row. */
2149 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2150 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2151 PSA_ERROR_BAD_STATE );
2152 PSA_ASSERT( psa_hash_abort( &operation ) );
2153
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002154 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002155 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002156 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002157 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002158
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002159 /* Call update after finish. */
2160 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2161 PSA_ASSERT( psa_hash_finish( &operation,
2162 hash, sizeof( hash ), &hash_len ) );
2163 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002164 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002165 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002166
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002167 /* Call verify without calling setup beforehand. */
2168 TEST_EQUAL( psa_hash_verify( &operation,
2169 valid_hash, sizeof( valid_hash ) ),
2170 PSA_ERROR_BAD_STATE );
2171 PSA_ASSERT( psa_hash_abort( &operation ) );
2172
2173 /* Call verify after finish. */
2174 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2175 PSA_ASSERT( psa_hash_finish( &operation,
2176 hash, sizeof( hash ), &hash_len ) );
2177 TEST_EQUAL( psa_hash_verify( &operation,
2178 valid_hash, sizeof( valid_hash ) ),
2179 PSA_ERROR_BAD_STATE );
2180 PSA_ASSERT( psa_hash_abort( &operation ) );
2181
2182 /* Call verify twice in a row. */
2183 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2184 PSA_ASSERT( psa_hash_verify( &operation,
2185 valid_hash, sizeof( valid_hash ) ) );
2186 TEST_EQUAL( psa_hash_verify( &operation,
2187 valid_hash, sizeof( valid_hash ) ),
2188 PSA_ERROR_BAD_STATE );
2189 PSA_ASSERT( psa_hash_abort( &operation ) );
2190
2191 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002192 TEST_EQUAL( psa_hash_finish( &operation,
2193 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002194 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002195 PSA_ASSERT( psa_hash_abort( &operation ) );
2196
2197 /* Call finish twice in a row. */
2198 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2199 PSA_ASSERT( psa_hash_finish( &operation,
2200 hash, sizeof( hash ), &hash_len ) );
2201 TEST_EQUAL( psa_hash_finish( &operation,
2202 hash, sizeof( hash ), &hash_len ),
2203 PSA_ERROR_BAD_STATE );
2204 PSA_ASSERT( psa_hash_abort( &operation ) );
2205
2206 /* Call finish after calling verify. */
2207 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2208 PSA_ASSERT( psa_hash_verify( &operation,
2209 valid_hash, sizeof( valid_hash ) ) );
2210 TEST_EQUAL( psa_hash_finish( &operation,
2211 hash, sizeof( hash ), &hash_len ),
2212 PSA_ERROR_BAD_STATE );
2213 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002214
2215exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002216 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002217}
2218/* END_CASE */
2219
itayzafrir27e69452018-11-01 14:26:34 +02002220/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2221void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002222{
2223 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002224 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2225 * appended to it */
2226 unsigned char hash[] = {
2227 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2228 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2229 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002230 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002231 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002232
Gilles Peskine8817f612018-12-18 00:18:46 +01002233 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002234
itayzafrir27e69452018-11-01 14:26:34 +02002235 /* psa_hash_verify with a smaller hash 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, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002238 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002239
itayzafrir27e69452018-11-01 14:26:34 +02002240 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002241 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002242 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002243 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002244
itayzafrir27e69452018-11-01 14:26:34 +02002245 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002246 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002247 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002248 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002249
itayzafrirec93d302018-10-18 18:01:10 +03002250exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002251 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002252}
2253/* END_CASE */
2254
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002255/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2256void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002257{
2258 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002259 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002260 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002261 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002262 size_t hash_len;
2263
Gilles Peskine8817f612018-12-18 00:18:46 +01002264 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002265
itayzafrir58028322018-10-25 10:22:01 +03002266 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002267 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002268 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002269 hash, expected_size - 1, &hash_len ),
2270 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002271
2272exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002273 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002274}
2275/* END_CASE */
2276
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002277/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2278void hash_clone_source_state( )
2279{
2280 psa_algorithm_t alg = PSA_ALG_SHA_256;
2281 unsigned char hash[PSA_HASH_MAX_SIZE];
2282 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2283 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2284 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2285 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2286 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2287 size_t hash_len;
2288
2289 PSA_ASSERT( psa_crypto_init( ) );
2290 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2291
2292 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2293 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2294 PSA_ASSERT( psa_hash_finish( &op_finished,
2295 hash, sizeof( hash ), &hash_len ) );
2296 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2297 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2298
2299 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2300 PSA_ERROR_BAD_STATE );
2301
2302 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2303 PSA_ASSERT( psa_hash_finish( &op_init,
2304 hash, sizeof( hash ), &hash_len ) );
2305 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2306 PSA_ASSERT( psa_hash_finish( &op_finished,
2307 hash, sizeof( hash ), &hash_len ) );
2308 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2309 PSA_ASSERT( psa_hash_finish( &op_aborted,
2310 hash, sizeof( hash ), &hash_len ) );
2311
2312exit:
2313 psa_hash_abort( &op_source );
2314 psa_hash_abort( &op_init );
2315 psa_hash_abort( &op_setup );
2316 psa_hash_abort( &op_finished );
2317 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002318 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002319}
2320/* END_CASE */
2321
2322/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2323void hash_clone_target_state( )
2324{
2325 psa_algorithm_t alg = PSA_ALG_SHA_256;
2326 unsigned char hash[PSA_HASH_MAX_SIZE];
2327 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2328 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2329 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2330 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2331 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2332 size_t hash_len;
2333
2334 PSA_ASSERT( psa_crypto_init( ) );
2335
2336 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2337 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2338 PSA_ASSERT( psa_hash_finish( &op_finished,
2339 hash, sizeof( hash ), &hash_len ) );
2340 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2341 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2342
2343 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2344 PSA_ASSERT( psa_hash_finish( &op_target,
2345 hash, sizeof( hash ), &hash_len ) );
2346
2347 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2348 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2349 PSA_ERROR_BAD_STATE );
2350 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2351 PSA_ERROR_BAD_STATE );
2352
2353exit:
2354 psa_hash_abort( &op_target );
2355 psa_hash_abort( &op_init );
2356 psa_hash_abort( &op_setup );
2357 psa_hash_abort( &op_finished );
2358 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002359 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002360}
2361/* END_CASE */
2362
itayzafrir58028322018-10-25 10:22:01 +03002363/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002364void mac_operation_init( )
2365{
Jaeden Amero252ef282019-02-15 14:05:35 +00002366 const uint8_t input[1] = { 0 };
2367
Jaeden Amero769ce272019-01-04 11:48:03 +00002368 /* Test each valid way of initializing the object, except for `= {0}`, as
2369 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2370 * though it's OK by the C standard. We could test for this, but we'd need
2371 * to supress the Clang warning for the test. */
2372 psa_mac_operation_t func = psa_mac_operation_init( );
2373 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2374 psa_mac_operation_t zero;
2375
2376 memset( &zero, 0, sizeof( zero ) );
2377
Jaeden Amero252ef282019-02-15 14:05:35 +00002378 /* A freshly-initialized MAC operation should not be usable. */
2379 TEST_EQUAL( psa_mac_update( &func,
2380 input, sizeof( input ) ),
2381 PSA_ERROR_BAD_STATE );
2382 TEST_EQUAL( psa_mac_update( &init,
2383 input, sizeof( input ) ),
2384 PSA_ERROR_BAD_STATE );
2385 TEST_EQUAL( psa_mac_update( &zero,
2386 input, sizeof( input ) ),
2387 PSA_ERROR_BAD_STATE );
2388
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002389 /* A default MAC operation should be abortable without error. */
2390 PSA_ASSERT( psa_mac_abort( &func ) );
2391 PSA_ASSERT( psa_mac_abort( &init ) );
2392 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002393}
2394/* END_CASE */
2395
2396/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002397void mac_setup( int key_type_arg,
2398 data_t *key,
2399 int alg_arg,
2400 int expected_status_arg )
2401{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002402 psa_key_type_t key_type = key_type_arg;
2403 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002404 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002405 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002406 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2407#if defined(KNOWN_SUPPORTED_MAC_ALG)
2408 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2409#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002410
Gilles Peskine8817f612018-12-18 00:18:46 +01002411 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002412
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002413 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2414 &operation, &status ) )
2415 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002416 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002417
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002418 /* The operation object should be reusable. */
2419#if defined(KNOWN_SUPPORTED_MAC_ALG)
2420 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2421 smoke_test_key_data,
2422 sizeof( smoke_test_key_data ),
2423 KNOWN_SUPPORTED_MAC_ALG,
2424 &operation, &status ) )
2425 goto exit;
2426 TEST_EQUAL( status, PSA_SUCCESS );
2427#endif
2428
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002429exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002430 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002431}
2432/* END_CASE */
2433
2434/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002435void mac_bad_order( )
2436{
2437 psa_key_handle_t handle = 0;
2438 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2439 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2440 const uint8_t key[] = {
2441 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2442 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2443 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002444 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002445 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2446 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2447 size_t sign_mac_length = 0;
2448 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2449 const uint8_t verify_mac[] = {
2450 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2451 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2452 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2453
2454 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002455 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
2456 psa_set_key_algorithm( &attributes, alg );
2457 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002458
Gilles Peskine73676cb2019-05-15 20:15:10 +02002459 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002460
Jaeden Amero252ef282019-02-15 14:05:35 +00002461 /* Call update without calling setup beforehand. */
2462 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2463 PSA_ERROR_BAD_STATE );
2464 PSA_ASSERT( psa_mac_abort( &operation ) );
2465
2466 /* Call sign finish without calling setup beforehand. */
2467 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2468 &sign_mac_length),
2469 PSA_ERROR_BAD_STATE );
2470 PSA_ASSERT( psa_mac_abort( &operation ) );
2471
2472 /* Call verify finish without calling setup beforehand. */
2473 TEST_EQUAL( psa_mac_verify_finish( &operation,
2474 verify_mac, sizeof( verify_mac ) ),
2475 PSA_ERROR_BAD_STATE );
2476 PSA_ASSERT( psa_mac_abort( &operation ) );
2477
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002478 /* Call setup twice in a row. */
2479 PSA_ASSERT( psa_mac_sign_setup( &operation,
2480 handle, alg ) );
2481 TEST_EQUAL( psa_mac_sign_setup( &operation,
2482 handle, alg ),
2483 PSA_ERROR_BAD_STATE );
2484 PSA_ASSERT( psa_mac_abort( &operation ) );
2485
Jaeden Amero252ef282019-02-15 14:05:35 +00002486 /* Call update after sign finish. */
2487 PSA_ASSERT( psa_mac_sign_setup( &operation,
2488 handle, alg ) );
2489 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2490 PSA_ASSERT( psa_mac_sign_finish( &operation,
2491 sign_mac, sizeof( sign_mac ),
2492 &sign_mac_length ) );
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 update after verify finish. */
2498 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002499 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002500 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2501 PSA_ASSERT( psa_mac_verify_finish( &operation,
2502 verify_mac, sizeof( verify_mac ) ) );
2503 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2504 PSA_ERROR_BAD_STATE );
2505 PSA_ASSERT( psa_mac_abort( &operation ) );
2506
2507 /* Call sign finish twice in a row. */
2508 PSA_ASSERT( psa_mac_sign_setup( &operation,
2509 handle, alg ) );
2510 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2511 PSA_ASSERT( psa_mac_sign_finish( &operation,
2512 sign_mac, sizeof( sign_mac ),
2513 &sign_mac_length ) );
2514 TEST_EQUAL( psa_mac_sign_finish( &operation,
2515 sign_mac, sizeof( sign_mac ),
2516 &sign_mac_length ),
2517 PSA_ERROR_BAD_STATE );
2518 PSA_ASSERT( psa_mac_abort( &operation ) );
2519
2520 /* Call verify finish twice in a row. */
2521 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002522 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002523 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2524 PSA_ASSERT( psa_mac_verify_finish( &operation,
2525 verify_mac, sizeof( verify_mac ) ) );
2526 TEST_EQUAL( psa_mac_verify_finish( &operation,
2527 verify_mac, sizeof( verify_mac ) ),
2528 PSA_ERROR_BAD_STATE );
2529 PSA_ASSERT( psa_mac_abort( &operation ) );
2530
2531 /* Setup sign but try verify. */
2532 PSA_ASSERT( psa_mac_sign_setup( &operation,
2533 handle, alg ) );
2534 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2535 TEST_EQUAL( psa_mac_verify_finish( &operation,
2536 verify_mac, sizeof( verify_mac ) ),
2537 PSA_ERROR_BAD_STATE );
2538 PSA_ASSERT( psa_mac_abort( &operation ) );
2539
2540 /* Setup verify but try sign. */
2541 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002542 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002543 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2544 TEST_EQUAL( psa_mac_sign_finish( &operation,
2545 sign_mac, sizeof( sign_mac ),
2546 &sign_mac_length ),
2547 PSA_ERROR_BAD_STATE );
2548 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002549
Gilles Peskine76b29a72019-05-28 14:08:50 +02002550 PSA_ASSERT( psa_destroy_key( handle ) );
2551
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002552exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002553 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002554}
2555/* END_CASE */
2556
2557/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002558void mac_sign( int key_type_arg,
2559 data_t *key,
2560 int alg_arg,
2561 data_t *input,
2562 data_t *expected_mac )
2563{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002564 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002565 psa_key_type_t key_type = key_type_arg;
2566 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002567 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002568 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002569 /* Leave a little extra room in the output buffer. At the end of the
2570 * test, we'll check that the implementation didn't overwrite onto
2571 * this extra room. */
2572 uint8_t actual_mac[PSA_MAC_MAX_SIZE + 10];
2573 size_t mac_buffer_size =
2574 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
2575 size_t mac_length = 0;
2576
2577 memset( actual_mac, '+', sizeof( actual_mac ) );
2578 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
2579 TEST_ASSERT( expected_mac->len <= mac_buffer_size );
2580
Gilles Peskine8817f612018-12-18 00:18:46 +01002581 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002582
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002583 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
2584 psa_set_key_algorithm( &attributes, alg );
2585 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002586
Gilles Peskine73676cb2019-05-15 20:15:10 +02002587 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002588
2589 /* Calculate the MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01002590 PSA_ASSERT( psa_mac_sign_setup( &operation,
2591 handle, alg ) );
2592 PSA_ASSERT( psa_mac_update( &operation,
2593 input->x, input->len ) );
2594 PSA_ASSERT( psa_mac_sign_finish( &operation,
2595 actual_mac, mac_buffer_size,
2596 &mac_length ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002597
2598 /* Compare with the expected value. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01002599 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2600 actual_mac, mac_length );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002601
2602 /* Verify that the end of the buffer is untouched. */
2603 TEST_ASSERT( mem_is_char( actual_mac + mac_length, '+',
2604 sizeof( actual_mac ) - mac_length ) );
2605
2606exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002607 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002608 PSA_DONE( );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002609}
2610/* END_CASE */
2611
2612/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002613void mac_verify( int key_type_arg,
2614 data_t *key,
2615 int alg_arg,
2616 data_t *input,
2617 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002618{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002619 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002620 psa_key_type_t key_type = key_type_arg;
2621 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002622 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002623 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002624
Gilles Peskine69c12672018-06-28 00:07:19 +02002625 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2626
Gilles Peskine8817f612018-12-18 00:18:46 +01002627 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002628
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002629 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
2630 psa_set_key_algorithm( &attributes, alg );
2631 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002632
Gilles Peskine73676cb2019-05-15 20:15:10 +02002633 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002634
Gilles Peskine8817f612018-12-18 00:18:46 +01002635 PSA_ASSERT( psa_mac_verify_setup( &operation,
2636 handle, alg ) );
2637 PSA_ASSERT( psa_destroy_key( handle ) );
2638 PSA_ASSERT( psa_mac_update( &operation,
2639 input->x, input->len ) );
2640 PSA_ASSERT( psa_mac_verify_finish( &operation,
2641 expected_mac->x,
2642 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002643
2644exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002645 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002646 PSA_DONE( );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002647}
2648/* END_CASE */
2649
2650/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002651void cipher_operation_init( )
2652{
Jaeden Ameroab439972019-02-15 14:12:05 +00002653 const uint8_t input[1] = { 0 };
2654 unsigned char output[1] = { 0 };
2655 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002656 /* Test each valid way of initializing the object, except for `= {0}`, as
2657 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2658 * though it's OK by the C standard. We could test for this, but we'd need
2659 * to supress the Clang warning for the test. */
2660 psa_cipher_operation_t func = psa_cipher_operation_init( );
2661 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2662 psa_cipher_operation_t zero;
2663
2664 memset( &zero, 0, sizeof( zero ) );
2665
Jaeden Ameroab439972019-02-15 14:12:05 +00002666 /* A freshly-initialized cipher operation should not be usable. */
2667 TEST_EQUAL( psa_cipher_update( &func,
2668 input, sizeof( input ),
2669 output, sizeof( output ),
2670 &output_length ),
2671 PSA_ERROR_BAD_STATE );
2672 TEST_EQUAL( psa_cipher_update( &init,
2673 input, sizeof( input ),
2674 output, sizeof( output ),
2675 &output_length ),
2676 PSA_ERROR_BAD_STATE );
2677 TEST_EQUAL( psa_cipher_update( &zero,
2678 input, sizeof( input ),
2679 output, sizeof( output ),
2680 &output_length ),
2681 PSA_ERROR_BAD_STATE );
2682
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002683 /* A default cipher operation should be abortable without error. */
2684 PSA_ASSERT( psa_cipher_abort( &func ) );
2685 PSA_ASSERT( psa_cipher_abort( &init ) );
2686 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002687}
2688/* END_CASE */
2689
2690/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002691void cipher_setup( int key_type_arg,
2692 data_t *key,
2693 int alg_arg,
2694 int expected_status_arg )
2695{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002696 psa_key_type_t key_type = key_type_arg;
2697 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002698 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002699 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002700 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002701#if defined(KNOWN_SUPPORTED_MAC_ALG)
2702 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2703#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002704
Gilles Peskine8817f612018-12-18 00:18:46 +01002705 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002706
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002707 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2708 &operation, &status ) )
2709 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002710 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002711
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002712 /* The operation object should be reusable. */
2713#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2714 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2715 smoke_test_key_data,
2716 sizeof( smoke_test_key_data ),
2717 KNOWN_SUPPORTED_CIPHER_ALG,
2718 &operation, &status ) )
2719 goto exit;
2720 TEST_EQUAL( status, PSA_SUCCESS );
2721#endif
2722
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002723exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002724 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002725}
2726/* END_CASE */
2727
2728/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00002729void cipher_bad_order( )
2730{
2731 psa_key_handle_t handle = 0;
2732 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2733 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002734 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002735 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
2736 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2737 const uint8_t key[] = {
2738 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2739 0xaa, 0xaa, 0xaa, 0xaa };
2740 const uint8_t text[] = {
2741 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2742 0xbb, 0xbb, 0xbb, 0xbb };
2743 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
2744 size_t length = 0;
2745
2746 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002747 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2748 psa_set_key_algorithm( &attributes, alg );
2749 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02002750 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002751
2752
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002753 /* Call encrypt setup twice in a row. */
2754 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2755 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
2756 PSA_ERROR_BAD_STATE );
2757 PSA_ASSERT( psa_cipher_abort( &operation ) );
2758
2759 /* Call decrypt setup twice in a row. */
2760 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
2761 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
2762 PSA_ERROR_BAD_STATE );
2763 PSA_ASSERT( psa_cipher_abort( &operation ) );
2764
Jaeden Ameroab439972019-02-15 14:12:05 +00002765 /* Generate an IV without calling setup beforehand. */
2766 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2767 buffer, sizeof( buffer ),
2768 &length ),
2769 PSA_ERROR_BAD_STATE );
2770 PSA_ASSERT( psa_cipher_abort( &operation ) );
2771
2772 /* Generate an IV twice in a row. */
2773 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2774 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2775 buffer, sizeof( buffer ),
2776 &length ) );
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 /* Generate an IV after it's already set. */
2784 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2785 PSA_ASSERT( psa_cipher_set_iv( &operation,
2786 iv, sizeof( iv ) ) );
2787 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2788 buffer, sizeof( buffer ),
2789 &length ),
2790 PSA_ERROR_BAD_STATE );
2791 PSA_ASSERT( psa_cipher_abort( &operation ) );
2792
2793 /* Set an IV without calling setup beforehand. */
2794 TEST_EQUAL( psa_cipher_set_iv( &operation,
2795 iv, sizeof( iv ) ),
2796 PSA_ERROR_BAD_STATE );
2797 PSA_ASSERT( psa_cipher_abort( &operation ) );
2798
2799 /* Set an IV after it's already set. */
2800 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2801 PSA_ASSERT( psa_cipher_set_iv( &operation,
2802 iv, sizeof( iv ) ) );
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 /* Set an IV after it's already generated. */
2809 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2810 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2811 buffer, sizeof( buffer ),
2812 &length ) );
2813 TEST_EQUAL( psa_cipher_set_iv( &operation,
2814 iv, sizeof( iv ) ),
2815 PSA_ERROR_BAD_STATE );
2816 PSA_ASSERT( psa_cipher_abort( &operation ) );
2817
2818 /* Call update without calling setup beforehand. */
2819 TEST_EQUAL( psa_cipher_update( &operation,
2820 text, sizeof( text ),
2821 buffer, sizeof( buffer ),
2822 &length ),
2823 PSA_ERROR_BAD_STATE );
2824 PSA_ASSERT( psa_cipher_abort( &operation ) );
2825
2826 /* Call update without an IV where an IV is required. */
2827 TEST_EQUAL( psa_cipher_update( &operation,
2828 text, sizeof( text ),
2829 buffer, sizeof( buffer ),
2830 &length ),
2831 PSA_ERROR_BAD_STATE );
2832 PSA_ASSERT( psa_cipher_abort( &operation ) );
2833
2834 /* Call update after finish. */
2835 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2836 PSA_ASSERT( psa_cipher_set_iv( &operation,
2837 iv, sizeof( iv ) ) );
2838 PSA_ASSERT( psa_cipher_finish( &operation,
2839 buffer, sizeof( buffer ), &length ) );
2840 TEST_EQUAL( psa_cipher_update( &operation,
2841 text, sizeof( text ),
2842 buffer, sizeof( buffer ),
2843 &length ),
2844 PSA_ERROR_BAD_STATE );
2845 PSA_ASSERT( psa_cipher_abort( &operation ) );
2846
2847 /* Call finish without calling setup beforehand. */
2848 TEST_EQUAL( psa_cipher_finish( &operation,
2849 buffer, sizeof( buffer ), &length ),
2850 PSA_ERROR_BAD_STATE );
2851 PSA_ASSERT( psa_cipher_abort( &operation ) );
2852
2853 /* Call finish without an IV where an IV is required. */
2854 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2855 /* Not calling update means we are encrypting an empty buffer, which is OK
2856 * for cipher modes with padding. */
2857 TEST_EQUAL( psa_cipher_finish( &operation,
2858 buffer, sizeof( buffer ), &length ),
2859 PSA_ERROR_BAD_STATE );
2860 PSA_ASSERT( psa_cipher_abort( &operation ) );
2861
2862 /* Call finish twice in a row. */
2863 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
2864 PSA_ASSERT( psa_cipher_set_iv( &operation,
2865 iv, sizeof( iv ) ) );
2866 PSA_ASSERT( psa_cipher_finish( &operation,
2867 buffer, sizeof( buffer ), &length ) );
2868 TEST_EQUAL( psa_cipher_finish( &operation,
2869 buffer, sizeof( buffer ), &length ),
2870 PSA_ERROR_BAD_STATE );
2871 PSA_ASSERT( psa_cipher_abort( &operation ) );
2872
Gilles Peskine76b29a72019-05-28 14:08:50 +02002873 PSA_ASSERT( psa_destroy_key( handle ) );
2874
Jaeden Ameroab439972019-02-15 14:12:05 +00002875exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002876 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002877}
2878/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002879
Gilles Peskine50e586b2018-06-08 14:28:46 +02002880/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002881void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02002882 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002883 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002884 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002885{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002886 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002887 psa_status_t status;
2888 psa_key_type_t key_type = key_type_arg;
2889 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002890 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002891 unsigned char *output = NULL;
2892 size_t output_buffer_size = 0;
2893 size_t function_output_length = 0;
2894 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002895 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002896 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002897
Gilles Peskine8817f612018-12-18 00:18:46 +01002898 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002899
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002900 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2901 psa_set_key_algorithm( &attributes, alg );
2902 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002903
Gilles Peskine73676cb2019-05-15 20:15:10 +02002904 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002905
Gilles Peskine8817f612018-12-18 00:18:46 +01002906 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2907 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002908
Gilles Peskine423005e2019-05-06 15:22:57 +02002909 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002910 output_buffer_size = ( (size_t) input->len +
2911 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002912 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002913
Gilles Peskine8817f612018-12-18 00:18:46 +01002914 PSA_ASSERT( psa_cipher_update( &operation,
2915 input->x, input->len,
2916 output, output_buffer_size,
2917 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002918 total_output_length += function_output_length;
2919 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002920 output + total_output_length,
2921 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002922 &function_output_length );
2923 total_output_length += function_output_length;
2924
Gilles Peskinefe11b722018-12-18 00:24:04 +01002925 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002926 if( expected_status == PSA_SUCCESS )
2927 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002928 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002929 ASSERT_COMPARE( expected_output->x, expected_output->len,
2930 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002931 }
2932
2933exit:
2934 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002935 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002936 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002937}
2938/* END_CASE */
2939
2940/* BEGIN_CASE */
2941void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02002942 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002943 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002944 int first_part_size_arg,
2945 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002946 data_t *expected_output )
2947{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002948 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002949 psa_key_type_t key_type = key_type_arg;
2950 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002951 size_t first_part_size = first_part_size_arg;
2952 size_t output1_length = output1_length_arg;
2953 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002954 unsigned char *output = NULL;
2955 size_t output_buffer_size = 0;
2956 size_t function_output_length = 0;
2957 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002958 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002959 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002960
Gilles Peskine8817f612018-12-18 00:18:46 +01002961 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002962
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002963 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2964 psa_set_key_algorithm( &attributes, alg );
2965 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002966
Gilles Peskine73676cb2019-05-15 20:15:10 +02002967 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002968
Gilles Peskine8817f612018-12-18 00:18:46 +01002969 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
2970 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002971
Gilles Peskine423005e2019-05-06 15:22:57 +02002972 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01002973 output_buffer_size = ( (size_t) input->len +
2974 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002975 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002976
Gilles Peskinee0866522019-02-19 19:44:00 +01002977 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002978 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2979 output, output_buffer_size,
2980 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002981 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002982 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002983 PSA_ASSERT( psa_cipher_update( &operation,
2984 input->x + first_part_size,
2985 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002986 output + total_output_length,
2987 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002988 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002989 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002990 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002991 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002992 output + total_output_length,
2993 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002994 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002995 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002996 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002997
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002998 ASSERT_COMPARE( expected_output->x, expected_output->len,
2999 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003000
3001exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003002 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003003 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003004 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003005}
3006/* END_CASE */
3007
3008/* BEGIN_CASE */
3009void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003010 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003011 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003012 int first_part_size_arg,
3013 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003014 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003015{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003016 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003017
3018 psa_key_type_t key_type = key_type_arg;
3019 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003020 size_t first_part_size = first_part_size_arg;
3021 size_t output1_length = output1_length_arg;
3022 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003023 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003024 size_t output_buffer_size = 0;
3025 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003026 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003027 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003028 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003029
Gilles Peskine8817f612018-12-18 00:18:46 +01003030 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003031
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003032 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3033 psa_set_key_algorithm( &attributes, alg );
3034 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003035
Gilles Peskine73676cb2019-05-15 20:15:10 +02003036 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003037
Gilles Peskine8817f612018-12-18 00:18:46 +01003038 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3039 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003040
Gilles Peskine423005e2019-05-06 15:22:57 +02003041 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003042
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003043 output_buffer_size = ( (size_t) input->len +
3044 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003045 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003046
Gilles Peskinee0866522019-02-19 19:44:00 +01003047 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003048 PSA_ASSERT( psa_cipher_update( &operation,
3049 input->x, first_part_size,
3050 output, output_buffer_size,
3051 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003052 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003053 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003054 PSA_ASSERT( psa_cipher_update( &operation,
3055 input->x + first_part_size,
3056 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003057 output + total_output_length,
3058 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003059 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003060 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003061 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003062 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003063 output + total_output_length,
3064 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003065 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003066 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003067 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003068
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003069 ASSERT_COMPARE( expected_output->x, expected_output->len,
3070 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003071
3072exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003073 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003074 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003075 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003076}
3077/* END_CASE */
3078
Gilles Peskine50e586b2018-06-08 14:28:46 +02003079/* BEGIN_CASE */
3080void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003081 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003082 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003083 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003084{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003085 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003086 psa_status_t status;
3087 psa_key_type_t key_type = key_type_arg;
3088 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003089 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003090 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003091 size_t output_buffer_size = 0;
3092 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003093 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003094 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003095 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003096
Gilles Peskine8817f612018-12-18 00:18:46 +01003097 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003098
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003099 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3100 psa_set_key_algorithm( &attributes, alg );
3101 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003102
Gilles Peskine73676cb2019-05-15 20:15:10 +02003103 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003104
Gilles Peskine8817f612018-12-18 00:18:46 +01003105 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3106 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003107
Gilles Peskine423005e2019-05-06 15:22:57 +02003108 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003109
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003110 output_buffer_size = ( (size_t) input->len +
3111 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003112 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003113
Gilles Peskine8817f612018-12-18 00:18:46 +01003114 PSA_ASSERT( psa_cipher_update( &operation,
3115 input->x, input->len,
3116 output, output_buffer_size,
3117 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003118 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003119 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003120 output + total_output_length,
3121 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003122 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003123 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003124 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003125
3126 if( expected_status == PSA_SUCCESS )
3127 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003128 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003129 ASSERT_COMPARE( expected_output->x, expected_output->len,
3130 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003131 }
3132
Gilles Peskine50e586b2018-06-08 14:28:46 +02003133exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003134 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003135 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003136 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003137}
3138/* END_CASE */
3139
Gilles Peskine50e586b2018-06-08 14:28:46 +02003140/* BEGIN_CASE */
3141void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003142 data_t *key,
3143 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003144{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003145 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003146 psa_key_type_t key_type = key_type_arg;
3147 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003148 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003149 size_t iv_size = 16;
3150 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003151 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003152 size_t output1_size = 0;
3153 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003154 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003155 size_t output2_size = 0;
3156 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003157 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003158 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3159 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003160 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003161
Gilles Peskine8817f612018-12-18 00:18:46 +01003162 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003163
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003164 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3165 psa_set_key_algorithm( &attributes, alg );
3166 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003167
Gilles Peskine73676cb2019-05-15 20:15:10 +02003168 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003169
Gilles Peskine8817f612018-12-18 00:18:46 +01003170 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3171 handle, alg ) );
3172 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3173 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003174
Gilles Peskine8817f612018-12-18 00:18:46 +01003175 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3176 iv, iv_size,
3177 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003178 output1_size = ( (size_t) input->len +
3179 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003180 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003181
Gilles Peskine8817f612018-12-18 00:18:46 +01003182 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3183 output1, output1_size,
3184 &output1_length ) );
3185 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003186 output1 + output1_length,
3187 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003188 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003189
Gilles Peskine048b7f02018-06-08 14:20:49 +02003190 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003191
Gilles Peskine8817f612018-12-18 00:18:46 +01003192 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003193
3194 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003195 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003196
Gilles Peskine8817f612018-12-18 00:18:46 +01003197 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3198 iv, iv_length ) );
3199 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3200 output2, output2_size,
3201 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003202 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003203 PSA_ASSERT( psa_cipher_finish( &operation2,
3204 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003205 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003206 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003207
Gilles Peskine048b7f02018-06-08 14:20:49 +02003208 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003209
Gilles Peskine8817f612018-12-18 00:18:46 +01003210 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003211
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003212 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003213
3214exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003215 mbedtls_free( output1 );
3216 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003217 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003218 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003219}
3220/* END_CASE */
3221
3222/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003223void cipher_verify_output_multipart( int alg_arg,
3224 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003225 data_t *key,
3226 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003227 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003228{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003229 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003230 psa_key_type_t key_type = key_type_arg;
3231 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003232 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003233 unsigned char iv[16] = {0};
3234 size_t iv_size = 16;
3235 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003236 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003237 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003238 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003239 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003240 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003241 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003242 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003243 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3244 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003245 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003246
Gilles Peskine8817f612018-12-18 00:18:46 +01003247 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003248
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003249 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3250 psa_set_key_algorithm( &attributes, alg );
3251 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003252
Gilles Peskine73676cb2019-05-15 20:15:10 +02003253 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003254
Gilles Peskine8817f612018-12-18 00:18:46 +01003255 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3256 handle, alg ) );
3257 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3258 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003259
Gilles Peskine8817f612018-12-18 00:18:46 +01003260 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3261 iv, iv_size,
3262 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003263 output1_buffer_size = ( (size_t) input->len +
3264 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003265 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003266
Gilles Peskinee0866522019-02-19 19:44:00 +01003267 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003268
Gilles Peskine8817f612018-12-18 00:18:46 +01003269 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3270 output1, output1_buffer_size,
3271 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003272 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003273
Gilles Peskine8817f612018-12-18 00:18:46 +01003274 PSA_ASSERT( psa_cipher_update( &operation1,
3275 input->x + first_part_size,
3276 input->len - first_part_size,
3277 output1, output1_buffer_size,
3278 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003279 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003280
Gilles Peskine8817f612018-12-18 00:18:46 +01003281 PSA_ASSERT( psa_cipher_finish( &operation1,
3282 output1 + output1_length,
3283 output1_buffer_size - output1_length,
3284 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003285 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003286
Gilles Peskine8817f612018-12-18 00:18:46 +01003287 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003288
Gilles Peskine048b7f02018-06-08 14:20:49 +02003289 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003290 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003291
Gilles Peskine8817f612018-12-18 00:18:46 +01003292 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3293 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003294
Gilles Peskine8817f612018-12-18 00:18:46 +01003295 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3296 output2, output2_buffer_size,
3297 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003298 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003299
Gilles Peskine8817f612018-12-18 00:18:46 +01003300 PSA_ASSERT( psa_cipher_update( &operation2,
3301 output1 + first_part_size,
3302 output1_length - first_part_size,
3303 output2, output2_buffer_size,
3304 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003305 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003306
Gilles Peskine8817f612018-12-18 00:18:46 +01003307 PSA_ASSERT( psa_cipher_finish( &operation2,
3308 output2 + output2_length,
3309 output2_buffer_size - output2_length,
3310 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003311 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003312
Gilles Peskine8817f612018-12-18 00:18:46 +01003313 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003314
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003315 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003316
3317exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03003318 mbedtls_free( output1 );
3319 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003320 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003321 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003322}
3323/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003324
Gilles Peskine20035e32018-02-03 22:44:14 +01003325/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003326void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003327 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003328 data_t *nonce,
3329 data_t *additional_data,
3330 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003331 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003332{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003333 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003334 psa_key_type_t key_type = key_type_arg;
3335 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003336 unsigned char *output_data = NULL;
3337 size_t output_size = 0;
3338 size_t output_length = 0;
3339 unsigned char *output_data2 = NULL;
3340 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003341 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003342 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003344
Gilles Peskine4abf7412018-06-18 16:35:34 +02003345 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003346 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3347 * should be exact. */
3348 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3349 TEST_EQUAL( output_size,
3350 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003351 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003352
Gilles Peskine8817f612018-12-18 00:18:46 +01003353 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003354
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003355 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3356 psa_set_key_algorithm( &attributes, alg );
3357 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003358
Gilles Peskine049c7532019-05-15 20:22:09 +02003359 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3360 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003361
Gilles Peskinefe11b722018-12-18 00:24:04 +01003362 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3363 nonce->x, nonce->len,
3364 additional_data->x,
3365 additional_data->len,
3366 input_data->x, input_data->len,
3367 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003368 &output_length ),
3369 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003370
3371 if( PSA_SUCCESS == expected_result )
3372 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003373 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003374
Gilles Peskine003a4a92019-05-14 16:09:40 +02003375 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3376 * should be exact. */
3377 TEST_EQUAL( input_data->len,
3378 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3379
Gilles Peskinefe11b722018-12-18 00:24:04 +01003380 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3381 nonce->x, nonce->len,
3382 additional_data->x,
3383 additional_data->len,
3384 output_data, output_length,
3385 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003386 &output_length2 ),
3387 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003388
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003389 ASSERT_COMPARE( input_data->x, input_data->len,
3390 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003391 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003392
Gilles Peskinea1cac842018-06-11 19:33:02 +02003393exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003394 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003395 mbedtls_free( output_data );
3396 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003397 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003398}
3399/* END_CASE */
3400
3401/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003402void aead_encrypt( int key_type_arg, data_t *key_data,
3403 int alg_arg,
3404 data_t *nonce,
3405 data_t *additional_data,
3406 data_t *input_data,
3407 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003408{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003409 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003410 psa_key_type_t key_type = key_type_arg;
3411 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003412 unsigned char *output_data = NULL;
3413 size_t output_size = 0;
3414 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003415 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003416 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003417
Gilles Peskine4abf7412018-06-18 16:35:34 +02003418 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003419 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3420 * should be exact. */
3421 TEST_EQUAL( output_size,
3422 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003423 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003424
Gilles Peskine8817f612018-12-18 00:18:46 +01003425 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003426
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003427 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3428 psa_set_key_algorithm( &attributes, alg );
3429 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003430
Gilles Peskine049c7532019-05-15 20:22:09 +02003431 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3432 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003433
Gilles Peskine8817f612018-12-18 00:18:46 +01003434 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3435 nonce->x, nonce->len,
3436 additional_data->x, additional_data->len,
3437 input_data->x, input_data->len,
3438 output_data, output_size,
3439 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003440
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003441 ASSERT_COMPARE( expected_result->x, expected_result->len,
3442 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003443
Gilles Peskinea1cac842018-06-11 19:33:02 +02003444exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003445 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003446 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003447 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003448}
3449/* END_CASE */
3450
3451/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003452void aead_decrypt( int key_type_arg, data_t *key_data,
3453 int alg_arg,
3454 data_t *nonce,
3455 data_t *additional_data,
3456 data_t *input_data,
3457 data_t *expected_data,
3458 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003459{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003460 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003461 psa_key_type_t key_type = key_type_arg;
3462 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003463 unsigned char *output_data = NULL;
3464 size_t output_size = 0;
3465 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003466 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003467 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003468 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003469
Gilles Peskine003a4a92019-05-14 16:09:40 +02003470 output_size = input_data->len - tag_length;
3471 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3472 * should be exact. */
3473 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3474 TEST_EQUAL( output_size,
3475 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003476 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003477
Gilles Peskine8817f612018-12-18 00:18:46 +01003478 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003479
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003480 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3481 psa_set_key_algorithm( &attributes, alg );
3482 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003483
Gilles Peskine049c7532019-05-15 20:22:09 +02003484 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3485 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003486
Gilles Peskinefe11b722018-12-18 00:24:04 +01003487 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3488 nonce->x, nonce->len,
3489 additional_data->x,
3490 additional_data->len,
3491 input_data->x, input_data->len,
3492 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003493 &output_length ),
3494 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003495
Gilles Peskine2d277862018-06-18 15:41:12 +02003496 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003497 ASSERT_COMPARE( expected_data->x, expected_data->len,
3498 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003499
Gilles Peskinea1cac842018-06-11 19:33:02 +02003500exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003501 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003502 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003503 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003504}
3505/* END_CASE */
3506
3507/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003508void signature_size( int type_arg,
3509 int bits,
3510 int alg_arg,
3511 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003512{
3513 psa_key_type_t type = type_arg;
3514 psa_algorithm_t alg = alg_arg;
Gilles Peskine2d277862018-06-18 15:41:12 +02003515 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003516 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003517exit:
3518 ;
3519}
3520/* END_CASE */
3521
3522/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003523void sign_deterministic( int key_type_arg, data_t *key_data,
3524 int alg_arg, data_t *input_data,
3525 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003526{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003527 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003528 psa_key_type_t key_type = key_type_arg;
3529 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003530 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003531 unsigned char *signature = NULL;
3532 size_t signature_size;
3533 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003534 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003535
Gilles Peskine8817f612018-12-18 00:18:46 +01003536 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003537
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003538 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3539 psa_set_key_algorithm( &attributes, alg );
3540 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003541
Gilles Peskine049c7532019-05-15 20:22:09 +02003542 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3543 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003544 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3545 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003546
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003547 /* Allocate a buffer which has the size advertized by the
3548 * library. */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003549 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3550 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003551 TEST_ASSERT( signature_size != 0 );
Gilles Peskine69c12672018-06-28 00:07:19 +02003552 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003553 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003554
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003555 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003556 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3557 input_data->x, input_data->len,
3558 signature, signature_size,
3559 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003560 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003561 ASSERT_COMPARE( output_data->x, output_data->len,
3562 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003563
3564exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003565 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003566 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01003567 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003568 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003569}
3570/* END_CASE */
3571
3572/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003573void sign_fail( int key_type_arg, data_t *key_data,
3574 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003575 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003576{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003577 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01003578 psa_key_type_t key_type = key_type_arg;
3579 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003580 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003581 psa_status_t actual_status;
3582 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003583 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003584 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003585 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003586
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003587 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003588
Gilles Peskine8817f612018-12-18 00:18:46 +01003589 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003590
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003591 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN );
3592 psa_set_key_algorithm( &attributes, alg );
3593 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003594
Gilles Peskine049c7532019-05-15 20:22:09 +02003595 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3596 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003597
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003598 actual_status = psa_asymmetric_sign( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003599 input_data->x, input_data->len,
Gilles Peskine20035e32018-02-03 22:44:14 +01003600 signature, signature_size,
3601 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003602 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003603 /* The value of *signature_length is unspecified on error, but
3604 * whatever it is, it should be less than signature_size, so that
3605 * if the caller tries to read *signature_length bytes without
3606 * checking the error code then they don't overflow a buffer. */
3607 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003608
3609exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003610 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003611 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01003612 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003613 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003614}
3615/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003616
3617/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02003618void sign_verify( int key_type_arg, data_t *key_data,
3619 int alg_arg, data_t *input_data )
3620{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003621 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02003622 psa_key_type_t key_type = key_type_arg;
3623 psa_algorithm_t alg = alg_arg;
3624 size_t key_bits;
3625 unsigned char *signature = NULL;
3626 size_t signature_size;
3627 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003628 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003629
Gilles Peskine8817f612018-12-18 00:18:46 +01003630 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003631
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003632 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY );
3633 psa_set_key_algorithm( &attributes, alg );
3634 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003635
Gilles Peskine049c7532019-05-15 20:22:09 +02003636 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3637 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003638 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3639 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003640
3641 /* Allocate a buffer which has the size advertized by the
3642 * library. */
3643 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type,
3644 key_bits, alg );
3645 TEST_ASSERT( signature_size != 0 );
3646 TEST_ASSERT( signature_size <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003647 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003648
3649 /* Perform the signature. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003650 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
3651 input_data->x, input_data->len,
3652 signature, signature_size,
3653 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003654 /* Check that the signature length looks sensible. */
3655 TEST_ASSERT( signature_length <= signature_size );
3656 TEST_ASSERT( signature_length > 0 );
3657
3658 /* Use the library to verify that the signature is correct. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003659 PSA_ASSERT( psa_asymmetric_verify(
3660 handle, alg,
3661 input_data->x, input_data->len,
3662 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003663
3664 if( input_data->len != 0 )
3665 {
3666 /* Flip a bit in the input and verify that the signature is now
3667 * detected as invalid. Flip a bit at the beginning, not at the end,
3668 * because ECDSA may ignore the last few bits of the input. */
3669 input_data->x[0] ^= 1;
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003670 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
3671 input_data->x, input_data->len,
3672 signature, signature_length ),
3673 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003674 }
3675
3676exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003677 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003678 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02003679 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003680 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003681}
3682/* END_CASE */
3683
3684/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003685void asymmetric_verify( int key_type_arg, data_t *key_data,
3686 int alg_arg, data_t *hash_data,
3687 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003688{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003689 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03003690 psa_key_type_t key_type = key_type_arg;
3691 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003692 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003693
Gilles Peskine69c12672018-06-28 00:07:19 +02003694 TEST_ASSERT( signature_data->len <= PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE );
3695
Gilles Peskine8817f612018-12-18 00:18:46 +01003696 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003697
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003698 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3699 psa_set_key_algorithm( &attributes, alg );
3700 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003701
Gilles Peskine049c7532019-05-15 20:22:09 +02003702 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3703 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03003704
Gilles Peskine8817f612018-12-18 00:18:46 +01003705 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
3706 hash_data->x, hash_data->len,
3707 signature_data->x,
3708 signature_data->len ) );
itayzafrir5c753392018-05-08 11:18:38 +03003709exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003710 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003711 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003712 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003713}
3714/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003715
3716/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03003717void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
3718 int alg_arg, data_t *hash_data,
3719 data_t *signature_data,
3720 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003721{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003722 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003723 psa_key_type_t key_type = key_type_arg;
3724 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003725 psa_status_t actual_status;
3726 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003727 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003728
Gilles Peskine8817f612018-12-18 00:18:46 +01003729 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003730
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003731 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY );
3732 psa_set_key_algorithm( &attributes, alg );
3733 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003734
Gilles Peskine049c7532019-05-15 20:22:09 +02003735 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3736 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003737
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003738 actual_status = psa_asymmetric_verify( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003739 hash_data->x, hash_data->len,
Gilles Peskine2d277862018-06-18 15:41:12 +02003740 signature_data->x,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003741 signature_data->len );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003742
Gilles Peskinefe11b722018-12-18 00:24:04 +01003743 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003744
3745exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003746 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003747 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003748 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003749}
3750/* END_CASE */
3751
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003752/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003753void asymmetric_encrypt( int key_type_arg,
3754 data_t *key_data,
3755 int alg_arg,
3756 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003757 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003758 int expected_output_length_arg,
3759 int expected_status_arg )
3760{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003761 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02003762 psa_key_type_t key_type = key_type_arg;
3763 psa_algorithm_t alg = alg_arg;
3764 size_t expected_output_length = expected_output_length_arg;
3765 size_t key_bits;
3766 unsigned char *output = NULL;
3767 size_t output_size;
3768 size_t output_length = ~0;
3769 psa_status_t actual_status;
3770 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003771 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003772
Gilles Peskine8817f612018-12-18 00:18:46 +01003773 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003774
Gilles Peskine656896e2018-06-29 19:12:28 +02003775 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003776 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3777 psa_set_key_algorithm( &attributes, alg );
3778 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003779 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3780 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003781
3782 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003783 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3784 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02003785 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003786 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003787
3788 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003789 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003790 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003791 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003792 output, output_size,
3793 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003794 TEST_EQUAL( actual_status, expected_status );
3795 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003796
Gilles Peskine68428122018-06-30 18:42:41 +02003797 /* If the label is empty, the test framework puts a non-null pointer
3798 * in label->x. Test that a null pointer works as well. */
3799 if( label->len == 0 )
3800 {
3801 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003802 if( output_size != 0 )
3803 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003804 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003805 input_data->x, input_data->len,
3806 NULL, label->len,
3807 output, output_size,
3808 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003809 TEST_EQUAL( actual_status, expected_status );
3810 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003811 }
3812
Gilles Peskine656896e2018-06-29 19:12:28 +02003813exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003814 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003815 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02003816 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003817 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003818}
3819/* END_CASE */
3820
3821/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003822void asymmetric_encrypt_decrypt( int key_type_arg,
3823 data_t *key_data,
3824 int alg_arg,
3825 data_t *input_data,
3826 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003827{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003828 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003829 psa_key_type_t key_type = key_type_arg;
3830 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003831 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003832 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003833 size_t output_size;
3834 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003835 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003836 size_t output2_size;
3837 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003838 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003839
Gilles Peskine8817f612018-12-18 00:18:46 +01003840 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003841
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003842 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3843 psa_set_key_algorithm( &attributes, alg );
3844 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003845
Gilles Peskine049c7532019-05-15 20:22:09 +02003846 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3847 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003848
3849 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003850 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
3851 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003852 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003853 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003854 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003855 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003856
Gilles Peskineeebd7382018-06-08 18:11:54 +02003857 /* We test encryption by checking that encrypt-then-decrypt gives back
3858 * the original plaintext because of the non-optional random
3859 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003860 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
3861 input_data->x, input_data->len,
3862 label->x, label->len,
3863 output, output_size,
3864 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003865 /* We don't know what ciphertext length to expect, but check that
3866 * it looks sensible. */
3867 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003868
Gilles Peskine8817f612018-12-18 00:18:46 +01003869 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3870 output, output_length,
3871 label->x, label->len,
3872 output2, output2_size,
3873 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003874 ASSERT_COMPARE( input_data->x, input_data->len,
3875 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003876
3877exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003878 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003879 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003880 mbedtls_free( output );
3881 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003882 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003883}
3884/* END_CASE */
3885
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003886/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003887void asymmetric_decrypt( int key_type_arg,
3888 data_t *key_data,
3889 int alg_arg,
3890 data_t *input_data,
3891 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003892 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003893{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003894 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003895 psa_key_type_t key_type = key_type_arg;
3896 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003897 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003898 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003899 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003900 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003901
Jaeden Amero412654a2019-02-06 12:57:46 +00003902 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003903 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003904
Gilles Peskine8817f612018-12-18 00:18:46 +01003905 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003906
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003907 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3908 psa_set_key_algorithm( &attributes, alg );
3909 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003910
Gilles Peskine049c7532019-05-15 20:22:09 +02003911 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3912 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003913
Gilles Peskine8817f612018-12-18 00:18:46 +01003914 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3915 input_data->x, input_data->len,
3916 label->x, label->len,
3917 output,
3918 output_size,
3919 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003920 ASSERT_COMPARE( expected_data->x, expected_data->len,
3921 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003922
Gilles Peskine68428122018-06-30 18:42:41 +02003923 /* If the label is empty, the test framework puts a non-null pointer
3924 * in label->x. Test that a null pointer works as well. */
3925 if( label->len == 0 )
3926 {
3927 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003928 if( output_size != 0 )
3929 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01003930 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
3931 input_data->x, input_data->len,
3932 NULL, label->len,
3933 output,
3934 output_size,
3935 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003936 ASSERT_COMPARE( expected_data->x, expected_data->len,
3937 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003938 }
3939
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003940exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003941 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003942 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003943 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003944 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003945}
3946/* END_CASE */
3947
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003948/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003949void asymmetric_decrypt_fail( int key_type_arg,
3950 data_t *key_data,
3951 int alg_arg,
3952 data_t *input_data,
3953 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003954 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003955 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003956{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003957 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003958 psa_key_type_t key_type = key_type_arg;
3959 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003960 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003961 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003962 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003963 psa_status_t actual_status;
3964 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003965 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003966
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003967 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003968
Gilles Peskine8817f612018-12-18 00:18:46 +01003969 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003970
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003971 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3972 psa_set_key_algorithm( &attributes, alg );
3973 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003974
Gilles Peskine049c7532019-05-15 20:22:09 +02003975 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3976 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003977
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003978 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003979 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003980 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003981 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003982 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003983 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003984 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003985
Gilles Peskine68428122018-06-30 18:42:41 +02003986 /* If the label is empty, the test framework puts a non-null pointer
3987 * in label->x. Test that a null pointer works as well. */
3988 if( label->len == 0 )
3989 {
3990 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003991 if( output_size != 0 )
3992 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003993 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003994 input_data->x, input_data->len,
3995 NULL, label->len,
3996 output, output_size,
3997 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003998 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003999 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004000 }
4001
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004002exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004003 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004004 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004005 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004006 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004007}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004008/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004009
4010/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004011void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004012{
4013 /* Test each valid way of initializing the object, except for `= {0}`, as
4014 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4015 * though it's OK by the C standard. We could test for this, but we'd need
4016 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004017 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004018 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4019 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4020 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004021
4022 memset( &zero, 0, sizeof( zero ) );
4023
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004024 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004025 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004026 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004027 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004028 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004029 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004030 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004031
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004032 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004033 PSA_ASSERT( psa_key_derivation_abort(&func) );
4034 PSA_ASSERT( psa_key_derivation_abort(&init) );
4035 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004036}
4037/* END_CASE */
4038
Janos Follath16de4a42019-06-13 16:32:24 +01004039/* BEGIN_CASE */
4040void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004041{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004042 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004043 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004044 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_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
Janos Follath16de4a42019-06-13 16:32:24 +01004048 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004049 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004050
4051exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004052 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004053 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004054}
4055/* END_CASE */
4056
Janos Follathaf3c2a02019-06-12 12:34:34 +01004057/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004058void derive_set_capacity( int alg_arg, int capacity_arg,
4059 int expected_status_arg )
4060{
4061 psa_algorithm_t alg = alg_arg;
4062 size_t capacity = capacity_arg;
4063 psa_status_t expected_status = expected_status_arg;
4064 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4065
4066 PSA_ASSERT( psa_crypto_init( ) );
4067
4068 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4069
4070 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4071 expected_status );
4072
4073exit:
4074 psa_key_derivation_abort( &operation );
4075 PSA_DONE( );
4076}
4077/* END_CASE */
4078
4079/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004080void derive_input( int alg_arg,
4081 int key_type_arg,
4082 int step1_arg, data_t *input1,
4083 int step2_arg, data_t *input2,
4084 int step3_arg, data_t *input3,
4085 int expected_status_arg1,
4086 int expected_status_arg2,
4087 int expected_status_arg3 )
4088{
4089 psa_algorithm_t alg = alg_arg;
4090 size_t key_type = key_type_arg;
4091 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4092 psa_status_t expected_statuses[] = {expected_status_arg1,
4093 expected_status_arg2,
4094 expected_status_arg3};
4095 data_t *inputs[] = {input1, input2, input3};
4096 psa_key_handle_t handles[] = {0, 0, 0};
4097 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4098 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4099 size_t i;
4100
4101 PSA_ASSERT( psa_crypto_init( ) );
4102
4103 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4104 psa_set_key_algorithm( &attributes, alg );
4105 psa_set_key_type( &attributes, key_type );
4106
4107 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4108
4109 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4110 {
4111 switch( steps[i] )
4112 {
4113 case PSA_KEY_DERIVATION_INPUT_SECRET:
4114 PSA_ASSERT( psa_import_key( &attributes,
4115 inputs[i]->x, inputs[i]->len,
4116 &handles[i] ) );
4117 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4118 handles[i] ),
4119 expected_statuses[i] );
4120 break;
4121 default:
4122 TEST_EQUAL( psa_key_derivation_input_bytes(
4123 &operation, steps[i],
4124 inputs[i]->x, inputs[i]->len ),
4125 expected_statuses[i] );
4126 break;
4127 }
4128 }
4129
4130exit:
4131 psa_key_derivation_abort( &operation );
4132 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4133 psa_destroy_key( handles[i] );
4134 PSA_DONE( );
4135}
4136/* END_CASE */
4137
Janos Follath71a4c912019-06-11 09:14:47 +01004138/* BEGIN_CASE depends_on:PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004139void test_derive_invalid_key_derivation_state( )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004140{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004141 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004142 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004143 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004144 psa_algorithm_t alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004145 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004146 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004147 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4148 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4149 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004150 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004151
Gilles Peskine8817f612018-12-18 00:18:46 +01004152 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004153
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004154 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4155 psa_set_key_algorithm( &attributes, alg );
4156 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004157
Gilles Peskine73676cb2019-05-15 20:15:10 +02004158 PSA_ASSERT( psa_import_key( &attributes,
4159 key_data, sizeof( key_data ),
4160 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004161
4162 /* valid key derivation */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004163 PSA_ASSERT( psa_key_derivation( &operation, handle, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004164 NULL, 0,
4165 NULL, 0,
4166 capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004167
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004168 /* state of operation shouldn't allow additional generation */
4169 TEST_EQUAL( psa_key_derivation( &operation, handle, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01004170 NULL, 0,
4171 NULL, 0,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004172 capacity ),
4173 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004174
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004175 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004176
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004177 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004178 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004179
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004180exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004181 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004182 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004183 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004184}
4185/* END_CASE */
4186
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004187/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004188void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004189{
4190 uint8_t output_buffer[16];
4191 size_t buffer_size = 16;
4192 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004193 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004194
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004195 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4196 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004197 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004198
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004199 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004200 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004201
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004202 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004203
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004204 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4205 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004206 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004207
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004208 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004209 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004210
4211exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004212 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004213}
4214/* END_CASE */
4215
4216/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004217void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004218 int step1_arg, data_t *input1,
4219 int step2_arg, data_t *input2,
4220 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004221 int requested_capacity_arg,
4222 data_t *expected_output1,
4223 data_t *expected_output2 )
4224{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004225 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004226 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4227 data_t *inputs[] = {input1, input2, input3};
4228 psa_key_handle_t handles[] = {0, 0, 0};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004229 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004230 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004231 uint8_t *expected_outputs[2] =
4232 {expected_output1->x, expected_output2->x};
4233 size_t output_sizes[2] =
4234 {expected_output1->len, expected_output2->len};
4235 size_t output_buffer_size = 0;
4236 uint8_t *output_buffer = NULL;
4237 size_t expected_capacity;
4238 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004240 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004241 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004242
4243 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4244 {
4245 if( output_sizes[i] > output_buffer_size )
4246 output_buffer_size = output_sizes[i];
4247 if( output_sizes[i] == 0 )
4248 expected_outputs[i] = NULL;
4249 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004250 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004251 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004252
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004253 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4254 psa_set_key_algorithm( &attributes, alg );
4255 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004256
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004257 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004258 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4259 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4260 requested_capacity ) );
4261 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004262 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004263 switch( steps[i] )
4264 {
4265 case 0:
4266 break;
4267 case PSA_KEY_DERIVATION_INPUT_SECRET:
4268 PSA_ASSERT( psa_import_key( &attributes,
4269 inputs[i]->x, inputs[i]->len,
4270 &handles[i] ) );
4271 PSA_ASSERT( psa_key_derivation_input_key(
4272 &operation, steps[i],
4273 handles[i] ) );
4274 break;
4275 default:
4276 PSA_ASSERT( psa_key_derivation_input_bytes(
4277 &operation, steps[i],
4278 inputs[i]->x, inputs[i]->len ) );
4279 break;
4280 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004281 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004282
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004283 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004284 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004285 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004286 expected_capacity = requested_capacity;
4287
4288 /* Expansion phase. */
4289 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4290 {
4291 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004292 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004293 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004294 if( expected_capacity == 0 && output_sizes[i] == 0 )
4295 {
4296 /* Reading 0 bytes when 0 bytes are available can go either way. */
4297 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004298 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004299 continue;
4300 }
4301 else if( expected_capacity == 0 ||
4302 output_sizes[i] > expected_capacity )
4303 {
4304 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004305 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004306 expected_capacity = 0;
4307 continue;
4308 }
4309 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004310 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004311 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004312 ASSERT_COMPARE( output_buffer, output_sizes[i],
4313 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004314 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004315 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004316 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004317 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004318 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004319 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004320 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004321
4322exit:
4323 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004324 psa_key_derivation_abort( &operation );
Gilles Peskine1468da72019-05-29 17:35:49 +02004325 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4326 psa_destroy_key( handles[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004327 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004328}
4329/* END_CASE */
4330
4331/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004332void derive_full( int alg_arg,
4333 data_t *key_data,
4334 data_t *salt,
4335 data_t *label,
4336 int requested_capacity_arg )
4337{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004338 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004339 psa_algorithm_t alg = alg_arg;
4340 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004341 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004342 unsigned char output_buffer[16];
4343 size_t expected_capacity = requested_capacity;
4344 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004345 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004346
Gilles Peskine8817f612018-12-18 00:18:46 +01004347 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004348
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004349 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4350 psa_set_key_algorithm( &attributes, alg );
4351 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004352
Gilles Peskine049c7532019-05-15 20:22:09 +02004353 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4354 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004355
4356 /* Extraction phase. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004357 if( PSA_ALG_IS_HKDF( alg ) )
4358 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004359 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4360 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004361 requested_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004362 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004363 PSA_KEY_DERIVATION_INPUT_SALT,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004364 salt->x, salt->len ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004365 PSA_ASSERT( psa_key_derivation_input_key( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004366 PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004367 handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004368 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004369 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004370 label->x, label->len ) );
4371 }
Janos Follath71a4c912019-06-11 09:14:47 +01004372
4373#if defined(PSA_PRE_1_0_KEY_DERIVATION)
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004374 else
4375 {
4376 // legacy
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004377 PSA_ASSERT( psa_key_derivation( &operation, handle, alg,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004378 salt->x, salt->len,
4379 label->x, label->len,
4380 requested_capacity ) );
4381 }
Janos Follath71a4c912019-06-11 09:14:47 +01004382#endif
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004383 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004384 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004385 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004386
4387 /* Expansion phase. */
4388 while( current_capacity > 0 )
4389 {
4390 size_t read_size = sizeof( output_buffer );
4391 if( read_size > current_capacity )
4392 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004393 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004394 output_buffer,
4395 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004396 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004397 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004398 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004399 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004400 }
4401
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004402 /* Check that the operation refuses to go over capacity. */
4403 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004404 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004405
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004406 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004407
4408exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004409 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004410 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004411 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004412}
4413/* END_CASE */
4414
Janos Follath71a4c912019-06-11 09:14:47 +01004415/* BEGIN_CASE depends_on:PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004416void derive_key_exercise( int alg_arg,
4417 data_t *key_data,
4418 data_t *salt,
4419 data_t *label,
4420 int derived_type_arg,
4421 int derived_bits_arg,
4422 int derived_usage_arg,
4423 int derived_alg_arg )
4424{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004425 psa_key_handle_t base_handle = 0;
4426 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004427 psa_algorithm_t alg = alg_arg;
4428 psa_key_type_t derived_type = derived_type_arg;
4429 size_t derived_bits = derived_bits_arg;
4430 psa_key_usage_t derived_usage = derived_usage_arg;
4431 psa_algorithm_t derived_alg = derived_alg_arg;
4432 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004433 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004434 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004435 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004436
Gilles Peskine8817f612018-12-18 00:18:46 +01004437 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004438
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004439 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4440 psa_set_key_algorithm( &attributes, alg );
4441 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004442 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4443 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004444
4445 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004446 PSA_ASSERT( psa_key_derivation( &operation, base_handle, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004447 salt->x, salt->len,
4448 label->x, label->len,
4449 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004450 psa_set_key_usage_flags( &attributes, derived_usage );
4451 psa_set_key_algorithm( &attributes, derived_alg );
4452 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004453 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004454 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004455 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004456
4457 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004458 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
4459 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4460 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004461
4462 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004463 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004464 goto exit;
4465
4466exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004467 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004468 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004469 psa_destroy_key( base_handle );
4470 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004471 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004472}
4473/* END_CASE */
4474
Janos Follath71a4c912019-06-11 09:14:47 +01004475/* BEGIN_CASE depends_on:PSA_PRE_1_0_KEY_DERIVATION */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004476void derive_key_export( int alg_arg,
4477 data_t *key_data,
4478 data_t *salt,
4479 data_t *label,
4480 int bytes1_arg,
4481 int bytes2_arg )
4482{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004483 psa_key_handle_t base_handle = 0;
4484 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004485 psa_algorithm_t alg = alg_arg;
4486 size_t bytes1 = bytes1_arg;
4487 size_t bytes2 = bytes2_arg;
4488 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004489 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004490 uint8_t *output_buffer = NULL;
4491 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004492 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4493 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004494 size_t length;
4495
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004496 ASSERT_ALLOC( output_buffer, capacity );
4497 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004498 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004499
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004500 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4501 psa_set_key_algorithm( &base_attributes, alg );
4502 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004503 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
4504 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004505
4506 /* Derive some material and output it. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004507 PSA_ASSERT( psa_key_derivation( &operation, base_handle, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004508 salt->x, salt->len,
4509 label->x, label->len,
4510 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004511 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004512 output_buffer,
4513 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004514 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004515
4516 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004517 PSA_ASSERT( psa_key_derivation( &operation, base_handle, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01004518 salt->x, salt->len,
4519 label->x, label->len,
4520 capacity ) );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004521 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4522 psa_set_key_algorithm( &derived_attributes, 0 );
4523 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004524 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004525 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004526 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004527 PSA_ASSERT( psa_export_key( derived_handle,
4528 export_buffer, bytes1,
4529 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004530 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01004531 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004532 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004533 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004534 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01004535 PSA_ASSERT( psa_export_key( derived_handle,
4536 export_buffer + bytes1, bytes2,
4537 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004538 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004539
4540 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004541 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4542 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004543
4544exit:
4545 mbedtls_free( output_buffer );
4546 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004547 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004548 psa_destroy_key( base_handle );
4549 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004550 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004551}
4552/* END_CASE */
4553
4554/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004555void key_agreement_setup( int alg_arg,
4556 int our_key_type_arg, data_t *our_key_data,
4557 data_t *peer_key_data,
4558 int expected_status_arg )
4559{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004560 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004561 psa_algorithm_t alg = alg_arg;
4562 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004563 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004564 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004565 psa_status_t expected_status = expected_status_arg;
4566 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004567
Gilles Peskine8817f612018-12-18 00:18:46 +01004568 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004569
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004570 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4571 psa_set_key_algorithm( &attributes, alg );
4572 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004573 PSA_ASSERT( psa_import_key( &attributes,
4574 our_key_data->x, our_key_data->len,
4575 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004576
Gilles Peskine77f40d82019-04-11 21:27:06 +02004577 /* The tests currently include inputs that should fail at either step.
4578 * Test cases that fail at the setup step should be changed to call
4579 * key_derivation_setup instead, and this function should be renamed
4580 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004581 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004582 if( status == PSA_SUCCESS )
4583 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004584 TEST_EQUAL( psa_key_derivation_key_agreement(
4585 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4586 our_key,
4587 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004588 expected_status );
4589 }
4590 else
4591 {
4592 TEST_ASSERT( status == expected_status );
4593 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004594
4595exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004596 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004597 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004598 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004599}
4600/* END_CASE */
4601
4602/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004603void raw_key_agreement( int alg_arg,
4604 int our_key_type_arg, data_t *our_key_data,
4605 data_t *peer_key_data,
4606 data_t *expected_output )
4607{
4608 psa_key_handle_t our_key = 0;
4609 psa_algorithm_t alg = alg_arg;
4610 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004611 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004612 unsigned char *output = NULL;
4613 size_t output_length = ~0;
4614
4615 ASSERT_ALLOC( output, expected_output->len );
4616 PSA_ASSERT( psa_crypto_init( ) );
4617
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004618 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4619 psa_set_key_algorithm( &attributes, alg );
4620 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004621 PSA_ASSERT( psa_import_key( &attributes,
4622 our_key_data->x, our_key_data->len,
4623 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004624
Gilles Peskinebe697d82019-05-16 18:00:41 +02004625 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4626 peer_key_data->x, peer_key_data->len,
4627 output, expected_output->len,
4628 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004629 ASSERT_COMPARE( output, output_length,
4630 expected_output->x, expected_output->len );
4631
4632exit:
4633 mbedtls_free( output );
4634 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004635 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004636}
4637/* END_CASE */
4638
4639/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004640void key_agreement_capacity( int alg_arg,
4641 int our_key_type_arg, data_t *our_key_data,
4642 data_t *peer_key_data,
4643 int expected_capacity_arg )
4644{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004645 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004646 psa_algorithm_t alg = alg_arg;
4647 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004648 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004649 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004650 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004651 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004652
Gilles Peskine8817f612018-12-18 00:18:46 +01004653 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004654
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004655 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4656 psa_set_key_algorithm( &attributes, alg );
4657 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004658 PSA_ASSERT( psa_import_key( &attributes,
4659 our_key_data->x, our_key_data->len,
4660 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004661
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004662 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004663 PSA_ASSERT( psa_key_derivation_key_agreement(
4664 &operation,
4665 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4666 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004667 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4668 {
4669 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004670 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004671 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004672 NULL, 0 ) );
4673 }
Gilles Peskine59685592018-09-18 12:11:34 +02004674
Gilles Peskinebf491972018-10-25 22:36:12 +02004675 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004676 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004677 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004678 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004679
Gilles Peskinebf491972018-10-25 22:36:12 +02004680 /* Test the actual capacity by reading the output. */
4681 while( actual_capacity > sizeof( output ) )
4682 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004683 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004684 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004685 actual_capacity -= sizeof( output );
4686 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004687 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004688 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004689 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004690 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004691
Gilles Peskine59685592018-09-18 12:11:34 +02004692exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004693 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004694 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004695 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004696}
4697/* END_CASE */
4698
4699/* BEGIN_CASE */
4700void key_agreement_output( int alg_arg,
4701 int our_key_type_arg, data_t *our_key_data,
4702 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004703 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004704{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004705 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02004706 psa_algorithm_t alg = alg_arg;
4707 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004708 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004709 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004710 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004711
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004712 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4713 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004714
Gilles Peskine8817f612018-12-18 00:18:46 +01004715 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004716
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004717 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4718 psa_set_key_algorithm( &attributes, alg );
4719 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004720 PSA_ASSERT( psa_import_key( &attributes,
4721 our_key_data->x, our_key_data->len,
4722 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004723
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004724 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004725 PSA_ASSERT( psa_key_derivation_key_agreement(
4726 &operation,
4727 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4728 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004729 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4730 {
4731 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004732 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004733 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004734 NULL, 0 ) );
4735 }
Gilles Peskine59685592018-09-18 12:11:34 +02004736
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004737 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004738 actual_output,
4739 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004740 ASSERT_COMPARE( actual_output, expected_output1->len,
4741 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004742 if( expected_output2->len != 0 )
4743 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004744 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004745 actual_output,
4746 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004747 ASSERT_COMPARE( actual_output, expected_output2->len,
4748 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004749 }
Gilles Peskine59685592018-09-18 12:11:34 +02004750
4751exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004752 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004753 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004754 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004755 mbedtls_free( actual_output );
4756}
4757/* END_CASE */
4758
4759/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004760void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004761{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004762 size_t bytes = bytes_arg;
4763 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004764 unsigned char *output = NULL;
4765 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004766 size_t i;
4767 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004768
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004769 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
4770 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004771 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004772
Gilles Peskine8817f612018-12-18 00:18:46 +01004773 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004774
Gilles Peskinea50d7392018-06-21 10:22:13 +02004775 /* Run several times, to ensure that every output byte will be
4776 * nonzero at least once with overwhelming probability
4777 * (2^(-8*number_of_runs)). */
4778 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004779 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004780 if( bytes != 0 )
4781 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004782 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004783
4784 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004785 ASSERT_COMPARE( output + bytes, sizeof( trail ),
4786 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004787
4788 for( i = 0; i < bytes; i++ )
4789 {
4790 if( output[i] != 0 )
4791 ++changed[i];
4792 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004793 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004794
4795 /* Check that every byte was changed to nonzero at least once. This
4796 * validates that psa_generate_random is overwriting every byte of
4797 * the output buffer. */
4798 for( i = 0; i < bytes; i++ )
4799 {
4800 TEST_ASSERT( changed[i] != 0 );
4801 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004802
4803exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004804 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004805 mbedtls_free( output );
4806 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004807}
4808/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004809
4810/* BEGIN_CASE */
4811void generate_key( int type_arg,
4812 int bits_arg,
4813 int usage_arg,
4814 int alg_arg,
4815 int expected_status_arg )
4816{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004817 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004818 psa_key_type_t type = type_arg;
4819 psa_key_usage_t usage = usage_arg;
4820 size_t bits = bits_arg;
4821 psa_algorithm_t alg = alg_arg;
4822 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004823 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004824 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004825
Gilles Peskine8817f612018-12-18 00:18:46 +01004826 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004827
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004828 psa_set_key_usage_flags( &attributes, usage );
4829 psa_set_key_algorithm( &attributes, alg );
4830 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004831 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004832
4833 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004834 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004835 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004836 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004837
4838 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004839 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
4840 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4841 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004842
Gilles Peskine818ca122018-06-20 18:16:48 +02004843 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004844 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004845 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004846
4847exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004848 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004849 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004850 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004851}
4852/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004853
Gilles Peskinee56e8782019-04-26 17:34:02 +02004854/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
4855void generate_key_rsa( int bits_arg,
4856 data_t *e_arg,
4857 int expected_status_arg )
4858{
4859 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004860 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02004861 size_t bits = bits_arg;
4862 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4863 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4864 psa_status_t expected_status = expected_status_arg;
4865 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4866 uint8_t *exported = NULL;
4867 size_t exported_size =
4868 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
4869 size_t exported_length = SIZE_MAX;
4870 uint8_t *e_read_buffer = NULL;
4871 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004872 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004873 size_t e_read_length = SIZE_MAX;
4874
4875 if( e_arg->len == 0 ||
4876 ( e_arg->len == 3 &&
4877 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4878 {
4879 is_default_public_exponent = 1;
4880 e_read_size = 0;
4881 }
4882 ASSERT_ALLOC( e_read_buffer, e_read_size );
4883 ASSERT_ALLOC( exported, exported_size );
4884
4885 PSA_ASSERT( psa_crypto_init( ) );
4886
4887 psa_set_key_usage_flags( &attributes, usage );
4888 psa_set_key_algorithm( &attributes, alg );
4889 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
4890 e_arg->x, e_arg->len ) );
4891 psa_set_key_bits( &attributes, bits );
4892
4893 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02004894 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004895 if( expected_status != PSA_SUCCESS )
4896 goto exit;
4897
4898 /* Test the key information */
4899 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4900 TEST_EQUAL( psa_get_key_type( &attributes ), type );
4901 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
4902 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
4903 e_read_buffer, e_read_size,
4904 &e_read_length ) );
4905 if( is_default_public_exponent )
4906 TEST_EQUAL( e_read_length, 0 );
4907 else
4908 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
4909
4910 /* Do something with the key according to its type and permitted usage. */
4911 if( ! exercise_key( handle, usage, alg ) )
4912 goto exit;
4913
4914 /* Export the key and check the public exponent. */
4915 PSA_ASSERT( psa_export_public_key( handle,
4916 exported, exported_size,
4917 &exported_length ) );
4918 {
4919 uint8_t *p = exported;
4920 uint8_t *end = exported + exported_length;
4921 size_t len;
4922 /* RSAPublicKey ::= SEQUENCE {
4923 * modulus INTEGER, -- n
4924 * publicExponent INTEGER } -- e
4925 */
4926 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004927 MBEDTLS_ASN1_SEQUENCE |
4928 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004929 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
4930 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
4931 MBEDTLS_ASN1_INTEGER ) );
4932 if( len >= 1 && p[0] == 0 )
4933 {
4934 ++p;
4935 --len;
4936 }
4937 if( e_arg->len == 0 )
4938 {
4939 TEST_EQUAL( len, 3 );
4940 TEST_EQUAL( p[0], 1 );
4941 TEST_EQUAL( p[1], 0 );
4942 TEST_EQUAL( p[2], 1 );
4943 }
4944 else
4945 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
4946 }
4947
4948exit:
4949 psa_reset_key_attributes( &attributes );
4950 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004951 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004952 mbedtls_free( e_read_buffer );
4953 mbedtls_free( exported );
4954}
4955/* END_CASE */
4956
Darryl Greend49a4992018-06-18 17:27:26 +01004957/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004958void persistent_key_load_key_from_storage( data_t *data,
4959 int type_arg, int bits_arg,
4960 int usage_flags_arg, int alg_arg,
4961 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01004962{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004963 psa_key_id_t key_id = 1;
4964 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004965 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004966 psa_key_handle_t base_key = 0;
4967 psa_key_type_t type = type_arg;
4968 size_t bits = bits_arg;
4969 psa_key_usage_t usage_flags = usage_flags_arg;
4970 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01004972 unsigned char *first_export = NULL;
4973 unsigned char *second_export = NULL;
4974 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
4975 size_t first_exported_length;
4976 size_t second_exported_length;
4977
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004978 if( usage_flags & PSA_KEY_USAGE_EXPORT )
4979 {
4980 ASSERT_ALLOC( first_export, export_size );
4981 ASSERT_ALLOC( second_export, export_size );
4982 }
Darryl Greend49a4992018-06-18 17:27:26 +01004983
Gilles Peskine8817f612018-12-18 00:18:46 +01004984 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01004985
Gilles Peskinec87af662019-05-15 16:12:22 +02004986 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02004987 psa_set_key_usage_flags( &attributes, usage_flags );
4988 psa_set_key_algorithm( &attributes, alg );
4989 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004990 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01004991
Darryl Green0c6575a2018-11-07 16:05:30 +00004992 switch( generation_method )
4993 {
4994 case IMPORT_KEY:
4995 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02004996 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
4997 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004998 break;
Darryl Greend49a4992018-06-18 17:27:26 +01004999
Darryl Green0c6575a2018-11-07 16:05:30 +00005000 case GENERATE_KEY:
5001 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005002 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005003 break;
5004
5005 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005006 {
5007 /* Create base key */
5008 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5009 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5010 psa_set_key_usage_flags( &base_attributes,
5011 PSA_KEY_USAGE_DERIVE );
5012 psa_set_key_algorithm( &base_attributes, derive_alg );
5013 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005014 PSA_ASSERT( psa_import_key( &base_attributes,
5015 data->x, data->len,
5016 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005017 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005018 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005019 PSA_ASSERT( psa_key_derivation_input_key(
5020 &operation,
5021 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005022 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005023 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005024 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005025 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5026 &operation,
5027 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005028 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005029 PSA_ASSERT( psa_destroy_key( base_key ) );
5030 base_key = 0;
5031 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005032 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005033 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005034 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005035
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005036 /* Export the key if permitted by the key policy. */
5037 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5038 {
5039 PSA_ASSERT( psa_export_key( handle,
5040 first_export, export_size,
5041 &first_exported_length ) );
5042 if( generation_method == IMPORT_KEY )
5043 ASSERT_COMPARE( data->x, data->len,
5044 first_export, first_exported_length );
5045 }
Darryl Greend49a4992018-06-18 17:27:26 +01005046
5047 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005048 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005049 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005050 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005051
Darryl Greend49a4992018-06-18 17:27:26 +01005052 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005053 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005054 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5055 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
5056 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5057 PSA_KEY_LIFETIME_PERSISTENT );
5058 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5059 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5060 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5061 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005062
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005063 /* Export the key again if permitted by the key policy. */
5064 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005065 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005066 PSA_ASSERT( psa_export_key( handle,
5067 second_export, export_size,
5068 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005069 ASSERT_COMPARE( first_export, first_exported_length,
5070 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005071 }
5072
5073 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005074 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005075 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005076
5077exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005078 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005079 mbedtls_free( first_export );
5080 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005081 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005082 psa_destroy_key( base_key );
5083 if( handle == 0 )
5084 {
5085 /* In case there was a test failure after creating the persistent key
5086 * but while it was not open, try to re-open the persistent key
5087 * to delete it. */
Gilles Peskine225010f2019-05-06 18:44:55 +02005088 psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005089 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005090 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005091 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005092}
5093/* END_CASE */