blob: 665580bfe5c8c6d514eca62c8672c78cc651a4e8 [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 Peskinebdc96fd2019-08-07 12:08:04 +02008/* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random()
9 * uses mbedtls_ctr_drbg internally. */
10#include "mbedtls/ctr_drbg.h"
11
Ronald Cron02c78b72020-05-27 09:22:32 +020012#include "test/psa_crypto_helpers.h"
itayzafrir3e02b3b2018-06-12 17:06:52 +030013
Gilles Peskinec744d992019-07-30 17:26:54 +020014/* Tests that require more than 128kB of RAM plus change have this symbol
15 * as a dependency. Currently we always define this symbol, so the tests
16 * are always executed. In the future we should make this conditional
17 * so that tests that require a lot of memory are skipped on constrained
18 * platforms. */
Gilles Peskine49232e82019-08-07 11:01:30 +020019#define HAVE_RAM_AVAILABLE_128K
Gilles Peskinec744d992019-07-30 17:26:54 +020020
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020021#include "psa/crypto.h"
22
Jaeden Amerof24c7f82018-06-27 17:20:43 +010023/** An invalid export length that will never be set by psa_export_key(). */
24static const size_t INVALID_EXPORT_LENGTH = ~0U;
25
Gilles Peskinef426e0f2019-02-25 17:42:03 +010026/* A hash algorithm that is known to be supported.
27 *
28 * This is used in some smoke tests.
29 */
30#if defined(MBEDTLS_MD2_C)
31#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD2
32#elif defined(MBEDTLS_MD4_C)
33#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD4
34#elif defined(MBEDTLS_MD5_C)
35#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_MD5
36/* MBEDTLS_RIPEMD160_C omitted. This is necessary for the sake of
37 * exercise_signature_key() because Mbed TLS doesn't support RIPEMD160
38 * in RSA PKCS#1v1.5 signatures. A RIPEMD160-only configuration would be
39 * implausible anyway. */
40#elif defined(MBEDTLS_SHA1_C)
41#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_1
42#elif defined(MBEDTLS_SHA256_C)
43#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_256
44#elif defined(MBEDTLS_SHA512_C)
45#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA_384
46#elif defined(MBEDTLS_SHA3_C)
47#define KNOWN_SUPPORTED_HASH_ALG PSA_ALG_SHA3_256
48#else
49#undef KNOWN_SUPPORTED_HASH_ALG
50#endif
51
52/* A block cipher that is known to be supported.
53 *
54 * For simplicity's sake, stick to block ciphers with 16-byte blocks.
55 */
56#if defined(MBEDTLS_AES_C)
57#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_AES
58#elif defined(MBEDTLS_ARIA_C)
59#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_ARIA
60#elif defined(MBEDTLS_CAMELLIA_C)
61#define KNOWN_SUPPORTED_BLOCK_CIPHER PSA_KEY_TYPE_CAMELLIA
62#undef KNOWN_SUPPORTED_BLOCK_CIPHER
63#endif
64
65/* A MAC mode that is known to be supported.
66 *
67 * It must either be HMAC with #KNOWN_SUPPORTED_HASH_ALG or
68 * a block cipher-based MAC with #KNOWN_SUPPORTED_BLOCK_CIPHER.
69 *
70 * This is used in some smoke tests.
71 */
72#if defined(KNOWN_SUPPORTED_HASH_ALG)
73#define KNOWN_SUPPORTED_MAC_ALG ( PSA_ALG_HMAC( KNOWN_SUPPORTED_HASH_ALG ) )
74#define KNOWN_SUPPORTED_MAC_KEY_TYPE PSA_KEY_TYPE_HMAC
75#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CMAC_C)
76#define KNOWN_SUPPORTED_MAC_ALG PSA_ALG_CMAC
77#define KNOWN_SUPPORTED_MAC_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
78#else
79#undef KNOWN_SUPPORTED_MAC_ALG
80#undef KNOWN_SUPPORTED_MAC_KEY_TYPE
81#endif
82
83/* A cipher algorithm and key type that are known to be supported.
84 *
85 * This is used in some smoke tests.
86 */
87#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CTR)
88#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CTR
89#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CBC)
90#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CBC_NO_PADDING
91#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_CFB)
92#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_CFB
93#elif defined(KNOWN_SUPPORTED_BLOCK_CIPHER) && defined(MBEDTLS_CIPHER_MODE_OFB)
94#define KNOWN_SUPPORTED_BLOCK_CIPHER_ALG PSA_ALG_OFB
95#else
96#undef KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
97#endif
98#if defined(KNOWN_SUPPORTED_BLOCK_CIPHER_ALG)
99#define KNOWN_SUPPORTED_CIPHER_ALG KNOWN_SUPPORTED_BLOCK_CIPHER_ALG
100#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE KNOWN_SUPPORTED_BLOCK_CIPHER
101#elif defined(MBEDTLS_RC4_C)
102#define KNOWN_SUPPORTED_CIPHER_ALG PSA_ALG_RC4
103#define KNOWN_SUPPORTED_CIPHER_KEY_TYPE PSA_KEY_TYPE_RC4
104#else
105#undef KNOWN_SUPPORTED_CIPHER_ALG
106#undef KNOWN_SUPPORTED_CIPHER_KEY_TYPE
107#endif
108
Gilles Peskine667c1112019-12-03 19:03:20 +0100109#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
110int lifetime_is_secure_element( psa_key_lifetime_t lifetime )
111{
112 /* At the moment, anything that isn't a built-in lifetime is either
113 * a secure element or unassigned. */
114 return( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
115 lifetime != PSA_KEY_LIFETIME_PERSISTENT );
116}
117#else
118int lifetime_is_secure_element( psa_key_lifetime_t lifetime )
119{
120 (void) lifetime;
121 return( 0 );
122}
123#endif
124
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200125/** Test if a buffer contains a constant byte value.
126 *
127 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200128 *
129 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200130 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200131 * \param size Size of the buffer in bytes.
132 *
Gilles Peskine3f669c32018-06-21 09:21:51 +0200133 * \return 1 if the buffer is all-bits-zero.
134 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200135 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200136static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200137{
138 size_t i;
139 for( i = 0; i < size; i++ )
140 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200141 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +0200142 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200143 }
Gilles Peskine3f669c32018-06-21 09:21:51 +0200144 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200145}
Gilles Peskine818ca122018-06-20 18:16:48 +0200146
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200147/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
148static int asn1_write_10x( unsigned char **p,
149 unsigned char *start,
150 size_t bits,
151 unsigned char x )
152{
153 int ret;
154 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +0200155 if( bits == 0 )
156 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
157 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200158 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +0300159 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200160 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
161 *p -= len;
162 ( *p )[len-1] = x;
163 if( bits % 8 == 0 )
164 ( *p )[1] |= 1;
165 else
166 ( *p )[0] |= 1 << ( bits % 8 );
167 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
168 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
169 MBEDTLS_ASN1_INTEGER ) );
170 return( len );
171}
172
173static int construct_fake_rsa_key( unsigned char *buffer,
174 size_t buffer_size,
175 unsigned char **p,
176 size_t bits,
177 int keypair )
178{
179 size_t half_bits = ( bits + 1 ) / 2;
180 int ret;
181 int len = 0;
182 /* Construct something that looks like a DER encoding of
183 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
184 * RSAPrivateKey ::= SEQUENCE {
185 * version Version,
186 * modulus INTEGER, -- n
187 * publicExponent INTEGER, -- e
188 * privateExponent INTEGER, -- d
189 * prime1 INTEGER, -- p
190 * prime2 INTEGER, -- q
191 * exponent1 INTEGER, -- d mod (p-1)
192 * exponent2 INTEGER, -- d mod (q-1)
193 * coefficient INTEGER, -- (inverse of q) mod p
194 * otherPrimeInfos OtherPrimeInfos OPTIONAL
195 * }
196 * Or, for a public key, the same structure with only
197 * version, modulus and publicExponent.
198 */
199 *p = buffer + buffer_size;
200 if( keypair )
201 {
202 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
203 asn1_write_10x( p, buffer, half_bits, 1 ) );
204 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
205 asn1_write_10x( p, buffer, half_bits, 1 ) );
206 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
207 asn1_write_10x( p, buffer, half_bits, 1 ) );
208 MBEDTLS_ASN1_CHK_ADD( len, /* q */
209 asn1_write_10x( p, buffer, half_bits, 1 ) );
210 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
211 asn1_write_10x( p, buffer, half_bits, 3 ) );
212 MBEDTLS_ASN1_CHK_ADD( len, /* d */
213 asn1_write_10x( p, buffer, bits, 1 ) );
214 }
215 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
216 asn1_write_10x( p, buffer, 17, 1 ) );
217 MBEDTLS_ASN1_CHK_ADD( len, /* n */
218 asn1_write_10x( p, buffer, bits, 1 ) );
219 if( keypair )
220 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
221 mbedtls_asn1_write_int( p, buffer, 0 ) );
222 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
223 {
224 const unsigned char tag =
225 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
226 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
227 }
228 return( len );
229}
230
Gilles Peskine667c1112019-12-03 19:03:20 +0100231int check_key_attributes_sanity( psa_key_handle_t key )
232{
233 int ok = 0;
234 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
235 psa_key_lifetime_t lifetime;
236 psa_key_id_t id;
237 psa_key_type_t type;
238 psa_key_type_t bits;
239
240 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
241 lifetime = psa_get_key_lifetime( &attributes );
242 id = psa_get_key_id( &attributes );
243 type = psa_get_key_type( &attributes );
244 bits = psa_get_key_bits( &attributes );
245
246 /* Persistence */
247 if( lifetime == PSA_KEY_LIFETIME_VOLATILE )
248 TEST_ASSERT( id == 0 );
249 else
250 {
251 TEST_ASSERT(
252 ( PSA_KEY_ID_USER_MIN <= id && id <= PSA_KEY_ID_USER_MAX ) ||
253 ( PSA_KEY_ID_USER_MIN <= id && id <= PSA_KEY_ID_USER_MAX ) );
254 }
255#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
256 /* randomly-generated 64-bit constant, should never appear in test data */
257 psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21;
258 psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number );
259 if( lifetime_is_secure_element( lifetime ) )
260 {
261 /* Mbed Crypto currently always exposes the slot number to
262 * applications. This is not mandated by the PSA specification
263 * and may change in future versions. */
264 TEST_EQUAL( status, 0 );
265 TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 );
266 }
267 else
268 {
269 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
270 }
271#endif
272
273 /* Type and size */
274 TEST_ASSERT( type != 0 );
275 TEST_ASSERT( bits != 0 );
276 TEST_ASSERT( bits <= PSA_MAX_KEY_BITS );
277 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
278 TEST_ASSERT( bits % 8 == 0 );
279
280 /* MAX macros concerning specific key types */
281 if( PSA_KEY_TYPE_IS_ECC( type ) )
282 TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS );
283 else if( PSA_KEY_TYPE_IS_RSA( type ) )
284 TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS );
285 TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) <= PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE );
286
287 ok = 1;
288
289exit:
290 psa_reset_key_attributes( &attributes );
291 return( ok );
292}
293
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100294int exercise_mac_setup( psa_key_type_t key_type,
295 const unsigned char *key_bytes,
296 size_t key_length,
297 psa_algorithm_t alg,
298 psa_mac_operation_t *operation,
299 psa_status_t *status )
300{
301 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200302 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100303
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100304 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200305 psa_set_key_algorithm( &attributes, alg );
306 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +0200307 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length,
308 &handle ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100309
310 *status = psa_mac_sign_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100311 /* Whether setup succeeded or failed, abort must succeed. */
312 PSA_ASSERT( psa_mac_abort( operation ) );
313 /* If setup failed, reproduce the failure, so that the caller can
314 * test the resulting state of the operation object. */
315 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100316 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100317 TEST_EQUAL( psa_mac_sign_setup( operation, handle, alg ),
318 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100319 }
320
321 psa_destroy_key( handle );
322 return( 1 );
323
324exit:
325 psa_destroy_key( handle );
326 return( 0 );
327}
328
329int exercise_cipher_setup( psa_key_type_t key_type,
330 const unsigned char *key_bytes,
331 size_t key_length,
332 psa_algorithm_t alg,
333 psa_cipher_operation_t *operation,
334 psa_status_t *status )
335{
336 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200337 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100338
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200339 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
340 psa_set_key_algorithm( &attributes, alg );
341 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +0200342 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length,
343 &handle ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100344
345 *status = psa_cipher_encrypt_setup( operation, handle, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100346 /* Whether setup succeeded or failed, abort must succeed. */
347 PSA_ASSERT( psa_cipher_abort( operation ) );
348 /* If setup failed, reproduce the failure, so that the caller can
349 * test the resulting state of the operation object. */
350 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100351 {
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100352 TEST_EQUAL( psa_cipher_encrypt_setup( operation, handle, alg ),
353 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100354 }
355
356 psa_destroy_key( handle );
357 return( 1 );
358
359exit:
360 psa_destroy_key( handle );
361 return( 0 );
362}
363
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100364static int exercise_mac_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200365 psa_key_usage_t usage,
366 psa_algorithm_t alg )
367{
Jaeden Amero769ce272019-01-04 11:48:03 +0000368 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200369 const unsigned char input[] = "foo";
Gilles Peskine69c12672018-06-28 00:07:19 +0200370 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200371 size_t mac_length = sizeof( mac );
372
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100373 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200374 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100375 PSA_ASSERT( psa_mac_sign_setup( &operation,
376 handle, alg ) );
377 PSA_ASSERT( psa_mac_update( &operation,
378 input, sizeof( input ) ) );
379 PSA_ASSERT( psa_mac_sign_finish( &operation,
380 mac, sizeof( mac ),
381 &mac_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200382 }
383
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100384 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200385 {
386 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100387 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200388 PSA_SUCCESS :
389 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine8817f612018-12-18 00:18:46 +0100390 PSA_ASSERT( psa_mac_verify_setup( &operation,
391 handle, alg ) );
392 PSA_ASSERT( psa_mac_update( &operation,
393 input, sizeof( input ) ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100394 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
395 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200396 }
397
398 return( 1 );
399
400exit:
401 psa_mac_abort( &operation );
402 return( 0 );
403}
404
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100405static int exercise_cipher_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200406 psa_key_usage_t usage,
407 psa_algorithm_t alg )
408{
Jaeden Amero5bae2272019-01-04 11:48:27 +0000409 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine818ca122018-06-20 18:16:48 +0200410 unsigned char iv[16] = {0};
411 size_t iv_length = sizeof( iv );
412 const unsigned char plaintext[16] = "Hello, world...";
413 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
414 size_t ciphertext_length = sizeof( ciphertext );
415 unsigned char decrypted[sizeof( ciphertext )];
416 size_t part_length;
417
418 if( usage & PSA_KEY_USAGE_ENCRYPT )
419 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100420 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
421 handle, alg ) );
422 PSA_ASSERT( psa_cipher_generate_iv( &operation,
423 iv, sizeof( iv ),
424 &iv_length ) );
425 PSA_ASSERT( psa_cipher_update( &operation,
426 plaintext, sizeof( plaintext ),
427 ciphertext, sizeof( ciphertext ),
428 &ciphertext_length ) );
429 PSA_ASSERT( psa_cipher_finish( &operation,
430 ciphertext + ciphertext_length,
431 sizeof( ciphertext ) - ciphertext_length,
432 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200433 ciphertext_length += part_length;
434 }
435
436 if( usage & PSA_KEY_USAGE_DECRYPT )
437 {
438 psa_status_t status;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200439 int maybe_invalid_padding = 0;
Gilles Peskine818ca122018-06-20 18:16:48 +0200440 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
441 {
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200442 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
443 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
444 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
445 * have this macro yet. */
446 iv_length = PSA_BLOCK_CIPHER_BLOCK_SIZE(
447 psa_get_key_type( &attributes ) );
448 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Gilles Peskine818ca122018-06-20 18:16:48 +0200449 }
Gilles Peskine8817f612018-12-18 00:18:46 +0100450 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
451 handle, alg ) );
452 PSA_ASSERT( psa_cipher_set_iv( &operation,
453 iv, iv_length ) );
454 PSA_ASSERT( psa_cipher_update( &operation,
455 ciphertext, ciphertext_length,
456 decrypted, sizeof( decrypted ),
457 &part_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200458 status = psa_cipher_finish( &operation,
459 decrypted + part_length,
460 sizeof( decrypted ) - part_length,
461 &part_length );
462 /* For a stream cipher, all inputs are valid. For a block cipher,
463 * if the input is some aribtrary data rather than an actual
464 ciphertext, a padding error is likely. */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200465 if( maybe_invalid_padding )
Gilles Peskine818ca122018-06-20 18:16:48 +0200466 TEST_ASSERT( status == PSA_SUCCESS ||
467 status == PSA_ERROR_INVALID_PADDING );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200468 else
469 PSA_ASSERT( status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200470 }
471
472 return( 1 );
473
474exit:
475 psa_cipher_abort( &operation );
476 return( 0 );
477}
478
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100479static int exercise_aead_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200480 psa_key_usage_t usage,
481 psa_algorithm_t alg )
482{
483 unsigned char nonce[16] = {0};
484 size_t nonce_length = sizeof( nonce );
485 unsigned char plaintext[16] = "Hello, world...";
486 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
487 size_t ciphertext_length = sizeof( ciphertext );
488 size_t plaintext_length = sizeof( ciphertext );
489
490 if( usage & PSA_KEY_USAGE_ENCRYPT )
491 {
Gilles Peskine8817f612018-12-18 00:18:46 +0100492 PSA_ASSERT( psa_aead_encrypt( handle, alg,
493 nonce, nonce_length,
494 NULL, 0,
495 plaintext, sizeof( plaintext ),
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 verify_status =
503 ( usage & PSA_KEY_USAGE_ENCRYPT ?
504 PSA_SUCCESS :
505 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100506 TEST_EQUAL( psa_aead_decrypt( handle, alg,
507 nonce, nonce_length,
508 NULL, 0,
509 ciphertext, ciphertext_length,
510 plaintext, sizeof( plaintext ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100511 &plaintext_length ),
512 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200513 }
514
515 return( 1 );
516
517exit:
518 return( 0 );
519}
520
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100521static int exercise_signature_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200522 psa_key_usage_t usage,
523 psa_algorithm_t alg )
524{
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200525 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
526 size_t payload_length = 16;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100527 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine818ca122018-06-20 18:16:48 +0200528 size_t signature_length = sizeof( signature );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100529 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
530
531 /* If the policy allows signing with any hash, just pick one. */
532 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
533 {
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100534#if defined(KNOWN_SUPPORTED_HASH_ALG)
535 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
536 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
Gilles Peskine57ab7212019-01-28 13:03:09 +0100537#else
538 test_fail( "No hash algorithm for hash-and-sign testing", __LINE__, __FILE__ );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100539 return( 1 );
Gilles Peskine57ab7212019-01-28 13:03:09 +0100540#endif
Gilles Peskine57ab7212019-01-28 13:03:09 +0100541 }
Gilles Peskine818ca122018-06-20 18:16:48 +0200542
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100543 if( usage & PSA_KEY_USAGE_SIGN_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200544 {
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200545 /* Some algorithms require the payload to have the size of
546 * the hash encoded in the algorithm. Use this input size
547 * even for algorithms that allow other input sizes. */
Gilles Peskinef969b3a2018-06-30 00:20:25 +0200548 if( hash_alg != 0 )
549 payload_length = PSA_HASH_SIZE( hash_alg );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100550 PSA_ASSERT( psa_sign_hash( handle, alg,
551 payload, payload_length,
552 signature, sizeof( signature ),
553 &signature_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200554 }
555
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100556 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
Gilles Peskine818ca122018-06-20 18:16:48 +0200557 {
558 psa_status_t verify_status =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100559 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
Gilles Peskine818ca122018-06-20 18:16:48 +0200560 PSA_SUCCESS :
561 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100562 TEST_EQUAL( psa_verify_hash( handle, alg,
563 payload, payload_length,
564 signature, signature_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +0100565 verify_status );
Gilles Peskine818ca122018-06-20 18:16:48 +0200566 }
567
568 return( 1 );
569
570exit:
571 return( 0 );
572}
573
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100574static int exercise_asymmetric_encryption_key( psa_key_handle_t handle,
Gilles Peskine818ca122018-06-20 18:16:48 +0200575 psa_key_usage_t usage,
576 psa_algorithm_t alg )
577{
578 unsigned char plaintext[256] = "Hello, world...";
579 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
580 size_t ciphertext_length = sizeof( ciphertext );
581 size_t plaintext_length = 16;
582
583 if( usage & PSA_KEY_USAGE_ENCRYPT )
584 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100585 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
586 plaintext, plaintext_length,
587 NULL, 0,
588 ciphertext, sizeof( ciphertext ),
589 &ciphertext_length ) );
Gilles Peskine818ca122018-06-20 18:16:48 +0200590 }
591
592 if( usage & PSA_KEY_USAGE_DECRYPT )
593 {
594 psa_status_t status =
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100595 psa_asymmetric_decrypt( handle, alg,
Gilles Peskine818ca122018-06-20 18:16:48 +0200596 ciphertext, ciphertext_length,
597 NULL, 0,
598 plaintext, sizeof( plaintext ),
599 &plaintext_length );
600 TEST_ASSERT( status == PSA_SUCCESS ||
601 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
602 ( status == PSA_ERROR_INVALID_ARGUMENT ||
603 status == PSA_ERROR_INVALID_PADDING ) ) );
604 }
605
606 return( 1 );
607
608exit:
609 return( 0 );
610}
Gilles Peskine02b75072018-07-01 22:31:34 +0200611
Janos Follathf2815ea2019-07-03 12:41:36 +0100612static int setup_key_derivation_wrap( psa_key_derivation_operation_t* operation,
613 psa_key_handle_t handle,
614 psa_algorithm_t alg,
615 unsigned char* input1, size_t input1_length,
616 unsigned char* input2, size_t input2_length,
617 size_t capacity )
618{
619 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
620 if( PSA_ALG_IS_HKDF( alg ) )
621 {
622 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
623 PSA_KEY_DERIVATION_INPUT_SALT,
624 input1, input1_length ) );
625 PSA_ASSERT( psa_key_derivation_input_key( operation,
626 PSA_KEY_DERIVATION_INPUT_SECRET,
627 handle ) );
628 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
629 PSA_KEY_DERIVATION_INPUT_INFO,
630 input2,
631 input2_length ) );
632 }
633 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
634 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
635 {
636 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
637 PSA_KEY_DERIVATION_INPUT_SEED,
638 input1, input1_length ) );
639 PSA_ASSERT( psa_key_derivation_input_key( operation,
640 PSA_KEY_DERIVATION_INPUT_SECRET,
641 handle ) );
642 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
643 PSA_KEY_DERIVATION_INPUT_LABEL,
644 input2, input2_length ) );
645 }
646 else
647 {
648 TEST_ASSERT( ! "Key derivation algorithm not supported" );
649 }
650
Gilles Peskinec744d992019-07-30 17:26:54 +0200651 if( capacity != SIZE_MAX )
652 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
Janos Follathf2815ea2019-07-03 12:41:36 +0100653
654 return( 1 );
655
656exit:
657 return( 0 );
658}
659
660
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100661static int exercise_key_derivation_key( psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +0200662 psa_key_usage_t usage,
663 psa_algorithm_t alg )
664{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200665 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathf2815ea2019-07-03 12:41:36 +0100666 unsigned char input1[] = "Input 1";
667 size_t input1_length = sizeof( input1 );
668 unsigned char input2[] = "Input 2";
669 size_t input2_length = sizeof( input2 );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200670 unsigned char output[1];
Janos Follathf2815ea2019-07-03 12:41:36 +0100671 size_t capacity = sizeof( output );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200672
673 if( usage & PSA_KEY_USAGE_DERIVE )
674 {
Janos Follathf2815ea2019-07-03 12:41:36 +0100675 if( !setup_key_derivation_wrap( &operation, handle, alg,
676 input1, input1_length,
677 input2, input2_length, capacity ) )
678 goto exit;
Gilles Peskine7607cd62019-05-29 17:35:00 +0200679
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200680 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200681 output,
Janos Follathf2815ea2019-07-03 12:41:36 +0100682 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200683 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +0200684 }
685
686 return( 1 );
687
688exit:
689 return( 0 );
690}
691
Gilles Peskinec7998b72018-11-07 18:45:02 +0100692/* We need two keys to exercise key agreement. Exercise the
693 * private key against its own public key. */
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200694static psa_status_t key_agreement_with_self(
695 psa_key_derivation_operation_t *operation,
696 psa_key_handle_t handle )
Gilles Peskinec7998b72018-11-07 18:45:02 +0100697{
698 psa_key_type_t private_key_type;
699 psa_key_type_t public_key_type;
700 size_t key_bits;
701 uint8_t *public_key = NULL;
702 size_t public_key_length;
David Saadab4ecc272019-02-14 13:48:10 +0200703 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200704 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
705 * but it's good enough: callers will report it as a failed test anyway. */
David Saadab4ecc272019-02-14 13:48:10 +0200706 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200707 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinec7998b72018-11-07 18:45:02 +0100708
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200709 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
710 private_key_type = psa_get_key_type( &attributes );
711 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200712 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100713 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
714 ASSERT_ALLOC( public_key, public_key_length );
Gilles Peskine8817f612018-12-18 00:18:46 +0100715 PSA_ASSERT( psa_export_public_key( handle,
716 public_key, public_key_length,
717 &public_key_length ) );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100718
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200719 status = psa_key_derivation_key_agreement(
720 operation, PSA_KEY_DERIVATION_INPUT_SECRET, handle,
721 public_key, public_key_length );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100722exit:
723 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200724 psa_reset_key_attributes( &attributes );
Gilles Peskinec7998b72018-11-07 18:45:02 +0100725 return( status );
726}
727
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200728/* We need two keys to exercise key agreement. Exercise the
729 * private key against its own public key. */
730static psa_status_t raw_key_agreement_with_self( psa_algorithm_t alg,
731 psa_key_handle_t handle )
732{
733 psa_key_type_t private_key_type;
734 psa_key_type_t public_key_type;
735 size_t key_bits;
736 uint8_t *public_key = NULL;
737 size_t public_key_length;
738 uint8_t output[1024];
739 size_t output_length;
740 /* Return GENERIC_ERROR if something other than the final call to
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200741 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
742 * but it's good enough: callers will report it as a failed test anyway. */
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200743 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200744 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200745
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200746 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
747 private_key_type = psa_get_key_type( &attributes );
748 key_bits = psa_get_key_bits( &attributes );
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200749 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200750 public_key_length = PSA_KEY_EXPORT_MAX_SIZE( public_key_type, key_bits );
751 ASSERT_ALLOC( public_key, public_key_length );
752 PSA_ASSERT( psa_export_public_key( handle,
753 public_key, public_key_length,
754 &public_key_length ) );
755
Gilles Peskinebe697d82019-05-16 18:00:41 +0200756 status = psa_raw_key_agreement( alg, handle,
757 public_key, public_key_length,
758 output, sizeof( output ), &output_length );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200759exit:
760 mbedtls_free( public_key );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200761 psa_reset_key_attributes( &attributes );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +0200762 return( status );
763}
764
765static int exercise_raw_key_agreement_key( psa_key_handle_t handle,
766 psa_key_usage_t usage,
767 psa_algorithm_t alg )
768{
769 int ok = 0;
770
771 if( usage & PSA_KEY_USAGE_DERIVE )
772 {
773 /* We need two keys to exercise key agreement. Exercise the
774 * private key against its own public key. */
775 PSA_ASSERT( raw_key_agreement_with_self( alg, handle ) );
776 }
777 ok = 1;
778
779exit:
780 return( ok );
781}
782
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100783static int exercise_key_agreement_key( psa_key_handle_t handle,
Gilles Peskine01d718c2018-09-18 12:01:02 +0200784 psa_key_usage_t usage,
785 psa_algorithm_t alg )
786{
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200787 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +0200788 unsigned char output[1];
789 int ok = 0;
790
791 if( usage & PSA_KEY_USAGE_DERIVE )
792 {
793 /* We need two keys to exercise key agreement. Exercise the
794 * private key against its own public key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200795 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
796 PSA_ASSERT( key_agreement_with_self( &operation, handle ) );
797 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +0200798 output,
799 sizeof( output ) ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +0200800 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +0200801 }
802 ok = 1;
803
804exit:
Gilles Peskine01d718c2018-09-18 12:01:02 +0200805 return( ok );
806}
807
Jaeden Amerof7dca862019-06-27 17:31:33 +0100808int asn1_skip_integer( unsigned char **p, const unsigned char *end,
809 size_t min_bits, size_t max_bits,
810 int must_be_odd )
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200811{
812 size_t len;
813 size_t actual_bits;
814 unsigned char msb;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100815 TEST_EQUAL( mbedtls_asn1_get_tag( p, end, &len,
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100816 MBEDTLS_ASN1_INTEGER ),
817 0 );
k-stachowiak9b88efc2019-09-13 15:26:53 +0200818
819 /* Check if the retrieved length doesn't extend the actual buffer's size.
820 * It is assumed here, that end >= p, which validates casting to size_t. */
821 TEST_ASSERT( len <= (size_t)( end - *p) );
822
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200823 /* Tolerate a slight departure from DER encoding:
824 * - 0 may be represented by an empty string or a 1-byte string.
825 * - The sign bit may be used as a value bit. */
826 if( ( len == 1 && ( *p )[0] == 0 ) ||
827 ( len > 1 && ( *p )[0] == 0 && ( ( *p )[1] & 0x80 ) != 0 ) )
828 {
829 ++( *p );
830 --len;
831 }
832 if( min_bits == 0 && len == 0 )
833 return( 1 );
834 msb = ( *p )[0];
835 TEST_ASSERT( msb != 0 );
836 actual_bits = 8 * ( len - 1 );
837 while( msb != 0 )
838 {
839 msb >>= 1;
840 ++actual_bits;
841 }
842 TEST_ASSERT( actual_bits >= min_bits );
843 TEST_ASSERT( actual_bits <= max_bits );
844 if( must_be_odd )
845 TEST_ASSERT( ( ( *p )[len-1] & 1 ) != 0 );
846 *p += len;
847 return( 1 );
848exit:
849 return( 0 );
850}
851
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200852static int exported_key_sanity_check( psa_key_type_t type, size_t bits,
853 uint8_t *exported, size_t exported_length )
854{
855 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100856 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200857 else
858 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, bits ) );
Gilles Peskined14664a2018-08-10 19:07:32 +0200859
860#if defined(MBEDTLS_DES_C)
861 if( type == PSA_KEY_TYPE_DES )
862 {
863 /* Check the parity bits. */
864 unsigned i;
865 for( i = 0; i < bits / 8; i++ )
866 {
867 unsigned bit_count = 0;
868 unsigned m;
869 for( m = 1; m <= 0x100; m <<= 1 )
870 {
871 if( exported[i] & m )
872 ++bit_count;
873 }
874 TEST_ASSERT( bit_count % 2 != 0 );
875 }
876 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200877 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200878#endif
879
880#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200881 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskined14664a2018-08-10 19:07:32 +0200882 {
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200883 uint8_t *p = exported;
884 uint8_t *end = exported + exported_length;
885 size_t len;
886 /* RSAPrivateKey ::= SEQUENCE {
Gilles Peskinedea46cf2018-08-21 16:12:54 +0200887 * version INTEGER, -- must be 0
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200888 * modulus INTEGER, -- n
889 * publicExponent INTEGER, -- e
890 * privateExponent INTEGER, -- d
891 * prime1 INTEGER, -- p
892 * prime2 INTEGER, -- q
893 * exponent1 INTEGER, -- d mod (p-1)
894 * exponent2 INTEGER, -- d mod (q-1)
895 * coefficient INTEGER, -- (inverse of q) mod p
896 * }
897 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100898 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
899 MBEDTLS_ASN1_SEQUENCE |
900 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
901 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200902 if( ! asn1_skip_integer( &p, end, 0, 0, 0 ) )
903 goto exit;
904 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
905 goto exit;
906 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
907 goto exit;
908 /* Require d to be at least half the size of n. */
909 if( ! asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
910 goto exit;
911 /* Require p and q to be at most half the size of n, rounded up. */
912 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
913 goto exit;
914 if( ! asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
915 goto exit;
916 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
917 goto exit;
918 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
919 goto exit;
920 if( ! asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
921 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100922 TEST_EQUAL( p, end );
Gilles Peskine0f915f12018-12-17 23:35:42 +0100923 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200924 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200925#endif /* MBEDTLS_RSA_C */
926
927#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200928 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
Gilles Peskined14664a2018-08-10 19:07:32 +0200929 {
Gilles Peskine5b802a32018-10-29 19:21:41 +0100930 /* Just the secret value */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100931 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskine5b802a32018-10-29 19:21:41 +0100932 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200933 else
Gilles Peskined14664a2018-08-10 19:07:32 +0200934#endif /* MBEDTLS_ECP_C */
935
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200936 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
937 {
938 uint8_t *p = exported;
939 uint8_t *end = exported + exported_length;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200940#if defined(MBEDTLS_RSA_C)
941 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
942 {
Jaeden Amerof7dca862019-06-27 17:31:33 +0100943 size_t len;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200944 /* RSAPublicKey ::= SEQUENCE {
945 * modulus INTEGER, -- n
946 * publicExponent INTEGER } -- e
947 */
Gilles Peskinefe11b722018-12-18 00:24:04 +0100948 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
949 MBEDTLS_ASN1_SEQUENCE |
Gilles Peskinef812dcf2018-12-18 00:33:25 +0100950 MBEDTLS_ASN1_CONSTRUCTED ),
951 0 );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100952 TEST_EQUAL( p + len, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200953 if( ! asn1_skip_integer( &p, end, bits, bits, 1 ) )
954 goto exit;
955 if( ! asn1_skip_integer( &p, end, 2, bits, 1 ) )
956 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +0100957 TEST_EQUAL( p, end );
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200958 }
959 else
960#endif /* MBEDTLS_RSA_C */
961#if defined(MBEDTLS_ECP_C)
962 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
963 {
Steven Cooreman3fa684e2020-07-30 15:04:07 +0200964 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
965 {
966 /* The representation of an ECC Montgomery public key is
967 * the raw compressed point */
968 TEST_EQUAL( p + PSA_BITS_TO_BYTES( bits ), end );
969 }
970 else
971 {
972 /* The representation of an ECC Weierstrass public key is:
973 * - The byte 0x04;
974 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
975 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
976 * - where m is the bit size associated with the curve.
977 */
978 TEST_EQUAL( p + 1 + 2 * PSA_BITS_TO_BYTES( bits ), end );
979 TEST_EQUAL( p[0], 4 );
980 }
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200981 }
982 else
983#endif /* MBEDTLS_ECP_C */
984 {
Jaeden Amero594a3302018-10-26 17:07:22 +0100985 char message[47];
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200986 mbedtls_snprintf( message, sizeof( message ),
987 "No sanity check for public key type=0x%08lx",
988 (unsigned long) type );
989 test_fail( message, __LINE__, __FILE__ );
Gilles Peskineb16841e2019-10-10 20:36:12 +0200990 (void) p;
991 (void) end;
Gilles Peskinedd2f95b2018-08-11 01:22:42 +0200992 return( 0 );
993 }
994 }
995 else
996
997 {
998 /* No sanity checks for other types */
999 }
1000
1001 return( 1 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001002
1003exit:
Gilles Peskinedd2f95b2018-08-11 01:22:42 +02001004 return( 0 );
Gilles Peskined14664a2018-08-10 19:07:32 +02001005}
1006
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001007static int exercise_export_key( psa_key_handle_t handle,
Gilles Peskined14664a2018-08-10 19:07:32 +02001008 psa_key_usage_t usage )
1009{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001010 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001011 uint8_t *exported = NULL;
1012 size_t exported_size = 0;
1013 size_t exported_length = 0;
1014 int ok = 0;
1015
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001016 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskineacec7b6f2018-09-13 20:34:11 +02001017
1018 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001019 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001020 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001021 TEST_EQUAL( psa_export_key( handle, NULL, 0, &exported_length ),
1022 PSA_ERROR_NOT_PERMITTED );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001023 ok = 1;
1024 goto exit;
Gilles Peskined14664a2018-08-10 19:07:32 +02001025 }
1026
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001027 exported_size = PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( &attributes ),
1028 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001029 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001030
Gilles Peskine8817f612018-12-18 00:18:46 +01001031 PSA_ASSERT( psa_export_key( handle,
1032 exported, exported_size,
1033 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001034 ok = exported_key_sanity_check( psa_get_key_type( &attributes ),
1035 psa_get_key_bits( &attributes ),
1036 exported, exported_length );
Gilles Peskined14664a2018-08-10 19:07:32 +02001037
1038exit:
1039 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001040 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +02001041 return( ok );
1042}
1043
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001044static int exercise_export_public_key( psa_key_handle_t handle )
Gilles Peskined14664a2018-08-10 19:07:32 +02001045{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001046 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined14664a2018-08-10 19:07:32 +02001047 psa_key_type_t public_type;
Gilles Peskined14664a2018-08-10 19:07:32 +02001048 uint8_t *exported = NULL;
1049 size_t exported_size = 0;
1050 size_t exported_length = 0;
1051 int ok = 0;
1052
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001053 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1054 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
Gilles Peskined14664a2018-08-10 19:07:32 +02001055 {
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001056 TEST_EQUAL( psa_export_public_key( handle, NULL, 0, &exported_length ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001057 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined14664a2018-08-10 19:07:32 +02001058 return( 1 );
1059 }
1060
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001061 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001062 psa_get_key_type( &attributes ) );
1063 exported_size = PSA_KEY_EXPORT_MAX_SIZE( public_type,
1064 psa_get_key_bits( &attributes ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001065 ASSERT_ALLOC( exported, exported_size );
Gilles Peskined14664a2018-08-10 19:07:32 +02001066
Gilles Peskine8817f612018-12-18 00:18:46 +01001067 PSA_ASSERT( psa_export_public_key( handle,
1068 exported, exported_size,
1069 &exported_length ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001070 ok = exported_key_sanity_check( public_type,
1071 psa_get_key_bits( &attributes ),
Gilles Peskined14664a2018-08-10 19:07:32 +02001072 exported, exported_length );
1073
1074exit:
1075 mbedtls_free( exported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001076 psa_reset_key_attributes( &attributes );
Gilles Peskined14664a2018-08-10 19:07:32 +02001077 return( ok );
1078}
1079
Gilles Peskinec9516fb2019-02-05 20:32:06 +01001080/** Do smoke tests on a key.
1081 *
1082 * Perform one of each operation indicated by \p alg (decrypt/encrypt,
1083 * sign/verify, or derivation) that is permitted according to \p usage.
1084 * \p usage and \p alg should correspond to the expected policy on the
1085 * key.
1086 *
1087 * Export the key if permitted by \p usage, and check that the output
1088 * looks sensible. If \p usage forbids export, check that
1089 * \p psa_export_key correctly rejects the attempt. If the key is
1090 * asymmetric, also check \p psa_export_public_key.
1091 *
1092 * If the key fails the tests, this function calls the test framework's
1093 * `test_fail` function and returns false. Otherwise this function returns
1094 * true. Therefore it should be used as follows:
1095 * ```
1096 * if( ! exercise_key( ... ) ) goto exit;
1097 * ```
1098 *
1099 * \param handle The key to exercise. It should be capable of performing
1100 * \p alg.
1101 * \param usage The usage flags to assume.
1102 * \param alg The algorithm to exercise.
1103 *
1104 * \retval 0 The key failed the smoke tests.
1105 * \retval 1 The key passed the smoke tests.
1106 */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001107static int exercise_key( psa_key_handle_t handle,
Gilles Peskine02b75072018-07-01 22:31:34 +02001108 psa_key_usage_t usage,
1109 psa_algorithm_t alg )
1110{
1111 int ok;
Gilles Peskine667c1112019-12-03 19:03:20 +01001112
1113 if( ! check_key_attributes_sanity( handle ) )
1114 return( 0 );
1115
Gilles Peskine02b75072018-07-01 22:31:34 +02001116 if( alg == 0 )
1117 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
1118 else if( PSA_ALG_IS_MAC( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001119 ok = exercise_mac_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001120 else if( PSA_ALG_IS_CIPHER( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001121 ok = exercise_cipher_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001122 else if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001123 ok = exercise_aead_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001124 else if( PSA_ALG_IS_SIGN( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001125 ok = exercise_signature_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001126 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001127 ok = exercise_asymmetric_encryption_key( handle, usage, alg );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001128 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001129 ok = exercise_key_derivation_key( handle, usage, alg );
Gilles Peskine2e46e9c2019-04-11 21:24:55 +02001130 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
1131 ok = exercise_raw_key_agreement_key( handle, usage, alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001132 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001133 ok = exercise_key_agreement_key( handle, usage, alg );
Gilles Peskine02b75072018-07-01 22:31:34 +02001134 else
1135 {
1136 char message[40];
1137 mbedtls_snprintf( message, sizeof( message ),
1138 "No code to exercise alg=0x%08lx",
1139 (unsigned long) alg );
1140 test_fail( message, __LINE__, __FILE__ );
1141 ok = 0;
1142 }
Gilles Peskined14664a2018-08-10 19:07:32 +02001143
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001144 ok = ok && exercise_export_key( handle, usage );
1145 ok = ok && exercise_export_public_key( handle );
Gilles Peskined14664a2018-08-10 19:07:32 +02001146
Gilles Peskine02b75072018-07-01 22:31:34 +02001147 return( ok );
1148}
1149
Gilles Peskine10df3412018-10-25 22:35:43 +02001150static psa_key_usage_t usage_to_exercise( psa_key_type_t type,
1151 psa_algorithm_t alg )
1152{
1153 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
1154 {
1155 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001156 PSA_KEY_USAGE_VERIFY_HASH :
1157 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine10df3412018-10-25 22:35:43 +02001158 }
1159 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
1160 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
1161 {
1162 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
1163 PSA_KEY_USAGE_ENCRYPT :
1164 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1165 }
1166 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
1167 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
1168 {
1169 return( PSA_KEY_USAGE_DERIVE );
1170 }
1171 else
1172 {
1173 return( 0 );
1174 }
1175
1176}
Darryl Green0c6575a2018-11-07 16:05:30 +00001177
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001178static int test_operations_on_invalid_handle( psa_key_handle_t handle )
1179{
1180 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1181 uint8_t buffer[1];
1182 size_t length;
1183 int ok = 0;
1184
Gilles Peskinec87af662019-05-15 16:12:22 +02001185 psa_set_key_id( &attributes, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001186 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1187 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1188 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
1189 TEST_EQUAL( psa_get_key_attributes( handle, &attributes ),
1190 PSA_ERROR_INVALID_HANDLE );
1191 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001192 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001193 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1194 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1195 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1196 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1197
1198 TEST_EQUAL( psa_export_key( handle,
1199 buffer, sizeof( buffer ), &length ),
1200 PSA_ERROR_INVALID_HANDLE );
1201 TEST_EQUAL( psa_export_public_key( handle,
1202 buffer, sizeof( buffer ), &length ),
1203 PSA_ERROR_INVALID_HANDLE );
1204
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001205 ok = 1;
1206
1207exit:
1208 psa_reset_key_attributes( &attributes );
1209 return( ok );
1210}
1211
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001212/* Assert that a key isn't reported as having a slot number. */
1213#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1214#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1215 do \
1216 { \
1217 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1218 TEST_EQUAL( psa_get_key_slot_number( \
1219 attributes, \
1220 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1221 PSA_ERROR_INVALID_ARGUMENT ); \
1222 } \
1223 while( 0 )
1224#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1225#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1226 ( (void) 0 )
1227#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1228
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001229/* An overapproximation of the amount of storage needed for a key of the
1230 * given type and with the given content. The API doesn't make it easy
1231 * to find a good value for the size. The current implementation doesn't
1232 * care about the value anyway. */
1233#define KEY_BITS_FROM_DATA( type, data ) \
1234 ( data )->len
1235
Darryl Green0c6575a2018-11-07 16:05:30 +00001236typedef enum {
1237 IMPORT_KEY = 0,
1238 GENERATE_KEY = 1,
1239 DERIVE_KEY = 2
1240} generate_method;
1241
Gilles Peskinee59236f2018-01-27 23:32:46 +01001242/* END_HEADER */
1243
1244/* BEGIN_DEPENDENCIES
1245 * depends_on:MBEDTLS_PSA_CRYPTO_C
1246 * END_DEPENDENCIES
1247 */
1248
1249/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001250void static_checks( )
1251{
1252 size_t max_truncated_mac_size =
1253 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1254
1255 /* Check that the length for a truncated MAC always fits in the algorithm
1256 * encoding. The shifted mask is the maximum truncated value. The
1257 * untruncated algorithm may be one byte larger. */
1258 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001259
1260#if defined(MBEDTLS_TEST_DEPRECATED)
1261 /* Check deprecated constants. */
1262 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1263 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1264 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1265 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1266 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1267 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1268 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1269 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001270
Paul Elliott8ff510a2020-06-02 17:19:28 +01001271 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
1272 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
1273 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
1274 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
1275 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
1276 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
1277 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
1278 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
1279 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
1280 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
1281 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
1282 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
1283 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
1284 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
1285 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
1286 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
1287 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
1288 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
1289 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
1290 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
1291 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
1292 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
1293 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
1294 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
1295 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
1296 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1297 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1298 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1299 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
1300 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
1301
1302 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
1303 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
1304 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
1305 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
1306 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
1307 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
1308 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1309 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001310
Paul Elliott75e27032020-06-03 15:17:39 +01001311 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
1312 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
1313 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
1314 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
1315 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
1316
1317 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
1318 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001319#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001320}
1321/* END_CASE */
1322
1323/* BEGIN_CASE */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001324void attributes_set_get( int id_arg, int lifetime_arg,
1325 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001326 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001327{
Gilles Peskine4747d192019-04-17 15:05:45 +02001328 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001329 psa_key_id_t id = id_arg;
1330 psa_key_lifetime_t lifetime = lifetime_arg;
1331 psa_key_usage_t usage_flags = usage_flags_arg;
1332 psa_algorithm_t alg = alg_arg;
1333 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001334 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001335
1336 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1337 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1338 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1339 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1340 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001341 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001342
Gilles Peskinec87af662019-05-15 16:12:22 +02001343 psa_set_key_id( &attributes, id );
1344 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001345 psa_set_key_usage_flags( &attributes, usage_flags );
1346 psa_set_key_algorithm( &attributes, alg );
1347 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001348 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001349
1350 TEST_EQUAL( psa_get_key_id( &attributes ), id );
1351 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1352 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1353 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1354 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001355 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001356
1357 psa_reset_key_attributes( &attributes );
1358
1359 TEST_EQUAL( psa_get_key_id( &attributes ), 0 );
1360 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1361 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1362 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1363 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001364 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001365}
1366/* END_CASE */
1367
1368/* BEGIN_CASE */
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001369void persistence_attributes( int id1_arg, int lifetime_arg, int id2_arg,
1370 int expected_id_arg, int expected_lifetime_arg )
1371{
1372 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1373 psa_key_id_t id1 = id1_arg;
1374 psa_key_lifetime_t lifetime = lifetime_arg;
1375 psa_key_id_t id2 = id2_arg;
1376 psa_key_id_t expected_id = expected_id_arg;
1377 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1378
1379 if( id1_arg != -1 )
1380 psa_set_key_id( &attributes, id1 );
1381 if( lifetime_arg != -1 )
1382 psa_set_key_lifetime( &attributes, lifetime );
1383 if( id2_arg != -1 )
1384 psa_set_key_id( &attributes, id2 );
1385
1386 TEST_EQUAL( psa_get_key_id( &attributes ), expected_id );
1387 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1388}
1389/* END_CASE */
1390
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001391/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1392void slot_number_attribute( )
1393{
1394 psa_key_slot_number_t slot_number = 0xdeadbeef;
1395 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1396
1397 /* Initially, there is no slot number. */
1398 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1399 PSA_ERROR_INVALID_ARGUMENT );
1400
1401 /* Test setting a slot number. */
1402 psa_set_key_slot_number( &attributes, 0 );
1403 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1404 TEST_EQUAL( slot_number, 0 );
1405
1406 /* Test changing the slot number. */
1407 psa_set_key_slot_number( &attributes, 42 );
1408 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1409 TEST_EQUAL( slot_number, 42 );
1410
1411 /* Test clearing the slot number. */
1412 psa_clear_key_slot_number( &attributes );
1413 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1414 PSA_ERROR_INVALID_ARGUMENT );
1415
1416 /* Clearing again should have no effect. */
1417 psa_clear_key_slot_number( &attributes );
1418 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1419 PSA_ERROR_INVALID_ARGUMENT );
1420
1421 /* Test that reset clears the slot number. */
1422 psa_set_key_slot_number( &attributes, 42 );
1423 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1424 TEST_EQUAL( slot_number, 42 );
1425 psa_reset_key_attributes( &attributes );
1426 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1427 PSA_ERROR_INVALID_ARGUMENT );
1428}
1429/* END_CASE */
1430
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001431/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001432void import_with_policy( int type_arg,
1433 int usage_arg, int alg_arg,
1434 int expected_status_arg )
1435{
1436 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1437 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1438 psa_key_handle_t handle = 0;
1439 psa_key_type_t type = type_arg;
1440 psa_key_usage_t usage = usage_arg;
1441 psa_algorithm_t alg = alg_arg;
1442 psa_status_t expected_status = expected_status_arg;
1443 const uint8_t key_material[16] = {0};
1444 psa_status_t status;
1445
1446 PSA_ASSERT( psa_crypto_init( ) );
1447
1448 psa_set_key_type( &attributes, type );
1449 psa_set_key_usage_flags( &attributes, usage );
1450 psa_set_key_algorithm( &attributes, alg );
1451
1452 status = psa_import_key( &attributes,
1453 key_material, sizeof( key_material ),
1454 &handle );
1455 TEST_EQUAL( status, expected_status );
1456 if( status != PSA_SUCCESS )
1457 goto exit;
1458
1459 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1460 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1461 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1462 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001463 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001464
1465 PSA_ASSERT( psa_destroy_key( handle ) );
1466 test_operations_on_invalid_handle( handle );
1467
1468exit:
1469 psa_destroy_key( handle );
1470 psa_reset_key_attributes( &got_attributes );
1471 PSA_DONE( );
1472}
1473/* END_CASE */
1474
1475/* BEGIN_CASE */
1476void import_with_data( data_t *data, int type_arg,
1477 int attr_bits_arg,
1478 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001479{
1480 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1481 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001482 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001483 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001484 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001485 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001486 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001487
Gilles Peskine8817f612018-12-18 00:18:46 +01001488 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001489
Gilles Peskine4747d192019-04-17 15:05:45 +02001490 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001491 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001492
Gilles Peskine73676cb2019-05-15 20:15:10 +02001493 status = psa_import_key( &attributes, data->x, data->len, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001494 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001495 if( status != PSA_SUCCESS )
1496 goto exit;
1497
1498 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1499 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001500 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001501 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001502 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001503
1504 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001505 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001506
1507exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001508 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001509 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001510 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001511}
1512/* END_CASE */
1513
1514/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001515void import_large_key( int type_arg, int byte_size_arg,
1516 int expected_status_arg )
1517{
1518 psa_key_type_t type = type_arg;
1519 size_t byte_size = byte_size_arg;
1520 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1521 psa_status_t expected_status = expected_status_arg;
1522 psa_key_handle_t handle = 0;
1523 psa_status_t status;
1524 uint8_t *buffer = NULL;
1525 size_t buffer_size = byte_size + 1;
1526 size_t n;
1527
1528 /* It would be better to skip the test than fail it if the allocation
1529 * fails, but the test framework doesn't support this yet. */
1530 ASSERT_ALLOC( buffer, buffer_size );
1531 memset( buffer, 'K', byte_size );
1532
1533 PSA_ASSERT( psa_crypto_init( ) );
1534
1535 /* Try importing the key */
1536 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1537 psa_set_key_type( &attributes, type );
1538 status = psa_import_key( &attributes, buffer, byte_size, &handle );
1539 TEST_EQUAL( status, expected_status );
1540
1541 if( status == PSA_SUCCESS )
1542 {
1543 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1544 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1545 TEST_EQUAL( psa_get_key_bits( &attributes ),
1546 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001547 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001548 memset( buffer, 0, byte_size + 1 );
1549 PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) );
1550 for( n = 0; n < byte_size; n++ )
1551 TEST_EQUAL( buffer[n], 'K' );
1552 for( n = byte_size; n < buffer_size; n++ )
1553 TEST_EQUAL( buffer[n], 0 );
1554 }
1555
1556exit:
1557 psa_destroy_key( handle );
1558 PSA_DONE( );
1559 mbedtls_free( buffer );
1560}
1561/* END_CASE */
1562
1563/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001564void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1565{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001566 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001567 size_t bits = bits_arg;
1568 psa_status_t expected_status = expected_status_arg;
1569 psa_status_t status;
1570 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001571 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001572 size_t buffer_size = /* Slight overapproximations */
1573 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001574 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001575 unsigned char *p;
1576 int ret;
1577 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001578 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001579
Gilles Peskine8817f612018-12-18 00:18:46 +01001580 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001581 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001582
1583 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1584 bits, keypair ) ) >= 0 );
1585 length = ret;
1586
1587 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001588 psa_set_key_type( &attributes, type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001589 status = psa_import_key( &attributes, p, length, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001590 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001591
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001592 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001593 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001594
1595exit:
1596 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001597 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001598}
1599/* END_CASE */
1600
1601/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001602void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001603 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001604 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001605 int expected_bits,
1606 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001607 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001608 int canonical_input )
1609{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001610 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001611 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001612 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001613 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001614 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001615 unsigned char *exported = NULL;
1616 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001617 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001618 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001619 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001620 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001621 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001622
Moran Pekercb088e72018-07-17 17:36:59 +03001623 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001624 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001625 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001626 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001627 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001628
Gilles Peskine4747d192019-04-17 15:05:45 +02001629 psa_set_key_usage_flags( &attributes, usage_arg );
1630 psa_set_key_algorithm( &attributes, alg );
1631 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001632
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001633 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001634 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001635
1636 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001637 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1638 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1639 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001640 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001641
1642 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001643 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001644 exported, export_size,
1645 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001646 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001647
1648 /* The exported length must be set by psa_export_key() to a value between 0
1649 * and export_size. On errors, the exported length must be 0. */
1650 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1651 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1652 TEST_ASSERT( exported_length <= export_size );
1653
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001654 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001655 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001656 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001657 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001658 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001659 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001660 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001661
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001662 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001663 goto exit;
1664
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001665 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001666 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001667 else
1668 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001669 psa_key_handle_t handle2;
Gilles Peskine049c7532019-05-15 20:22:09 +02001670 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
1671 &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001672 PSA_ASSERT( psa_export_key( handle2,
1673 reexported,
1674 export_size,
1675 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001676 ASSERT_COMPARE( exported, exported_length,
1677 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001678 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001679 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001680 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001681
1682destroy:
1683 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001684 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001685 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001686
1687exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001688 mbedtls_free( exported );
1689 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001690 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001691 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001692}
1693/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001694
Moran Pekerf709f4a2018-06-06 17:26:04 +03001695/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001696void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001697 int type_arg,
1698 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001699 int export_size_delta,
1700 int expected_export_status_arg,
1701 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001702{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001703 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001704 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001705 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001706 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001707 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001708 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001709 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001710 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001711 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001712
Gilles Peskine8817f612018-12-18 00:18:46 +01001713 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001714
Gilles Peskine4747d192019-04-17 15:05:45 +02001715 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1716 psa_set_key_algorithm( &attributes, alg );
1717 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001718
1719 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001720 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001721
Gilles Peskine49c25912018-10-29 15:15:31 +01001722 /* Export the public key */
1723 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001724 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001725 exported, export_size,
1726 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001727 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001728 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001729 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001730 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001731 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001732 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1733 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001734 TEST_ASSERT( expected_public_key->len <=
1735 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001736 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1737 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001738 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001739
1740exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001741 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001742 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001743 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001744 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001745}
1746/* END_CASE */
1747
Gilles Peskine20035e32018-02-03 22:44:14 +01001748/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001749void import_and_exercise_key( data_t *data,
1750 int type_arg,
1751 int bits_arg,
1752 int alg_arg )
1753{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001754 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001755 psa_key_type_t type = type_arg;
1756 size_t bits = bits_arg;
1757 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001758 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001759 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001760 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001761
Gilles Peskine8817f612018-12-18 00:18:46 +01001762 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001763
Gilles Peskine4747d192019-04-17 15:05:45 +02001764 psa_set_key_usage_flags( &attributes, usage );
1765 psa_set_key_algorithm( &attributes, alg );
1766 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001767
1768 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001769 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001770
1771 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001772 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1773 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1774 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001775
1776 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001777 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001778 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001779
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001780 PSA_ASSERT( psa_destroy_key( handle ) );
1781 test_operations_on_invalid_handle( handle );
1782
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001783exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001784 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001785 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001786 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001787}
1788/* END_CASE */
1789
1790/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001791void effective_key_attributes( int type_arg, int expected_type_arg,
1792 int bits_arg, int expected_bits_arg,
1793 int usage_arg, int expected_usage_arg,
1794 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001795{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001796 psa_key_handle_t handle = 0;
Gilles Peskine1a960492019-11-26 17:12:21 +01001797 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001798 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001799 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001800 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001801 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001802 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001803 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001804 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001805 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001806
Gilles Peskine8817f612018-12-18 00:18:46 +01001807 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001808
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001809 psa_set_key_usage_flags( &attributes, usage );
1810 psa_set_key_algorithm( &attributes, alg );
1811 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001812 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001813
Gilles Peskine1a960492019-11-26 17:12:21 +01001814 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
1815 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001816
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001817 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001818 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1819 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1820 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1821 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001822
1823exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001824 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001825 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001826 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001827}
1828/* END_CASE */
1829
1830/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001831void check_key_policy( int type_arg, int bits_arg,
1832 int usage_arg, int alg_arg )
1833{
1834 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1835 usage_arg, usage_arg, alg_arg, alg_arg );
1836 goto exit;
1837}
1838/* END_CASE */
1839
1840/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001841void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001842{
1843 /* Test each valid way of initializing the object, except for `= {0}`, as
1844 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1845 * though it's OK by the C standard. We could test for this, but we'd need
1846 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001847 psa_key_attributes_t func = psa_key_attributes_init( );
1848 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1849 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001850
1851 memset( &zero, 0, sizeof( zero ) );
1852
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001853 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1854 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1855 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001856
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001857 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1858 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1859 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1860
1861 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1862 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1863 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1864
1865 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1866 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1867 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1868
1869 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1870 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1871 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001872}
1873/* END_CASE */
1874
1875/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001876void mac_key_policy( int policy_usage,
1877 int policy_alg,
1878 int key_type,
1879 data_t *key_data,
1880 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001881{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001882 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001883 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001884 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001885 psa_status_t status;
1886 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001887
Gilles Peskine8817f612018-12-18 00:18:46 +01001888 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001889
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001890 psa_set_key_usage_flags( &attributes, policy_usage );
1891 psa_set_key_algorithm( &attributes, policy_alg );
1892 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001893
Gilles Peskine049c7532019-05-15 20:22:09 +02001894 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1895 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001896
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001897 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001898 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001899 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001900 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001901 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001902 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001903 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001904
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001905 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001906 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001907 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001908 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001909 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001910 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001911 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001912
1913exit:
1914 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001915 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001916 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001917}
1918/* END_CASE */
1919
1920/* BEGIN_CASE */
1921void cipher_key_policy( int policy_usage,
1922 int policy_alg,
1923 int key_type,
1924 data_t *key_data,
1925 int exercise_alg )
1926{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001927 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001928 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001929 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001930 psa_status_t status;
1931
Gilles Peskine8817f612018-12-18 00:18:46 +01001932 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001933
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001934 psa_set_key_usage_flags( &attributes, policy_usage );
1935 psa_set_key_algorithm( &attributes, policy_alg );
1936 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001937
Gilles Peskine049c7532019-05-15 20:22:09 +02001938 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1939 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001940
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001941 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001942 if( policy_alg == exercise_alg &&
1943 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001944 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001945 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001946 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001947 psa_cipher_abort( &operation );
1948
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001949 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001950 if( policy_alg == exercise_alg &&
1951 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001952 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001953 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001954 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001955
1956exit:
1957 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001958 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001959 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001960}
1961/* END_CASE */
1962
1963/* BEGIN_CASE */
1964void aead_key_policy( int policy_usage,
1965 int policy_alg,
1966 int key_type,
1967 data_t *key_data,
1968 int nonce_length_arg,
1969 int tag_length_arg,
1970 int exercise_alg )
1971{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001972 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001973 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001974 psa_status_t status;
1975 unsigned char nonce[16] = {0};
1976 size_t nonce_length = nonce_length_arg;
1977 unsigned char tag[16];
1978 size_t tag_length = tag_length_arg;
1979 size_t output_length;
1980
1981 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1982 TEST_ASSERT( tag_length <= sizeof( tag ) );
1983
Gilles Peskine8817f612018-12-18 00:18:46 +01001984 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001985
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001986 psa_set_key_usage_flags( &attributes, policy_usage );
1987 psa_set_key_algorithm( &attributes, policy_alg );
1988 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001989
Gilles Peskine049c7532019-05-15 20:22:09 +02001990 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1991 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001992
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001993 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001994 nonce, nonce_length,
1995 NULL, 0,
1996 NULL, 0,
1997 tag, tag_length,
1998 &output_length );
1999 if( policy_alg == exercise_alg &&
2000 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002001 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002002 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002003 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002004
2005 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002006 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002007 nonce, nonce_length,
2008 NULL, 0,
2009 tag, tag_length,
2010 NULL, 0,
2011 &output_length );
2012 if( policy_alg == exercise_alg &&
2013 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002014 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002015 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002016 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002017
2018exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002019 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002020 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002021}
2022/* END_CASE */
2023
2024/* BEGIN_CASE */
2025void asymmetric_encryption_key_policy( int policy_usage,
2026 int policy_alg,
2027 int key_type,
2028 data_t *key_data,
2029 int exercise_alg )
2030{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002031 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002032 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002033 psa_status_t status;
2034 size_t key_bits;
2035 size_t buffer_length;
2036 unsigned char *buffer = NULL;
2037 size_t output_length;
2038
Gilles Peskine8817f612018-12-18 00:18:46 +01002039 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002040
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002041 psa_set_key_usage_flags( &attributes, policy_usage );
2042 psa_set_key_algorithm( &attributes, policy_alg );
2043 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002044
Gilles Peskine049c7532019-05-15 20:22:09 +02002045 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2046 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002047
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002048 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
2049 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002050 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2051 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002052 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002053
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002054 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002055 NULL, 0,
2056 NULL, 0,
2057 buffer, buffer_length,
2058 &output_length );
2059 if( policy_alg == exercise_alg &&
2060 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002061 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002062 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002063 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002064
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002065 if( buffer_length != 0 )
2066 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002067 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002068 buffer, buffer_length,
2069 NULL, 0,
2070 buffer, buffer_length,
2071 &output_length );
2072 if( policy_alg == exercise_alg &&
2073 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002074 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002075 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002076 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002077
2078exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002079 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002080 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002081 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002082 mbedtls_free( buffer );
2083}
2084/* END_CASE */
2085
2086/* BEGIN_CASE */
2087void asymmetric_signature_key_policy( int policy_usage,
2088 int policy_alg,
2089 int key_type,
2090 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002091 int exercise_alg,
2092 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002093{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002094 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002095 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002096 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002097 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2098 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2099 * compatible with the policy and `payload_length_arg` is supposed to be
2100 * a valid input length to sign. If `payload_length_arg <= 0`,
2101 * `exercise_alg` is supposed to be forbidden by the policy. */
2102 int compatible_alg = payload_length_arg > 0;
2103 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002104 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002105 size_t signature_length;
2106
Gilles Peskine8817f612018-12-18 00:18:46 +01002107 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002108
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002109 psa_set_key_usage_flags( &attributes, policy_usage );
2110 psa_set_key_algorithm( &attributes, policy_alg );
2111 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002112
Gilles Peskine049c7532019-05-15 20:22:09 +02002113 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2114 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002115
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002116 status = psa_sign_hash( handle, exercise_alg,
2117 payload, payload_length,
2118 signature, sizeof( signature ),
2119 &signature_length );
2120 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002121 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002122 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002123 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002124
2125 memset( signature, 0, sizeof( signature ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002126 status = psa_verify_hash( handle, exercise_alg,
2127 payload, payload_length,
2128 signature, sizeof( signature ) );
2129 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002130 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002131 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002132 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002133
2134exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002135 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002136 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002137}
2138/* END_CASE */
2139
Janos Follathba3fab92019-06-11 14:50:16 +01002140/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002141void derive_key_policy( int policy_usage,
2142 int policy_alg,
2143 int key_type,
2144 data_t *key_data,
2145 int exercise_alg )
2146{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002147 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002148 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002149 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002150 psa_status_t status;
2151
Gilles Peskine8817f612018-12-18 00:18:46 +01002152 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002153
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002154 psa_set_key_usage_flags( &attributes, policy_usage );
2155 psa_set_key_algorithm( &attributes, policy_alg );
2156 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002157
Gilles Peskine049c7532019-05-15 20:22:09 +02002158 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2159 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002160
Janos Follathba3fab92019-06-11 14:50:16 +01002161 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2162
2163 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2164 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002165 {
Janos Follathba3fab92019-06-11 14:50:16 +01002166 PSA_ASSERT( psa_key_derivation_input_bytes(
2167 &operation,
2168 PSA_KEY_DERIVATION_INPUT_SEED,
2169 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002170 }
Janos Follathba3fab92019-06-11 14:50:16 +01002171
2172 status = psa_key_derivation_input_key( &operation,
2173 PSA_KEY_DERIVATION_INPUT_SECRET,
2174 handle );
2175
Gilles Peskineea0fb492018-07-12 17:17:20 +02002176 if( policy_alg == exercise_alg &&
2177 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002178 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002179 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002180 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002181
2182exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002183 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002184 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002185 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002186}
2187/* END_CASE */
2188
2189/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002190void agreement_key_policy( int policy_usage,
2191 int policy_alg,
2192 int key_type_arg,
2193 data_t *key_data,
2194 int exercise_alg )
2195{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002196 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002197 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002198 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002199 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002200 psa_status_t status;
2201
Gilles Peskine8817f612018-12-18 00:18:46 +01002202 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002203
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002204 psa_set_key_usage_flags( &attributes, policy_usage );
2205 psa_set_key_algorithm( &attributes, policy_alg );
2206 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002207
Gilles Peskine049c7532019-05-15 20:22:09 +02002208 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2209 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002210
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002211 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2212 status = key_agreement_with_self( &operation, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002213
Gilles Peskine01d718c2018-09-18 12:01:02 +02002214 if( policy_alg == exercise_alg &&
2215 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002216 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002217 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002218 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002219
2220exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002221 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002222 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002223 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002224}
2225/* END_CASE */
2226
2227/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002228void key_policy_alg2( int key_type_arg, data_t *key_data,
2229 int usage_arg, int alg_arg, int alg2_arg )
2230{
2231 psa_key_handle_t handle = 0;
2232 psa_key_type_t key_type = key_type_arg;
2233 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2234 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2235 psa_key_usage_t usage = usage_arg;
2236 psa_algorithm_t alg = alg_arg;
2237 psa_algorithm_t alg2 = alg2_arg;
2238
2239 PSA_ASSERT( psa_crypto_init( ) );
2240
2241 psa_set_key_usage_flags( &attributes, usage );
2242 psa_set_key_algorithm( &attributes, alg );
2243 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2244 psa_set_key_type( &attributes, key_type );
2245 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2246 &handle ) );
2247
2248 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
2249 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2250 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2251 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2252
2253 if( ! exercise_key( handle, usage, alg ) )
2254 goto exit;
2255 if( ! exercise_key( handle, usage, alg2 ) )
2256 goto exit;
2257
2258exit:
2259 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002260 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002261}
2262/* END_CASE */
2263
2264/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002265void raw_agreement_key_policy( int policy_usage,
2266 int policy_alg,
2267 int key_type_arg,
2268 data_t *key_data,
2269 int exercise_alg )
2270{
2271 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002272 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002273 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002274 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002275 psa_status_t status;
2276
2277 PSA_ASSERT( psa_crypto_init( ) );
2278
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002279 psa_set_key_usage_flags( &attributes, policy_usage );
2280 psa_set_key_algorithm( &attributes, policy_alg );
2281 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002282
Gilles Peskine049c7532019-05-15 20:22:09 +02002283 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2284 &handle ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002285
2286 status = raw_key_agreement_with_self( exercise_alg, handle );
2287
2288 if( policy_alg == exercise_alg &&
2289 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
2290 PSA_ASSERT( status );
2291 else
2292 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2293
2294exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002295 psa_key_derivation_abort( &operation );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002296 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002297 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002298}
2299/* END_CASE */
2300
2301/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002302void copy_success( int source_usage_arg,
2303 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002304 int type_arg, data_t *material,
2305 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002306 int target_usage_arg,
2307 int target_alg_arg, int target_alg2_arg,
2308 int expected_usage_arg,
2309 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002310{
Gilles Peskineca25db92019-04-19 11:43:08 +02002311 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2312 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002313 psa_key_usage_t expected_usage = expected_usage_arg;
2314 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002315 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02002316 psa_key_handle_t source_handle = 0;
2317 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002318 uint8_t *export_buffer = NULL;
2319
Gilles Peskine57ab7212019-01-28 13:03:09 +01002320 PSA_ASSERT( psa_crypto_init( ) );
2321
Gilles Peskineca25db92019-04-19 11:43:08 +02002322 /* Prepare the source key. */
2323 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2324 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002325 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002326 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002327 PSA_ASSERT( psa_import_key( &source_attributes,
2328 material->x, material->len,
2329 &source_handle ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002330 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002331
Gilles Peskineca25db92019-04-19 11:43:08 +02002332 /* Prepare the target attributes. */
2333 if( copy_attributes )
2334 target_attributes = source_attributes;
2335 if( target_usage_arg != -1 )
2336 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2337 if( target_alg_arg != -1 )
2338 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002339 if( target_alg2_arg != -1 )
2340 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002341
2342 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02002343 PSA_ASSERT( psa_copy_key( source_handle,
2344 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002345
2346 /* Destroy the source to ensure that this doesn't affect the target. */
2347 PSA_ASSERT( psa_destroy_key( source_handle ) );
2348
2349 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02002350 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
2351 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2352 psa_get_key_type( &target_attributes ) );
2353 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2354 psa_get_key_bits( &target_attributes ) );
2355 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2356 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002357 TEST_EQUAL( expected_alg2,
2358 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002359 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2360 {
2361 size_t length;
2362 ASSERT_ALLOC( export_buffer, material->len );
2363 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2364 material->len, &length ) );
2365 ASSERT_COMPARE( material->x, material->len,
2366 export_buffer, length );
2367 }
2368 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2369 goto exit;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002370 if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) )
2371 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002372
2373 PSA_ASSERT( psa_close_key( target_handle ) );
2374
2375exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002376 psa_reset_key_attributes( &source_attributes );
2377 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002378 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002379 mbedtls_free( export_buffer );
2380}
2381/* END_CASE */
2382
2383/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002384void copy_fail( int source_usage_arg,
2385 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002386 int type_arg, data_t *material,
2387 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002388 int target_usage_arg,
2389 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002390 int expected_status_arg )
2391{
2392 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2393 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2394 psa_key_handle_t source_handle = 0;
2395 psa_key_handle_t target_handle = 0;
2396
2397 PSA_ASSERT( psa_crypto_init( ) );
2398
2399 /* Prepare the source key. */
2400 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2401 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002402 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002403 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002404 PSA_ASSERT( psa_import_key( &source_attributes,
2405 material->x, material->len,
2406 &source_handle ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002407
2408 /* Prepare the target attributes. */
2409 psa_set_key_type( &target_attributes, target_type_arg );
2410 psa_set_key_bits( &target_attributes, target_bits_arg );
2411 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2412 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002413 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002414
2415 /* Try to copy the key. */
2416 TEST_EQUAL( psa_copy_key( source_handle,
2417 &target_attributes, &target_handle ),
2418 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002419
2420 PSA_ASSERT( psa_destroy_key( source_handle ) );
2421
Gilles Peskine4a644642019-05-03 17:14:08 +02002422exit:
2423 psa_reset_key_attributes( &source_attributes );
2424 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002425 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002426}
2427/* END_CASE */
2428
2429/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002430void hash_operation_init( )
2431{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002432 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002433 /* Test each valid way of initializing the object, except for `= {0}`, as
2434 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2435 * though it's OK by the C standard. We could test for this, but we'd need
2436 * to supress the Clang warning for the test. */
2437 psa_hash_operation_t func = psa_hash_operation_init( );
2438 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2439 psa_hash_operation_t zero;
2440
2441 memset( &zero, 0, sizeof( zero ) );
2442
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002443 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002444 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2445 PSA_ERROR_BAD_STATE );
2446 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2447 PSA_ERROR_BAD_STATE );
2448 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2449 PSA_ERROR_BAD_STATE );
2450
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002451 /* A default hash operation should be abortable without error. */
2452 PSA_ASSERT( psa_hash_abort( &func ) );
2453 PSA_ASSERT( psa_hash_abort( &init ) );
2454 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002455}
2456/* END_CASE */
2457
2458/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002459void hash_setup( int alg_arg,
2460 int expected_status_arg )
2461{
2462 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002463 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002464 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002465 psa_status_t status;
2466
Gilles Peskine8817f612018-12-18 00:18:46 +01002467 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002468
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002469 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002470 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002471
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002472 /* Whether setup succeeded or failed, abort must succeed. */
2473 PSA_ASSERT( psa_hash_abort( &operation ) );
2474
2475 /* If setup failed, reproduce the failure, so as to
2476 * test the resulting state of the operation object. */
2477 if( status != PSA_SUCCESS )
2478 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2479
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002480 /* Now the operation object should be reusable. */
2481#if defined(KNOWN_SUPPORTED_HASH_ALG)
2482 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2483 PSA_ASSERT( psa_hash_abort( &operation ) );
2484#endif
2485
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002486exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002487 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002488}
2489/* END_CASE */
2490
2491/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002492void hash_compute_fail( int alg_arg, data_t *input,
2493 int output_size_arg, int expected_status_arg )
2494{
2495 psa_algorithm_t alg = alg_arg;
2496 uint8_t *output = NULL;
2497 size_t output_size = output_size_arg;
2498 size_t output_length = INVALID_EXPORT_LENGTH;
2499 psa_status_t expected_status = expected_status_arg;
2500 psa_status_t status;
2501
2502 ASSERT_ALLOC( output, output_size );
2503
2504 PSA_ASSERT( psa_crypto_init( ) );
2505
2506 status = psa_hash_compute( alg, input->x, input->len,
2507 output, output_size, &output_length );
2508 TEST_EQUAL( status, expected_status );
2509 TEST_ASSERT( output_length <= output_size );
2510
2511exit:
2512 mbedtls_free( output );
2513 PSA_DONE( );
2514}
2515/* END_CASE */
2516
2517/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002518void hash_compare_fail( int alg_arg, data_t *input,
2519 data_t *reference_hash,
2520 int expected_status_arg )
2521{
2522 psa_algorithm_t alg = alg_arg;
2523 psa_status_t expected_status = expected_status_arg;
2524 psa_status_t status;
2525
2526 PSA_ASSERT( psa_crypto_init( ) );
2527
2528 status = psa_hash_compare( alg, input->x, input->len,
2529 reference_hash->x, reference_hash->len );
2530 TEST_EQUAL( status, expected_status );
2531
2532exit:
2533 PSA_DONE( );
2534}
2535/* END_CASE */
2536
2537/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002538void hash_compute_compare( int alg_arg, data_t *input,
2539 data_t *expected_output )
2540{
2541 psa_algorithm_t alg = alg_arg;
2542 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2543 size_t output_length = INVALID_EXPORT_LENGTH;
2544 size_t i;
2545
2546 PSA_ASSERT( psa_crypto_init( ) );
2547
2548 /* Compute with tight buffer */
2549 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2550 output, PSA_HASH_SIZE( alg ),
2551 &output_length ) );
2552 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2553 ASSERT_COMPARE( output, output_length,
2554 expected_output->x, expected_output->len );
2555
2556 /* Compute with larger buffer */
2557 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2558 output, sizeof( output ),
2559 &output_length ) );
2560 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2561 ASSERT_COMPARE( output, output_length,
2562 expected_output->x, expected_output->len );
2563
2564 /* Compare with correct hash */
2565 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2566 output, output_length ) );
2567
2568 /* Compare with trailing garbage */
2569 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2570 output, output_length + 1 ),
2571 PSA_ERROR_INVALID_SIGNATURE );
2572
2573 /* Compare with truncated hash */
2574 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2575 output, output_length - 1 ),
2576 PSA_ERROR_INVALID_SIGNATURE );
2577
2578 /* Compare with corrupted value */
2579 for( i = 0; i < output_length; i++ )
2580 {
2581 test_set_step( i );
2582 output[i] ^= 1;
2583 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2584 output, output_length ),
2585 PSA_ERROR_INVALID_SIGNATURE );
2586 output[i] ^= 1;
2587 }
2588
2589exit:
2590 PSA_DONE( );
2591}
2592/* END_CASE */
2593
2594/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002595void hash_bad_order( )
2596{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002597 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002598 unsigned char input[] = "";
2599 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002600 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002601 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2602 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2603 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002604 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002605 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002606 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002607
Gilles Peskine8817f612018-12-18 00:18:46 +01002608 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002609
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002610 /* Call setup twice in a row. */
2611 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2612 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2613 PSA_ERROR_BAD_STATE );
2614 PSA_ASSERT( psa_hash_abort( &operation ) );
2615
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002616 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002617 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002618 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002619 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002620
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002621 /* Call update after finish. */
2622 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2623 PSA_ASSERT( psa_hash_finish( &operation,
2624 hash, sizeof( hash ), &hash_len ) );
2625 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002626 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002627 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002628
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002629 /* Call verify without calling setup beforehand. */
2630 TEST_EQUAL( psa_hash_verify( &operation,
2631 valid_hash, sizeof( valid_hash ) ),
2632 PSA_ERROR_BAD_STATE );
2633 PSA_ASSERT( psa_hash_abort( &operation ) );
2634
2635 /* Call verify after finish. */
2636 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2637 PSA_ASSERT( psa_hash_finish( &operation,
2638 hash, sizeof( hash ), &hash_len ) );
2639 TEST_EQUAL( psa_hash_verify( &operation,
2640 valid_hash, sizeof( valid_hash ) ),
2641 PSA_ERROR_BAD_STATE );
2642 PSA_ASSERT( psa_hash_abort( &operation ) );
2643
2644 /* Call verify twice in a row. */
2645 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2646 PSA_ASSERT( psa_hash_verify( &operation,
2647 valid_hash, sizeof( valid_hash ) ) );
2648 TEST_EQUAL( psa_hash_verify( &operation,
2649 valid_hash, sizeof( valid_hash ) ),
2650 PSA_ERROR_BAD_STATE );
2651 PSA_ASSERT( psa_hash_abort( &operation ) );
2652
2653 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002654 TEST_EQUAL( psa_hash_finish( &operation,
2655 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002656 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002657 PSA_ASSERT( psa_hash_abort( &operation ) );
2658
2659 /* Call finish twice in a row. */
2660 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2661 PSA_ASSERT( psa_hash_finish( &operation,
2662 hash, sizeof( hash ), &hash_len ) );
2663 TEST_EQUAL( psa_hash_finish( &operation,
2664 hash, sizeof( hash ), &hash_len ),
2665 PSA_ERROR_BAD_STATE );
2666 PSA_ASSERT( psa_hash_abort( &operation ) );
2667
2668 /* Call finish after calling verify. */
2669 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2670 PSA_ASSERT( psa_hash_verify( &operation,
2671 valid_hash, sizeof( valid_hash ) ) );
2672 TEST_EQUAL( psa_hash_finish( &operation,
2673 hash, sizeof( hash ), &hash_len ),
2674 PSA_ERROR_BAD_STATE );
2675 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002676
2677exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002678 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002679}
2680/* END_CASE */
2681
itayzafrir27e69452018-11-01 14:26:34 +02002682/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2683void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002684{
2685 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002686 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2687 * appended to it */
2688 unsigned char hash[] = {
2689 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2690 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2691 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002692 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002693 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002694
Gilles Peskine8817f612018-12-18 00:18:46 +01002695 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002696
itayzafrir27e69452018-11-01 14:26:34 +02002697 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002698 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002699 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002700 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002701
itayzafrir27e69452018-11-01 14:26:34 +02002702 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002703 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002704 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002705 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002706
itayzafrir27e69452018-11-01 14:26:34 +02002707 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002708 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002709 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002710 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002711
itayzafrirec93d302018-10-18 18:01:10 +03002712exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002713 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002714}
2715/* END_CASE */
2716
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002717/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2718void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002719{
2720 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002721 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002722 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002723 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002724 size_t hash_len;
2725
Gilles Peskine8817f612018-12-18 00:18:46 +01002726 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002727
itayzafrir58028322018-10-25 10:22:01 +03002728 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002729 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002730 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002731 hash, expected_size - 1, &hash_len ),
2732 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002733
2734exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002735 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002736}
2737/* END_CASE */
2738
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002739/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2740void hash_clone_source_state( )
2741{
2742 psa_algorithm_t alg = PSA_ALG_SHA_256;
2743 unsigned char hash[PSA_HASH_MAX_SIZE];
2744 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2745 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2746 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2747 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2748 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2749 size_t hash_len;
2750
2751 PSA_ASSERT( psa_crypto_init( ) );
2752 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2753
2754 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2755 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2756 PSA_ASSERT( psa_hash_finish( &op_finished,
2757 hash, sizeof( hash ), &hash_len ) );
2758 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2759 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2760
2761 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2762 PSA_ERROR_BAD_STATE );
2763
2764 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2765 PSA_ASSERT( psa_hash_finish( &op_init,
2766 hash, sizeof( hash ), &hash_len ) );
2767 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2768 PSA_ASSERT( psa_hash_finish( &op_finished,
2769 hash, sizeof( hash ), &hash_len ) );
2770 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2771 PSA_ASSERT( psa_hash_finish( &op_aborted,
2772 hash, sizeof( hash ), &hash_len ) );
2773
2774exit:
2775 psa_hash_abort( &op_source );
2776 psa_hash_abort( &op_init );
2777 psa_hash_abort( &op_setup );
2778 psa_hash_abort( &op_finished );
2779 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002780 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002781}
2782/* END_CASE */
2783
2784/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2785void hash_clone_target_state( )
2786{
2787 psa_algorithm_t alg = PSA_ALG_SHA_256;
2788 unsigned char hash[PSA_HASH_MAX_SIZE];
2789 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2790 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2791 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2792 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2793 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2794 size_t hash_len;
2795
2796 PSA_ASSERT( psa_crypto_init( ) );
2797
2798 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2799 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2800 PSA_ASSERT( psa_hash_finish( &op_finished,
2801 hash, sizeof( hash ), &hash_len ) );
2802 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2803 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2804
2805 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2806 PSA_ASSERT( psa_hash_finish( &op_target,
2807 hash, sizeof( hash ), &hash_len ) );
2808
2809 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2810 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2811 PSA_ERROR_BAD_STATE );
2812 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2813 PSA_ERROR_BAD_STATE );
2814
2815exit:
2816 psa_hash_abort( &op_target );
2817 psa_hash_abort( &op_init );
2818 psa_hash_abort( &op_setup );
2819 psa_hash_abort( &op_finished );
2820 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002821 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002822}
2823/* END_CASE */
2824
itayzafrir58028322018-10-25 10:22:01 +03002825/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002826void mac_operation_init( )
2827{
Jaeden Amero252ef282019-02-15 14:05:35 +00002828 const uint8_t input[1] = { 0 };
2829
Jaeden Amero769ce272019-01-04 11:48:03 +00002830 /* Test each valid way of initializing the object, except for `= {0}`, as
2831 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2832 * though it's OK by the C standard. We could test for this, but we'd need
2833 * to supress the Clang warning for the test. */
2834 psa_mac_operation_t func = psa_mac_operation_init( );
2835 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2836 psa_mac_operation_t zero;
2837
2838 memset( &zero, 0, sizeof( zero ) );
2839
Jaeden Amero252ef282019-02-15 14:05:35 +00002840 /* A freshly-initialized MAC operation should not be usable. */
2841 TEST_EQUAL( psa_mac_update( &func,
2842 input, sizeof( input ) ),
2843 PSA_ERROR_BAD_STATE );
2844 TEST_EQUAL( psa_mac_update( &init,
2845 input, sizeof( input ) ),
2846 PSA_ERROR_BAD_STATE );
2847 TEST_EQUAL( psa_mac_update( &zero,
2848 input, sizeof( input ) ),
2849 PSA_ERROR_BAD_STATE );
2850
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002851 /* A default MAC operation should be abortable without error. */
2852 PSA_ASSERT( psa_mac_abort( &func ) );
2853 PSA_ASSERT( psa_mac_abort( &init ) );
2854 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002855}
2856/* END_CASE */
2857
2858/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002859void mac_setup( int key_type_arg,
2860 data_t *key,
2861 int alg_arg,
2862 int expected_status_arg )
2863{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002864 psa_key_type_t key_type = key_type_arg;
2865 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002866 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002867 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002868 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2869#if defined(KNOWN_SUPPORTED_MAC_ALG)
2870 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2871#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002872
Gilles Peskine8817f612018-12-18 00:18:46 +01002873 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002874
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002875 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2876 &operation, &status ) )
2877 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002878 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002879
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002880 /* The operation object should be reusable. */
2881#if defined(KNOWN_SUPPORTED_MAC_ALG)
2882 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2883 smoke_test_key_data,
2884 sizeof( smoke_test_key_data ),
2885 KNOWN_SUPPORTED_MAC_ALG,
2886 &operation, &status ) )
2887 goto exit;
2888 TEST_EQUAL( status, PSA_SUCCESS );
2889#endif
2890
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002891exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002892 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002893}
2894/* END_CASE */
2895
2896/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002897void mac_bad_order( )
2898{
2899 psa_key_handle_t handle = 0;
2900 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2901 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2902 const uint8_t key[] = {
2903 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2904 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2905 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002906 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002907 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2908 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2909 size_t sign_mac_length = 0;
2910 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2911 const uint8_t verify_mac[] = {
2912 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2913 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2914 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2915
2916 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002917 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002918 psa_set_key_algorithm( &attributes, alg );
2919 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002920
Gilles Peskine73676cb2019-05-15 20:15:10 +02002921 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002922
Jaeden Amero252ef282019-02-15 14:05:35 +00002923 /* Call update without calling setup beforehand. */
2924 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2925 PSA_ERROR_BAD_STATE );
2926 PSA_ASSERT( psa_mac_abort( &operation ) );
2927
2928 /* Call sign finish without calling setup beforehand. */
2929 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2930 &sign_mac_length),
2931 PSA_ERROR_BAD_STATE );
2932 PSA_ASSERT( psa_mac_abort( &operation ) );
2933
2934 /* Call verify finish without calling setup beforehand. */
2935 TEST_EQUAL( psa_mac_verify_finish( &operation,
2936 verify_mac, sizeof( verify_mac ) ),
2937 PSA_ERROR_BAD_STATE );
2938 PSA_ASSERT( psa_mac_abort( &operation ) );
2939
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002940 /* Call setup twice in a row. */
2941 PSA_ASSERT( psa_mac_sign_setup( &operation,
2942 handle, alg ) );
2943 TEST_EQUAL( psa_mac_sign_setup( &operation,
2944 handle, alg ),
2945 PSA_ERROR_BAD_STATE );
2946 PSA_ASSERT( psa_mac_abort( &operation ) );
2947
Jaeden Amero252ef282019-02-15 14:05:35 +00002948 /* Call update after sign finish. */
2949 PSA_ASSERT( psa_mac_sign_setup( &operation,
2950 handle, alg ) );
2951 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2952 PSA_ASSERT( psa_mac_sign_finish( &operation,
2953 sign_mac, sizeof( sign_mac ),
2954 &sign_mac_length ) );
2955 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2956 PSA_ERROR_BAD_STATE );
2957 PSA_ASSERT( psa_mac_abort( &operation ) );
2958
2959 /* Call update after verify finish. */
2960 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002961 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002962 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2963 PSA_ASSERT( psa_mac_verify_finish( &operation,
2964 verify_mac, sizeof( verify_mac ) ) );
2965 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2966 PSA_ERROR_BAD_STATE );
2967 PSA_ASSERT( psa_mac_abort( &operation ) );
2968
2969 /* Call sign finish twice in a row. */
2970 PSA_ASSERT( psa_mac_sign_setup( &operation,
2971 handle, alg ) );
2972 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2973 PSA_ASSERT( psa_mac_sign_finish( &operation,
2974 sign_mac, sizeof( sign_mac ),
2975 &sign_mac_length ) );
2976 TEST_EQUAL( psa_mac_sign_finish( &operation,
2977 sign_mac, sizeof( sign_mac ),
2978 &sign_mac_length ),
2979 PSA_ERROR_BAD_STATE );
2980 PSA_ASSERT( psa_mac_abort( &operation ) );
2981
2982 /* Call verify finish twice in a row. */
2983 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002984 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002985 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2986 PSA_ASSERT( psa_mac_verify_finish( &operation,
2987 verify_mac, sizeof( verify_mac ) ) );
2988 TEST_EQUAL( psa_mac_verify_finish( &operation,
2989 verify_mac, sizeof( verify_mac ) ),
2990 PSA_ERROR_BAD_STATE );
2991 PSA_ASSERT( psa_mac_abort( &operation ) );
2992
2993 /* Setup sign but try verify. */
2994 PSA_ASSERT( psa_mac_sign_setup( &operation,
2995 handle, alg ) );
2996 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2997 TEST_EQUAL( psa_mac_verify_finish( &operation,
2998 verify_mac, sizeof( verify_mac ) ),
2999 PSA_ERROR_BAD_STATE );
3000 PSA_ASSERT( psa_mac_abort( &operation ) );
3001
3002 /* Setup verify but try sign. */
3003 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003004 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003005 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3006 TEST_EQUAL( psa_mac_sign_finish( &operation,
3007 sign_mac, sizeof( sign_mac ),
3008 &sign_mac_length ),
3009 PSA_ERROR_BAD_STATE );
3010 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003011
Gilles Peskine76b29a72019-05-28 14:08:50 +02003012 PSA_ASSERT( psa_destroy_key( handle ) );
3013
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003014exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003015 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003016}
3017/* END_CASE */
3018
3019/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003020void mac_sign( int key_type_arg,
3021 data_t *key,
3022 int alg_arg,
3023 data_t *input,
3024 data_t *expected_mac )
3025{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003026 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003027 psa_key_type_t key_type = key_type_arg;
3028 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003029 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003030 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003031 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003032 size_t mac_buffer_size =
3033 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
3034 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003035 const size_t output_sizes_to_test[] = {
3036 0,
3037 1,
3038 expected_mac->len - 1,
3039 expected_mac->len,
3040 expected_mac->len + 1,
3041 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003042
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003043 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
Gilles Peskine3d404d62020-08-25 23:47:36 +02003044 /* We expect PSA_MAC_FINAL_SIZE to be exact. */
3045 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003046
Gilles Peskine8817f612018-12-18 00:18:46 +01003047 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003048
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003049 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003050 psa_set_key_algorithm( &attributes, alg );
3051 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003052
Gilles Peskine73676cb2019-05-15 20:15:10 +02003053 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003054
Gilles Peskine8b356b52020-08-25 23:44:59 +02003055 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3056 {
3057 const size_t output_size = output_sizes_to_test[i];
3058 psa_status_t expected_status =
3059 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3060 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003061
Gilles Peskine8b356b52020-08-25 23:44:59 +02003062 test_set_step( output_size );
3063 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003064
Gilles Peskine8b356b52020-08-25 23:44:59 +02003065 /* Calculate the MAC. */
3066 PSA_ASSERT( psa_mac_sign_setup( &operation,
3067 handle, alg ) );
3068 PSA_ASSERT( psa_mac_update( &operation,
3069 input->x, input->len ) );
3070 TEST_EQUAL( psa_mac_sign_finish( &operation,
3071 actual_mac, output_size,
3072 &mac_length ),
3073 expected_status );
3074 PSA_ASSERT( psa_mac_abort( &operation ) );
3075
3076 if( expected_status == PSA_SUCCESS )
3077 {
3078 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3079 actual_mac, mac_length );
3080 }
3081 mbedtls_free( actual_mac );
3082 actual_mac = NULL;
3083 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003084
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003085exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003086 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003087 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003088 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003089 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003090}
3091/* END_CASE */
3092
3093/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003094void mac_verify( int key_type_arg,
3095 data_t *key,
3096 int alg_arg,
3097 data_t *input,
3098 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003099{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003100 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003101 psa_key_type_t key_type = key_type_arg;
3102 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003103 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003104 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003105 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003106
Gilles Peskine69c12672018-06-28 00:07:19 +02003107 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3108
Gilles Peskine8817f612018-12-18 00:18:46 +01003109 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003110
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003111 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003112 psa_set_key_algorithm( &attributes, alg );
3113 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003114
Gilles Peskine73676cb2019-05-15 20:15:10 +02003115 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003116
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003117 /* Test the correct MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003118 PSA_ASSERT( psa_mac_verify_setup( &operation,
3119 handle, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003120 PSA_ASSERT( psa_mac_update( &operation,
3121 input->x, input->len ) );
3122 PSA_ASSERT( psa_mac_verify_finish( &operation,
3123 expected_mac->x,
3124 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003125
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003126 /* Test a MAC that's too short. */
3127 PSA_ASSERT( psa_mac_verify_setup( &operation,
3128 handle, alg ) );
3129 PSA_ASSERT( psa_mac_update( &operation,
3130 input->x, input->len ) );
3131 TEST_EQUAL( psa_mac_verify_finish( &operation,
3132 expected_mac->x,
3133 expected_mac->len - 1 ),
3134 PSA_ERROR_INVALID_SIGNATURE );
3135
3136 /* Test a MAC that's too long. */
3137 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3138 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
3139 PSA_ASSERT( psa_mac_verify_setup( &operation,
3140 handle, alg ) );
3141 PSA_ASSERT( psa_mac_update( &operation,
3142 input->x, input->len ) );
3143 TEST_EQUAL( psa_mac_verify_finish( &operation,
3144 perturbed_mac,
3145 expected_mac->len + 1 ),
3146 PSA_ERROR_INVALID_SIGNATURE );
3147
3148 /* Test changing one byte. */
3149 for( size_t i = 0; i < expected_mac->len; i++ )
3150 {
3151 test_set_step( i );
3152 perturbed_mac[i] ^= 1;
3153 PSA_ASSERT( psa_mac_verify_setup( &operation,
3154 handle, alg ) );
3155 PSA_ASSERT( psa_mac_update( &operation,
3156 input->x, input->len ) );
3157 TEST_EQUAL( psa_mac_verify_finish( &operation,
3158 perturbed_mac,
3159 expected_mac->len ),
3160 PSA_ERROR_INVALID_SIGNATURE );
3161 perturbed_mac[i] ^= 1;
3162 }
3163
Gilles Peskine8c9def32018-02-08 10:02:12 +01003164exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003165 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003166 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003167 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003168 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003169}
3170/* END_CASE */
3171
3172/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003173void cipher_operation_init( )
3174{
Jaeden Ameroab439972019-02-15 14:12:05 +00003175 const uint8_t input[1] = { 0 };
3176 unsigned char output[1] = { 0 };
3177 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003178 /* Test each valid way of initializing the object, except for `= {0}`, as
3179 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3180 * though it's OK by the C standard. We could test for this, but we'd need
3181 * to supress the Clang warning for the test. */
3182 psa_cipher_operation_t func = psa_cipher_operation_init( );
3183 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3184 psa_cipher_operation_t zero;
3185
3186 memset( &zero, 0, sizeof( zero ) );
3187
Jaeden Ameroab439972019-02-15 14:12:05 +00003188 /* A freshly-initialized cipher operation should not be usable. */
3189 TEST_EQUAL( psa_cipher_update( &func,
3190 input, sizeof( input ),
3191 output, sizeof( output ),
3192 &output_length ),
3193 PSA_ERROR_BAD_STATE );
3194 TEST_EQUAL( psa_cipher_update( &init,
3195 input, sizeof( input ),
3196 output, sizeof( output ),
3197 &output_length ),
3198 PSA_ERROR_BAD_STATE );
3199 TEST_EQUAL( psa_cipher_update( &zero,
3200 input, sizeof( input ),
3201 output, sizeof( output ),
3202 &output_length ),
3203 PSA_ERROR_BAD_STATE );
3204
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003205 /* A default cipher operation should be abortable without error. */
3206 PSA_ASSERT( psa_cipher_abort( &func ) );
3207 PSA_ASSERT( psa_cipher_abort( &init ) );
3208 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003209}
3210/* END_CASE */
3211
3212/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003213void cipher_setup( int key_type_arg,
3214 data_t *key,
3215 int alg_arg,
3216 int expected_status_arg )
3217{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003218 psa_key_type_t key_type = key_type_arg;
3219 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003220 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003221 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003222 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003223#if defined(KNOWN_SUPPORTED_MAC_ALG)
3224 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3225#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003226
Gilles Peskine8817f612018-12-18 00:18:46 +01003227 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003228
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003229 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3230 &operation, &status ) )
3231 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003232 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003233
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003234 /* The operation object should be reusable. */
3235#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3236 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3237 smoke_test_key_data,
3238 sizeof( smoke_test_key_data ),
3239 KNOWN_SUPPORTED_CIPHER_ALG,
3240 &operation, &status ) )
3241 goto exit;
3242 TEST_EQUAL( status, PSA_SUCCESS );
3243#endif
3244
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003245exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003246 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003247 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003248}
3249/* END_CASE */
3250
3251/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00003252void cipher_bad_order( )
3253{
3254 psa_key_handle_t handle = 0;
3255 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3256 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003257 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003258 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3259 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3260 const uint8_t key[] = {
3261 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3262 0xaa, 0xaa, 0xaa, 0xaa };
3263 const uint8_t text[] = {
3264 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3265 0xbb, 0xbb, 0xbb, 0xbb };
3266 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3267 size_t length = 0;
3268
3269 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003270 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3271 psa_set_key_algorithm( &attributes, alg );
3272 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02003273 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003274
3275
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003276 /* Call encrypt setup twice in a row. */
3277 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3278 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
3279 PSA_ERROR_BAD_STATE );
3280 PSA_ASSERT( psa_cipher_abort( &operation ) );
3281
3282 /* Call decrypt setup twice in a row. */
3283 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
3284 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
3285 PSA_ERROR_BAD_STATE );
3286 PSA_ASSERT( psa_cipher_abort( &operation ) );
3287
Jaeden Ameroab439972019-02-15 14:12:05 +00003288 /* Generate an IV without calling setup beforehand. */
3289 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3290 buffer, sizeof( buffer ),
3291 &length ),
3292 PSA_ERROR_BAD_STATE );
3293 PSA_ASSERT( psa_cipher_abort( &operation ) );
3294
3295 /* Generate an IV twice in a row. */
3296 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3297 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3298 buffer, sizeof( buffer ),
3299 &length ) );
3300 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3301 buffer, sizeof( buffer ),
3302 &length ),
3303 PSA_ERROR_BAD_STATE );
3304 PSA_ASSERT( psa_cipher_abort( &operation ) );
3305
3306 /* Generate an IV after it's already set. */
3307 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3308 PSA_ASSERT( psa_cipher_set_iv( &operation,
3309 iv, sizeof( iv ) ) );
3310 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3311 buffer, sizeof( buffer ),
3312 &length ),
3313 PSA_ERROR_BAD_STATE );
3314 PSA_ASSERT( psa_cipher_abort( &operation ) );
3315
3316 /* Set an IV without calling setup beforehand. */
3317 TEST_EQUAL( psa_cipher_set_iv( &operation,
3318 iv, sizeof( iv ) ),
3319 PSA_ERROR_BAD_STATE );
3320 PSA_ASSERT( psa_cipher_abort( &operation ) );
3321
3322 /* Set an IV after it's already set. */
3323 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3324 PSA_ASSERT( psa_cipher_set_iv( &operation,
3325 iv, sizeof( iv ) ) );
3326 TEST_EQUAL( psa_cipher_set_iv( &operation,
3327 iv, sizeof( iv ) ),
3328 PSA_ERROR_BAD_STATE );
3329 PSA_ASSERT( psa_cipher_abort( &operation ) );
3330
3331 /* Set an IV after it's already generated. */
3332 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3333 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3334 buffer, sizeof( buffer ),
3335 &length ) );
3336 TEST_EQUAL( psa_cipher_set_iv( &operation,
3337 iv, sizeof( iv ) ),
3338 PSA_ERROR_BAD_STATE );
3339 PSA_ASSERT( psa_cipher_abort( &operation ) );
3340
3341 /* Call update without calling setup beforehand. */
3342 TEST_EQUAL( psa_cipher_update( &operation,
3343 text, sizeof( text ),
3344 buffer, sizeof( buffer ),
3345 &length ),
3346 PSA_ERROR_BAD_STATE );
3347 PSA_ASSERT( psa_cipher_abort( &operation ) );
3348
3349 /* Call update without an IV where an IV is required. */
3350 TEST_EQUAL( psa_cipher_update( &operation,
3351 text, sizeof( text ),
3352 buffer, sizeof( buffer ),
3353 &length ),
3354 PSA_ERROR_BAD_STATE );
3355 PSA_ASSERT( psa_cipher_abort( &operation ) );
3356
3357 /* Call update after finish. */
3358 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3359 PSA_ASSERT( psa_cipher_set_iv( &operation,
3360 iv, sizeof( iv ) ) );
3361 PSA_ASSERT( psa_cipher_finish( &operation,
3362 buffer, sizeof( buffer ), &length ) );
3363 TEST_EQUAL( psa_cipher_update( &operation,
3364 text, sizeof( text ),
3365 buffer, sizeof( buffer ),
3366 &length ),
3367 PSA_ERROR_BAD_STATE );
3368 PSA_ASSERT( psa_cipher_abort( &operation ) );
3369
3370 /* Call finish without calling setup beforehand. */
3371 TEST_EQUAL( psa_cipher_finish( &operation,
3372 buffer, sizeof( buffer ), &length ),
3373 PSA_ERROR_BAD_STATE );
3374 PSA_ASSERT( psa_cipher_abort( &operation ) );
3375
3376 /* Call finish without an IV where an IV is required. */
3377 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3378 /* Not calling update means we are encrypting an empty buffer, which is OK
3379 * for cipher modes with padding. */
3380 TEST_EQUAL( psa_cipher_finish( &operation,
3381 buffer, sizeof( buffer ), &length ),
3382 PSA_ERROR_BAD_STATE );
3383 PSA_ASSERT( psa_cipher_abort( &operation ) );
3384
3385 /* Call finish twice in a row. */
3386 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3387 PSA_ASSERT( psa_cipher_set_iv( &operation,
3388 iv, sizeof( iv ) ) );
3389 PSA_ASSERT( psa_cipher_finish( &operation,
3390 buffer, sizeof( buffer ), &length ) );
3391 TEST_EQUAL( psa_cipher_finish( &operation,
3392 buffer, sizeof( buffer ), &length ),
3393 PSA_ERROR_BAD_STATE );
3394 PSA_ASSERT( psa_cipher_abort( &operation ) );
3395
Gilles Peskine76b29a72019-05-28 14:08:50 +02003396 PSA_ASSERT( psa_destroy_key( handle ) );
3397
Jaeden Ameroab439972019-02-15 14:12:05 +00003398exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003399 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003400 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003401}
3402/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003403
Gilles Peskine50e586b2018-06-08 14:28:46 +02003404/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003405void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003406 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003407 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003408 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003409{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003410 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003411 psa_status_t status;
3412 psa_key_type_t key_type = key_type_arg;
3413 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003414 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003415 unsigned char *output = NULL;
3416 size_t output_buffer_size = 0;
3417 size_t function_output_length = 0;
3418 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003419 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003420 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003421
Gilles Peskine8817f612018-12-18 00:18:46 +01003422 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003423
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003424 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3425 psa_set_key_algorithm( &attributes, alg );
3426 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003427
Gilles Peskine73676cb2019-05-15 20:15:10 +02003428 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003429
Gilles Peskine8817f612018-12-18 00:18:46 +01003430 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3431 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003432
Gilles Peskine423005e2019-05-06 15:22:57 +02003433 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003434 output_buffer_size = ( (size_t) input->len +
3435 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003436 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003437
Gilles Peskine8817f612018-12-18 00:18:46 +01003438 PSA_ASSERT( psa_cipher_update( &operation,
3439 input->x, input->len,
3440 output, output_buffer_size,
3441 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003442 total_output_length += function_output_length;
3443 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003444 output + total_output_length,
3445 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003446 &function_output_length );
3447 total_output_length += function_output_length;
3448
Gilles Peskinefe11b722018-12-18 00:24:04 +01003449 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003450 if( expected_status == PSA_SUCCESS )
3451 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003452 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003453 ASSERT_COMPARE( expected_output->x, expected_output->len,
3454 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003455 }
3456
3457exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003458 psa_cipher_abort( &operation );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003459 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003460 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003461 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003462}
3463/* END_CASE */
3464
3465/* BEGIN_CASE */
3466void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003467 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003468 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003469 int first_part_size_arg,
3470 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003471 data_t *expected_output )
3472{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003473 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003474 psa_key_type_t key_type = key_type_arg;
3475 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003476 size_t first_part_size = first_part_size_arg;
3477 size_t output1_length = output1_length_arg;
3478 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003479 unsigned char *output = NULL;
3480 size_t output_buffer_size = 0;
3481 size_t function_output_length = 0;
3482 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003483 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003484 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003485
Gilles Peskine8817f612018-12-18 00:18:46 +01003486 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003487
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003488 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3489 psa_set_key_algorithm( &attributes, alg );
3490 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003491
Gilles Peskine73676cb2019-05-15 20:15:10 +02003492 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003493
Gilles Peskine8817f612018-12-18 00:18:46 +01003494 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3495 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003496
Gilles Peskine423005e2019-05-06 15:22:57 +02003497 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003498 output_buffer_size = ( (size_t) input->len +
3499 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003500 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003501
Gilles Peskinee0866522019-02-19 19:44:00 +01003502 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003503 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3504 output, output_buffer_size,
3505 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003506 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003507 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003508 PSA_ASSERT( psa_cipher_update( &operation,
3509 input->x + first_part_size,
3510 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003511 output + total_output_length,
3512 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003513 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003514 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003515 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003516 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003517 output + total_output_length,
3518 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003519 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003520 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003521 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003522
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003523 ASSERT_COMPARE( expected_output->x, expected_output->len,
3524 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003525
3526exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003527 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003528 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003529 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003530 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003531}
3532/* END_CASE */
3533
3534/* BEGIN_CASE */
3535void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003536 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003537 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003538 int first_part_size_arg,
3539 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003540 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003541{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003542 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003543
3544 psa_key_type_t key_type = key_type_arg;
3545 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003546 size_t first_part_size = first_part_size_arg;
3547 size_t output1_length = output1_length_arg;
3548 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003549 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003550 size_t output_buffer_size = 0;
3551 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003552 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003553 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003554 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003555
Gilles Peskine8817f612018-12-18 00:18:46 +01003556 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003557
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003558 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3559 psa_set_key_algorithm( &attributes, alg );
3560 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003561
Gilles Peskine73676cb2019-05-15 20:15:10 +02003562 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003563
Gilles Peskine8817f612018-12-18 00:18:46 +01003564 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3565 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003566
Gilles Peskine423005e2019-05-06 15:22:57 +02003567 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003568
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003569 output_buffer_size = ( (size_t) input->len +
3570 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003571 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003572
Gilles Peskinee0866522019-02-19 19:44:00 +01003573 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003574 PSA_ASSERT( psa_cipher_update( &operation,
3575 input->x, first_part_size,
3576 output, output_buffer_size,
3577 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003578 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003579 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003580 PSA_ASSERT( psa_cipher_update( &operation,
3581 input->x + first_part_size,
3582 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003583 output + total_output_length,
3584 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003585 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003586 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003587 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003588 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003589 output + total_output_length,
3590 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003591 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003592 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003593 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003594
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003595 ASSERT_COMPARE( expected_output->x, expected_output->len,
3596 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003597
3598exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003599 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003600 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003601 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003602 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003603}
3604/* END_CASE */
3605
Gilles Peskine50e586b2018-06-08 14:28:46 +02003606/* BEGIN_CASE */
3607void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003608 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003609 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003610 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003611{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003612 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003613 psa_status_t status;
3614 psa_key_type_t key_type = key_type_arg;
3615 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003616 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003617 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003618 size_t output_buffer_size = 0;
3619 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003620 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003621 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003622 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003623
Gilles Peskine8817f612018-12-18 00:18:46 +01003624 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003625
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003626 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3627 psa_set_key_algorithm( &attributes, alg );
3628 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003629
Gilles Peskine73676cb2019-05-15 20:15:10 +02003630 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003631
Gilles Peskine8817f612018-12-18 00:18:46 +01003632 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3633 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003634
Gilles Peskine423005e2019-05-06 15:22:57 +02003635 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003636
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003637 output_buffer_size = ( (size_t) input->len +
3638 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003639 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003640
Gilles Peskine8817f612018-12-18 00:18:46 +01003641 PSA_ASSERT( psa_cipher_update( &operation,
3642 input->x, input->len,
3643 output, output_buffer_size,
3644 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003645 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003646 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003647 output + total_output_length,
3648 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003649 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003650 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003651 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003652
3653 if( expected_status == PSA_SUCCESS )
3654 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003655 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003656 ASSERT_COMPARE( expected_output->x, expected_output->len,
3657 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003658 }
3659
Gilles Peskine50e586b2018-06-08 14:28:46 +02003660exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003661 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003662 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003663 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003664 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003665}
3666/* END_CASE */
3667
Gilles Peskine50e586b2018-06-08 14:28:46 +02003668/* BEGIN_CASE */
3669void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003670 data_t *key,
3671 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003672{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003673 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003674 psa_key_type_t key_type = key_type_arg;
3675 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003676 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003677 size_t iv_size = 16;
3678 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003679 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003680 size_t output1_size = 0;
3681 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003682 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003683 size_t output2_size = 0;
3684 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003685 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003686 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3687 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003688 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003689
Gilles Peskine8817f612018-12-18 00:18:46 +01003690 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003691
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003692 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3693 psa_set_key_algorithm( &attributes, alg );
3694 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003695
Gilles Peskine73676cb2019-05-15 20:15:10 +02003696 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003697
Gilles Peskine8817f612018-12-18 00:18:46 +01003698 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3699 handle, alg ) );
3700 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3701 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003702
Gilles Peskine8817f612018-12-18 00:18:46 +01003703 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3704 iv, iv_size,
3705 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003706 output1_size = ( (size_t) input->len +
3707 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003708 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003709
Gilles Peskine8817f612018-12-18 00:18:46 +01003710 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3711 output1, output1_size,
3712 &output1_length ) );
3713 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003714 output1 + output1_length,
3715 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003716 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003717
Gilles Peskine048b7f02018-06-08 14:20:49 +02003718 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003719
Gilles Peskine8817f612018-12-18 00:18:46 +01003720 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003721
3722 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003723 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003724
Gilles Peskine8817f612018-12-18 00:18:46 +01003725 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3726 iv, iv_length ) );
3727 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3728 output2, output2_size,
3729 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003730 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003731 PSA_ASSERT( psa_cipher_finish( &operation2,
3732 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003733 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003734 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003735
Gilles Peskine048b7f02018-06-08 14:20:49 +02003736 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003737
Gilles Peskine8817f612018-12-18 00:18:46 +01003738 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003739
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003740 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003741
3742exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003743 psa_cipher_abort( &operation1 );
3744 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003745 mbedtls_free( output1 );
3746 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003747 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003748 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003749}
3750/* END_CASE */
3751
3752/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003753void cipher_verify_output_multipart( int alg_arg,
3754 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003755 data_t *key,
3756 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003757 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003758{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003759 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003760 psa_key_type_t key_type = key_type_arg;
3761 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003762 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003763 unsigned char iv[16] = {0};
3764 size_t iv_size = 16;
3765 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003766 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003767 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003768 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003769 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003770 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003771 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003772 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003773 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3774 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003775 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003776
Gilles Peskine8817f612018-12-18 00:18:46 +01003777 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003778
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003779 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3780 psa_set_key_algorithm( &attributes, alg );
3781 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003782
Gilles Peskine73676cb2019-05-15 20:15:10 +02003783 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003784
Gilles Peskine8817f612018-12-18 00:18:46 +01003785 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3786 handle, alg ) );
3787 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3788 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003789
Gilles Peskine8817f612018-12-18 00:18:46 +01003790 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3791 iv, iv_size,
3792 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003793 output1_buffer_size = ( (size_t) input->len +
3794 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003795 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003796
Gilles Peskinee0866522019-02-19 19:44:00 +01003797 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003798
Gilles Peskine8817f612018-12-18 00:18:46 +01003799 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3800 output1, output1_buffer_size,
3801 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003802 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003803
Gilles Peskine8817f612018-12-18 00:18:46 +01003804 PSA_ASSERT( psa_cipher_update( &operation1,
3805 input->x + first_part_size,
3806 input->len - first_part_size,
3807 output1, output1_buffer_size,
3808 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003809 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003810
Gilles Peskine8817f612018-12-18 00:18:46 +01003811 PSA_ASSERT( psa_cipher_finish( &operation1,
3812 output1 + output1_length,
3813 output1_buffer_size - output1_length,
3814 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003815 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003816
Gilles Peskine8817f612018-12-18 00:18:46 +01003817 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003818
Gilles Peskine048b7f02018-06-08 14:20:49 +02003819 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003820 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003821
Gilles Peskine8817f612018-12-18 00:18:46 +01003822 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3823 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003824
Gilles Peskine8817f612018-12-18 00:18:46 +01003825 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3826 output2, output2_buffer_size,
3827 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003828 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003829
Gilles Peskine8817f612018-12-18 00:18:46 +01003830 PSA_ASSERT( psa_cipher_update( &operation2,
3831 output1 + first_part_size,
3832 output1_length - first_part_size,
3833 output2, output2_buffer_size,
3834 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003835 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003836
Gilles Peskine8817f612018-12-18 00:18:46 +01003837 PSA_ASSERT( psa_cipher_finish( &operation2,
3838 output2 + output2_length,
3839 output2_buffer_size - output2_length,
3840 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003841 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003842
Gilles Peskine8817f612018-12-18 00:18:46 +01003843 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003844
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003845 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003846
3847exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003848 psa_cipher_abort( &operation1 );
3849 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003850 mbedtls_free( output1 );
3851 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003852 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003853 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003854}
3855/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003856
Gilles Peskine20035e32018-02-03 22:44:14 +01003857/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003858void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003859 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003860 data_t *nonce,
3861 data_t *additional_data,
3862 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003863 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003864{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003865 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003866 psa_key_type_t key_type = key_type_arg;
3867 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003868 unsigned char *output_data = NULL;
3869 size_t output_size = 0;
3870 size_t output_length = 0;
3871 unsigned char *output_data2 = NULL;
3872 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003873 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003874 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003875 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003876
Gilles Peskine4abf7412018-06-18 16:35:34 +02003877 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003878 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3879 * should be exact. */
3880 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3881 TEST_EQUAL( output_size,
3882 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003883 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003884
Gilles Peskine8817f612018-12-18 00:18:46 +01003885 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003886
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003887 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3888 psa_set_key_algorithm( &attributes, alg );
3889 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003890
Gilles Peskine049c7532019-05-15 20:22:09 +02003891 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3892 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003893
Gilles Peskinefe11b722018-12-18 00:24:04 +01003894 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3895 nonce->x, nonce->len,
3896 additional_data->x,
3897 additional_data->len,
3898 input_data->x, input_data->len,
3899 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003900 &output_length ),
3901 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003902
3903 if( PSA_SUCCESS == expected_result )
3904 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003905 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003906
Gilles Peskine003a4a92019-05-14 16:09:40 +02003907 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3908 * should be exact. */
3909 TEST_EQUAL( input_data->len,
3910 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3911
Gilles Peskinefe11b722018-12-18 00:24:04 +01003912 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3913 nonce->x, nonce->len,
3914 additional_data->x,
3915 additional_data->len,
3916 output_data, output_length,
3917 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003918 &output_length2 ),
3919 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003920
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003921 ASSERT_COMPARE( input_data->x, input_data->len,
3922 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003923 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003924
Gilles Peskinea1cac842018-06-11 19:33:02 +02003925exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003926 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003927 mbedtls_free( output_data );
3928 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003929 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003930}
3931/* END_CASE */
3932
3933/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003934void aead_encrypt( int key_type_arg, data_t *key_data,
3935 int alg_arg,
3936 data_t *nonce,
3937 data_t *additional_data,
3938 data_t *input_data,
3939 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003940{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003941 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003942 psa_key_type_t key_type = key_type_arg;
3943 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003944 unsigned char *output_data = NULL;
3945 size_t output_size = 0;
3946 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003947 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003948 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003949
Gilles Peskine4abf7412018-06-18 16:35:34 +02003950 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003951 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3952 * should be exact. */
3953 TEST_EQUAL( output_size,
3954 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003955 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003956
Gilles Peskine8817f612018-12-18 00:18:46 +01003957 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003958
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003959 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3960 psa_set_key_algorithm( &attributes, alg );
3961 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003962
Gilles Peskine049c7532019-05-15 20:22:09 +02003963 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3964 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003965
Gilles Peskine8817f612018-12-18 00:18:46 +01003966 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3967 nonce->x, nonce->len,
3968 additional_data->x, additional_data->len,
3969 input_data->x, input_data->len,
3970 output_data, output_size,
3971 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003972
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003973 ASSERT_COMPARE( expected_result->x, expected_result->len,
3974 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003975
Gilles Peskinea1cac842018-06-11 19:33:02 +02003976exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003977 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003978 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003979 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003980}
3981/* END_CASE */
3982
3983/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003984void aead_decrypt( int key_type_arg, data_t *key_data,
3985 int alg_arg,
3986 data_t *nonce,
3987 data_t *additional_data,
3988 data_t *input_data,
3989 data_t *expected_data,
3990 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003991{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003992 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003993 psa_key_type_t key_type = key_type_arg;
3994 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003995 unsigned char *output_data = NULL;
3996 size_t output_size = 0;
3997 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003998 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003999 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004000 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004001
Gilles Peskine003a4a92019-05-14 16:09:40 +02004002 output_size = input_data->len - tag_length;
4003 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4004 * should be exact. */
4005 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4006 TEST_EQUAL( output_size,
4007 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004008 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004009
Gilles Peskine8817f612018-12-18 00:18:46 +01004010 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004011
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004012 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4013 psa_set_key_algorithm( &attributes, alg );
4014 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004015
Gilles Peskine049c7532019-05-15 20:22:09 +02004016 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4017 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004018
Gilles Peskinefe11b722018-12-18 00:24:04 +01004019 TEST_EQUAL( psa_aead_decrypt( handle, alg,
4020 nonce->x, nonce->len,
4021 additional_data->x,
4022 additional_data->len,
4023 input_data->x, input_data->len,
4024 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004025 &output_length ),
4026 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004027
Gilles Peskine2d277862018-06-18 15:41:12 +02004028 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004029 ASSERT_COMPARE( expected_data->x, expected_data->len,
4030 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004031
Gilles Peskinea1cac842018-06-11 19:33:02 +02004032exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004033 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004034 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004035 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004036}
4037/* END_CASE */
4038
4039/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004040void signature_size( int type_arg,
4041 int bits,
4042 int alg_arg,
4043 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004044{
4045 psa_key_type_t type = type_arg;
4046 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004047 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004048
Gilles Peskinefe11b722018-12-18 00:24:04 +01004049 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004050#if defined(MBEDTLS_TEST_DEPRECATED)
4051 TEST_EQUAL( actual_size,
4052 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4053#endif /* MBEDTLS_TEST_DEPRECATED */
4054
Gilles Peskinee59236f2018-01-27 23:32:46 +01004055exit:
4056 ;
4057}
4058/* END_CASE */
4059
4060/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004061void sign_deterministic( int key_type_arg, data_t *key_data,
4062 int alg_arg, data_t *input_data,
4063 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004064{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004065 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004066 psa_key_type_t key_type = key_type_arg;
4067 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004068 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004069 unsigned char *signature = NULL;
4070 size_t signature_size;
4071 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004072 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004073
Gilles Peskine8817f612018-12-18 00:18:46 +01004074 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004075
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004076 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004077 psa_set_key_algorithm( &attributes, alg );
4078 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004079
Gilles Peskine049c7532019-05-15 20:22:09 +02004080 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4081 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004082 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4083 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004084
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004085 /* Allocate a buffer which has the size advertized by the
4086 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004087 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004088 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004089 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004090 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004091 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004092
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004093 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004094 PSA_ASSERT( psa_sign_hash( handle, alg,
4095 input_data->x, input_data->len,
4096 signature, signature_size,
4097 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004098 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004099 ASSERT_COMPARE( output_data->x, output_data->len,
4100 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004101
Gilles Peskine0627f982019-11-26 19:12:16 +01004102#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004103 memset( signature, 0, signature_size );
4104 signature_length = INVALID_EXPORT_LENGTH;
Gilles Peskine0627f982019-11-26 19:12:16 +01004105 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
4106 input_data->x, input_data->len,
4107 signature, signature_size,
4108 &signature_length ) );
4109 ASSERT_COMPARE( output_data->x, output_data->len,
4110 signature, signature_length );
4111#endif /* MBEDTLS_TEST_DEPRECATED */
4112
Gilles Peskine20035e32018-02-03 22:44:14 +01004113exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004114 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004115 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01004116 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004117 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004118}
4119/* END_CASE */
4120
4121/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004122void sign_fail( int key_type_arg, data_t *key_data,
4123 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004124 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004125{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004126 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01004127 psa_key_type_t key_type = key_type_arg;
4128 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004129 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004130 psa_status_t actual_status;
4131 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004132 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004133 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004134 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004135
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004136 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004137
Gilles Peskine8817f612018-12-18 00:18:46 +01004138 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004139
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004140 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004141 psa_set_key_algorithm( &attributes, alg );
4142 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004143
Gilles Peskine049c7532019-05-15 20:22:09 +02004144 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4145 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004146
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004147 actual_status = psa_sign_hash( handle, alg,
4148 input_data->x, input_data->len,
4149 signature, signature_size,
4150 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004151 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004152 /* The value of *signature_length is unspecified on error, but
4153 * whatever it is, it should be less than signature_size, so that
4154 * if the caller tries to read *signature_length bytes without
4155 * checking the error code then they don't overflow a buffer. */
4156 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004157
Gilles Peskine895242b2019-11-29 12:15:40 +01004158#if defined(MBEDTLS_TEST_DEPRECATED)
4159 signature_length = INVALID_EXPORT_LENGTH;
4160 TEST_EQUAL( psa_asymmetric_sign( handle, alg,
4161 input_data->x, input_data->len,
4162 signature, signature_size,
4163 &signature_length ),
4164 expected_status );
4165 TEST_ASSERT( signature_length <= signature_size );
4166#endif /* MBEDTLS_TEST_DEPRECATED */
4167
Gilles Peskine20035e32018-02-03 22:44:14 +01004168exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004169 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004170 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01004171 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004172 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004173}
4174/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004175
4176/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004177void sign_verify( int key_type_arg, data_t *key_data,
4178 int alg_arg, data_t *input_data )
4179{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004180 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02004181 psa_key_type_t key_type = key_type_arg;
4182 psa_algorithm_t alg = alg_arg;
4183 size_t key_bits;
4184 unsigned char *signature = NULL;
4185 size_t signature_size;
4186 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004188
Gilles Peskine8817f612018-12-18 00:18:46 +01004189 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004190
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004191 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004192 psa_set_key_algorithm( &attributes, alg );
4193 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004194
Gilles Peskine049c7532019-05-15 20:22:09 +02004195 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4196 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004197 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4198 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004199
4200 /* Allocate a buffer which has the size advertized by the
4201 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004202 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004203 key_bits, alg );
4204 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004205 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004206 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004207
4208 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004209 PSA_ASSERT( psa_sign_hash( handle, alg,
4210 input_data->x, input_data->len,
4211 signature, signature_size,
4212 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004213 /* Check that the signature length looks sensible. */
4214 TEST_ASSERT( signature_length <= signature_size );
4215 TEST_ASSERT( signature_length > 0 );
4216
4217 /* Use the library to verify that the signature is correct. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004218 PSA_ASSERT( psa_verify_hash( handle, alg,
4219 input_data->x, input_data->len,
4220 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004221
4222 if( input_data->len != 0 )
4223 {
4224 /* Flip a bit in the input and verify that the signature is now
4225 * detected as invalid. Flip a bit at the beginning, not at the end,
4226 * because ECDSA may ignore the last few bits of the input. */
4227 input_data->x[0] ^= 1;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004228 TEST_EQUAL( psa_verify_hash( handle, alg,
4229 input_data->x, input_data->len,
4230 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004231 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004232 }
4233
4234exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004235 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004236 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02004237 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004238 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004239}
4240/* END_CASE */
4241
4242/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004243void asymmetric_verify( int key_type_arg, data_t *key_data,
4244 int alg_arg, data_t *hash_data,
4245 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004246{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004247 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03004248 psa_key_type_t key_type = key_type_arg;
4249 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004250 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004251
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004252 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004253
Gilles Peskine8817f612018-12-18 00:18:46 +01004254 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004255
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004256 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004257 psa_set_key_algorithm( &attributes, alg );
4258 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004259
Gilles Peskine049c7532019-05-15 20:22:09 +02004260 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4261 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03004262
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004263 PSA_ASSERT( psa_verify_hash( handle, alg,
4264 hash_data->x, hash_data->len,
4265 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004266
4267#if defined(MBEDTLS_TEST_DEPRECATED)
4268 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
4269 hash_data->x, hash_data->len,
4270 signature_data->x,
4271 signature_data->len ) );
4272
4273#endif /* MBEDTLS_TEST_DEPRECATED */
4274
itayzafrir5c753392018-05-08 11:18:38 +03004275exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004276 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004277 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004278 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004279}
4280/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004281
4282/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004283void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4284 int alg_arg, data_t *hash_data,
4285 data_t *signature_data,
4286 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004287{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004288 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004289 psa_key_type_t key_type = key_type_arg;
4290 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004291 psa_status_t actual_status;
4292 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004293 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004294
Gilles Peskine8817f612018-12-18 00:18:46 +01004295 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004296
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004297 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004298 psa_set_key_algorithm( &attributes, alg );
4299 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004300
Gilles Peskine049c7532019-05-15 20:22:09 +02004301 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4302 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004303
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004304 actual_status = psa_verify_hash( handle, alg,
4305 hash_data->x, hash_data->len,
4306 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004307 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004308
Gilles Peskine895242b2019-11-29 12:15:40 +01004309#if defined(MBEDTLS_TEST_DEPRECATED)
4310 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
4311 hash_data->x, hash_data->len,
4312 signature_data->x, signature_data->len ),
4313 expected_status );
4314#endif /* MBEDTLS_TEST_DEPRECATED */
4315
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004316exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004317 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004318 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004319 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004320}
4321/* END_CASE */
4322
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004323/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004324void asymmetric_encrypt( int key_type_arg,
4325 data_t *key_data,
4326 int alg_arg,
4327 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004328 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004329 int expected_output_length_arg,
4330 int expected_status_arg )
4331{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004332 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02004333 psa_key_type_t key_type = key_type_arg;
4334 psa_algorithm_t alg = alg_arg;
4335 size_t expected_output_length = expected_output_length_arg;
4336 size_t key_bits;
4337 unsigned char *output = NULL;
4338 size_t output_size;
4339 size_t output_length = ~0;
4340 psa_status_t actual_status;
4341 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004343
Gilles Peskine8817f612018-12-18 00:18:46 +01004344 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004345
Gilles Peskine656896e2018-06-29 19:12:28 +02004346 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004347 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4348 psa_set_key_algorithm( &attributes, alg );
4349 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004350 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4351 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004352
4353 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004354 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4355 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004356 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004357 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004358
4359 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004360 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004361 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004362 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004363 output, output_size,
4364 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004365 TEST_EQUAL( actual_status, expected_status );
4366 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004367
Gilles Peskine68428122018-06-30 18:42:41 +02004368 /* If the label is empty, the test framework puts a non-null pointer
4369 * in label->x. Test that a null pointer works as well. */
4370 if( label->len == 0 )
4371 {
4372 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004373 if( output_size != 0 )
4374 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004375 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004376 input_data->x, input_data->len,
4377 NULL, label->len,
4378 output, output_size,
4379 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004380 TEST_EQUAL( actual_status, expected_status );
4381 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004382 }
4383
Gilles Peskine656896e2018-06-29 19:12:28 +02004384exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004385 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004386 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02004387 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004388 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004389}
4390/* END_CASE */
4391
4392/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004393void asymmetric_encrypt_decrypt( int key_type_arg,
4394 data_t *key_data,
4395 int alg_arg,
4396 data_t *input_data,
4397 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004398{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004399 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004400 psa_key_type_t key_type = key_type_arg;
4401 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004402 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004403 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004404 size_t output_size;
4405 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004406 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004407 size_t output2_size;
4408 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004409 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004410
Gilles Peskine8817f612018-12-18 00:18:46 +01004411 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004412
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004413 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4414 psa_set_key_algorithm( &attributes, alg );
4415 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004416
Gilles Peskine049c7532019-05-15 20:22:09 +02004417 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4418 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004419
4420 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004421 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4422 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004423 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004424 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004425 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004426 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004427
Gilles Peskineeebd7382018-06-08 18:11:54 +02004428 /* We test encryption by checking that encrypt-then-decrypt gives back
4429 * the original plaintext because of the non-optional random
4430 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004431 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
4432 input_data->x, input_data->len,
4433 label->x, label->len,
4434 output, output_size,
4435 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004436 /* We don't know what ciphertext length to expect, but check that
4437 * it looks sensible. */
4438 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004439
Gilles Peskine8817f612018-12-18 00:18:46 +01004440 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4441 output, output_length,
4442 label->x, label->len,
4443 output2, output2_size,
4444 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004445 ASSERT_COMPARE( input_data->x, input_data->len,
4446 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004447
4448exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004449 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004450 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004451 mbedtls_free( output );
4452 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004453 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004454}
4455/* END_CASE */
4456
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004457/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004458void asymmetric_decrypt( int key_type_arg,
4459 data_t *key_data,
4460 int alg_arg,
4461 data_t *input_data,
4462 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004463 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004464{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004465 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004466 psa_key_type_t key_type = key_type_arg;
4467 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004468 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004469 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004470 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004471 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004472
Jaeden Amero412654a2019-02-06 12:57:46 +00004473 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004474 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004475
Gilles Peskine8817f612018-12-18 00:18:46 +01004476 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004477
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004478 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4479 psa_set_key_algorithm( &attributes, alg );
4480 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004481
Gilles Peskine049c7532019-05-15 20:22:09 +02004482 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4483 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004484
Gilles Peskine8817f612018-12-18 00:18:46 +01004485 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4486 input_data->x, input_data->len,
4487 label->x, label->len,
4488 output,
4489 output_size,
4490 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004491 ASSERT_COMPARE( expected_data->x, expected_data->len,
4492 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004493
Gilles Peskine68428122018-06-30 18:42:41 +02004494 /* If the label is empty, the test framework puts a non-null pointer
4495 * in label->x. Test that a null pointer works as well. */
4496 if( label->len == 0 )
4497 {
4498 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004499 if( output_size != 0 )
4500 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004501 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4502 input_data->x, input_data->len,
4503 NULL, label->len,
4504 output,
4505 output_size,
4506 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004507 ASSERT_COMPARE( expected_data->x, expected_data->len,
4508 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004509 }
4510
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004511exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004512 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004513 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004514 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004515 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004516}
4517/* END_CASE */
4518
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004519/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004520void asymmetric_decrypt_fail( int key_type_arg,
4521 data_t *key_data,
4522 int alg_arg,
4523 data_t *input_data,
4524 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004525 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004526 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004527{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004528 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004529 psa_key_type_t key_type = key_type_arg;
4530 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004531 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004532 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004533 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004534 psa_status_t actual_status;
4535 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004536 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004537
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004538 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004539
Gilles Peskine8817f612018-12-18 00:18:46 +01004540 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004541
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004542 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4543 psa_set_key_algorithm( &attributes, alg );
4544 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004545
Gilles Peskine049c7532019-05-15 20:22:09 +02004546 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4547 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004548
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004549 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004550 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004551 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004552 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004553 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004554 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004555 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004556
Gilles Peskine68428122018-06-30 18:42:41 +02004557 /* If the label is empty, the test framework puts a non-null pointer
4558 * in label->x. Test that a null pointer works as well. */
4559 if( label->len == 0 )
4560 {
4561 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004562 if( output_size != 0 )
4563 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004564 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004565 input_data->x, input_data->len,
4566 NULL, label->len,
4567 output, output_size,
4568 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004569 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004570 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004571 }
4572
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004573exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004574 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004575 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004576 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004577 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004578}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004579/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004580
4581/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004582void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004583{
4584 /* Test each valid way of initializing the object, except for `= {0}`, as
4585 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4586 * though it's OK by the C standard. We could test for this, but we'd need
4587 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004588 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004589 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4590 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4591 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004592
4593 memset( &zero, 0, sizeof( zero ) );
4594
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004595 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004596 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004597 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004598 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004599 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004600 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004601 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004602
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004603 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004604 PSA_ASSERT( psa_key_derivation_abort(&func) );
4605 PSA_ASSERT( psa_key_derivation_abort(&init) );
4606 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004607}
4608/* END_CASE */
4609
Janos Follath16de4a42019-06-13 16:32:24 +01004610/* BEGIN_CASE */
4611void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004612{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004613 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004614 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004615 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004616
Gilles Peskine8817f612018-12-18 00:18:46 +01004617 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004618
Janos Follath16de4a42019-06-13 16:32:24 +01004619 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004620 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004621
4622exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004623 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004624 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004625}
4626/* END_CASE */
4627
Janos Follathaf3c2a02019-06-12 12:34:34 +01004628/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004629void derive_set_capacity( int alg_arg, int capacity_arg,
4630 int expected_status_arg )
4631{
4632 psa_algorithm_t alg = alg_arg;
4633 size_t capacity = capacity_arg;
4634 psa_status_t expected_status = expected_status_arg;
4635 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4636
4637 PSA_ASSERT( psa_crypto_init( ) );
4638
4639 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4640
4641 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4642 expected_status );
4643
4644exit:
4645 psa_key_derivation_abort( &operation );
4646 PSA_DONE( );
4647}
4648/* END_CASE */
4649
4650/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004651void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004652 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004653 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004654 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004655 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004656 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004657 int expected_status_arg3,
4658 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004659{
4660 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004661 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4662 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004663 psa_status_t expected_statuses[] = {expected_status_arg1,
4664 expected_status_arg2,
4665 expected_status_arg3};
4666 data_t *inputs[] = {input1, input2, input3};
4667 psa_key_handle_t handles[] = {0, 0, 0};
4668 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4669 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4670 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004671 psa_key_type_t output_key_type = output_key_type_arg;
4672 psa_key_handle_t output_handle = 0;
4673 psa_status_t expected_output_status = expected_output_status_arg;
4674 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004675
4676 PSA_ASSERT( psa_crypto_init( ) );
4677
4678 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4679 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004680
4681 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4682
4683 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4684 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004685 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004686 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004687 psa_set_key_type( &attributes, key_types[i] );
4688 PSA_ASSERT( psa_import_key( &attributes,
4689 inputs[i]->x, inputs[i]->len,
4690 &handles[i] ) );
4691 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4692 handles[i] ),
4693 expected_statuses[i] );
4694 }
4695 else
4696 {
4697 TEST_EQUAL( psa_key_derivation_input_bytes(
4698 &operation, steps[i],
4699 inputs[i]->x, inputs[i]->len ),
4700 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004701 }
4702 }
4703
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004704 if( output_key_type != PSA_KEY_TYPE_NONE )
4705 {
4706 psa_reset_key_attributes( &attributes );
4707 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4708 psa_set_key_bits( &attributes, 8 );
4709 actual_output_status =
4710 psa_key_derivation_output_key( &attributes, &operation,
4711 &output_handle );
4712 }
4713 else
4714 {
4715 uint8_t buffer[1];
4716 actual_output_status =
4717 psa_key_derivation_output_bytes( &operation,
4718 buffer, sizeof( buffer ) );
4719 }
4720 TEST_EQUAL( actual_output_status, expected_output_status );
4721
Janos Follathaf3c2a02019-06-12 12:34:34 +01004722exit:
4723 psa_key_derivation_abort( &operation );
4724 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4725 psa_destroy_key( handles[i] );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004726 psa_destroy_key( output_handle );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004727 PSA_DONE( );
4728}
4729/* END_CASE */
4730
Janos Follathd958bb72019-07-03 15:02:16 +01004731/* BEGIN_CASE */
4732void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004733{
Janos Follathd958bb72019-07-03 15:02:16 +01004734 psa_algorithm_t alg = alg_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004735 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004736 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004737 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004738 unsigned char input1[] = "Input 1";
4739 size_t input1_length = sizeof( input1 );
4740 unsigned char input2[] = "Input 2";
4741 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004742 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004743 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004744 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4745 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4746 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004747 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004748
Gilles Peskine8817f612018-12-18 00:18:46 +01004749 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004750
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004751 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4752 psa_set_key_algorithm( &attributes, alg );
4753 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004754
Gilles Peskine73676cb2019-05-15 20:15:10 +02004755 PSA_ASSERT( psa_import_key( &attributes,
4756 key_data, sizeof( key_data ),
4757 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004758
4759 /* valid key derivation */
Janos Follathd958bb72019-07-03 15:02:16 +01004760 if( !setup_key_derivation_wrap( &operation, handle, alg,
4761 input1, input1_length,
4762 input2, input2_length,
4763 capacity ) )
4764 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004765
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004766 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004767 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004768 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004769
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004770 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004771
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004772 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004773 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004774
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004775exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004776 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004777 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004778 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004779}
4780/* END_CASE */
4781
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004782/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004783void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004784{
4785 uint8_t output_buffer[16];
4786 size_t buffer_size = 16;
4787 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004788 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004789
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004790 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4791 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004792 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004793
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004794 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004795 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004796
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004797 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004798
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004799 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4800 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004801 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004802
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004803 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004804 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004805
4806exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004807 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004808}
4809/* END_CASE */
4810
4811/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004812void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004813 int step1_arg, data_t *input1,
4814 int step2_arg, data_t *input2,
4815 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004816 int requested_capacity_arg,
4817 data_t *expected_output1,
4818 data_t *expected_output2 )
4819{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004820 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004821 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4822 data_t *inputs[] = {input1, input2, input3};
4823 psa_key_handle_t handles[] = {0, 0, 0};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004824 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004825 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004826 uint8_t *expected_outputs[2] =
4827 {expected_output1->x, expected_output2->x};
4828 size_t output_sizes[2] =
4829 {expected_output1->len, expected_output2->len};
4830 size_t output_buffer_size = 0;
4831 uint8_t *output_buffer = NULL;
4832 size_t expected_capacity;
4833 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004834 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004835 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004836 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004837
4838 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4839 {
4840 if( output_sizes[i] > output_buffer_size )
4841 output_buffer_size = output_sizes[i];
4842 if( output_sizes[i] == 0 )
4843 expected_outputs[i] = NULL;
4844 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004845 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004846 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004847
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004848 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4849 psa_set_key_algorithm( &attributes, alg );
4850 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004851
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004852 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004853 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4854 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4855 requested_capacity ) );
4856 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004857 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004858 switch( steps[i] )
4859 {
4860 case 0:
4861 break;
4862 case PSA_KEY_DERIVATION_INPUT_SECRET:
4863 PSA_ASSERT( psa_import_key( &attributes,
4864 inputs[i]->x, inputs[i]->len,
4865 &handles[i] ) );
4866 PSA_ASSERT( psa_key_derivation_input_key(
4867 &operation, steps[i],
4868 handles[i] ) );
4869 break;
4870 default:
4871 PSA_ASSERT( psa_key_derivation_input_bytes(
4872 &operation, steps[i],
4873 inputs[i]->x, inputs[i]->len ) );
4874 break;
4875 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004876 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004877
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004878 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004879 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004880 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004881 expected_capacity = requested_capacity;
4882
4883 /* Expansion phase. */
4884 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4885 {
4886 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004887 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004888 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004889 if( expected_capacity == 0 && output_sizes[i] == 0 )
4890 {
4891 /* Reading 0 bytes when 0 bytes are available can go either way. */
4892 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004893 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004894 continue;
4895 }
4896 else if( expected_capacity == 0 ||
4897 output_sizes[i] > expected_capacity )
4898 {
4899 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004900 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004901 expected_capacity = 0;
4902 continue;
4903 }
4904 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004905 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004906 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004907 ASSERT_COMPARE( output_buffer, output_sizes[i],
4908 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004909 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004910 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004911 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004912 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004913 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004914 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004915 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004916
4917exit:
4918 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004919 psa_key_derivation_abort( &operation );
Gilles Peskine1468da72019-05-29 17:35:49 +02004920 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4921 psa_destroy_key( handles[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004922 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004923}
4924/* END_CASE */
4925
4926/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004927void derive_full( int alg_arg,
4928 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004929 data_t *input1,
4930 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004931 int requested_capacity_arg )
4932{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004933 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004934 psa_algorithm_t alg = alg_arg;
4935 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004936 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004937 unsigned char output_buffer[16];
4938 size_t expected_capacity = requested_capacity;
4939 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004940 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004941
Gilles Peskine8817f612018-12-18 00:18:46 +01004942 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004943
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004944 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4945 psa_set_key_algorithm( &attributes, alg );
4946 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004947
Gilles Peskine049c7532019-05-15 20:22:09 +02004948 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4949 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004950
Janos Follathf2815ea2019-07-03 12:41:36 +01004951 if( !setup_key_derivation_wrap( &operation, handle, alg,
4952 input1->x, input1->len,
4953 input2->x, input2->len,
4954 requested_capacity ) )
4955 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004956
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004957 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004958 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004959 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004960
4961 /* Expansion phase. */
4962 while( current_capacity > 0 )
4963 {
4964 size_t read_size = sizeof( output_buffer );
4965 if( read_size > current_capacity )
4966 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004967 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004968 output_buffer,
4969 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004970 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004972 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004973 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004974 }
4975
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004976 /* Check that the operation refuses to go over capacity. */
4977 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004978 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004979
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004980 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004981
4982exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004983 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004984 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004985 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004986}
4987/* END_CASE */
4988
Janos Follathe60c9052019-07-03 13:51:30 +01004989/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004990void derive_key_exercise( int alg_arg,
4991 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004992 data_t *input1,
4993 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004994 int derived_type_arg,
4995 int derived_bits_arg,
4996 int derived_usage_arg,
4997 int derived_alg_arg )
4998{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004999 psa_key_handle_t base_handle = 0;
5000 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005001 psa_algorithm_t alg = alg_arg;
5002 psa_key_type_t derived_type = derived_type_arg;
5003 size_t derived_bits = derived_bits_arg;
5004 psa_key_usage_t derived_usage = derived_usage_arg;
5005 psa_algorithm_t derived_alg = derived_alg_arg;
5006 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005007 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005008 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005009 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005010
Gilles Peskine8817f612018-12-18 00:18:46 +01005011 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005012
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005013 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5014 psa_set_key_algorithm( &attributes, alg );
5015 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005016 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5017 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005018
5019 /* Derive a key. */
Janos Follathe60c9052019-07-03 13:51:30 +01005020 if ( setup_key_derivation_wrap( &operation, base_handle, alg,
5021 input1->x, input1->len,
5022 input2->x, input2->len, capacity ) )
5023 goto exit;
5024
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005025 psa_set_key_usage_flags( &attributes, derived_usage );
5026 psa_set_key_algorithm( &attributes, derived_alg );
5027 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005028 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005029 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005030 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005031
5032 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005033 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
5034 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5035 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005036
5037 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005038 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005039 goto exit;
5040
5041exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005042 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005043 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005044 psa_destroy_key( base_handle );
5045 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005046 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005047}
5048/* END_CASE */
5049
Janos Follath42fd8882019-07-03 14:17:09 +01005050/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005051void derive_key_export( int alg_arg,
5052 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005053 data_t *input1,
5054 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005055 int bytes1_arg,
5056 int bytes2_arg )
5057{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005058 psa_key_handle_t base_handle = 0;
5059 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005060 psa_algorithm_t alg = alg_arg;
5061 size_t bytes1 = bytes1_arg;
5062 size_t bytes2 = bytes2_arg;
5063 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005064 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005065 uint8_t *output_buffer = NULL;
5066 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005067 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5068 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005069 size_t length;
5070
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005071 ASSERT_ALLOC( output_buffer, capacity );
5072 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005073 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005074
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005075 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5076 psa_set_key_algorithm( &base_attributes, alg );
5077 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005078 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
5079 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005080
5081 /* Derive some material and output it. */
Janos Follath42fd8882019-07-03 14:17:09 +01005082 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5083 input1->x, input1->len,
5084 input2->x, input2->len, capacity ) )
5085 goto exit;
5086
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005087 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005088 output_buffer,
5089 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005090 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005091
5092 /* Derive the same output again, but this time store it in key objects. */
Janos Follath42fd8882019-07-03 14:17:09 +01005093 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5094 input1->x, input1->len,
5095 input2->x, input2->len, capacity ) )
5096 goto exit;
5097
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005098 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5099 psa_set_key_algorithm( &derived_attributes, 0 );
5100 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005101 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005102 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005103 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01005104 PSA_ASSERT( psa_export_key( derived_handle,
5105 export_buffer, bytes1,
5106 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005107 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01005108 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005109 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005110 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005111 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01005112 PSA_ASSERT( psa_export_key( derived_handle,
5113 export_buffer + bytes1, bytes2,
5114 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005115 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005116
5117 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005118 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5119 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005120
5121exit:
5122 mbedtls_free( output_buffer );
5123 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005124 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005125 psa_destroy_key( base_handle );
5126 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005127 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005128}
5129/* END_CASE */
5130
5131/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005132void derive_key( int alg_arg,
5133 data_t *key_data, data_t *input1, data_t *input2,
5134 int type_arg, int bits_arg,
5135 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02005136{
5137 psa_key_handle_t base_handle = 0;
5138 psa_key_handle_t derived_handle = 0;
5139 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005140 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005141 size_t bits = bits_arg;
5142 psa_status_t expected_status = expected_status_arg;
5143 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5144 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5145 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5146
5147 PSA_ASSERT( psa_crypto_init( ) );
5148
5149 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5150 psa_set_key_algorithm( &base_attributes, alg );
5151 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5152 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
5153 &base_handle ) );
5154
5155 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5156 input1->x, input1->len,
5157 input2->x, input2->len, SIZE_MAX ) )
5158 goto exit;
5159
5160 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5161 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005162 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005163 psa_set_key_bits( &derived_attributes, bits );
5164 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
5165 &derived_handle ),
5166 expected_status );
5167
5168exit:
5169 psa_key_derivation_abort( &operation );
5170 psa_destroy_key( base_handle );
5171 psa_destroy_key( derived_handle );
5172 PSA_DONE( );
5173}
5174/* END_CASE */
5175
5176/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005177void key_agreement_setup( int alg_arg,
5178 int our_key_type_arg, data_t *our_key_data,
5179 data_t *peer_key_data,
5180 int expected_status_arg )
5181{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005182 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005183 psa_algorithm_t alg = alg_arg;
5184 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005185 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005186 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005187 psa_status_t expected_status = expected_status_arg;
5188 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005189
Gilles Peskine8817f612018-12-18 00:18:46 +01005190 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005191
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005192 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5193 psa_set_key_algorithm( &attributes, alg );
5194 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005195 PSA_ASSERT( psa_import_key( &attributes,
5196 our_key_data->x, our_key_data->len,
5197 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005198
Gilles Peskine77f40d82019-04-11 21:27:06 +02005199 /* The tests currently include inputs that should fail at either step.
5200 * Test cases that fail at the setup step should be changed to call
5201 * key_derivation_setup instead, and this function should be renamed
5202 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005203 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005204 if( status == PSA_SUCCESS )
5205 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005206 TEST_EQUAL( psa_key_derivation_key_agreement(
5207 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5208 our_key,
5209 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005210 expected_status );
5211 }
5212 else
5213 {
5214 TEST_ASSERT( status == expected_status );
5215 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005216
5217exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005218 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005219 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005220 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005221}
5222/* END_CASE */
5223
5224/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005225void raw_key_agreement( int alg_arg,
5226 int our_key_type_arg, data_t *our_key_data,
5227 data_t *peer_key_data,
5228 data_t *expected_output )
5229{
5230 psa_key_handle_t our_key = 0;
5231 psa_algorithm_t alg = alg_arg;
5232 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005233 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005234 unsigned char *output = NULL;
5235 size_t output_length = ~0;
5236
5237 ASSERT_ALLOC( output, expected_output->len );
5238 PSA_ASSERT( psa_crypto_init( ) );
5239
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005240 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5241 psa_set_key_algorithm( &attributes, alg );
5242 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005243 PSA_ASSERT( psa_import_key( &attributes,
5244 our_key_data->x, our_key_data->len,
5245 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005246
Gilles Peskinebe697d82019-05-16 18:00:41 +02005247 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5248 peer_key_data->x, peer_key_data->len,
5249 output, expected_output->len,
5250 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005251 ASSERT_COMPARE( output, output_length,
5252 expected_output->x, expected_output->len );
5253
5254exit:
5255 mbedtls_free( output );
5256 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005257 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005258}
5259/* END_CASE */
5260
5261/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005262void key_agreement_capacity( int alg_arg,
5263 int our_key_type_arg, data_t *our_key_data,
5264 data_t *peer_key_data,
5265 int expected_capacity_arg )
5266{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005267 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005268 psa_algorithm_t alg = alg_arg;
5269 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005270 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005271 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005272 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005273 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005274
Gilles Peskine8817f612018-12-18 00:18:46 +01005275 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005276
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005277 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5278 psa_set_key_algorithm( &attributes, alg );
5279 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005280 PSA_ASSERT( psa_import_key( &attributes,
5281 our_key_data->x, our_key_data->len,
5282 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005283
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005284 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005285 PSA_ASSERT( psa_key_derivation_key_agreement(
5286 &operation,
5287 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5288 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005289 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5290 {
5291 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005292 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005293 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005294 NULL, 0 ) );
5295 }
Gilles Peskine59685592018-09-18 12:11:34 +02005296
Gilles Peskinebf491972018-10-25 22:36:12 +02005297 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005298 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005299 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005300 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005301
Gilles Peskinebf491972018-10-25 22:36:12 +02005302 /* Test the actual capacity by reading the output. */
5303 while( actual_capacity > sizeof( output ) )
5304 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005305 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005306 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005307 actual_capacity -= sizeof( output );
5308 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005309 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005310 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005311 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005312 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005313
Gilles Peskine59685592018-09-18 12:11:34 +02005314exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005315 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005316 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005317 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005318}
5319/* END_CASE */
5320
5321/* BEGIN_CASE */
5322void key_agreement_output( int alg_arg,
5323 int our_key_type_arg, data_t *our_key_data,
5324 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005325 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005326{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005327 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005328 psa_algorithm_t alg = alg_arg;
5329 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005330 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005331 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005332 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005333
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005334 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5335 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005336
Gilles Peskine8817f612018-12-18 00:18:46 +01005337 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005338
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005339 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5340 psa_set_key_algorithm( &attributes, alg );
5341 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005342 PSA_ASSERT( psa_import_key( &attributes,
5343 our_key_data->x, our_key_data->len,
5344 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005345
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005346 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005347 PSA_ASSERT( psa_key_derivation_key_agreement(
5348 &operation,
5349 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5350 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005351 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5352 {
5353 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005354 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005355 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005356 NULL, 0 ) );
5357 }
Gilles Peskine59685592018-09-18 12:11:34 +02005358
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005359 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005360 actual_output,
5361 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005362 ASSERT_COMPARE( actual_output, expected_output1->len,
5363 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005364 if( expected_output2->len != 0 )
5365 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005366 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005367 actual_output,
5368 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005369 ASSERT_COMPARE( actual_output, expected_output2->len,
5370 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005371 }
Gilles Peskine59685592018-09-18 12:11:34 +02005372
5373exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005374 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005375 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005376 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005377 mbedtls_free( actual_output );
5378}
5379/* END_CASE */
5380
5381/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005382void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005383{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005384 size_t bytes = bytes_arg;
5385 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005386 unsigned char *output = NULL;
5387 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005388 size_t i;
5389 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005390
Simon Butcher49f8e312020-03-03 15:51:50 +00005391 TEST_ASSERT( bytes_arg >= 0 );
5392
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005393 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5394 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005395 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005396
Gilles Peskine8817f612018-12-18 00:18:46 +01005397 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005398
Gilles Peskinea50d7392018-06-21 10:22:13 +02005399 /* Run several times, to ensure that every output byte will be
5400 * nonzero at least once with overwhelming probability
5401 * (2^(-8*number_of_runs)). */
5402 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005403 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005404 if( bytes != 0 )
5405 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005406 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005407
5408 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005409 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5410 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005411
5412 for( i = 0; i < bytes; i++ )
5413 {
5414 if( output[i] != 0 )
5415 ++changed[i];
5416 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005417 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005418
5419 /* Check that every byte was changed to nonzero at least once. This
5420 * validates that psa_generate_random is overwriting every byte of
5421 * the output buffer. */
5422 for( i = 0; i < bytes; i++ )
5423 {
5424 TEST_ASSERT( changed[i] != 0 );
5425 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005426
5427exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005428 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005429 mbedtls_free( output );
5430 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005431}
5432/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005433
5434/* BEGIN_CASE */
5435void generate_key( int type_arg,
5436 int bits_arg,
5437 int usage_arg,
5438 int alg_arg,
5439 int expected_status_arg )
5440{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005441 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005442 psa_key_type_t type = type_arg;
5443 psa_key_usage_t usage = usage_arg;
5444 size_t bits = bits_arg;
5445 psa_algorithm_t alg = alg_arg;
5446 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005447 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005448 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005449
Gilles Peskine8817f612018-12-18 00:18:46 +01005450 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005451
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005452 psa_set_key_usage_flags( &attributes, usage );
5453 psa_set_key_algorithm( &attributes, alg );
5454 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005455 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005456
5457 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005458 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005459 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005460 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005461
5462 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005463 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
5464 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5465 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005466
Gilles Peskine818ca122018-06-20 18:16:48 +02005467 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005468 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005469 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005470
5471exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005472 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005473 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005474 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005475}
5476/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005477
Gilles Peskinee56e8782019-04-26 17:34:02 +02005478/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5479void generate_key_rsa( int bits_arg,
5480 data_t *e_arg,
5481 int expected_status_arg )
5482{
5483 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005484 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005485 size_t bits = bits_arg;
5486 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5487 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5488 psa_status_t expected_status = expected_status_arg;
5489 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5490 uint8_t *exported = NULL;
5491 size_t exported_size =
5492 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5493 size_t exported_length = SIZE_MAX;
5494 uint8_t *e_read_buffer = NULL;
5495 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005496 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005497 size_t e_read_length = SIZE_MAX;
5498
5499 if( e_arg->len == 0 ||
5500 ( e_arg->len == 3 &&
5501 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5502 {
5503 is_default_public_exponent = 1;
5504 e_read_size = 0;
5505 }
5506 ASSERT_ALLOC( e_read_buffer, e_read_size );
5507 ASSERT_ALLOC( exported, exported_size );
5508
5509 PSA_ASSERT( psa_crypto_init( ) );
5510
5511 psa_set_key_usage_flags( &attributes, usage );
5512 psa_set_key_algorithm( &attributes, alg );
5513 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5514 e_arg->x, e_arg->len ) );
5515 psa_set_key_bits( &attributes, bits );
5516
5517 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005518 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005519 if( expected_status != PSA_SUCCESS )
5520 goto exit;
5521
5522 /* Test the key information */
5523 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5524 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5525 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5526 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5527 e_read_buffer, e_read_size,
5528 &e_read_length ) );
5529 if( is_default_public_exponent )
5530 TEST_EQUAL( e_read_length, 0 );
5531 else
5532 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5533
5534 /* Do something with the key according to its type and permitted usage. */
5535 if( ! exercise_key( handle, usage, alg ) )
5536 goto exit;
5537
5538 /* Export the key and check the public exponent. */
5539 PSA_ASSERT( psa_export_public_key( handle,
5540 exported, exported_size,
5541 &exported_length ) );
5542 {
5543 uint8_t *p = exported;
5544 uint8_t *end = exported + exported_length;
5545 size_t len;
5546 /* RSAPublicKey ::= SEQUENCE {
5547 * modulus INTEGER, -- n
5548 * publicExponent INTEGER } -- e
5549 */
5550 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005551 MBEDTLS_ASN1_SEQUENCE |
5552 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005553 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5554 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5555 MBEDTLS_ASN1_INTEGER ) );
5556 if( len >= 1 && p[0] == 0 )
5557 {
5558 ++p;
5559 --len;
5560 }
5561 if( e_arg->len == 0 )
5562 {
5563 TEST_EQUAL( len, 3 );
5564 TEST_EQUAL( p[0], 1 );
5565 TEST_EQUAL( p[1], 0 );
5566 TEST_EQUAL( p[2], 1 );
5567 }
5568 else
5569 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5570 }
5571
5572exit:
5573 psa_reset_key_attributes( &attributes );
5574 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005575 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005576 mbedtls_free( e_read_buffer );
5577 mbedtls_free( exported );
5578}
5579/* END_CASE */
5580
Darryl Greend49a4992018-06-18 17:27:26 +01005581/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005582void persistent_key_load_key_from_storage( data_t *data,
5583 int type_arg, int bits_arg,
5584 int usage_flags_arg, int alg_arg,
5585 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005586{
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005587 psa_key_id_t key_id = 1;
5588 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005589 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005590 psa_key_handle_t base_key = 0;
5591 psa_key_type_t type = type_arg;
5592 size_t bits = bits_arg;
5593 psa_key_usage_t usage_flags = usage_flags_arg;
5594 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005595 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005596 unsigned char *first_export = NULL;
5597 unsigned char *second_export = NULL;
5598 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5599 size_t first_exported_length;
5600 size_t second_exported_length;
5601
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005602 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5603 {
5604 ASSERT_ALLOC( first_export, export_size );
5605 ASSERT_ALLOC( second_export, export_size );
5606 }
Darryl Greend49a4992018-06-18 17:27:26 +01005607
Gilles Peskine8817f612018-12-18 00:18:46 +01005608 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005609
Gilles Peskinec87af662019-05-15 16:12:22 +02005610 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005611 psa_set_key_usage_flags( &attributes, usage_flags );
5612 psa_set_key_algorithm( &attributes, alg );
5613 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005614 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005615
Darryl Green0c6575a2018-11-07 16:05:30 +00005616 switch( generation_method )
5617 {
5618 case IMPORT_KEY:
5619 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005620 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
5621 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005622 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005623
Darryl Green0c6575a2018-11-07 16:05:30 +00005624 case GENERATE_KEY:
5625 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005626 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005627 break;
5628
5629 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005630 {
5631 /* Create base key */
5632 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5633 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5634 psa_set_key_usage_flags( &base_attributes,
5635 PSA_KEY_USAGE_DERIVE );
5636 psa_set_key_algorithm( &base_attributes, derive_alg );
5637 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005638 PSA_ASSERT( psa_import_key( &base_attributes,
5639 data->x, data->len,
5640 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005641 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005642 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005643 PSA_ASSERT( psa_key_derivation_input_key(
5644 &operation,
5645 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005646 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005647 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005648 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005649 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5650 &operation,
5651 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005652 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005653 PSA_ASSERT( psa_destroy_key( base_key ) );
5654 base_key = 0;
5655 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005656 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005657 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005658 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005659
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005660 /* Export the key if permitted by the key policy. */
5661 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5662 {
5663 PSA_ASSERT( psa_export_key( handle,
5664 first_export, export_size,
5665 &first_exported_length ) );
5666 if( generation_method == IMPORT_KEY )
5667 ASSERT_COMPARE( data->x, data->len,
5668 first_export, first_exported_length );
5669 }
Darryl Greend49a4992018-06-18 17:27:26 +01005670
5671 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005672 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005673 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005674 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005675
Darryl Greend49a4992018-06-18 17:27:26 +01005676 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005677 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005678 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5679 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
5680 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5681 PSA_KEY_LIFETIME_PERSISTENT );
5682 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5683 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5684 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5685 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005686
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005687 /* Export the key again if permitted by the key policy. */
5688 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005689 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005690 PSA_ASSERT( psa_export_key( handle,
5691 second_export, export_size,
5692 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005693 ASSERT_COMPARE( first_export, first_exported_length,
5694 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005695 }
5696
5697 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005698 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005699 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005700
5701exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005702 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005703 mbedtls_free( first_export );
5704 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005705 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005706 psa_destroy_key( base_key );
5707 if( handle == 0 )
5708 {
5709 /* In case there was a test failure after creating the persistent key
5710 * but while it was not open, try to re-open the persistent key
5711 * to delete it. */
Gilles Peskinee92c68a2020-08-26 00:06:25 +02005712 (void) psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005713 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005714 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005715 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005716}
5717/* END_CASE */