blob: 3f34211d68bab03313d56e9badb0add5be0a7db8 [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;
Ronald Cron71016a92020-08-28 19:01:50 +0200236 mbedtls_svc_key_id_t id;
Gilles Peskine667c1112019-12-03 19:03:20 +0100237 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;
Ronald Cron71016a92020-08-28 19:01:50 +02001329 mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( 1, id_arg );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001330 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;
Ronald Cron71016a92020-08-28 19:01:50 +02001373 mbedtls_svc_key_id_t id1 = mbedtls_svc_key_id_make( 1, id1_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001374 psa_key_lifetime_t lifetime = lifetime_arg;
Ronald Cron71016a92020-08-28 19:01:50 +02001375 mbedtls_svc_key_id_t id2 = mbedtls_svc_key_id_make( 1, id2_arg );
1376 mbedtls_svc_key_id_t expected_id =
1377 mbedtls_svc_key_id_make( 1, expected_id_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001378 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1379
1380 if( id1_arg != -1 )
1381 psa_set_key_id( &attributes, id1 );
1382 if( lifetime_arg != -1 )
1383 psa_set_key_lifetime( &attributes, lifetime );
1384 if( id2_arg != -1 )
1385 psa_set_key_id( &attributes, id2 );
1386
1387 TEST_EQUAL( psa_get_key_id( &attributes ), expected_id );
1388 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1389}
1390/* END_CASE */
1391
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001392/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1393void slot_number_attribute( )
1394{
1395 psa_key_slot_number_t slot_number = 0xdeadbeef;
1396 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1397
1398 /* Initially, there is no slot number. */
1399 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1400 PSA_ERROR_INVALID_ARGUMENT );
1401
1402 /* Test setting a slot number. */
1403 psa_set_key_slot_number( &attributes, 0 );
1404 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1405 TEST_EQUAL( slot_number, 0 );
1406
1407 /* Test changing the slot number. */
1408 psa_set_key_slot_number( &attributes, 42 );
1409 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1410 TEST_EQUAL( slot_number, 42 );
1411
1412 /* Test clearing the slot number. */
1413 psa_clear_key_slot_number( &attributes );
1414 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1415 PSA_ERROR_INVALID_ARGUMENT );
1416
1417 /* Clearing again should have no effect. */
1418 psa_clear_key_slot_number( &attributes );
1419 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1420 PSA_ERROR_INVALID_ARGUMENT );
1421
1422 /* Test that reset clears the slot number. */
1423 psa_set_key_slot_number( &attributes, 42 );
1424 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1425 TEST_EQUAL( slot_number, 42 );
1426 psa_reset_key_attributes( &attributes );
1427 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1428 PSA_ERROR_INVALID_ARGUMENT );
1429}
1430/* END_CASE */
1431
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001432/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001433void import_with_policy( int type_arg,
1434 int usage_arg, int alg_arg,
1435 int expected_status_arg )
1436{
1437 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1438 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1439 psa_key_handle_t handle = 0;
1440 psa_key_type_t type = type_arg;
1441 psa_key_usage_t usage = usage_arg;
1442 psa_algorithm_t alg = alg_arg;
1443 psa_status_t expected_status = expected_status_arg;
1444 const uint8_t key_material[16] = {0};
1445 psa_status_t status;
1446
1447 PSA_ASSERT( psa_crypto_init( ) );
1448
1449 psa_set_key_type( &attributes, type );
1450 psa_set_key_usage_flags( &attributes, usage );
1451 psa_set_key_algorithm( &attributes, alg );
1452
1453 status = psa_import_key( &attributes,
1454 key_material, sizeof( key_material ),
1455 &handle );
1456 TEST_EQUAL( status, expected_status );
1457 if( status != PSA_SUCCESS )
1458 goto exit;
1459
1460 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1461 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1462 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1463 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001464 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001465
1466 PSA_ASSERT( psa_destroy_key( handle ) );
1467 test_operations_on_invalid_handle( handle );
1468
1469exit:
1470 psa_destroy_key( handle );
1471 psa_reset_key_attributes( &got_attributes );
1472 PSA_DONE( );
1473}
1474/* END_CASE */
1475
1476/* BEGIN_CASE */
1477void import_with_data( data_t *data, int type_arg,
1478 int attr_bits_arg,
1479 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001480{
1481 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1482 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001483 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001484 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001485 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001486 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001487 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001488
Gilles Peskine8817f612018-12-18 00:18:46 +01001489 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001490
Gilles Peskine4747d192019-04-17 15:05:45 +02001491 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001492 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001493
Gilles Peskine73676cb2019-05-15 20:15:10 +02001494 status = psa_import_key( &attributes, data->x, data->len, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001495 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001496 if( status != PSA_SUCCESS )
1497 goto exit;
1498
1499 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1500 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001501 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001502 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001503 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001504
1505 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001506 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001507
1508exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001509 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001510 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001511 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001512}
1513/* END_CASE */
1514
1515/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001516void import_large_key( int type_arg, int byte_size_arg,
1517 int expected_status_arg )
1518{
1519 psa_key_type_t type = type_arg;
1520 size_t byte_size = byte_size_arg;
1521 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1522 psa_status_t expected_status = expected_status_arg;
1523 psa_key_handle_t handle = 0;
1524 psa_status_t status;
1525 uint8_t *buffer = NULL;
1526 size_t buffer_size = byte_size + 1;
1527 size_t n;
1528
1529 /* It would be better to skip the test than fail it if the allocation
1530 * fails, but the test framework doesn't support this yet. */
1531 ASSERT_ALLOC( buffer, buffer_size );
1532 memset( buffer, 'K', byte_size );
1533
1534 PSA_ASSERT( psa_crypto_init( ) );
1535
1536 /* Try importing the key */
1537 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1538 psa_set_key_type( &attributes, type );
1539 status = psa_import_key( &attributes, buffer, byte_size, &handle );
1540 TEST_EQUAL( status, expected_status );
1541
1542 if( status == PSA_SUCCESS )
1543 {
1544 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1545 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1546 TEST_EQUAL( psa_get_key_bits( &attributes ),
1547 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001548 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001549 memset( buffer, 0, byte_size + 1 );
1550 PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) );
1551 for( n = 0; n < byte_size; n++ )
1552 TEST_EQUAL( buffer[n], 'K' );
1553 for( n = byte_size; n < buffer_size; n++ )
1554 TEST_EQUAL( buffer[n], 0 );
1555 }
1556
1557exit:
1558 psa_destroy_key( handle );
1559 PSA_DONE( );
1560 mbedtls_free( buffer );
1561}
1562/* END_CASE */
1563
1564/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001565void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1566{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001567 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001568 size_t bits = bits_arg;
1569 psa_status_t expected_status = expected_status_arg;
1570 psa_status_t status;
1571 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001572 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001573 size_t buffer_size = /* Slight overapproximations */
1574 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001575 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001576 unsigned char *p;
1577 int ret;
1578 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001579 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001580
Gilles Peskine8817f612018-12-18 00:18:46 +01001581 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001582 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001583
1584 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1585 bits, keypair ) ) >= 0 );
1586 length = ret;
1587
1588 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001589 psa_set_key_type( &attributes, type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001590 status = psa_import_key( &attributes, p, length, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001591 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001592
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001593 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001594 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001595
1596exit:
1597 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001598 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001599}
1600/* END_CASE */
1601
1602/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001603void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001604 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001605 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001606 int expected_bits,
1607 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001608 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001609 int canonical_input )
1610{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001611 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001612 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001613 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001614 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001615 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001616 unsigned char *exported = NULL;
1617 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001618 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001619 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001620 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001621 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001622 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001623
Moran Pekercb088e72018-07-17 17:36:59 +03001624 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001625 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001626 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001627 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001628 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001629
Gilles Peskine4747d192019-04-17 15:05:45 +02001630 psa_set_key_usage_flags( &attributes, usage_arg );
1631 psa_set_key_algorithm( &attributes, alg );
1632 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001633
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001634 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001635 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001636
1637 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001638 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1639 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1640 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001641 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001642
1643 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001644 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001645 exported, export_size,
1646 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001647 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001648
1649 /* The exported length must be set by psa_export_key() to a value between 0
1650 * and export_size. On errors, the exported length must be 0. */
1651 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1652 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1653 TEST_ASSERT( exported_length <= export_size );
1654
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001655 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001656 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001657 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001658 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001659 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001660 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001661 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001662
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001663 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001664 goto exit;
1665
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001666 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001667 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001668 else
1669 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001670 psa_key_handle_t handle2;
Gilles Peskine049c7532019-05-15 20:22:09 +02001671 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
1672 &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001673 PSA_ASSERT( psa_export_key( handle2,
1674 reexported,
1675 export_size,
1676 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001677 ASSERT_COMPARE( exported, exported_length,
1678 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001679 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001680 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001681 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001682
1683destroy:
1684 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001685 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001686 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001687
1688exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001689 mbedtls_free( exported );
1690 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001691 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001692 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001693}
1694/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001695
Moran Pekerf709f4a2018-06-06 17:26:04 +03001696/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001697void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001698 int type_arg,
1699 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001700 int export_size_delta,
1701 int expected_export_status_arg,
1702 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001703{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001704 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001705 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001706 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001707 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001708 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001709 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001710 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001711 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001713
Gilles Peskine8817f612018-12-18 00:18:46 +01001714 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001715
Gilles Peskine4747d192019-04-17 15:05:45 +02001716 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1717 psa_set_key_algorithm( &attributes, alg );
1718 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001719
1720 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001721 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001722
Gilles Peskine49c25912018-10-29 15:15:31 +01001723 /* Export the public key */
1724 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001725 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001726 exported, export_size,
1727 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001728 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001729 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001730 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001731 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001732 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001733 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1734 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001735 TEST_ASSERT( expected_public_key->len <=
1736 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001737 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1738 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001739 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001740
1741exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001742 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001743 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001744 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001745 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001746}
1747/* END_CASE */
1748
Gilles Peskine20035e32018-02-03 22:44:14 +01001749/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001750void import_and_exercise_key( data_t *data,
1751 int type_arg,
1752 int bits_arg,
1753 int alg_arg )
1754{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001755 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001756 psa_key_type_t type = type_arg;
1757 size_t bits = bits_arg;
1758 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001759 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001760 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001761 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001762
Gilles Peskine8817f612018-12-18 00:18:46 +01001763 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001764
Gilles Peskine4747d192019-04-17 15:05:45 +02001765 psa_set_key_usage_flags( &attributes, usage );
1766 psa_set_key_algorithm( &attributes, alg );
1767 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001768
1769 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001770 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001771
1772 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001773 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1774 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1775 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001776
1777 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001778 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001779 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001780
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001781 PSA_ASSERT( psa_destroy_key( handle ) );
1782 test_operations_on_invalid_handle( handle );
1783
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001784exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001785 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001786 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001787 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001788}
1789/* END_CASE */
1790
1791/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001792void effective_key_attributes( int type_arg, int expected_type_arg,
1793 int bits_arg, int expected_bits_arg,
1794 int usage_arg, int expected_usage_arg,
1795 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001796{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001797 psa_key_handle_t handle = 0;
Gilles Peskine1a960492019-11-26 17:12:21 +01001798 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001799 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001800 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001801 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001802 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001803 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001804 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001805 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001806 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001807
Gilles Peskine8817f612018-12-18 00:18:46 +01001808 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001809
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001810 psa_set_key_usage_flags( &attributes, usage );
1811 psa_set_key_algorithm( &attributes, alg );
1812 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001813 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001814
Gilles Peskine1a960492019-11-26 17:12:21 +01001815 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
1816 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001817
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001818 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001819 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1820 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1821 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1822 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001823
1824exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001825 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001826 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001827 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001828}
1829/* END_CASE */
1830
1831/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001832void check_key_policy( int type_arg, int bits_arg,
1833 int usage_arg, int alg_arg )
1834{
1835 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1836 usage_arg, usage_arg, alg_arg, alg_arg );
1837 goto exit;
1838}
1839/* END_CASE */
1840
1841/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001842void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001843{
1844 /* Test each valid way of initializing the object, except for `= {0}`, as
1845 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1846 * though it's OK by the C standard. We could test for this, but we'd need
1847 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001848 psa_key_attributes_t func = psa_key_attributes_init( );
1849 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1850 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001851
1852 memset( &zero, 0, sizeof( zero ) );
1853
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001854 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1855 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1856 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001857
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001858 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1859 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1860 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1861
1862 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1863 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1864 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1865
1866 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1867 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1868 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1869
1870 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1871 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1872 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001873}
1874/* END_CASE */
1875
1876/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001877void mac_key_policy( int policy_usage,
1878 int policy_alg,
1879 int key_type,
1880 data_t *key_data,
1881 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001882{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001883 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001884 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001885 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001886 psa_status_t status;
1887 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001888
Gilles Peskine8817f612018-12-18 00:18:46 +01001889 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001890
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001891 psa_set_key_usage_flags( &attributes, policy_usage );
1892 psa_set_key_algorithm( &attributes, policy_alg );
1893 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001894
Gilles Peskine049c7532019-05-15 20:22:09 +02001895 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1896 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001897
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001898 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001899 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001900 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001901 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001902 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001903 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001904 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001905
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001906 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001907 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001908 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001909 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001910 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001911 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001912 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001913
1914exit:
1915 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001916 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001917 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001918}
1919/* END_CASE */
1920
1921/* BEGIN_CASE */
1922void cipher_key_policy( int policy_usage,
1923 int policy_alg,
1924 int key_type,
1925 data_t *key_data,
1926 int exercise_alg )
1927{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001928 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001929 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001930 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001931 psa_status_t status;
1932
Gilles Peskine8817f612018-12-18 00:18:46 +01001933 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001934
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001935 psa_set_key_usage_flags( &attributes, policy_usage );
1936 psa_set_key_algorithm( &attributes, policy_alg );
1937 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001938
Gilles Peskine049c7532019-05-15 20:22:09 +02001939 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1940 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001941
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001942 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001943 if( policy_alg == exercise_alg &&
1944 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001945 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001946 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001947 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001948 psa_cipher_abort( &operation );
1949
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001950 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001951 if( policy_alg == exercise_alg &&
1952 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001953 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001954 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001955 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001956
1957exit:
1958 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001959 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001960 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001961}
1962/* END_CASE */
1963
1964/* BEGIN_CASE */
1965void aead_key_policy( int policy_usage,
1966 int policy_alg,
1967 int key_type,
1968 data_t *key_data,
1969 int nonce_length_arg,
1970 int tag_length_arg,
1971 int exercise_alg )
1972{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001973 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001974 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001975 psa_status_t status;
1976 unsigned char nonce[16] = {0};
1977 size_t nonce_length = nonce_length_arg;
1978 unsigned char tag[16];
1979 size_t tag_length = tag_length_arg;
1980 size_t output_length;
1981
1982 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1983 TEST_ASSERT( tag_length <= sizeof( tag ) );
1984
Gilles Peskine8817f612018-12-18 00:18:46 +01001985 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001986
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001987 psa_set_key_usage_flags( &attributes, policy_usage );
1988 psa_set_key_algorithm( &attributes, policy_alg );
1989 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001990
Gilles Peskine049c7532019-05-15 20:22:09 +02001991 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1992 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001993
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001994 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001995 nonce, nonce_length,
1996 NULL, 0,
1997 NULL, 0,
1998 tag, tag_length,
1999 &output_length );
2000 if( policy_alg == exercise_alg &&
2001 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002002 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002003 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002004 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002005
2006 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002007 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002008 nonce, nonce_length,
2009 NULL, 0,
2010 tag, tag_length,
2011 NULL, 0,
2012 &output_length );
2013 if( policy_alg == exercise_alg &&
2014 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002015 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002016 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002017 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002018
2019exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002020 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002021 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002022}
2023/* END_CASE */
2024
2025/* BEGIN_CASE */
2026void asymmetric_encryption_key_policy( int policy_usage,
2027 int policy_alg,
2028 int key_type,
2029 data_t *key_data,
2030 int exercise_alg )
2031{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002032 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002033 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002034 psa_status_t status;
2035 size_t key_bits;
2036 size_t buffer_length;
2037 unsigned char *buffer = NULL;
2038 size_t output_length;
2039
Gilles Peskine8817f612018-12-18 00:18:46 +01002040 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002041
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002042 psa_set_key_usage_flags( &attributes, policy_usage );
2043 psa_set_key_algorithm( &attributes, policy_alg );
2044 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002045
Gilles Peskine049c7532019-05-15 20:22:09 +02002046 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2047 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002048
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002049 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
2050 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002051 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2052 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002053 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002054
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002055 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002056 NULL, 0,
2057 NULL, 0,
2058 buffer, buffer_length,
2059 &output_length );
2060 if( policy_alg == exercise_alg &&
2061 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002062 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002063 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002064 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002065
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002066 if( buffer_length != 0 )
2067 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002068 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002069 buffer, buffer_length,
2070 NULL, 0,
2071 buffer, buffer_length,
2072 &output_length );
2073 if( policy_alg == exercise_alg &&
2074 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002075 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002076 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002077 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002078
2079exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002080 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002081 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002082 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002083 mbedtls_free( buffer );
2084}
2085/* END_CASE */
2086
2087/* BEGIN_CASE */
2088void asymmetric_signature_key_policy( int policy_usage,
2089 int policy_alg,
2090 int key_type,
2091 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002092 int exercise_alg,
2093 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002094{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002095 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002096 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002097 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002098 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2099 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2100 * compatible with the policy and `payload_length_arg` is supposed to be
2101 * a valid input length to sign. If `payload_length_arg <= 0`,
2102 * `exercise_alg` is supposed to be forbidden by the policy. */
2103 int compatible_alg = payload_length_arg > 0;
2104 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002105 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002106 size_t signature_length;
2107
Gilles Peskine8817f612018-12-18 00:18:46 +01002108 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002109
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002110 psa_set_key_usage_flags( &attributes, policy_usage );
2111 psa_set_key_algorithm( &attributes, policy_alg );
2112 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002113
Gilles Peskine049c7532019-05-15 20:22:09 +02002114 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2115 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002116
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002117 status = psa_sign_hash( handle, exercise_alg,
2118 payload, payload_length,
2119 signature, sizeof( signature ),
2120 &signature_length );
2121 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002122 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002123 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002124 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002125
2126 memset( signature, 0, sizeof( signature ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002127 status = psa_verify_hash( handle, exercise_alg,
2128 payload, payload_length,
2129 signature, sizeof( signature ) );
2130 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002131 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002132 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002133 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002134
2135exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002136 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002137 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002138}
2139/* END_CASE */
2140
Janos Follathba3fab92019-06-11 14:50:16 +01002141/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002142void derive_key_policy( int policy_usage,
2143 int policy_alg,
2144 int key_type,
2145 data_t *key_data,
2146 int exercise_alg )
2147{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002148 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002149 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002150 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002151 psa_status_t status;
2152
Gilles Peskine8817f612018-12-18 00:18:46 +01002153 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002154
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002155 psa_set_key_usage_flags( &attributes, policy_usage );
2156 psa_set_key_algorithm( &attributes, policy_alg );
2157 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002158
Gilles Peskine049c7532019-05-15 20:22:09 +02002159 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2160 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002161
Janos Follathba3fab92019-06-11 14:50:16 +01002162 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2163
2164 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2165 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002166 {
Janos Follathba3fab92019-06-11 14:50:16 +01002167 PSA_ASSERT( psa_key_derivation_input_bytes(
2168 &operation,
2169 PSA_KEY_DERIVATION_INPUT_SEED,
2170 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002171 }
Janos Follathba3fab92019-06-11 14:50:16 +01002172
2173 status = psa_key_derivation_input_key( &operation,
2174 PSA_KEY_DERIVATION_INPUT_SECRET,
2175 handle );
2176
Gilles Peskineea0fb492018-07-12 17:17:20 +02002177 if( policy_alg == exercise_alg &&
2178 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002179 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002180 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002181 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002182
2183exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002184 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002185 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002186 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002187}
2188/* END_CASE */
2189
2190/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002191void agreement_key_policy( int policy_usage,
2192 int policy_alg,
2193 int key_type_arg,
2194 data_t *key_data,
2195 int exercise_alg )
2196{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002197 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002198 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002199 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002200 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002201 psa_status_t status;
2202
Gilles Peskine8817f612018-12-18 00:18:46 +01002203 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002204
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002205 psa_set_key_usage_flags( &attributes, policy_usage );
2206 psa_set_key_algorithm( &attributes, policy_alg );
2207 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002208
Gilles Peskine049c7532019-05-15 20:22:09 +02002209 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2210 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002211
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002212 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2213 status = key_agreement_with_self( &operation, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002214
Gilles Peskine01d718c2018-09-18 12:01:02 +02002215 if( policy_alg == exercise_alg &&
2216 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002217 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002218 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002219 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002220
2221exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002222 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002223 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002224 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002225}
2226/* END_CASE */
2227
2228/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002229void key_policy_alg2( int key_type_arg, data_t *key_data,
2230 int usage_arg, int alg_arg, int alg2_arg )
2231{
2232 psa_key_handle_t handle = 0;
2233 psa_key_type_t key_type = key_type_arg;
2234 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2235 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2236 psa_key_usage_t usage = usage_arg;
2237 psa_algorithm_t alg = alg_arg;
2238 psa_algorithm_t alg2 = alg2_arg;
2239
2240 PSA_ASSERT( psa_crypto_init( ) );
2241
2242 psa_set_key_usage_flags( &attributes, usage );
2243 psa_set_key_algorithm( &attributes, alg );
2244 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2245 psa_set_key_type( &attributes, key_type );
2246 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2247 &handle ) );
2248
2249 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
2250 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2251 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2252 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2253
2254 if( ! exercise_key( handle, usage, alg ) )
2255 goto exit;
2256 if( ! exercise_key( handle, usage, alg2 ) )
2257 goto exit;
2258
2259exit:
2260 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002261 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002262}
2263/* END_CASE */
2264
2265/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002266void raw_agreement_key_policy( int policy_usage,
2267 int policy_alg,
2268 int key_type_arg,
2269 data_t *key_data,
2270 int exercise_alg )
2271{
2272 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002273 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002274 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002275 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002276 psa_status_t status;
2277
2278 PSA_ASSERT( psa_crypto_init( ) );
2279
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002280 psa_set_key_usage_flags( &attributes, policy_usage );
2281 psa_set_key_algorithm( &attributes, policy_alg );
2282 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002283
Gilles Peskine049c7532019-05-15 20:22:09 +02002284 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2285 &handle ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002286
2287 status = raw_key_agreement_with_self( exercise_alg, handle );
2288
2289 if( policy_alg == exercise_alg &&
2290 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
2291 PSA_ASSERT( status );
2292 else
2293 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2294
2295exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002296 psa_key_derivation_abort( &operation );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002297 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002298 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002299}
2300/* END_CASE */
2301
2302/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002303void copy_success( int source_usage_arg,
2304 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002305 int type_arg, data_t *material,
2306 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002307 int target_usage_arg,
2308 int target_alg_arg, int target_alg2_arg,
2309 int expected_usage_arg,
2310 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002311{
Gilles Peskineca25db92019-04-19 11:43:08 +02002312 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2313 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002314 psa_key_usage_t expected_usage = expected_usage_arg;
2315 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002316 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02002317 psa_key_handle_t source_handle = 0;
2318 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002319 uint8_t *export_buffer = NULL;
2320
Gilles Peskine57ab7212019-01-28 13:03:09 +01002321 PSA_ASSERT( psa_crypto_init( ) );
2322
Gilles Peskineca25db92019-04-19 11:43:08 +02002323 /* Prepare the source key. */
2324 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2325 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002326 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002327 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002328 PSA_ASSERT( psa_import_key( &source_attributes,
2329 material->x, material->len,
2330 &source_handle ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002331 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002332
Gilles Peskineca25db92019-04-19 11:43:08 +02002333 /* Prepare the target attributes. */
2334 if( copy_attributes )
2335 target_attributes = source_attributes;
2336 if( target_usage_arg != -1 )
2337 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2338 if( target_alg_arg != -1 )
2339 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002340 if( target_alg2_arg != -1 )
2341 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002342
2343 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02002344 PSA_ASSERT( psa_copy_key( source_handle,
2345 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002346
2347 /* Destroy the source to ensure that this doesn't affect the target. */
2348 PSA_ASSERT( psa_destroy_key( source_handle ) );
2349
2350 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02002351 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
2352 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2353 psa_get_key_type( &target_attributes ) );
2354 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2355 psa_get_key_bits( &target_attributes ) );
2356 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2357 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002358 TEST_EQUAL( expected_alg2,
2359 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002360 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2361 {
2362 size_t length;
2363 ASSERT_ALLOC( export_buffer, material->len );
2364 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2365 material->len, &length ) );
2366 ASSERT_COMPARE( material->x, material->len,
2367 export_buffer, length );
2368 }
2369 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2370 goto exit;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002371 if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) )
2372 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002373
2374 PSA_ASSERT( psa_close_key( target_handle ) );
2375
2376exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002377 psa_reset_key_attributes( &source_attributes );
2378 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002379 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002380 mbedtls_free( export_buffer );
2381}
2382/* END_CASE */
2383
2384/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002385void copy_fail( int source_usage_arg,
2386 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002387 int type_arg, data_t *material,
2388 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002389 int target_usage_arg,
2390 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002391 int expected_status_arg )
2392{
2393 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2394 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2395 psa_key_handle_t source_handle = 0;
2396 psa_key_handle_t target_handle = 0;
2397
2398 PSA_ASSERT( psa_crypto_init( ) );
2399
2400 /* Prepare the source key. */
2401 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2402 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002403 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002404 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002405 PSA_ASSERT( psa_import_key( &source_attributes,
2406 material->x, material->len,
2407 &source_handle ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002408
2409 /* Prepare the target attributes. */
2410 psa_set_key_type( &target_attributes, target_type_arg );
2411 psa_set_key_bits( &target_attributes, target_bits_arg );
2412 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2413 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002414 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002415
2416 /* Try to copy the key. */
2417 TEST_EQUAL( psa_copy_key( source_handle,
2418 &target_attributes, &target_handle ),
2419 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002420
2421 PSA_ASSERT( psa_destroy_key( source_handle ) );
2422
Gilles Peskine4a644642019-05-03 17:14:08 +02002423exit:
2424 psa_reset_key_attributes( &source_attributes );
2425 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002426 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002427}
2428/* END_CASE */
2429
2430/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002431void hash_operation_init( )
2432{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002433 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002434 /* Test each valid way of initializing the object, except for `= {0}`, as
2435 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2436 * though it's OK by the C standard. We could test for this, but we'd need
2437 * to supress the Clang warning for the test. */
2438 psa_hash_operation_t func = psa_hash_operation_init( );
2439 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2440 psa_hash_operation_t zero;
2441
2442 memset( &zero, 0, sizeof( zero ) );
2443
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002444 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002445 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2446 PSA_ERROR_BAD_STATE );
2447 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2448 PSA_ERROR_BAD_STATE );
2449 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2450 PSA_ERROR_BAD_STATE );
2451
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002452 /* A default hash operation should be abortable without error. */
2453 PSA_ASSERT( psa_hash_abort( &func ) );
2454 PSA_ASSERT( psa_hash_abort( &init ) );
2455 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002456}
2457/* END_CASE */
2458
2459/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002460void hash_setup( int alg_arg,
2461 int expected_status_arg )
2462{
2463 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002464 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002465 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002466 psa_status_t status;
2467
Gilles Peskine8817f612018-12-18 00:18:46 +01002468 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002469
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002470 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002471 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002472
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002473 /* Whether setup succeeded or failed, abort must succeed. */
2474 PSA_ASSERT( psa_hash_abort( &operation ) );
2475
2476 /* If setup failed, reproduce the failure, so as to
2477 * test the resulting state of the operation object. */
2478 if( status != PSA_SUCCESS )
2479 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2480
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002481 /* Now the operation object should be reusable. */
2482#if defined(KNOWN_SUPPORTED_HASH_ALG)
2483 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2484 PSA_ASSERT( psa_hash_abort( &operation ) );
2485#endif
2486
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002487exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002488 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002489}
2490/* END_CASE */
2491
2492/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002493void hash_compute_fail( int alg_arg, data_t *input,
2494 int output_size_arg, int expected_status_arg )
2495{
2496 psa_algorithm_t alg = alg_arg;
2497 uint8_t *output = NULL;
2498 size_t output_size = output_size_arg;
2499 size_t output_length = INVALID_EXPORT_LENGTH;
2500 psa_status_t expected_status = expected_status_arg;
2501 psa_status_t status;
2502
2503 ASSERT_ALLOC( output, output_size );
2504
2505 PSA_ASSERT( psa_crypto_init( ) );
2506
2507 status = psa_hash_compute( alg, input->x, input->len,
2508 output, output_size, &output_length );
2509 TEST_EQUAL( status, expected_status );
2510 TEST_ASSERT( output_length <= output_size );
2511
2512exit:
2513 mbedtls_free( output );
2514 PSA_DONE( );
2515}
2516/* END_CASE */
2517
2518/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002519void hash_compare_fail( int alg_arg, data_t *input,
2520 data_t *reference_hash,
2521 int expected_status_arg )
2522{
2523 psa_algorithm_t alg = alg_arg;
2524 psa_status_t expected_status = expected_status_arg;
2525 psa_status_t status;
2526
2527 PSA_ASSERT( psa_crypto_init( ) );
2528
2529 status = psa_hash_compare( alg, input->x, input->len,
2530 reference_hash->x, reference_hash->len );
2531 TEST_EQUAL( status, expected_status );
2532
2533exit:
2534 PSA_DONE( );
2535}
2536/* END_CASE */
2537
2538/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002539void hash_compute_compare( int alg_arg, data_t *input,
2540 data_t *expected_output )
2541{
2542 psa_algorithm_t alg = alg_arg;
2543 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2544 size_t output_length = INVALID_EXPORT_LENGTH;
2545 size_t i;
2546
2547 PSA_ASSERT( psa_crypto_init( ) );
2548
2549 /* Compute with tight buffer */
2550 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2551 output, PSA_HASH_SIZE( alg ),
2552 &output_length ) );
2553 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2554 ASSERT_COMPARE( output, output_length,
2555 expected_output->x, expected_output->len );
2556
2557 /* Compute with larger buffer */
2558 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2559 output, sizeof( output ),
2560 &output_length ) );
2561 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2562 ASSERT_COMPARE( output, output_length,
2563 expected_output->x, expected_output->len );
2564
2565 /* Compare with correct hash */
2566 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2567 output, output_length ) );
2568
2569 /* Compare with trailing garbage */
2570 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2571 output, output_length + 1 ),
2572 PSA_ERROR_INVALID_SIGNATURE );
2573
2574 /* Compare with truncated hash */
2575 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2576 output, output_length - 1 ),
2577 PSA_ERROR_INVALID_SIGNATURE );
2578
2579 /* Compare with corrupted value */
2580 for( i = 0; i < output_length; i++ )
2581 {
2582 test_set_step( i );
2583 output[i] ^= 1;
2584 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2585 output, output_length ),
2586 PSA_ERROR_INVALID_SIGNATURE );
2587 output[i] ^= 1;
2588 }
2589
2590exit:
2591 PSA_DONE( );
2592}
2593/* END_CASE */
2594
2595/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002596void hash_bad_order( )
2597{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002598 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002599 unsigned char input[] = "";
2600 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002601 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002602 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2603 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2604 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002605 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002606 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002607 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002608
Gilles Peskine8817f612018-12-18 00:18:46 +01002609 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002610
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002611 /* Call setup twice in a row. */
2612 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2613 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2614 PSA_ERROR_BAD_STATE );
2615 PSA_ASSERT( psa_hash_abort( &operation ) );
2616
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002617 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002618 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002619 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002620 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002621
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002622 /* Call update after finish. */
2623 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2624 PSA_ASSERT( psa_hash_finish( &operation,
2625 hash, sizeof( hash ), &hash_len ) );
2626 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002627 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002628 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002629
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002630 /* Call verify without calling setup beforehand. */
2631 TEST_EQUAL( psa_hash_verify( &operation,
2632 valid_hash, sizeof( valid_hash ) ),
2633 PSA_ERROR_BAD_STATE );
2634 PSA_ASSERT( psa_hash_abort( &operation ) );
2635
2636 /* Call verify after finish. */
2637 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2638 PSA_ASSERT( psa_hash_finish( &operation,
2639 hash, sizeof( hash ), &hash_len ) );
2640 TEST_EQUAL( psa_hash_verify( &operation,
2641 valid_hash, sizeof( valid_hash ) ),
2642 PSA_ERROR_BAD_STATE );
2643 PSA_ASSERT( psa_hash_abort( &operation ) );
2644
2645 /* Call verify twice in a row. */
2646 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2647 PSA_ASSERT( psa_hash_verify( &operation,
2648 valid_hash, sizeof( valid_hash ) ) );
2649 TEST_EQUAL( psa_hash_verify( &operation,
2650 valid_hash, sizeof( valid_hash ) ),
2651 PSA_ERROR_BAD_STATE );
2652 PSA_ASSERT( psa_hash_abort( &operation ) );
2653
2654 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002655 TEST_EQUAL( psa_hash_finish( &operation,
2656 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002657 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002658 PSA_ASSERT( psa_hash_abort( &operation ) );
2659
2660 /* Call finish twice in a row. */
2661 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2662 PSA_ASSERT( psa_hash_finish( &operation,
2663 hash, sizeof( hash ), &hash_len ) );
2664 TEST_EQUAL( psa_hash_finish( &operation,
2665 hash, sizeof( hash ), &hash_len ),
2666 PSA_ERROR_BAD_STATE );
2667 PSA_ASSERT( psa_hash_abort( &operation ) );
2668
2669 /* Call finish after calling verify. */
2670 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2671 PSA_ASSERT( psa_hash_verify( &operation,
2672 valid_hash, sizeof( valid_hash ) ) );
2673 TEST_EQUAL( psa_hash_finish( &operation,
2674 hash, sizeof( hash ), &hash_len ),
2675 PSA_ERROR_BAD_STATE );
2676 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002677
2678exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002679 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002680}
2681/* END_CASE */
2682
itayzafrir27e69452018-11-01 14:26:34 +02002683/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2684void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002685{
2686 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002687 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2688 * appended to it */
2689 unsigned char hash[] = {
2690 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2691 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2692 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002693 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002694 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002695
Gilles Peskine8817f612018-12-18 00:18:46 +01002696 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002697
itayzafrir27e69452018-11-01 14:26:34 +02002698 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002699 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002700 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002701 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002702
itayzafrir27e69452018-11-01 14:26:34 +02002703 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002704 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002705 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002706 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002707
itayzafrir27e69452018-11-01 14:26:34 +02002708 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002709 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002710 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002711 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002712
itayzafrirec93d302018-10-18 18:01:10 +03002713exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002714 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002715}
2716/* END_CASE */
2717
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002718/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2719void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002720{
2721 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002722 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002723 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002724 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002725 size_t hash_len;
2726
Gilles Peskine8817f612018-12-18 00:18:46 +01002727 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002728
itayzafrir58028322018-10-25 10:22:01 +03002729 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002730 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002731 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002732 hash, expected_size - 1, &hash_len ),
2733 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002734
2735exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002736 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002737}
2738/* END_CASE */
2739
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002740/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2741void hash_clone_source_state( )
2742{
2743 psa_algorithm_t alg = PSA_ALG_SHA_256;
2744 unsigned char hash[PSA_HASH_MAX_SIZE];
2745 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2746 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2747 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2748 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2749 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2750 size_t hash_len;
2751
2752 PSA_ASSERT( psa_crypto_init( ) );
2753 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2754
2755 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2756 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2757 PSA_ASSERT( psa_hash_finish( &op_finished,
2758 hash, sizeof( hash ), &hash_len ) );
2759 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2760 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2761
2762 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2763 PSA_ERROR_BAD_STATE );
2764
2765 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2766 PSA_ASSERT( psa_hash_finish( &op_init,
2767 hash, sizeof( hash ), &hash_len ) );
2768 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2769 PSA_ASSERT( psa_hash_finish( &op_finished,
2770 hash, sizeof( hash ), &hash_len ) );
2771 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2772 PSA_ASSERT( psa_hash_finish( &op_aborted,
2773 hash, sizeof( hash ), &hash_len ) );
2774
2775exit:
2776 psa_hash_abort( &op_source );
2777 psa_hash_abort( &op_init );
2778 psa_hash_abort( &op_setup );
2779 psa_hash_abort( &op_finished );
2780 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002781 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002782}
2783/* END_CASE */
2784
2785/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2786void hash_clone_target_state( )
2787{
2788 psa_algorithm_t alg = PSA_ALG_SHA_256;
2789 unsigned char hash[PSA_HASH_MAX_SIZE];
2790 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2791 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2792 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2793 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2794 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2795 size_t hash_len;
2796
2797 PSA_ASSERT( psa_crypto_init( ) );
2798
2799 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2800 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2801 PSA_ASSERT( psa_hash_finish( &op_finished,
2802 hash, sizeof( hash ), &hash_len ) );
2803 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2804 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2805
2806 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2807 PSA_ASSERT( psa_hash_finish( &op_target,
2808 hash, sizeof( hash ), &hash_len ) );
2809
2810 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2811 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2812 PSA_ERROR_BAD_STATE );
2813 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2814 PSA_ERROR_BAD_STATE );
2815
2816exit:
2817 psa_hash_abort( &op_target );
2818 psa_hash_abort( &op_init );
2819 psa_hash_abort( &op_setup );
2820 psa_hash_abort( &op_finished );
2821 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002822 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002823}
2824/* END_CASE */
2825
itayzafrir58028322018-10-25 10:22:01 +03002826/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002827void mac_operation_init( )
2828{
Jaeden Amero252ef282019-02-15 14:05:35 +00002829 const uint8_t input[1] = { 0 };
2830
Jaeden Amero769ce272019-01-04 11:48:03 +00002831 /* Test each valid way of initializing the object, except for `= {0}`, as
2832 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2833 * though it's OK by the C standard. We could test for this, but we'd need
2834 * to supress the Clang warning for the test. */
2835 psa_mac_operation_t func = psa_mac_operation_init( );
2836 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2837 psa_mac_operation_t zero;
2838
2839 memset( &zero, 0, sizeof( zero ) );
2840
Jaeden Amero252ef282019-02-15 14:05:35 +00002841 /* A freshly-initialized MAC operation should not be usable. */
2842 TEST_EQUAL( psa_mac_update( &func,
2843 input, sizeof( input ) ),
2844 PSA_ERROR_BAD_STATE );
2845 TEST_EQUAL( psa_mac_update( &init,
2846 input, sizeof( input ) ),
2847 PSA_ERROR_BAD_STATE );
2848 TEST_EQUAL( psa_mac_update( &zero,
2849 input, sizeof( input ) ),
2850 PSA_ERROR_BAD_STATE );
2851
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002852 /* A default MAC operation should be abortable without error. */
2853 PSA_ASSERT( psa_mac_abort( &func ) );
2854 PSA_ASSERT( psa_mac_abort( &init ) );
2855 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002856}
2857/* END_CASE */
2858
2859/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002860void mac_setup( int key_type_arg,
2861 data_t *key,
2862 int alg_arg,
2863 int expected_status_arg )
2864{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002865 psa_key_type_t key_type = key_type_arg;
2866 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002867 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002868 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002869 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2870#if defined(KNOWN_SUPPORTED_MAC_ALG)
2871 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2872#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002873
Gilles Peskine8817f612018-12-18 00:18:46 +01002874 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002875
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002876 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2877 &operation, &status ) )
2878 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002879 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002880
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002881 /* The operation object should be reusable. */
2882#if defined(KNOWN_SUPPORTED_MAC_ALG)
2883 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2884 smoke_test_key_data,
2885 sizeof( smoke_test_key_data ),
2886 KNOWN_SUPPORTED_MAC_ALG,
2887 &operation, &status ) )
2888 goto exit;
2889 TEST_EQUAL( status, PSA_SUCCESS );
2890#endif
2891
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002892exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002893 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002894}
2895/* END_CASE */
2896
2897/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002898void mac_bad_order( )
2899{
2900 psa_key_handle_t handle = 0;
2901 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2902 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2903 const uint8_t key[] = {
2904 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2905 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2906 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002907 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002908 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2909 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2910 size_t sign_mac_length = 0;
2911 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2912 const uint8_t verify_mac[] = {
2913 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2914 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2915 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2916
2917 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002918 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002919 psa_set_key_algorithm( &attributes, alg );
2920 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002921
Gilles Peskine73676cb2019-05-15 20:15:10 +02002922 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002923
Jaeden Amero252ef282019-02-15 14:05:35 +00002924 /* Call update without calling setup beforehand. */
2925 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2926 PSA_ERROR_BAD_STATE );
2927 PSA_ASSERT( psa_mac_abort( &operation ) );
2928
2929 /* Call sign finish without calling setup beforehand. */
2930 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2931 &sign_mac_length),
2932 PSA_ERROR_BAD_STATE );
2933 PSA_ASSERT( psa_mac_abort( &operation ) );
2934
2935 /* Call verify finish without calling setup beforehand. */
2936 TEST_EQUAL( psa_mac_verify_finish( &operation,
2937 verify_mac, sizeof( verify_mac ) ),
2938 PSA_ERROR_BAD_STATE );
2939 PSA_ASSERT( psa_mac_abort( &operation ) );
2940
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002941 /* Call setup twice in a row. */
2942 PSA_ASSERT( psa_mac_sign_setup( &operation,
2943 handle, alg ) );
2944 TEST_EQUAL( psa_mac_sign_setup( &operation,
2945 handle, alg ),
2946 PSA_ERROR_BAD_STATE );
2947 PSA_ASSERT( psa_mac_abort( &operation ) );
2948
Jaeden Amero252ef282019-02-15 14:05:35 +00002949 /* Call update after sign finish. */
2950 PSA_ASSERT( psa_mac_sign_setup( &operation,
2951 handle, alg ) );
2952 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2953 PSA_ASSERT( psa_mac_sign_finish( &operation,
2954 sign_mac, sizeof( sign_mac ),
2955 &sign_mac_length ) );
2956 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2957 PSA_ERROR_BAD_STATE );
2958 PSA_ASSERT( psa_mac_abort( &operation ) );
2959
2960 /* Call update after verify finish. */
2961 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002962 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002963 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2964 PSA_ASSERT( psa_mac_verify_finish( &operation,
2965 verify_mac, sizeof( verify_mac ) ) );
2966 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2967 PSA_ERROR_BAD_STATE );
2968 PSA_ASSERT( psa_mac_abort( &operation ) );
2969
2970 /* Call sign finish twice in a row. */
2971 PSA_ASSERT( psa_mac_sign_setup( &operation,
2972 handle, alg ) );
2973 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2974 PSA_ASSERT( psa_mac_sign_finish( &operation,
2975 sign_mac, sizeof( sign_mac ),
2976 &sign_mac_length ) );
2977 TEST_EQUAL( psa_mac_sign_finish( &operation,
2978 sign_mac, sizeof( sign_mac ),
2979 &sign_mac_length ),
2980 PSA_ERROR_BAD_STATE );
2981 PSA_ASSERT( psa_mac_abort( &operation ) );
2982
2983 /* Call verify finish twice in a row. */
2984 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002985 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002986 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2987 PSA_ASSERT( psa_mac_verify_finish( &operation,
2988 verify_mac, sizeof( verify_mac ) ) );
2989 TEST_EQUAL( psa_mac_verify_finish( &operation,
2990 verify_mac, sizeof( verify_mac ) ),
2991 PSA_ERROR_BAD_STATE );
2992 PSA_ASSERT( psa_mac_abort( &operation ) );
2993
2994 /* Setup sign but try verify. */
2995 PSA_ASSERT( psa_mac_sign_setup( &operation,
2996 handle, alg ) );
2997 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2998 TEST_EQUAL( psa_mac_verify_finish( &operation,
2999 verify_mac, sizeof( verify_mac ) ),
3000 PSA_ERROR_BAD_STATE );
3001 PSA_ASSERT( psa_mac_abort( &operation ) );
3002
3003 /* Setup verify but try sign. */
3004 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003005 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003006 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3007 TEST_EQUAL( psa_mac_sign_finish( &operation,
3008 sign_mac, sizeof( sign_mac ),
3009 &sign_mac_length ),
3010 PSA_ERROR_BAD_STATE );
3011 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003012
Gilles Peskine76b29a72019-05-28 14:08:50 +02003013 PSA_ASSERT( psa_destroy_key( handle ) );
3014
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003015exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003016 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003017}
3018/* END_CASE */
3019
3020/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003021void mac_sign( int key_type_arg,
3022 data_t *key,
3023 int alg_arg,
3024 data_t *input,
3025 data_t *expected_mac )
3026{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003027 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003028 psa_key_type_t key_type = key_type_arg;
3029 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003030 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003031 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003032 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003033 size_t mac_buffer_size =
3034 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
3035 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003036 const size_t output_sizes_to_test[] = {
3037 0,
3038 1,
3039 expected_mac->len - 1,
3040 expected_mac->len,
3041 expected_mac->len + 1,
3042 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003043
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003044 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
Gilles Peskine3d404d62020-08-25 23:47:36 +02003045 /* We expect PSA_MAC_FINAL_SIZE to be exact. */
3046 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003047
Gilles Peskine8817f612018-12-18 00:18:46 +01003048 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003049
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003050 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003051 psa_set_key_algorithm( &attributes, alg );
3052 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003053
Gilles Peskine73676cb2019-05-15 20:15:10 +02003054 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003055
Gilles Peskine8b356b52020-08-25 23:44:59 +02003056 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3057 {
3058 const size_t output_size = output_sizes_to_test[i];
3059 psa_status_t expected_status =
3060 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3061 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003062
Gilles Peskine8b356b52020-08-25 23:44:59 +02003063 test_set_step( output_size );
3064 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003065
Gilles Peskine8b356b52020-08-25 23:44:59 +02003066 /* Calculate the MAC. */
3067 PSA_ASSERT( psa_mac_sign_setup( &operation,
3068 handle, alg ) );
3069 PSA_ASSERT( psa_mac_update( &operation,
3070 input->x, input->len ) );
3071 TEST_EQUAL( psa_mac_sign_finish( &operation,
3072 actual_mac, output_size,
3073 &mac_length ),
3074 expected_status );
3075 PSA_ASSERT( psa_mac_abort( &operation ) );
3076
3077 if( expected_status == PSA_SUCCESS )
3078 {
3079 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3080 actual_mac, mac_length );
3081 }
3082 mbedtls_free( actual_mac );
3083 actual_mac = NULL;
3084 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003085
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003086exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003087 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003088 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003089 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003090 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003091}
3092/* END_CASE */
3093
3094/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003095void mac_verify( int key_type_arg,
3096 data_t *key,
3097 int alg_arg,
3098 data_t *input,
3099 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003100{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003101 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003102 psa_key_type_t key_type = key_type_arg;
3103 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003104 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003105 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003106 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003107
Gilles Peskine69c12672018-06-28 00:07:19 +02003108 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3109
Gilles Peskine8817f612018-12-18 00:18:46 +01003110 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003111
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003112 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003113 psa_set_key_algorithm( &attributes, alg );
3114 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003115
Gilles Peskine73676cb2019-05-15 20:15:10 +02003116 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003117
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003118 /* Test the correct MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003119 PSA_ASSERT( psa_mac_verify_setup( &operation,
3120 handle, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003121 PSA_ASSERT( psa_mac_update( &operation,
3122 input->x, input->len ) );
3123 PSA_ASSERT( psa_mac_verify_finish( &operation,
3124 expected_mac->x,
3125 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003126
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003127 /* Test a MAC that's too short. */
3128 PSA_ASSERT( psa_mac_verify_setup( &operation,
3129 handle, alg ) );
3130 PSA_ASSERT( psa_mac_update( &operation,
3131 input->x, input->len ) );
3132 TEST_EQUAL( psa_mac_verify_finish( &operation,
3133 expected_mac->x,
3134 expected_mac->len - 1 ),
3135 PSA_ERROR_INVALID_SIGNATURE );
3136
3137 /* Test a MAC that's too long. */
3138 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3139 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
3140 PSA_ASSERT( psa_mac_verify_setup( &operation,
3141 handle, alg ) );
3142 PSA_ASSERT( psa_mac_update( &operation,
3143 input->x, input->len ) );
3144 TEST_EQUAL( psa_mac_verify_finish( &operation,
3145 perturbed_mac,
3146 expected_mac->len + 1 ),
3147 PSA_ERROR_INVALID_SIGNATURE );
3148
3149 /* Test changing one byte. */
3150 for( size_t i = 0; i < expected_mac->len; i++ )
3151 {
3152 test_set_step( i );
3153 perturbed_mac[i] ^= 1;
3154 PSA_ASSERT( psa_mac_verify_setup( &operation,
3155 handle, alg ) );
3156 PSA_ASSERT( psa_mac_update( &operation,
3157 input->x, input->len ) );
3158 TEST_EQUAL( psa_mac_verify_finish( &operation,
3159 perturbed_mac,
3160 expected_mac->len ),
3161 PSA_ERROR_INVALID_SIGNATURE );
3162 perturbed_mac[i] ^= 1;
3163 }
3164
Gilles Peskine8c9def32018-02-08 10:02:12 +01003165exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003166 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003167 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003168 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003169 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003170}
3171/* END_CASE */
3172
3173/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003174void cipher_operation_init( )
3175{
Jaeden Ameroab439972019-02-15 14:12:05 +00003176 const uint8_t input[1] = { 0 };
3177 unsigned char output[1] = { 0 };
3178 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003179 /* Test each valid way of initializing the object, except for `= {0}`, as
3180 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3181 * though it's OK by the C standard. We could test for this, but we'd need
3182 * to supress the Clang warning for the test. */
3183 psa_cipher_operation_t func = psa_cipher_operation_init( );
3184 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3185 psa_cipher_operation_t zero;
3186
3187 memset( &zero, 0, sizeof( zero ) );
3188
Jaeden Ameroab439972019-02-15 14:12:05 +00003189 /* A freshly-initialized cipher operation should not be usable. */
3190 TEST_EQUAL( psa_cipher_update( &func,
3191 input, sizeof( input ),
3192 output, sizeof( output ),
3193 &output_length ),
3194 PSA_ERROR_BAD_STATE );
3195 TEST_EQUAL( psa_cipher_update( &init,
3196 input, sizeof( input ),
3197 output, sizeof( output ),
3198 &output_length ),
3199 PSA_ERROR_BAD_STATE );
3200 TEST_EQUAL( psa_cipher_update( &zero,
3201 input, sizeof( input ),
3202 output, sizeof( output ),
3203 &output_length ),
3204 PSA_ERROR_BAD_STATE );
3205
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003206 /* A default cipher operation should be abortable without error. */
3207 PSA_ASSERT( psa_cipher_abort( &func ) );
3208 PSA_ASSERT( psa_cipher_abort( &init ) );
3209 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003210}
3211/* END_CASE */
3212
3213/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003214void cipher_setup( int key_type_arg,
3215 data_t *key,
3216 int alg_arg,
3217 int expected_status_arg )
3218{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003219 psa_key_type_t key_type = key_type_arg;
3220 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003221 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003222 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003223 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003224#if defined(KNOWN_SUPPORTED_MAC_ALG)
3225 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3226#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003227
Gilles Peskine8817f612018-12-18 00:18:46 +01003228 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003229
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003230 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3231 &operation, &status ) )
3232 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003233 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003234
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003235 /* The operation object should be reusable. */
3236#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3237 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3238 smoke_test_key_data,
3239 sizeof( smoke_test_key_data ),
3240 KNOWN_SUPPORTED_CIPHER_ALG,
3241 &operation, &status ) )
3242 goto exit;
3243 TEST_EQUAL( status, PSA_SUCCESS );
3244#endif
3245
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003246exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003247 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003248 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003249}
3250/* END_CASE */
3251
3252/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00003253void cipher_bad_order( )
3254{
3255 psa_key_handle_t handle = 0;
3256 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3257 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003258 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003259 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3260 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3261 const uint8_t key[] = {
3262 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3263 0xaa, 0xaa, 0xaa, 0xaa };
3264 const uint8_t text[] = {
3265 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3266 0xbb, 0xbb, 0xbb, 0xbb };
3267 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3268 size_t length = 0;
3269
3270 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003271 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3272 psa_set_key_algorithm( &attributes, alg );
3273 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02003274 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003275
3276
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003277 /* Call encrypt setup twice in a row. */
3278 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3279 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
3280 PSA_ERROR_BAD_STATE );
3281 PSA_ASSERT( psa_cipher_abort( &operation ) );
3282
3283 /* Call decrypt setup twice in a row. */
3284 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
3285 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
3286 PSA_ERROR_BAD_STATE );
3287 PSA_ASSERT( psa_cipher_abort( &operation ) );
3288
Jaeden Ameroab439972019-02-15 14:12:05 +00003289 /* Generate an IV without calling setup beforehand. */
3290 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3291 buffer, sizeof( buffer ),
3292 &length ),
3293 PSA_ERROR_BAD_STATE );
3294 PSA_ASSERT( psa_cipher_abort( &operation ) );
3295
3296 /* Generate an IV twice in a row. */
3297 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3298 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3299 buffer, sizeof( buffer ),
3300 &length ) );
3301 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3302 buffer, sizeof( buffer ),
3303 &length ),
3304 PSA_ERROR_BAD_STATE );
3305 PSA_ASSERT( psa_cipher_abort( &operation ) );
3306
3307 /* Generate an IV after it's already set. */
3308 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3309 PSA_ASSERT( psa_cipher_set_iv( &operation,
3310 iv, sizeof( iv ) ) );
3311 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3312 buffer, sizeof( buffer ),
3313 &length ),
3314 PSA_ERROR_BAD_STATE );
3315 PSA_ASSERT( psa_cipher_abort( &operation ) );
3316
3317 /* Set an IV without calling setup beforehand. */
3318 TEST_EQUAL( psa_cipher_set_iv( &operation,
3319 iv, sizeof( iv ) ),
3320 PSA_ERROR_BAD_STATE );
3321 PSA_ASSERT( psa_cipher_abort( &operation ) );
3322
3323 /* Set an IV after it's already set. */
3324 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3325 PSA_ASSERT( psa_cipher_set_iv( &operation,
3326 iv, sizeof( iv ) ) );
3327 TEST_EQUAL( psa_cipher_set_iv( &operation,
3328 iv, sizeof( iv ) ),
3329 PSA_ERROR_BAD_STATE );
3330 PSA_ASSERT( psa_cipher_abort( &operation ) );
3331
3332 /* Set an IV after it's already generated. */
3333 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3334 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3335 buffer, sizeof( buffer ),
3336 &length ) );
3337 TEST_EQUAL( psa_cipher_set_iv( &operation,
3338 iv, sizeof( iv ) ),
3339 PSA_ERROR_BAD_STATE );
3340 PSA_ASSERT( psa_cipher_abort( &operation ) );
3341
3342 /* Call update without calling setup beforehand. */
3343 TEST_EQUAL( psa_cipher_update( &operation,
3344 text, sizeof( text ),
3345 buffer, sizeof( buffer ),
3346 &length ),
3347 PSA_ERROR_BAD_STATE );
3348 PSA_ASSERT( psa_cipher_abort( &operation ) );
3349
3350 /* Call update without an IV where an IV is required. */
3351 TEST_EQUAL( psa_cipher_update( &operation,
3352 text, sizeof( text ),
3353 buffer, sizeof( buffer ),
3354 &length ),
3355 PSA_ERROR_BAD_STATE );
3356 PSA_ASSERT( psa_cipher_abort( &operation ) );
3357
3358 /* Call update after finish. */
3359 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3360 PSA_ASSERT( psa_cipher_set_iv( &operation,
3361 iv, sizeof( iv ) ) );
3362 PSA_ASSERT( psa_cipher_finish( &operation,
3363 buffer, sizeof( buffer ), &length ) );
3364 TEST_EQUAL( psa_cipher_update( &operation,
3365 text, sizeof( text ),
3366 buffer, sizeof( buffer ),
3367 &length ),
3368 PSA_ERROR_BAD_STATE );
3369 PSA_ASSERT( psa_cipher_abort( &operation ) );
3370
3371 /* Call finish without calling setup beforehand. */
3372 TEST_EQUAL( psa_cipher_finish( &operation,
3373 buffer, sizeof( buffer ), &length ),
3374 PSA_ERROR_BAD_STATE );
3375 PSA_ASSERT( psa_cipher_abort( &operation ) );
3376
3377 /* Call finish without an IV where an IV is required. */
3378 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3379 /* Not calling update means we are encrypting an empty buffer, which is OK
3380 * for cipher modes with padding. */
3381 TEST_EQUAL( psa_cipher_finish( &operation,
3382 buffer, sizeof( buffer ), &length ),
3383 PSA_ERROR_BAD_STATE );
3384 PSA_ASSERT( psa_cipher_abort( &operation ) );
3385
3386 /* Call finish twice in a row. */
3387 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3388 PSA_ASSERT( psa_cipher_set_iv( &operation,
3389 iv, sizeof( iv ) ) );
3390 PSA_ASSERT( psa_cipher_finish( &operation,
3391 buffer, sizeof( buffer ), &length ) );
3392 TEST_EQUAL( psa_cipher_finish( &operation,
3393 buffer, sizeof( buffer ), &length ),
3394 PSA_ERROR_BAD_STATE );
3395 PSA_ASSERT( psa_cipher_abort( &operation ) );
3396
Gilles Peskine76b29a72019-05-28 14:08:50 +02003397 PSA_ASSERT( psa_destroy_key( handle ) );
3398
Jaeden Ameroab439972019-02-15 14:12:05 +00003399exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003400 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003401 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003402}
3403/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003404
Gilles Peskine50e586b2018-06-08 14:28:46 +02003405/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003406void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003407 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003408 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003409 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003410{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003411 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003412 psa_status_t status;
3413 psa_key_type_t key_type = key_type_arg;
3414 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003415 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003416 unsigned char *output = NULL;
3417 size_t output_buffer_size = 0;
3418 size_t function_output_length = 0;
3419 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003420 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003421 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003422
Gilles Peskine8817f612018-12-18 00:18:46 +01003423 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003424
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003425 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3426 psa_set_key_algorithm( &attributes, alg );
3427 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003428
Gilles Peskine73676cb2019-05-15 20:15:10 +02003429 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003430
Gilles Peskine8817f612018-12-18 00:18:46 +01003431 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3432 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003433
Gilles Peskine423005e2019-05-06 15:22:57 +02003434 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003435 output_buffer_size = ( (size_t) input->len +
3436 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003437 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003438
Gilles Peskine8817f612018-12-18 00:18:46 +01003439 PSA_ASSERT( psa_cipher_update( &operation,
3440 input->x, input->len,
3441 output, output_buffer_size,
3442 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003443 total_output_length += function_output_length;
3444 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003445 output + total_output_length,
3446 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003447 &function_output_length );
3448 total_output_length += function_output_length;
3449
Gilles Peskinefe11b722018-12-18 00:24:04 +01003450 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003451 if( expected_status == PSA_SUCCESS )
3452 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003453 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003454 ASSERT_COMPARE( expected_output->x, expected_output->len,
3455 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003456 }
3457
3458exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003459 psa_cipher_abort( &operation );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003460 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003461 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003462 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003463}
3464/* END_CASE */
3465
3466/* BEGIN_CASE */
3467void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003468 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003469 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003470 int first_part_size_arg,
3471 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003472 data_t *expected_output )
3473{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003474 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003475 psa_key_type_t key_type = key_type_arg;
3476 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003477 size_t first_part_size = first_part_size_arg;
3478 size_t output1_length = output1_length_arg;
3479 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003480 unsigned char *output = NULL;
3481 size_t output_buffer_size = 0;
3482 size_t function_output_length = 0;
3483 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003484 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003485 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003486
Gilles Peskine8817f612018-12-18 00:18:46 +01003487 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003488
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003489 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3490 psa_set_key_algorithm( &attributes, alg );
3491 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003492
Gilles Peskine73676cb2019-05-15 20:15:10 +02003493 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003494
Gilles Peskine8817f612018-12-18 00:18:46 +01003495 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3496 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003497
Gilles Peskine423005e2019-05-06 15:22:57 +02003498 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003499 output_buffer_size = ( (size_t) input->len +
3500 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003501 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003502
Gilles Peskinee0866522019-02-19 19:44:00 +01003503 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003504 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3505 output, output_buffer_size,
3506 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003507 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003508 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003509 PSA_ASSERT( psa_cipher_update( &operation,
3510 input->x + first_part_size,
3511 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003512 output + total_output_length,
3513 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003514 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003515 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003516 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003517 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003518 output + total_output_length,
3519 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003520 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003521 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003522 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003523
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003524 ASSERT_COMPARE( expected_output->x, expected_output->len,
3525 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003526
3527exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003528 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003529 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003530 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003531 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003532}
3533/* END_CASE */
3534
3535/* BEGIN_CASE */
3536void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003537 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003538 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003539 int first_part_size_arg,
3540 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003541 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003542{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003543 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003544
3545 psa_key_type_t key_type = key_type_arg;
3546 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003547 size_t first_part_size = first_part_size_arg;
3548 size_t output1_length = output1_length_arg;
3549 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003550 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003551 size_t output_buffer_size = 0;
3552 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003553 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003554 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003555 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003556
Gilles Peskine8817f612018-12-18 00:18:46 +01003557 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003558
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003559 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3560 psa_set_key_algorithm( &attributes, alg );
3561 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003562
Gilles Peskine73676cb2019-05-15 20:15:10 +02003563 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003564
Gilles Peskine8817f612018-12-18 00:18:46 +01003565 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3566 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003567
Gilles Peskine423005e2019-05-06 15:22:57 +02003568 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003569
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003570 output_buffer_size = ( (size_t) input->len +
3571 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003572 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003573
Gilles Peskinee0866522019-02-19 19:44:00 +01003574 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003575 PSA_ASSERT( psa_cipher_update( &operation,
3576 input->x, first_part_size,
3577 output, output_buffer_size,
3578 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003579 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003580 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003581 PSA_ASSERT( psa_cipher_update( &operation,
3582 input->x + first_part_size,
3583 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003584 output + total_output_length,
3585 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003586 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003587 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003588 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003589 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003590 output + total_output_length,
3591 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003592 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003593 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003594 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003595
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003596 ASSERT_COMPARE( expected_output->x, expected_output->len,
3597 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003598
3599exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003600 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003601 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003602 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003603 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003604}
3605/* END_CASE */
3606
Gilles Peskine50e586b2018-06-08 14:28:46 +02003607/* BEGIN_CASE */
3608void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003609 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003610 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003611 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003612{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003613 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003614 psa_status_t status;
3615 psa_key_type_t key_type = key_type_arg;
3616 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003617 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003618 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003619 size_t output_buffer_size = 0;
3620 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003621 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003622 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003623 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003624
Gilles Peskine8817f612018-12-18 00:18:46 +01003625 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003626
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003627 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3628 psa_set_key_algorithm( &attributes, alg );
3629 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003630
Gilles Peskine73676cb2019-05-15 20:15:10 +02003631 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003632
Gilles Peskine8817f612018-12-18 00:18:46 +01003633 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3634 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003635
Gilles Peskine423005e2019-05-06 15:22:57 +02003636 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003637
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003638 output_buffer_size = ( (size_t) input->len +
3639 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003640 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003641
Gilles Peskine8817f612018-12-18 00:18:46 +01003642 PSA_ASSERT( psa_cipher_update( &operation,
3643 input->x, input->len,
3644 output, output_buffer_size,
3645 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003646 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003647 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003648 output + total_output_length,
3649 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003650 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003651 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003652 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003653
3654 if( expected_status == PSA_SUCCESS )
3655 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003656 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003657 ASSERT_COMPARE( expected_output->x, expected_output->len,
3658 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003659 }
3660
Gilles Peskine50e586b2018-06-08 14:28:46 +02003661exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003662 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003663 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003664 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003665 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003666}
3667/* END_CASE */
3668
Gilles Peskine50e586b2018-06-08 14:28:46 +02003669/* BEGIN_CASE */
3670void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003671 data_t *key,
3672 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003673{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003674 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003675 psa_key_type_t key_type = key_type_arg;
3676 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003677 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003678 size_t iv_size = 16;
3679 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003680 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003681 size_t output1_size = 0;
3682 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003683 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003684 size_t output2_size = 0;
3685 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003686 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003687 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3688 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003689 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003690
Gilles Peskine8817f612018-12-18 00:18:46 +01003691 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003692
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003693 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3694 psa_set_key_algorithm( &attributes, alg );
3695 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003696
Gilles Peskine73676cb2019-05-15 20:15:10 +02003697 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003698
Gilles Peskine8817f612018-12-18 00:18:46 +01003699 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3700 handle, alg ) );
3701 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3702 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003703
Gilles Peskine8817f612018-12-18 00:18:46 +01003704 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3705 iv, iv_size,
3706 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003707 output1_size = ( (size_t) input->len +
3708 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003709 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003710
Gilles Peskine8817f612018-12-18 00:18:46 +01003711 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3712 output1, output1_size,
3713 &output1_length ) );
3714 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003715 output1 + output1_length,
3716 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003717 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003718
Gilles Peskine048b7f02018-06-08 14:20:49 +02003719 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003720
Gilles Peskine8817f612018-12-18 00:18:46 +01003721 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003722
3723 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003724 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003725
Gilles Peskine8817f612018-12-18 00:18:46 +01003726 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3727 iv, iv_length ) );
3728 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3729 output2, output2_size,
3730 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003731 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003732 PSA_ASSERT( psa_cipher_finish( &operation2,
3733 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003734 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003735 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003736
Gilles Peskine048b7f02018-06-08 14:20:49 +02003737 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003738
Gilles Peskine8817f612018-12-18 00:18:46 +01003739 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003740
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003741 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003742
3743exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003744 psa_cipher_abort( &operation1 );
3745 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003746 mbedtls_free( output1 );
3747 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003748 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003749 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003750}
3751/* END_CASE */
3752
3753/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003754void cipher_verify_output_multipart( int alg_arg,
3755 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003756 data_t *key,
3757 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003758 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003759{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003760 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003761 psa_key_type_t key_type = key_type_arg;
3762 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003763 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003764 unsigned char iv[16] = {0};
3765 size_t iv_size = 16;
3766 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003767 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003768 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003769 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003770 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003771 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003772 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003773 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003774 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3775 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003776 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003777
Gilles Peskine8817f612018-12-18 00:18:46 +01003778 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003779
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003780 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3781 psa_set_key_algorithm( &attributes, alg );
3782 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003783
Gilles Peskine73676cb2019-05-15 20:15:10 +02003784 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003785
Gilles Peskine8817f612018-12-18 00:18:46 +01003786 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3787 handle, alg ) );
3788 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3789 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003790
Gilles Peskine8817f612018-12-18 00:18:46 +01003791 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3792 iv, iv_size,
3793 &iv_length ) );
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003794 output1_buffer_size = ( (size_t) input->len +
3795 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003796 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003797
Gilles Peskinee0866522019-02-19 19:44:00 +01003798 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003799
Gilles Peskine8817f612018-12-18 00:18:46 +01003800 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3801 output1, output1_buffer_size,
3802 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003803 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003804
Gilles Peskine8817f612018-12-18 00:18:46 +01003805 PSA_ASSERT( psa_cipher_update( &operation1,
3806 input->x + first_part_size,
3807 input->len - first_part_size,
3808 output1, output1_buffer_size,
3809 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003810 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003811
Gilles Peskine8817f612018-12-18 00:18:46 +01003812 PSA_ASSERT( psa_cipher_finish( &operation1,
3813 output1 + output1_length,
3814 output1_buffer_size - output1_length,
3815 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003816 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003817
Gilles Peskine8817f612018-12-18 00:18:46 +01003818 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003819
Gilles Peskine048b7f02018-06-08 14:20:49 +02003820 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003821 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003822
Gilles Peskine8817f612018-12-18 00:18:46 +01003823 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3824 iv, iv_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003825
Gilles Peskine8817f612018-12-18 00:18:46 +01003826 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3827 output2, output2_buffer_size,
3828 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003829 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003830
Gilles Peskine8817f612018-12-18 00:18:46 +01003831 PSA_ASSERT( psa_cipher_update( &operation2,
3832 output1 + first_part_size,
3833 output1_length - first_part_size,
3834 output2, output2_buffer_size,
3835 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003836 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003837
Gilles Peskine8817f612018-12-18 00:18:46 +01003838 PSA_ASSERT( psa_cipher_finish( &operation2,
3839 output2 + output2_length,
3840 output2_buffer_size - output2_length,
3841 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003842 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003843
Gilles Peskine8817f612018-12-18 00:18:46 +01003844 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003845
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003846 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003847
3848exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003849 psa_cipher_abort( &operation1 );
3850 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003851 mbedtls_free( output1 );
3852 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003853 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003854 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003855}
3856/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003857
Gilles Peskine20035e32018-02-03 22:44:14 +01003858/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003859void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003860 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003861 data_t *nonce,
3862 data_t *additional_data,
3863 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003864 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003865{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003866 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003867 psa_key_type_t key_type = key_type_arg;
3868 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003869 unsigned char *output_data = NULL;
3870 size_t output_size = 0;
3871 size_t output_length = 0;
3872 unsigned char *output_data2 = NULL;
3873 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003874 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003875 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003876 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003877
Gilles Peskine4abf7412018-06-18 16:35:34 +02003878 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003879 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3880 * should be exact. */
3881 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3882 TEST_EQUAL( output_size,
3883 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003884 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003885
Gilles Peskine8817f612018-12-18 00:18:46 +01003886 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003887
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003888 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3889 psa_set_key_algorithm( &attributes, alg );
3890 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003891
Gilles Peskine049c7532019-05-15 20:22:09 +02003892 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3893 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003894
Gilles Peskinefe11b722018-12-18 00:24:04 +01003895 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3896 nonce->x, nonce->len,
3897 additional_data->x,
3898 additional_data->len,
3899 input_data->x, input_data->len,
3900 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003901 &output_length ),
3902 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003903
3904 if( PSA_SUCCESS == expected_result )
3905 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003906 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003907
Gilles Peskine003a4a92019-05-14 16:09:40 +02003908 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3909 * should be exact. */
3910 TEST_EQUAL( input_data->len,
3911 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3912
Gilles Peskinefe11b722018-12-18 00:24:04 +01003913 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3914 nonce->x, nonce->len,
3915 additional_data->x,
3916 additional_data->len,
3917 output_data, output_length,
3918 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003919 &output_length2 ),
3920 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003921
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003922 ASSERT_COMPARE( input_data->x, input_data->len,
3923 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003924 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003925
Gilles Peskinea1cac842018-06-11 19:33:02 +02003926exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003927 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003928 mbedtls_free( output_data );
3929 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003930 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003931}
3932/* END_CASE */
3933
3934/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003935void aead_encrypt( int key_type_arg, data_t *key_data,
3936 int alg_arg,
3937 data_t *nonce,
3938 data_t *additional_data,
3939 data_t *input_data,
3940 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003941{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003942 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003943 psa_key_type_t key_type = key_type_arg;
3944 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003945 unsigned char *output_data = NULL;
3946 size_t output_size = 0;
3947 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003948 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003949 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003950
Gilles Peskine4abf7412018-06-18 16:35:34 +02003951 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003952 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3953 * should be exact. */
3954 TEST_EQUAL( output_size,
3955 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003956 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003957
Gilles Peskine8817f612018-12-18 00:18:46 +01003958 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003959
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003960 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3961 psa_set_key_algorithm( &attributes, alg );
3962 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003963
Gilles Peskine049c7532019-05-15 20:22:09 +02003964 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3965 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003966
Gilles Peskine8817f612018-12-18 00:18:46 +01003967 PSA_ASSERT( psa_aead_encrypt( handle, alg,
3968 nonce->x, nonce->len,
3969 additional_data->x, additional_data->len,
3970 input_data->x, input_data->len,
3971 output_data, output_size,
3972 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003973
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003974 ASSERT_COMPARE( expected_result->x, expected_result->len,
3975 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003976
Gilles Peskinea1cac842018-06-11 19:33:02 +02003977exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003978 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003979 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003980 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003981}
3982/* END_CASE */
3983
3984/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003985void aead_decrypt( int key_type_arg, data_t *key_data,
3986 int alg_arg,
3987 data_t *nonce,
3988 data_t *additional_data,
3989 data_t *input_data,
3990 data_t *expected_data,
3991 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003992{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003993 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003994 psa_key_type_t key_type = key_type_arg;
3995 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003996 unsigned char *output_data = NULL;
3997 size_t output_size = 0;
3998 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003999 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004000 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004001 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004002
Gilles Peskine003a4a92019-05-14 16:09:40 +02004003 output_size = input_data->len - tag_length;
4004 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4005 * should be exact. */
4006 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4007 TEST_EQUAL( output_size,
4008 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004009 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004010
Gilles Peskine8817f612018-12-18 00:18:46 +01004011 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004012
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004013 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4014 psa_set_key_algorithm( &attributes, alg );
4015 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004016
Gilles Peskine049c7532019-05-15 20:22:09 +02004017 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4018 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004019
Gilles Peskinefe11b722018-12-18 00:24:04 +01004020 TEST_EQUAL( psa_aead_decrypt( handle, alg,
4021 nonce->x, nonce->len,
4022 additional_data->x,
4023 additional_data->len,
4024 input_data->x, input_data->len,
4025 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004026 &output_length ),
4027 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004028
Gilles Peskine2d277862018-06-18 15:41:12 +02004029 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004030 ASSERT_COMPARE( expected_data->x, expected_data->len,
4031 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004032
Gilles Peskinea1cac842018-06-11 19:33:02 +02004033exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004034 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004035 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004036 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004037}
4038/* END_CASE */
4039
4040/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004041void signature_size( int type_arg,
4042 int bits,
4043 int alg_arg,
4044 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004045{
4046 psa_key_type_t type = type_arg;
4047 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004048 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004049
Gilles Peskinefe11b722018-12-18 00:24:04 +01004050 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004051#if defined(MBEDTLS_TEST_DEPRECATED)
4052 TEST_EQUAL( actual_size,
4053 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4054#endif /* MBEDTLS_TEST_DEPRECATED */
4055
Gilles Peskinee59236f2018-01-27 23:32:46 +01004056exit:
4057 ;
4058}
4059/* END_CASE */
4060
4061/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004062void sign_deterministic( int key_type_arg, data_t *key_data,
4063 int alg_arg, data_t *input_data,
4064 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004065{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004066 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004067 psa_key_type_t key_type = key_type_arg;
4068 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004069 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004070 unsigned char *signature = NULL;
4071 size_t signature_size;
4072 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004073 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004074
Gilles Peskine8817f612018-12-18 00:18:46 +01004075 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004076
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004077 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004078 psa_set_key_algorithm( &attributes, alg );
4079 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004080
Gilles Peskine049c7532019-05-15 20:22:09 +02004081 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4082 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004083 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4084 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004085
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004086 /* Allocate a buffer which has the size advertized by the
4087 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004088 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004089 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004090 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004091 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004092 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004093
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004094 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004095 PSA_ASSERT( psa_sign_hash( handle, alg,
4096 input_data->x, input_data->len,
4097 signature, signature_size,
4098 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004099 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004100 ASSERT_COMPARE( output_data->x, output_data->len,
4101 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004102
Gilles Peskine0627f982019-11-26 19:12:16 +01004103#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004104 memset( signature, 0, signature_size );
4105 signature_length = INVALID_EXPORT_LENGTH;
Gilles Peskine0627f982019-11-26 19:12:16 +01004106 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
4107 input_data->x, input_data->len,
4108 signature, signature_size,
4109 &signature_length ) );
4110 ASSERT_COMPARE( output_data->x, output_data->len,
4111 signature, signature_length );
4112#endif /* MBEDTLS_TEST_DEPRECATED */
4113
Gilles Peskine20035e32018-02-03 22:44:14 +01004114exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004115 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004116 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01004117 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004118 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004119}
4120/* END_CASE */
4121
4122/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004123void sign_fail( int key_type_arg, data_t *key_data,
4124 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004125 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004126{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004127 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01004128 psa_key_type_t key_type = key_type_arg;
4129 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004130 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004131 psa_status_t actual_status;
4132 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004133 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004134 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004135 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004136
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004137 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004138
Gilles Peskine8817f612018-12-18 00:18:46 +01004139 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004140
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004141 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004142 psa_set_key_algorithm( &attributes, alg );
4143 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004144
Gilles Peskine049c7532019-05-15 20:22:09 +02004145 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4146 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004147
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004148 actual_status = psa_sign_hash( handle, alg,
4149 input_data->x, input_data->len,
4150 signature, signature_size,
4151 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004152 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004153 /* The value of *signature_length is unspecified on error, but
4154 * whatever it is, it should be less than signature_size, so that
4155 * if the caller tries to read *signature_length bytes without
4156 * checking the error code then they don't overflow a buffer. */
4157 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004158
Gilles Peskine895242b2019-11-29 12:15:40 +01004159#if defined(MBEDTLS_TEST_DEPRECATED)
4160 signature_length = INVALID_EXPORT_LENGTH;
4161 TEST_EQUAL( psa_asymmetric_sign( handle, alg,
4162 input_data->x, input_data->len,
4163 signature, signature_size,
4164 &signature_length ),
4165 expected_status );
4166 TEST_ASSERT( signature_length <= signature_size );
4167#endif /* MBEDTLS_TEST_DEPRECATED */
4168
Gilles Peskine20035e32018-02-03 22:44:14 +01004169exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004170 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004171 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01004172 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004173 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004174}
4175/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004176
4177/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004178void sign_verify( int key_type_arg, data_t *key_data,
4179 int alg_arg, data_t *input_data )
4180{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004181 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02004182 psa_key_type_t key_type = key_type_arg;
4183 psa_algorithm_t alg = alg_arg;
4184 size_t key_bits;
4185 unsigned char *signature = NULL;
4186 size_t signature_size;
4187 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004188 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004189
Gilles Peskine8817f612018-12-18 00:18:46 +01004190 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004191
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004192 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004193 psa_set_key_algorithm( &attributes, alg );
4194 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004195
Gilles Peskine049c7532019-05-15 20:22:09 +02004196 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4197 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004198 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4199 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004200
4201 /* Allocate a buffer which has the size advertized by the
4202 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004203 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004204 key_bits, alg );
4205 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004206 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004207 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004208
4209 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004210 PSA_ASSERT( psa_sign_hash( handle, alg,
4211 input_data->x, input_data->len,
4212 signature, signature_size,
4213 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004214 /* Check that the signature length looks sensible. */
4215 TEST_ASSERT( signature_length <= signature_size );
4216 TEST_ASSERT( signature_length > 0 );
4217
4218 /* Use the library to verify that the signature is correct. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004219 PSA_ASSERT( psa_verify_hash( handle, alg,
4220 input_data->x, input_data->len,
4221 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004222
4223 if( input_data->len != 0 )
4224 {
4225 /* Flip a bit in the input and verify that the signature is now
4226 * detected as invalid. Flip a bit at the beginning, not at the end,
4227 * because ECDSA may ignore the last few bits of the input. */
4228 input_data->x[0] ^= 1;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004229 TEST_EQUAL( psa_verify_hash( handle, alg,
4230 input_data->x, input_data->len,
4231 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004232 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004233 }
4234
4235exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004236 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004237 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02004238 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004239 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004240}
4241/* END_CASE */
4242
4243/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004244void asymmetric_verify( int key_type_arg, data_t *key_data,
4245 int alg_arg, data_t *hash_data,
4246 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004247{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004248 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03004249 psa_key_type_t key_type = key_type_arg;
4250 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004251 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004252
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004253 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004254
Gilles Peskine8817f612018-12-18 00:18:46 +01004255 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004256
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004257 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004258 psa_set_key_algorithm( &attributes, alg );
4259 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004260
Gilles Peskine049c7532019-05-15 20:22:09 +02004261 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4262 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03004263
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004264 PSA_ASSERT( psa_verify_hash( handle, alg,
4265 hash_data->x, hash_data->len,
4266 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004267
4268#if defined(MBEDTLS_TEST_DEPRECATED)
4269 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
4270 hash_data->x, hash_data->len,
4271 signature_data->x,
4272 signature_data->len ) );
4273
4274#endif /* MBEDTLS_TEST_DEPRECATED */
4275
itayzafrir5c753392018-05-08 11:18:38 +03004276exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004277 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004278 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004279 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004280}
4281/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004282
4283/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004284void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4285 int alg_arg, data_t *hash_data,
4286 data_t *signature_data,
4287 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004288{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004289 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004290 psa_key_type_t key_type = key_type_arg;
4291 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004292 psa_status_t actual_status;
4293 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004294 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004295
Gilles Peskine8817f612018-12-18 00:18:46 +01004296 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004297
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004298 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004299 psa_set_key_algorithm( &attributes, alg );
4300 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004301
Gilles Peskine049c7532019-05-15 20:22:09 +02004302 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4303 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004304
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004305 actual_status = psa_verify_hash( handle, alg,
4306 hash_data->x, hash_data->len,
4307 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004308 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004309
Gilles Peskine895242b2019-11-29 12:15:40 +01004310#if defined(MBEDTLS_TEST_DEPRECATED)
4311 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
4312 hash_data->x, hash_data->len,
4313 signature_data->x, signature_data->len ),
4314 expected_status );
4315#endif /* MBEDTLS_TEST_DEPRECATED */
4316
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004317exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004318 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004319 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004320 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004321}
4322/* END_CASE */
4323
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004324/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004325void asymmetric_encrypt( int key_type_arg,
4326 data_t *key_data,
4327 int alg_arg,
4328 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004329 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004330 int expected_output_length_arg,
4331 int expected_status_arg )
4332{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004333 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02004334 psa_key_type_t key_type = key_type_arg;
4335 psa_algorithm_t alg = alg_arg;
4336 size_t expected_output_length = expected_output_length_arg;
4337 size_t key_bits;
4338 unsigned char *output = NULL;
4339 size_t output_size;
4340 size_t output_length = ~0;
4341 psa_status_t actual_status;
4342 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004343 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004344
Gilles Peskine8817f612018-12-18 00:18:46 +01004345 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004346
Gilles Peskine656896e2018-06-29 19:12:28 +02004347 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004348 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4349 psa_set_key_algorithm( &attributes, alg );
4350 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004351 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4352 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004353
4354 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004355 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4356 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004357 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004358 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004359
4360 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004361 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004362 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004363 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004364 output, output_size,
4365 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004366 TEST_EQUAL( actual_status, expected_status );
4367 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004368
Gilles Peskine68428122018-06-30 18:42:41 +02004369 /* If the label is empty, the test framework puts a non-null pointer
4370 * in label->x. Test that a null pointer works as well. */
4371 if( label->len == 0 )
4372 {
4373 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004374 if( output_size != 0 )
4375 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004376 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004377 input_data->x, input_data->len,
4378 NULL, label->len,
4379 output, output_size,
4380 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004381 TEST_EQUAL( actual_status, expected_status );
4382 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004383 }
4384
Gilles Peskine656896e2018-06-29 19:12:28 +02004385exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004386 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004387 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02004388 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004389 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004390}
4391/* END_CASE */
4392
4393/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004394void asymmetric_encrypt_decrypt( int key_type_arg,
4395 data_t *key_data,
4396 int alg_arg,
4397 data_t *input_data,
4398 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004399{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004400 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004401 psa_key_type_t key_type = key_type_arg;
4402 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004403 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004404 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004405 size_t output_size;
4406 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004407 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004408 size_t output2_size;
4409 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004410 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004411
Gilles Peskine8817f612018-12-18 00:18:46 +01004412 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004413
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004414 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4415 psa_set_key_algorithm( &attributes, alg );
4416 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004417
Gilles Peskine049c7532019-05-15 20:22:09 +02004418 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4419 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004420
4421 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004422 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4423 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004424 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004425 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004426 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004427 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004428
Gilles Peskineeebd7382018-06-08 18:11:54 +02004429 /* We test encryption by checking that encrypt-then-decrypt gives back
4430 * the original plaintext because of the non-optional random
4431 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004432 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
4433 input_data->x, input_data->len,
4434 label->x, label->len,
4435 output, output_size,
4436 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004437 /* We don't know what ciphertext length to expect, but check that
4438 * it looks sensible. */
4439 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004440
Gilles Peskine8817f612018-12-18 00:18:46 +01004441 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4442 output, output_length,
4443 label->x, label->len,
4444 output2, output2_size,
4445 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004446 ASSERT_COMPARE( input_data->x, input_data->len,
4447 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004448
4449exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004450 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004451 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004452 mbedtls_free( output );
4453 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004454 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004455}
4456/* END_CASE */
4457
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004458/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004459void asymmetric_decrypt( int key_type_arg,
4460 data_t *key_data,
4461 int alg_arg,
4462 data_t *input_data,
4463 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004464 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004465{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004466 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004467 psa_key_type_t key_type = key_type_arg;
4468 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004469 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004470 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004471 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004472 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004473
Jaeden Amero412654a2019-02-06 12:57:46 +00004474 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004475 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004476
Gilles Peskine8817f612018-12-18 00:18:46 +01004477 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004478
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004479 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4480 psa_set_key_algorithm( &attributes, alg );
4481 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004482
Gilles Peskine049c7532019-05-15 20:22:09 +02004483 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4484 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004485
Gilles Peskine8817f612018-12-18 00:18:46 +01004486 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4487 input_data->x, input_data->len,
4488 label->x, label->len,
4489 output,
4490 output_size,
4491 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004492 ASSERT_COMPARE( expected_data->x, expected_data->len,
4493 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004494
Gilles Peskine68428122018-06-30 18:42:41 +02004495 /* If the label is empty, the test framework puts a non-null pointer
4496 * in label->x. Test that a null pointer works as well. */
4497 if( label->len == 0 )
4498 {
4499 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004500 if( output_size != 0 )
4501 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004502 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4503 input_data->x, input_data->len,
4504 NULL, label->len,
4505 output,
4506 output_size,
4507 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004508 ASSERT_COMPARE( expected_data->x, expected_data->len,
4509 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004510 }
4511
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004512exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004513 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004514 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004515 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004516 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004517}
4518/* END_CASE */
4519
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004520/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004521void asymmetric_decrypt_fail( int key_type_arg,
4522 data_t *key_data,
4523 int alg_arg,
4524 data_t *input_data,
4525 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004526 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004527 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004528{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004529 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004530 psa_key_type_t key_type = key_type_arg;
4531 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004532 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004533 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004534 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004535 psa_status_t actual_status;
4536 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004537 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004538
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004539 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004540
Gilles Peskine8817f612018-12-18 00:18:46 +01004541 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004542
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004543 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4544 psa_set_key_algorithm( &attributes, alg );
4545 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004546
Gilles Peskine049c7532019-05-15 20:22:09 +02004547 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4548 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004549
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004550 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004551 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004552 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004553 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004554 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004555 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004556 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004557
Gilles Peskine68428122018-06-30 18:42:41 +02004558 /* If the label is empty, the test framework puts a non-null pointer
4559 * in label->x. Test that a null pointer works as well. */
4560 if( label->len == 0 )
4561 {
4562 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004563 if( output_size != 0 )
4564 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004565 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004566 input_data->x, input_data->len,
4567 NULL, label->len,
4568 output, output_size,
4569 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004570 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004571 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004572 }
4573
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004574exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004575 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004576 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004577 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004578 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004579}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004580/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004581
4582/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004583void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004584{
4585 /* Test each valid way of initializing the object, except for `= {0}`, as
4586 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4587 * though it's OK by the C standard. We could test for this, but we'd need
4588 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004589 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004590 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4591 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4592 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004593
4594 memset( &zero, 0, sizeof( zero ) );
4595
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004596 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004597 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004598 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004599 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004600 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004601 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004602 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004603
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004604 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004605 PSA_ASSERT( psa_key_derivation_abort(&func) );
4606 PSA_ASSERT( psa_key_derivation_abort(&init) );
4607 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004608}
4609/* END_CASE */
4610
Janos Follath16de4a42019-06-13 16:32:24 +01004611/* BEGIN_CASE */
4612void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004613{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004614 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004615 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004616 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004617
Gilles Peskine8817f612018-12-18 00:18:46 +01004618 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004619
Janos Follath16de4a42019-06-13 16:32:24 +01004620 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004621 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004622
4623exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004624 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004625 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004626}
4627/* END_CASE */
4628
Janos Follathaf3c2a02019-06-12 12:34:34 +01004629/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004630void derive_set_capacity( int alg_arg, int capacity_arg,
4631 int expected_status_arg )
4632{
4633 psa_algorithm_t alg = alg_arg;
4634 size_t capacity = capacity_arg;
4635 psa_status_t expected_status = expected_status_arg;
4636 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4637
4638 PSA_ASSERT( psa_crypto_init( ) );
4639
4640 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4641
4642 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4643 expected_status );
4644
4645exit:
4646 psa_key_derivation_abort( &operation );
4647 PSA_DONE( );
4648}
4649/* END_CASE */
4650
4651/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004652void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004653 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004654 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004655 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004656 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004657 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004658 int expected_status_arg3,
4659 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004660{
4661 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004662 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4663 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004664 psa_status_t expected_statuses[] = {expected_status_arg1,
4665 expected_status_arg2,
4666 expected_status_arg3};
4667 data_t *inputs[] = {input1, input2, input3};
4668 psa_key_handle_t handles[] = {0, 0, 0};
4669 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4670 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4671 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004672 psa_key_type_t output_key_type = output_key_type_arg;
4673 psa_key_handle_t output_handle = 0;
4674 psa_status_t expected_output_status = expected_output_status_arg;
4675 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004676
4677 PSA_ASSERT( psa_crypto_init( ) );
4678
4679 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4680 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004681
4682 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4683
4684 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4685 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004686 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004687 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004688 psa_set_key_type( &attributes, key_types[i] );
4689 PSA_ASSERT( psa_import_key( &attributes,
4690 inputs[i]->x, inputs[i]->len,
4691 &handles[i] ) );
4692 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4693 handles[i] ),
4694 expected_statuses[i] );
4695 }
4696 else
4697 {
4698 TEST_EQUAL( psa_key_derivation_input_bytes(
4699 &operation, steps[i],
4700 inputs[i]->x, inputs[i]->len ),
4701 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004702 }
4703 }
4704
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004705 if( output_key_type != PSA_KEY_TYPE_NONE )
4706 {
4707 psa_reset_key_attributes( &attributes );
4708 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4709 psa_set_key_bits( &attributes, 8 );
4710 actual_output_status =
4711 psa_key_derivation_output_key( &attributes, &operation,
4712 &output_handle );
4713 }
4714 else
4715 {
4716 uint8_t buffer[1];
4717 actual_output_status =
4718 psa_key_derivation_output_bytes( &operation,
4719 buffer, sizeof( buffer ) );
4720 }
4721 TEST_EQUAL( actual_output_status, expected_output_status );
4722
Janos Follathaf3c2a02019-06-12 12:34:34 +01004723exit:
4724 psa_key_derivation_abort( &operation );
4725 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4726 psa_destroy_key( handles[i] );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004727 psa_destroy_key( output_handle );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004728 PSA_DONE( );
4729}
4730/* END_CASE */
4731
Janos Follathd958bb72019-07-03 15:02:16 +01004732/* BEGIN_CASE */
4733void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004734{
Janos Follathd958bb72019-07-03 15:02:16 +01004735 psa_algorithm_t alg = alg_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004736 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004737 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004738 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004739 unsigned char input1[] = "Input 1";
4740 size_t input1_length = sizeof( input1 );
4741 unsigned char input2[] = "Input 2";
4742 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004743 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004744 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004745 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4746 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4747 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004748 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004749
Gilles Peskine8817f612018-12-18 00:18:46 +01004750 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004751
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004752 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4753 psa_set_key_algorithm( &attributes, alg );
4754 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004755
Gilles Peskine73676cb2019-05-15 20:15:10 +02004756 PSA_ASSERT( psa_import_key( &attributes,
4757 key_data, sizeof( key_data ),
4758 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004759
4760 /* valid key derivation */
Janos Follathd958bb72019-07-03 15:02:16 +01004761 if( !setup_key_derivation_wrap( &operation, handle, alg,
4762 input1, input1_length,
4763 input2, input2_length,
4764 capacity ) )
4765 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004766
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004767 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004768 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004769 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004770
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004771 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004772
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004773 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004774 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004775
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004776exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004777 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004778 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004779 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004780}
4781/* END_CASE */
4782
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004783/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004784void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004785{
4786 uint8_t output_buffer[16];
4787 size_t buffer_size = 16;
4788 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004789 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004790
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004791 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4792 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004793 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004794
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004795 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004796 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004797
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004798 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004799
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004800 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4801 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004802 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004803
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004804 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004805 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004806
4807exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004808 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004809}
4810/* END_CASE */
4811
4812/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004813void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004814 int step1_arg, data_t *input1,
4815 int step2_arg, data_t *input2,
4816 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004817 int requested_capacity_arg,
4818 data_t *expected_output1,
4819 data_t *expected_output2 )
4820{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004821 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004822 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4823 data_t *inputs[] = {input1, input2, input3};
4824 psa_key_handle_t handles[] = {0, 0, 0};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004825 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004826 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004827 uint8_t *expected_outputs[2] =
4828 {expected_output1->x, expected_output2->x};
4829 size_t output_sizes[2] =
4830 {expected_output1->len, expected_output2->len};
4831 size_t output_buffer_size = 0;
4832 uint8_t *output_buffer = NULL;
4833 size_t expected_capacity;
4834 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004835 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004836 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004837 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004838
4839 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4840 {
4841 if( output_sizes[i] > output_buffer_size )
4842 output_buffer_size = output_sizes[i];
4843 if( output_sizes[i] == 0 )
4844 expected_outputs[i] = NULL;
4845 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004846 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004847 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004848
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004849 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4850 psa_set_key_algorithm( &attributes, alg );
4851 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004852
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004853 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004854 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4855 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4856 requested_capacity ) );
4857 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004858 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004859 switch( steps[i] )
4860 {
4861 case 0:
4862 break;
4863 case PSA_KEY_DERIVATION_INPUT_SECRET:
4864 PSA_ASSERT( psa_import_key( &attributes,
4865 inputs[i]->x, inputs[i]->len,
4866 &handles[i] ) );
4867 PSA_ASSERT( psa_key_derivation_input_key(
4868 &operation, steps[i],
4869 handles[i] ) );
4870 break;
4871 default:
4872 PSA_ASSERT( psa_key_derivation_input_bytes(
4873 &operation, steps[i],
4874 inputs[i]->x, inputs[i]->len ) );
4875 break;
4876 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004877 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004878
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004879 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004880 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004881 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004882 expected_capacity = requested_capacity;
4883
4884 /* Expansion phase. */
4885 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4886 {
4887 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004888 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004889 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004890 if( expected_capacity == 0 && output_sizes[i] == 0 )
4891 {
4892 /* Reading 0 bytes when 0 bytes are available can go either way. */
4893 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004894 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004895 continue;
4896 }
4897 else if( expected_capacity == 0 ||
4898 output_sizes[i] > expected_capacity )
4899 {
4900 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004901 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004902 expected_capacity = 0;
4903 continue;
4904 }
4905 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004906 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004907 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004908 ASSERT_COMPARE( output_buffer, output_sizes[i],
4909 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004910 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004911 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004912 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004913 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004914 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004915 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004916 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004917
4918exit:
4919 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004920 psa_key_derivation_abort( &operation );
Gilles Peskine1468da72019-05-29 17:35:49 +02004921 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4922 psa_destroy_key( handles[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004923 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004924}
4925/* END_CASE */
4926
4927/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004928void derive_full( int alg_arg,
4929 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004930 data_t *input1,
4931 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004932 int requested_capacity_arg )
4933{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004934 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004935 psa_algorithm_t alg = alg_arg;
4936 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004937 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004938 unsigned char output_buffer[16];
4939 size_t expected_capacity = requested_capacity;
4940 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004941 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004942
Gilles Peskine8817f612018-12-18 00:18:46 +01004943 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004944
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004945 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4946 psa_set_key_algorithm( &attributes, alg );
4947 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004948
Gilles Peskine049c7532019-05-15 20:22:09 +02004949 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4950 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004951
Janos Follathf2815ea2019-07-03 12:41:36 +01004952 if( !setup_key_derivation_wrap( &operation, handle, alg,
4953 input1->x, input1->len,
4954 input2->x, input2->len,
4955 requested_capacity ) )
4956 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004957
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004958 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004959 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004960 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004961
4962 /* Expansion phase. */
4963 while( current_capacity > 0 )
4964 {
4965 size_t read_size = sizeof( output_buffer );
4966 if( read_size > current_capacity )
4967 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004968 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004969 output_buffer,
4970 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004971 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004972 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004973 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004974 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004975 }
4976
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004977 /* Check that the operation refuses to go over capacity. */
4978 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004979 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004980
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004981 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004982
4983exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004984 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004985 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004986 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004987}
4988/* END_CASE */
4989
Janos Follathe60c9052019-07-03 13:51:30 +01004990/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004991void derive_key_exercise( int alg_arg,
4992 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004993 data_t *input1,
4994 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004995 int derived_type_arg,
4996 int derived_bits_arg,
4997 int derived_usage_arg,
4998 int derived_alg_arg )
4999{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005000 psa_key_handle_t base_handle = 0;
5001 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005002 psa_algorithm_t alg = alg_arg;
5003 psa_key_type_t derived_type = derived_type_arg;
5004 size_t derived_bits = derived_bits_arg;
5005 psa_key_usage_t derived_usage = derived_usage_arg;
5006 psa_algorithm_t derived_alg = derived_alg_arg;
5007 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005008 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005009 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005010 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005011
Gilles Peskine8817f612018-12-18 00:18:46 +01005012 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005013
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005014 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5015 psa_set_key_algorithm( &attributes, alg );
5016 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005017 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5018 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005019
5020 /* Derive a key. */
Janos Follathe60c9052019-07-03 13:51:30 +01005021 if ( setup_key_derivation_wrap( &operation, base_handle, alg,
5022 input1->x, input1->len,
5023 input2->x, input2->len, capacity ) )
5024 goto exit;
5025
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005026 psa_set_key_usage_flags( &attributes, derived_usage );
5027 psa_set_key_algorithm( &attributes, derived_alg );
5028 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005029 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005030 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005031 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005032
5033 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005034 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
5035 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5036 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005037
5038 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005039 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005040 goto exit;
5041
5042exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005043 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005044 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005045 psa_destroy_key( base_handle );
5046 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005047 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005048}
5049/* END_CASE */
5050
Janos Follath42fd8882019-07-03 14:17:09 +01005051/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005052void derive_key_export( int alg_arg,
5053 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005054 data_t *input1,
5055 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005056 int bytes1_arg,
5057 int bytes2_arg )
5058{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005059 psa_key_handle_t base_handle = 0;
5060 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005061 psa_algorithm_t alg = alg_arg;
5062 size_t bytes1 = bytes1_arg;
5063 size_t bytes2 = bytes2_arg;
5064 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005065 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005066 uint8_t *output_buffer = NULL;
5067 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005068 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5069 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005070 size_t length;
5071
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005072 ASSERT_ALLOC( output_buffer, capacity );
5073 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005074 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005075
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005076 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5077 psa_set_key_algorithm( &base_attributes, alg );
5078 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005079 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
5080 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005081
5082 /* Derive some material and output it. */
Janos Follath42fd8882019-07-03 14:17:09 +01005083 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5084 input1->x, input1->len,
5085 input2->x, input2->len, capacity ) )
5086 goto exit;
5087
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005088 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005089 output_buffer,
5090 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005091 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005092
5093 /* Derive the same output again, but this time store it in key objects. */
Janos Follath42fd8882019-07-03 14:17:09 +01005094 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5095 input1->x, input1->len,
5096 input2->x, input2->len, capacity ) )
5097 goto exit;
5098
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005099 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5100 psa_set_key_algorithm( &derived_attributes, 0 );
5101 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005102 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005103 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005104 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01005105 PSA_ASSERT( psa_export_key( derived_handle,
5106 export_buffer, bytes1,
5107 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005108 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01005109 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005110 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005111 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005112 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01005113 PSA_ASSERT( psa_export_key( derived_handle,
5114 export_buffer + bytes1, bytes2,
5115 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005116 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005117
5118 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005119 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5120 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005121
5122exit:
5123 mbedtls_free( output_buffer );
5124 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005125 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005126 psa_destroy_key( base_handle );
5127 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005128 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005129}
5130/* END_CASE */
5131
5132/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005133void derive_key( int alg_arg,
5134 data_t *key_data, data_t *input1, data_t *input2,
5135 int type_arg, int bits_arg,
5136 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02005137{
5138 psa_key_handle_t base_handle = 0;
5139 psa_key_handle_t derived_handle = 0;
5140 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005141 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005142 size_t bits = bits_arg;
5143 psa_status_t expected_status = expected_status_arg;
5144 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5145 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5146 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5147
5148 PSA_ASSERT( psa_crypto_init( ) );
5149
5150 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5151 psa_set_key_algorithm( &base_attributes, alg );
5152 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5153 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
5154 &base_handle ) );
5155
5156 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5157 input1->x, input1->len,
5158 input2->x, input2->len, SIZE_MAX ) )
5159 goto exit;
5160
5161 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5162 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005163 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005164 psa_set_key_bits( &derived_attributes, bits );
5165 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
5166 &derived_handle ),
5167 expected_status );
5168
5169exit:
5170 psa_key_derivation_abort( &operation );
5171 psa_destroy_key( base_handle );
5172 psa_destroy_key( derived_handle );
5173 PSA_DONE( );
5174}
5175/* END_CASE */
5176
5177/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005178void key_agreement_setup( int alg_arg,
5179 int our_key_type_arg, data_t *our_key_data,
5180 data_t *peer_key_data,
5181 int expected_status_arg )
5182{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005183 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005184 psa_algorithm_t alg = alg_arg;
5185 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005186 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005187 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005188 psa_status_t expected_status = expected_status_arg;
5189 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005190
Gilles Peskine8817f612018-12-18 00:18:46 +01005191 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005192
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005193 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5194 psa_set_key_algorithm( &attributes, alg );
5195 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005196 PSA_ASSERT( psa_import_key( &attributes,
5197 our_key_data->x, our_key_data->len,
5198 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005199
Gilles Peskine77f40d82019-04-11 21:27:06 +02005200 /* The tests currently include inputs that should fail at either step.
5201 * Test cases that fail at the setup step should be changed to call
5202 * key_derivation_setup instead, and this function should be renamed
5203 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005204 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005205 if( status == PSA_SUCCESS )
5206 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005207 TEST_EQUAL( psa_key_derivation_key_agreement(
5208 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5209 our_key,
5210 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005211 expected_status );
5212 }
5213 else
5214 {
5215 TEST_ASSERT( status == expected_status );
5216 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005217
5218exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005219 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005220 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005221 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005222}
5223/* END_CASE */
5224
5225/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005226void raw_key_agreement( int alg_arg,
5227 int our_key_type_arg, data_t *our_key_data,
5228 data_t *peer_key_data,
5229 data_t *expected_output )
5230{
5231 psa_key_handle_t our_key = 0;
5232 psa_algorithm_t alg = alg_arg;
5233 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005234 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005235 unsigned char *output = NULL;
5236 size_t output_length = ~0;
5237
5238 ASSERT_ALLOC( output, expected_output->len );
5239 PSA_ASSERT( psa_crypto_init( ) );
5240
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005241 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5242 psa_set_key_algorithm( &attributes, alg );
5243 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005244 PSA_ASSERT( psa_import_key( &attributes,
5245 our_key_data->x, our_key_data->len,
5246 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005247
Gilles Peskinebe697d82019-05-16 18:00:41 +02005248 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5249 peer_key_data->x, peer_key_data->len,
5250 output, expected_output->len,
5251 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005252 ASSERT_COMPARE( output, output_length,
5253 expected_output->x, expected_output->len );
5254
5255exit:
5256 mbedtls_free( output );
5257 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005258 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005259}
5260/* END_CASE */
5261
5262/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005263void key_agreement_capacity( int alg_arg,
5264 int our_key_type_arg, data_t *our_key_data,
5265 data_t *peer_key_data,
5266 int expected_capacity_arg )
5267{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005268 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005269 psa_algorithm_t alg = alg_arg;
5270 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005271 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005272 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005273 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005274 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005275
Gilles Peskine8817f612018-12-18 00:18:46 +01005276 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005277
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005278 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5279 psa_set_key_algorithm( &attributes, alg );
5280 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005281 PSA_ASSERT( psa_import_key( &attributes,
5282 our_key_data->x, our_key_data->len,
5283 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005284
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005285 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005286 PSA_ASSERT( psa_key_derivation_key_agreement(
5287 &operation,
5288 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5289 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005290 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5291 {
5292 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005293 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005294 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005295 NULL, 0 ) );
5296 }
Gilles Peskine59685592018-09-18 12:11:34 +02005297
Gilles Peskinebf491972018-10-25 22:36:12 +02005298 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005299 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005300 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005301 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005302
Gilles Peskinebf491972018-10-25 22:36:12 +02005303 /* Test the actual capacity by reading the output. */
5304 while( actual_capacity > sizeof( output ) )
5305 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005306 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005307 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005308 actual_capacity -= sizeof( output );
5309 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005310 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005311 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005312 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005313 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005314
Gilles Peskine59685592018-09-18 12:11:34 +02005315exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005316 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005317 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005318 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005319}
5320/* END_CASE */
5321
5322/* BEGIN_CASE */
5323void key_agreement_output( int alg_arg,
5324 int our_key_type_arg, data_t *our_key_data,
5325 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005326 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005327{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005328 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005329 psa_algorithm_t alg = alg_arg;
5330 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005331 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005332 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005333 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005334
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005335 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5336 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005337
Gilles Peskine8817f612018-12-18 00:18:46 +01005338 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005339
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005340 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5341 psa_set_key_algorithm( &attributes, alg );
5342 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005343 PSA_ASSERT( psa_import_key( &attributes,
5344 our_key_data->x, our_key_data->len,
5345 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005346
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005347 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005348 PSA_ASSERT( psa_key_derivation_key_agreement(
5349 &operation,
5350 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5351 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005352 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5353 {
5354 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005355 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005356 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005357 NULL, 0 ) );
5358 }
Gilles Peskine59685592018-09-18 12:11:34 +02005359
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005360 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005361 actual_output,
5362 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005363 ASSERT_COMPARE( actual_output, expected_output1->len,
5364 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005365 if( expected_output2->len != 0 )
5366 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005367 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005368 actual_output,
5369 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005370 ASSERT_COMPARE( actual_output, expected_output2->len,
5371 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005372 }
Gilles Peskine59685592018-09-18 12:11:34 +02005373
5374exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005375 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005376 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005377 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005378 mbedtls_free( actual_output );
5379}
5380/* END_CASE */
5381
5382/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005383void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005384{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005385 size_t bytes = bytes_arg;
5386 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005387 unsigned char *output = NULL;
5388 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005389 size_t i;
5390 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005391
Simon Butcher49f8e312020-03-03 15:51:50 +00005392 TEST_ASSERT( bytes_arg >= 0 );
5393
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005394 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5395 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005396 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005397
Gilles Peskine8817f612018-12-18 00:18:46 +01005398 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005399
Gilles Peskinea50d7392018-06-21 10:22:13 +02005400 /* Run several times, to ensure that every output byte will be
5401 * nonzero at least once with overwhelming probability
5402 * (2^(-8*number_of_runs)). */
5403 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005404 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005405 if( bytes != 0 )
5406 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005407 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005408
5409 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005410 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5411 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005412
5413 for( i = 0; i < bytes; i++ )
5414 {
5415 if( output[i] != 0 )
5416 ++changed[i];
5417 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005418 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005419
5420 /* Check that every byte was changed to nonzero at least once. This
5421 * validates that psa_generate_random is overwriting every byte of
5422 * the output buffer. */
5423 for( i = 0; i < bytes; i++ )
5424 {
5425 TEST_ASSERT( changed[i] != 0 );
5426 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005427
5428exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005429 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005430 mbedtls_free( output );
5431 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005432}
5433/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005434
5435/* BEGIN_CASE */
5436void generate_key( int type_arg,
5437 int bits_arg,
5438 int usage_arg,
5439 int alg_arg,
5440 int expected_status_arg )
5441{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005442 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005443 psa_key_type_t type = type_arg;
5444 psa_key_usage_t usage = usage_arg;
5445 size_t bits = bits_arg;
5446 psa_algorithm_t alg = alg_arg;
5447 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005448 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005449 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005450
Gilles Peskine8817f612018-12-18 00:18:46 +01005451 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005452
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005453 psa_set_key_usage_flags( &attributes, usage );
5454 psa_set_key_algorithm( &attributes, alg );
5455 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005456 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005457
5458 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005459 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005460 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005461 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005462
5463 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005464 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
5465 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5466 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005467
Gilles Peskine818ca122018-06-20 18:16:48 +02005468 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005469 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005470 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005471
5472exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005473 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005474 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005475 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005476}
5477/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005478
Gilles Peskinee56e8782019-04-26 17:34:02 +02005479/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5480void generate_key_rsa( int bits_arg,
5481 data_t *e_arg,
5482 int expected_status_arg )
5483{
5484 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005485 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005486 size_t bits = bits_arg;
5487 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5488 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5489 psa_status_t expected_status = expected_status_arg;
5490 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5491 uint8_t *exported = NULL;
5492 size_t exported_size =
5493 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5494 size_t exported_length = SIZE_MAX;
5495 uint8_t *e_read_buffer = NULL;
5496 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005497 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005498 size_t e_read_length = SIZE_MAX;
5499
5500 if( e_arg->len == 0 ||
5501 ( e_arg->len == 3 &&
5502 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5503 {
5504 is_default_public_exponent = 1;
5505 e_read_size = 0;
5506 }
5507 ASSERT_ALLOC( e_read_buffer, e_read_size );
5508 ASSERT_ALLOC( exported, exported_size );
5509
5510 PSA_ASSERT( psa_crypto_init( ) );
5511
5512 psa_set_key_usage_flags( &attributes, usage );
5513 psa_set_key_algorithm( &attributes, alg );
5514 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5515 e_arg->x, e_arg->len ) );
5516 psa_set_key_bits( &attributes, bits );
5517
5518 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005519 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005520 if( expected_status != PSA_SUCCESS )
5521 goto exit;
5522
5523 /* Test the key information */
5524 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5525 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5526 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5527 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5528 e_read_buffer, e_read_size,
5529 &e_read_length ) );
5530 if( is_default_public_exponent )
5531 TEST_EQUAL( e_read_length, 0 );
5532 else
5533 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5534
5535 /* Do something with the key according to its type and permitted usage. */
5536 if( ! exercise_key( handle, usage, alg ) )
5537 goto exit;
5538
5539 /* Export the key and check the public exponent. */
5540 PSA_ASSERT( psa_export_public_key( handle,
5541 exported, exported_size,
5542 &exported_length ) );
5543 {
5544 uint8_t *p = exported;
5545 uint8_t *end = exported + exported_length;
5546 size_t len;
5547 /* RSAPublicKey ::= SEQUENCE {
5548 * modulus INTEGER, -- n
5549 * publicExponent INTEGER } -- e
5550 */
5551 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005552 MBEDTLS_ASN1_SEQUENCE |
5553 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005554 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5555 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5556 MBEDTLS_ASN1_INTEGER ) );
5557 if( len >= 1 && p[0] == 0 )
5558 {
5559 ++p;
5560 --len;
5561 }
5562 if( e_arg->len == 0 )
5563 {
5564 TEST_EQUAL( len, 3 );
5565 TEST_EQUAL( p[0], 1 );
5566 TEST_EQUAL( p[1], 0 );
5567 TEST_EQUAL( p[2], 1 );
5568 }
5569 else
5570 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5571 }
5572
5573exit:
5574 psa_reset_key_attributes( &attributes );
5575 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005576 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005577 mbedtls_free( e_read_buffer );
5578 mbedtls_free( exported );
5579}
5580/* END_CASE */
5581
Darryl Greend49a4992018-06-18 17:27:26 +01005582/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005583void persistent_key_load_key_from_storage( data_t *data,
5584 int type_arg, int bits_arg,
5585 int usage_flags_arg, int alg_arg,
5586 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005587{
Ronald Cron71016a92020-08-28 19:01:50 +02005588 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005589 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005590 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005591 psa_key_handle_t base_key = 0;
5592 psa_key_type_t type = type_arg;
5593 size_t bits = bits_arg;
5594 psa_key_usage_t usage_flags = usage_flags_arg;
5595 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005596 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005597 unsigned char *first_export = NULL;
5598 unsigned char *second_export = NULL;
5599 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5600 size_t first_exported_length;
5601 size_t second_exported_length;
5602
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005603 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5604 {
5605 ASSERT_ALLOC( first_export, export_size );
5606 ASSERT_ALLOC( second_export, export_size );
5607 }
Darryl Greend49a4992018-06-18 17:27:26 +01005608
Gilles Peskine8817f612018-12-18 00:18:46 +01005609 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005610
Gilles Peskinec87af662019-05-15 16:12:22 +02005611 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005612 psa_set_key_usage_flags( &attributes, usage_flags );
5613 psa_set_key_algorithm( &attributes, alg );
5614 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005615 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005616
Darryl Green0c6575a2018-11-07 16:05:30 +00005617 switch( generation_method )
5618 {
5619 case IMPORT_KEY:
5620 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005621 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
5622 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005623 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005624
Darryl Green0c6575a2018-11-07 16:05:30 +00005625 case GENERATE_KEY:
5626 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005627 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005628 break;
5629
5630 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005631 {
5632 /* Create base key */
5633 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5634 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5635 psa_set_key_usage_flags( &base_attributes,
5636 PSA_KEY_USAGE_DERIVE );
5637 psa_set_key_algorithm( &base_attributes, derive_alg );
5638 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005639 PSA_ASSERT( psa_import_key( &base_attributes,
5640 data->x, data->len,
5641 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005642 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005643 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005644 PSA_ASSERT( psa_key_derivation_input_key(
5645 &operation,
5646 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005647 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005648 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005649 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005650 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5651 &operation,
5652 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005653 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005654 PSA_ASSERT( psa_destroy_key( base_key ) );
5655 base_key = 0;
5656 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005657 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005658 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005659 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005660
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005661 /* Export the key if permitted by the key policy. */
5662 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5663 {
5664 PSA_ASSERT( psa_export_key( handle,
5665 first_export, export_size,
5666 &first_exported_length ) );
5667 if( generation_method == IMPORT_KEY )
5668 ASSERT_COMPARE( data->x, data->len,
5669 first_export, first_exported_length );
5670 }
Darryl Greend49a4992018-06-18 17:27:26 +01005671
5672 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005673 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005674 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005675 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005676
Darryl Greend49a4992018-06-18 17:27:26 +01005677 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005678 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005679 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5680 TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
5681 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5682 PSA_KEY_LIFETIME_PERSISTENT );
5683 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5684 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5685 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5686 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005687
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005688 /* Export the key again if permitted by the key policy. */
5689 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005690 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005691 PSA_ASSERT( psa_export_key( handle,
5692 second_export, export_size,
5693 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005694 ASSERT_COMPARE( first_export, first_exported_length,
5695 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005696 }
5697
5698 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005699 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005700 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005701
5702exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005703 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005704 mbedtls_free( first_export );
5705 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005706 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005707 psa_destroy_key( base_key );
5708 if( handle == 0 )
5709 {
5710 /* In case there was a test failure after creating the persistent key
5711 * but while it was not open, try to re-open the persistent key
5712 * to delete it. */
Gilles Peskinee92c68a2020-08-26 00:06:25 +02005713 (void) psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005714 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005715 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005716 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005717}
5718/* END_CASE */