blob: 45916b91b1fd67fa69784a7e9b0683aeebbb5720 [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 )
Ronald Cronecfb2372020-07-23 17:13:42 +0200248 TEST_ASSERT( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) == 0 );
Gilles Peskine667c1112019-12-03 19:03:20 +0100249 else
250 {
251 TEST_ASSERT(
Ronald Cronecfb2372020-07-23 17:13:42 +0200252 ( PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
253 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <= PSA_KEY_ID_USER_MAX ) );
Gilles Peskine667c1112019-12-03 19:03:20 +0100254 }
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;
Ronald Cronecfb2372020-07-23 17:13:42 +02001181 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001182 uint8_t buffer[1];
1183 size_t length;
1184 int ok = 0;
1185
Ronald Cronecfb2372020-07-23 17:13:42 +02001186 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001187 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
1188 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
1189 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
1190 TEST_EQUAL( psa_get_key_attributes( handle, &attributes ),
1191 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +02001192 TEST_EQUAL(
1193 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1194 TEST_EQUAL(
1195 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02001196 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001197 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1198 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1199 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
1200 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
1201
1202 TEST_EQUAL( psa_export_key( handle,
1203 buffer, sizeof( buffer ), &length ),
1204 PSA_ERROR_INVALID_HANDLE );
1205 TEST_EQUAL( psa_export_public_key( handle,
1206 buffer, sizeof( buffer ), &length ),
1207 PSA_ERROR_INVALID_HANDLE );
1208
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001209 ok = 1;
1210
1211exit:
1212 psa_reset_key_attributes( &attributes );
1213 return( ok );
1214}
1215
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001216/* Assert that a key isn't reported as having a slot number. */
1217#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1218#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1219 do \
1220 { \
1221 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
1222 TEST_EQUAL( psa_get_key_slot_number( \
1223 attributes, \
1224 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
1225 PSA_ERROR_INVALID_ARGUMENT ); \
1226 } \
1227 while( 0 )
1228#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1229#define ASSERT_NO_SLOT_NUMBER( attributes ) \
1230 ( (void) 0 )
1231#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1232
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001233/* An overapproximation of the amount of storage needed for a key of the
1234 * given type and with the given content. The API doesn't make it easy
1235 * to find a good value for the size. The current implementation doesn't
1236 * care about the value anyway. */
1237#define KEY_BITS_FROM_DATA( type, data ) \
1238 ( data )->len
1239
Darryl Green0c6575a2018-11-07 16:05:30 +00001240typedef enum {
1241 IMPORT_KEY = 0,
1242 GENERATE_KEY = 1,
1243 DERIVE_KEY = 2
1244} generate_method;
1245
Gilles Peskinee59236f2018-01-27 23:32:46 +01001246/* END_HEADER */
1247
1248/* BEGIN_DEPENDENCIES
1249 * depends_on:MBEDTLS_PSA_CRYPTO_C
1250 * END_DEPENDENCIES
1251 */
1252
1253/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001254void static_checks( )
1255{
1256 size_t max_truncated_mac_size =
1257 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
1258
1259 /* Check that the length for a truncated MAC always fits in the algorithm
1260 * encoding. The shifted mask is the maximum truncated value. The
1261 * untruncated algorithm may be one byte larger. */
1262 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
Gilles Peskine841b14b2019-11-26 17:37:37 +01001263
1264#if defined(MBEDTLS_TEST_DEPRECATED)
1265 /* Check deprecated constants. */
1266 TEST_EQUAL( PSA_ERROR_UNKNOWN_ERROR, PSA_ERROR_GENERIC_ERROR );
1267 TEST_EQUAL( PSA_ERROR_OCCUPIED_SLOT, PSA_ERROR_ALREADY_EXISTS );
1268 TEST_EQUAL( PSA_ERROR_EMPTY_SLOT, PSA_ERROR_DOES_NOT_EXIST );
1269 TEST_EQUAL( PSA_ERROR_INSUFFICIENT_CAPACITY, PSA_ERROR_INSUFFICIENT_DATA );
1270 TEST_EQUAL( PSA_ERROR_TAMPERING_DETECTED, PSA_ERROR_CORRUPTION_DETECTED );
1271 TEST_EQUAL( PSA_KEY_USAGE_SIGN, PSA_KEY_USAGE_SIGN_HASH );
1272 TEST_EQUAL( PSA_KEY_USAGE_VERIFY, PSA_KEY_USAGE_VERIFY_HASH );
1273 TEST_EQUAL( PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE, PSA_SIGNATURE_MAX_SIZE );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001274
Paul Elliott8ff510a2020-06-02 17:19:28 +01001275 TEST_EQUAL( PSA_ECC_CURVE_SECP160K1, PSA_ECC_FAMILY_SECP_K1 );
1276 TEST_EQUAL( PSA_ECC_CURVE_SECP192K1, PSA_ECC_FAMILY_SECP_K1 );
1277 TEST_EQUAL( PSA_ECC_CURVE_SECP224K1, PSA_ECC_FAMILY_SECP_K1 );
1278 TEST_EQUAL( PSA_ECC_CURVE_SECP256K1, PSA_ECC_FAMILY_SECP_K1 );
1279 TEST_EQUAL( PSA_ECC_CURVE_SECP160R1, PSA_ECC_FAMILY_SECP_R1 );
1280 TEST_EQUAL( PSA_ECC_CURVE_SECP192R1, PSA_ECC_FAMILY_SECP_R1 );
1281 TEST_EQUAL( PSA_ECC_CURVE_SECP224R1, PSA_ECC_FAMILY_SECP_R1 );
1282 TEST_EQUAL( PSA_ECC_CURVE_SECP256R1, PSA_ECC_FAMILY_SECP_R1 );
1283 TEST_EQUAL( PSA_ECC_CURVE_SECP384R1, PSA_ECC_FAMILY_SECP_R1 );
1284 TEST_EQUAL( PSA_ECC_CURVE_SECP521R1, PSA_ECC_FAMILY_SECP_R1 );
1285 TEST_EQUAL( PSA_ECC_CURVE_SECP160R2, PSA_ECC_FAMILY_SECP_R2 );
1286 TEST_EQUAL( PSA_ECC_CURVE_SECT163K1, PSA_ECC_FAMILY_SECT_K1 );
1287 TEST_EQUAL( PSA_ECC_CURVE_SECT233K1, PSA_ECC_FAMILY_SECT_K1 );
1288 TEST_EQUAL( PSA_ECC_CURVE_SECT239K1, PSA_ECC_FAMILY_SECT_K1 );
1289 TEST_EQUAL( PSA_ECC_CURVE_SECT283K1, PSA_ECC_FAMILY_SECT_K1 );
1290 TEST_EQUAL( PSA_ECC_CURVE_SECT409K1, PSA_ECC_FAMILY_SECT_K1 );
1291 TEST_EQUAL( PSA_ECC_CURVE_SECT571K1, PSA_ECC_FAMILY_SECT_K1 );
1292 TEST_EQUAL( PSA_ECC_CURVE_SECT163R1, PSA_ECC_FAMILY_SECT_R1 );
1293 TEST_EQUAL( PSA_ECC_CURVE_SECT193R1, PSA_ECC_FAMILY_SECT_R1 );
1294 TEST_EQUAL( PSA_ECC_CURVE_SECT233R1, PSA_ECC_FAMILY_SECT_R1 );
1295 TEST_EQUAL( PSA_ECC_CURVE_SECT283R1, PSA_ECC_FAMILY_SECT_R1 );
1296 TEST_EQUAL( PSA_ECC_CURVE_SECT409R1, PSA_ECC_FAMILY_SECT_R1 );
1297 TEST_EQUAL( PSA_ECC_CURVE_SECT571R1, PSA_ECC_FAMILY_SECT_R1 );
1298 TEST_EQUAL( PSA_ECC_CURVE_SECT163R2, PSA_ECC_FAMILY_SECT_R2 );
1299 TEST_EQUAL( PSA_ECC_CURVE_SECT193R2, PSA_ECC_FAMILY_SECT_R2 );
1300 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1301 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1302 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1303 TEST_EQUAL( PSA_ECC_CURVE_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY );
1304 TEST_EQUAL( PSA_ECC_CURVE_CURVE448, PSA_ECC_FAMILY_MONTGOMERY );
1305
1306 TEST_EQUAL( PSA_ECC_CURVE_SECP_K1, PSA_ECC_FAMILY_SECP_K1 );
1307 TEST_EQUAL( PSA_ECC_CURVE_SECP_R1, PSA_ECC_FAMILY_SECP_R1 );
1308 TEST_EQUAL( PSA_ECC_CURVE_SECP_R2, PSA_ECC_FAMILY_SECP_R2 );
1309 TEST_EQUAL( PSA_ECC_CURVE_SECT_K1, PSA_ECC_FAMILY_SECT_K1 );
1310 TEST_EQUAL( PSA_ECC_CURVE_SECT_R1, PSA_ECC_FAMILY_SECT_R1 );
1311 TEST_EQUAL( PSA_ECC_CURVE_SECT_R2, PSA_ECC_FAMILY_SECT_R2 );
1312 TEST_EQUAL( PSA_ECC_CURVE_BRAINPOOL_P_R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
1313 TEST_EQUAL( PSA_ECC_CURVE_MONTGOMERY, PSA_ECC_FAMILY_MONTGOMERY );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001314
Paul Elliott75e27032020-06-03 15:17:39 +01001315 TEST_EQUAL( PSA_DH_GROUP_FFDHE2048, PSA_DH_FAMILY_RFC7919 );
1316 TEST_EQUAL( PSA_DH_GROUP_FFDHE3072, PSA_DH_FAMILY_RFC7919 );
1317 TEST_EQUAL( PSA_DH_GROUP_FFDHE4096, PSA_DH_FAMILY_RFC7919 );
1318 TEST_EQUAL( PSA_DH_GROUP_FFDHE6144, PSA_DH_FAMILY_RFC7919 );
1319 TEST_EQUAL( PSA_DH_GROUP_FFDHE8192, PSA_DH_FAMILY_RFC7919 );
1320
1321 TEST_EQUAL( PSA_DH_GROUP_RFC7919, PSA_DH_FAMILY_RFC7919 );
1322 TEST_EQUAL( PSA_DH_GROUP_CUSTOM, PSA_DH_FAMILY_CUSTOM );
Gilles Peskineb87b7192019-12-04 16:24:10 +01001323#endif
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +02001324}
1325/* END_CASE */
1326
1327/* BEGIN_CASE */
Ronald Cron81e00502020-07-28 15:06:14 +02001328void attributes_set_get( int owner_id_arg, int id_arg, int lifetime_arg,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001329 int usage_flags_arg, int alg_arg,
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001330 int type_arg, int bits_arg )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001331{
Gilles Peskine4747d192019-04-17 15:05:45 +02001332 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron81e00502020-07-28 15:06:14 +02001333 mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( owner_id_arg, id_arg );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001334 psa_key_lifetime_t lifetime = lifetime_arg;
1335 psa_key_usage_t usage_flags = usage_flags_arg;
1336 psa_algorithm_t alg = alg_arg;
1337 psa_key_type_t type = type_arg;
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001338 size_t bits = bits_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001339
Ronald Cronecfb2372020-07-23 17:13:42 +02001340 TEST_EQUAL(
1341 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1342 TEST_EQUAL(
1343 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001344 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1345 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1346 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1347 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001348 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001349
Gilles Peskinec87af662019-05-15 16:12:22 +02001350 psa_set_key_id( &attributes, id );
1351 psa_set_key_lifetime( &attributes, lifetime );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001352 psa_set_key_usage_flags( &attributes, usage_flags );
1353 psa_set_key_algorithm( &attributes, alg );
1354 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001355 psa_set_key_bits( &attributes, bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001356
Ronald Cronecfb2372020-07-23 17:13:42 +02001357 TEST_ASSERT( mbedtls_svc_key_id_equal(
1358 psa_get_key_id( &attributes ), id ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001359 TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
1360 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
1361 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
1362 TEST_EQUAL( psa_get_key_type( &attributes ), type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001363 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001364
1365 psa_reset_key_attributes( &attributes );
1366
Ronald Cronecfb2372020-07-23 17:13:42 +02001367 TEST_EQUAL(
1368 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
1369 TEST_EQUAL(
1370 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001371 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
1372 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
1373 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
1374 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02001375 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001376}
1377/* END_CASE */
1378
1379/* BEGIN_CASE */
Ronald Cronecfb2372020-07-23 17:13:42 +02001380void persistence_attributes( int id1_arg, int owner_id1_arg, int lifetime_arg,
1381 int id2_arg, int owner_id2_arg,
1382 int expected_id_arg, int expected_owner_id_arg,
1383 int expected_lifetime_arg )
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001384{
1385 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +02001386 mbedtls_svc_key_id_t id1 =
1387 mbedtls_svc_key_id_make( owner_id1_arg, id1_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001388 psa_key_lifetime_t lifetime = lifetime_arg;
Ronald Cronecfb2372020-07-23 17:13:42 +02001389 mbedtls_svc_key_id_t id2 =
1390 mbedtls_svc_key_id_make( owner_id2_arg, id2_arg );
Ronald Cron71016a92020-08-28 19:01:50 +02001391 mbedtls_svc_key_id_t expected_id =
Ronald Cronecfb2372020-07-23 17:13:42 +02001392 mbedtls_svc_key_id_make( expected_owner_id_arg, expected_id_arg );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001393 psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
1394
1395 if( id1_arg != -1 )
1396 psa_set_key_id( &attributes, id1 );
1397 if( lifetime_arg != -1 )
1398 psa_set_key_lifetime( &attributes, lifetime );
1399 if( id2_arg != -1 )
1400 psa_set_key_id( &attributes, id2 );
1401
Ronald Cronecfb2372020-07-23 17:13:42 +02001402 TEST_ASSERT( mbedtls_svc_key_id_equal(
1403 psa_get_key_id( &attributes ), expected_id ) );
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001404 TEST_EQUAL( psa_get_key_lifetime( &attributes ), expected_lifetime );
1405}
1406/* END_CASE */
1407
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001408/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
1409void slot_number_attribute( )
1410{
1411 psa_key_slot_number_t slot_number = 0xdeadbeef;
1412 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1413
1414 /* Initially, there is no slot number. */
1415 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1416 PSA_ERROR_INVALID_ARGUMENT );
1417
1418 /* Test setting a slot number. */
1419 psa_set_key_slot_number( &attributes, 0 );
1420 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1421 TEST_EQUAL( slot_number, 0 );
1422
1423 /* Test changing the slot number. */
1424 psa_set_key_slot_number( &attributes, 42 );
1425 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1426 TEST_EQUAL( slot_number, 42 );
1427
1428 /* Test clearing the slot number. */
1429 psa_clear_key_slot_number( &attributes );
1430 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1431 PSA_ERROR_INVALID_ARGUMENT );
1432
1433 /* Clearing again should have no effect. */
1434 psa_clear_key_slot_number( &attributes );
1435 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1436 PSA_ERROR_INVALID_ARGUMENT );
1437
1438 /* Test that reset clears the slot number. */
1439 psa_set_key_slot_number( &attributes, 42 );
1440 PSA_ASSERT( psa_get_key_slot_number( &attributes, &slot_number ) );
1441 TEST_EQUAL( slot_number, 42 );
1442 psa_reset_key_attributes( &attributes );
1443 TEST_EQUAL( psa_get_key_slot_number( &attributes, &slot_number ),
1444 PSA_ERROR_INVALID_ARGUMENT );
1445}
1446/* END_CASE */
1447
Gilles Peskinedd835cb2019-05-15 16:14:57 +02001448/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +02001449void import_with_policy( int type_arg,
1450 int usage_arg, int alg_arg,
1451 int expected_status_arg )
1452{
1453 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1454 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1455 psa_key_handle_t handle = 0;
1456 psa_key_type_t type = type_arg;
1457 psa_key_usage_t usage = usage_arg;
1458 psa_algorithm_t alg = alg_arg;
1459 psa_status_t expected_status = expected_status_arg;
1460 const uint8_t key_material[16] = {0};
1461 psa_status_t status;
1462
1463 PSA_ASSERT( psa_crypto_init( ) );
1464
1465 psa_set_key_type( &attributes, type );
1466 psa_set_key_usage_flags( &attributes, usage );
1467 psa_set_key_algorithm( &attributes, alg );
1468
1469 status = psa_import_key( &attributes,
1470 key_material, sizeof( key_material ),
1471 &handle );
1472 TEST_EQUAL( status, expected_status );
1473 if( status != PSA_SUCCESS )
1474 goto exit;
1475
1476 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1477 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1478 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1479 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001480 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001481
1482 PSA_ASSERT( psa_destroy_key( handle ) );
1483 test_operations_on_invalid_handle( handle );
1484
1485exit:
1486 psa_destroy_key( handle );
1487 psa_reset_key_attributes( &got_attributes );
1488 PSA_DONE( );
1489}
1490/* END_CASE */
1491
1492/* BEGIN_CASE */
1493void import_with_data( data_t *data, int type_arg,
1494 int attr_bits_arg,
1495 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001496{
1497 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1498 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001499 psa_key_handle_t handle = 0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001500 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001501 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001502 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001503 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001504
Gilles Peskine8817f612018-12-18 00:18:46 +01001505 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001506
Gilles Peskine4747d192019-04-17 15:05:45 +02001507 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001508 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +02001509
Gilles Peskine73676cb2019-05-15 20:15:10 +02001510 status = psa_import_key( &attributes, data->x, data->len, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001511 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001512 if( status != PSA_SUCCESS )
1513 goto exit;
1514
1515 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1516 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +02001517 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001518 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001519 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001520
1521 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001522 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001523
1524exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001525 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001526 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001527 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001528}
1529/* END_CASE */
1530
1531/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +02001532void import_large_key( int type_arg, int byte_size_arg,
1533 int expected_status_arg )
1534{
1535 psa_key_type_t type = type_arg;
1536 size_t byte_size = byte_size_arg;
1537 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1538 psa_status_t expected_status = expected_status_arg;
1539 psa_key_handle_t handle = 0;
1540 psa_status_t status;
1541 uint8_t *buffer = NULL;
1542 size_t buffer_size = byte_size + 1;
1543 size_t n;
1544
1545 /* It would be better to skip the test than fail it if the allocation
1546 * fails, but the test framework doesn't support this yet. */
1547 ASSERT_ALLOC( buffer, buffer_size );
1548 memset( buffer, 'K', byte_size );
1549
1550 PSA_ASSERT( psa_crypto_init( ) );
1551
1552 /* Try importing the key */
1553 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1554 psa_set_key_type( &attributes, type );
1555 status = psa_import_key( &attributes, buffer, byte_size, &handle );
1556 TEST_EQUAL( status, expected_status );
1557
1558 if( status == PSA_SUCCESS )
1559 {
1560 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1561 TEST_EQUAL( psa_get_key_type( &attributes ), type );
1562 TEST_EQUAL( psa_get_key_bits( &attributes ),
1563 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001564 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +02001565 memset( buffer, 0, byte_size + 1 );
1566 PSA_ASSERT( psa_export_key( handle, buffer, byte_size, &n ) );
1567 for( n = 0; n < byte_size; n++ )
1568 TEST_EQUAL( buffer[n], 'K' );
1569 for( n = byte_size; n < buffer_size; n++ )
1570 TEST_EQUAL( buffer[n], 0 );
1571 }
1572
1573exit:
1574 psa_destroy_key( handle );
1575 PSA_DONE( );
1576 mbedtls_free( buffer );
1577}
1578/* END_CASE */
1579
1580/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001581void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
1582{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001583 psa_key_handle_t handle = 0;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001584 size_t bits = bits_arg;
1585 psa_status_t expected_status = expected_status_arg;
1586 psa_status_t status;
1587 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001588 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001589 size_t buffer_size = /* Slight overapproximations */
1590 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001591 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001592 unsigned char *p;
1593 int ret;
1594 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001595 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001596
Gilles Peskine8817f612018-12-18 00:18:46 +01001597 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001598 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001599
1600 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
1601 bits, keypair ) ) >= 0 );
1602 length = ret;
1603
1604 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001605 psa_set_key_type( &attributes, type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02001606 status = psa_import_key( &attributes, p, length, &handle );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001607 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001608
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001609 if( status == PSA_SUCCESS )
Gilles Peskine8817f612018-12-18 00:18:46 +01001610 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001611
1612exit:
1613 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001614 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +02001615}
1616/* END_CASE */
1617
1618/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001619void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +03001620 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +02001621 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001622 int expected_bits,
1623 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001624 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001625 int canonical_input )
1626{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001627 psa_key_handle_t handle = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001628 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001629 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001630 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001631 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001632 unsigned char *exported = NULL;
1633 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001634 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001635 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001636 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +02001637 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001638 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001639
Moran Pekercb088e72018-07-17 17:36:59 +03001640 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001641 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001642 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +02001643 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01001644 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001645
Gilles Peskine4747d192019-04-17 15:05:45 +02001646 psa_set_key_usage_flags( &attributes, usage_arg );
1647 psa_set_key_algorithm( &attributes, alg );
1648 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07001649
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001650 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001651 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001652
1653 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001654 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1655 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1656 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +02001657 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001658
1659 /* Export the key */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001660 status = psa_export_key( handle,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001661 exported, export_size,
1662 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001663 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001664
1665 /* The exported length must be set by psa_export_key() to a value between 0
1666 * and export_size. On errors, the exported length must be 0. */
1667 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
1668 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
1669 TEST_ASSERT( exported_length <= export_size );
1670
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001671 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +02001672 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001673 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001674 {
Gilles Peskinefe11b722018-12-18 00:24:04 +01001675 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001676 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001677 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001678
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001679 if( ! exercise_export_key( handle, usage_arg ) )
Gilles Peskine8f609232018-08-11 01:24:55 +02001680 goto exit;
1681
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001682 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001683 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001684 else
1685 {
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001686 psa_key_handle_t handle2;
Gilles Peskine049c7532019-05-15 20:22:09 +02001687 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
1688 &handle2 ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01001689 PSA_ASSERT( psa_export_key( handle2,
1690 reexported,
1691 export_size,
1692 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02001693 ASSERT_COMPARE( exported, exported_length,
1694 reexported, reexported_length );
Gilles Peskine8817f612018-12-18 00:18:46 +01001695 PSA_ASSERT( psa_close_key( handle2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001696 }
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001697 TEST_ASSERT( exported_length <= PSA_KEY_EXPORT_MAX_SIZE( type, psa_get_key_bits( &got_attributes ) ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001698
1699destroy:
1700 /* Destroy the key */
Gilles Peskine8817f612018-12-18 00:18:46 +01001701 PSA_ASSERT( psa_destroy_key( handle ) );
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001702 test_operations_on_invalid_handle( handle );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001703
1704exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001705 mbedtls_free( exported );
1706 mbedtls_free( reexported );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001707 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001708 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001709}
1710/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +01001711
Moran Pekerf709f4a2018-06-06 17:26:04 +03001712/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03001713void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +02001714 int type_arg,
1715 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +01001716 int export_size_delta,
1717 int expected_export_status_arg,
1718 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +03001719{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001720 psa_key_handle_t handle = 0;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001721 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +02001722 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001723 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001724 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001725 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +01001726 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +01001727 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +02001728 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +03001729
Gilles Peskine8817f612018-12-18 00:18:46 +01001730 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001731
Gilles Peskine4747d192019-04-17 15:05:45 +02001732 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
1733 psa_set_key_algorithm( &attributes, alg );
1734 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001735
1736 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001737 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001738
Gilles Peskine49c25912018-10-29 15:15:31 +01001739 /* Export the public key */
1740 ASSERT_ALLOC( exported, export_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001741 status = psa_export_public_key( handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001742 exported, export_size,
1743 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001744 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +01001745 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001746 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001747 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001748 size_t bits;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001749 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
1750 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001751 TEST_ASSERT( expected_public_key->len <=
1752 PSA_KEY_EXPORT_MAX_SIZE( public_type, bits ) );
Gilles Peskine49c25912018-10-29 15:15:31 +01001753 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
1754 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +01001755 }
Moran Pekerf709f4a2018-06-06 17:26:04 +03001756
1757exit:
itayzafrir3e02b3b2018-06-12 17:06:52 +03001758 mbedtls_free( exported );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001759 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001760 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001761 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +03001762}
1763/* END_CASE */
1764
Gilles Peskine20035e32018-02-03 22:44:14 +01001765/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001766void import_and_exercise_key( data_t *data,
1767 int type_arg,
1768 int bits_arg,
1769 int alg_arg )
1770{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001771 psa_key_handle_t handle = 0;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001772 psa_key_type_t type = type_arg;
1773 size_t bits = bits_arg;
1774 psa_algorithm_t alg = alg_arg;
Gilles Peskine10df3412018-10-25 22:35:43 +02001775 psa_key_usage_t usage = usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +02001776 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001777 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001778
Gilles Peskine8817f612018-12-18 00:18:46 +01001779 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001780
Gilles Peskine4747d192019-04-17 15:05:45 +02001781 psa_set_key_usage_flags( &attributes, usage );
1782 psa_set_key_algorithm( &attributes, alg );
1783 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001784
1785 /* Import the key */
Gilles Peskine73676cb2019-05-15 20:15:10 +02001786 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001787
1788 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001789 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
1790 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
1791 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001792
1793 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001794 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02001795 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001796
Gilles Peskine4cf3a432019-04-18 22:28:52 +02001797 PSA_ASSERT( psa_destroy_key( handle ) );
1798 test_operations_on_invalid_handle( handle );
1799
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001800exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001801 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001802 psa_reset_key_attributes( &got_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001803 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +02001804}
1805/* END_CASE */
1806
1807/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001808void effective_key_attributes( int type_arg, int expected_type_arg,
1809 int bits_arg, int expected_bits_arg,
1810 int usage_arg, int expected_usage_arg,
1811 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001812{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001813 psa_key_handle_t handle = 0;
Gilles Peskine1a960492019-11-26 17:12:21 +01001814 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001815 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +01001816 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001817 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001818 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001819 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +02001820 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +01001821 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001822 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +02001823
Gilles Peskine8817f612018-12-18 00:18:46 +01001824 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001825
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001826 psa_set_key_usage_flags( &attributes, usage );
1827 psa_set_key_algorithm( &attributes, alg );
1828 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +01001829 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +02001830
Gilles Peskine1a960492019-11-26 17:12:21 +01001831 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
1832 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +02001833
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001834 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +01001835 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
1836 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
1837 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
1838 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +02001839
1840exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001841 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001842 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001843 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001844}
1845/* END_CASE */
1846
1847/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +01001848void check_key_policy( int type_arg, int bits_arg,
1849 int usage_arg, int alg_arg )
1850{
1851 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
1852 usage_arg, usage_arg, alg_arg, alg_arg );
1853 goto exit;
1854}
1855/* END_CASE */
1856
1857/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001858void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +00001859{
1860 /* Test each valid way of initializing the object, except for `= {0}`, as
1861 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1862 * though it's OK by the C standard. We could test for this, but we'd need
1863 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001864 psa_key_attributes_t func = psa_key_attributes_init( );
1865 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
1866 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +00001867
1868 memset( &zero, 0, sizeof( zero ) );
1869
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001870 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
1871 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
1872 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001873
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001874 TEST_EQUAL( psa_get_key_type( &func ), 0 );
1875 TEST_EQUAL( psa_get_key_type( &init ), 0 );
1876 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
1877
1878 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
1879 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
1880 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
1881
1882 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
1883 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
1884 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
1885
1886 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
1887 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
1888 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +00001889}
1890/* END_CASE */
1891
1892/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001893void mac_key_policy( int policy_usage,
1894 int policy_alg,
1895 int key_type,
1896 data_t *key_data,
1897 int exercise_alg )
Gilles Peskined5b33222018-06-18 22:20:03 +02001898{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001899 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001900 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +00001901 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001902 psa_status_t status;
1903 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +02001904
Gilles Peskine8817f612018-12-18 00:18:46 +01001905 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001906
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001907 psa_set_key_usage_flags( &attributes, policy_usage );
1908 psa_set_key_algorithm( &attributes, policy_alg );
1909 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +02001910
Gilles Peskine049c7532019-05-15 20:22:09 +02001911 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1912 &handle ) );
Gilles Peskined5b33222018-06-18 22:20:03 +02001913
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001914 status = psa_mac_sign_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001915 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001916 ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001917 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001918 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001919 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001920 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +02001921
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001922 memset( mac, 0, sizeof( mac ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001923 status = psa_mac_verify_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001924 if( policy_alg == exercise_alg &&
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001925 ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001926 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001927 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001928 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001929
1930exit:
1931 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001932 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001933 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001934}
1935/* END_CASE */
1936
1937/* BEGIN_CASE */
1938void cipher_key_policy( int policy_usage,
1939 int policy_alg,
1940 int key_type,
1941 data_t *key_data,
1942 int exercise_alg )
1943{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001944 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001945 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +00001946 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001947 psa_status_t status;
1948
Gilles Peskine8817f612018-12-18 00:18:46 +01001949 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001950
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001951 psa_set_key_usage_flags( &attributes, policy_usage );
1952 psa_set_key_algorithm( &attributes, policy_alg );
1953 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001954
Gilles Peskine049c7532019-05-15 20:22:09 +02001955 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
1956 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001957
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001958 status = psa_cipher_encrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001959 if( policy_alg == exercise_alg &&
1960 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001961 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001962 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001963 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001964 psa_cipher_abort( &operation );
1965
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001966 status = psa_cipher_decrypt_setup( &operation, handle, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001967 if( policy_alg == exercise_alg &&
1968 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001969 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001970 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001971 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001972
1973exit:
1974 psa_cipher_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001975 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001976 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001977}
1978/* END_CASE */
1979
1980/* BEGIN_CASE */
1981void aead_key_policy( int policy_usage,
1982 int policy_alg,
1983 int key_type,
1984 data_t *key_data,
1985 int nonce_length_arg,
1986 int tag_length_arg,
1987 int exercise_alg )
1988{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01001989 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001990 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001991 psa_status_t status;
1992 unsigned char nonce[16] = {0};
1993 size_t nonce_length = nonce_length_arg;
1994 unsigned char tag[16];
1995 size_t tag_length = tag_length_arg;
1996 size_t output_length;
1997
1998 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
1999 TEST_ASSERT( tag_length <= sizeof( tag ) );
2000
Gilles Peskine8817f612018-12-18 00:18:46 +01002001 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002002
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002003 psa_set_key_usage_flags( &attributes, policy_usage );
2004 psa_set_key_algorithm( &attributes, policy_alg );
2005 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002006
Gilles Peskine049c7532019-05-15 20:22:09 +02002007 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2008 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002009
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002010 status = psa_aead_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002011 nonce, nonce_length,
2012 NULL, 0,
2013 NULL, 0,
2014 tag, tag_length,
2015 &output_length );
2016 if( policy_alg == exercise_alg &&
2017 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002018 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002019 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002020 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002021
2022 memset( tag, 0, sizeof( tag ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002023 status = psa_aead_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002024 nonce, nonce_length,
2025 NULL, 0,
2026 tag, tag_length,
2027 NULL, 0,
2028 &output_length );
2029 if( policy_alg == exercise_alg &&
2030 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002031 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002032 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002033 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002034
2035exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002036 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002037 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002038}
2039/* END_CASE */
2040
2041/* BEGIN_CASE */
2042void asymmetric_encryption_key_policy( int policy_usage,
2043 int policy_alg,
2044 int key_type,
2045 data_t *key_data,
2046 int exercise_alg )
2047{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002048 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002049 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002050 psa_status_t status;
2051 size_t key_bits;
2052 size_t buffer_length;
2053 unsigned char *buffer = NULL;
2054 size_t output_length;
2055
Gilles Peskine8817f612018-12-18 00:18:46 +01002056 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002057
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002058 psa_set_key_usage_flags( &attributes, policy_usage );
2059 psa_set_key_algorithm( &attributes, policy_alg );
2060 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002061
Gilles Peskine049c7532019-05-15 20:22:09 +02002062 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2063 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002064
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002065 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
2066 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002067 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
2068 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002069 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002070
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002071 status = psa_asymmetric_encrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002072 NULL, 0,
2073 NULL, 0,
2074 buffer, buffer_length,
2075 &output_length );
2076 if( policy_alg == exercise_alg &&
2077 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002078 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002079 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002080 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002081
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02002082 if( buffer_length != 0 )
2083 memset( buffer, 0, buffer_length );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002084 status = psa_asymmetric_decrypt( handle, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002085 buffer, buffer_length,
2086 NULL, 0,
2087 buffer, buffer_length,
2088 &output_length );
2089 if( policy_alg == exercise_alg &&
2090 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002091 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002092 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002093 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002094
2095exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002096 psa_destroy_key( handle );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002097 psa_reset_key_attributes( &attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002098 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002099 mbedtls_free( buffer );
2100}
2101/* END_CASE */
2102
2103/* BEGIN_CASE */
2104void asymmetric_signature_key_policy( int policy_usage,
2105 int policy_alg,
2106 int key_type,
2107 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002108 int exercise_alg,
2109 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002110{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002111 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002112 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002113 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01002114 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
2115 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
2116 * compatible with the policy and `payload_length_arg` is supposed to be
2117 * a valid input length to sign. If `payload_length_arg <= 0`,
2118 * `exercise_alg` is supposed to be forbidden by the policy. */
2119 int compatible_alg = payload_length_arg > 0;
2120 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002121 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002122 size_t signature_length;
2123
Gilles Peskine8817f612018-12-18 00:18:46 +01002124 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002125
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002126 psa_set_key_usage_flags( &attributes, policy_usage );
2127 psa_set_key_algorithm( &attributes, policy_alg );
2128 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002129
Gilles Peskine049c7532019-05-15 20:22:09 +02002130 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2131 &handle ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002132
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002133 status = psa_sign_hash( handle, exercise_alg,
2134 payload, payload_length,
2135 signature, sizeof( signature ),
2136 &signature_length );
2137 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002138 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002139 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002140 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002141
2142 memset( signature, 0, sizeof( signature ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002143 status = psa_verify_hash( handle, exercise_alg,
2144 payload, payload_length,
2145 signature, sizeof( signature ) );
2146 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01002147 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02002148 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002149 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02002150
2151exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002152 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002153 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02002154}
2155/* END_CASE */
2156
Janos Follathba3fab92019-06-11 14:50:16 +01002157/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02002158void derive_key_policy( int policy_usage,
2159 int policy_alg,
2160 int key_type,
2161 data_t *key_data,
2162 int exercise_alg )
2163{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002164 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002165 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002166 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02002167 psa_status_t status;
2168
Gilles Peskine8817f612018-12-18 00:18:46 +01002169 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002170
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002171 psa_set_key_usage_flags( &attributes, policy_usage );
2172 psa_set_key_algorithm( &attributes, policy_alg );
2173 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002174
Gilles Peskine049c7532019-05-15 20:22:09 +02002175 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2176 &handle ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002177
Janos Follathba3fab92019-06-11 14:50:16 +01002178 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2179
2180 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
2181 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01002182 {
Janos Follathba3fab92019-06-11 14:50:16 +01002183 PSA_ASSERT( psa_key_derivation_input_bytes(
2184 &operation,
2185 PSA_KEY_DERIVATION_INPUT_SEED,
2186 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01002187 }
Janos Follathba3fab92019-06-11 14:50:16 +01002188
2189 status = psa_key_derivation_input_key( &operation,
2190 PSA_KEY_DERIVATION_INPUT_SECRET,
2191 handle );
2192
Gilles Peskineea0fb492018-07-12 17:17:20 +02002193 if( policy_alg == exercise_alg &&
2194 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002195 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002196 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002197 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002198
2199exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002200 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002201 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002202 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02002203}
2204/* END_CASE */
2205
2206/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02002207void agreement_key_policy( int policy_usage,
2208 int policy_alg,
2209 int key_type_arg,
2210 data_t *key_data,
2211 int exercise_alg )
2212{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002213 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002214 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002215 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002216 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02002217 psa_status_t status;
2218
Gilles Peskine8817f612018-12-18 00:18:46 +01002219 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002220
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002221 psa_set_key_usage_flags( &attributes, policy_usage );
2222 psa_set_key_algorithm( &attributes, policy_alg );
2223 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002224
Gilles Peskine049c7532019-05-15 20:22:09 +02002225 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2226 &handle ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002227
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002228 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
2229 status = key_agreement_with_self( &operation, handle );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002230
Gilles Peskine01d718c2018-09-18 12:01:02 +02002231 if( policy_alg == exercise_alg &&
2232 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01002233 PSA_ASSERT( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002234 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01002235 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002236
2237exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002238 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01002239 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002240 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02002241}
2242/* END_CASE */
2243
2244/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002245void key_policy_alg2( int key_type_arg, data_t *key_data,
2246 int usage_arg, int alg_arg, int alg2_arg )
2247{
2248 psa_key_handle_t handle = 0;
2249 psa_key_type_t key_type = key_type_arg;
2250 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2251 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
2252 psa_key_usage_t usage = usage_arg;
2253 psa_algorithm_t alg = alg_arg;
2254 psa_algorithm_t alg2 = alg2_arg;
2255
2256 PSA_ASSERT( psa_crypto_init( ) );
2257
2258 psa_set_key_usage_flags( &attributes, usage );
2259 psa_set_key_algorithm( &attributes, alg );
2260 psa_set_key_enrollment_algorithm( &attributes, alg2 );
2261 psa_set_key_type( &attributes, key_type );
2262 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2263 &handle ) );
2264
2265 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
2266 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
2267 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
2268 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
2269
2270 if( ! exercise_key( handle, usage, alg ) )
2271 goto exit;
2272 if( ! exercise_key( handle, usage, alg2 ) )
2273 goto exit;
2274
2275exit:
2276 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002277 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02002278}
2279/* END_CASE */
2280
2281/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002282void raw_agreement_key_policy( int policy_usage,
2283 int policy_alg,
2284 int key_type_arg,
2285 data_t *key_data,
2286 int exercise_alg )
2287{
2288 psa_key_handle_t handle = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002289 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002290 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002291 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002292 psa_status_t status;
2293
2294 PSA_ASSERT( psa_crypto_init( ) );
2295
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002296 psa_set_key_usage_flags( &attributes, policy_usage );
2297 psa_set_key_algorithm( &attributes, policy_alg );
2298 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002299
Gilles Peskine049c7532019-05-15 20:22:09 +02002300 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2301 &handle ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002302
2303 status = raw_key_agreement_with_self( exercise_alg, handle );
2304
2305 if( policy_alg == exercise_alg &&
2306 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
2307 PSA_ASSERT( status );
2308 else
2309 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
2310
2311exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02002312 psa_key_derivation_abort( &operation );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002313 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002314 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02002315}
2316/* END_CASE */
2317
2318/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002319void copy_success( int source_usage_arg,
2320 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002321 int type_arg, data_t *material,
2322 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002323 int target_usage_arg,
2324 int target_alg_arg, int target_alg2_arg,
2325 int expected_usage_arg,
2326 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01002327{
Gilles Peskineca25db92019-04-19 11:43:08 +02002328 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2329 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002330 psa_key_usage_t expected_usage = expected_usage_arg;
2331 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002332 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Gilles Peskineca25db92019-04-19 11:43:08 +02002333 psa_key_handle_t source_handle = 0;
2334 psa_key_handle_t target_handle = 0;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002335 uint8_t *export_buffer = NULL;
2336
Gilles Peskine57ab7212019-01-28 13:03:09 +01002337 PSA_ASSERT( psa_crypto_init( ) );
2338
Gilles Peskineca25db92019-04-19 11:43:08 +02002339 /* Prepare the source key. */
2340 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2341 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002342 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02002343 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002344 PSA_ASSERT( psa_import_key( &source_attributes,
2345 material->x, material->len,
2346 &source_handle ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02002347 PSA_ASSERT( psa_get_key_attributes( source_handle, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002348
Gilles Peskineca25db92019-04-19 11:43:08 +02002349 /* Prepare the target attributes. */
2350 if( copy_attributes )
2351 target_attributes = source_attributes;
2352 if( target_usage_arg != -1 )
2353 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2354 if( target_alg_arg != -1 )
2355 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002356 if( target_alg2_arg != -1 )
2357 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002358
2359 /* Copy the key. */
Gilles Peskine4a644642019-05-03 17:14:08 +02002360 PSA_ASSERT( psa_copy_key( source_handle,
2361 &target_attributes, &target_handle ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002362
2363 /* Destroy the source to ensure that this doesn't affect the target. */
2364 PSA_ASSERT( psa_destroy_key( source_handle ) );
2365
2366 /* Test that the target slot has the expected content and policy. */
Gilles Peskineca25db92019-04-19 11:43:08 +02002367 PSA_ASSERT( psa_get_key_attributes( target_handle, &target_attributes ) );
2368 TEST_EQUAL( psa_get_key_type( &source_attributes ),
2369 psa_get_key_type( &target_attributes ) );
2370 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
2371 psa_get_key_bits( &target_attributes ) );
2372 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
2373 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002374 TEST_EQUAL( expected_alg2,
2375 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002376 if( expected_usage & PSA_KEY_USAGE_EXPORT )
2377 {
2378 size_t length;
2379 ASSERT_ALLOC( export_buffer, material->len );
2380 PSA_ASSERT( psa_export_key( target_handle, export_buffer,
2381 material->len, &length ) );
2382 ASSERT_COMPARE( material->x, material->len,
2383 export_buffer, length );
2384 }
2385 if( ! exercise_key( target_handle, expected_usage, expected_alg ) )
2386 goto exit;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002387 if( ! exercise_key( target_handle, expected_usage, expected_alg2 ) )
2388 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01002389
2390 PSA_ASSERT( psa_close_key( target_handle ) );
2391
2392exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02002393 psa_reset_key_attributes( &source_attributes );
2394 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002395 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01002396 mbedtls_free( export_buffer );
2397}
2398/* END_CASE */
2399
2400/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002401void copy_fail( int source_usage_arg,
2402 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002403 int type_arg, data_t *material,
2404 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002405 int target_usage_arg,
2406 int target_alg_arg, int target_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02002407 int expected_status_arg )
2408{
2409 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
2410 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
2411 psa_key_handle_t source_handle = 0;
2412 psa_key_handle_t target_handle = 0;
2413
2414 PSA_ASSERT( psa_crypto_init( ) );
2415
2416 /* Prepare the source key. */
2417 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
2418 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002419 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002420 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02002421 PSA_ASSERT( psa_import_key( &source_attributes,
2422 material->x, material->len,
2423 &source_handle ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02002424
2425 /* Prepare the target attributes. */
2426 psa_set_key_type( &target_attributes, target_type_arg );
2427 psa_set_key_bits( &target_attributes, target_bits_arg );
2428 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
2429 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02002430 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02002431
2432 /* Try to copy the key. */
2433 TEST_EQUAL( psa_copy_key( source_handle,
2434 &target_attributes, &target_handle ),
2435 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002436
2437 PSA_ASSERT( psa_destroy_key( source_handle ) );
2438
Gilles Peskine4a644642019-05-03 17:14:08 +02002439exit:
2440 psa_reset_key_attributes( &source_attributes );
2441 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002442 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02002443}
2444/* END_CASE */
2445
2446/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00002447void hash_operation_init( )
2448{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002449 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00002450 /* Test each valid way of initializing the object, except for `= {0}`, as
2451 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2452 * though it's OK by the C standard. We could test for this, but we'd need
2453 * to supress the Clang warning for the test. */
2454 psa_hash_operation_t func = psa_hash_operation_init( );
2455 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
2456 psa_hash_operation_t zero;
2457
2458 memset( &zero, 0, sizeof( zero ) );
2459
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002460 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002461 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
2462 PSA_ERROR_BAD_STATE );
2463 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
2464 PSA_ERROR_BAD_STATE );
2465 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
2466 PSA_ERROR_BAD_STATE );
2467
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002468 /* A default hash operation should be abortable without error. */
2469 PSA_ASSERT( psa_hash_abort( &func ) );
2470 PSA_ASSERT( psa_hash_abort( &init ) );
2471 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002472}
2473/* END_CASE */
2474
2475/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002476void hash_setup( int alg_arg,
2477 int expected_status_arg )
2478{
2479 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002480 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002481 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002482 psa_status_t status;
2483
Gilles Peskine8817f612018-12-18 00:18:46 +01002484 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002485
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002486 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002487 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002488
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01002489 /* Whether setup succeeded or failed, abort must succeed. */
2490 PSA_ASSERT( psa_hash_abort( &operation ) );
2491
2492 /* If setup failed, reproduce the failure, so as to
2493 * test the resulting state of the operation object. */
2494 if( status != PSA_SUCCESS )
2495 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
2496
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002497 /* Now the operation object should be reusable. */
2498#if defined(KNOWN_SUPPORTED_HASH_ALG)
2499 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
2500 PSA_ASSERT( psa_hash_abort( &operation ) );
2501#endif
2502
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002503exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002504 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002505}
2506/* END_CASE */
2507
2508/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002509void hash_compute_fail( int alg_arg, data_t *input,
2510 int output_size_arg, int expected_status_arg )
2511{
2512 psa_algorithm_t alg = alg_arg;
2513 uint8_t *output = NULL;
2514 size_t output_size = output_size_arg;
2515 size_t output_length = INVALID_EXPORT_LENGTH;
2516 psa_status_t expected_status = expected_status_arg;
2517 psa_status_t status;
2518
2519 ASSERT_ALLOC( output, output_size );
2520
2521 PSA_ASSERT( psa_crypto_init( ) );
2522
2523 status = psa_hash_compute( alg, input->x, input->len,
2524 output, output_size, &output_length );
2525 TEST_EQUAL( status, expected_status );
2526 TEST_ASSERT( output_length <= output_size );
2527
2528exit:
2529 mbedtls_free( output );
2530 PSA_DONE( );
2531}
2532/* END_CASE */
2533
2534/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01002535void hash_compare_fail( int alg_arg, data_t *input,
2536 data_t *reference_hash,
2537 int expected_status_arg )
2538{
2539 psa_algorithm_t alg = alg_arg;
2540 psa_status_t expected_status = expected_status_arg;
2541 psa_status_t status;
2542
2543 PSA_ASSERT( psa_crypto_init( ) );
2544
2545 status = psa_hash_compare( alg, input->x, input->len,
2546 reference_hash->x, reference_hash->len );
2547 TEST_EQUAL( status, expected_status );
2548
2549exit:
2550 PSA_DONE( );
2551}
2552/* END_CASE */
2553
2554/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01002555void hash_compute_compare( int alg_arg, data_t *input,
2556 data_t *expected_output )
2557{
2558 psa_algorithm_t alg = alg_arg;
2559 uint8_t output[PSA_HASH_MAX_SIZE + 1];
2560 size_t output_length = INVALID_EXPORT_LENGTH;
2561 size_t i;
2562
2563 PSA_ASSERT( psa_crypto_init( ) );
2564
2565 /* Compute with tight buffer */
2566 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2567 output, PSA_HASH_SIZE( alg ),
2568 &output_length ) );
2569 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2570 ASSERT_COMPARE( output, output_length,
2571 expected_output->x, expected_output->len );
2572
2573 /* Compute with larger buffer */
2574 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
2575 output, sizeof( output ),
2576 &output_length ) );
2577 TEST_EQUAL( output_length, PSA_HASH_SIZE( alg ) );
2578 ASSERT_COMPARE( output, output_length,
2579 expected_output->x, expected_output->len );
2580
2581 /* Compare with correct hash */
2582 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
2583 output, output_length ) );
2584
2585 /* Compare with trailing garbage */
2586 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2587 output, output_length + 1 ),
2588 PSA_ERROR_INVALID_SIGNATURE );
2589
2590 /* Compare with truncated hash */
2591 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2592 output, output_length - 1 ),
2593 PSA_ERROR_INVALID_SIGNATURE );
2594
2595 /* Compare with corrupted value */
2596 for( i = 0; i < output_length; i++ )
2597 {
2598 test_set_step( i );
2599 output[i] ^= 1;
2600 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
2601 output, output_length ),
2602 PSA_ERROR_INVALID_SIGNATURE );
2603 output[i] ^= 1;
2604 }
2605
2606exit:
2607 PSA_DONE( );
2608}
2609/* END_CASE */
2610
2611/* BEGIN_CASE */
itayzafrirf86548d2018-11-01 10:44:32 +02002612void hash_bad_order( )
2613{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002614 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02002615 unsigned char input[] = "";
2616 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002617 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02002618 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2619 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2620 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002621 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02002622 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00002623 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02002624
Gilles Peskine8817f612018-12-18 00:18:46 +01002625 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002626
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002627 /* Call setup twice in a row. */
2628 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2629 TEST_EQUAL( psa_hash_setup( &operation, alg ),
2630 PSA_ERROR_BAD_STATE );
2631 PSA_ASSERT( psa_hash_abort( &operation ) );
2632
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002633 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002634 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002635 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002636 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002637
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002638 /* Call update after finish. */
2639 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2640 PSA_ASSERT( psa_hash_finish( &operation,
2641 hash, sizeof( hash ), &hash_len ) );
2642 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002643 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002644 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002645
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002646 /* Call verify without calling setup beforehand. */
2647 TEST_EQUAL( psa_hash_verify( &operation,
2648 valid_hash, sizeof( valid_hash ) ),
2649 PSA_ERROR_BAD_STATE );
2650 PSA_ASSERT( psa_hash_abort( &operation ) );
2651
2652 /* Call verify after finish. */
2653 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2654 PSA_ASSERT( psa_hash_finish( &operation,
2655 hash, sizeof( hash ), &hash_len ) );
2656 TEST_EQUAL( psa_hash_verify( &operation,
2657 valid_hash, sizeof( valid_hash ) ),
2658 PSA_ERROR_BAD_STATE );
2659 PSA_ASSERT( psa_hash_abort( &operation ) );
2660
2661 /* Call verify twice in a row. */
2662 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2663 PSA_ASSERT( psa_hash_verify( &operation,
2664 valid_hash, sizeof( valid_hash ) ) );
2665 TEST_EQUAL( psa_hash_verify( &operation,
2666 valid_hash, sizeof( valid_hash ) ),
2667 PSA_ERROR_BAD_STATE );
2668 PSA_ASSERT( psa_hash_abort( &operation ) );
2669
2670 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01002671 TEST_EQUAL( psa_hash_finish( &operation,
2672 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002673 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00002674 PSA_ASSERT( psa_hash_abort( &operation ) );
2675
2676 /* Call finish twice in a row. */
2677 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2678 PSA_ASSERT( psa_hash_finish( &operation,
2679 hash, sizeof( hash ), &hash_len ) );
2680 TEST_EQUAL( psa_hash_finish( &operation,
2681 hash, sizeof( hash ), &hash_len ),
2682 PSA_ERROR_BAD_STATE );
2683 PSA_ASSERT( psa_hash_abort( &operation ) );
2684
2685 /* Call finish after calling verify. */
2686 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
2687 PSA_ASSERT( psa_hash_verify( &operation,
2688 valid_hash, sizeof( valid_hash ) ) );
2689 TEST_EQUAL( psa_hash_finish( &operation,
2690 hash, sizeof( hash ), &hash_len ),
2691 PSA_ERROR_BAD_STATE );
2692 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02002693
2694exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002695 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02002696}
2697/* END_CASE */
2698
itayzafrir27e69452018-11-01 14:26:34 +02002699/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2700void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03002701{
2702 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02002703 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
2704 * appended to it */
2705 unsigned char hash[] = {
2706 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
2707 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
2708 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
itayzafrirec93d302018-10-18 18:01:10 +03002709 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002710 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03002711
Gilles Peskine8817f612018-12-18 00:18:46 +01002712 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03002713
itayzafrir27e69452018-11-01 14:26:34 +02002714 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002715 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002716 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002717 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002718
itayzafrir27e69452018-11-01 14:26:34 +02002719 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01002720 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002721 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002722 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03002723
itayzafrir27e69452018-11-01 14:26:34 +02002724 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002725 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002726 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01002727 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03002728
itayzafrirec93d302018-10-18 18:01:10 +03002729exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002730 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03002731}
2732/* END_CASE */
2733
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002734/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2735void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03002736{
2737 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02002738 unsigned char hash[PSA_HASH_MAX_SIZE];
itayzafrir58028322018-10-25 10:22:01 +03002739 size_t expected_size = PSA_HASH_SIZE( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00002740 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03002741 size_t hash_len;
2742
Gilles Peskine8817f612018-12-18 00:18:46 +01002743 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03002744
itayzafrir58028322018-10-25 10:22:01 +03002745 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01002746 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01002747 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01002748 hash, expected_size - 1, &hash_len ),
2749 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03002750
2751exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002752 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03002753}
2754/* END_CASE */
2755
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002756/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2757void hash_clone_source_state( )
2758{
2759 psa_algorithm_t alg = PSA_ALG_SHA_256;
2760 unsigned char hash[PSA_HASH_MAX_SIZE];
2761 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
2762 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2763 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2764 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2765 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2766 size_t hash_len;
2767
2768 PSA_ASSERT( psa_crypto_init( ) );
2769 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
2770
2771 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2772 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2773 PSA_ASSERT( psa_hash_finish( &op_finished,
2774 hash, sizeof( hash ), &hash_len ) );
2775 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2776 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2777
2778 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
2779 PSA_ERROR_BAD_STATE );
2780
2781 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
2782 PSA_ASSERT( psa_hash_finish( &op_init,
2783 hash, sizeof( hash ), &hash_len ) );
2784 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
2785 PSA_ASSERT( psa_hash_finish( &op_finished,
2786 hash, sizeof( hash ), &hash_len ) );
2787 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
2788 PSA_ASSERT( psa_hash_finish( &op_aborted,
2789 hash, sizeof( hash ), &hash_len ) );
2790
2791exit:
2792 psa_hash_abort( &op_source );
2793 psa_hash_abort( &op_init );
2794 psa_hash_abort( &op_setup );
2795 psa_hash_abort( &op_finished );
2796 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002797 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002798}
2799/* END_CASE */
2800
2801/* BEGIN_CASE depends_on:MBEDTLS_SHA256_C */
2802void hash_clone_target_state( )
2803{
2804 psa_algorithm_t alg = PSA_ALG_SHA_256;
2805 unsigned char hash[PSA_HASH_MAX_SIZE];
2806 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
2807 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
2808 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
2809 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
2810 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
2811 size_t hash_len;
2812
2813 PSA_ASSERT( psa_crypto_init( ) );
2814
2815 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
2816 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
2817 PSA_ASSERT( psa_hash_finish( &op_finished,
2818 hash, sizeof( hash ), &hash_len ) );
2819 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
2820 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
2821
2822 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
2823 PSA_ASSERT( psa_hash_finish( &op_target,
2824 hash, sizeof( hash ), &hash_len ) );
2825
2826 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
2827 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
2828 PSA_ERROR_BAD_STATE );
2829 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
2830 PSA_ERROR_BAD_STATE );
2831
2832exit:
2833 psa_hash_abort( &op_target );
2834 psa_hash_abort( &op_init );
2835 psa_hash_abort( &op_setup );
2836 psa_hash_abort( &op_finished );
2837 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002838 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002839}
2840/* END_CASE */
2841
itayzafrir58028322018-10-25 10:22:01 +03002842/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00002843void mac_operation_init( )
2844{
Jaeden Amero252ef282019-02-15 14:05:35 +00002845 const uint8_t input[1] = { 0 };
2846
Jaeden Amero769ce272019-01-04 11:48:03 +00002847 /* Test each valid way of initializing the object, except for `= {0}`, as
2848 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2849 * though it's OK by the C standard. We could test for this, but we'd need
2850 * to supress the Clang warning for the test. */
2851 psa_mac_operation_t func = psa_mac_operation_init( );
2852 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
2853 psa_mac_operation_t zero;
2854
2855 memset( &zero, 0, sizeof( zero ) );
2856
Jaeden Amero252ef282019-02-15 14:05:35 +00002857 /* A freshly-initialized MAC operation should not be usable. */
2858 TEST_EQUAL( psa_mac_update( &func,
2859 input, sizeof( input ) ),
2860 PSA_ERROR_BAD_STATE );
2861 TEST_EQUAL( psa_mac_update( &init,
2862 input, sizeof( input ) ),
2863 PSA_ERROR_BAD_STATE );
2864 TEST_EQUAL( psa_mac_update( &zero,
2865 input, sizeof( input ) ),
2866 PSA_ERROR_BAD_STATE );
2867
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002868 /* A default MAC operation should be abortable without error. */
2869 PSA_ASSERT( psa_mac_abort( &func ) );
2870 PSA_ASSERT( psa_mac_abort( &init ) );
2871 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00002872}
2873/* END_CASE */
2874
2875/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002876void mac_setup( int key_type_arg,
2877 data_t *key,
2878 int alg_arg,
2879 int expected_status_arg )
2880{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002881 psa_key_type_t key_type = key_type_arg;
2882 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002883 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002884 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002885 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
2886#if defined(KNOWN_SUPPORTED_MAC_ALG)
2887 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2888#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002889
Gilles Peskine8817f612018-12-18 00:18:46 +01002890 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002891
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002892 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
2893 &operation, &status ) )
2894 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002895 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002896
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002897 /* The operation object should be reusable. */
2898#if defined(KNOWN_SUPPORTED_MAC_ALG)
2899 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
2900 smoke_test_key_data,
2901 sizeof( smoke_test_key_data ),
2902 KNOWN_SUPPORTED_MAC_ALG,
2903 &operation, &status ) )
2904 goto exit;
2905 TEST_EQUAL( status, PSA_SUCCESS );
2906#endif
2907
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002908exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002909 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002910}
2911/* END_CASE */
2912
2913/* BEGIN_CASE */
Jaeden Amero252ef282019-02-15 14:05:35 +00002914void mac_bad_order( )
2915{
2916 psa_key_handle_t handle = 0;
2917 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
2918 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
2919 const uint8_t key[] = {
2920 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2921 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2922 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002923 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00002924 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
2925 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
2926 size_t sign_mac_length = 0;
2927 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
2928 const uint8_t verify_mac[] = {
2929 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
2930 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
2931 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
2932
2933 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002934 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002935 psa_set_key_algorithm( &attributes, alg );
2936 psa_set_key_type( &attributes, key_type );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002937
Gilles Peskine73676cb2019-05-15 20:15:10 +02002938 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02002939
Jaeden Amero252ef282019-02-15 14:05:35 +00002940 /* Call update without calling setup beforehand. */
2941 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2942 PSA_ERROR_BAD_STATE );
2943 PSA_ASSERT( psa_mac_abort( &operation ) );
2944
2945 /* Call sign finish without calling setup beforehand. */
2946 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
2947 &sign_mac_length),
2948 PSA_ERROR_BAD_STATE );
2949 PSA_ASSERT( psa_mac_abort( &operation ) );
2950
2951 /* Call verify finish without calling setup beforehand. */
2952 TEST_EQUAL( psa_mac_verify_finish( &operation,
2953 verify_mac, sizeof( verify_mac ) ),
2954 PSA_ERROR_BAD_STATE );
2955 PSA_ASSERT( psa_mac_abort( &operation ) );
2956
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002957 /* Call setup twice in a row. */
2958 PSA_ASSERT( psa_mac_sign_setup( &operation,
2959 handle, alg ) );
2960 TEST_EQUAL( psa_mac_sign_setup( &operation,
2961 handle, alg ),
2962 PSA_ERROR_BAD_STATE );
2963 PSA_ASSERT( psa_mac_abort( &operation ) );
2964
Jaeden Amero252ef282019-02-15 14:05:35 +00002965 /* Call update after sign finish. */
2966 PSA_ASSERT( psa_mac_sign_setup( &operation,
2967 handle, alg ) );
2968 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2969 PSA_ASSERT( psa_mac_sign_finish( &operation,
2970 sign_mac, sizeof( sign_mac ),
2971 &sign_mac_length ) );
2972 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2973 PSA_ERROR_BAD_STATE );
2974 PSA_ASSERT( psa_mac_abort( &operation ) );
2975
2976 /* Call update after verify finish. */
2977 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02002978 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00002979 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2980 PSA_ASSERT( psa_mac_verify_finish( &operation,
2981 verify_mac, sizeof( verify_mac ) ) );
2982 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
2983 PSA_ERROR_BAD_STATE );
2984 PSA_ASSERT( psa_mac_abort( &operation ) );
2985
2986 /* Call sign finish twice in a row. */
2987 PSA_ASSERT( psa_mac_sign_setup( &operation,
2988 handle, alg ) );
2989 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
2990 PSA_ASSERT( psa_mac_sign_finish( &operation,
2991 sign_mac, sizeof( sign_mac ),
2992 &sign_mac_length ) );
2993 TEST_EQUAL( psa_mac_sign_finish( &operation,
2994 sign_mac, sizeof( sign_mac ),
2995 &sign_mac_length ),
2996 PSA_ERROR_BAD_STATE );
2997 PSA_ASSERT( psa_mac_abort( &operation ) );
2998
2999 /* Call verify finish twice in a row. */
3000 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003001 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003002 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3003 PSA_ASSERT( psa_mac_verify_finish( &operation,
3004 verify_mac, sizeof( verify_mac ) ) );
3005 TEST_EQUAL( psa_mac_verify_finish( &operation,
3006 verify_mac, sizeof( verify_mac ) ),
3007 PSA_ERROR_BAD_STATE );
3008 PSA_ASSERT( psa_mac_abort( &operation ) );
3009
3010 /* Setup sign but try verify. */
3011 PSA_ASSERT( psa_mac_sign_setup( &operation,
3012 handle, alg ) );
3013 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3014 TEST_EQUAL( psa_mac_verify_finish( &operation,
3015 verify_mac, sizeof( verify_mac ) ),
3016 PSA_ERROR_BAD_STATE );
3017 PSA_ASSERT( psa_mac_abort( &operation ) );
3018
3019 /* Setup verify but try sign. */
3020 PSA_ASSERT( psa_mac_verify_setup( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02003021 handle, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00003022 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
3023 TEST_EQUAL( psa_mac_sign_finish( &operation,
3024 sign_mac, sizeof( sign_mac ),
3025 &sign_mac_length ),
3026 PSA_ERROR_BAD_STATE );
3027 PSA_ASSERT( psa_mac_abort( &operation ) );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003028
Gilles Peskine76b29a72019-05-28 14:08:50 +02003029 PSA_ASSERT( psa_destroy_key( handle ) );
3030
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003031exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003032 PSA_DONE( );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01003033}
3034/* END_CASE */
3035
3036/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003037void mac_sign( int key_type_arg,
3038 data_t *key,
3039 int alg_arg,
3040 data_t *input,
3041 data_t *expected_mac )
3042{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003043 psa_key_handle_t handle = 0;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003044 psa_key_type_t key_type = key_type_arg;
3045 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003046 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003047 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003048 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003049 size_t mac_buffer_size =
3050 PSA_MAC_FINAL_SIZE( key_type, PSA_BYTES_TO_BITS( key->len ), alg );
3051 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02003052 const size_t output_sizes_to_test[] = {
3053 0,
3054 1,
3055 expected_mac->len - 1,
3056 expected_mac->len,
3057 expected_mac->len + 1,
3058 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003059
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003060 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
Gilles Peskine3d404d62020-08-25 23:47:36 +02003061 /* We expect PSA_MAC_FINAL_SIZE to be exact. */
3062 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003063
Gilles Peskine8817f612018-12-18 00:18:46 +01003064 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003065
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003066 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003067 psa_set_key_algorithm( &attributes, alg );
3068 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003069
Gilles Peskine73676cb2019-05-15 20:15:10 +02003070 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003071
Gilles Peskine8b356b52020-08-25 23:44:59 +02003072 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
3073 {
3074 const size_t output_size = output_sizes_to_test[i];
3075 psa_status_t expected_status =
3076 ( output_size >= expected_mac->len ? PSA_SUCCESS :
3077 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003078
Gilles Peskine8b356b52020-08-25 23:44:59 +02003079 test_set_step( output_size );
3080 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003081
Gilles Peskine8b356b52020-08-25 23:44:59 +02003082 /* Calculate the MAC. */
3083 PSA_ASSERT( psa_mac_sign_setup( &operation,
3084 handle, alg ) );
3085 PSA_ASSERT( psa_mac_update( &operation,
3086 input->x, input->len ) );
3087 TEST_EQUAL( psa_mac_sign_finish( &operation,
3088 actual_mac, output_size,
3089 &mac_length ),
3090 expected_status );
3091 PSA_ASSERT( psa_mac_abort( &operation ) );
3092
3093 if( expected_status == PSA_SUCCESS )
3094 {
3095 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
3096 actual_mac, mac_length );
3097 }
3098 mbedtls_free( actual_mac );
3099 actual_mac = NULL;
3100 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003101
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003102exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003103 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003104 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003105 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02003106 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02003107}
3108/* END_CASE */
3109
3110/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003111void mac_verify( int key_type_arg,
3112 data_t *key,
3113 int alg_arg,
3114 data_t *input,
3115 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003116{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003117 psa_key_handle_t handle = 0;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003118 psa_key_type_t key_type = key_type_arg;
3119 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00003120 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003121 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003122 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003123
Gilles Peskine69c12672018-06-28 00:07:19 +02003124 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
3125
Gilles Peskine8817f612018-12-18 00:18:46 +01003126 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003127
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003128 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003129 psa_set_key_algorithm( &attributes, alg );
3130 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07003131
Gilles Peskine73676cb2019-05-15 20:15:10 +02003132 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02003133
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003134 /* Test the correct MAC. */
Gilles Peskine8817f612018-12-18 00:18:46 +01003135 PSA_ASSERT( psa_mac_verify_setup( &operation,
3136 handle, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01003137 PSA_ASSERT( psa_mac_update( &operation,
3138 input->x, input->len ) );
3139 PSA_ASSERT( psa_mac_verify_finish( &operation,
3140 expected_mac->x,
3141 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003142
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003143 /* Test a MAC that's too short. */
3144 PSA_ASSERT( psa_mac_verify_setup( &operation,
3145 handle, alg ) );
3146 PSA_ASSERT( psa_mac_update( &operation,
3147 input->x, input->len ) );
3148 TEST_EQUAL( psa_mac_verify_finish( &operation,
3149 expected_mac->x,
3150 expected_mac->len - 1 ),
3151 PSA_ERROR_INVALID_SIGNATURE );
3152
3153 /* Test a MAC that's too long. */
3154 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
3155 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
3156 PSA_ASSERT( psa_mac_verify_setup( &operation,
3157 handle, alg ) );
3158 PSA_ASSERT( psa_mac_update( &operation,
3159 input->x, input->len ) );
3160 TEST_EQUAL( psa_mac_verify_finish( &operation,
3161 perturbed_mac,
3162 expected_mac->len + 1 ),
3163 PSA_ERROR_INVALID_SIGNATURE );
3164
3165 /* Test changing one byte. */
3166 for( size_t i = 0; i < expected_mac->len; i++ )
3167 {
3168 test_set_step( i );
3169 perturbed_mac[i] ^= 1;
3170 PSA_ASSERT( psa_mac_verify_setup( &operation,
3171 handle, alg ) );
3172 PSA_ASSERT( psa_mac_update( &operation,
3173 input->x, input->len ) );
3174 TEST_EQUAL( psa_mac_verify_finish( &operation,
3175 perturbed_mac,
3176 expected_mac->len ),
3177 PSA_ERROR_INVALID_SIGNATURE );
3178 perturbed_mac[i] ^= 1;
3179 }
3180
Gilles Peskine8c9def32018-02-08 10:02:12 +01003181exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003182 psa_mac_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003183 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003184 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02003185 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003186}
3187/* END_CASE */
3188
3189/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00003190void cipher_operation_init( )
3191{
Jaeden Ameroab439972019-02-15 14:12:05 +00003192 const uint8_t input[1] = { 0 };
3193 unsigned char output[1] = { 0 };
3194 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003195 /* Test each valid way of initializing the object, except for `= {0}`, as
3196 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
3197 * though it's OK by the C standard. We could test for this, but we'd need
3198 * to supress the Clang warning for the test. */
3199 psa_cipher_operation_t func = psa_cipher_operation_init( );
3200 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
3201 psa_cipher_operation_t zero;
3202
3203 memset( &zero, 0, sizeof( zero ) );
3204
Jaeden Ameroab439972019-02-15 14:12:05 +00003205 /* A freshly-initialized cipher operation should not be usable. */
3206 TEST_EQUAL( psa_cipher_update( &func,
3207 input, sizeof( input ),
3208 output, sizeof( output ),
3209 &output_length ),
3210 PSA_ERROR_BAD_STATE );
3211 TEST_EQUAL( psa_cipher_update( &init,
3212 input, sizeof( input ),
3213 output, sizeof( output ),
3214 &output_length ),
3215 PSA_ERROR_BAD_STATE );
3216 TEST_EQUAL( psa_cipher_update( &zero,
3217 input, sizeof( input ),
3218 output, sizeof( output ),
3219 &output_length ),
3220 PSA_ERROR_BAD_STATE );
3221
Jaeden Amero5229bbb2019-02-07 16:33:37 +00003222 /* A default cipher operation should be abortable without error. */
3223 PSA_ASSERT( psa_cipher_abort( &func ) );
3224 PSA_ASSERT( psa_cipher_abort( &init ) );
3225 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00003226}
3227/* END_CASE */
3228
3229/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003230void cipher_setup( int key_type_arg,
3231 data_t *key,
3232 int alg_arg,
3233 int expected_status_arg )
3234{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003235 psa_key_type_t key_type = key_type_arg;
3236 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003237 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003238 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003239 psa_status_t status;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003240#if defined(KNOWN_SUPPORTED_MAC_ALG)
3241 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
3242#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003243
Gilles Peskine8817f612018-12-18 00:18:46 +01003244 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003245
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003246 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
3247 &operation, &status ) )
3248 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003249 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003250
Gilles Peskinef426e0f2019-02-25 17:42:03 +01003251 /* The operation object should be reusable. */
3252#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
3253 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
3254 smoke_test_key_data,
3255 sizeof( smoke_test_key_data ),
3256 KNOWN_SUPPORTED_CIPHER_ALG,
3257 &operation, &status ) )
3258 goto exit;
3259 TEST_EQUAL( status, PSA_SUCCESS );
3260#endif
3261
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003262exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003263 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003264 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003265}
3266/* END_CASE */
3267
3268/* BEGIN_CASE */
Jaeden Ameroab439972019-02-15 14:12:05 +00003269void cipher_bad_order( )
3270{
3271 psa_key_handle_t handle = 0;
3272 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
3273 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003274 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00003275 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
3276 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3277 const uint8_t key[] = {
3278 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
3279 0xaa, 0xaa, 0xaa, 0xaa };
3280 const uint8_t text[] = {
3281 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
3282 0xbb, 0xbb, 0xbb, 0xbb };
3283 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_SIZE(PSA_KEY_TYPE_AES)] = { 0 };
3284 size_t length = 0;
3285
3286 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003287 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3288 psa_set_key_algorithm( &attributes, alg );
3289 psa_set_key_type( &attributes, key_type );
Gilles Peskine73676cb2019-05-15 20:15:10 +02003290 PSA_ASSERT( psa_import_key( &attributes, key, sizeof( key ), &handle ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00003291
3292
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003293 /* Call encrypt setup twice in a row. */
3294 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3295 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, handle, alg ),
3296 PSA_ERROR_BAD_STATE );
3297 PSA_ASSERT( psa_cipher_abort( &operation ) );
3298
3299 /* Call decrypt setup twice in a row. */
3300 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, handle, alg ) );
3301 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, handle, alg ),
3302 PSA_ERROR_BAD_STATE );
3303 PSA_ASSERT( psa_cipher_abort( &operation ) );
3304
Jaeden Ameroab439972019-02-15 14:12:05 +00003305 /* Generate an IV without calling setup beforehand. */
3306 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3307 buffer, sizeof( buffer ),
3308 &length ),
3309 PSA_ERROR_BAD_STATE );
3310 PSA_ASSERT( psa_cipher_abort( &operation ) );
3311
3312 /* Generate an IV twice in a row. */
3313 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3314 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3315 buffer, sizeof( buffer ),
3316 &length ) );
3317 TEST_EQUAL( psa_cipher_generate_iv( &operation,
3318 buffer, sizeof( buffer ),
3319 &length ),
3320 PSA_ERROR_BAD_STATE );
3321 PSA_ASSERT( psa_cipher_abort( &operation ) );
3322
3323 /* Generate 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_generate_iv( &operation,
3328 buffer, sizeof( buffer ),
3329 &length ),
3330 PSA_ERROR_BAD_STATE );
3331 PSA_ASSERT( psa_cipher_abort( &operation ) );
3332
3333 /* Set an IV without calling setup beforehand. */
3334 TEST_EQUAL( psa_cipher_set_iv( &operation,
3335 iv, sizeof( iv ) ),
3336 PSA_ERROR_BAD_STATE );
3337 PSA_ASSERT( psa_cipher_abort( &operation ) );
3338
3339 /* Set an IV after it's already set. */
3340 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3341 PSA_ASSERT( psa_cipher_set_iv( &operation,
3342 iv, sizeof( iv ) ) );
3343 TEST_EQUAL( psa_cipher_set_iv( &operation,
3344 iv, sizeof( iv ) ),
3345 PSA_ERROR_BAD_STATE );
3346 PSA_ASSERT( psa_cipher_abort( &operation ) );
3347
3348 /* Set an IV after it's already generated. */
3349 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3350 PSA_ASSERT( psa_cipher_generate_iv( &operation,
3351 buffer, sizeof( buffer ),
3352 &length ) );
3353 TEST_EQUAL( psa_cipher_set_iv( &operation,
3354 iv, sizeof( iv ) ),
3355 PSA_ERROR_BAD_STATE );
3356 PSA_ASSERT( psa_cipher_abort( &operation ) );
3357
3358 /* Call update without calling setup beforehand. */
3359 TEST_EQUAL( psa_cipher_update( &operation,
3360 text, sizeof( text ),
3361 buffer, sizeof( buffer ),
3362 &length ),
3363 PSA_ERROR_BAD_STATE );
3364 PSA_ASSERT( psa_cipher_abort( &operation ) );
3365
3366 /* Call update without an IV where an IV is required. */
3367 TEST_EQUAL( psa_cipher_update( &operation,
3368 text, sizeof( text ),
3369 buffer, sizeof( buffer ),
3370 &length ),
3371 PSA_ERROR_BAD_STATE );
3372 PSA_ASSERT( psa_cipher_abort( &operation ) );
3373
3374 /* Call update after finish. */
3375 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3376 PSA_ASSERT( psa_cipher_set_iv( &operation,
3377 iv, sizeof( iv ) ) );
3378 PSA_ASSERT( psa_cipher_finish( &operation,
3379 buffer, sizeof( buffer ), &length ) );
3380 TEST_EQUAL( psa_cipher_update( &operation,
3381 text, sizeof( text ),
3382 buffer, sizeof( buffer ),
3383 &length ),
3384 PSA_ERROR_BAD_STATE );
3385 PSA_ASSERT( psa_cipher_abort( &operation ) );
3386
3387 /* Call finish without calling setup beforehand. */
3388 TEST_EQUAL( psa_cipher_finish( &operation,
3389 buffer, sizeof( buffer ), &length ),
3390 PSA_ERROR_BAD_STATE );
3391 PSA_ASSERT( psa_cipher_abort( &operation ) );
3392
3393 /* Call finish without an IV where an IV is required. */
3394 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3395 /* Not calling update means we are encrypting an empty buffer, which is OK
3396 * for cipher modes with padding. */
3397 TEST_EQUAL( psa_cipher_finish( &operation,
3398 buffer, sizeof( buffer ), &length ),
3399 PSA_ERROR_BAD_STATE );
3400 PSA_ASSERT( psa_cipher_abort( &operation ) );
3401
3402 /* Call finish twice in a row. */
3403 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, handle, alg ) );
3404 PSA_ASSERT( psa_cipher_set_iv( &operation,
3405 iv, sizeof( iv ) ) );
3406 PSA_ASSERT( psa_cipher_finish( &operation,
3407 buffer, sizeof( buffer ), &length ) );
3408 TEST_EQUAL( psa_cipher_finish( &operation,
3409 buffer, sizeof( buffer ), &length ),
3410 PSA_ERROR_BAD_STATE );
3411 PSA_ASSERT( psa_cipher_abort( &operation ) );
3412
Gilles Peskine76b29a72019-05-28 14:08:50 +02003413 PSA_ASSERT( psa_destroy_key( handle ) );
3414
Jaeden Ameroab439972019-02-15 14:12:05 +00003415exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003416 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003417 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003418}
3419/* END_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003420
Gilles Peskine50e586b2018-06-08 14:28:46 +02003421/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003422void cipher_encrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003423 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003424 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003425 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003426{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003427 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003428 psa_status_t status;
3429 psa_key_type_t key_type = key_type_arg;
3430 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003431 psa_status_t expected_status = expected_status_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003432 unsigned char *output = NULL;
3433 size_t output_buffer_size = 0;
3434 size_t function_output_length = 0;
3435 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003436 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003437 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003438
Gilles Peskine8817f612018-12-18 00:18:46 +01003439 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003440
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003441 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3442 psa_set_key_algorithm( &attributes, alg );
3443 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003444
Gilles Peskine73676cb2019-05-15 20:15:10 +02003445 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003446
Gilles Peskine8817f612018-12-18 00:18:46 +01003447 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3448 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003449
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003450 if( iv->len > 0 )
3451 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003452 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003453 }
3454
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003455 output_buffer_size = ( (size_t) input->len +
3456 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003457 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003458
Gilles Peskine8817f612018-12-18 00:18:46 +01003459 PSA_ASSERT( psa_cipher_update( &operation,
3460 input->x, input->len,
3461 output, output_buffer_size,
3462 &function_output_length ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003463 total_output_length += function_output_length;
3464 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003465 output + total_output_length,
3466 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003467 &function_output_length );
3468 total_output_length += function_output_length;
3469
Gilles Peskinefe11b722018-12-18 00:24:04 +01003470 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003471 if( expected_status == PSA_SUCCESS )
3472 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003473 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003474 ASSERT_COMPARE( expected_output->x, expected_output->len,
3475 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003476 }
3477
3478exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003479 psa_cipher_abort( &operation );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003480 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003481 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003482 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003483}
3484/* END_CASE */
3485
3486/* BEGIN_CASE */
3487void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003488 data_t *key, data_t *iv,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003489 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003490 int first_part_size_arg,
3491 int output1_length_arg, int output2_length_arg,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003492 data_t *expected_output )
3493{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003494 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003495 psa_key_type_t key_type = key_type_arg;
3496 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003497 size_t first_part_size = first_part_size_arg;
3498 size_t output1_length = output1_length_arg;
3499 size_t output2_length = output2_length_arg;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003500 unsigned char *output = NULL;
3501 size_t output_buffer_size = 0;
3502 size_t function_output_length = 0;
3503 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003504 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003505 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003506
Gilles Peskine8817f612018-12-18 00:18:46 +01003507 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003508
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003509 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3510 psa_set_key_algorithm( &attributes, alg );
3511 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003512
Gilles Peskine73676cb2019-05-15 20:15:10 +02003513 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003514
Gilles Peskine8817f612018-12-18 00:18:46 +01003515 PSA_ASSERT( psa_cipher_encrypt_setup( &operation,
3516 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003517
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003518 if( iv->len > 0 )
3519 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003520 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003521 }
3522
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003523 output_buffer_size = ( (size_t) input->len +
3524 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003525 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003526
Gilles Peskinee0866522019-02-19 19:44:00 +01003527 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003528 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
3529 output, output_buffer_size,
3530 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003531 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003532 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003533 PSA_ASSERT( psa_cipher_update( &operation,
3534 input->x + first_part_size,
3535 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003536 output + total_output_length,
3537 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003538 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003539 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003540 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003541 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003542 output + total_output_length,
3543 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003544 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003545 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003546 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003547
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003548 ASSERT_COMPARE( expected_output->x, expected_output->len,
3549 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003550
3551exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003552 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003553 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003554 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003555 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003556}
3557/* END_CASE */
3558
3559/* BEGIN_CASE */
3560void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003561 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003562 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003563 int first_part_size_arg,
3564 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003565 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003566{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003567 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003568
3569 psa_key_type_t key_type = key_type_arg;
3570 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003571 size_t first_part_size = first_part_size_arg;
3572 size_t output1_length = output1_length_arg;
3573 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003574 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003575 size_t output_buffer_size = 0;
3576 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003577 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003578 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003579 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003580
Gilles Peskine8817f612018-12-18 00:18:46 +01003581 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003582
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003583 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3584 psa_set_key_algorithm( &attributes, alg );
3585 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003586
Gilles Peskine73676cb2019-05-15 20:15:10 +02003587 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003588
Gilles Peskine8817f612018-12-18 00:18:46 +01003589 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3590 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003591
Steven Cooreman177deba2020-09-07 17:14:14 +02003592 if( iv->len > 0 )
3593 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003594 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003595 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003596
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003597 output_buffer_size = ( (size_t) input->len +
3598 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003599 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003600
Gilles Peskinee0866522019-02-19 19:44:00 +01003601 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01003602 PSA_ASSERT( psa_cipher_update( &operation,
3603 input->x, first_part_size,
3604 output, output_buffer_size,
3605 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003606 TEST_ASSERT( function_output_length == output1_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003607 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003608 PSA_ASSERT( psa_cipher_update( &operation,
3609 input->x + first_part_size,
3610 input->len - first_part_size,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003611 output + total_output_length,
3612 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003613 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01003614 TEST_ASSERT( function_output_length == output2_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003615 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003616 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003617 output + total_output_length,
3618 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003619 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003620 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01003621 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003622
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003623 ASSERT_COMPARE( expected_output->x, expected_output->len,
3624 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003625
3626exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003627 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003628 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003629 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003630 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003631}
3632/* END_CASE */
3633
Gilles Peskine50e586b2018-06-08 14:28:46 +02003634/* BEGIN_CASE */
3635void cipher_decrypt( int alg_arg, int key_type_arg,
Gilles Peskine423005e2019-05-06 15:22:57 +02003636 data_t *key, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003637 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003638 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02003639{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003640 psa_key_handle_t handle = 0;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003641 psa_status_t status;
3642 psa_key_type_t key_type = key_type_arg;
3643 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02003644 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003645 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003646 size_t output_buffer_size = 0;
3647 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003648 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003649 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003650 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003651
Gilles Peskine8817f612018-12-18 00:18:46 +01003652 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003653
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003654 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3655 psa_set_key_algorithm( &attributes, alg );
3656 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003657
Gilles Peskine73676cb2019-05-15 20:15:10 +02003658 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003659
Gilles Peskine8817f612018-12-18 00:18:46 +01003660 PSA_ASSERT( psa_cipher_decrypt_setup( &operation,
3661 handle, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003662
Steven Cooreman177deba2020-09-07 17:14:14 +02003663 if( iv->len > 0 )
3664 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003665 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003666 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02003667
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003668 output_buffer_size = ( (size_t) input->len +
3669 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003670 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003671
Gilles Peskine8817f612018-12-18 00:18:46 +01003672 PSA_ASSERT( psa_cipher_update( &operation,
3673 input->x, input->len,
3674 output, output_buffer_size,
3675 &function_output_length ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003676 total_output_length += function_output_length;
Gilles Peskine50e586b2018-06-08 14:28:46 +02003677 status = psa_cipher_finish( &operation,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003678 output + total_output_length,
3679 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02003680 &function_output_length );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02003681 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01003682 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003683
3684 if( expected_status == PSA_SUCCESS )
3685 {
Gilles Peskine8817f612018-12-18 00:18:46 +01003686 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003687 ASSERT_COMPARE( expected_output->x, expected_output->len,
3688 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003689 }
3690
Gilles Peskine50e586b2018-06-08 14:28:46 +02003691exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003692 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003693 mbedtls_free( output );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003694 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003695 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02003696}
3697/* END_CASE */
3698
Gilles Peskine50e586b2018-06-08 14:28:46 +02003699/* BEGIN_CASE */
3700void cipher_verify_output( int alg_arg, int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003701 data_t *key,
3702 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02003703{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003704 psa_key_handle_t handle = 0;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003705 psa_key_type_t key_type = key_type_arg;
3706 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07003707 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02003708 size_t iv_size = 16;
3709 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003710 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003711 size_t output1_size = 0;
3712 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003713 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003714 size_t output2_size = 0;
3715 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003716 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003717 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3718 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003719 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003720
Gilles Peskine8817f612018-12-18 00:18:46 +01003721 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003722
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003723 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3724 psa_set_key_algorithm( &attributes, alg );
3725 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003726
Gilles Peskine73676cb2019-05-15 20:15:10 +02003727 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003728
Gilles Peskine8817f612018-12-18 00:18:46 +01003729 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3730 handle, alg ) );
3731 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3732 handle, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003733
Steven Cooreman177deba2020-09-07 17:14:14 +02003734 if( alg != PSA_ALG_ECB_NO_PADDING )
3735 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003736 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3737 iv, iv_size,
3738 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003739 }
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003740 output1_size = ( (size_t) input->len +
3741 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003742 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03003743
Gilles Peskine8817f612018-12-18 00:18:46 +01003744 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
3745 output1, output1_size,
3746 &output1_length ) );
3747 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003748 output1 + output1_length,
3749 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003750 &function_output_length ) );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003751
Gilles Peskine048b7f02018-06-08 14:20:49 +02003752 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003753
Gilles Peskine8817f612018-12-18 00:18:46 +01003754 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003755
3756 output2_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003757 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03003758
Steven Cooreman177deba2020-09-07 17:14:14 +02003759 if( iv_length > 0 )
3760 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003761 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3762 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003763 }
3764
Gilles Peskine8817f612018-12-18 00:18:46 +01003765 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
3766 output2, output2_size,
3767 &output2_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003768 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01003769 PSA_ASSERT( psa_cipher_finish( &operation2,
3770 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01003771 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01003772 &function_output_length ) );
Moran Pekerded84402018-06-06 16:36:50 +03003773
Gilles Peskine048b7f02018-06-08 14:20:49 +02003774 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003775
Gilles Peskine8817f612018-12-18 00:18:46 +01003776 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03003777
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003778 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03003779
3780exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003781 psa_cipher_abort( &operation1 );
3782 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003783 mbedtls_free( output1 );
3784 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003785 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003786 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03003787}
3788/* END_CASE */
3789
3790/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02003791void cipher_verify_output_multipart( int alg_arg,
3792 int key_type_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03003793 data_t *key,
3794 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01003795 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03003796{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003797 psa_key_handle_t handle = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003798 psa_key_type_t key_type = key_type_arg;
3799 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01003800 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03003801 unsigned char iv[16] = {0};
3802 size_t iv_size = 16;
3803 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003804 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003805 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003806 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03003807 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003808 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03003809 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02003810 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00003811 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
3812 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003813 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03003814
Gilles Peskine8817f612018-12-18 00:18:46 +01003815 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03003816
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003817 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3818 psa_set_key_algorithm( &attributes, alg );
3819 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03003820
Gilles Peskine73676cb2019-05-15 20:15:10 +02003821 PSA_ASSERT( psa_import_key( &attributes, key->x, key->len, &handle ) );
Moran Pekerded84402018-06-06 16:36:50 +03003822
Gilles Peskine8817f612018-12-18 00:18:46 +01003823 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1,
3824 handle, alg ) );
3825 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2,
3826 handle, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03003827
Steven Cooreman177deba2020-09-07 17:14:14 +02003828 if( alg != PSA_ALG_ECB_NO_PADDING )
3829 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003830 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
3831 iv, iv_size,
3832 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003833 }
3834
Gilles Peskine9d8eea72018-12-17 23:34:57 +01003835 output1_buffer_size = ( (size_t) input->len +
3836 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003837 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03003838
Gilles Peskinee0866522019-02-19 19:44:00 +01003839 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003840
Gilles Peskine8817f612018-12-18 00:18:46 +01003841 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
3842 output1, output1_buffer_size,
3843 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003844 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003845
Gilles Peskine8817f612018-12-18 00:18:46 +01003846 PSA_ASSERT( psa_cipher_update( &operation1,
3847 input->x + first_part_size,
3848 input->len - first_part_size,
3849 output1, output1_buffer_size,
3850 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003851 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003852
Gilles Peskine8817f612018-12-18 00:18:46 +01003853 PSA_ASSERT( psa_cipher_finish( &operation1,
3854 output1 + output1_length,
3855 output1_buffer_size - output1_length,
3856 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003857 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02003858
Gilles Peskine8817f612018-12-18 00:18:46 +01003859 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003860
Gilles Peskine048b7f02018-06-08 14:20:49 +02003861 output2_buffer_size = output1_length;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003862 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003863
Steven Cooreman177deba2020-09-07 17:14:14 +02003864 if( iv_length > 0 )
3865 {
Steven Cooremana6033e92020-08-25 11:47:50 +02003866 PSA_ASSERT( psa_cipher_set_iv( &operation2,
3867 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003868 }
Moran Pekerded84402018-06-06 16:36:50 +03003869
Gilles Peskine8817f612018-12-18 00:18:46 +01003870 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
3871 output2, output2_buffer_size,
3872 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003873 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003874
Gilles Peskine8817f612018-12-18 00:18:46 +01003875 PSA_ASSERT( psa_cipher_update( &operation2,
3876 output1 + first_part_size,
3877 output1_length - first_part_size,
3878 output2, output2_buffer_size,
3879 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003880 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03003881
Gilles Peskine8817f612018-12-18 00:18:46 +01003882 PSA_ASSERT( psa_cipher_finish( &operation2,
3883 output2 + output2_length,
3884 output2_buffer_size - output2_length,
3885 &function_output_length ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02003886 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02003887
Gilles Peskine8817f612018-12-18 00:18:46 +01003888 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003889
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003890 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003891
3892exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003893 psa_cipher_abort( &operation1 );
3894 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003895 mbedtls_free( output1 );
3896 mbedtls_free( output2 );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003897 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003898 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003899}
3900/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003901
Gilles Peskine20035e32018-02-03 22:44:14 +01003902/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003903void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003904 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003905 data_t *nonce,
3906 data_t *additional_data,
3907 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003908 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003909{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003910 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003911 psa_key_type_t key_type = key_type_arg;
3912 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003913 unsigned char *output_data = NULL;
3914 size_t output_size = 0;
3915 size_t output_length = 0;
3916 unsigned char *output_data2 = NULL;
3917 size_t output_length2 = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003918 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine4abf7412018-06-18 16:35:34 +02003919 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003920 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003921
Gilles Peskine4abf7412018-06-18 16:35:34 +02003922 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003923 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3924 * should be exact. */
3925 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
3926 TEST_EQUAL( output_size,
3927 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003928 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003929
Gilles Peskine8817f612018-12-18 00:18:46 +01003930 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003931
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003932 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3933 psa_set_key_algorithm( &attributes, alg );
3934 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003935
Gilles Peskine049c7532019-05-15 20:22:09 +02003936 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3937 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003938
Gilles Peskinefe11b722018-12-18 00:24:04 +01003939 TEST_EQUAL( psa_aead_encrypt( handle, alg,
3940 nonce->x, nonce->len,
3941 additional_data->x,
3942 additional_data->len,
3943 input_data->x, input_data->len,
3944 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003945 &output_length ),
3946 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003947
3948 if( PSA_SUCCESS == expected_result )
3949 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003950 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003951
Gilles Peskine003a4a92019-05-14 16:09:40 +02003952 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3953 * should be exact. */
3954 TEST_EQUAL( input_data->len,
3955 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, output_length ) );
3956
Gilles Peskinefe11b722018-12-18 00:24:04 +01003957 TEST_EQUAL( psa_aead_decrypt( handle, alg,
3958 nonce->x, nonce->len,
3959 additional_data->x,
3960 additional_data->len,
3961 output_data, output_length,
3962 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003963 &output_length2 ),
3964 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003965
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003966 ASSERT_COMPARE( input_data->x, input_data->len,
3967 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003968 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003969
Gilles Peskinea1cac842018-06-11 19:33:02 +02003970exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003971 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003972 mbedtls_free( output_data );
3973 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003974 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003975}
3976/* END_CASE */
3977
3978/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003979void aead_encrypt( int key_type_arg, data_t *key_data,
3980 int alg_arg,
3981 data_t *nonce,
3982 data_t *additional_data,
3983 data_t *input_data,
3984 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003985{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003986 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003987 psa_key_type_t key_type = key_type_arg;
3988 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003989 unsigned char *output_data = NULL;
3990 size_t output_size = 0;
3991 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003992 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003993 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003994
Gilles Peskine4abf7412018-06-18 16:35:34 +02003995 output_size = input_data->len + tag_length;
Gilles Peskine003a4a92019-05-14 16:09:40 +02003996 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3997 * should be exact. */
3998 TEST_EQUAL( output_size,
3999 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004000 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004001
Gilles Peskine8817f612018-12-18 00:18:46 +01004002 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004003
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004004 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4005 psa_set_key_algorithm( &attributes, alg );
4006 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004007
Gilles Peskine049c7532019-05-15 20:22:09 +02004008 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4009 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004010
Gilles Peskine8817f612018-12-18 00:18:46 +01004011 PSA_ASSERT( psa_aead_encrypt( handle, alg,
4012 nonce->x, nonce->len,
4013 additional_data->x, additional_data->len,
4014 input_data->x, input_data->len,
4015 output_data, output_size,
4016 &output_length ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004017
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004018 ASSERT_COMPARE( expected_result->x, expected_result->len,
4019 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02004020
Gilles Peskinea1cac842018-06-11 19:33:02 +02004021exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004022 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004023 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004024 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004025}
4026/* END_CASE */
4027
4028/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02004029void aead_decrypt( int key_type_arg, data_t *key_data,
4030 int alg_arg,
4031 data_t *nonce,
4032 data_t *additional_data,
4033 data_t *input_data,
4034 data_t *expected_data,
4035 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02004036{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004037 psa_key_handle_t handle = 0;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004038 psa_key_type_t key_type = key_type_arg;
4039 psa_algorithm_t alg = alg_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004040 unsigned char *output_data = NULL;
4041 size_t output_size = 0;
4042 size_t output_length = 0;
Gilles Peskine003a4a92019-05-14 16:09:40 +02004043 size_t tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004044 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02004045 psa_status_t expected_result = expected_result_arg;
Gilles Peskinea1cac842018-06-11 19:33:02 +02004046
Gilles Peskine003a4a92019-05-14 16:09:40 +02004047 output_size = input_data->len - tag_length;
4048 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
4049 * should be exact. */
4050 if( expected_result != PSA_ERROR_INVALID_ARGUMENT )
4051 TEST_EQUAL( output_size,
4052 PSA_AEAD_DECRYPT_OUTPUT_SIZE( alg, input_data->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004053 ASSERT_ALLOC( output_data, output_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004054
Gilles Peskine8817f612018-12-18 00:18:46 +01004055 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004056
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004057 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4058 psa_set_key_algorithm( &attributes, alg );
4059 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004060
Gilles Peskine049c7532019-05-15 20:22:09 +02004061 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4062 &handle ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004063
Gilles Peskinefe11b722018-12-18 00:24:04 +01004064 TEST_EQUAL( psa_aead_decrypt( handle, alg,
4065 nonce->x, nonce->len,
4066 additional_data->x,
4067 additional_data->len,
4068 input_data->x, input_data->len,
4069 output_data, output_size,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004070 &output_length ),
4071 expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004072
Gilles Peskine2d277862018-06-18 15:41:12 +02004073 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004074 ASSERT_COMPARE( expected_data->x, expected_data->len,
4075 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004076
Gilles Peskinea1cac842018-06-11 19:33:02 +02004077exit:
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004078 psa_destroy_key( handle );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004079 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004080 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02004081}
4082/* END_CASE */
4083
4084/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004085void signature_size( int type_arg,
4086 int bits,
4087 int alg_arg,
4088 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004089{
4090 psa_key_type_t type = type_arg;
4091 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004092 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004093
Gilles Peskinefe11b722018-12-18 00:24:04 +01004094 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01004095#if defined(MBEDTLS_TEST_DEPRECATED)
4096 TEST_EQUAL( actual_size,
4097 PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( type, bits, alg ) );
4098#endif /* MBEDTLS_TEST_DEPRECATED */
4099
Gilles Peskinee59236f2018-01-27 23:32:46 +01004100exit:
4101 ;
4102}
4103/* END_CASE */
4104
4105/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004106void sign_deterministic( int key_type_arg, data_t *key_data,
4107 int alg_arg, data_t *input_data,
4108 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004109{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004110 psa_key_handle_t handle = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004111 psa_key_type_t key_type = key_type_arg;
4112 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004113 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01004114 unsigned char *signature = NULL;
4115 size_t signature_size;
4116 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004117 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004118
Gilles Peskine8817f612018-12-18 00:18:46 +01004119 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004120
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004121 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004122 psa_set_key_algorithm( &attributes, alg );
4123 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004124
Gilles Peskine049c7532019-05-15 20:22:09 +02004125 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4126 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004127 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4128 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01004129
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004130 /* Allocate a buffer which has the size advertized by the
4131 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004132 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004133 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01004134 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004135 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004136 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004137
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004138 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004139 PSA_ASSERT( psa_sign_hash( handle, alg,
4140 input_data->x, input_data->len,
4141 signature, signature_size,
4142 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004143 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004144 ASSERT_COMPARE( output_data->x, output_data->len,
4145 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01004146
Gilles Peskine0627f982019-11-26 19:12:16 +01004147#if defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine895242b2019-11-29 12:15:40 +01004148 memset( signature, 0, signature_size );
4149 signature_length = INVALID_EXPORT_LENGTH;
Gilles Peskine0627f982019-11-26 19:12:16 +01004150 PSA_ASSERT( psa_asymmetric_sign( handle, alg,
4151 input_data->x, input_data->len,
4152 signature, signature_size,
4153 &signature_length ) );
4154 ASSERT_COMPARE( output_data->x, output_data->len,
4155 signature, signature_length );
4156#endif /* MBEDTLS_TEST_DEPRECATED */
4157
Gilles Peskine20035e32018-02-03 22:44:14 +01004158exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004159 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004160 psa_destroy_key( handle );
Gilles Peskine0189e752018-02-03 23:57:22 +01004161 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004162 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004163}
4164/* END_CASE */
4165
4166/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004167void sign_fail( int key_type_arg, data_t *key_data,
4168 int alg_arg, data_t *input_data,
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004169 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01004170{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004171 psa_key_handle_t handle = 0;
Gilles Peskine20035e32018-02-03 22:44:14 +01004172 psa_key_type_t key_type = key_type_arg;
4173 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004174 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01004175 psa_status_t actual_status;
4176 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01004177 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01004178 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004179 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01004180
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004181 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004182
Gilles Peskine8817f612018-12-18 00:18:46 +01004183 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004184
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004185 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004186 psa_set_key_algorithm( &attributes, alg );
4187 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07004188
Gilles Peskine049c7532019-05-15 20:22:09 +02004189 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4190 &handle ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01004191
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004192 actual_status = psa_sign_hash( handle, alg,
4193 input_data->x, input_data->len,
4194 signature, signature_size,
4195 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004196 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02004197 /* The value of *signature_length is unspecified on error, but
4198 * whatever it is, it should be less than signature_size, so that
4199 * if the caller tries to read *signature_length bytes without
4200 * checking the error code then they don't overflow a buffer. */
4201 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01004202
Gilles Peskine895242b2019-11-29 12:15:40 +01004203#if defined(MBEDTLS_TEST_DEPRECATED)
4204 signature_length = INVALID_EXPORT_LENGTH;
4205 TEST_EQUAL( psa_asymmetric_sign( handle, alg,
4206 input_data->x, input_data->len,
4207 signature, signature_size,
4208 &signature_length ),
4209 expected_status );
4210 TEST_ASSERT( signature_length <= signature_size );
4211#endif /* MBEDTLS_TEST_DEPRECATED */
4212
Gilles Peskine20035e32018-02-03 22:44:14 +01004213exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004214 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004215 psa_destroy_key( handle );
Gilles Peskine20035e32018-02-03 22:44:14 +01004216 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004217 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01004218}
4219/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03004220
4221/* BEGIN_CASE */
Gilles Peskine9911b022018-06-29 17:30:48 +02004222void sign_verify( int key_type_arg, data_t *key_data,
4223 int alg_arg, data_t *input_data )
4224{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004225 psa_key_handle_t handle = 0;
Gilles Peskine9911b022018-06-29 17:30:48 +02004226 psa_key_type_t key_type = key_type_arg;
4227 psa_algorithm_t alg = alg_arg;
4228 size_t key_bits;
4229 unsigned char *signature = NULL;
4230 size_t signature_size;
4231 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004232 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02004233
Gilles Peskine8817f612018-12-18 00:18:46 +01004234 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004235
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004236 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004237 psa_set_key_algorithm( &attributes, alg );
4238 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02004239
Gilles Peskine049c7532019-05-15 20:22:09 +02004240 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4241 &handle ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004242 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4243 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02004244
4245 /* Allocate a buffer which has the size advertized by the
4246 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004247 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02004248 key_bits, alg );
4249 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004250 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004251 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02004252
4253 /* Perform the signature. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004254 PSA_ASSERT( psa_sign_hash( handle, alg,
4255 input_data->x, input_data->len,
4256 signature, signature_size,
4257 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004258 /* Check that the signature length looks sensible. */
4259 TEST_ASSERT( signature_length <= signature_size );
4260 TEST_ASSERT( signature_length > 0 );
4261
4262 /* Use the library to verify that the signature is correct. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004263 PSA_ASSERT( psa_verify_hash( handle, alg,
4264 input_data->x, input_data->len,
4265 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02004266
4267 if( input_data->len != 0 )
4268 {
4269 /* Flip a bit in the input and verify that the signature is now
4270 * detected as invalid. Flip a bit at the beginning, not at the end,
4271 * because ECDSA may ignore the last few bits of the input. */
4272 input_data->x[0] ^= 1;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004273 TEST_EQUAL( psa_verify_hash( handle, alg,
4274 input_data->x, input_data->len,
4275 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004276 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02004277 }
4278
4279exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004280 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004281 psa_destroy_key( handle );
Gilles Peskine9911b022018-06-29 17:30:48 +02004282 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004283 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02004284}
4285/* END_CASE */
4286
4287/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004288void asymmetric_verify( int key_type_arg, data_t *key_data,
4289 int alg_arg, data_t *hash_data,
4290 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03004291{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004292 psa_key_handle_t handle = 0;
itayzafrir5c753392018-05-08 11:18:38 +03004293 psa_key_type_t key_type = key_type_arg;
4294 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004295 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03004296
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004297 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02004298
Gilles Peskine8817f612018-12-18 00:18:46 +01004299 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03004300
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004301 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004302 psa_set_key_algorithm( &attributes, alg );
4303 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03004304
Gilles Peskine049c7532019-05-15 20:22:09 +02004305 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4306 &handle ) );
itayzafrir5c753392018-05-08 11:18:38 +03004307
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004308 PSA_ASSERT( psa_verify_hash( handle, alg,
4309 hash_data->x, hash_data->len,
4310 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01004311
4312#if defined(MBEDTLS_TEST_DEPRECATED)
4313 PSA_ASSERT( psa_asymmetric_verify( handle, alg,
4314 hash_data->x, hash_data->len,
4315 signature_data->x,
4316 signature_data->len ) );
4317
4318#endif /* MBEDTLS_TEST_DEPRECATED */
4319
itayzafrir5c753392018-05-08 11:18:38 +03004320exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004321 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004322 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004323 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03004324}
4325/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004326
4327/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +03004328void asymmetric_verify_fail( int key_type_arg, data_t *key_data,
4329 int alg_arg, data_t *hash_data,
4330 data_t *signature_data,
4331 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004332{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004333 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004334 psa_key_type_t key_type = key_type_arg;
4335 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004336 psa_status_t actual_status;
4337 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004339
Gilles Peskine8817f612018-12-18 00:18:46 +01004340 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004341
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004342 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004343 psa_set_key_algorithm( &attributes, alg );
4344 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004345
Gilles Peskine049c7532019-05-15 20:22:09 +02004346 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4347 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004348
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004349 actual_status = psa_verify_hash( handle, alg,
4350 hash_data->x, hash_data->len,
4351 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004352 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004353
Gilles Peskine895242b2019-11-29 12:15:40 +01004354#if defined(MBEDTLS_TEST_DEPRECATED)
4355 TEST_EQUAL( psa_asymmetric_verify( handle, alg,
4356 hash_data->x, hash_data->len,
4357 signature_data->x, signature_data->len ),
4358 expected_status );
4359#endif /* MBEDTLS_TEST_DEPRECATED */
4360
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004361exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004362 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004363 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004364 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004365}
4366/* END_CASE */
4367
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004368/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02004369void asymmetric_encrypt( int key_type_arg,
4370 data_t *key_data,
4371 int alg_arg,
4372 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02004373 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02004374 int expected_output_length_arg,
4375 int expected_status_arg )
4376{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004377 psa_key_handle_t handle = 0;
Gilles Peskine656896e2018-06-29 19:12:28 +02004378 psa_key_type_t key_type = key_type_arg;
4379 psa_algorithm_t alg = alg_arg;
4380 size_t expected_output_length = expected_output_length_arg;
4381 size_t key_bits;
4382 unsigned char *output = NULL;
4383 size_t output_size;
4384 size_t output_length = ~0;
4385 psa_status_t actual_status;
4386 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004387 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02004388
Gilles Peskine8817f612018-12-18 00:18:46 +01004389 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004390
Gilles Peskine656896e2018-06-29 19:12:28 +02004391 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004392 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
4393 psa_set_key_algorithm( &attributes, alg );
4394 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004395 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4396 &handle ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02004397
4398 /* Determine the maximum output length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004399 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4400 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine656896e2018-06-29 19:12:28 +02004401 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004402 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02004403
4404 /* Encrypt the input */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004405 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02004406 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004407 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02004408 output, output_size,
4409 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004410 TEST_EQUAL( actual_status, expected_status );
4411 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02004412
Gilles Peskine68428122018-06-30 18:42:41 +02004413 /* If the label is empty, the test framework puts a non-null pointer
4414 * in label->x. Test that a null pointer works as well. */
4415 if( label->len == 0 )
4416 {
4417 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004418 if( output_size != 0 )
4419 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004420 actual_status = psa_asymmetric_encrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004421 input_data->x, input_data->len,
4422 NULL, label->len,
4423 output, output_size,
4424 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004425 TEST_EQUAL( actual_status, expected_status );
4426 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004427 }
4428
Gilles Peskine656896e2018-06-29 19:12:28 +02004429exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004430 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004431 psa_destroy_key( handle );
Gilles Peskine656896e2018-06-29 19:12:28 +02004432 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004433 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02004434}
4435/* END_CASE */
4436
4437/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004438void asymmetric_encrypt_decrypt( int key_type_arg,
4439 data_t *key_data,
4440 int alg_arg,
4441 data_t *input_data,
4442 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004443{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004444 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004445 psa_key_type_t key_type = key_type_arg;
4446 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004447 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004448 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004449 size_t output_size;
4450 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004451 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004452 size_t output2_size;
4453 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004454 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004455
Gilles Peskine8817f612018-12-18 00:18:46 +01004456 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004457
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004458 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
4459 psa_set_key_algorithm( &attributes, alg );
4460 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004461
Gilles Peskine049c7532019-05-15 20:22:09 +02004462 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4463 &handle ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004464
4465 /* Determine the maximum ciphertext length */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004466 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
4467 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004468 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004469 ASSERT_ALLOC( output, output_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004470 output2_size = input_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004471 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004472
Gilles Peskineeebd7382018-06-08 18:11:54 +02004473 /* We test encryption by checking that encrypt-then-decrypt gives back
4474 * the original plaintext because of the non-optional random
4475 * part of encryption process which prevents using fixed vectors. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004476 PSA_ASSERT( psa_asymmetric_encrypt( handle, alg,
4477 input_data->x, input_data->len,
4478 label->x, label->len,
4479 output, output_size,
4480 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004481 /* We don't know what ciphertext length to expect, but check that
4482 * it looks sensible. */
4483 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03004484
Gilles Peskine8817f612018-12-18 00:18:46 +01004485 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4486 output, output_length,
4487 label->x, label->len,
4488 output2, output2_size,
4489 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004490 ASSERT_COMPARE( input_data->x, input_data->len,
4491 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004492
4493exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004494 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004495 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004496 mbedtls_free( output );
4497 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004498 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004499}
4500/* END_CASE */
4501
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004502/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004503void asymmetric_decrypt( int key_type_arg,
4504 data_t *key_data,
4505 int alg_arg,
4506 data_t *input_data,
4507 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02004508 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004509{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004510 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004511 psa_key_type_t key_type = key_type_arg;
4512 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004513 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03004514 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004515 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004516 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004517
Jaeden Amero412654a2019-02-06 12:57:46 +00004518 output_size = expected_data->len;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004519 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004520
Gilles Peskine8817f612018-12-18 00:18:46 +01004521 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004522
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004523 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4524 psa_set_key_algorithm( &attributes, alg );
4525 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004526
Gilles Peskine049c7532019-05-15 20:22:09 +02004527 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4528 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004529
Gilles Peskine8817f612018-12-18 00:18:46 +01004530 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4531 input_data->x, input_data->len,
4532 label->x, label->len,
4533 output,
4534 output_size,
4535 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004536 ASSERT_COMPARE( expected_data->x, expected_data->len,
4537 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004538
Gilles Peskine68428122018-06-30 18:42:41 +02004539 /* If the label is empty, the test framework puts a non-null pointer
4540 * in label->x. Test that a null pointer works as well. */
4541 if( label->len == 0 )
4542 {
4543 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004544 if( output_size != 0 )
4545 memset( output, 0, output_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004546 PSA_ASSERT( psa_asymmetric_decrypt( handle, alg,
4547 input_data->x, input_data->len,
4548 NULL, label->len,
4549 output,
4550 output_size,
4551 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02004552 ASSERT_COMPARE( expected_data->x, expected_data->len,
4553 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02004554 }
4555
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004556exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004557 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004558 psa_destroy_key( handle );
itayzafrir3e02b3b2018-06-12 17:06:52 +03004559 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004560 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004561}
4562/* END_CASE */
4563
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004564/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02004565void asymmetric_decrypt_fail( int key_type_arg,
4566 data_t *key_data,
4567 int alg_arg,
4568 data_t *input_data,
4569 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00004570 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02004571 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004572{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004573 psa_key_handle_t handle = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004574 psa_key_type_t key_type = key_type_arg;
4575 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004576 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00004577 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004578 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004579 psa_status_t actual_status;
4580 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004581 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004582
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004583 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004584
Gilles Peskine8817f612018-12-18 00:18:46 +01004585 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004586
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004587 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
4588 psa_set_key_algorithm( &attributes, alg );
4589 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03004590
Gilles Peskine049c7532019-05-15 20:22:09 +02004591 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4592 &handle ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004593
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004594 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004595 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02004596 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02004597 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02004598 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004599 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004600 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004601
Gilles Peskine68428122018-06-30 18:42:41 +02004602 /* If the label is empty, the test framework puts a non-null pointer
4603 * in label->x. Test that a null pointer works as well. */
4604 if( label->len == 0 )
4605 {
4606 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004607 if( output_size != 0 )
4608 memset( output, 0, output_size );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004609 actual_status = psa_asymmetric_decrypt( handle, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004610 input_data->x, input_data->len,
4611 NULL, label->len,
4612 output, output_size,
4613 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004614 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004615 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004616 }
4617
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004618exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004619 psa_reset_key_attributes( &attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004620 psa_destroy_key( handle );
Gilles Peskine2d277862018-06-18 15:41:12 +02004621 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004622 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004623}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004624/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004625
4626/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004627void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004628{
4629 /* Test each valid way of initializing the object, except for `= {0}`, as
4630 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4631 * though it's OK by the C standard. We could test for this, but we'd need
4632 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004633 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004634 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4635 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4636 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004637
4638 memset( &zero, 0, sizeof( zero ) );
4639
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004640 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004641 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004642 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004643 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004644 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004645 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004646 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004647
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004648 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004649 PSA_ASSERT( psa_key_derivation_abort(&func) );
4650 PSA_ASSERT( psa_key_derivation_abort(&init) );
4651 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004652}
4653/* END_CASE */
4654
Janos Follath16de4a42019-06-13 16:32:24 +01004655/* BEGIN_CASE */
4656void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004657{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004658 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004659 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004660 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004661
Gilles Peskine8817f612018-12-18 00:18:46 +01004662 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004663
Janos Follath16de4a42019-06-13 16:32:24 +01004664 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004665 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004666
4667exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004668 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004669 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004670}
4671/* END_CASE */
4672
Janos Follathaf3c2a02019-06-12 12:34:34 +01004673/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004674void derive_set_capacity( int alg_arg, int capacity_arg,
4675 int expected_status_arg )
4676{
4677 psa_algorithm_t alg = alg_arg;
4678 size_t capacity = capacity_arg;
4679 psa_status_t expected_status = expected_status_arg;
4680 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4681
4682 PSA_ASSERT( psa_crypto_init( ) );
4683
4684 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4685
4686 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4687 expected_status );
4688
4689exit:
4690 psa_key_derivation_abort( &operation );
4691 PSA_DONE( );
4692}
4693/* END_CASE */
4694
4695/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004696void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004697 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004698 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004699 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004700 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004701 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004702 int expected_status_arg3,
4703 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004704{
4705 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004706 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4707 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004708 psa_status_t expected_statuses[] = {expected_status_arg1,
4709 expected_status_arg2,
4710 expected_status_arg3};
4711 data_t *inputs[] = {input1, input2, input3};
4712 psa_key_handle_t handles[] = {0, 0, 0};
4713 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4714 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4715 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004716 psa_key_type_t output_key_type = output_key_type_arg;
4717 psa_key_handle_t output_handle = 0;
4718 psa_status_t expected_output_status = expected_output_status_arg;
4719 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004720
4721 PSA_ASSERT( psa_crypto_init( ) );
4722
4723 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4724 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004725
4726 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4727
4728 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4729 {
Gilles Peskineb8965192019-09-24 16:21:10 +02004730 if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004731 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004732 psa_set_key_type( &attributes, key_types[i] );
4733 PSA_ASSERT( psa_import_key( &attributes,
4734 inputs[i]->x, inputs[i]->len,
4735 &handles[i] ) );
4736 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
4737 handles[i] ),
4738 expected_statuses[i] );
4739 }
4740 else
4741 {
4742 TEST_EQUAL( psa_key_derivation_input_bytes(
4743 &operation, steps[i],
4744 inputs[i]->x, inputs[i]->len ),
4745 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004746 }
4747 }
4748
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004749 if( output_key_type != PSA_KEY_TYPE_NONE )
4750 {
4751 psa_reset_key_attributes( &attributes );
4752 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4753 psa_set_key_bits( &attributes, 8 );
4754 actual_output_status =
4755 psa_key_derivation_output_key( &attributes, &operation,
4756 &output_handle );
4757 }
4758 else
4759 {
4760 uint8_t buffer[1];
4761 actual_output_status =
4762 psa_key_derivation_output_bytes( &operation,
4763 buffer, sizeof( buffer ) );
4764 }
4765 TEST_EQUAL( actual_output_status, expected_output_status );
4766
Janos Follathaf3c2a02019-06-12 12:34:34 +01004767exit:
4768 psa_key_derivation_abort( &operation );
4769 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4770 psa_destroy_key( handles[i] );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004771 psa_destroy_key( output_handle );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004772 PSA_DONE( );
4773}
4774/* END_CASE */
4775
Janos Follathd958bb72019-07-03 15:02:16 +01004776/* BEGIN_CASE */
4777void test_derive_invalid_key_derivation_state( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004778{
Janos Follathd958bb72019-07-03 15:02:16 +01004779 psa_algorithm_t alg = alg_arg;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004780 psa_key_handle_t handle = 0;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004781 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004782 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004783 unsigned char input1[] = "Input 1";
4784 size_t input1_length = sizeof( input1 );
4785 unsigned char input2[] = "Input 2";
4786 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004787 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004788 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004789 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4790 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4791 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004792 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004793
Gilles Peskine8817f612018-12-18 00:18:46 +01004794 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004795
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004796 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4797 psa_set_key_algorithm( &attributes, alg );
4798 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004799
Gilles Peskine73676cb2019-05-15 20:15:10 +02004800 PSA_ASSERT( psa_import_key( &attributes,
4801 key_data, sizeof( key_data ),
4802 &handle ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004803
4804 /* valid key derivation */
Janos Follathd958bb72019-07-03 15:02:16 +01004805 if( !setup_key_derivation_wrap( &operation, handle, alg,
4806 input1, input1_length,
4807 input2, input2_length,
4808 capacity ) )
4809 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004810
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004811 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004812 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004813 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004814
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004815 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004816
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004817 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004818 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004819
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004820exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004821 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004822 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004823 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004824}
4825/* END_CASE */
4826
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004827/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004828void test_derive_invalid_key_derivation_tests( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004829{
4830 uint8_t output_buffer[16];
4831 size_t buffer_size = 16;
4832 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004833 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004834
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004835 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4836 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004837 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004838
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004839 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004840 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004841
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004842 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004843
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004844 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4845 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004846 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004847
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004848 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004849 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004850
4851exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004852 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004853}
4854/* END_CASE */
4855
4856/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004857void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004858 int step1_arg, data_t *input1,
4859 int step2_arg, data_t *input2,
4860 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004861 int requested_capacity_arg,
4862 data_t *expected_output1,
4863 data_t *expected_output2 )
4864{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004865 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004866 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4867 data_t *inputs[] = {input1, input2, input3};
4868 psa_key_handle_t handles[] = {0, 0, 0};
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004869 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004870 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004871 uint8_t *expected_outputs[2] =
4872 {expected_output1->x, expected_output2->x};
4873 size_t output_sizes[2] =
4874 {expected_output1->len, expected_output2->len};
4875 size_t output_buffer_size = 0;
4876 uint8_t *output_buffer = NULL;
4877 size_t expected_capacity;
4878 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004879 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004880 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004881 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004882
4883 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4884 {
4885 if( output_sizes[i] > output_buffer_size )
4886 output_buffer_size = output_sizes[i];
4887 if( output_sizes[i] == 0 )
4888 expected_outputs[i] = NULL;
4889 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004890 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004891 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004892
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004893 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4894 psa_set_key_algorithm( &attributes, alg );
4895 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004896
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004897 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004898 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4899 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4900 requested_capacity ) );
4901 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004902 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004903 switch( steps[i] )
4904 {
4905 case 0:
4906 break;
4907 case PSA_KEY_DERIVATION_INPUT_SECRET:
4908 PSA_ASSERT( psa_import_key( &attributes,
4909 inputs[i]->x, inputs[i]->len,
4910 &handles[i] ) );
4911 PSA_ASSERT( psa_key_derivation_input_key(
4912 &operation, steps[i],
4913 handles[i] ) );
4914 break;
4915 default:
4916 PSA_ASSERT( psa_key_derivation_input_bytes(
4917 &operation, steps[i],
4918 inputs[i]->x, inputs[i]->len ) );
4919 break;
4920 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004921 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004922
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004923 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004924 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004925 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004926 expected_capacity = requested_capacity;
4927
4928 /* Expansion phase. */
4929 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4930 {
4931 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004932 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004933 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004934 if( expected_capacity == 0 && output_sizes[i] == 0 )
4935 {
4936 /* Reading 0 bytes when 0 bytes are available can go either way. */
4937 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004938 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004939 continue;
4940 }
4941 else if( expected_capacity == 0 ||
4942 output_sizes[i] > expected_capacity )
4943 {
4944 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004945 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004946 expected_capacity = 0;
4947 continue;
4948 }
4949 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004950 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004951 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004952 ASSERT_COMPARE( output_buffer, output_sizes[i],
4953 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004954 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004955 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004956 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004957 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004958 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004959 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004960 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004961
4962exit:
4963 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004964 psa_key_derivation_abort( &operation );
Gilles Peskine1468da72019-05-29 17:35:49 +02004965 for( i = 0; i < ARRAY_LENGTH( handles ); i++ )
4966 psa_destroy_key( handles[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004967 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004968}
4969/* END_CASE */
4970
4971/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004972void derive_full( int alg_arg,
4973 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004974 data_t *input1,
4975 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004976 int requested_capacity_arg )
4977{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01004978 psa_key_handle_t handle = 0;
Gilles Peskined54931c2018-07-17 21:06:59 +02004979 psa_algorithm_t alg = alg_arg;
4980 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004981 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004982 unsigned char output_buffer[16];
4983 size_t expected_capacity = requested_capacity;
4984 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004985 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004986
Gilles Peskine8817f612018-12-18 00:18:46 +01004987 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004988
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004989 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4990 psa_set_key_algorithm( &attributes, alg );
4991 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004992
Gilles Peskine049c7532019-05-15 20:22:09 +02004993 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
4994 &handle ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004995
Janos Follathf2815ea2019-07-03 12:41:36 +01004996 if( !setup_key_derivation_wrap( &operation, handle, alg,
4997 input1->x, input1->len,
4998 input2->x, input2->len,
4999 requested_capacity ) )
5000 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01005001
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005002 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005003 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005004 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005005
5006 /* Expansion phase. */
5007 while( current_capacity > 0 )
5008 {
5009 size_t read_size = sizeof( output_buffer );
5010 if( read_size > current_capacity )
5011 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005012 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005013 output_buffer,
5014 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005015 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005016 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005017 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005018 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02005019 }
5020
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005021 /* Check that the operation refuses to go over capacity. */
5022 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005023 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02005024
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005025 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02005026
5027exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005028 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005029 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005030 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02005031}
5032/* END_CASE */
5033
Janos Follathe60c9052019-07-03 13:51:30 +01005034/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005035void derive_key_exercise( int alg_arg,
5036 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01005037 data_t *input1,
5038 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005039 int derived_type_arg,
5040 int derived_bits_arg,
5041 int derived_usage_arg,
5042 int derived_alg_arg )
5043{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005044 psa_key_handle_t base_handle = 0;
5045 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005046 psa_algorithm_t alg = alg_arg;
5047 psa_key_type_t derived_type = derived_type_arg;
5048 size_t derived_bits = derived_bits_arg;
5049 psa_key_usage_t derived_usage = derived_usage_arg;
5050 psa_algorithm_t derived_alg = derived_alg_arg;
5051 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005052 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005053 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005054 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005055
Gilles Peskine8817f612018-12-18 00:18:46 +01005056 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005057
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005058 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5059 psa_set_key_algorithm( &attributes, alg );
5060 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005061 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
5062 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005063
5064 /* Derive a key. */
Janos Follathe60c9052019-07-03 13:51:30 +01005065 if ( setup_key_derivation_wrap( &operation, base_handle, alg,
5066 input1->x, input1->len,
5067 input2->x, input2->len, capacity ) )
5068 goto exit;
5069
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005070 psa_set_key_usage_flags( &attributes, derived_usage );
5071 psa_set_key_algorithm( &attributes, derived_alg );
5072 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005073 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005074 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005075 &derived_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005076
5077 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005078 PSA_ASSERT( psa_get_key_attributes( derived_handle, &got_attributes ) );
5079 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
5080 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005081
5082 /* Exercise the derived key. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005083 if( ! exercise_key( derived_handle, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02005084 goto exit;
5085
5086exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005087 psa_key_derivation_abort( &operation );
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005088 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005089 psa_destroy_key( base_handle );
5090 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005091 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005092}
5093/* END_CASE */
5094
Janos Follath42fd8882019-07-03 14:17:09 +01005095/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02005096void derive_key_export( int alg_arg,
5097 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01005098 data_t *input1,
5099 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02005100 int bytes1_arg,
5101 int bytes2_arg )
5102{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005103 psa_key_handle_t base_handle = 0;
5104 psa_key_handle_t derived_handle = 0;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005105 psa_algorithm_t alg = alg_arg;
5106 size_t bytes1 = bytes1_arg;
5107 size_t bytes2 = bytes2_arg;
5108 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005109 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005110 uint8_t *output_buffer = NULL;
5111 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005112 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5113 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02005114 size_t length;
5115
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005116 ASSERT_ALLOC( output_buffer, capacity );
5117 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01005118 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005119
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005120 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5121 psa_set_key_algorithm( &base_attributes, alg );
5122 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005123 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
5124 &base_handle ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005125
5126 /* Derive some material and output it. */
Janos Follath42fd8882019-07-03 14:17:09 +01005127 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5128 input1->x, input1->len,
5129 input2->x, input2->len, capacity ) )
5130 goto exit;
5131
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005132 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005133 output_buffer,
5134 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005135 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005136
5137 /* Derive the same output again, but this time store it in key objects. */
Janos Follath42fd8882019-07-03 14:17:09 +01005138 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5139 input1->x, input1->len,
5140 input2->x, input2->len, capacity ) )
5141 goto exit;
5142
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005143 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5144 psa_set_key_algorithm( &derived_attributes, 0 );
5145 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005146 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005147 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005148 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01005149 PSA_ASSERT( psa_export_key( derived_handle,
5150 export_buffer, bytes1,
5151 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005152 TEST_EQUAL( length, bytes1 );
Gilles Peskine8817f612018-12-18 00:18:46 +01005153 PSA_ASSERT( psa_destroy_key( derived_handle ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005154 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005155 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005156 &derived_handle ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01005157 PSA_ASSERT( psa_export_key( derived_handle,
5158 export_buffer + bytes1, bytes2,
5159 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005160 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005161
5162 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005163 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
5164 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005165
5166exit:
5167 mbedtls_free( output_buffer );
5168 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005169 psa_key_derivation_abort( &operation );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005170 psa_destroy_key( base_handle );
5171 psa_destroy_key( derived_handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005172 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02005173}
5174/* END_CASE */
5175
5176/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005177void derive_key( int alg_arg,
5178 data_t *key_data, data_t *input1, data_t *input2,
5179 int type_arg, int bits_arg,
5180 int expected_status_arg )
Gilles Peskinec744d992019-07-30 17:26:54 +02005181{
5182 psa_key_handle_t base_handle = 0;
5183 psa_key_handle_t derived_handle = 0;
5184 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005185 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02005186 size_t bits = bits_arg;
5187 psa_status_t expected_status = expected_status_arg;
5188 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
5189 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5190 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
5191
5192 PSA_ASSERT( psa_crypto_init( ) );
5193
5194 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
5195 psa_set_key_algorithm( &base_attributes, alg );
5196 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
5197 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
5198 &base_handle ) );
5199
5200 if( !setup_key_derivation_wrap( &operation, base_handle, alg,
5201 input1->x, input1->len,
5202 input2->x, input2->len, SIZE_MAX ) )
5203 goto exit;
5204
5205 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
5206 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02005207 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02005208 psa_set_key_bits( &derived_attributes, bits );
5209 TEST_EQUAL( psa_key_derivation_output_key( &derived_attributes, &operation,
5210 &derived_handle ),
5211 expected_status );
5212
5213exit:
5214 psa_key_derivation_abort( &operation );
5215 psa_destroy_key( base_handle );
5216 psa_destroy_key( derived_handle );
5217 PSA_DONE( );
5218}
5219/* END_CASE */
5220
5221/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005222void key_agreement_setup( int alg_arg,
5223 int our_key_type_arg, data_t *our_key_data,
5224 data_t *peer_key_data,
5225 int expected_status_arg )
5226{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005227 psa_key_handle_t our_key = 0;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005228 psa_algorithm_t alg = alg_arg;
5229 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005230 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005231 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02005232 psa_status_t expected_status = expected_status_arg;
5233 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005234
Gilles Peskine8817f612018-12-18 00:18:46 +01005235 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005236
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005237 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5238 psa_set_key_algorithm( &attributes, alg );
5239 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005240 PSA_ASSERT( psa_import_key( &attributes,
5241 our_key_data->x, our_key_data->len,
5242 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005243
Gilles Peskine77f40d82019-04-11 21:27:06 +02005244 /* The tests currently include inputs that should fail at either step.
5245 * Test cases that fail at the setup step should be changed to call
5246 * key_derivation_setup instead, and this function should be renamed
5247 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005248 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02005249 if( status == PSA_SUCCESS )
5250 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005251 TEST_EQUAL( psa_key_derivation_key_agreement(
5252 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
5253 our_key,
5254 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02005255 expected_status );
5256 }
5257 else
5258 {
5259 TEST_ASSERT( status == expected_status );
5260 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02005261
5262exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005263 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005264 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005265 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005266}
5267/* END_CASE */
5268
5269/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02005270void raw_key_agreement( int alg_arg,
5271 int our_key_type_arg, data_t *our_key_data,
5272 data_t *peer_key_data,
5273 data_t *expected_output )
5274{
5275 psa_key_handle_t our_key = 0;
5276 psa_algorithm_t alg = alg_arg;
5277 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005278 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02005279 unsigned char *output = NULL;
5280 size_t output_length = ~0;
5281
5282 ASSERT_ALLOC( output, expected_output->len );
5283 PSA_ASSERT( psa_crypto_init( ) );
5284
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005285 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5286 psa_set_key_algorithm( &attributes, alg );
5287 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005288 PSA_ASSERT( psa_import_key( &attributes,
5289 our_key_data->x, our_key_data->len,
5290 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005291
Gilles Peskinebe697d82019-05-16 18:00:41 +02005292 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
5293 peer_key_data->x, peer_key_data->len,
5294 output, expected_output->len,
5295 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005296 ASSERT_COMPARE( output, output_length,
5297 expected_output->x, expected_output->len );
5298
5299exit:
5300 mbedtls_free( output );
5301 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005302 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02005303}
5304/* END_CASE */
5305
5306/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02005307void key_agreement_capacity( int alg_arg,
5308 int our_key_type_arg, data_t *our_key_data,
5309 data_t *peer_key_data,
5310 int expected_capacity_arg )
5311{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005312 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005313 psa_algorithm_t alg = alg_arg;
5314 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005315 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005316 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02005317 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02005318 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02005319
Gilles Peskine8817f612018-12-18 00:18:46 +01005320 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005321
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005322 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5323 psa_set_key_algorithm( &attributes, alg );
5324 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005325 PSA_ASSERT( psa_import_key( &attributes,
5326 our_key_data->x, our_key_data->len,
5327 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005328
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005329 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005330 PSA_ASSERT( psa_key_derivation_key_agreement(
5331 &operation,
5332 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5333 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005334 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5335 {
5336 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005337 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005338 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005339 NULL, 0 ) );
5340 }
Gilles Peskine59685592018-09-18 12:11:34 +02005341
Gilles Peskinebf491972018-10-25 22:36:12 +02005342 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005343 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005344 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01005345 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02005346
Gilles Peskinebf491972018-10-25 22:36:12 +02005347 /* Test the actual capacity by reading the output. */
5348 while( actual_capacity > sizeof( output ) )
5349 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005350 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005351 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02005352 actual_capacity -= sizeof( output );
5353 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005354 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005355 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005356 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02005357 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02005358
Gilles Peskine59685592018-09-18 12:11:34 +02005359exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005360 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005361 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005362 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005363}
5364/* END_CASE */
5365
5366/* BEGIN_CASE */
5367void key_agreement_output( int alg_arg,
5368 int our_key_type_arg, data_t *our_key_data,
5369 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005370 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02005371{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005372 psa_key_handle_t our_key = 0;
Gilles Peskine59685592018-09-18 12:11:34 +02005373 psa_algorithm_t alg = alg_arg;
5374 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005375 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005376 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005377 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02005378
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005379 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
5380 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005381
Gilles Peskine8817f612018-12-18 00:18:46 +01005382 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005383
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005384 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
5385 psa_set_key_algorithm( &attributes, alg );
5386 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02005387 PSA_ASSERT( psa_import_key( &attributes,
5388 our_key_data->x, our_key_data->len,
5389 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02005390
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005391 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005392 PSA_ASSERT( psa_key_derivation_key_agreement(
5393 &operation,
5394 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
5395 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005396 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
5397 {
5398 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005399 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02005400 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02005401 NULL, 0 ) );
5402 }
Gilles Peskine59685592018-09-18 12:11:34 +02005403
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005404 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005405 actual_output,
5406 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005407 ASSERT_COMPARE( actual_output, expected_output1->len,
5408 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005409 if( expected_output2->len != 0 )
5410 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005411 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005412 actual_output,
5413 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005414 ASSERT_COMPARE( actual_output, expected_output2->len,
5415 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02005416 }
Gilles Peskine59685592018-09-18 12:11:34 +02005417
5418exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005419 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02005420 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005421 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02005422 mbedtls_free( actual_output );
5423}
5424/* END_CASE */
5425
5426/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02005427void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02005428{
Gilles Peskinea50d7392018-06-21 10:22:13 +02005429 size_t bytes = bytes_arg;
5430 const unsigned char trail[] = "don't overwrite me";
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005431 unsigned char *output = NULL;
5432 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02005433 size_t i;
5434 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02005435
Simon Butcher49f8e312020-03-03 15:51:50 +00005436 TEST_ASSERT( bytes_arg >= 0 );
5437
Gilles Peskine8cebbba2018-09-27 13:54:18 +02005438 ASSERT_ALLOC( output, bytes + sizeof( trail ) );
5439 ASSERT_ALLOC( changed, bytes );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005440 memcpy( output + bytes, trail, sizeof( trail ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005441
Gilles Peskine8817f612018-12-18 00:18:46 +01005442 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02005443
Gilles Peskinea50d7392018-06-21 10:22:13 +02005444 /* Run several times, to ensure that every output byte will be
5445 * nonzero at least once with overwhelming probability
5446 * (2^(-8*number_of_runs)). */
5447 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02005448 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02005449 if( bytes != 0 )
5450 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01005451 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005452
5453 /* Check that no more than bytes have been overwritten */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01005454 ASSERT_COMPARE( output + bytes, sizeof( trail ),
5455 trail, sizeof( trail ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005456
5457 for( i = 0; i < bytes; i++ )
5458 {
5459 if( output[i] != 0 )
5460 ++changed[i];
5461 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005462 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02005463
5464 /* Check that every byte was changed to nonzero at least once. This
5465 * validates that psa_generate_random is overwriting every byte of
5466 * the output buffer. */
5467 for( i = 0; i < bytes; i++ )
5468 {
5469 TEST_ASSERT( changed[i] != 0 );
5470 }
Gilles Peskine05d69892018-06-19 22:00:52 +02005471
5472exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005473 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02005474 mbedtls_free( output );
5475 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02005476}
5477/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02005478
5479/* BEGIN_CASE */
5480void generate_key( int type_arg,
5481 int bits_arg,
5482 int usage_arg,
5483 int alg_arg,
5484 int expected_status_arg )
5485{
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005486 psa_key_handle_t handle = 0;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005487 psa_key_type_t type = type_arg;
5488 psa_key_usage_t usage = usage_arg;
5489 size_t bits = bits_arg;
5490 psa_algorithm_t alg = alg_arg;
5491 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005492 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005493 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005494
Gilles Peskine8817f612018-12-18 00:18:46 +01005495 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005496
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005497 psa_set_key_usage_flags( &attributes, usage );
5498 psa_set_key_algorithm( &attributes, alg );
5499 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005500 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005501
5502 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005503 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005504 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005505 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005506
5507 /* Test the key information */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02005508 PSA_ASSERT( psa_get_key_attributes( handle, &got_attributes ) );
5509 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
5510 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005511
Gilles Peskine818ca122018-06-20 18:16:48 +02005512 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005513 if( ! exercise_key( handle, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02005514 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02005515
5516exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005517 psa_reset_key_attributes( &got_attributes );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005518 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005519 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005520}
5521/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03005522
Gilles Peskinee56e8782019-04-26 17:34:02 +02005523/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_GENPRIME:MBEDTLS_PKCS1_V15 */
5524void generate_key_rsa( int bits_arg,
5525 data_t *e_arg,
5526 int expected_status_arg )
5527{
5528 psa_key_handle_t handle = 0;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005529 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005530 size_t bits = bits_arg;
5531 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
5532 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
5533 psa_status_t expected_status = expected_status_arg;
5534 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
5535 uint8_t *exported = NULL;
5536 size_t exported_size =
5537 PSA_KEY_EXPORT_MAX_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
5538 size_t exported_length = SIZE_MAX;
5539 uint8_t *e_read_buffer = NULL;
5540 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02005541 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005542 size_t e_read_length = SIZE_MAX;
5543
5544 if( e_arg->len == 0 ||
5545 ( e_arg->len == 3 &&
5546 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
5547 {
5548 is_default_public_exponent = 1;
5549 e_read_size = 0;
5550 }
5551 ASSERT_ALLOC( e_read_buffer, e_read_size );
5552 ASSERT_ALLOC( exported, exported_size );
5553
5554 PSA_ASSERT( psa_crypto_init( ) );
5555
5556 psa_set_key_usage_flags( &attributes, usage );
5557 psa_set_key_algorithm( &attributes, alg );
5558 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5559 e_arg->x, e_arg->len ) );
5560 psa_set_key_bits( &attributes, bits );
5561
5562 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005563 TEST_EQUAL( psa_generate_key( &attributes, &handle ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005564 if( expected_status != PSA_SUCCESS )
5565 goto exit;
5566
5567 /* Test the key information */
5568 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
5569 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5570 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5571 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5572 e_read_buffer, e_read_size,
5573 &e_read_length ) );
5574 if( is_default_public_exponent )
5575 TEST_EQUAL( e_read_length, 0 );
5576 else
5577 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5578
5579 /* Do something with the key according to its type and permitted usage. */
5580 if( ! exercise_key( handle, usage, alg ) )
5581 goto exit;
5582
5583 /* Export the key and check the public exponent. */
5584 PSA_ASSERT( psa_export_public_key( handle,
5585 exported, exported_size,
5586 &exported_length ) );
5587 {
5588 uint8_t *p = exported;
5589 uint8_t *end = exported + exported_length;
5590 size_t len;
5591 /* RSAPublicKey ::= SEQUENCE {
5592 * modulus INTEGER, -- n
5593 * publicExponent INTEGER } -- e
5594 */
5595 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005596 MBEDTLS_ASN1_SEQUENCE |
5597 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005598 TEST_ASSERT( asn1_skip_integer( &p, end, bits, bits, 1 ) );
5599 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5600 MBEDTLS_ASN1_INTEGER ) );
5601 if( len >= 1 && p[0] == 0 )
5602 {
5603 ++p;
5604 --len;
5605 }
5606 if( e_arg->len == 0 )
5607 {
5608 TEST_EQUAL( len, 3 );
5609 TEST_EQUAL( p[0], 1 );
5610 TEST_EQUAL( p[1], 0 );
5611 TEST_EQUAL( p[2], 1 );
5612 }
5613 else
5614 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5615 }
5616
5617exit:
5618 psa_reset_key_attributes( &attributes );
5619 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005620 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005621 mbedtls_free( e_read_buffer );
5622 mbedtls_free( exported );
5623}
5624/* END_CASE */
5625
Darryl Greend49a4992018-06-18 17:27:26 +01005626/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005627void persistent_key_load_key_from_storage( data_t *data,
5628 int type_arg, int bits_arg,
5629 int usage_flags_arg, int alg_arg,
5630 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005631{
Ronald Cron71016a92020-08-28 19:01:50 +02005632 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005633 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005634 psa_key_handle_t handle = 0;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005635 psa_key_handle_t base_key = 0;
5636 psa_key_type_t type = type_arg;
5637 size_t bits = bits_arg;
5638 psa_key_usage_t usage_flags = usage_flags_arg;
5639 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005640 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005641 unsigned char *first_export = NULL;
5642 unsigned char *second_export = NULL;
5643 size_t export_size = PSA_KEY_EXPORT_MAX_SIZE( type, bits );
5644 size_t first_exported_length;
5645 size_t second_exported_length;
5646
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005647 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5648 {
5649 ASSERT_ALLOC( first_export, export_size );
5650 ASSERT_ALLOC( second_export, export_size );
5651 }
Darryl Greend49a4992018-06-18 17:27:26 +01005652
Gilles Peskine8817f612018-12-18 00:18:46 +01005653 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005654
Gilles Peskinec87af662019-05-15 16:12:22 +02005655 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005656 psa_set_key_usage_flags( &attributes, usage_flags );
5657 psa_set_key_algorithm( &attributes, alg );
5658 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005659 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005660
Darryl Green0c6575a2018-11-07 16:05:30 +00005661 switch( generation_method )
5662 {
5663 case IMPORT_KEY:
5664 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005665 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
5666 &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005667 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005668
Darryl Green0c6575a2018-11-07 16:05:30 +00005669 case GENERATE_KEY:
5670 /* Generate a key */
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005671 PSA_ASSERT( psa_generate_key( &attributes, &handle ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005672 break;
5673
5674 case DERIVE_KEY:
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005675 {
5676 /* Create base key */
5677 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5678 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5679 psa_set_key_usage_flags( &base_attributes,
5680 PSA_KEY_USAGE_DERIVE );
5681 psa_set_key_algorithm( &base_attributes, derive_alg );
5682 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005683 PSA_ASSERT( psa_import_key( &base_attributes,
5684 data->x, data->len,
5685 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005686 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005687 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005688 PSA_ASSERT( psa_key_derivation_input_key(
5689 &operation,
5690 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005691 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005692 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005693 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005694 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5695 &operation,
5696 &handle ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005697 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005698 PSA_ASSERT( psa_destroy_key( base_key ) );
5699 base_key = 0;
5700 }
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005701 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005702 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005703 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005704
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005705 /* Export the key if permitted by the key policy. */
5706 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5707 {
5708 PSA_ASSERT( psa_export_key( handle,
5709 first_export, export_size,
5710 &first_exported_length ) );
5711 if( generation_method == IMPORT_KEY )
5712 ASSERT_COMPARE( data->x, data->len,
5713 first_export, first_exported_length );
5714 }
Darryl Greend49a4992018-06-18 17:27:26 +01005715
5716 /* Shutdown and restart */
Gilles Peskine76b29a72019-05-28 14:08:50 +02005717 PSA_ASSERT( psa_close_key( handle ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005718 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005719 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005720
Darryl Greend49a4992018-06-18 17:27:26 +01005721 /* Check key slot still contains key data */
Gilles Peskine225010f2019-05-06 18:44:55 +02005722 PSA_ASSERT( psa_open_key( key_id, &handle ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005723 PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005724 TEST_ASSERT( mbedtls_svc_key_id_equal(
5725 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005726 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5727 PSA_KEY_LIFETIME_PERSISTENT );
5728 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5729 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5730 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), usage_flags );
5731 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005732
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005733 /* Export the key again if permitted by the key policy. */
5734 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005735 {
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005736 PSA_ASSERT( psa_export_key( handle,
5737 second_export, export_size,
5738 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005739 ASSERT_COMPARE( first_export, first_exported_length,
5740 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005741 }
5742
5743 /* Do something with the key according to its type and permitted usage. */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005744 if( ! exercise_key( handle, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005745 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005746
5747exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005748 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005749 mbedtls_free( first_export );
5750 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005751 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005752 psa_destroy_key( base_key );
5753 if( handle == 0 )
5754 {
5755 /* In case there was a test failure after creating the persistent key
5756 * but while it was not open, try to re-open the persistent key
5757 * to delete it. */
Gilles Peskinee92c68a2020-08-26 00:06:25 +02005758 (void) psa_open_key( key_id, &handle );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005759 }
Gilles Peskinebdf309c2018-12-03 15:36:32 +01005760 psa_destroy_key( handle );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005761 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005762}
5763/* END_CASE */