blob: 3e193c5302854afeb4d17286b78fc0c6985d32d5 [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
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020012#include "psa/crypto.h"
Ronald Cron41841072020-09-17 15:28:26 +020013#include "psa_crypto_slot_management.h"
Gilles Peskinebdc96fd2019-08-07 12:08:04 +020014
Gilles Peskine8e94efe2021-02-13 00:25:53 +010015#include "test/asn1_helpers.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010016#include "test/psa_crypto_helpers.h"
Gilles Peskinee78b0022021-02-13 00:41:11 +010017#include "test/psa_exercise_key.h"
Ronald Cron28a45ed2021-02-09 20:35:42 +010018
Gilles Peskine4023c012021-05-27 13:21:20 +020019/* If this comes up, it's a bug in the test code or in the test data. */
20#define UNUSED 0xdeadbeef
21
Dave Rodgman647791d2021-06-23 12:49:59 +010022/* Assert that an operation is (not) active.
23 * This serves as a proxy for checking if the operation is aborted. */
24#define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 )
25#define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 )
26
Jaeden Amerof24c7f82018-06-27 17:20:43 +010027/** An invalid export length that will never be set by psa_export_key(). */
28static const size_t INVALID_EXPORT_LENGTH = ~0U;
29
Gilles Peskinea7aa4422018-08-14 15:17:54 +020030/** Test if a buffer contains a constant byte value.
31 *
32 * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020033 *
34 * \param buffer Pointer to the beginning of the buffer.
Gilles Peskinea7aa4422018-08-14 15:17:54 +020035 * \param c Expected value of every byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020036 * \param size Size of the buffer in bytes.
37 *
Gilles Peskine3f669c32018-06-21 09:21:51 +020038 * \return 1 if the buffer is all-bits-zero.
39 * \return 0 if there is at least one nonzero byte.
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020040 */
Gilles Peskinea7aa4422018-08-14 15:17:54 +020041static int mem_is_char( void *buffer, unsigned char c, size_t size )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020042{
43 size_t i;
44 for( i = 0; i < size; i++ )
45 {
Gilles Peskinea7aa4422018-08-14 15:17:54 +020046 if( ( (unsigned char *) buffer )[i] != c )
Gilles Peskine3f669c32018-06-21 09:21:51 +020047 return( 0 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020048 }
Gilles Peskine3f669c32018-06-21 09:21:51 +020049 return( 1 );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +020050}
Gilles Peskine818ca122018-06-20 18:16:48 +020051
Gilles Peskine0b352bc2018-06-28 00:16:11 +020052/* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */
53static int asn1_write_10x( unsigned char **p,
54 unsigned char *start,
55 size_t bits,
56 unsigned char x )
57{
58 int ret;
59 int len = bits / 8 + 1;
Gilles Peskine480416a2018-06-28 19:04:07 +020060 if( bits == 0 )
61 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
62 if( bits <= 8 && x >= 1 << ( bits - 1 ) )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020063 return( MBEDTLS_ERR_ASN1_INVALID_DATA );
Moran Pekercb088e72018-07-17 17:36:59 +030064 if( *p < start || *p - start < (ptrdiff_t) len )
Gilles Peskine0b352bc2018-06-28 00:16:11 +020065 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
66 *p -= len;
67 ( *p )[len-1] = x;
68 if( bits % 8 == 0 )
69 ( *p )[1] |= 1;
70 else
71 ( *p )[0] |= 1 << ( bits % 8 );
72 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
73 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
74 MBEDTLS_ASN1_INTEGER ) );
75 return( len );
76}
77
78static int construct_fake_rsa_key( unsigned char *buffer,
79 size_t buffer_size,
80 unsigned char **p,
81 size_t bits,
82 int keypair )
83{
84 size_t half_bits = ( bits + 1 ) / 2;
85 int ret;
86 int len = 0;
87 /* Construct something that looks like a DER encoding of
88 * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2:
89 * RSAPrivateKey ::= SEQUENCE {
90 * version Version,
91 * modulus INTEGER, -- n
92 * publicExponent INTEGER, -- e
93 * privateExponent INTEGER, -- d
94 * prime1 INTEGER, -- p
95 * prime2 INTEGER, -- q
96 * exponent1 INTEGER, -- d mod (p-1)
97 * exponent2 INTEGER, -- d mod (q-1)
98 * coefficient INTEGER, -- (inverse of q) mod p
99 * otherPrimeInfos OtherPrimeInfos OPTIONAL
100 * }
101 * Or, for a public key, the same structure with only
102 * version, modulus and publicExponent.
103 */
104 *p = buffer + buffer_size;
105 if( keypair )
106 {
107 MBEDTLS_ASN1_CHK_ADD( len, /* pq */
108 asn1_write_10x( p, buffer, half_bits, 1 ) );
109 MBEDTLS_ASN1_CHK_ADD( len, /* dq */
110 asn1_write_10x( p, buffer, half_bits, 1 ) );
111 MBEDTLS_ASN1_CHK_ADD( len, /* dp */
112 asn1_write_10x( p, buffer, half_bits, 1 ) );
113 MBEDTLS_ASN1_CHK_ADD( len, /* q */
114 asn1_write_10x( p, buffer, half_bits, 1 ) );
115 MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */
116 asn1_write_10x( p, buffer, half_bits, 3 ) );
117 MBEDTLS_ASN1_CHK_ADD( len, /* d */
118 asn1_write_10x( p, buffer, bits, 1 ) );
119 }
120 MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */
121 asn1_write_10x( p, buffer, 17, 1 ) );
122 MBEDTLS_ASN1_CHK_ADD( len, /* n */
123 asn1_write_10x( p, buffer, bits, 1 ) );
124 if( keypair )
125 MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */
126 mbedtls_asn1_write_int( p, buffer, 0 ) );
127 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) );
128 {
129 const unsigned char tag =
130 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE;
131 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) );
132 }
133 return( len );
134}
135
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100136int exercise_mac_setup( psa_key_type_t key_type,
137 const unsigned char *key_bytes,
138 size_t key_length,
139 psa_algorithm_t alg,
140 psa_mac_operation_t *operation,
141 psa_status_t *status )
142{
Ronald Cron5425a212020-08-04 14:58:35 +0200143 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200144 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100145
Gilles Peskine89d8c5c2019-11-26 17:01:59 +0100146 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200147 psa_set_key_algorithm( &attributes, alg );
148 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200149 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100150
Ronald Cron5425a212020-08-04 14:58:35 +0200151 *status = psa_mac_sign_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100152 /* Whether setup succeeded or failed, abort must succeed. */
153 PSA_ASSERT( psa_mac_abort( operation ) );
154 /* If setup failed, reproduce the failure, so that the caller can
155 * test the resulting state of the operation object. */
156 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100157 {
Ronald Cron5425a212020-08-04 14:58:35 +0200158 TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100159 }
160
Ronald Cron5425a212020-08-04 14:58:35 +0200161 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100162 return( 1 );
163
164exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200165 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100166 return( 0 );
167}
168
169int exercise_cipher_setup( psa_key_type_t key_type,
170 const unsigned char *key_bytes,
171 size_t key_length,
172 psa_algorithm_t alg,
173 psa_cipher_operation_t *operation,
174 psa_status_t *status )
175{
Ronald Cron5425a212020-08-04 14:58:35 +0200176 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200177 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100178
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200179 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
180 psa_set_key_algorithm( &attributes, alg );
181 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +0200182 PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100183
Ronald Cron5425a212020-08-04 14:58:35 +0200184 *status = psa_cipher_encrypt_setup( operation, key, alg );
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100185 /* Whether setup succeeded or failed, abort must succeed. */
186 PSA_ASSERT( psa_cipher_abort( operation ) );
187 /* If setup failed, reproduce the failure, so that the caller can
188 * test the resulting state of the operation object. */
189 if( *status != PSA_SUCCESS )
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100190 {
Ronald Cron5425a212020-08-04 14:58:35 +0200191 TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ),
Gilles Peskine9e0a4a52019-02-25 22:11:18 +0100192 *status );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100193 }
194
Ronald Cron5425a212020-08-04 14:58:35 +0200195 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100196 return( 1 );
197
198exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200199 psa_destroy_key( key );
Gilles Peskinef426e0f2019-02-25 17:42:03 +0100200 return( 0 );
201}
202
Ronald Cron5425a212020-08-04 14:58:35 +0200203static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key )
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200204{
205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cronecfb2372020-07-23 17:13:42 +0200206 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200207 uint8_t buffer[1];
208 size_t length;
209 int ok = 0;
210
Ronald Cronecfb2372020-07-23 17:13:42 +0200211 psa_set_key_id( &attributes, key_id );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200212 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
213 psa_set_key_algorithm( &attributes, PSA_ALG_CTR );
214 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Ronald Cron5425a212020-08-04 14:58:35 +0200215 TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000216 PSA_ERROR_INVALID_HANDLE );
Ronald Cronecfb2372020-07-23 17:13:42 +0200217 TEST_EQUAL(
218 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 );
219 TEST_EQUAL(
220 MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +0200221 TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200222 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
223 TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
224 TEST_EQUAL( psa_get_key_type( &attributes ), 0 );
225 TEST_EQUAL( psa_get_key_bits( &attributes ), 0 );
226
Ronald Cron5425a212020-08-04 14:58:35 +0200227 TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000228 PSA_ERROR_INVALID_HANDLE );
Ronald Cron5425a212020-08-04 14:58:35 +0200229 TEST_EQUAL( psa_export_public_key( key,
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200230 buffer, sizeof( buffer ), &length ),
Maulik Patel3240c9d2021-03-17 16:11:05 +0000231 PSA_ERROR_INVALID_HANDLE );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200232
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200233 ok = 1;
234
235exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100236 /*
237 * Key attributes may have been returned by psa_get_key_attributes()
238 * thus reset them as required.
239 */
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200240 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100241
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200242 return( ok );
243}
244
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200245/* Assert that a key isn't reported as having a slot number. */
246#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
247#define ASSERT_NO_SLOT_NUMBER( attributes ) \
248 do \
249 { \
250 psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \
251 TEST_EQUAL( psa_get_key_slot_number( \
252 attributes, \
253 &ASSERT_NO_SLOT_NUMBER_slot_number ), \
254 PSA_ERROR_INVALID_ARGUMENT ); \
255 } \
256 while( 0 )
257#else /* MBEDTLS_PSA_CRYPTO_SE_C */
258#define ASSERT_NO_SLOT_NUMBER( attributes ) \
259 ( (void) 0 )
260#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
261
Gilles Peskinebdf309c2018-12-03 15:36:32 +0100262/* An overapproximation of the amount of storage needed for a key of the
263 * given type and with the given content. The API doesn't make it easy
264 * to find a good value for the size. The current implementation doesn't
265 * care about the value anyway. */
266#define KEY_BITS_FROM_DATA( type, data ) \
267 ( data )->len
268
Darryl Green0c6575a2018-11-07 16:05:30 +0000269typedef enum {
270 IMPORT_KEY = 0,
271 GENERATE_KEY = 1,
272 DERIVE_KEY = 2
273} generate_method;
274
Gilles Peskinee59236f2018-01-27 23:32:46 +0100275/* END_HEADER */
276
277/* BEGIN_DEPENDENCIES
278 * depends_on:MBEDTLS_PSA_CRYPTO_C
279 * END_DEPENDENCIES
280 */
281
282/* BEGIN_CASE */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200283void static_checks( )
284{
285 size_t max_truncated_mac_size =
286 PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET;
287
288 /* Check that the length for a truncated MAC always fits in the algorithm
289 * encoding. The shifted mask is the maximum truncated value. The
290 * untruncated algorithm may be one byte larger. */
291 TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size );
292}
293/* END_CASE */
294
295/* BEGIN_CASE */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200296void import_with_policy( int type_arg,
297 int usage_arg, int alg_arg,
298 int expected_status_arg )
299{
300 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
301 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200302 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine6edfa292019-07-31 15:53:45 +0200303 psa_key_type_t type = type_arg;
304 psa_key_usage_t usage = usage_arg;
305 psa_algorithm_t alg = alg_arg;
306 psa_status_t expected_status = expected_status_arg;
307 const uint8_t key_material[16] = {0};
308 psa_status_t status;
309
310 PSA_ASSERT( psa_crypto_init( ) );
311
312 psa_set_key_type( &attributes, type );
313 psa_set_key_usage_flags( &attributes, usage );
314 psa_set_key_algorithm( &attributes, alg );
315
316 status = psa_import_key( &attributes,
317 key_material, sizeof( key_material ),
Ronald Cron5425a212020-08-04 14:58:35 +0200318 &key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200319 TEST_EQUAL( status, expected_status );
320 if( status != PSA_SUCCESS )
321 goto exit;
322
Ronald Cron5425a212020-08-04 14:58:35 +0200323 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200324 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +0200325 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ),
326 update_key_usage_flags( usage ) );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200327 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200328 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200329
Ronald Cron5425a212020-08-04 14:58:35 +0200330 PSA_ASSERT( psa_destroy_key( key ) );
331 test_operations_on_invalid_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200332
333exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100334 /*
335 * Key attributes may have been returned by psa_get_key_attributes()
336 * thus reset them as required.
337 */
Gilles Peskine6edfa292019-07-31 15:53:45 +0200338 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100339
340 psa_destroy_key( key );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200341 PSA_DONE( );
342}
343/* END_CASE */
344
345/* BEGIN_CASE */
346void import_with_data( data_t *data, int type_arg,
347 int attr_bits_arg,
348 int expected_status_arg )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200349{
350 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
351 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +0200352 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200353 psa_key_type_t type = type_arg;
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200354 size_t attr_bits = attr_bits_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200355 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100356 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100357
Gilles Peskine8817f612018-12-18 00:18:46 +0100358 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100359
Gilles Peskine4747d192019-04-17 15:05:45 +0200360 psa_set_key_type( &attributes, type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200361 psa_set_key_bits( &attributes, attr_bits );
Gilles Peskine6edfa292019-07-31 15:53:45 +0200362
Ronald Cron5425a212020-08-04 14:58:35 +0200363 status = psa_import_key( &attributes, data->x, data->len, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100364 TEST_EQUAL( status, expected_status );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200365 if( status != PSA_SUCCESS )
366 goto exit;
367
Ronald Cron5425a212020-08-04 14:58:35 +0200368 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200369 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
Gilles Peskine8fb3a9e2019-05-03 16:59:21 +0200370 if( attr_bits != 0 )
Gilles Peskine7e0cff92019-07-30 13:48:52 +0200371 TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200372 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200373
Ronald Cron5425a212020-08-04 14:58:35 +0200374 PSA_ASSERT( psa_destroy_key( key ) );
375 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100376
377exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100378 /*
379 * Key attributes may have been returned by psa_get_key_attributes()
380 * thus reset them as required.
381 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200382 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100383
384 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200385 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100386}
387/* END_CASE */
388
389/* BEGIN_CASE */
Gilles Peskinec744d992019-07-30 17:26:54 +0200390void import_large_key( int type_arg, int byte_size_arg,
391 int expected_status_arg )
392{
393 psa_key_type_t type = type_arg;
394 size_t byte_size = byte_size_arg;
395 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
396 psa_status_t expected_status = expected_status_arg;
Ronald Cron5425a212020-08-04 14:58:35 +0200397 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +0200398 psa_status_t status;
399 uint8_t *buffer = NULL;
400 size_t buffer_size = byte_size + 1;
401 size_t n;
402
Steven Cooreman69967ce2021-01-18 18:01:08 +0100403 /* Skip the test case if the target running the test cannot
404 * accomodate large keys due to heap size constraints */
405 ASSERT_ALLOC_WEAK( buffer, buffer_size );
Gilles Peskinec744d992019-07-30 17:26:54 +0200406 memset( buffer, 'K', byte_size );
407
408 PSA_ASSERT( psa_crypto_init( ) );
409
410 /* Try importing the key */
411 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
412 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200413 status = psa_import_key( &attributes, buffer, byte_size, &key );
Steven Cooreman83fdb702021-01-21 14:24:39 +0100414 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinec744d992019-07-30 17:26:54 +0200415 TEST_EQUAL( status, expected_status );
416
417 if( status == PSA_SUCCESS )
418 {
Ronald Cron5425a212020-08-04 14:58:35 +0200419 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200420 TEST_EQUAL( psa_get_key_type( &attributes ), type );
421 TEST_EQUAL( psa_get_key_bits( &attributes ),
422 PSA_BYTES_TO_BITS( byte_size ) );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200423 ASSERT_NO_SLOT_NUMBER( &attributes );
Gilles Peskinec744d992019-07-30 17:26:54 +0200424 memset( buffer, 0, byte_size + 1 );
Ronald Cron5425a212020-08-04 14:58:35 +0200425 PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) );
Gilles Peskinec744d992019-07-30 17:26:54 +0200426 for( n = 0; n < byte_size; n++ )
427 TEST_EQUAL( buffer[n], 'K' );
428 for( n = byte_size; n < buffer_size; n++ )
429 TEST_EQUAL( buffer[n], 0 );
430 }
431
432exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100433 /*
434 * Key attributes may have been returned by psa_get_key_attributes()
435 * thus reset them as required.
436 */
437 psa_reset_key_attributes( &attributes );
438
Ronald Cron5425a212020-08-04 14:58:35 +0200439 psa_destroy_key( key );
Gilles Peskinec744d992019-07-30 17:26:54 +0200440 PSA_DONE( );
441 mbedtls_free( buffer );
442}
443/* END_CASE */
444
445/* BEGIN_CASE */
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200446void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg )
447{
Ronald Cron5425a212020-08-04 14:58:35 +0200448 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200449 size_t bits = bits_arg;
450 psa_status_t expected_status = expected_status_arg;
451 psa_status_t status;
452 psa_key_type_t type =
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200453 keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200454 size_t buffer_size = /* Slight overapproximations */
455 keypair ? bits * 9 / 16 + 80 : bits / 8 + 20;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200456 unsigned char *buffer = NULL;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200457 unsigned char *p;
458 int ret;
459 size_t length;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200461
Gilles Peskine8817f612018-12-18 00:18:46 +0100462 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200463 ASSERT_ALLOC( buffer, buffer_size );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200464
465 TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p,
466 bits, keypair ) ) >= 0 );
467 length = ret;
468
469 /* Try importing the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200470 psa_set_key_type( &attributes, type );
Ronald Cron5425a212020-08-04 14:58:35 +0200471 status = psa_import_key( &attributes, p, length, &key );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100472 TEST_EQUAL( status, expected_status );
Gilles Peskine76b29a72019-05-28 14:08:50 +0200473
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200474 if( status == PSA_SUCCESS )
Ronald Cron5425a212020-08-04 14:58:35 +0200475 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200476
477exit:
478 mbedtls_free( buffer );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200479 PSA_DONE( );
Gilles Peskine0b352bc2018-06-28 00:16:11 +0200480}
481/* END_CASE */
482
483/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300484void import_export( data_t *data,
Moran Pekera964a8f2018-06-04 18:42:36 +0300485 int type_arg,
Gilles Peskine1ecf92c22019-05-24 15:00:06 +0200486 int usage_arg, int alg_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100487 int expected_bits,
488 int export_size_delta,
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200489 int expected_export_status_arg,
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100490 int canonical_input )
491{
Ronald Cron5425a212020-08-04 14:58:35 +0200492 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100493 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200494 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200495 psa_status_t expected_export_status = expected_export_status_arg;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100496 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100497 unsigned char *exported = NULL;
498 unsigned char *reexported = NULL;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100499 size_t export_size;
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100500 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100501 size_t reexported_length;
Gilles Peskine4747d192019-04-17 15:05:45 +0200502 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200503 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100504
Moran Pekercb088e72018-07-17 17:36:59 +0300505 export_size = (ptrdiff_t) data->len + export_size_delta;
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200506 ASSERT_ALLOC( exported, export_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100507 if( ! canonical_input )
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200508 ASSERT_ALLOC( reexported, export_size );
Gilles Peskine8817f612018-12-18 00:18:46 +0100509 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100510
Gilles Peskine4747d192019-04-17 15:05:45 +0200511 psa_set_key_usage_flags( &attributes, usage_arg );
512 psa_set_key_algorithm( &attributes, alg );
513 psa_set_key_type( &attributes, type );
mohammad1603a97cb8c2018-03-28 03:46:26 -0700514
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100515 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200516 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100517
518 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200519 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200520 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
521 TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits );
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200522 ASSERT_NO_SLOT_NUMBER( &got_attributes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100523
524 /* Export the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200525 status = psa_export_key( key, exported, export_size, &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100526 TEST_EQUAL( status, expected_export_status );
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100527
528 /* The exported length must be set by psa_export_key() to a value between 0
529 * and export_size. On errors, the exported length must be 0. */
530 TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH );
531 TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 );
532 TEST_ASSERT( exported_length <= export_size );
533
Gilles Peskinea7aa4422018-08-14 15:17:54 +0200534 TEST_ASSERT( mem_is_char( exported + exported_length, 0,
Gilles Peskine3f669c32018-06-21 09:21:51 +0200535 export_size - exported_length ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100536 if( status != PSA_SUCCESS )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200537 {
Gilles Peskinefe11b722018-12-18 00:24:04 +0100538 TEST_EQUAL( exported_length, 0 );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100539 goto destroy;
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200540 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100541
Gilles Peskineea38a922021-02-13 00:05:16 +0100542 /* Run sanity checks on the exported key. For non-canonical inputs,
543 * this validates the canonical representations. For canonical inputs,
544 * this doesn't directly validate the implementation, but it still helps
545 * by cross-validating the test data with the sanity check code. */
546 if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) )
Gilles Peskine8f609232018-08-11 01:24:55 +0200547 goto exit;
548
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100549 if( canonical_input )
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200550 ASSERT_COMPARE( data->x, data->len, exported, exported_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100551 else
552 {
Ronald Cron5425a212020-08-04 14:58:35 +0200553 mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine049c7532019-05-15 20:22:09 +0200554 PSA_ASSERT( psa_import_key( &attributes, exported, exported_length,
Ronald Cron5425a212020-08-04 14:58:35 +0200555 &key2 ) );
556 PSA_ASSERT( psa_export_key( key2,
Gilles Peskine8817f612018-12-18 00:18:46 +0100557 reexported,
558 export_size,
559 &reexported_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +0200560 ASSERT_COMPARE( exported, exported_length,
561 reexported, reexported_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200562 PSA_ASSERT( psa_destroy_key( key2 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100563 }
gabor-mezei-armceface22021-01-21 12:26:17 +0100564 TEST_ASSERT( exported_length <=
565 PSA_EXPORT_KEY_OUTPUT_SIZE( type,
566 psa_get_key_bits( &got_attributes ) ) );
567 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100568
569destroy:
570 /* Destroy the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200571 PSA_ASSERT( psa_destroy_key( key ) );
572 test_operations_on_invalid_key( key );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573
574exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100575 /*
576 * Key attributes may have been returned by psa_get_key_attributes()
577 * thus reset them as required.
578 */
579 psa_reset_key_attributes( &got_attributes );
580
itayzafrir3e02b3b2018-06-12 17:06:52 +0300581 mbedtls_free( exported );
582 mbedtls_free( reexported );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200583 PSA_DONE( );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100584}
585/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100586
Moran Pekerf709f4a2018-06-06 17:26:04 +0300587/* BEGIN_CASE */
itayzafrir3e02b3b2018-06-12 17:06:52 +0300588void import_export_public_key( data_t *data,
Gilles Peskine2d277862018-06-18 15:41:12 +0200589 int type_arg,
590 int alg_arg,
Gilles Peskine49c25912018-10-29 15:15:31 +0100591 int export_size_delta,
592 int expected_export_status_arg,
593 data_t *expected_public_key )
Moran Pekerf709f4a2018-06-06 17:26:04 +0300594{
Ronald Cron5425a212020-08-04 14:58:35 +0200595 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300596 psa_key_type_t type = type_arg;
Gilles Peskine4abf7412018-06-18 16:35:34 +0200597 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +0200598 psa_status_t expected_export_status = expected_export_status_arg;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300599 psa_status_t status;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300600 unsigned char *exported = NULL;
Gilles Peskine49c25912018-10-29 15:15:31 +0100601 size_t export_size = expected_public_key->len + export_size_delta;
Jaeden Amero2a671e92018-06-27 17:47:40 +0100602 size_t exported_length = INVALID_EXPORT_LENGTH;
Gilles Peskine4747d192019-04-17 15:05:45 +0200603 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerf709f4a2018-06-06 17:26:04 +0300604
Gilles Peskine8817f612018-12-18 00:18:46 +0100605 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300606
Gilles Peskine4747d192019-04-17 15:05:45 +0200607 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
608 psa_set_key_algorithm( &attributes, alg );
609 psa_set_key_type( &attributes, type );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300610
611 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200612 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300613
Gilles Peskine49c25912018-10-29 15:15:31 +0100614 /* Export the public key */
615 ASSERT_ALLOC( exported, export_size );
Ronald Cron5425a212020-08-04 14:58:35 +0200616 status = psa_export_public_key( key,
Gilles Peskine2d277862018-06-18 15:41:12 +0200617 exported, export_size,
618 &exported_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +0100619 TEST_EQUAL( status, expected_export_status );
Gilles Peskine49c25912018-10-29 15:15:31 +0100620 if( status == PSA_SUCCESS )
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100621 {
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200622 psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100623 size_t bits;
Ronald Cron5425a212020-08-04 14:58:35 +0200624 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200625 bits = psa_get_key_bits( &attributes );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100626 TEST_ASSERT( expected_public_key->len <=
gabor-mezei-armcbcec212020-12-18 14:23:51 +0100627 PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100628 TEST_ASSERT( expected_public_key->len <=
629 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) );
630 TEST_ASSERT( expected_public_key->len <=
631 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskine49c25912018-10-29 15:15:31 +0100632 ASSERT_COMPARE( expected_public_key->x, expected_public_key->len,
633 exported, exported_length );
Gilles Peskined8b7d4f2018-10-29 15:18:41 +0100634 }
Moran Pekerf709f4a2018-06-06 17:26:04 +0300635
636exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100637 /*
638 * Key attributes may have been returned by psa_get_key_attributes()
639 * thus reset them as required.
640 */
641 psa_reset_key_attributes( &attributes );
642
itayzafrir3e02b3b2018-06-12 17:06:52 +0300643 mbedtls_free( exported );
Ronald Cron5425a212020-08-04 14:58:35 +0200644 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200645 PSA_DONE( );
Moran Pekerf709f4a2018-06-06 17:26:04 +0300646}
647/* END_CASE */
648
Gilles Peskine20035e32018-02-03 22:44:14 +0100649/* BEGIN_CASE */
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200650void import_and_exercise_key( data_t *data,
651 int type_arg,
652 int bits_arg,
653 int alg_arg )
654{
Ronald Cron5425a212020-08-04 14:58:35 +0200655 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200656 psa_key_type_t type = type_arg;
657 size_t bits = bits_arg;
658 psa_algorithm_t alg = alg_arg;
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100659 psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg );
Gilles Peskine4747d192019-04-17 15:05:45 +0200660 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200661 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200662
Gilles Peskine8817f612018-12-18 00:18:46 +0100663 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200664
Gilles Peskine4747d192019-04-17 15:05:45 +0200665 psa_set_key_usage_flags( &attributes, usage );
666 psa_set_key_algorithm( &attributes, alg );
667 psa_set_key_type( &attributes, type );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200668
669 /* Import the key */
Ronald Cron5425a212020-08-04 14:58:35 +0200670 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200671
672 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +0200673 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200674 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
675 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200676
677 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +0100678 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +0200679 goto exit;
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200680
Ronald Cron5425a212020-08-04 14:58:35 +0200681 PSA_ASSERT( psa_destroy_key( key ) );
682 test_operations_on_invalid_key( key );
Gilles Peskine4cf3a432019-04-18 22:28:52 +0200683
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200684exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100685 /*
686 * Key attributes may have been returned by psa_get_key_attributes()
687 * thus reset them as required.
688 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200689 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100690
691 psa_reset_key_attributes( &attributes );
692 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200693 PSA_DONE( );
Gilles Peskinea680c7a2018-06-26 16:12:43 +0200694}
695/* END_CASE */
696
697/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100698void effective_key_attributes( int type_arg, int expected_type_arg,
699 int bits_arg, int expected_bits_arg,
700 int usage_arg, int expected_usage_arg,
701 int alg_arg, int expected_alg_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200702{
Ronald Cron5425a212020-08-04 14:58:35 +0200703 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a960492019-11-26 17:12:21 +0100704 psa_key_type_t key_type = type_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100705 psa_key_type_t expected_key_type = expected_type_arg;
Gilles Peskine1a960492019-11-26 17:12:21 +0100706 size_t bits = bits_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100707 size_t expected_bits = expected_bits_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200708 psa_algorithm_t alg = alg_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100709 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskined5b33222018-06-18 22:20:03 +0200710 psa_key_usage_t usage = usage_arg;
Gilles Peskine06c28892019-11-26 18:07:46 +0100711 psa_key_usage_t expected_usage = expected_usage_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200712 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined5b33222018-06-18 22:20:03 +0200713
Gilles Peskine8817f612018-12-18 00:18:46 +0100714 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200715
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200716 psa_set_key_usage_flags( &attributes, usage );
717 psa_set_key_algorithm( &attributes, alg );
718 psa_set_key_type( &attributes, key_type );
Gilles Peskine1a960492019-11-26 17:12:21 +0100719 psa_set_key_bits( &attributes, bits );
Gilles Peskined5b33222018-06-18 22:20:03 +0200720
Ronald Cron5425a212020-08-04 14:58:35 +0200721 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Gilles Peskine1a960492019-11-26 17:12:21 +0100722 psa_reset_key_attributes( &attributes );
Gilles Peskined5b33222018-06-18 22:20:03 +0200723
Ronald Cron5425a212020-08-04 14:58:35 +0200724 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine06c28892019-11-26 18:07:46 +0100725 TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type );
726 TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits );
727 TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage );
728 TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg );
Gilles Peskined5b33222018-06-18 22:20:03 +0200729
730exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100731 /*
732 * Key attributes may have been returned by psa_get_key_attributes()
733 * thus reset them as required.
734 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200735 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100736
737 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200738 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +0200739}
740/* END_CASE */
741
742/* BEGIN_CASE */
Gilles Peskine06c28892019-11-26 18:07:46 +0100743void check_key_policy( int type_arg, int bits_arg,
744 int usage_arg, int alg_arg )
745{
746 test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg,
747 usage_arg, usage_arg, alg_arg, alg_arg );
748 goto exit;
749}
750/* END_CASE */
751
752/* BEGIN_CASE */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200753void key_attributes_init( )
Jaeden Amero70261c52019-01-04 11:47:20 +0000754{
755 /* Test each valid way of initializing the object, except for `= {0}`, as
756 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
757 * though it's OK by the C standard. We could test for this, but we'd need
758 * to supress the Clang warning for the test. */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200759 psa_key_attributes_t func = psa_key_attributes_init( );
760 psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT;
761 psa_key_attributes_t zero;
Jaeden Amero70261c52019-01-04 11:47:20 +0000762
763 memset( &zero, 0, sizeof( zero ) );
764
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200765 TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE );
766 TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE );
767 TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +0000768
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200769 TEST_EQUAL( psa_get_key_type( &func ), 0 );
770 TEST_EQUAL( psa_get_key_type( &init ), 0 );
771 TEST_EQUAL( psa_get_key_type( &zero ), 0 );
772
773 TEST_EQUAL( psa_get_key_bits( &func ), 0 );
774 TEST_EQUAL( psa_get_key_bits( &init ), 0 );
775 TEST_EQUAL( psa_get_key_bits( &zero ), 0 );
776
777 TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 );
778 TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 );
779 TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 );
780
781 TEST_EQUAL( psa_get_key_algorithm( &func ), 0 );
782 TEST_EQUAL( psa_get_key_algorithm( &init ), 0 );
783 TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 );
Jaeden Amero70261c52019-01-04 11:47:20 +0000784}
785/* END_CASE */
786
787/* BEGIN_CASE */
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200788void mac_key_policy( int policy_usage,
789 int policy_alg,
790 int key_type,
791 data_t *key_data,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100792 int exercise_alg,
793 int expected_status_arg )
Gilles Peskined5b33222018-06-18 22:20:03 +0200794{
Ronald Cron5425a212020-08-04 14:58:35 +0200795 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200796 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero769ce272019-01-04 11:48:03 +0000797 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200798 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100799 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200800 unsigned char mac[PSA_MAC_MAX_SIZE];
Gilles Peskined5b33222018-06-18 22:20:03 +0200801
Gilles Peskine8817f612018-12-18 00:18:46 +0100802 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200803
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200804 psa_set_key_usage_flags( &attributes, policy_usage );
805 psa_set_key_algorithm( &attributes, policy_alg );
806 psa_set_key_type( &attributes, key_type );
Gilles Peskined5b33222018-06-18 22:20:03 +0200807
Gilles Peskine049c7532019-05-15 20:22:09 +0200808 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200809 &key ) );
Gilles Peskined5b33222018-06-18 22:20:03 +0200810
Ronald Cron5425a212020-08-04 14:58:35 +0200811 status = psa_mac_sign_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100812 if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100813 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100814 else
815 TEST_EQUAL( status, expected_status );
816
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200817 psa_mac_abort( &operation );
Gilles Peskined5b33222018-06-18 22:20:03 +0200818
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200819 memset( mac, 0, sizeof( mac ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200820 status = psa_mac_verify_setup( &operation, key, exercise_alg );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100821 if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100822 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100823 else
824 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200825
826exit:
827 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200828 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200829 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200830}
831/* END_CASE */
832
833/* BEGIN_CASE */
834void cipher_key_policy( int policy_usage,
835 int policy_alg,
836 int key_type,
837 data_t *key_data,
838 int exercise_alg )
839{
Ronald Cron5425a212020-08-04 14:58:35 +0200840 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200841 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero5bae2272019-01-04 11:48:27 +0000842 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200843 psa_status_t status;
844
Gilles Peskine8817f612018-12-18 00:18:46 +0100845 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200846
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200847 psa_set_key_usage_flags( &attributes, policy_usage );
848 psa_set_key_algorithm( &attributes, policy_alg );
849 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200850
Gilles Peskine049c7532019-05-15 20:22:09 +0200851 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200852 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200853
Ronald Cron5425a212020-08-04 14:58:35 +0200854 status = psa_cipher_encrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200855 if( policy_alg == exercise_alg &&
856 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100857 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200858 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100859 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200860 psa_cipher_abort( &operation );
861
Ronald Cron5425a212020-08-04 14:58:35 +0200862 status = psa_cipher_decrypt_setup( &operation, key, exercise_alg );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200863 if( policy_alg == exercise_alg &&
864 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100865 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200866 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100867 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200868
869exit:
870 psa_cipher_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +0200871 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200872 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200873}
874/* END_CASE */
875
876/* BEGIN_CASE */
877void aead_key_policy( int policy_usage,
878 int policy_alg,
879 int key_type,
880 data_t *key_data,
881 int nonce_length_arg,
882 int tag_length_arg,
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100883 int exercise_alg,
884 int expected_status_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200885{
Ronald Cron5425a212020-08-04 14:58:35 +0200886 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200887 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200888 psa_status_t status;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100889 psa_status_t expected_status = expected_status_arg;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200890 unsigned char nonce[16] = {0};
891 size_t nonce_length = nonce_length_arg;
892 unsigned char tag[16];
893 size_t tag_length = tag_length_arg;
894 size_t output_length;
895
896 TEST_ASSERT( nonce_length <= sizeof( nonce ) );
897 TEST_ASSERT( tag_length <= sizeof( tag ) );
898
Gilles Peskine8817f612018-12-18 00:18:46 +0100899 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200900
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200901 psa_set_key_usage_flags( &attributes, policy_usage );
902 psa_set_key_algorithm( &attributes, policy_alg );
903 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200904
Gilles Peskine049c7532019-05-15 20:22:09 +0200905 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200906 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200907
Ronald Cron5425a212020-08-04 14:58:35 +0200908 status = psa_aead_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200909 nonce, nonce_length,
910 NULL, 0,
911 NULL, 0,
912 tag, tag_length,
913 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100914 if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
915 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200916 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100917 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200918
919 memset( tag, 0, sizeof( tag ) );
Ronald Cron5425a212020-08-04 14:58:35 +0200920 status = psa_aead_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200921 nonce, nonce_length,
922 NULL, 0,
923 tag, tag_length,
924 NULL, 0,
925 &output_length );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100926 if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 )
927 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
928 else if( expected_status == PSA_SUCCESS )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100929 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200930 else
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100931 TEST_EQUAL( status, expected_status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200932
933exit:
Ronald Cron5425a212020-08-04 14:58:35 +0200934 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +0200935 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200936}
937/* END_CASE */
938
939/* BEGIN_CASE */
940void asymmetric_encryption_key_policy( int policy_usage,
941 int policy_alg,
942 int key_type,
943 data_t *key_data,
944 int exercise_alg )
945{
Ronald Cron5425a212020-08-04 14:58:35 +0200946 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200947 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200948 psa_status_t status;
949 size_t key_bits;
950 size_t buffer_length;
951 unsigned char *buffer = NULL;
952 size_t output_length;
953
Gilles Peskine8817f612018-12-18 00:18:46 +0100954 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200955
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +0200956 psa_set_key_usage_flags( &attributes, policy_usage );
957 psa_set_key_algorithm( &attributes, policy_alg );
958 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200959
Gilles Peskine049c7532019-05-15 20:22:09 +0200960 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +0200961 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200962
Ronald Cron5425a212020-08-04 14:58:35 +0200963 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +0200964 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200965 buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits,
966 exercise_alg );
Gilles Peskine8cebbba2018-09-27 13:54:18 +0200967 ASSERT_ALLOC( buffer, buffer_length );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200968
Ronald Cron5425a212020-08-04 14:58:35 +0200969 status = psa_asymmetric_encrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200970 NULL, 0,
971 NULL, 0,
972 buffer, buffer_length,
973 &output_length );
974 if( policy_alg == exercise_alg &&
975 ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +0100976 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200977 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100978 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200979
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +0200980 if( buffer_length != 0 )
981 memset( buffer, 0, buffer_length );
Ronald Cron5425a212020-08-04 14:58:35 +0200982 status = psa_asymmetric_decrypt( key, exercise_alg,
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200983 buffer, buffer_length,
984 NULL, 0,
985 buffer, buffer_length,
986 &output_length );
987 if( policy_alg == exercise_alg &&
988 ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +0100989 TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200990 else
Gilles Peskinefe11b722018-12-18 00:24:04 +0100991 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +0200992
993exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100994 /*
995 * Key attributes may have been returned by psa_get_key_attributes()
996 * thus reset them as required.
997 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +0200998 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +0100999
1000 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001001 PSA_DONE( );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001002 mbedtls_free( buffer );
1003}
1004/* END_CASE */
1005
1006/* BEGIN_CASE */
1007void asymmetric_signature_key_policy( int policy_usage,
1008 int policy_alg,
1009 int key_type,
1010 data_t *key_data,
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001011 int exercise_alg,
1012 int payload_length_arg )
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001013{
Ronald Cron5425a212020-08-04 14:58:35 +02001014 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001015 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001016 psa_status_t status;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001017 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
1018 /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be
1019 * compatible with the policy and `payload_length_arg` is supposed to be
1020 * a valid input length to sign. If `payload_length_arg <= 0`,
1021 * `exercise_alg` is supposed to be forbidden by the policy. */
1022 int compatible_alg = payload_length_arg > 0;
1023 size_t payload_length = compatible_alg ? payload_length_arg : 0;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001024 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001025 size_t signature_length;
1026
Gilles Peskine8817f612018-12-18 00:18:46 +01001027 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001028
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001029 psa_set_key_usage_flags( &attributes, policy_usage );
1030 psa_set_key_algorithm( &attributes, policy_alg );
1031 psa_set_key_type( &attributes, key_type );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001032
Gilles Peskine049c7532019-05-15 20:22:09 +02001033 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001034 &key ) );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001035
Ronald Cron5425a212020-08-04 14:58:35 +02001036 status = psa_sign_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001037 payload, payload_length,
1038 signature, sizeof( signature ),
1039 &signature_length );
1040 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001041 PSA_ASSERT( status );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001042 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001043 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001044
1045 memset( signature, 0, sizeof( signature ) );
Ronald Cron5425a212020-08-04 14:58:35 +02001046 status = psa_verify_hash( key, exercise_alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001047 payload, payload_length,
1048 signature, sizeof( signature ) );
1049 if( compatible_alg && ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 )
Gilles Peskinefe11b722018-12-18 00:24:04 +01001050 TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine76f5c7b2018-07-06 16:53:09 +02001051 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001052 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskined5b33222018-06-18 22:20:03 +02001053
1054exit:
Ronald Cron5425a212020-08-04 14:58:35 +02001055 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001056 PSA_DONE( );
Gilles Peskined5b33222018-06-18 22:20:03 +02001057}
1058/* END_CASE */
1059
Janos Follathba3fab92019-06-11 14:50:16 +01001060/* BEGIN_CASE */
Gilles Peskineea0fb492018-07-12 17:17:20 +02001061void derive_key_policy( int policy_usage,
1062 int policy_alg,
1063 int key_type,
1064 data_t *key_data,
1065 int exercise_alg )
1066{
Ronald Cron5425a212020-08-04 14:58:35 +02001067 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001068 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001069 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02001070 psa_status_t status;
1071
Gilles Peskine8817f612018-12-18 00:18:46 +01001072 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001073
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001074 psa_set_key_usage_flags( &attributes, policy_usage );
1075 psa_set_key_algorithm( &attributes, policy_alg );
1076 psa_set_key_type( &attributes, key_type );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001077
Gilles Peskine049c7532019-05-15 20:22:09 +02001078 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001079 &key ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001080
Janos Follathba3fab92019-06-11 14:50:16 +01001081 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
1082
1083 if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) ||
1084 PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) )
Janos Follath0c1ed842019-06-28 13:35:36 +01001085 {
Janos Follathba3fab92019-06-11 14:50:16 +01001086 PSA_ASSERT( psa_key_derivation_input_bytes(
1087 &operation,
1088 PSA_KEY_DERIVATION_INPUT_SEED,
1089 (const uint8_t*) "", 0) );
Janos Follath0c1ed842019-06-28 13:35:36 +01001090 }
Janos Follathba3fab92019-06-11 14:50:16 +01001091
1092 status = psa_key_derivation_input_key( &operation,
1093 PSA_KEY_DERIVATION_INPUT_SECRET,
Ronald Cron5425a212020-08-04 14:58:35 +02001094 key );
Janos Follathba3fab92019-06-11 14:50:16 +01001095
Gilles Peskineea0fb492018-07-12 17:17:20 +02001096 if( policy_alg == exercise_alg &&
1097 ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 )
Gilles Peskine8817f612018-12-18 00:18:46 +01001098 PSA_ASSERT( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001099 else
Gilles Peskinefe11b722018-12-18 00:24:04 +01001100 TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001101
1102exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001103 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001104 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001105 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02001106}
1107/* END_CASE */
1108
1109/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02001110void agreement_key_policy( int policy_usage,
1111 int policy_alg,
1112 int key_type_arg,
1113 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001114 int exercise_alg,
1115 int expected_status_arg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02001116{
Ronald Cron5425a212020-08-04 14:58:35 +02001117 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001118 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001119 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001120 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001121 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001122 psa_status_t expected_status = expected_status_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02001123
Gilles Peskine8817f612018-12-18 00:18:46 +01001124 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001125
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001126 psa_set_key_usage_flags( &attributes, policy_usage );
1127 psa_set_key_algorithm( &attributes, policy_alg );
1128 psa_set_key_type( &attributes, key_type );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001129
Gilles Peskine049c7532019-05-15 20:22:09 +02001130 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001131 &key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001132
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001133 PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) );
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001134 status = mbedtls_test_psa_key_agreement_with_self( &operation, key );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001135
Steven Cooremance48e852020-10-05 16:02:45 +02001136 TEST_EQUAL( status, expected_status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001137
1138exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001139 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001140 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001141 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02001142}
1143/* END_CASE */
1144
1145/* BEGIN_CASE */
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001146void key_policy_alg2( int key_type_arg, data_t *key_data,
1147 int usage_arg, int alg_arg, int alg2_arg )
1148{
Ronald Cron5425a212020-08-04 14:58:35 +02001149 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001150 psa_key_type_t key_type = key_type_arg;
1151 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1152 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
1153 psa_key_usage_t usage = usage_arg;
1154 psa_algorithm_t alg = alg_arg;
1155 psa_algorithm_t alg2 = alg2_arg;
1156
1157 PSA_ASSERT( psa_crypto_init( ) );
1158
1159 psa_set_key_usage_flags( &attributes, usage );
1160 psa_set_key_algorithm( &attributes, alg );
1161 psa_set_key_enrollment_algorithm( &attributes, alg2 );
1162 psa_set_key_type( &attributes, key_type );
1163 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001164 &key ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001165
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02001166 /* Update the usage flags to obtain extended usage flags */
1167 usage = update_key_usage_flags( usage );
Ronald Cron5425a212020-08-04 14:58:35 +02001168 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001169 TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage );
1170 TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg );
1171 TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 );
1172
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001173 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001174 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001175 if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) )
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001176 goto exit;
1177
1178exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001179 /*
1180 * Key attributes may have been returned by psa_get_key_attributes()
1181 * thus reset them as required.
1182 */
1183 psa_reset_key_attributes( &got_attributes );
1184
Ronald Cron5425a212020-08-04 14:58:35 +02001185 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001186 PSA_DONE( );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +02001187}
1188/* END_CASE */
1189
1190/* BEGIN_CASE */
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001191void raw_agreement_key_policy( int policy_usage,
1192 int policy_alg,
1193 int key_type_arg,
1194 data_t *key_data,
Steven Cooremance48e852020-10-05 16:02:45 +02001195 int exercise_alg,
1196 int expected_status_arg )
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001197{
Ronald Cron5425a212020-08-04 14:58:35 +02001198 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001199 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001200 psa_key_type_t key_type = key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001201 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001202 psa_status_t status;
Steven Cooremance48e852020-10-05 16:02:45 +02001203 psa_status_t expected_status = expected_status_arg;
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001204
1205 PSA_ASSERT( psa_crypto_init( ) );
1206
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001207 psa_set_key_usage_flags( &attributes, policy_usage );
1208 psa_set_key_algorithm( &attributes, policy_alg );
1209 psa_set_key_type( &attributes, key_type );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001210
Gilles Peskine049c7532019-05-15 20:22:09 +02001211 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001212 &key ) );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001213
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001214 status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001215
Steven Cooremance48e852020-10-05 16:02:45 +02001216 TEST_EQUAL( status, expected_status );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001217
1218exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02001219 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001220 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001221 PSA_DONE( );
Gilles Peskine04ee2d22019-04-11 21:25:46 +02001222}
1223/* END_CASE */
1224
1225/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001226void copy_success( int source_usage_arg,
1227 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001228 int type_arg, data_t *material,
1229 int copy_attributes,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001230 int target_usage_arg,
1231 int target_alg_arg, int target_alg2_arg,
1232 int expected_usage_arg,
1233 int expected_alg_arg, int expected_alg2_arg )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001234{
Gilles Peskineca25db92019-04-19 11:43:08 +02001235 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1236 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001237 psa_key_usage_t expected_usage = expected_usage_arg;
1238 psa_algorithm_t expected_alg = expected_alg_arg;
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001239 psa_algorithm_t expected_alg2 = expected_alg2_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02001240 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1241 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001242 uint8_t *export_buffer = NULL;
1243
Gilles Peskine57ab7212019-01-28 13:03:09 +01001244 PSA_ASSERT( psa_crypto_init( ) );
1245
Gilles Peskineca25db92019-04-19 11:43:08 +02001246 /* Prepare the source key. */
1247 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1248 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001249 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskineca25db92019-04-19 11:43:08 +02001250 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001251 PSA_ASSERT( psa_import_key( &source_attributes,
1252 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001253 &source_key ) );
1254 PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001255
Gilles Peskineca25db92019-04-19 11:43:08 +02001256 /* Prepare the target attributes. */
1257 if( copy_attributes )
Ronald Cron65f38a32020-10-23 17:11:13 +02001258 {
Gilles Peskineca25db92019-04-19 11:43:08 +02001259 target_attributes = source_attributes;
Ronald Cron65f38a32020-10-23 17:11:13 +02001260 /* Set volatile lifetime to reset the key identifier to 0. */
1261 psa_set_key_lifetime( &target_attributes, PSA_KEY_LIFETIME_VOLATILE );
1262 }
1263
Gilles Peskineca25db92019-04-19 11:43:08 +02001264 if( target_usage_arg != -1 )
1265 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1266 if( target_alg_arg != -1 )
1267 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001268 if( target_alg2_arg != -1 )
1269 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001270
1271 /* Copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001272 PSA_ASSERT( psa_copy_key( source_key,
1273 &target_attributes, &target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001274
1275 /* Destroy the source to ensure that this doesn't affect the target. */
Ronald Cron5425a212020-08-04 14:58:35 +02001276 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001277
1278 /* Test that the target slot has the expected content and policy. */
Ronald Cron5425a212020-08-04 14:58:35 +02001279 PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) );
Gilles Peskineca25db92019-04-19 11:43:08 +02001280 TEST_EQUAL( psa_get_key_type( &source_attributes ),
1281 psa_get_key_type( &target_attributes ) );
1282 TEST_EQUAL( psa_get_key_bits( &source_attributes ),
1283 psa_get_key_bits( &target_attributes ) );
1284 TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
1285 TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001286 TEST_EQUAL( expected_alg2,
1287 psa_get_key_enrollment_algorithm( &target_attributes ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001288 if( expected_usage & PSA_KEY_USAGE_EXPORT )
1289 {
1290 size_t length;
1291 ASSERT_ALLOC( export_buffer, material->len );
Ronald Cron5425a212020-08-04 14:58:35 +02001292 PSA_ASSERT( psa_export_key( target_key, export_buffer,
Gilles Peskine57ab7212019-01-28 13:03:09 +01001293 material->len, &length ) );
1294 ASSERT_COMPARE( material->x, material->len,
1295 export_buffer, length );
1296 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +01001297
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001298 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) )
Gilles Peskine57ab7212019-01-28 13:03:09 +01001299 goto exit;
Gilles Peskinec18e25f2021-02-12 23:48:20 +01001300 if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) )
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001301 goto exit;
Gilles Peskine57ab7212019-01-28 13:03:09 +01001302
Ronald Cron5425a212020-08-04 14:58:35 +02001303 PSA_ASSERT( psa_destroy_key( target_key ) );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001304
1305exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001306 /*
1307 * Source and target key attributes may have been returned by
1308 * psa_get_key_attributes() thus reset them as required.
1309 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02001310 psa_reset_key_attributes( &source_attributes );
1311 psa_reset_key_attributes( &target_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01001312
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001313 PSA_DONE( );
Gilles Peskine57ab7212019-01-28 13:03:09 +01001314 mbedtls_free( export_buffer );
1315}
1316/* END_CASE */
1317
1318/* BEGIN_CASE */
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001319void copy_fail( int source_usage_arg,
1320 int source_alg_arg, int source_alg2_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001321 int type_arg, data_t *material,
1322 int target_type_arg, int target_bits_arg,
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001323 int target_usage_arg,
1324 int target_alg_arg, int target_alg2_arg,
Ronald Cron88a55462021-03-31 09:39:07 +02001325 int target_id_arg, int target_lifetime_arg,
Gilles Peskine4a644642019-05-03 17:14:08 +02001326 int expected_status_arg )
1327{
1328 psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
1329 psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02001330 mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT;
1331 mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT;
Ronald Cron88a55462021-03-31 09:39:07 +02001332 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001333
1334 PSA_ASSERT( psa_crypto_init( ) );
1335
1336 /* Prepare the source key. */
1337 psa_set_key_usage_flags( &source_attributes, source_usage_arg );
1338 psa_set_key_algorithm( &source_attributes, source_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001339 psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001340 psa_set_key_type( &source_attributes, type_arg );
Gilles Peskine049c7532019-05-15 20:22:09 +02001341 PSA_ASSERT( psa_import_key( &source_attributes,
1342 material->x, material->len,
Ronald Cron5425a212020-08-04 14:58:35 +02001343 &source_key ) );
Gilles Peskine4a644642019-05-03 17:14:08 +02001344
1345 /* Prepare the target attributes. */
Ronald Cron88a55462021-03-31 09:39:07 +02001346 psa_set_key_id( &target_attributes, key_id );
1347 psa_set_key_lifetime( &target_attributes, target_lifetime_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001348 psa_set_key_type( &target_attributes, target_type_arg );
1349 psa_set_key_bits( &target_attributes, target_bits_arg );
1350 psa_set_key_usage_flags( &target_attributes, target_usage_arg );
1351 psa_set_key_algorithm( &target_attributes, target_alg_arg );
Gilles Peskinebcdd44b2019-05-20 17:28:11 +02001352 psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
Gilles Peskine4a644642019-05-03 17:14:08 +02001353
1354 /* Try to copy the key. */
Ronald Cron5425a212020-08-04 14:58:35 +02001355 TEST_EQUAL( psa_copy_key( source_key,
1356 &target_attributes, &target_key ),
Gilles Peskine4a644642019-05-03 17:14:08 +02001357 expected_status_arg );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001358
Ronald Cron5425a212020-08-04 14:58:35 +02001359 PSA_ASSERT( psa_destroy_key( source_key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001360
Gilles Peskine4a644642019-05-03 17:14:08 +02001361exit:
1362 psa_reset_key_attributes( &source_attributes );
1363 psa_reset_key_attributes( &target_attributes );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001364 PSA_DONE( );
Gilles Peskine4a644642019-05-03 17:14:08 +02001365}
1366/* END_CASE */
1367
1368/* BEGIN_CASE */
Jaeden Amero6a25b412019-01-04 11:47:44 +00001369void hash_operation_init( )
1370{
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001371 const uint8_t input[1] = { 0 };
Jaeden Amero6a25b412019-01-04 11:47:44 +00001372 /* Test each valid way of initializing the object, except for `= {0}`, as
1373 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1374 * though it's OK by the C standard. We could test for this, but we'd need
1375 * to supress the Clang warning for the test. */
1376 psa_hash_operation_t func = psa_hash_operation_init( );
1377 psa_hash_operation_t init = PSA_HASH_OPERATION_INIT;
1378 psa_hash_operation_t zero;
1379
1380 memset( &zero, 0, sizeof( zero ) );
1381
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001382 /* A freshly-initialized hash operation should not be usable. */
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001383 TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ),
1384 PSA_ERROR_BAD_STATE );
1385 TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ),
1386 PSA_ERROR_BAD_STATE );
1387 TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ),
1388 PSA_ERROR_BAD_STATE );
1389
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001390 /* A default hash operation should be abortable without error. */
1391 PSA_ASSERT( psa_hash_abort( &func ) );
1392 PSA_ASSERT( psa_hash_abort( &init ) );
1393 PSA_ASSERT( psa_hash_abort( &zero ) );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001394}
1395/* END_CASE */
1396
1397/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001398void hash_setup( int alg_arg,
1399 int expected_status_arg )
1400{
1401 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001402 psa_status_t expected_status = expected_status_arg;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001403 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001404 psa_status_t status;
1405
Gilles Peskine8817f612018-12-18 00:18:46 +01001406 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001407
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001408 status = psa_hash_setup( &operation, alg );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001409 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001410
Gilles Peskine9e0a4a52019-02-25 22:11:18 +01001411 /* Whether setup succeeded or failed, abort must succeed. */
1412 PSA_ASSERT( psa_hash_abort( &operation ) );
1413
1414 /* If setup failed, reproduce the failure, so as to
1415 * test the resulting state of the operation object. */
1416 if( status != PSA_SUCCESS )
1417 TEST_EQUAL( psa_hash_setup( &operation, alg ), status );
1418
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001419 /* Now the operation object should be reusable. */
1420#if defined(KNOWN_SUPPORTED_HASH_ALG)
1421 PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) );
1422 PSA_ASSERT( psa_hash_abort( &operation ) );
1423#endif
1424
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001425exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001426 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001427}
1428/* END_CASE */
1429
1430/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001431void hash_compute_fail( int alg_arg, data_t *input,
1432 int output_size_arg, int expected_status_arg )
1433{
1434 psa_algorithm_t alg = alg_arg;
1435 uint8_t *output = NULL;
1436 size_t output_size = output_size_arg;
1437 size_t output_length = INVALID_EXPORT_LENGTH;
1438 psa_status_t expected_status = expected_status_arg;
1439 psa_status_t status;
1440
1441 ASSERT_ALLOC( output, output_size );
1442
1443 PSA_ASSERT( psa_crypto_init( ) );
1444
1445 status = psa_hash_compute( alg, input->x, input->len,
1446 output, output_size, &output_length );
1447 TEST_EQUAL( status, expected_status );
1448 TEST_ASSERT( output_length <= output_size );
1449
1450exit:
1451 mbedtls_free( output );
1452 PSA_DONE( );
1453}
1454/* END_CASE */
1455
1456/* BEGIN_CASE */
Gilles Peskine88e08462020-01-28 20:43:00 +01001457void hash_compare_fail( int alg_arg, data_t *input,
1458 data_t *reference_hash,
1459 int expected_status_arg )
1460{
1461 psa_algorithm_t alg = alg_arg;
1462 psa_status_t expected_status = expected_status_arg;
1463 psa_status_t status;
1464
1465 PSA_ASSERT( psa_crypto_init( ) );
1466
1467 status = psa_hash_compare( alg, input->x, input->len,
1468 reference_hash->x, reference_hash->len );
1469 TEST_EQUAL( status, expected_status );
1470
1471exit:
1472 PSA_DONE( );
1473}
1474/* END_CASE */
1475
1476/* BEGIN_CASE */
Gilles Peskine0a749c82019-11-28 19:33:58 +01001477void hash_compute_compare( int alg_arg, data_t *input,
1478 data_t *expected_output )
1479{
1480 psa_algorithm_t alg = alg_arg;
1481 uint8_t output[PSA_HASH_MAX_SIZE + 1];
1482 size_t output_length = INVALID_EXPORT_LENGTH;
1483 size_t i;
1484
1485 PSA_ASSERT( psa_crypto_init( ) );
1486
1487 /* Compute with tight buffer */
1488 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001489 output, PSA_HASH_LENGTH( alg ),
Gilles Peskine0a749c82019-11-28 19:33:58 +01001490 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001491 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001492 ASSERT_COMPARE( output, output_length,
1493 expected_output->x, expected_output->len );
1494
1495 /* Compute with larger buffer */
1496 PSA_ASSERT( psa_hash_compute( alg, input->x, input->len,
1497 output, sizeof( output ),
1498 &output_length ) );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001499 TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001500 ASSERT_COMPARE( output, output_length,
1501 expected_output->x, expected_output->len );
1502
1503 /* Compare with correct hash */
1504 PSA_ASSERT( psa_hash_compare( alg, input->x, input->len,
1505 output, output_length ) );
1506
1507 /* Compare with trailing garbage */
1508 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1509 output, output_length + 1 ),
1510 PSA_ERROR_INVALID_SIGNATURE );
1511
1512 /* Compare with truncated hash */
1513 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1514 output, output_length - 1 ),
1515 PSA_ERROR_INVALID_SIGNATURE );
1516
1517 /* Compare with corrupted value */
1518 for( i = 0; i < output_length; i++ )
1519 {
Chris Jones9634bb12021-01-20 15:56:42 +00001520 mbedtls_test_set_step( i );
Gilles Peskine0a749c82019-11-28 19:33:58 +01001521 output[i] ^= 1;
1522 TEST_EQUAL( psa_hash_compare( alg, input->x, input->len,
1523 output, output_length ),
1524 PSA_ERROR_INVALID_SIGNATURE );
1525 output[i] ^= 1;
1526 }
1527
1528exit:
1529 PSA_DONE( );
1530}
1531/* END_CASE */
1532
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001533/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirf86548d2018-11-01 10:44:32 +02001534void hash_bad_order( )
1535{
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001536 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirf86548d2018-11-01 10:44:32 +02001537 unsigned char input[] = "";
1538 /* SHA-256 hash of an empty string */
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001539 const unsigned char valid_hash[] = {
itayzafrirf86548d2018-11-01 10:44:32 +02001540 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1541 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1542 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 };
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001543 unsigned char hash[sizeof(valid_hash)] = { 0 };
itayzafrirf86548d2018-11-01 10:44:32 +02001544 size_t hash_len;
Jaeden Amero6a25b412019-01-04 11:47:44 +00001545 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirf86548d2018-11-01 10:44:32 +02001546
Gilles Peskine8817f612018-12-18 00:18:46 +01001547 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001548
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001549 /* Call setup twice in a row. */
1550 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001551 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001552 TEST_EQUAL( psa_hash_setup( &operation, alg ),
1553 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001554 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001555 PSA_ASSERT( psa_hash_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001556 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001557
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001558 /* Call update without calling setup beforehand. */
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001559 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001560 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001561 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001562
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001563 /* Check that update calls abort on error. */
1564 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman6f710582021-06-24 18:14:52 +01001565 operation.id = UINT_MAX;
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001566 ASSERT_OPERATION_IS_ACTIVE( operation );
1567 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
1568 PSA_ERROR_BAD_STATE );
1569 ASSERT_OPERATION_IS_INACTIVE( operation );
1570 PSA_ASSERT( psa_hash_abort( &operation ) );
1571 ASSERT_OPERATION_IS_INACTIVE( operation );
1572
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001573 /* Call update after finish. */
1574 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1575 PSA_ASSERT( psa_hash_finish( &operation,
1576 hash, sizeof( hash ), &hash_len ) );
1577 TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001578 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001579 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001580
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001581 /* Call verify without calling setup beforehand. */
1582 TEST_EQUAL( psa_hash_verify( &operation,
1583 valid_hash, sizeof( valid_hash ) ),
1584 PSA_ERROR_BAD_STATE );
1585 PSA_ASSERT( psa_hash_abort( &operation ) );
1586
1587 /* Call verify after finish. */
1588 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1589 PSA_ASSERT( psa_hash_finish( &operation,
1590 hash, sizeof( hash ), &hash_len ) );
1591 TEST_EQUAL( psa_hash_verify( &operation,
1592 valid_hash, sizeof( valid_hash ) ),
1593 PSA_ERROR_BAD_STATE );
1594 PSA_ASSERT( psa_hash_abort( &operation ) );
1595
1596 /* Call verify twice in a row. */
1597 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001598 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001599 PSA_ASSERT( psa_hash_verify( &operation,
1600 valid_hash, sizeof( valid_hash ) ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001601 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001602 TEST_EQUAL( psa_hash_verify( &operation,
1603 valid_hash, sizeof( valid_hash ) ),
1604 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001605 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001606 PSA_ASSERT( psa_hash_abort( &operation ) );
1607
1608 /* Call finish without calling setup beforehand. */
Gilles Peskinefe11b722018-12-18 00:24:04 +01001609 TEST_EQUAL( psa_hash_finish( &operation,
1610 hash, sizeof( hash ), &hash_len ),
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001611 PSA_ERROR_BAD_STATE );
Jaeden Amero11aa7ee2019-02-19 11:44:55 +00001612 PSA_ASSERT( psa_hash_abort( &operation ) );
1613
1614 /* Call finish twice in a row. */
1615 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1616 PSA_ASSERT( psa_hash_finish( &operation,
1617 hash, sizeof( hash ), &hash_len ) );
1618 TEST_EQUAL( psa_hash_finish( &operation,
1619 hash, sizeof( hash ), &hash_len ),
1620 PSA_ERROR_BAD_STATE );
1621 PSA_ASSERT( psa_hash_abort( &operation ) );
1622
1623 /* Call finish after calling verify. */
1624 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
1625 PSA_ASSERT( psa_hash_verify( &operation,
1626 valid_hash, sizeof( valid_hash ) ) );
1627 TEST_EQUAL( psa_hash_finish( &operation,
1628 hash, sizeof( hash ), &hash_len ),
1629 PSA_ERROR_BAD_STATE );
1630 PSA_ASSERT( psa_hash_abort( &operation ) );
itayzafrirf86548d2018-11-01 10:44:32 +02001631
1632exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001633 PSA_DONE( );
itayzafrirf86548d2018-11-01 10:44:32 +02001634}
1635/* END_CASE */
1636
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001637/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrir27e69452018-11-01 14:26:34 +02001638void hash_verify_bad_args( )
itayzafrirec93d302018-10-18 18:01:10 +03001639{
1640 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrir27e69452018-11-01 14:26:34 +02001641 /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb)
1642 * appended to it */
1643 unsigned char hash[] = {
1644 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8,
1645 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
1646 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001647 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001648 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrirec93d302018-10-18 18:01:10 +03001649
Gilles Peskine8817f612018-12-18 00:18:46 +01001650 PSA_ASSERT( psa_crypto_init( ) );
itayzafrirec93d302018-10-18 18:01:10 +03001651
itayzafrir27e69452018-11-01 14:26:34 +02001652 /* psa_hash_verify with a smaller hash than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001653 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001654 ASSERT_OPERATION_IS_ACTIVE( operation );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001655 TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001656 PSA_ERROR_INVALID_SIGNATURE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001657 ASSERT_OPERATION_IS_INACTIVE( operation );
1658 PSA_ASSERT( psa_hash_abort( &operation ) );
1659 ASSERT_OPERATION_IS_INACTIVE( operation );
itayzafrirec93d302018-10-18 18:01:10 +03001660
itayzafrir27e69452018-11-01 14:26:34 +02001661 /* psa_hash_verify with a non-matching hash */
Gilles Peskine8817f612018-12-18 00:18:46 +01001662 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001663 TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001664 PSA_ERROR_INVALID_SIGNATURE );
itayzafrirec93d302018-10-18 18:01:10 +03001665
itayzafrir27e69452018-11-01 14:26:34 +02001666 /* psa_hash_verify with a hash longer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001667 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001668 TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ),
Gilles Peskinefe11b722018-12-18 00:24:04 +01001669 PSA_ERROR_INVALID_SIGNATURE );
itayzafrir4271df92018-10-24 18:16:19 +03001670
itayzafrirec93d302018-10-18 18:01:10 +03001671exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001672 PSA_DONE( );
itayzafrirec93d302018-10-18 18:01:10 +03001673}
1674/* END_CASE */
1675
Ronald Cronee414c72021-03-18 18:50:08 +01001676/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001677void hash_finish_bad_args( )
itayzafrir58028322018-10-25 10:22:01 +03001678{
1679 psa_algorithm_t alg = PSA_ALG_SHA_256;
itayzafrirb2dd5ed2018-11-01 11:58:59 +02001680 unsigned char hash[PSA_HASH_MAX_SIZE];
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001681 size_t expected_size = PSA_HASH_LENGTH( alg );
Jaeden Amero6a25b412019-01-04 11:47:44 +00001682 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
itayzafrir58028322018-10-25 10:22:01 +03001683 size_t hash_len;
1684
Gilles Peskine8817f612018-12-18 00:18:46 +01001685 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir58028322018-10-25 10:22:01 +03001686
itayzafrir58028322018-10-25 10:22:01 +03001687 /* psa_hash_finish with a smaller hash buffer than expected */
Gilles Peskine8817f612018-12-18 00:18:46 +01001688 PSA_ASSERT( psa_hash_setup( &operation, alg ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01001689 TEST_EQUAL( psa_hash_finish( &operation,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01001690 hash, expected_size - 1, &hash_len ),
1691 PSA_ERROR_BUFFER_TOO_SMALL );
itayzafrir58028322018-10-25 10:22:01 +03001692
1693exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001694 PSA_DONE( );
itayzafrir58028322018-10-25 10:22:01 +03001695}
1696/* END_CASE */
1697
Ronald Cronee414c72021-03-18 18:50:08 +01001698/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001699void hash_clone_source_state( )
1700{
1701 psa_algorithm_t alg = PSA_ALG_SHA_256;
1702 unsigned char hash[PSA_HASH_MAX_SIZE];
1703 psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT;
1704 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1705 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1706 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1707 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1708 size_t hash_len;
1709
1710 PSA_ASSERT( psa_crypto_init( ) );
1711 PSA_ASSERT( psa_hash_setup( &op_source, alg ) );
1712
1713 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1714 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1715 PSA_ASSERT( psa_hash_finish( &op_finished,
1716 hash, sizeof( hash ), &hash_len ) );
1717 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1718 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1719
1720 TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ),
1721 PSA_ERROR_BAD_STATE );
1722
1723 PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) );
1724 PSA_ASSERT( psa_hash_finish( &op_init,
1725 hash, sizeof( hash ), &hash_len ) );
1726 PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) );
1727 PSA_ASSERT( psa_hash_finish( &op_finished,
1728 hash, sizeof( hash ), &hash_len ) );
1729 PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) );
1730 PSA_ASSERT( psa_hash_finish( &op_aborted,
1731 hash, sizeof( hash ), &hash_len ) );
1732
1733exit:
1734 psa_hash_abort( &op_source );
1735 psa_hash_abort( &op_init );
1736 psa_hash_abort( &op_setup );
1737 psa_hash_abort( &op_finished );
1738 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001739 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001740}
1741/* END_CASE */
1742
Ronald Cronee414c72021-03-18 18:50:08 +01001743/* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001744void hash_clone_target_state( )
1745{
1746 psa_algorithm_t alg = PSA_ALG_SHA_256;
1747 unsigned char hash[PSA_HASH_MAX_SIZE];
1748 psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT;
1749 psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT;
1750 psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT;
1751 psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT;
1752 psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT;
1753 size_t hash_len;
1754
1755 PSA_ASSERT( psa_crypto_init( ) );
1756
1757 PSA_ASSERT( psa_hash_setup( &op_setup, alg ) );
1758 PSA_ASSERT( psa_hash_setup( &op_finished, alg ) );
1759 PSA_ASSERT( psa_hash_finish( &op_finished,
1760 hash, sizeof( hash ), &hash_len ) );
1761 PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) );
1762 PSA_ASSERT( psa_hash_abort( &op_aborted ) );
1763
1764 PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) );
1765 PSA_ASSERT( psa_hash_finish( &op_target,
1766 hash, sizeof( hash ), &hash_len ) );
1767
1768 TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE );
1769 TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ),
1770 PSA_ERROR_BAD_STATE );
1771 TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ),
1772 PSA_ERROR_BAD_STATE );
1773
1774exit:
1775 psa_hash_abort( &op_target );
1776 psa_hash_abort( &op_init );
1777 psa_hash_abort( &op_setup );
1778 psa_hash_abort( &op_finished );
1779 psa_hash_abort( &op_aborted );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001780 PSA_DONE( );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001781}
1782/* END_CASE */
1783
itayzafrir58028322018-10-25 10:22:01 +03001784/* BEGIN_CASE */
Jaeden Amero769ce272019-01-04 11:48:03 +00001785void mac_operation_init( )
1786{
Jaeden Amero252ef282019-02-15 14:05:35 +00001787 const uint8_t input[1] = { 0 };
1788
Jaeden Amero769ce272019-01-04 11:48:03 +00001789 /* Test each valid way of initializing the object, except for `= {0}`, as
1790 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
1791 * though it's OK by the C standard. We could test for this, but we'd need
1792 * to supress the Clang warning for the test. */
1793 psa_mac_operation_t func = psa_mac_operation_init( );
1794 psa_mac_operation_t init = PSA_MAC_OPERATION_INIT;
1795 psa_mac_operation_t zero;
1796
1797 memset( &zero, 0, sizeof( zero ) );
1798
Jaeden Amero252ef282019-02-15 14:05:35 +00001799 /* A freshly-initialized MAC operation should not be usable. */
1800 TEST_EQUAL( psa_mac_update( &func,
1801 input, sizeof( input ) ),
1802 PSA_ERROR_BAD_STATE );
1803 TEST_EQUAL( psa_mac_update( &init,
1804 input, sizeof( input ) ),
1805 PSA_ERROR_BAD_STATE );
1806 TEST_EQUAL( psa_mac_update( &zero,
1807 input, sizeof( input ) ),
1808 PSA_ERROR_BAD_STATE );
1809
Jaeden Amero5229bbb2019-02-07 16:33:37 +00001810 /* A default MAC operation should be abortable without error. */
1811 PSA_ASSERT( psa_mac_abort( &func ) );
1812 PSA_ASSERT( psa_mac_abort( &init ) );
1813 PSA_ASSERT( psa_mac_abort( &zero ) );
Jaeden Amero769ce272019-01-04 11:48:03 +00001814}
1815/* END_CASE */
1816
1817/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001818void mac_setup( int key_type_arg,
1819 data_t *key,
1820 int alg_arg,
1821 int expected_status_arg )
1822{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001823 psa_key_type_t key_type = key_type_arg;
1824 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02001825 psa_status_t expected_status = expected_status_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001826 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001827 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
1828#if defined(KNOWN_SUPPORTED_MAC_ALG)
1829 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
1830#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001831
Gilles Peskine8817f612018-12-18 00:18:46 +01001832 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001833
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001834 if( ! exercise_mac_setup( key_type, key->x, key->len, alg,
1835 &operation, &status ) )
1836 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01001837 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001838
Gilles Peskinef426e0f2019-02-25 17:42:03 +01001839 /* The operation object should be reusable. */
1840#if defined(KNOWN_SUPPORTED_MAC_ALG)
1841 if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE,
1842 smoke_test_key_data,
1843 sizeof( smoke_test_key_data ),
1844 KNOWN_SUPPORTED_MAC_ALG,
1845 &operation, &status ) )
1846 goto exit;
1847 TEST_EQUAL( status, PSA_SUCCESS );
1848#endif
1849
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001850exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001851 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001852}
1853/* END_CASE */
1854
Gilles Peskined6dc40c2021-01-12 12:55:31 +01001855/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */
Jaeden Amero252ef282019-02-15 14:05:35 +00001856void mac_bad_order( )
1857{
Ronald Cron5425a212020-08-04 14:58:35 +02001858 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001859 psa_key_type_t key_type = PSA_KEY_TYPE_HMAC;
1860 psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256);
Ronald Cron5425a212020-08-04 14:58:35 +02001861 const uint8_t key_data[] = {
Jaeden Amero252ef282019-02-15 14:05:35 +00001862 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1863 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
1864 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001865 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Amero252ef282019-02-15 14:05:35 +00001866 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1867 uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 };
1868 size_t sign_mac_length = 0;
1869 const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb };
1870 const uint8_t verify_mac[] = {
1871 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd,
1872 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a,
1873 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 };
1874
1875 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001876 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001877 psa_set_key_algorithm( &attributes, alg );
1878 psa_set_key_type( &attributes, key_type );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001879
Ronald Cron5425a212020-08-04 14:58:35 +02001880 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
1881 &key ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001882
Jaeden Amero252ef282019-02-15 14:05:35 +00001883 /* Call update without calling setup beforehand. */
1884 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1885 PSA_ERROR_BAD_STATE );
1886 PSA_ASSERT( psa_mac_abort( &operation ) );
1887
1888 /* Call sign finish without calling setup beforehand. */
1889 TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ),
1890 &sign_mac_length),
1891 PSA_ERROR_BAD_STATE );
1892 PSA_ASSERT( psa_mac_abort( &operation ) );
1893
1894 /* Call verify finish without calling setup beforehand. */
1895 TEST_EQUAL( psa_mac_verify_finish( &operation,
1896 verify_mac, sizeof( verify_mac ) ),
1897 PSA_ERROR_BAD_STATE );
1898 PSA_ASSERT( psa_mac_abort( &operation ) );
1899
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001900 /* Call setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001901 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001902 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02001903 TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001904 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001905 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001906 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01001907 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001908
Jaeden Amero252ef282019-02-15 14:05:35 +00001909 /* Call update after sign finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001910 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001911 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1912 PSA_ASSERT( psa_mac_sign_finish( &operation,
1913 sign_mac, sizeof( sign_mac ),
1914 &sign_mac_length ) );
1915 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1916 PSA_ERROR_BAD_STATE );
1917 PSA_ASSERT( psa_mac_abort( &operation ) );
1918
1919 /* Call update after verify finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02001920 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001921 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1922 PSA_ASSERT( psa_mac_verify_finish( &operation,
1923 verify_mac, sizeof( verify_mac ) ) );
1924 TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ),
1925 PSA_ERROR_BAD_STATE );
1926 PSA_ASSERT( psa_mac_abort( &operation ) );
1927
1928 /* Call sign finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001929 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001930 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1931 PSA_ASSERT( psa_mac_sign_finish( &operation,
1932 sign_mac, sizeof( sign_mac ),
1933 &sign_mac_length ) );
1934 TEST_EQUAL( psa_mac_sign_finish( &operation,
1935 sign_mac, sizeof( sign_mac ),
1936 &sign_mac_length ),
1937 PSA_ERROR_BAD_STATE );
1938 PSA_ASSERT( psa_mac_abort( &operation ) );
1939
1940 /* Call verify finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02001941 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001942 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
1943 PSA_ASSERT( psa_mac_verify_finish( &operation,
1944 verify_mac, sizeof( verify_mac ) ) );
1945 TEST_EQUAL( psa_mac_verify_finish( &operation,
1946 verify_mac, sizeof( verify_mac ) ),
1947 PSA_ERROR_BAD_STATE );
1948 PSA_ASSERT( psa_mac_abort( &operation ) );
1949
1950 /* Setup sign but try verify. */
Ronald Cron5425a212020-08-04 14:58:35 +02001951 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001952 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01001953 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00001954 TEST_EQUAL( psa_mac_verify_finish( &operation,
1955 verify_mac, sizeof( verify_mac ) ),
1956 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01001957 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00001958 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01001959 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00001960
1961 /* Setup verify but try sign. */
Ronald Cron5425a212020-08-04 14:58:35 +02001962 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Jaeden Amero252ef282019-02-15 14:05:35 +00001963 PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01001964 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00001965 TEST_EQUAL( psa_mac_sign_finish( &operation,
1966 sign_mac, sizeof( sign_mac ),
1967 &sign_mac_length ),
1968 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01001969 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero252ef282019-02-15 14:05:35 +00001970 PSA_ASSERT( psa_mac_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01001971 ASSERT_OPERATION_IS_INACTIVE( operation );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001972
Ronald Cron5425a212020-08-04 14:58:35 +02001973 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02001974
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001975exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02001976 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001977}
1978/* END_CASE */
1979
1980/* BEGIN_CASE */
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001981void mac_sign( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02001982 data_t *key_data,
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001983 int alg_arg,
1984 data_t *input,
1985 data_t *expected_mac )
1986{
Ronald Cron5425a212020-08-04 14:58:35 +02001987 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001988 psa_key_type_t key_type = key_type_arg;
1989 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00001990 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02001991 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine5e65cec2020-08-25 23:38:39 +02001992 uint8_t *actual_mac = NULL;
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001993 size_t mac_buffer_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01001994 PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02001995 size_t mac_length = 0;
Gilles Peskine8b356b52020-08-25 23:44:59 +02001996 const size_t output_sizes_to_test[] = {
1997 0,
1998 1,
1999 expected_mac->len - 1,
2000 expected_mac->len,
2001 expected_mac->len + 1,
2002 };
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002003
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002004 TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002005 /* We expect PSA_MAC_LENGTH to be exact. */
Gilles Peskine3d404d62020-08-25 23:47:36 +02002006 TEST_ASSERT( expected_mac->len == mac_buffer_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002007
Gilles Peskine8817f612018-12-18 00:18:46 +01002008 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002009
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002010 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002011 psa_set_key_algorithm( &attributes, alg );
2012 psa_set_key_type( &attributes, key_type );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002013
Ronald Cron5425a212020-08-04 14:58:35 +02002014 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2015 &key ) );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002016
Gilles Peskine8b356b52020-08-25 23:44:59 +02002017 for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ )
2018 {
2019 const size_t output_size = output_sizes_to_test[i];
2020 psa_status_t expected_status =
2021 ( output_size >= expected_mac->len ? PSA_SUCCESS :
2022 PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002023
Chris Jones9634bb12021-01-20 15:56:42 +00002024 mbedtls_test_set_step( output_size );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002025 ASSERT_ALLOC( actual_mac, output_size );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002026
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002027 /* Calculate the MAC, one-shot case. */
2028 TEST_EQUAL( psa_mac_compute( key, alg,
2029 input->x, input->len,
2030 actual_mac, output_size, &mac_length ),
2031 expected_status );
2032 if( expected_status == PSA_SUCCESS )
2033 {
2034 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2035 actual_mac, mac_length );
2036 }
2037
2038 if( output_size > 0 )
2039 memset( actual_mac, 0, output_size );
2040
2041 /* Calculate the MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002042 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
Gilles Peskine8b356b52020-08-25 23:44:59 +02002043 PSA_ASSERT( psa_mac_update( &operation,
2044 input->x, input->len ) );
2045 TEST_EQUAL( psa_mac_sign_finish( &operation,
2046 actual_mac, output_size,
2047 &mac_length ),
2048 expected_status );
2049 PSA_ASSERT( psa_mac_abort( &operation ) );
2050
2051 if( expected_status == PSA_SUCCESS )
2052 {
2053 ASSERT_COMPARE( expected_mac->x, expected_mac->len,
2054 actual_mac, mac_length );
2055 }
2056 mbedtls_free( actual_mac );
2057 actual_mac = NULL;
2058 }
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002059
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002060exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002061 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002062 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002063 PSA_DONE( );
Gilles Peskine5e65cec2020-08-25 23:38:39 +02002064 mbedtls_free( actual_mac );
Gilles Peskinea7aa4422018-08-14 15:17:54 +02002065}
2066/* END_CASE */
2067
2068/* BEGIN_CASE */
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002069void mac_verify( int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002070 data_t *key_data,
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002071 int alg_arg,
2072 data_t *input,
2073 data_t *expected_mac )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002074{
Ronald Cron5425a212020-08-04 14:58:35 +02002075 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002076 psa_key_type_t key_type = key_type_arg;
2077 psa_algorithm_t alg = alg_arg;
Jaeden Amero769ce272019-01-04 11:48:03 +00002078 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002079 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002080 uint8_t *perturbed_mac = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002081
Gilles Peskine69c12672018-06-28 00:07:19 +02002082 TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE );
2083
Gilles Peskine8817f612018-12-18 00:18:46 +01002084 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002085
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002086 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002087 psa_set_key_algorithm( &attributes, alg );
2088 psa_set_key_type( &attributes, key_type );
mohammad16036df908f2018-04-02 08:34:15 -07002089
Ronald Cron5425a212020-08-04 14:58:35 +02002090 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2091 &key ) );
Gilles Peskinec0ec9722018-06-18 17:03:37 +02002092
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002093 /* Verify correct MAC, one-shot case. */
2094 PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len,
2095 expected_mac->x, expected_mac->len ) );
2096
2097 /* Verify correct MAC, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002098 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine8817f612018-12-18 00:18:46 +01002099 PSA_ASSERT( psa_mac_update( &operation,
2100 input->x, input->len ) );
2101 PSA_ASSERT( psa_mac_verify_finish( &operation,
2102 expected_mac->x,
2103 expected_mac->len ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002104
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002105 /* Test a MAC that's too short, one-shot case. */
2106 TEST_EQUAL( psa_mac_verify( key, alg,
2107 input->x, input->len,
2108 expected_mac->x,
2109 expected_mac->len - 1 ),
2110 PSA_ERROR_INVALID_SIGNATURE );
2111
2112 /* Test a MAC that's too short, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002113 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002114 PSA_ASSERT( psa_mac_update( &operation,
2115 input->x, input->len ) );
2116 TEST_EQUAL( psa_mac_verify_finish( &operation,
2117 expected_mac->x,
2118 expected_mac->len - 1 ),
2119 PSA_ERROR_INVALID_SIGNATURE );
2120
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002121 /* Test a MAC that's too long, one-shot case. */
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002122 ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 );
2123 memcpy( perturbed_mac, expected_mac->x, expected_mac->len );
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002124 TEST_EQUAL( psa_mac_verify( key, alg,
2125 input->x, input->len,
2126 perturbed_mac, expected_mac->len + 1 ),
2127 PSA_ERROR_INVALID_SIGNATURE );
2128
2129 /* Test a MAC that's too long, multi-part case. */
Ronald Cron5425a212020-08-04 14:58:35 +02002130 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002131 PSA_ASSERT( psa_mac_update( &operation,
2132 input->x, input->len ) );
2133 TEST_EQUAL( psa_mac_verify_finish( &operation,
2134 perturbed_mac,
2135 expected_mac->len + 1 ),
2136 PSA_ERROR_INVALID_SIGNATURE );
2137
2138 /* Test changing one byte. */
2139 for( size_t i = 0; i < expected_mac->len; i++ )
2140 {
Chris Jones9634bb12021-01-20 15:56:42 +00002141 mbedtls_test_set_step( i );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002142 perturbed_mac[i] ^= 1;
gabor-mezei-arm534bb992021-03-01 15:35:48 +01002143
2144 TEST_EQUAL( psa_mac_verify( key, alg,
2145 input->x, input->len,
2146 perturbed_mac, expected_mac->len ),
2147 PSA_ERROR_INVALID_SIGNATURE );
2148
Ronald Cron5425a212020-08-04 14:58:35 +02002149 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002150 PSA_ASSERT( psa_mac_update( &operation,
2151 input->x, input->len ) );
2152 TEST_EQUAL( psa_mac_verify_finish( &operation,
2153 perturbed_mac,
2154 expected_mac->len ),
2155 PSA_ERROR_INVALID_SIGNATURE );
2156 perturbed_mac[i] ^= 1;
2157 }
2158
Gilles Peskine8c9def32018-02-08 10:02:12 +01002159exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002160 psa_mac_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002161 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002162 PSA_DONE( );
Gilles Peskine29c4a6c2020-08-26 00:01:39 +02002163 mbedtls_free( perturbed_mac );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002164}
2165/* END_CASE */
2166
2167/* BEGIN_CASE */
Jaeden Amero5bae2272019-01-04 11:48:27 +00002168void cipher_operation_init( )
2169{
Jaeden Ameroab439972019-02-15 14:12:05 +00002170 const uint8_t input[1] = { 0 };
2171 unsigned char output[1] = { 0 };
2172 size_t output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002173 /* Test each valid way of initializing the object, except for `= {0}`, as
2174 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
2175 * though it's OK by the C standard. We could test for this, but we'd need
2176 * to supress the Clang warning for the test. */
2177 psa_cipher_operation_t func = psa_cipher_operation_init( );
2178 psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT;
2179 psa_cipher_operation_t zero;
2180
2181 memset( &zero, 0, sizeof( zero ) );
2182
Jaeden Ameroab439972019-02-15 14:12:05 +00002183 /* A freshly-initialized cipher operation should not be usable. */
2184 TEST_EQUAL( psa_cipher_update( &func,
2185 input, sizeof( input ),
2186 output, sizeof( output ),
2187 &output_length ),
2188 PSA_ERROR_BAD_STATE );
2189 TEST_EQUAL( psa_cipher_update( &init,
2190 input, sizeof( input ),
2191 output, sizeof( output ),
2192 &output_length ),
2193 PSA_ERROR_BAD_STATE );
2194 TEST_EQUAL( psa_cipher_update( &zero,
2195 input, sizeof( input ),
2196 output, sizeof( output ),
2197 &output_length ),
2198 PSA_ERROR_BAD_STATE );
2199
Jaeden Amero5229bbb2019-02-07 16:33:37 +00002200 /* A default cipher operation should be abortable without error. */
2201 PSA_ASSERT( psa_cipher_abort( &func ) );
2202 PSA_ASSERT( psa_cipher_abort( &init ) );
2203 PSA_ASSERT( psa_cipher_abort( &zero ) );
Jaeden Amero5bae2272019-01-04 11:48:27 +00002204}
2205/* END_CASE */
2206
2207/* BEGIN_CASE */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002208void cipher_setup( int key_type_arg,
2209 data_t *key,
2210 int alg_arg,
2211 int expected_status_arg )
2212{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002213 psa_key_type_t key_type = key_type_arg;
2214 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002215 psa_status_t expected_status = expected_status_arg;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002216 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002217 psa_status_t status;
Gilles Peskine612ffd22021-01-20 18:51:00 +01002218#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002219 const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk";
2220#endif
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002221
Gilles Peskine8817f612018-12-18 00:18:46 +01002222 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002223
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002224 if( ! exercise_cipher_setup( key_type, key->x, key->len, alg,
2225 &operation, &status ) )
2226 goto exit;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002227 TEST_EQUAL( status, expected_status );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002228
Gilles Peskinef426e0f2019-02-25 17:42:03 +01002229 /* The operation object should be reusable. */
2230#if defined(KNOWN_SUPPORTED_CIPHER_ALG)
2231 if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE,
2232 smoke_test_key_data,
2233 sizeof( smoke_test_key_data ),
2234 KNOWN_SUPPORTED_CIPHER_ALG,
2235 &operation, &status ) )
2236 goto exit;
2237 TEST_EQUAL( status, PSA_SUCCESS );
2238#endif
2239
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002240exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002241 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002242 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002243}
2244/* END_CASE */
2245
Ronald Cronee414c72021-03-18 18:50:08 +01002246/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */
Jaeden Ameroab439972019-02-15 14:12:05 +00002247void cipher_bad_order( )
2248{
Ronald Cron5425a212020-08-04 14:58:35 +02002249 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002250 psa_key_type_t key_type = PSA_KEY_TYPE_AES;
2251 psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002252 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Jaeden Ameroab439972019-02-15 14:12:05 +00002253 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002254 unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Ronald Cron5425a212020-08-04 14:58:35 +02002255 const uint8_t key_data[] = {
Jaeden Ameroab439972019-02-15 14:12:05 +00002256 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
2257 0xaa, 0xaa, 0xaa, 0xaa };
2258 const uint8_t text[] = {
2259 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb,
2260 0xbb, 0xbb, 0xbb, 0xbb };
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002261 uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 };
Jaeden Ameroab439972019-02-15 14:12:05 +00002262 size_t length = 0;
2263
2264 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002265 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2266 psa_set_key_algorithm( &attributes, alg );
2267 psa_set_key_type( &attributes, key_type );
Ronald Cron5425a212020-08-04 14:58:35 +02002268 PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ),
2269 &key ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002270
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002271 /* Call encrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002272 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002273 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002274 TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002275 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002276 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002277 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002278 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002279
2280 /* Call decrypt setup twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002281 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002282 ASSERT_OPERATION_IS_ACTIVE( operation );
Ronald Cron5425a212020-08-04 14:58:35 +02002283 TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ),
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002284 PSA_ERROR_BAD_STATE );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002285 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002286 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman5ae6f752021-06-24 11:36:14 +01002287 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002288
Jaeden Ameroab439972019-02-15 14:12:05 +00002289 /* Generate an IV without calling setup beforehand. */
2290 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2291 buffer, sizeof( buffer ),
2292 &length ),
2293 PSA_ERROR_BAD_STATE );
2294 PSA_ASSERT( psa_cipher_abort( &operation ) );
2295
2296 /* Generate an IV twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002297 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002298 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2299 buffer, sizeof( buffer ),
2300 &length ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002301 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002302 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2303 buffer, sizeof( buffer ),
2304 &length ),
2305 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002306 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002307 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002308 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002309
2310 /* Generate an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002311 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002312 PSA_ASSERT( psa_cipher_set_iv( &operation,
2313 iv, sizeof( iv ) ) );
2314 TEST_EQUAL( psa_cipher_generate_iv( &operation,
2315 buffer, sizeof( buffer ),
2316 &length ),
2317 PSA_ERROR_BAD_STATE );
2318 PSA_ASSERT( psa_cipher_abort( &operation ) );
2319
2320 /* Set an IV without calling setup beforehand. */
2321 TEST_EQUAL( psa_cipher_set_iv( &operation,
2322 iv, sizeof( iv ) ),
2323 PSA_ERROR_BAD_STATE );
2324 PSA_ASSERT( psa_cipher_abort( &operation ) );
2325
2326 /* Set an IV after it's already set. */
Ronald Cron5425a212020-08-04 14:58:35 +02002327 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002328 PSA_ASSERT( psa_cipher_set_iv( &operation,
2329 iv, sizeof( iv ) ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002330 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002331 TEST_EQUAL( psa_cipher_set_iv( &operation,
2332 iv, sizeof( iv ) ),
2333 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002334 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002335 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002336 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002337
2338 /* Set an IV after it's already generated. */
Ronald Cron5425a212020-08-04 14:58:35 +02002339 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002340 PSA_ASSERT( psa_cipher_generate_iv( &operation,
2341 buffer, sizeof( buffer ),
2342 &length ) );
2343 TEST_EQUAL( psa_cipher_set_iv( &operation,
2344 iv, sizeof( iv ) ),
2345 PSA_ERROR_BAD_STATE );
2346 PSA_ASSERT( psa_cipher_abort( &operation ) );
2347
2348 /* Call update without calling setup beforehand. */
2349 TEST_EQUAL( psa_cipher_update( &operation,
2350 text, sizeof( text ),
2351 buffer, sizeof( buffer ),
2352 &length ),
2353 PSA_ERROR_BAD_STATE );
2354 PSA_ASSERT( psa_cipher_abort( &operation ) );
2355
2356 /* Call update without an IV where an IV is required. */
Dave Rodgman095dadc2021-06-23 12:48:52 +01002357 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002358 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002359 TEST_EQUAL( psa_cipher_update( &operation,
2360 text, sizeof( text ),
2361 buffer, sizeof( buffer ),
2362 &length ),
2363 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002364 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002365 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002366 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002367
2368 /* Call update after finish. */
Ronald Cron5425a212020-08-04 14:58:35 +02002369 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002370 PSA_ASSERT( psa_cipher_set_iv( &operation,
2371 iv, sizeof( iv ) ) );
2372 PSA_ASSERT( psa_cipher_finish( &operation,
2373 buffer, sizeof( buffer ), &length ) );
2374 TEST_EQUAL( psa_cipher_update( &operation,
2375 text, sizeof( text ),
2376 buffer, sizeof( buffer ),
2377 &length ),
2378 PSA_ERROR_BAD_STATE );
2379 PSA_ASSERT( psa_cipher_abort( &operation ) );
2380
2381 /* Call finish without calling setup beforehand. */
2382 TEST_EQUAL( psa_cipher_finish( &operation,
2383 buffer, sizeof( buffer ), &length ),
2384 PSA_ERROR_BAD_STATE );
2385 PSA_ASSERT( psa_cipher_abort( &operation ) );
2386
2387 /* Call finish without an IV where an IV is required. */
Ronald Cron5425a212020-08-04 14:58:35 +02002388 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002389 /* Not calling update means we are encrypting an empty buffer, which is OK
2390 * for cipher modes with padding. */
Dave Rodgman647791d2021-06-23 12:49:59 +01002391 ASSERT_OPERATION_IS_ACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002392 TEST_EQUAL( psa_cipher_finish( &operation,
2393 buffer, sizeof( buffer ), &length ),
2394 PSA_ERROR_BAD_STATE );
Dave Rodgman647791d2021-06-23 12:49:59 +01002395 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002396 PSA_ASSERT( psa_cipher_abort( &operation ) );
Dave Rodgman647791d2021-06-23 12:49:59 +01002397 ASSERT_OPERATION_IS_INACTIVE( operation );
Jaeden Ameroab439972019-02-15 14:12:05 +00002398
2399 /* Call finish twice in a row. */
Ronald Cron5425a212020-08-04 14:58:35 +02002400 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Jaeden Ameroab439972019-02-15 14:12:05 +00002401 PSA_ASSERT( psa_cipher_set_iv( &operation,
2402 iv, sizeof( iv ) ) );
2403 PSA_ASSERT( psa_cipher_finish( &operation,
2404 buffer, sizeof( buffer ), &length ) );
2405 TEST_EQUAL( psa_cipher_finish( &operation,
2406 buffer, sizeof( buffer ), &length ),
2407 PSA_ERROR_BAD_STATE );
2408 PSA_ASSERT( psa_cipher_abort( &operation ) );
2409
Ronald Cron5425a212020-08-04 14:58:35 +02002410 PSA_ASSERT( psa_destroy_key( key ) );
Gilles Peskine76b29a72019-05-28 14:08:50 +02002411
Jaeden Ameroab439972019-02-15 14:12:05 +00002412exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002413 psa_cipher_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002414 PSA_DONE( );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002415}
2416/* END_CASE */
2417
2418/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002419void cipher_encrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002420 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002421 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002422 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002423{
Ronald Cron5425a212020-08-04 14:58:35 +02002424 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002425 psa_status_t status;
2426 psa_key_type_t key_type = key_type_arg;
2427 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002428 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002429 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002430 size_t output_buffer_size = 0;
2431 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002432 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002433 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002434 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002435
Gilles Peskine8817f612018-12-18 00:18:46 +01002436 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002437
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002438 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2439 psa_set_key_algorithm( &attributes, alg );
2440 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002441
Ronald Cron5425a212020-08-04 14:58:35 +02002442 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2443 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002444
Ronald Cron5425a212020-08-04 14:58:35 +02002445 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002446
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002447 if( iv->len > 0 )
2448 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002449 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002450 }
2451
gabor-mezei-armceface22021-01-21 12:26:17 +01002452 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2453 TEST_ASSERT( output_buffer_size <=
2454 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002455 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002456
Gilles Peskine8817f612018-12-18 00:18:46 +01002457 PSA_ASSERT( psa_cipher_update( &operation,
2458 input->x, input->len,
2459 output, output_buffer_size,
2460 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002461 TEST_ASSERT( function_output_length <=
2462 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2463 TEST_ASSERT( function_output_length <=
2464 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002465 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002466
Gilles Peskine50e586b2018-06-08 14:28:46 +02002467 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002468 ( output_buffer_size == 0 ? NULL :
2469 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002470 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002471 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002472 TEST_ASSERT( function_output_length <=
2473 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2474 TEST_ASSERT( function_output_length <=
2475 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002476 total_output_length += function_output_length;
2477
Gilles Peskinefe11b722018-12-18 00:24:04 +01002478 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002479 if( expected_status == PSA_SUCCESS )
2480 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002481 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002482 ASSERT_COMPARE( expected_output->x, expected_output->len,
2483 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002484 }
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002485
Gilles Peskine50e586b2018-06-08 14:28:46 +02002486exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002487 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002488 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002489 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002490 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002491}
2492/* END_CASE */
2493
2494/* BEGIN_CASE */
2495void cipher_encrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002496 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002497 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002498 int first_part_size_arg,
2499 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002500 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002501{
Ronald Cron5425a212020-08-04 14:58:35 +02002502 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002503 psa_key_type_t key_type = key_type_arg;
2504 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002505 size_t first_part_size = first_part_size_arg;
2506 size_t output1_length = output1_length_arg;
2507 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002508 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002509 size_t output_buffer_size = 0;
2510 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002511 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002512 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002513 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002514
Gilles Peskine8817f612018-12-18 00:18:46 +01002515 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002516
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002517 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
2518 psa_set_key_algorithm( &attributes, alg );
2519 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002520
Ronald Cron5425a212020-08-04 14:58:35 +02002521 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2522 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002523
Ronald Cron5425a212020-08-04 14:58:35 +02002524 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002525
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002526 if( iv->len > 0 )
2527 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002528 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002529 }
2530
gabor-mezei-armceface22021-01-21 12:26:17 +01002531 output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2532 TEST_ASSERT( output_buffer_size <=
2533 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002534 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002535
Gilles Peskinee0866522019-02-19 19:44:00 +01002536 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002537 PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size,
2538 output, output_buffer_size,
2539 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002540 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002541 TEST_ASSERT( function_output_length <=
2542 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2543 TEST_ASSERT( function_output_length <=
2544 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002545 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002546
Gilles Peskine8817f612018-12-18 00:18:46 +01002547 PSA_ASSERT( psa_cipher_update( &operation,
2548 input->x + first_part_size,
2549 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002550 ( output_buffer_size == 0 ? NULL :
2551 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002552 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002553 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002554 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002555 TEST_ASSERT( function_output_length <=
2556 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2557 alg,
2558 input->len - first_part_size ) );
2559 TEST_ASSERT( function_output_length <=
2560 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002561 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002562
Gilles Peskine8817f612018-12-18 00:18:46 +01002563 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002564 ( output_buffer_size == 0 ? NULL :
2565 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002566 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002567 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002568 TEST_ASSERT( function_output_length <=
2569 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2570 TEST_ASSERT( function_output_length <=
2571 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002572 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002573 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002574
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002575 ASSERT_COMPARE( expected_output->x, expected_output->len,
2576 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002577
2578exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002579 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002580 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002581 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002582 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002583}
2584/* END_CASE */
2585
2586/* BEGIN_CASE */
2587void cipher_decrypt_multipart( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002588 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002589 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002590 int first_part_size_arg,
2591 int output1_length_arg, int output2_length_arg,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002592 data_t *expected_output )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002593{
Ronald Cron5425a212020-08-04 14:58:35 +02002594 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002595 psa_key_type_t key_type = key_type_arg;
2596 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002597 size_t first_part_size = first_part_size_arg;
2598 size_t output1_length = output1_length_arg;
2599 size_t output2_length = output2_length_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002600 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002601 size_t output_buffer_size = 0;
2602 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002603 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002604 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002605 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002606
Gilles Peskine8817f612018-12-18 00:18:46 +01002607 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002608
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002609 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2610 psa_set_key_algorithm( &attributes, alg );
2611 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002612
Ronald Cron5425a212020-08-04 14:58:35 +02002613 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2614 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002615
Ronald Cron5425a212020-08-04 14:58:35 +02002616 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002617
Steven Cooreman177deba2020-09-07 17:14:14 +02002618 if( iv->len > 0 )
2619 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002620 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002621 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002622
gabor-mezei-armceface22021-01-21 12:26:17 +01002623 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2624 TEST_ASSERT( output_buffer_size <=
2625 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002626 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002627
Gilles Peskinee0866522019-02-19 19:44:00 +01002628 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine8817f612018-12-18 00:18:46 +01002629 PSA_ASSERT( psa_cipher_update( &operation,
2630 input->x, first_part_size,
2631 output, output_buffer_size,
2632 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002633 TEST_ASSERT( function_output_length == output1_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002634 TEST_ASSERT( function_output_length <=
2635 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2636 TEST_ASSERT( function_output_length <=
2637 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002638 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002639
Gilles Peskine8817f612018-12-18 00:18:46 +01002640 PSA_ASSERT( psa_cipher_update( &operation,
2641 input->x + first_part_size,
2642 input->len - first_part_size,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002643 ( output_buffer_size == 0 ? NULL :
2644 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002645 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002646 &function_output_length ) );
Gilles Peskinee0866522019-02-19 19:44:00 +01002647 TEST_ASSERT( function_output_length == output2_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002648 TEST_ASSERT( function_output_length <=
2649 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2650 alg,
2651 input->len - first_part_size ) );
2652 TEST_ASSERT( function_output_length <=
2653 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002654 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002655
Gilles Peskine8817f612018-12-18 00:18:46 +01002656 PSA_ASSERT( psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002657 ( output_buffer_size == 0 ? NULL :
2658 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002659 output_buffer_size - total_output_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002660 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002661 TEST_ASSERT( function_output_length <=
2662 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2663 TEST_ASSERT( function_output_length <=
2664 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002665 total_output_length += function_output_length;
Gilles Peskine8817f612018-12-18 00:18:46 +01002666 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002667
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002668 ASSERT_COMPARE( expected_output->x, expected_output->len,
2669 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002670
2671exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002672 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002673 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002674 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002675 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002676}
2677/* END_CASE */
2678
Gilles Peskine50e586b2018-06-08 14:28:46 +02002679/* BEGIN_CASE */
2680void cipher_decrypt( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002681 data_t *key_data, data_t *iv,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002682 data_t *input, data_t *expected_output,
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002683 int expected_status_arg )
Gilles Peskine50e586b2018-06-08 14:28:46 +02002684{
Ronald Cron5425a212020-08-04 14:58:35 +02002685 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002686 psa_status_t status;
2687 psa_key_type_t key_type = key_type_arg;
2688 psa_algorithm_t alg = alg_arg;
Gilles Peskineb866e2b2018-06-21 09:25:10 +02002689 psa_status_t expected_status = expected_status_arg;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002690 unsigned char *output = NULL;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002691 size_t output_buffer_size = 0;
2692 size_t function_output_length = 0;
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002693 size_t total_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002694 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002695 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine50e586b2018-06-08 14:28:46 +02002696
Gilles Peskine8817f612018-12-18 00:18:46 +01002697 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002698
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002699 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
2700 psa_set_key_algorithm( &attributes, alg );
2701 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002702
Ronald Cron5425a212020-08-04 14:58:35 +02002703 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2704 &key ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002705
Ronald Cron5425a212020-08-04 14:58:35 +02002706 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002707
Steven Cooreman177deba2020-09-07 17:14:14 +02002708 if( iv->len > 0 )
2709 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002710 PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002711 }
Gilles Peskine50e586b2018-06-08 14:28:46 +02002712
gabor-mezei-armceface22021-01-21 12:26:17 +01002713 output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2714 TEST_ASSERT( output_buffer_size <=
2715 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002716 ASSERT_ALLOC( output, output_buffer_size );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002717
Gilles Peskine8817f612018-12-18 00:18:46 +01002718 PSA_ASSERT( psa_cipher_update( &operation,
2719 input->x, input->len,
2720 output, output_buffer_size,
2721 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002722 TEST_ASSERT( function_output_length <=
2723 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2724 TEST_ASSERT( function_output_length <=
2725 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002726 total_output_length += function_output_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002727
Gilles Peskine50e586b2018-06-08 14:28:46 +02002728 status = psa_cipher_finish( &operation,
Gilles Peskine0c510f32021-03-24 00:41:51 +01002729 ( output_buffer_size == 0 ? NULL :
2730 output + total_output_length ),
Gilles Peskineee46fe72019-02-19 19:05:33 +01002731 output_buffer_size - total_output_length,
Gilles Peskine50e586b2018-06-08 14:28:46 +02002732 &function_output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +01002733 TEST_ASSERT( function_output_length <=
2734 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2735 TEST_ASSERT( function_output_length <=
2736 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskinea7ec95f2018-06-08 14:40:59 +02002737 total_output_length += function_output_length;
Gilles Peskinefe11b722018-12-18 00:24:04 +01002738 TEST_EQUAL( status, expected_status );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002739
2740 if( expected_status == PSA_SUCCESS )
2741 {
Gilles Peskine8817f612018-12-18 00:18:46 +01002742 PSA_ASSERT( psa_cipher_abort( &operation ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002743 ASSERT_COMPARE( expected_output->x, expected_output->len,
2744 output, total_output_length );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002745 }
2746
Gilles Peskine50e586b2018-06-08 14:28:46 +02002747exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002748 psa_cipher_abort( &operation );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002749 mbedtls_free( output );
Ronald Cron5425a212020-08-04 14:58:35 +02002750 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002751 PSA_DONE( );
Gilles Peskine50e586b2018-06-08 14:28:46 +02002752}
2753/* END_CASE */
2754
Gilles Peskine50e586b2018-06-08 14:28:46 +02002755/* BEGIN_CASE */
2756void cipher_verify_output( int alg_arg, int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002757 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002758 data_t *input )
mohammad1603d7d7ba52018-03-12 18:51:53 +02002759{
Ronald Cron5425a212020-08-04 14:58:35 +02002760 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002761 psa_key_type_t key_type = key_type_arg;
2762 psa_algorithm_t alg = alg_arg;
mohammad1603e6b67a12018-03-12 10:38:49 -07002763 unsigned char iv[16] = {0};
mohammad1603d7d7ba52018-03-12 18:51:53 +02002764 size_t iv_size = 16;
2765 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002766 unsigned char *output1 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002767 size_t output1_size = 0;
2768 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002769 unsigned char *output2 = NULL;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002770 size_t output2_size = 0;
2771 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002772 size_t function_output_length = 0;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002773 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2774 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002775 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002776
Gilles Peskine8817f612018-12-18 00:18:46 +01002777 PSA_ASSERT( psa_crypto_init( ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002778
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002779 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2780 psa_set_key_algorithm( &attributes, alg );
2781 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002782
Ronald Cron5425a212020-08-04 14:58:35 +02002783 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2784 &key ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002785
Ronald Cron5425a212020-08-04 14:58:35 +02002786 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2787 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002788
Steven Cooreman177deba2020-09-07 17:14:14 +02002789 if( alg != PSA_ALG_ECB_NO_PADDING )
2790 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002791 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2792 iv, iv_size,
2793 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002794 }
gabor-mezei-armceface22021-01-21 12:26:17 +01002795 output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2796 TEST_ASSERT( output1_size <=
2797 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002798 ASSERT_ALLOC( output1, output1_size );
Moran Pekerded84402018-06-06 16:36:50 +03002799
Gilles Peskine8817f612018-12-18 00:18:46 +01002800 PSA_ASSERT( psa_cipher_update( &operation1, input->x, input->len,
2801 output1, output1_size,
2802 &output1_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002803 TEST_ASSERT( output1_length <=
2804 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) );
2805 TEST_ASSERT( output1_length <=
2806 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) );
2807
Gilles Peskine8817f612018-12-18 00:18:46 +01002808 PSA_ASSERT( psa_cipher_finish( &operation1,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002809 output1 + output1_length,
2810 output1_size - output1_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002811 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002812 TEST_ASSERT( function_output_length <=
2813 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2814 TEST_ASSERT( function_output_length <=
2815 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002816
Gilles Peskine048b7f02018-06-08 14:20:49 +02002817 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002818
Gilles Peskine8817f612018-12-18 00:18:46 +01002819 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002820
2821 output2_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002822 TEST_ASSERT( output2_size <=
2823 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2824 TEST_ASSERT( output2_size <=
2825 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002826 ASSERT_ALLOC( output2, output2_size );
Moran Pekerded84402018-06-06 16:36:50 +03002827
Steven Cooreman177deba2020-09-07 17:14:14 +02002828 if( iv_length > 0 )
2829 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002830 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2831 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002832 }
2833
Gilles Peskine8817f612018-12-18 00:18:46 +01002834 PSA_ASSERT( psa_cipher_update( &operation2, output1, output1_length,
2835 output2, output2_size,
2836 &output2_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002837 TEST_ASSERT( output2_length <=
2838 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, output1_length ) );
2839 TEST_ASSERT( output2_length <=
2840 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length ) );
2841
Gilles Peskine048b7f02018-06-08 14:20:49 +02002842 function_output_length = 0;
Gilles Peskine8817f612018-12-18 00:18:46 +01002843 PSA_ASSERT( psa_cipher_finish( &operation2,
2844 output2 + output2_length,
Gilles Peskineee46fe72019-02-19 19:05:33 +01002845 output2_size - output2_length,
Gilles Peskine8817f612018-12-18 00:18:46 +01002846 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002847 TEST_ASSERT( function_output_length <=
2848 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2849 TEST_ASSERT( function_output_length <=
2850 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Moran Pekerded84402018-06-06 16:36:50 +03002851
Gilles Peskine048b7f02018-06-08 14:20:49 +02002852 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002853
Gilles Peskine8817f612018-12-18 00:18:46 +01002854 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
Moran Pekerded84402018-06-06 16:36:50 +03002855
Gilles Peskinebd7dea92018-09-27 13:57:19 +02002856 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
Moran Pekerded84402018-06-06 16:36:50 +03002857
2858exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02002859 psa_cipher_abort( &operation1 );
2860 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03002861 mbedtls_free( output1 );
2862 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02002863 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02002864 PSA_DONE( );
Moran Pekerded84402018-06-06 16:36:50 +03002865}
2866/* END_CASE */
2867
2868/* BEGIN_CASE */
Gilles Peskine50e586b2018-06-08 14:28:46 +02002869void cipher_verify_output_multipart( int alg_arg,
2870 int key_type_arg,
Ronald Cron5425a212020-08-04 14:58:35 +02002871 data_t *key_data,
itayzafrir3e02b3b2018-06-12 17:06:52 +03002872 data_t *input,
Gilles Peskinee0866522019-02-19 19:44:00 +01002873 int first_part_size_arg )
Moran Pekerded84402018-06-06 16:36:50 +03002874{
Ronald Cron5425a212020-08-04 14:58:35 +02002875 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002876 psa_key_type_t key_type = key_type_arg;
2877 psa_algorithm_t alg = alg_arg;
Gilles Peskinee0866522019-02-19 19:44:00 +01002878 size_t first_part_size = first_part_size_arg;
Moran Pekerded84402018-06-06 16:36:50 +03002879 unsigned char iv[16] = {0};
2880 size_t iv_size = 16;
2881 size_t iv_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002882 unsigned char *output1 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002883 size_t output1_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002884 size_t output1_length = 0;
itayzafrir3e02b3b2018-06-12 17:06:52 +03002885 unsigned char *output2 = NULL;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002886 size_t output2_buffer_size = 0;
Moran Pekerded84402018-06-06 16:36:50 +03002887 size_t output2_length = 0;
Gilles Peskine048b7f02018-06-08 14:20:49 +02002888 size_t function_output_length;
Jaeden Amero5bae2272019-01-04 11:48:27 +00002889 psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT;
2890 psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002891 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Moran Pekerded84402018-06-06 16:36:50 +03002892
Gilles Peskine8817f612018-12-18 00:18:46 +01002893 PSA_ASSERT( psa_crypto_init( ) );
Moran Pekerded84402018-06-06 16:36:50 +03002894
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02002895 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
2896 psa_set_key_algorithm( &attributes, alg );
2897 psa_set_key_type( &attributes, key_type );
Moran Pekered346952018-07-05 15:22:45 +03002898
Ronald Cron5425a212020-08-04 14:58:35 +02002899 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
2900 &key ) );
Moran Pekerded84402018-06-06 16:36:50 +03002901
Ronald Cron5425a212020-08-04 14:58:35 +02002902 PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) );
2903 PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) );
Moran Pekerded84402018-06-06 16:36:50 +03002904
Steven Cooreman177deba2020-09-07 17:14:14 +02002905 if( alg != PSA_ALG_ECB_NO_PADDING )
2906 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002907 PSA_ASSERT( psa_cipher_generate_iv( &operation1,
2908 iv, iv_size,
2909 &iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002910 }
2911
gabor-mezei-armceface22021-01-21 12:26:17 +01002912 output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len );
2913 TEST_ASSERT( output1_buffer_size <=
2914 PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002915 ASSERT_ALLOC( output1, output1_buffer_size );
Moran Pekerded84402018-06-06 16:36:50 +03002916
Gilles Peskinee0866522019-02-19 19:44:00 +01002917 TEST_ASSERT( first_part_size <= input->len );
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002918
Gilles Peskine8817f612018-12-18 00:18:46 +01002919 PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size,
2920 output1, output1_buffer_size,
2921 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002922 TEST_ASSERT( function_output_length <=
2923 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2924 TEST_ASSERT( function_output_length <=
2925 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002926 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002927
Gilles Peskine8817f612018-12-18 00:18:46 +01002928 PSA_ASSERT( psa_cipher_update( &operation1,
2929 input->x + first_part_size,
2930 input->len - first_part_size,
2931 output1, output1_buffer_size,
2932 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002933 TEST_ASSERT( function_output_length <=
2934 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2935 alg,
2936 input->len - first_part_size ) );
2937 TEST_ASSERT( function_output_length <=
2938 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002939 output1_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002940
Gilles Peskine8817f612018-12-18 00:18:46 +01002941 PSA_ASSERT( psa_cipher_finish( &operation1,
2942 output1 + output1_length,
2943 output1_buffer_size - output1_length,
2944 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002945 TEST_ASSERT( function_output_length <=
2946 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2947 TEST_ASSERT( function_output_length <=
2948 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002949 output1_length += function_output_length;
mohammad1603d7d7ba52018-03-12 18:51:53 +02002950
Gilles Peskine8817f612018-12-18 00:18:46 +01002951 PSA_ASSERT( psa_cipher_abort( &operation1 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002952
Gilles Peskine048b7f02018-06-08 14:20:49 +02002953 output2_buffer_size = output1_length;
gabor-mezei-armceface22021-01-21 12:26:17 +01002954 TEST_ASSERT( output2_buffer_size <=
2955 PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) );
2956 TEST_ASSERT( output2_buffer_size <=
2957 PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02002958 ASSERT_ALLOC( output2, output2_buffer_size );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002959
Steven Cooreman177deba2020-09-07 17:14:14 +02002960 if( iv_length > 0 )
2961 {
Steven Cooremana6033e92020-08-25 11:47:50 +02002962 PSA_ASSERT( psa_cipher_set_iv( &operation2,
2963 iv, iv_length ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002964 }
Moran Pekerded84402018-06-06 16:36:50 +03002965
Gilles Peskine8817f612018-12-18 00:18:46 +01002966 PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size,
2967 output2, output2_buffer_size,
2968 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002969 TEST_ASSERT( function_output_length <=
2970 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) );
2971 TEST_ASSERT( function_output_length <=
2972 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002973 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002974
Gilles Peskine8817f612018-12-18 00:18:46 +01002975 PSA_ASSERT( psa_cipher_update( &operation2,
2976 output1 + first_part_size,
2977 output1_length - first_part_size,
2978 output2, output2_buffer_size,
2979 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002980 TEST_ASSERT( function_output_length <=
2981 PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type,
2982 alg,
2983 output1_length - first_part_size ) );
2984 TEST_ASSERT( function_output_length <=
2985 PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002986 output2_length += function_output_length;
Moran Pekerded84402018-06-06 16:36:50 +03002987
Gilles Peskine8817f612018-12-18 00:18:46 +01002988 PSA_ASSERT( psa_cipher_finish( &operation2,
2989 output2 + output2_length,
2990 output2_buffer_size - output2_length,
2991 &function_output_length ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01002992 TEST_ASSERT( function_output_length <=
2993 PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) );
2994 TEST_ASSERT( function_output_length <=
2995 PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE );
Gilles Peskine048b7f02018-06-08 14:20:49 +02002996 output2_length += function_output_length;
Gilles Peskine4ca9c3f2018-06-06 18:44:09 +02002997
Gilles Peskine8817f612018-12-18 00:18:46 +01002998 PSA_ASSERT( psa_cipher_abort( &operation2 ) );
mohammad1603d7d7ba52018-03-12 18:51:53 +02002999
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003000 ASSERT_COMPARE( input->x, input->len, output2, output2_length );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003001
3002exit:
Gilles Peskine64f13ef2020-08-25 23:15:20 +02003003 psa_cipher_abort( &operation1 );
3004 psa_cipher_abort( &operation2 );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003005 mbedtls_free( output1 );
3006 mbedtls_free( output2 );
Ronald Cron5425a212020-08-04 14:58:35 +02003007 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003008 PSA_DONE( );
mohammad1603d7d7ba52018-03-12 18:51:53 +02003009}
3010/* END_CASE */
Gilles Peskine7268afc2018-06-06 15:19:24 +02003011
Gilles Peskine20035e32018-02-03 22:44:14 +01003012/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003013void aead_encrypt_decrypt( int key_type_arg, data_t *key_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003014 int alg_arg,
Gilles Peskine7da96b02018-08-17 18:45:42 +02003015 data_t *nonce,
3016 data_t *additional_data,
3017 data_t *input_data,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003018 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003019{
Ronald Cron5425a212020-08-04 14:58:35 +02003020 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003021 psa_key_type_t key_type = key_type_arg;
3022 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003023 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003024 unsigned char *output_data = NULL;
3025 size_t output_size = 0;
3026 size_t output_length = 0;
3027 unsigned char *output_data2 = NULL;
3028 size_t output_length2 = 0;
Steven Cooremanf49478b2021-02-15 15:19:25 +01003029 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003030 psa_status_t expected_result = expected_result_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003031 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003032
Gilles Peskine8817f612018-12-18 00:18:46 +01003033 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003034
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003035 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3036 psa_set_key_algorithm( &attributes, alg );
3037 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003038
Gilles Peskine049c7532019-05-15 20:22:09 +02003039 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003040 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003041 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3042 key_bits = psa_get_key_bits( &attributes );
3043
3044 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3045 alg );
3046 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3047 * should be exact. */
3048 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3049 expected_result != PSA_ERROR_NOT_SUPPORTED )
3050 {
3051 TEST_EQUAL( output_size,
3052 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3053 TEST_ASSERT( output_size <=
3054 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3055 }
3056 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003057
Steven Cooremanf49478b2021-02-15 15:19:25 +01003058 status = psa_aead_encrypt( key, alg,
3059 nonce->x, nonce->len,
3060 additional_data->x,
3061 additional_data->len,
3062 input_data->x, input_data->len,
3063 output_data, output_size,
3064 &output_length );
3065
3066 /* If the operation is not supported, just skip and not fail in case the
3067 * encryption involves a common limitation of cryptography hardwares and
3068 * an alternative implementation. */
3069 if( status == PSA_ERROR_NOT_SUPPORTED )
3070 {
3071 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3072 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
3073 }
3074
3075 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003076
3077 if( PSA_SUCCESS == expected_result )
3078 {
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003079 ASSERT_ALLOC( output_data2, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003080
Gilles Peskine003a4a92019-05-14 16:09:40 +02003081 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3082 * should be exact. */
3083 TEST_EQUAL( input_data->len,
Bence Szépkútiec174e22021-03-19 18:46:15 +01003084 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) );
Gilles Peskine003a4a92019-05-14 16:09:40 +02003085
gabor-mezei-armceface22021-01-21 12:26:17 +01003086 TEST_ASSERT( input_data->len <=
3087 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) );
3088
Ronald Cron5425a212020-08-04 14:58:35 +02003089 TEST_EQUAL( psa_aead_decrypt( key, alg,
Gilles Peskinefe11b722018-12-18 00:24:04 +01003090 nonce->x, nonce->len,
3091 additional_data->x,
3092 additional_data->len,
3093 output_data, output_length,
3094 output_data2, output_length,
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003095 &output_length2 ),
3096 expected_result );
Gilles Peskine2d277862018-06-18 15:41:12 +02003097
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003098 ASSERT_COMPARE( input_data->x, input_data->len,
3099 output_data2, output_length2 );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003100 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003101
Gilles Peskinea1cac842018-06-11 19:33:02 +02003102exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003103 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003104 mbedtls_free( output_data );
3105 mbedtls_free( output_data2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003106 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003107}
3108/* END_CASE */
3109
3110/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003111void aead_encrypt( int key_type_arg, data_t *key_data,
3112 int alg_arg,
3113 data_t *nonce,
3114 data_t *additional_data,
3115 data_t *input_data,
3116 data_t *expected_result )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003117{
Ronald Cron5425a212020-08-04 14:58:35 +02003118 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003119 psa_key_type_t key_type = key_type_arg;
3120 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003121 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003122 unsigned char *output_data = NULL;
3123 size_t output_size = 0;
3124 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003125 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Steven Cooremand588ea12021-01-11 19:36:04 +01003126 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003127
Gilles Peskine8817f612018-12-18 00:18:46 +01003128 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003129
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003130 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3131 psa_set_key_algorithm( &attributes, alg );
3132 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003133
Gilles Peskine049c7532019-05-15 20:22:09 +02003134 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003135 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003136 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3137 key_bits = psa_get_key_bits( &attributes );
3138
3139 output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3140 alg );
3141 /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE
3142 * should be exact. */
3143 TEST_EQUAL( output_size,
3144 PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3145 TEST_ASSERT( output_size <=
3146 PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3147 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003148
Steven Cooremand588ea12021-01-11 19:36:04 +01003149 status = psa_aead_encrypt( key, alg,
3150 nonce->x, nonce->len,
3151 additional_data->x, additional_data->len,
3152 input_data->x, input_data->len,
3153 output_data, output_size,
3154 &output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003155
Ronald Cron28a45ed2021-02-09 20:35:42 +01003156 /* If the operation is not supported, just skip and not fail in case the
3157 * encryption involves a common limitation of cryptography hardwares and
3158 * an alternative implementation. */
3159 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003160 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003161 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3162 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003163 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003164
3165 PSA_ASSERT( status );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003166 ASSERT_COMPARE( expected_result->x, expected_result->len,
3167 output_data, output_length );
Gilles Peskine2d277862018-06-18 15:41:12 +02003168
Gilles Peskinea1cac842018-06-11 19:33:02 +02003169exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003170 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003171 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003172 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003173}
3174/* END_CASE */
3175
3176/* BEGIN_CASE */
Gilles Peskine7da96b02018-08-17 18:45:42 +02003177void aead_decrypt( int key_type_arg, data_t *key_data,
3178 int alg_arg,
3179 data_t *nonce,
3180 data_t *additional_data,
3181 data_t *input_data,
3182 data_t *expected_data,
3183 int expected_result_arg )
Gilles Peskinea1cac842018-06-11 19:33:02 +02003184{
Ronald Cron5425a212020-08-04 14:58:35 +02003185 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003186 psa_key_type_t key_type = key_type_arg;
3187 psa_algorithm_t alg = alg_arg;
Bence Szépkútiec174e22021-03-19 18:46:15 +01003188 size_t key_bits;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003189 unsigned char *output_data = NULL;
3190 size_t output_size = 0;
3191 size_t output_length = 0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003192 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine4abf7412018-06-18 16:35:34 +02003193 psa_status_t expected_result = expected_result_arg;
Steven Cooremand588ea12021-01-11 19:36:04 +01003194 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinea1cac842018-06-11 19:33:02 +02003195
Gilles Peskine8817f612018-12-18 00:18:46 +01003196 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003197
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003198 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3199 psa_set_key_algorithm( &attributes, alg );
3200 psa_set_key_type( &attributes, key_type );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003201
Gilles Peskine049c7532019-05-15 20:22:09 +02003202 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003203 &key ) );
Bence Szépkútiec174e22021-03-19 18:46:15 +01003204 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3205 key_bits = psa_get_key_bits( &attributes );
3206
3207 output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits,
3208 alg );
3209 if( expected_result != PSA_ERROR_INVALID_ARGUMENT &&
3210 expected_result != PSA_ERROR_NOT_SUPPORTED )
3211 {
3212 /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE
3213 * should be exact. */
3214 TEST_EQUAL( output_size,
3215 PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) );
3216 TEST_ASSERT( output_size <=
3217 PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) );
3218 }
3219 ASSERT_ALLOC( output_data, output_size );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003220
Steven Cooremand588ea12021-01-11 19:36:04 +01003221 status = psa_aead_decrypt( key, alg,
3222 nonce->x, nonce->len,
3223 additional_data->x,
3224 additional_data->len,
3225 input_data->x, input_data->len,
3226 output_data, output_size,
3227 &output_length );
3228
Ronald Cron28a45ed2021-02-09 20:35:42 +01003229 /* If the operation is not supported, just skip and not fail in case the
3230 * decryption involves a common limitation of cryptography hardwares and
3231 * an alternative implementation. */
3232 if( status == PSA_ERROR_NOT_SUPPORTED )
Steven Cooremand588ea12021-01-11 19:36:04 +01003233 {
Ronald Cron28a45ed2021-02-09 20:35:42 +01003234 MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 );
3235 MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len );
Steven Cooremand588ea12021-01-11 19:36:04 +01003236 }
Steven Cooremand588ea12021-01-11 19:36:04 +01003237
3238 TEST_EQUAL( status, expected_result );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003239
Gilles Peskine2d277862018-06-18 15:41:12 +02003240 if( expected_result == PSA_SUCCESS )
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003241 ASSERT_COMPARE( expected_data->x, expected_data->len,
3242 output_data, output_length );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003243
Gilles Peskinea1cac842018-06-11 19:33:02 +02003244exit:
Ronald Cron5425a212020-08-04 14:58:35 +02003245 psa_destroy_key( key );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003246 mbedtls_free( output_data );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003247 PSA_DONE( );
Gilles Peskinea1cac842018-06-11 19:33:02 +02003248}
3249/* END_CASE */
3250
3251/* BEGIN_CASE */
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003252void signature_size( int type_arg,
3253 int bits,
3254 int alg_arg,
3255 int expected_size_arg )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003256{
3257 psa_key_type_t type = type_arg;
3258 psa_algorithm_t alg = alg_arg;
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003259 size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003260
Gilles Peskinefe11b722018-12-18 00:24:04 +01003261 TEST_EQUAL( actual_size, (size_t) expected_size_arg );
Gilles Peskine841b14b2019-11-26 17:37:37 +01003262
Gilles Peskinee59236f2018-01-27 23:32:46 +01003263exit:
3264 ;
3265}
3266/* END_CASE */
3267
3268/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003269void sign_hash_deterministic( int key_type_arg, data_t *key_data,
3270 int alg_arg, data_t *input_data,
3271 data_t *output_data )
Gilles Peskinee59236f2018-01-27 23:32:46 +01003272{
Ronald Cron5425a212020-08-04 14:58:35 +02003273 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinee59236f2018-01-27 23:32:46 +01003274 psa_key_type_t key_type = key_type_arg;
3275 psa_algorithm_t alg = alg_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003276 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +01003277 unsigned char *signature = NULL;
3278 size_t signature_size;
3279 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003280 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003281
Gilles Peskine8817f612018-12-18 00:18:46 +01003282 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003283
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003284 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003285 psa_set_key_algorithm( &attributes, alg );
3286 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003287
Gilles Peskine049c7532019-05-15 20:22:09 +02003288 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003289 &key ) );
3290 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003291 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine20035e32018-02-03 22:44:14 +01003292
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003293 /* Allocate a buffer which has the size advertized by the
3294 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003295 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003296 key_bits, alg );
Gilles Peskine20035e32018-02-03 22:44:14 +01003297 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003298 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003299 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003300
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003301 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003302 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003303 input_data->x, input_data->len,
3304 signature, signature_size,
3305 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003306 /* Verify that the signature is what is expected. */
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003307 ASSERT_COMPARE( output_data->x, output_data->len,
3308 signature, signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003309
3310exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003311 /*
3312 * Key attributes may have been returned by psa_get_key_attributes()
3313 * thus reset them as required.
3314 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003315 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003316
Ronald Cron5425a212020-08-04 14:58:35 +02003317 psa_destroy_key( key );
Gilles Peskine0189e752018-02-03 23:57:22 +01003318 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003319 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003320}
3321/* END_CASE */
3322
3323/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003324void sign_hash_fail( int key_type_arg, data_t *key_data,
3325 int alg_arg, data_t *input_data,
3326 int signature_size_arg, int expected_status_arg )
Gilles Peskine20035e32018-02-03 22:44:14 +01003327{
Ronald Cron5425a212020-08-04 14:58:35 +02003328 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003329 psa_key_type_t key_type = key_type_arg;
3330 psa_algorithm_t alg = alg_arg;
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003331 size_t signature_size = signature_size_arg;
Gilles Peskine20035e32018-02-03 22:44:14 +01003332 psa_status_t actual_status;
3333 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +01003334 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +01003335 size_t signature_length = 0xdeadbeef;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003336 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine20035e32018-02-03 22:44:14 +01003337
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003338 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003339
Gilles Peskine8817f612018-12-18 00:18:46 +01003340 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003341
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003342 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003343 psa_set_key_algorithm( &attributes, alg );
3344 psa_set_key_type( &attributes, key_type );
mohammad1603a97cb8c2018-03-28 03:46:26 -07003345
Gilles Peskine049c7532019-05-15 20:22:09 +02003346 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003347 &key ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01003348
Ronald Cron5425a212020-08-04 14:58:35 +02003349 actual_status = psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003350 input_data->x, input_data->len,
3351 signature, signature_size,
3352 &signature_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003353 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine860ce9d2018-06-28 12:23:00 +02003354 /* The value of *signature_length is unspecified on error, but
3355 * whatever it is, it should be less than signature_size, so that
3356 * if the caller tries to read *signature_length bytes without
3357 * checking the error code then they don't overflow a buffer. */
3358 TEST_ASSERT( signature_length <= signature_size );
Gilles Peskine20035e32018-02-03 22:44:14 +01003359
3360exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003361 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003362 psa_destroy_key( key );
Gilles Peskine20035e32018-02-03 22:44:14 +01003363 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003364 PSA_DONE( );
Gilles Peskine20035e32018-02-03 22:44:14 +01003365}
3366/* END_CASE */
mohammad16038cc1cee2018-03-28 01:21:33 +03003367
3368/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003369void sign_verify_hash( int key_type_arg, data_t *key_data,
3370 int alg_arg, data_t *input_data )
Gilles Peskine9911b022018-06-29 17:30:48 +02003371{
Ronald Cron5425a212020-08-04 14:58:35 +02003372 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003373 psa_key_type_t key_type = key_type_arg;
3374 psa_algorithm_t alg = alg_arg;
3375 size_t key_bits;
3376 unsigned char *signature = NULL;
3377 size_t signature_size;
3378 size_t signature_length = 0xdeadbeef;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003379 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine9911b022018-06-29 17:30:48 +02003380
Gilles Peskine8817f612018-12-18 00:18:46 +01003381 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003382
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003383 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003384 psa_set_key_algorithm( &attributes, alg );
3385 psa_set_key_type( &attributes, key_type );
Gilles Peskine9911b022018-06-29 17:30:48 +02003386
Gilles Peskine049c7532019-05-15 20:22:09 +02003387 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003388 &key ) );
3389 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003390 key_bits = psa_get_key_bits( &attributes );
Gilles Peskine9911b022018-06-29 17:30:48 +02003391
3392 /* Allocate a buffer which has the size advertized by the
3393 * library. */
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003394 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type,
Gilles Peskine9911b022018-06-29 17:30:48 +02003395 key_bits, alg );
3396 TEST_ASSERT( signature_size != 0 );
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003397 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003398 ASSERT_ALLOC( signature, signature_size );
Gilles Peskine9911b022018-06-29 17:30:48 +02003399
3400 /* Perform the signature. */
Ronald Cron5425a212020-08-04 14:58:35 +02003401 PSA_ASSERT( psa_sign_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003402 input_data->x, input_data->len,
3403 signature, signature_size,
3404 &signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003405 /* Check that the signature length looks sensible. */
3406 TEST_ASSERT( signature_length <= signature_size );
3407 TEST_ASSERT( signature_length > 0 );
3408
3409 /* Use the library to verify that the signature is correct. */
Ronald Cron5425a212020-08-04 14:58:35 +02003410 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003411 input_data->x, input_data->len,
3412 signature, signature_length ) );
Gilles Peskine9911b022018-06-29 17:30:48 +02003413
3414 if( input_data->len != 0 )
3415 {
3416 /* Flip a bit in the input and verify that the signature is now
3417 * detected as invalid. Flip a bit at the beginning, not at the end,
3418 * because ECDSA may ignore the last few bits of the input. */
3419 input_data->x[0] ^= 1;
Ronald Cron5425a212020-08-04 14:58:35 +02003420 TEST_EQUAL( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003421 input_data->x, input_data->len,
3422 signature, signature_length ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01003423 PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine9911b022018-06-29 17:30:48 +02003424 }
3425
3426exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003427 /*
3428 * Key attributes may have been returned by psa_get_key_attributes()
3429 * thus reset them as required.
3430 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003431 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003432
Ronald Cron5425a212020-08-04 14:58:35 +02003433 psa_destroy_key( key );
Gilles Peskine9911b022018-06-29 17:30:48 +02003434 mbedtls_free( signature );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003435 PSA_DONE( );
Gilles Peskine9911b022018-06-29 17:30:48 +02003436}
3437/* END_CASE */
3438
3439/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003440void verify_hash( int key_type_arg, data_t *key_data,
3441 int alg_arg, data_t *hash_data,
3442 data_t *signature_data )
itayzafrir5c753392018-05-08 11:18:38 +03003443{
Ronald Cron5425a212020-08-04 14:58:35 +02003444 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003445 psa_key_type_t key_type = key_type_arg;
3446 psa_algorithm_t alg = alg_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003447 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
itayzafrir5c753392018-05-08 11:18:38 +03003448
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003449 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
Gilles Peskine69c12672018-06-28 00:07:19 +02003450
Gilles Peskine8817f612018-12-18 00:18:46 +01003451 PSA_ASSERT( psa_crypto_init( ) );
itayzafrir5c753392018-05-08 11:18:38 +03003452
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003453 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003454 psa_set_key_algorithm( &attributes, alg );
3455 psa_set_key_type( &attributes, key_type );
itayzafrir5c753392018-05-08 11:18:38 +03003456
Gilles Peskine049c7532019-05-15 20:22:09 +02003457 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003458 &key ) );
itayzafrir5c753392018-05-08 11:18:38 +03003459
Ronald Cron5425a212020-08-04 14:58:35 +02003460 PSA_ASSERT( psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003461 hash_data->x, hash_data->len,
3462 signature_data->x, signature_data->len ) );
Gilles Peskine0627f982019-11-26 19:12:16 +01003463
itayzafrir5c753392018-05-08 11:18:38 +03003464exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003465 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003466 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003467 PSA_DONE( );
itayzafrir5c753392018-05-08 11:18:38 +03003468}
3469/* END_CASE */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003470
3471/* BEGIN_CASE */
gabor-mezei-armb9530232021-04-16 14:21:21 +02003472void verify_hash_fail( int key_type_arg, data_t *key_data,
3473 int alg_arg, data_t *hash_data,
3474 data_t *signature_data,
3475 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003476{
Ronald Cron5425a212020-08-04 14:58:35 +02003477 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003478 psa_key_type_t key_type = key_type_arg;
3479 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003480 psa_status_t actual_status;
3481 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003482 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003483
Gilles Peskine8817f612018-12-18 00:18:46 +01003484 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003485
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003486 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003487 psa_set_key_algorithm( &attributes, alg );
3488 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003489
Gilles Peskine049c7532019-05-15 20:22:09 +02003490 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003491 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003492
Ronald Cron5425a212020-08-04 14:58:35 +02003493 actual_status = psa_verify_hash( key, alg,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003494 hash_data->x, hash_data->len,
3495 signature_data->x, signature_data->len );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003496 TEST_EQUAL( actual_status, expected_status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003497
3498exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003499 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003500 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003501 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003502}
3503/* END_CASE */
3504
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003505/* BEGIN_CASE */
gabor-mezei-arm53028482021-04-15 18:19:50 +02003506void sign_message_deterministic( int key_type_arg,
3507 data_t *key_data,
3508 int alg_arg,
3509 data_t *input_data,
3510 data_t *output_data )
3511{
3512 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3513 psa_key_type_t key_type = key_type_arg;
3514 psa_algorithm_t alg = alg_arg;
3515 size_t key_bits;
3516 unsigned char *signature = NULL;
3517 size_t signature_size;
3518 size_t signature_length = 0xdeadbeef;
3519 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3520
3521 PSA_ASSERT( psa_crypto_init( ) );
3522
3523 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3524 psa_set_key_algorithm( &attributes, alg );
3525 psa_set_key_type( &attributes, key_type );
3526
3527 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3528 &key ) );
3529 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3530 key_bits = psa_get_key_bits( &attributes );
3531
3532 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3533 TEST_ASSERT( signature_size != 0 );
3534 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3535 ASSERT_ALLOC( signature, signature_size );
3536
3537 PSA_ASSERT( psa_sign_message( key, alg,
3538 input_data->x, input_data->len,
3539 signature, signature_size,
3540 &signature_length ) );
3541
3542 ASSERT_COMPARE( output_data->x, output_data->len,
3543 signature, signature_length );
3544
3545exit:
3546 psa_reset_key_attributes( &attributes );
3547
3548 psa_destroy_key( key );
3549 mbedtls_free( signature );
3550 PSA_DONE( );
3551
3552}
3553/* END_CASE */
3554
3555/* BEGIN_CASE */
3556void sign_message_fail( int key_type_arg,
3557 data_t *key_data,
3558 int alg_arg,
3559 data_t *input_data,
3560 int signature_size_arg,
3561 int expected_status_arg )
3562{
3563 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3564 psa_key_type_t key_type = key_type_arg;
3565 psa_algorithm_t alg = alg_arg;
3566 size_t signature_size = signature_size_arg;
3567 psa_status_t actual_status;
3568 psa_status_t expected_status = expected_status_arg;
3569 unsigned char *signature = NULL;
3570 size_t signature_length = 0xdeadbeef;
3571 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3572
3573 ASSERT_ALLOC( signature, signature_size );
3574
3575 PSA_ASSERT( psa_crypto_init( ) );
3576
3577 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
3578 psa_set_key_algorithm( &attributes, alg );
3579 psa_set_key_type( &attributes, key_type );
3580
3581 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3582 &key ) );
3583
3584 actual_status = psa_sign_message( key, alg,
3585 input_data->x, input_data->len,
3586 signature, signature_size,
3587 &signature_length );
3588 TEST_EQUAL( actual_status, expected_status );
3589 /* The value of *signature_length is unspecified on error, but
3590 * whatever it is, it should be less than signature_size, so that
3591 * if the caller tries to read *signature_length bytes without
3592 * checking the error code then they don't overflow a buffer. */
3593 TEST_ASSERT( signature_length <= signature_size );
3594
3595exit:
3596 psa_reset_key_attributes( &attributes );
3597 psa_destroy_key( key );
3598 mbedtls_free( signature );
3599 PSA_DONE( );
3600}
3601/* END_CASE */
3602
3603/* BEGIN_CASE */
3604void sign_verify_message( int key_type_arg,
3605 data_t *key_data,
3606 int alg_arg,
3607 data_t *input_data )
3608{
3609 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3610 psa_key_type_t key_type = key_type_arg;
3611 psa_algorithm_t alg = alg_arg;
3612 size_t key_bits;
3613 unsigned char *signature = NULL;
3614 size_t signature_size;
3615 size_t signature_length = 0xdeadbeef;
3616 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3617
3618 PSA_ASSERT( psa_crypto_init( ) );
3619
3620 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE |
3621 PSA_KEY_USAGE_VERIFY_MESSAGE );
3622 psa_set_key_algorithm( &attributes, alg );
3623 psa_set_key_type( &attributes, key_type );
3624
3625 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3626 &key ) );
3627 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3628 key_bits = psa_get_key_bits( &attributes );
3629
3630 signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg );
3631 TEST_ASSERT( signature_size != 0 );
3632 TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE );
3633 ASSERT_ALLOC( signature, signature_size );
3634
3635 PSA_ASSERT( psa_sign_message( key, alg,
3636 input_data->x, input_data->len,
3637 signature, signature_size,
3638 &signature_length ) );
3639 TEST_ASSERT( signature_length <= signature_size );
3640 TEST_ASSERT( signature_length > 0 );
3641
3642 PSA_ASSERT( psa_verify_message( key, alg,
3643 input_data->x, input_data->len,
3644 signature, signature_length ) );
3645
3646 if( input_data->len != 0 )
3647 {
3648 /* Flip a bit in the input and verify that the signature is now
3649 * detected as invalid. Flip a bit at the beginning, not at the end,
3650 * because ECDSA may ignore the last few bits of the input. */
3651 input_data->x[0] ^= 1;
3652 TEST_EQUAL( psa_verify_message( key, alg,
3653 input_data->x, input_data->len,
3654 signature, signature_length ),
3655 PSA_ERROR_INVALID_SIGNATURE );
3656 }
3657
3658exit:
3659 psa_reset_key_attributes( &attributes );
3660
3661 psa_destroy_key( key );
3662 mbedtls_free( signature );
3663 PSA_DONE( );
3664}
3665/* END_CASE */
3666
3667/* BEGIN_CASE */
3668void verify_message( int key_type_arg,
3669 data_t *key_data,
3670 int alg_arg,
3671 data_t *input_data,
3672 data_t *signature_data )
3673{
3674 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3675 psa_key_type_t key_type = key_type_arg;
3676 psa_algorithm_t alg = alg_arg;
3677 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3678
3679 TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE );
3680
3681 PSA_ASSERT( psa_crypto_init( ) );
3682
3683 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3684 psa_set_key_algorithm( &attributes, alg );
3685 psa_set_key_type( &attributes, key_type );
3686
3687 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3688 &key ) );
3689
3690 PSA_ASSERT( psa_verify_message( key, alg,
3691 input_data->x, input_data->len,
3692 signature_data->x, signature_data->len ) );
3693
3694exit:
3695 psa_reset_key_attributes( &attributes );
3696 psa_destroy_key( key );
3697 PSA_DONE( );
3698}
3699/* END_CASE */
3700
3701/* BEGIN_CASE */
3702void verify_message_fail( int key_type_arg,
3703 data_t *key_data,
3704 int alg_arg,
3705 data_t *hash_data,
3706 data_t *signature_data,
3707 int expected_status_arg )
3708{
3709 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
3710 psa_key_type_t key_type = key_type_arg;
3711 psa_algorithm_t alg = alg_arg;
3712 psa_status_t actual_status;
3713 psa_status_t expected_status = expected_status_arg;
3714 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
3715
3716 PSA_ASSERT( psa_crypto_init( ) );
3717
3718 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE );
3719 psa_set_key_algorithm( &attributes, alg );
3720 psa_set_key_type( &attributes, key_type );
3721
3722 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
3723 &key ) );
3724
3725 actual_status = psa_verify_message( key, alg,
3726 hash_data->x, hash_data->len,
3727 signature_data->x,
3728 signature_data->len );
3729 TEST_EQUAL( actual_status, expected_status );
3730
3731exit:
3732 psa_reset_key_attributes( &attributes );
3733 psa_destroy_key( key );
3734 PSA_DONE( );
3735}
3736/* END_CASE */
3737
3738/* BEGIN_CASE */
Gilles Peskine656896e2018-06-29 19:12:28 +02003739void asymmetric_encrypt( int key_type_arg,
3740 data_t *key_data,
3741 int alg_arg,
3742 data_t *input_data,
Gilles Peskine68428122018-06-30 18:42:41 +02003743 data_t *label,
Gilles Peskine656896e2018-06-29 19:12:28 +02003744 int expected_output_length_arg,
3745 int expected_status_arg )
3746{
Ronald Cron5425a212020-08-04 14:58:35 +02003747 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003748 psa_key_type_t key_type = key_type_arg;
3749 psa_algorithm_t alg = alg_arg;
3750 size_t expected_output_length = expected_output_length_arg;
3751 size_t key_bits;
3752 unsigned char *output = NULL;
3753 size_t output_size;
3754 size_t output_length = ~0;
3755 psa_status_t actual_status;
3756 psa_status_t expected_status = expected_status_arg;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003757 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine656896e2018-06-29 19:12:28 +02003758
Gilles Peskine8817f612018-12-18 00:18:46 +01003759 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskinebdf309c2018-12-03 15:36:32 +01003760
Gilles Peskine656896e2018-06-29 19:12:28 +02003761 /* Import the key */
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003762 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
3763 psa_set_key_algorithm( &attributes, alg );
3764 psa_set_key_type( &attributes, key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02003765 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003766 &key ) );
Gilles Peskine656896e2018-06-29 19:12:28 +02003767
3768 /* Determine the maximum output length */
Ronald Cron5425a212020-08-04 14:58:35 +02003769 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003770 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003771
Gilles Peskine656896e2018-06-29 19:12:28 +02003772 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003773 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003774 ASSERT_ALLOC( output, output_size );
Gilles Peskine656896e2018-06-29 19:12:28 +02003775
3776 /* Encrypt the input */
Ronald Cron5425a212020-08-04 14:58:35 +02003777 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine656896e2018-06-29 19:12:28 +02003778 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003779 label->x, label->len,
Gilles Peskine656896e2018-06-29 19:12:28 +02003780 output, output_size,
3781 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003782 TEST_EQUAL( actual_status, expected_status );
3783 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine656896e2018-06-29 19:12:28 +02003784
Gilles Peskine68428122018-06-30 18:42:41 +02003785 /* If the label is empty, the test framework puts a non-null pointer
3786 * in label->x. Test that a null pointer works as well. */
3787 if( label->len == 0 )
3788 {
3789 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003790 if( output_size != 0 )
3791 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003792 actual_status = psa_asymmetric_encrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02003793 input_data->x, input_data->len,
3794 NULL, label->len,
3795 output, output_size,
3796 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003797 TEST_EQUAL( actual_status, expected_status );
3798 TEST_EQUAL( output_length, expected_output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003799 }
3800
Gilles Peskine656896e2018-06-29 19:12:28 +02003801exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003802 /*
3803 * Key attributes may have been returned by psa_get_key_attributes()
3804 * thus reset them as required.
3805 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003806 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003807
Ronald Cron5425a212020-08-04 14:58:35 +02003808 psa_destroy_key( key );
Gilles Peskine656896e2018-06-29 19:12:28 +02003809 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003810 PSA_DONE( );
Gilles Peskine656896e2018-06-29 19:12:28 +02003811}
3812/* END_CASE */
3813
3814/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003815void asymmetric_encrypt_decrypt( int key_type_arg,
3816 data_t *key_data,
3817 int alg_arg,
3818 data_t *input_data,
3819 data_t *label )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003820{
Ronald Cron5425a212020-08-04 14:58:35 +02003821 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003822 psa_key_type_t key_type = key_type_arg;
3823 psa_algorithm_t alg = alg_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003824 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003825 unsigned char *output = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003826 size_t output_size;
3827 size_t output_length = ~0;
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003828 unsigned char *output2 = NULL;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003829 size_t output2_size;
3830 size_t output2_length = ~0;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003831 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003832
Gilles Peskine8817f612018-12-18 00:18:46 +01003833 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003834
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003835 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
3836 psa_set_key_algorithm( &attributes, alg );
3837 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003838
Gilles Peskine049c7532019-05-15 20:22:09 +02003839 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003840 &key ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003841
3842 /* Determine the maximum ciphertext length */
Ronald Cron5425a212020-08-04 14:58:35 +02003843 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02003844 key_bits = psa_get_key_bits( &attributes );
gabor-mezei-armceface22021-01-21 12:26:17 +01003845
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003846 output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
gabor-mezei-armceface22021-01-21 12:26:17 +01003847 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003848 ASSERT_ALLOC( output, output_size );
gabor-mezei-armceface22021-01-21 12:26:17 +01003849
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003850 output2_size = input_data->len;
gabor-mezei-armceface22021-01-21 12:26:17 +01003851 TEST_ASSERT( output2_size <=
3852 PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) );
3853 TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003854 ASSERT_ALLOC( output2, output2_size );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003855
Gilles Peskineeebd7382018-06-08 18:11:54 +02003856 /* We test encryption by checking that encrypt-then-decrypt gives back
3857 * the original plaintext because of the non-optional random
3858 * part of encryption process which prevents using fixed vectors. */
Ronald Cron5425a212020-08-04 14:58:35 +02003859 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003860 input_data->x, input_data->len,
3861 label->x, label->len,
3862 output, output_size,
3863 &output_length ) );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003864 /* We don't know what ciphertext length to expect, but check that
3865 * it looks sensible. */
3866 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein0f3bdbd2018-05-02 23:56:12 +03003867
Ronald Cron5425a212020-08-04 14:58:35 +02003868 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003869 output, output_length,
3870 label->x, label->len,
3871 output2, output2_size,
3872 &output2_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003873 ASSERT_COMPARE( input_data->x, input_data->len,
3874 output2, output2_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003875
3876exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003877 /*
3878 * Key attributes may have been returned by psa_get_key_attributes()
3879 * thus reset them as required.
3880 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003881 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01003882
Ronald Cron5425a212020-08-04 14:58:35 +02003883 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003884 mbedtls_free( output );
3885 mbedtls_free( output2 );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003886 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003887}
3888/* END_CASE */
3889
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003890/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003891void asymmetric_decrypt( int key_type_arg,
3892 data_t *key_data,
3893 int alg_arg,
3894 data_t *input_data,
3895 data_t *label,
Gilles Peskine66763a02018-06-29 21:54:10 +02003896 data_t *expected_data )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003897{
Ronald Cron5425a212020-08-04 14:58:35 +02003898 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003899 psa_key_type_t key_type = key_type_arg;
3900 psa_algorithm_t alg = alg_arg;
gabor-mezei-armceface22021-01-21 12:26:17 +01003901 size_t key_bits;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003902 unsigned char *output = NULL;
Nir Sonnenscheind70bc482018-06-04 16:31:13 +03003903 size_t output_size = 0;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003904 size_t output_length = ~0;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003905 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003906
Gilles Peskine8817f612018-12-18 00:18:46 +01003907 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003908
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003909 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3910 psa_set_key_algorithm( &attributes, alg );
3911 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003912
Gilles Peskine049c7532019-05-15 20:22:09 +02003913 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003914 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003915
gabor-mezei-armceface22021-01-21 12:26:17 +01003916 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
3917 key_bits = psa_get_key_bits( &attributes );
3918
3919 /* Determine the maximum ciphertext length */
3920 output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg );
3921 TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE );
3922 ASSERT_ALLOC( output, output_size );
3923
Ronald Cron5425a212020-08-04 14:58:35 +02003924 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003925 input_data->x, input_data->len,
3926 label->x, label->len,
3927 output,
3928 output_size,
3929 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003930 ASSERT_COMPARE( expected_data->x, expected_data->len,
3931 output, output_length );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003932
Gilles Peskine68428122018-06-30 18:42:41 +02003933 /* If the label is empty, the test framework puts a non-null pointer
3934 * in label->x. Test that a null pointer works as well. */
3935 if( label->len == 0 )
3936 {
3937 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02003938 if( output_size != 0 )
3939 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02003940 PSA_ASSERT( psa_asymmetric_decrypt( key, alg,
Gilles Peskine8817f612018-12-18 00:18:46 +01003941 input_data->x, input_data->len,
3942 NULL, label->len,
3943 output,
3944 output_size,
3945 &output_length ) );
Gilles Peskinebd7dea92018-09-27 13:57:19 +02003946 ASSERT_COMPARE( expected_data->x, expected_data->len,
3947 output, output_length );
Gilles Peskine68428122018-06-30 18:42:41 +02003948 }
3949
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003950exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02003951 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02003952 psa_destroy_key( key );
itayzafrir3e02b3b2018-06-12 17:06:52 +03003953 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02003954 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003955}
3956/* END_CASE */
3957
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003958/* BEGIN_CASE */
Gilles Peskine68428122018-06-30 18:42:41 +02003959void asymmetric_decrypt_fail( int key_type_arg,
3960 data_t *key_data,
3961 int alg_arg,
3962 data_t *input_data,
3963 data_t *label,
Jaeden Amerof8daab72019-02-06 12:57:46 +00003964 int output_size_arg,
Gilles Peskine2d277862018-06-18 15:41:12 +02003965 int expected_status_arg )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003966{
Ronald Cron5425a212020-08-04 14:58:35 +02003967 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003968 psa_key_type_t key_type = key_type_arg;
3969 psa_algorithm_t alg = alg_arg;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003970 unsigned char *output = NULL;
Jaeden Amerof8daab72019-02-06 12:57:46 +00003971 size_t output_size = output_size_arg;
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003972 size_t output_length = ~0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003973 psa_status_t actual_status;
3974 psa_status_t expected_status = expected_status_arg;
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003975 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003976
Gilles Peskine8cebbba2018-09-27 13:54:18 +02003977 ASSERT_ALLOC( output, output_size );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003978
Gilles Peskine8817f612018-12-18 00:18:46 +01003979 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003980
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02003981 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
3982 psa_set_key_algorithm( &attributes, alg );
3983 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheind7082602018-06-04 16:45:27 +03003984
Gilles Peskine049c7532019-05-15 20:22:09 +02003985 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02003986 &key ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003987
Ronald Cron5425a212020-08-04 14:58:35 +02003988 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003989 input_data->x, input_data->len,
Gilles Peskine68428122018-06-30 18:42:41 +02003990 label->x, label->len,
Gilles Peskine4abf7412018-06-18 16:35:34 +02003991 output, output_size,
Gilles Peskine2d277862018-06-18 15:41:12 +02003992 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01003993 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02003994 TEST_ASSERT( output_length <= output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003995
Gilles Peskine68428122018-06-30 18:42:41 +02003996 /* If the label is empty, the test framework puts a non-null pointer
3997 * in label->x. Test that a null pointer works as well. */
3998 if( label->len == 0 )
3999 {
4000 output_length = ~0;
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004001 if( output_size != 0 )
4002 memset( output, 0, output_size );
Ronald Cron5425a212020-08-04 14:58:35 +02004003 actual_status = psa_asymmetric_decrypt( key, alg,
Gilles Peskine68428122018-06-30 18:42:41 +02004004 input_data->x, input_data->len,
4005 NULL, label->len,
4006 output, output_size,
4007 &output_length );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004008 TEST_EQUAL( actual_status, expected_status );
Gilles Peskine55c94dd2018-06-30 18:54:48 +02004009 TEST_ASSERT( output_length <= output_size );
Gilles Peskine68428122018-06-30 18:42:41 +02004010 }
4011
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004012exit:
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004013 psa_reset_key_attributes( &attributes );
Ronald Cron5425a212020-08-04 14:58:35 +02004014 psa_destroy_key( key );
Gilles Peskine2d277862018-06-18 15:41:12 +02004015 mbedtls_free( output );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004016 PSA_DONE( );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004017}
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004018/* END_CASE */
Gilles Peskine05d69892018-06-19 22:00:52 +02004019
4020/* BEGIN_CASE */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004021void key_derivation_init( )
Jaeden Amerod94d6712019-01-04 14:11:48 +00004022{
4023 /* Test each valid way of initializing the object, except for `= {0}`, as
4024 * Clang 5 complains when `-Wmissing-field-initializers` is used, even
4025 * though it's OK by the C standard. We could test for this, but we'd need
4026 * to supress the Clang warning for the test. */
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004027 size_t capacity;
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004028 psa_key_derivation_operation_t func = psa_key_derivation_operation_init( );
4029 psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT;
4030 psa_key_derivation_operation_t zero;
Jaeden Amerod94d6712019-01-04 14:11:48 +00004031
4032 memset( &zero, 0, sizeof( zero ) );
4033
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004034 /* A default operation should not be able to report its capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004035 TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004036 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004037 TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004038 PSA_ERROR_BAD_STATE );
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004039 TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ),
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004040 PSA_ERROR_BAD_STATE );
Jaeden Amero5229bbb2019-02-07 16:33:37 +00004041
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004042 /* A default operation should be abortable without error. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004043 PSA_ASSERT( psa_key_derivation_abort(&func) );
4044 PSA_ASSERT( psa_key_derivation_abort(&init) );
4045 PSA_ASSERT( psa_key_derivation_abort(&zero) );
Jaeden Amerod94d6712019-01-04 14:11:48 +00004046}
4047/* END_CASE */
4048
Janos Follath16de4a42019-06-13 16:32:24 +01004049/* BEGIN_CASE */
4050void derive_setup( int alg_arg, int expected_status_arg )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004051{
Gilles Peskineea0fb492018-07-12 17:17:20 +02004052 psa_algorithm_t alg = alg_arg;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004053 psa_status_t expected_status = expected_status_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004054 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004055
Gilles Peskine8817f612018-12-18 00:18:46 +01004056 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004057
Janos Follath16de4a42019-06-13 16:32:24 +01004058 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004059 expected_status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004060
4061exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004062 psa_key_derivation_abort( &operation );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004063 PSA_DONE( );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004064}
4065/* END_CASE */
4066
Janos Follathaf3c2a02019-06-12 12:34:34 +01004067/* BEGIN_CASE */
Janos Follatha27c9272019-06-14 09:59:36 +01004068void derive_set_capacity( int alg_arg, int capacity_arg,
4069 int expected_status_arg )
4070{
4071 psa_algorithm_t alg = alg_arg;
4072 size_t capacity = capacity_arg;
4073 psa_status_t expected_status = expected_status_arg;
4074 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4075
4076 PSA_ASSERT( psa_crypto_init( ) );
4077
4078 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4079
4080 TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ),
4081 expected_status );
4082
4083exit:
4084 psa_key_derivation_abort( &operation );
4085 PSA_DONE( );
4086}
4087/* END_CASE */
4088
4089/* BEGIN_CASE */
Janos Follathaf3c2a02019-06-12 12:34:34 +01004090void derive_input( int alg_arg,
Gilles Peskine6842ba42019-09-23 13:49:33 +02004091 int step_arg1, int key_type_arg1, data_t *input1,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004092 int expected_status_arg1,
Gilles Peskine2058c072019-09-24 17:19:33 +02004093 int step_arg2, int key_type_arg2, data_t *input2,
Janos Follathaf3c2a02019-06-12 12:34:34 +01004094 int expected_status_arg2,
Gilles Peskine2058c072019-09-24 17:19:33 +02004095 int step_arg3, int key_type_arg3, data_t *input3,
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004096 int expected_status_arg3,
4097 int output_key_type_arg, int expected_output_status_arg )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004098{
4099 psa_algorithm_t alg = alg_arg;
Gilles Peskine6842ba42019-09-23 13:49:33 +02004100 psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3};
4101 psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3};
Janos Follathaf3c2a02019-06-12 12:34:34 +01004102 psa_status_t expected_statuses[] = {expected_status_arg1,
4103 expected_status_arg2,
4104 expected_status_arg3};
4105 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004106 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4107 MBEDTLS_SVC_KEY_ID_INIT,
4108 MBEDTLS_SVC_KEY_ID_INIT };
Janos Follathaf3c2a02019-06-12 12:34:34 +01004109 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4110 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4111 size_t i;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004112 psa_key_type_t output_key_type = output_key_type_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004113 mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004114 psa_status_t expected_output_status = expected_output_status_arg;
4115 psa_status_t actual_output_status;
Janos Follathaf3c2a02019-06-12 12:34:34 +01004116
4117 PSA_ASSERT( psa_crypto_init( ) );
4118
4119 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4120 psa_set_key_algorithm( &attributes, alg );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004121
4122 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4123
4124 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
4125 {
Gilles Peskine4023c012021-05-27 13:21:20 +02004126 mbedtls_test_set_step( i );
4127 if( steps[i] == 0 )
4128 {
4129 /* Skip this step */
4130 }
4131 else if( key_types[i] != PSA_KEY_TYPE_NONE )
Janos Follathaf3c2a02019-06-12 12:34:34 +01004132 {
Gilles Peskine6842ba42019-09-23 13:49:33 +02004133 psa_set_key_type( &attributes, key_types[i] );
4134 PSA_ASSERT( psa_import_key( &attributes,
4135 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004136 &keys[i] ) );
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004137 if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) &&
4138 steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET )
4139 {
4140 // When taking a private key as secret input, use key agreement
4141 // to add the shared secret to the derivation
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004142 TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self(
4143 &operation, keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004144 expected_statuses[i] );
4145 }
4146 else
4147 {
4148 TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i],
Ronald Cron5425a212020-08-04 14:58:35 +02004149 keys[i] ),
Steven Cooreman0ee0d522020-10-05 16:03:42 +02004150 expected_statuses[i] );
4151 }
Gilles Peskine6842ba42019-09-23 13:49:33 +02004152 }
4153 else
4154 {
4155 TEST_EQUAL( psa_key_derivation_input_bytes(
4156 &operation, steps[i],
4157 inputs[i]->x, inputs[i]->len ),
4158 expected_statuses[i] );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004159 }
4160 }
4161
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004162 if( output_key_type != PSA_KEY_TYPE_NONE )
4163 {
4164 psa_reset_key_attributes( &attributes );
4165 psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
4166 psa_set_key_bits( &attributes, 8 );
4167 actual_output_status =
4168 psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004169 &output_key );
Gilles Peskine1a2904c2019-09-24 17:45:07 +02004170 }
4171 else
4172 {
4173 uint8_t buffer[1];
4174 actual_output_status =
4175 psa_key_derivation_output_bytes( &operation,
4176 buffer, sizeof( buffer ) );
4177 }
4178 TEST_EQUAL( actual_output_status, expected_output_status );
4179
Janos Follathaf3c2a02019-06-12 12:34:34 +01004180exit:
4181 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004182 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4183 psa_destroy_key( keys[i] );
4184 psa_destroy_key( output_key );
Janos Follathaf3c2a02019-06-12 12:34:34 +01004185 PSA_DONE( );
4186}
4187/* END_CASE */
4188
Janos Follathd958bb72019-07-03 15:02:16 +01004189/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004190void derive_over_capacity( int alg_arg )
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004191{
Janos Follathd958bb72019-07-03 15:02:16 +01004192 psa_algorithm_t alg = alg_arg;
Ronald Cron5425a212020-08-04 14:58:35 +02004193 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Nir Sonnenschein4eda37b2018-10-31 12:15:58 +02004194 size_t key_type = PSA_KEY_TYPE_DERIVE;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004195 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Janos Follathd958bb72019-07-03 15:02:16 +01004196 unsigned char input1[] = "Input 1";
4197 size_t input1_length = sizeof( input1 );
4198 unsigned char input2[] = "Input 2";
4199 size_t input2_length = sizeof( input2 );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004200 uint8_t buffer[42];
Nir Sonnenschein1caf6d22018-11-01 12:27:20 +02004201 size_t capacity = sizeof( buffer );
Nir Sonnenscheindd69d8b2018-11-01 12:24:23 +02004202 const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4203 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
4204 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b};
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004205 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004206
Gilles Peskine8817f612018-12-18 00:18:46 +01004207 PSA_ASSERT( psa_crypto_init( ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004208
Gilles Peskine2c2cf0e2019-04-19 19:58:20 +02004209 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4210 psa_set_key_algorithm( &attributes, alg );
4211 psa_set_key_type( &attributes, key_type );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004212
Gilles Peskine73676cb2019-05-15 20:15:10 +02004213 PSA_ASSERT( psa_import_key( &attributes,
4214 key_data, sizeof( key_data ),
Ronald Cron5425a212020-08-04 14:58:35 +02004215 &key ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004216
4217 /* valid key derivation */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004218 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4219 input1, input1_length,
4220 input2, input2_length,
4221 capacity ) )
Janos Follathd958bb72019-07-03 15:02:16 +01004222 goto exit;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004223
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004224 /* state of operation shouldn't allow additional generation */
Janos Follathd958bb72019-07-03 15:02:16 +01004225 TEST_EQUAL( psa_key_derivation_setup( &operation, alg ),
Gilles Peskinef812dcf2018-12-18 00:33:25 +01004226 PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004227
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004228 PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004229
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004230 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ),
David Saadab4ecc272019-02-14 13:48:10 +02004231 PSA_ERROR_INSUFFICIENT_DATA );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004232
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004233exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004234 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004235 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004236 PSA_DONE( );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004237}
4238/* END_CASE */
4239
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004240/* BEGIN_CASE */
Gilles Peskine1c77edd2021-05-27 11:55:02 +02004241void derive_actions_without_setup( )
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004242{
4243 uint8_t output_buffer[16];
4244 size_t buffer_size = 16;
4245 size_t capacity = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004246 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004247
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004248 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4249 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004250 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004251
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004252 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004253 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004254
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004255 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004256
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004257 TEST_ASSERT( psa_key_derivation_output_bytes( &operation,
4258 output_buffer, buffer_size )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004259 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004260
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004261 TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004262 == PSA_ERROR_BAD_STATE );
Nir Sonnenscheinb46e7ca2018-10-25 14:46:09 +03004263
4264exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004265 psa_key_derivation_abort( &operation );
Nir Sonnenscheine5204c92018-10-22 17:24:55 +03004266}
4267/* END_CASE */
4268
4269/* BEGIN_CASE */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004270void derive_output( int alg_arg,
Gilles Peskine1468da72019-05-29 17:35:49 +02004271 int step1_arg, data_t *input1,
4272 int step2_arg, data_t *input2,
4273 int step3_arg, data_t *input3,
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004274 int requested_capacity_arg,
4275 data_t *expected_output1,
4276 data_t *expected_output2 )
4277{
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004278 psa_algorithm_t alg = alg_arg;
Gilles Peskine1468da72019-05-29 17:35:49 +02004279 psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg};
4280 data_t *inputs[] = {input1, input2, input3};
Ronald Cron5425a212020-08-04 14:58:35 +02004281 mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT,
4282 MBEDTLS_SVC_KEY_ID_INIT,
4283 MBEDTLS_SVC_KEY_ID_INIT };
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004284 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004285 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004286 uint8_t *expected_outputs[2] =
4287 {expected_output1->x, expected_output2->x};
4288 size_t output_sizes[2] =
4289 {expected_output1->len, expected_output2->len};
4290 size_t output_buffer_size = 0;
4291 uint8_t *output_buffer = NULL;
4292 size_t expected_capacity;
4293 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004294 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004295 psa_status_t status;
Gilles Peskine1468da72019-05-29 17:35:49 +02004296 size_t i;
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004297
4298 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4299 {
4300 if( output_sizes[i] > output_buffer_size )
4301 output_buffer_size = output_sizes[i];
4302 if( output_sizes[i] == 0 )
4303 expected_outputs[i] = NULL;
4304 }
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004305 ASSERT_ALLOC( output_buffer, output_buffer_size );
Gilles Peskine8817f612018-12-18 00:18:46 +01004306 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004307
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004308 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4309 psa_set_key_algorithm( &attributes, alg );
4310 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004311
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004312 /* Extraction phase. */
Gilles Peskine1468da72019-05-29 17:35:49 +02004313 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
4314 PSA_ASSERT( psa_key_derivation_set_capacity( &operation,
4315 requested_capacity ) );
4316 for( i = 0; i < ARRAY_LENGTH( steps ); i++ )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004317 {
Gilles Peskine1468da72019-05-29 17:35:49 +02004318 switch( steps[i] )
4319 {
4320 case 0:
4321 break;
4322 case PSA_KEY_DERIVATION_INPUT_SECRET:
4323 PSA_ASSERT( psa_import_key( &attributes,
4324 inputs[i]->x, inputs[i]->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004325 &keys[i] ) );
gabor-mezei-armceface22021-01-21 12:26:17 +01004326
4327 if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
4328 {
4329 PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) );
4330 TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <=
4331 PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE );
4332 }
4333
Gilles Peskine1468da72019-05-29 17:35:49 +02004334 PSA_ASSERT( psa_key_derivation_input_key(
Ronald Cron5425a212020-08-04 14:58:35 +02004335 &operation, steps[i], keys[i] ) );
Gilles Peskine1468da72019-05-29 17:35:49 +02004336 break;
4337 default:
4338 PSA_ASSERT( psa_key_derivation_input_bytes(
4339 &operation, steps[i],
4340 inputs[i]->x, inputs[i]->len ) );
4341 break;
4342 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004343 }
Gilles Peskine1468da72019-05-29 17:35:49 +02004344
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004345 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004346 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004347 TEST_EQUAL( current_capacity, requested_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004348 expected_capacity = requested_capacity;
4349
4350 /* Expansion phase. */
4351 for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ )
4352 {
4353 /* Read some bytes. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004354 status = psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004355 output_buffer, output_sizes[i] );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004356 if( expected_capacity == 0 && output_sizes[i] == 0 )
4357 {
4358 /* Reading 0 bytes when 0 bytes are available can go either way. */
4359 TEST_ASSERT( status == PSA_SUCCESS ||
David Saadab4ecc272019-02-14 13:48:10 +02004360 status == PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004361 continue;
4362 }
4363 else if( expected_capacity == 0 ||
4364 output_sizes[i] > expected_capacity )
4365 {
4366 /* Capacity exceeded. */
David Saadab4ecc272019-02-14 13:48:10 +02004367 TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004368 expected_capacity = 0;
4369 continue;
4370 }
4371 /* Success. Check the read data. */
Gilles Peskine8817f612018-12-18 00:18:46 +01004372 PSA_ASSERT( status );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004373 if( output_sizes[i] != 0 )
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004374 ASSERT_COMPARE( output_buffer, output_sizes[i],
4375 expected_outputs[i], output_sizes[i] );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004376 /* Check the operation status. */
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004377 expected_capacity -= output_sizes[i];
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004378 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004379 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004380 TEST_EQUAL( expected_capacity, current_capacity );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004381 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004382 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004383
4384exit:
4385 mbedtls_free( output_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004386 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004387 for( i = 0; i < ARRAY_LENGTH( keys ); i++ )
4388 psa_destroy_key( keys[i] );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004389 PSA_DONE( );
Gilles Peskine96ee5c72018-07-12 17:24:54 +02004390}
4391/* END_CASE */
4392
4393/* BEGIN_CASE */
Gilles Peskined54931c2018-07-17 21:06:59 +02004394void derive_full( int alg_arg,
4395 data_t *key_data,
Janos Follath47f27ed2019-06-25 13:24:52 +01004396 data_t *input1,
4397 data_t *input2,
Gilles Peskined54931c2018-07-17 21:06:59 +02004398 int requested_capacity_arg )
4399{
Ronald Cron5425a212020-08-04 14:58:35 +02004400 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004401 psa_algorithm_t alg = alg_arg;
4402 size_t requested_capacity = requested_capacity_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004403 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004404 unsigned char output_buffer[16];
4405 size_t expected_capacity = requested_capacity;
4406 size_t current_capacity;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004407 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskined54931c2018-07-17 21:06:59 +02004408
Gilles Peskine8817f612018-12-18 00:18:46 +01004409 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004410
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004411 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4412 psa_set_key_algorithm( &attributes, alg );
4413 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskined54931c2018-07-17 21:06:59 +02004414
Gilles Peskine049c7532019-05-15 20:22:09 +02004415 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004416 &key ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004417
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004418 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
4419 input1->x, input1->len,
4420 input2->x, input2->len,
4421 requested_capacity ) )
Janos Follathf2815ea2019-07-03 12:41:36 +01004422 goto exit;
Janos Follath47f27ed2019-06-25 13:24:52 +01004423
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004424 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004425 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004426 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004427
4428 /* Expansion phase. */
4429 while( current_capacity > 0 )
4430 {
4431 size_t read_size = sizeof( output_buffer );
4432 if( read_size > current_capacity )
4433 read_size = current_capacity;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004434 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004435 output_buffer,
4436 read_size ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004437 expected_capacity -= read_size;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004438 PSA_ASSERT( psa_key_derivation_get_capacity( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004439 &current_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004440 TEST_EQUAL( current_capacity, expected_capacity );
Gilles Peskined54931c2018-07-17 21:06:59 +02004441 }
4442
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004443 /* Check that the operation refuses to go over capacity. */
4444 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004445 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskined54931c2018-07-17 21:06:59 +02004446
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004447 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskined54931c2018-07-17 21:06:59 +02004448
4449exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004450 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004451 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004452 PSA_DONE( );
Gilles Peskined54931c2018-07-17 21:06:59 +02004453}
4454/* END_CASE */
4455
Janos Follathe60c9052019-07-03 13:51:30 +01004456/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004457void derive_key_exercise( int alg_arg,
4458 data_t *key_data,
Janos Follathe60c9052019-07-03 13:51:30 +01004459 data_t *input1,
4460 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004461 int derived_type_arg,
4462 int derived_bits_arg,
4463 int derived_usage_arg,
4464 int derived_alg_arg )
4465{
Ronald Cron5425a212020-08-04 14:58:35 +02004466 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4467 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004468 psa_algorithm_t alg = alg_arg;
4469 psa_key_type_t derived_type = derived_type_arg;
4470 size_t derived_bits = derived_bits_arg;
4471 psa_key_usage_t derived_usage = derived_usage_arg;
4472 psa_algorithm_t derived_alg = derived_alg_arg;
4473 size_t capacity = PSA_BITS_TO_BYTES( derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004474 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004475 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004476 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004477
Gilles Peskine8817f612018-12-18 00:18:46 +01004478 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004479
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004480 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4481 psa_set_key_algorithm( &attributes, alg );
4482 psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004483 PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004484 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004485
4486 /* Derive a key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004487 if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4488 input1->x, input1->len,
4489 input2->x, input2->len,
4490 capacity ) )
Janos Follathe60c9052019-07-03 13:51:30 +01004491 goto exit;
4492
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004493 psa_set_key_usage_flags( &attributes, derived_usage );
4494 psa_set_key_algorithm( &attributes, derived_alg );
4495 psa_set_key_type( &attributes, derived_type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004496 psa_set_key_bits( &attributes, derived_bits );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004497 PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004498 &derived_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004499
4500 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004501 PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004502 TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type );
4503 TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004504
4505 /* Exercise the derived key. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004506 if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) )
Gilles Peskine0386fba2018-07-12 17:29:22 +02004507 goto exit;
4508
4509exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004510 /*
4511 * Key attributes may have been returned by psa_get_key_attributes()
4512 * thus reset them as required.
4513 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004514 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004515
4516 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004517 psa_destroy_key( base_key );
4518 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004519 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004520}
4521/* END_CASE */
4522
Janos Follath42fd8882019-07-03 14:17:09 +01004523/* BEGIN_CASE */
Gilles Peskine0386fba2018-07-12 17:29:22 +02004524void derive_key_export( int alg_arg,
4525 data_t *key_data,
Janos Follath42fd8882019-07-03 14:17:09 +01004526 data_t *input1,
4527 data_t *input2,
Gilles Peskine0386fba2018-07-12 17:29:22 +02004528 int bytes1_arg,
4529 int bytes2_arg )
4530{
Ronald Cron5425a212020-08-04 14:58:35 +02004531 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4532 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004533 psa_algorithm_t alg = alg_arg;
4534 size_t bytes1 = bytes1_arg;
4535 size_t bytes2 = bytes2_arg;
4536 size_t capacity = bytes1 + bytes2;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004537 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004538 uint8_t *output_buffer = NULL;
4539 uint8_t *export_buffer = NULL;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004540 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4541 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine0386fba2018-07-12 17:29:22 +02004542 size_t length;
4543
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004544 ASSERT_ALLOC( output_buffer, capacity );
4545 ASSERT_ALLOC( export_buffer, capacity );
Gilles Peskine8817f612018-12-18 00:18:46 +01004546 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004547
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004548 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4549 psa_set_key_algorithm( &base_attributes, alg );
4550 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02004551 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004552 &base_key ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004553
4554 /* Derive some material and output it. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004555 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4556 input1->x, input1->len,
4557 input2->x, input2->len,
4558 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004559 goto exit;
4560
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004561 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004562 output_buffer,
4563 capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004564 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004565
4566 /* Derive the same output again, but this time store it in key objects. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004567 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4568 input1->x, input1->len,
4569 input2->x, input2->len,
4570 capacity ) )
Janos Follath42fd8882019-07-03 14:17:09 +01004571 goto exit;
4572
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004573 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4574 psa_set_key_algorithm( &derived_attributes, 0 );
4575 psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004576 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004577 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004578 &derived_key ) );
4579 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004580 export_buffer, bytes1,
4581 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004582 TEST_EQUAL( length, bytes1 );
Ronald Cron5425a212020-08-04 14:58:35 +02004583 PSA_ASSERT( psa_destroy_key( derived_key ) );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004584 psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004585 PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02004586 &derived_key ) );
4587 PSA_ASSERT( psa_export_key( derived_key,
Gilles Peskine8817f612018-12-18 00:18:46 +01004588 export_buffer + bytes1, bytes2,
4589 &length ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004590 TEST_EQUAL( length, bytes2 );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004591
4592 /* Compare the outputs from the two runs. */
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004593 ASSERT_COMPARE( output_buffer, bytes1 + bytes2,
4594 export_buffer, capacity );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004595
4596exit:
4597 mbedtls_free( output_buffer );
4598 mbedtls_free( export_buffer );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004599 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004600 psa_destroy_key( base_key );
4601 psa_destroy_key( derived_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004602 PSA_DONE( );
Gilles Peskine0386fba2018-07-12 17:29:22 +02004603}
4604/* END_CASE */
4605
4606/* BEGIN_CASE */
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004607void derive_key( int alg_arg,
4608 data_t *key_data, data_t *input1, data_t *input2,
4609 int type_arg, int bits_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004610 int expected_status_arg,
4611 int is_large_output )
Gilles Peskinec744d992019-07-30 17:26:54 +02004612{
Ronald Cron5425a212020-08-04 14:58:35 +02004613 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
4614 mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec744d992019-07-30 17:26:54 +02004615 psa_algorithm_t alg = alg_arg;
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004616 psa_key_type_t type = type_arg;
Gilles Peskinec744d992019-07-30 17:26:54 +02004617 size_t bits = bits_arg;
4618 psa_status_t expected_status = expected_status_arg;
4619 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
4620 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
4621 psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT;
4622
4623 PSA_ASSERT( psa_crypto_init( ) );
4624
4625 psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE );
4626 psa_set_key_algorithm( &base_attributes, alg );
4627 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
4628 PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02004629 &base_key ) );
Gilles Peskinec744d992019-07-30 17:26:54 +02004630
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004631 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg,
4632 input1->x, input1->len,
4633 input2->x, input2->len,
4634 SIZE_MAX ) )
Gilles Peskinec744d992019-07-30 17:26:54 +02004635 goto exit;
4636
4637 psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT );
4638 psa_set_key_algorithm( &derived_attributes, 0 );
Gilles Peskine7c227ae2019-07-31 15:14:44 +02004639 psa_set_key_type( &derived_attributes, type );
Gilles Peskinec744d992019-07-30 17:26:54 +02004640 psa_set_key_bits( &derived_attributes, bits );
Steven Cooreman83fdb702021-01-21 14:24:39 +01004641
4642 psa_status_t status =
4643 psa_key_derivation_output_key( &derived_attributes,
4644 &operation,
4645 &derived_key );
4646 if( is_large_output > 0 )
4647 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4648 TEST_EQUAL( status, expected_status );
Gilles Peskinec744d992019-07-30 17:26:54 +02004649
4650exit:
4651 psa_key_derivation_abort( &operation );
Ronald Cron5425a212020-08-04 14:58:35 +02004652 psa_destroy_key( base_key );
4653 psa_destroy_key( derived_key );
Gilles Peskinec744d992019-07-30 17:26:54 +02004654 PSA_DONE( );
4655}
4656/* END_CASE */
4657
4658/* BEGIN_CASE */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004659void key_agreement_setup( int alg_arg,
Steven Cooremance48e852020-10-05 16:02:45 +02004660 int our_key_type_arg, int our_key_alg_arg,
4661 data_t *our_key_data, data_t *peer_key_data,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004662 int expected_status_arg )
4663{
Ronald Cron5425a212020-08-04 14:58:35 +02004664 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004665 psa_algorithm_t alg = alg_arg;
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004666 psa_algorithm_t our_key_alg = our_key_alg_arg;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004667 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004668 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004669 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine77f40d82019-04-11 21:27:06 +02004670 psa_status_t expected_status = expected_status_arg;
4671 psa_status_t status;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004672
Gilles Peskine8817f612018-12-18 00:18:46 +01004673 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004674
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004675 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02004676 psa_set_key_algorithm( &attributes, our_key_alg );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004677 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004678 PSA_ASSERT( psa_import_key( &attributes,
4679 our_key_data->x, our_key_data->len,
4680 &our_key ) );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004681
Gilles Peskine77f40d82019-04-11 21:27:06 +02004682 /* The tests currently include inputs that should fail at either step.
4683 * Test cases that fail at the setup step should be changed to call
4684 * key_derivation_setup instead, and this function should be renamed
4685 * to key_agreement_fail. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004686 status = psa_key_derivation_setup( &operation, alg );
Gilles Peskine77f40d82019-04-11 21:27:06 +02004687 if( status == PSA_SUCCESS )
4688 {
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004689 TEST_EQUAL( psa_key_derivation_key_agreement(
4690 &operation, PSA_KEY_DERIVATION_INPUT_SECRET,
4691 our_key,
4692 peer_key_data->x, peer_key_data->len ),
Gilles Peskine77f40d82019-04-11 21:27:06 +02004693 expected_status );
4694 }
4695 else
4696 {
4697 TEST_ASSERT( status == expected_status );
4698 }
Gilles Peskine01d718c2018-09-18 12:01:02 +02004699
4700exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004701 psa_key_derivation_abort( &operation );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004702 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004703 PSA_DONE( );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004704}
4705/* END_CASE */
4706
4707/* BEGIN_CASE */
Gilles Peskinef0cba732019-04-11 22:12:38 +02004708void raw_key_agreement( int alg_arg,
4709 int our_key_type_arg, data_t *our_key_data,
4710 data_t *peer_key_data,
4711 data_t *expected_output )
4712{
Ronald Cron5425a212020-08-04 14:58:35 +02004713 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004714 psa_algorithm_t alg = alg_arg;
4715 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004716 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004717 unsigned char *output = NULL;
4718 size_t output_length = ~0;
gabor-mezei-armceface22021-01-21 12:26:17 +01004719 size_t key_bits;
Gilles Peskinef0cba732019-04-11 22:12:38 +02004720
4721 ASSERT_ALLOC( output, expected_output->len );
4722 PSA_ASSERT( psa_crypto_init( ) );
4723
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004724 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4725 psa_set_key_algorithm( &attributes, alg );
4726 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004727 PSA_ASSERT( psa_import_key( &attributes,
4728 our_key_data->x, our_key_data->len,
4729 &our_key ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004730
gabor-mezei-armceface22021-01-21 12:26:17 +01004731 PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) );
4732 key_bits = psa_get_key_bits( &attributes );
4733
Gilles Peskinebe697d82019-05-16 18:00:41 +02004734 PSA_ASSERT( psa_raw_key_agreement( alg, our_key,
4735 peer_key_data->x, peer_key_data->len,
4736 output, expected_output->len,
4737 &output_length ) );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004738 ASSERT_COMPARE( output, output_length,
4739 expected_output->x, expected_output->len );
gabor-mezei-armceface22021-01-21 12:26:17 +01004740 TEST_ASSERT( output_length <=
4741 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) );
4742 TEST_ASSERT( output_length <=
4743 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004744
4745exit:
4746 mbedtls_free( output );
4747 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004748 PSA_DONE( );
Gilles Peskinef0cba732019-04-11 22:12:38 +02004749}
4750/* END_CASE */
4751
4752/* BEGIN_CASE */
Gilles Peskine59685592018-09-18 12:11:34 +02004753void key_agreement_capacity( int alg_arg,
4754 int our_key_type_arg, data_t *our_key_data,
4755 data_t *peer_key_data,
4756 int expected_capacity_arg )
4757{
Ronald Cron5425a212020-08-04 14:58:35 +02004758 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004759 psa_algorithm_t alg = alg_arg;
4760 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004761 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004762 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004763 size_t actual_capacity;
Gilles Peskinebf491972018-10-25 22:36:12 +02004764 unsigned char output[16];
Gilles Peskine59685592018-09-18 12:11:34 +02004765
Gilles Peskine8817f612018-12-18 00:18:46 +01004766 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004767
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004768 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4769 psa_set_key_algorithm( &attributes, alg );
4770 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004771 PSA_ASSERT( psa_import_key( &attributes,
4772 our_key_data->x, our_key_data->len,
4773 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004774
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004775 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004776 PSA_ASSERT( psa_key_derivation_key_agreement(
4777 &operation,
4778 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4779 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004780 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4781 {
4782 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004783 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004784 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004785 NULL, 0 ) );
4786 }
Gilles Peskine59685592018-09-18 12:11:34 +02004787
Gilles Peskinebf491972018-10-25 22:36:12 +02004788 /* Test the advertized capacity. */
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004789 PSA_ASSERT( psa_key_derivation_get_capacity(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004790 &operation, &actual_capacity ) );
Gilles Peskinefe11b722018-12-18 00:24:04 +01004791 TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg );
Gilles Peskine59685592018-09-18 12:11:34 +02004792
Gilles Peskinebf491972018-10-25 22:36:12 +02004793 /* Test the actual capacity by reading the output. */
4794 while( actual_capacity > sizeof( output ) )
4795 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004796 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004797 output, sizeof( output ) ) );
Gilles Peskinebf491972018-10-25 22:36:12 +02004798 actual_capacity -= sizeof( output );
4799 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004800 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004801 output, actual_capacity ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004802 TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ),
David Saadab4ecc272019-02-14 13:48:10 +02004803 PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskinebf491972018-10-25 22:36:12 +02004804
Gilles Peskine59685592018-09-18 12:11:34 +02004805exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004806 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004807 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004808 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004809}
4810/* END_CASE */
4811
4812/* BEGIN_CASE */
4813void key_agreement_output( int alg_arg,
4814 int our_key_type_arg, data_t *our_key_data,
4815 data_t *peer_key_data,
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004816 data_t *expected_output1, data_t *expected_output2 )
Gilles Peskine59685592018-09-18 12:11:34 +02004817{
Ronald Cron5425a212020-08-04 14:58:35 +02004818 mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine59685592018-09-18 12:11:34 +02004819 psa_algorithm_t alg = alg_arg;
4820 psa_key_type_t our_key_type = our_key_type_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004821 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004822 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004823 uint8_t *actual_output = NULL;
Gilles Peskine59685592018-09-18 12:11:34 +02004824
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004825 ASSERT_ALLOC( actual_output, MAX( expected_output1->len,
4826 expected_output2->len ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004827
Gilles Peskine8817f612018-12-18 00:18:46 +01004828 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004829
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004830 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE );
4831 psa_set_key_algorithm( &attributes, alg );
4832 psa_set_key_type( &attributes, our_key_type );
Gilles Peskine049c7532019-05-15 20:22:09 +02004833 PSA_ASSERT( psa_import_key( &attributes,
4834 our_key_data->x, our_key_data->len,
4835 &our_key ) );
Gilles Peskine59685592018-09-18 12:11:34 +02004836
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004837 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004838 PSA_ASSERT( psa_key_derivation_key_agreement(
4839 &operation,
4840 PSA_KEY_DERIVATION_INPUT_SECRET, our_key,
4841 peer_key_data->x, peer_key_data->len ) );
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004842 if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) )
4843 {
4844 /* The test data is for info="" */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004845 PSA_ASSERT( psa_key_derivation_input_bytes( &operation,
Gilles Peskine03410b52019-05-16 16:05:19 +02004846 PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskinef8a9d942019-04-11 22:13:20 +02004847 NULL, 0 ) );
4848 }
Gilles Peskine59685592018-09-18 12:11:34 +02004849
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004850 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004851 actual_output,
4852 expected_output1->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004853 ASSERT_COMPARE( actual_output, expected_output1->len,
4854 expected_output1->x, expected_output1->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004855 if( expected_output2->len != 0 )
4856 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004857 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02004858 actual_output,
4859 expected_output2->len ) );
Gilles Peskine0dfba2d2018-12-18 00:40:50 +01004860 ASSERT_COMPARE( actual_output, expected_output2->len,
4861 expected_output2->x, expected_output2->len );
Gilles Peskine3ec8ed82018-10-25 22:37:15 +02004862 }
Gilles Peskine59685592018-09-18 12:11:34 +02004863
4864exit:
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004865 psa_key_derivation_abort( &operation );
Gilles Peskine59685592018-09-18 12:11:34 +02004866 psa_destroy_key( our_key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004867 PSA_DONE( );
Gilles Peskine59685592018-09-18 12:11:34 +02004868 mbedtls_free( actual_output );
4869}
4870/* END_CASE */
4871
4872/* BEGIN_CASE */
Gilles Peskinea50d7392018-06-21 10:22:13 +02004873void generate_random( int bytes_arg )
Gilles Peskine05d69892018-06-19 22:00:52 +02004874{
Gilles Peskinea50d7392018-06-21 10:22:13 +02004875 size_t bytes = bytes_arg;
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004876 unsigned char *output = NULL;
4877 unsigned char *changed = NULL;
Gilles Peskinea50d7392018-06-21 10:22:13 +02004878 size_t i;
4879 unsigned run;
Gilles Peskine05d69892018-06-19 22:00:52 +02004880
Simon Butcher49f8e312020-03-03 15:51:50 +00004881 TEST_ASSERT( bytes_arg >= 0 );
4882
Gilles Peskine91892022021-02-08 19:50:26 +01004883 ASSERT_ALLOC( output, bytes );
Gilles Peskine8cebbba2018-09-27 13:54:18 +02004884 ASSERT_ALLOC( changed, bytes );
Gilles Peskine05d69892018-06-19 22:00:52 +02004885
Gilles Peskine8817f612018-12-18 00:18:46 +01004886 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine05d69892018-06-19 22:00:52 +02004887
Gilles Peskinea50d7392018-06-21 10:22:13 +02004888 /* Run several times, to ensure that every output byte will be
4889 * nonzero at least once with overwhelming probability
4890 * (2^(-8*number_of_runs)). */
4891 for( run = 0; run < 10; run++ )
Gilles Peskine05d69892018-06-19 22:00:52 +02004892 {
Gilles Peskinef7ab5ad2018-09-26 18:19:24 +02004893 if( bytes != 0 )
4894 memset( output, 0, bytes );
Gilles Peskine8817f612018-12-18 00:18:46 +01004895 PSA_ASSERT( psa_generate_random( output, bytes ) );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004896
Gilles Peskinea50d7392018-06-21 10:22:13 +02004897 for( i = 0; i < bytes; i++ )
4898 {
4899 if( output[i] != 0 )
4900 ++changed[i];
4901 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004902 }
Gilles Peskinea50d7392018-06-21 10:22:13 +02004903
4904 /* Check that every byte was changed to nonzero at least once. This
4905 * validates that psa_generate_random is overwriting every byte of
4906 * the output buffer. */
4907 for( i = 0; i < bytes; i++ )
4908 {
4909 TEST_ASSERT( changed[i] != 0 );
4910 }
Gilles Peskine05d69892018-06-19 22:00:52 +02004911
4912exit:
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004913 PSA_DONE( );
Gilles Peskinea50d7392018-06-21 10:22:13 +02004914 mbedtls_free( output );
4915 mbedtls_free( changed );
Gilles Peskine05d69892018-06-19 22:00:52 +02004916}
4917/* END_CASE */
Gilles Peskine12313cd2018-06-20 00:20:32 +02004918
4919/* BEGIN_CASE */
4920void generate_key( int type_arg,
4921 int bits_arg,
4922 int usage_arg,
4923 int alg_arg,
Steven Cooreman83fdb702021-01-21 14:24:39 +01004924 int expected_status_arg,
4925 int is_large_key )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004926{
Ronald Cron5425a212020-08-04 14:58:35 +02004927 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004928 psa_key_type_t type = type_arg;
4929 psa_key_usage_t usage = usage_arg;
4930 size_t bits = bits_arg;
4931 psa_algorithm_t alg = alg_arg;
4932 psa_status_t expected_status = expected_status_arg;
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004933 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004934 psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004935
Gilles Peskine8817f612018-12-18 00:18:46 +01004936 PSA_ASSERT( psa_crypto_init( ) );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004937
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004938 psa_set_key_usage_flags( &attributes, usage );
4939 psa_set_key_algorithm( &attributes, alg );
4940 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02004941 psa_set_key_bits( &attributes, bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004942
4943 /* Generate a key */
Steven Cooreman83fdb702021-01-21 14:24:39 +01004944 psa_status_t status = psa_generate_key( &attributes, &key );
4945
4946 if( is_large_key > 0 )
4947 TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY );
4948 TEST_EQUAL( status , expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004949 if( expected_status != PSA_SUCCESS )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004950 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004951
4952 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02004953 PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02004954 TEST_EQUAL( psa_get_key_type( &got_attributes ), type );
4955 TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004956
Gilles Peskine818ca122018-06-20 18:16:48 +02004957 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01004958 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskine02b75072018-07-01 22:31:34 +02004959 goto exit;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004960
4961exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004962 /*
4963 * Key attributes may have been returned by psa_get_key_attributes()
4964 * thus reset them as required.
4965 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02004966 psa_reset_key_attributes( &got_attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01004967
Ronald Cron5425a212020-08-04 14:58:35 +02004968 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02004969 PSA_DONE( );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004970}
4971/* END_CASE */
itayzafrir0adf0fc2018-09-06 16:24:41 +03004972
Ronald Cronee414c72021-03-18 18:50:08 +01004973/* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN:MBEDTLS_GENPRIME */
Gilles Peskinee56e8782019-04-26 17:34:02 +02004974void generate_key_rsa( int bits_arg,
4975 data_t *e_arg,
4976 int expected_status_arg )
4977{
Ronald Cron5425a212020-08-04 14:58:35 +02004978 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskinec93b80c2019-05-16 19:39:54 +02004979 psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR;
Gilles Peskinee56e8782019-04-26 17:34:02 +02004980 size_t bits = bits_arg;
4981 psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
4982 psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW;
4983 psa_status_t expected_status = expected_status_arg;
4984 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
4985 uint8_t *exported = NULL;
4986 size_t exported_size =
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004987 PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004988 size_t exported_length = SIZE_MAX;
4989 uint8_t *e_read_buffer = NULL;
4990 int is_default_public_exponent = 0;
Gilles Peskineaa02c172019-04-28 11:44:17 +02004991 size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits );
Gilles Peskinee56e8782019-04-26 17:34:02 +02004992 size_t e_read_length = SIZE_MAX;
4993
4994 if( e_arg->len == 0 ||
4995 ( e_arg->len == 3 &&
4996 e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) )
4997 {
4998 is_default_public_exponent = 1;
4999 e_read_size = 0;
5000 }
5001 ASSERT_ALLOC( e_read_buffer, e_read_size );
5002 ASSERT_ALLOC( exported, exported_size );
5003
5004 PSA_ASSERT( psa_crypto_init( ) );
5005
5006 psa_set_key_usage_flags( &attributes, usage );
5007 psa_set_key_algorithm( &attributes, alg );
5008 PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type,
5009 e_arg->x, e_arg->len ) );
5010 psa_set_key_bits( &attributes, bits );
5011
5012 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005013 TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005014 if( expected_status != PSA_SUCCESS )
5015 goto exit;
5016
5017 /* Test the key information */
Ronald Cron5425a212020-08-04 14:58:35 +02005018 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005019 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5020 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
5021 PSA_ASSERT( psa_get_key_domain_parameters( &attributes,
5022 e_read_buffer, e_read_size,
5023 &e_read_length ) );
5024 if( is_default_public_exponent )
5025 TEST_EQUAL( e_read_length, 0 );
5026 else
5027 ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len );
5028
5029 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005030 if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) )
Gilles Peskinee56e8782019-04-26 17:34:02 +02005031 goto exit;
5032
5033 /* Export the key and check the public exponent. */
Ronald Cron5425a212020-08-04 14:58:35 +02005034 PSA_ASSERT( psa_export_public_key( key,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005035 exported, exported_size,
5036 &exported_length ) );
5037 {
5038 uint8_t *p = exported;
5039 uint8_t *end = exported + exported_length;
5040 size_t len;
5041 /* RSAPublicKey ::= SEQUENCE {
5042 * modulus INTEGER, -- n
5043 * publicExponent INTEGER } -- e
5044 */
5045 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005046 MBEDTLS_ASN1_SEQUENCE |
5047 MBEDTLS_ASN1_CONSTRUCTED ) );
Gilles Peskine8e94efe2021-02-13 00:25:53 +01005048 TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005049 TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len,
5050 MBEDTLS_ASN1_INTEGER ) );
5051 if( len >= 1 && p[0] == 0 )
5052 {
5053 ++p;
5054 --len;
5055 }
5056 if( e_arg->len == 0 )
5057 {
5058 TEST_EQUAL( len, 3 );
5059 TEST_EQUAL( p[0], 1 );
5060 TEST_EQUAL( p[1], 0 );
5061 TEST_EQUAL( p[2], 1 );
5062 }
5063 else
5064 ASSERT_COMPARE( p, len, e_arg->x, e_arg->len );
5065 }
5066
5067exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005068 /*
5069 * Key attributes may have been returned by psa_get_key_attributes() or
5070 * set by psa_set_key_domain_parameters() thus reset them as required.
5071 */
Gilles Peskinee56e8782019-04-26 17:34:02 +02005072 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005073
Ronald Cron5425a212020-08-04 14:58:35 +02005074 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005075 PSA_DONE( );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005076 mbedtls_free( e_read_buffer );
5077 mbedtls_free( exported );
5078}
5079/* END_CASE */
5080
Darryl Greend49a4992018-06-18 17:27:26 +01005081/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005082void persistent_key_load_key_from_storage( data_t *data,
5083 int type_arg, int bits_arg,
5084 int usage_flags_arg, int alg_arg,
5085 int generation_method )
Darryl Greend49a4992018-06-18 17:27:26 +01005086{
Ronald Cron71016a92020-08-28 19:01:50 +02005087 mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005088 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Ronald Cron5425a212020-08-04 14:58:35 +02005089 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
5090 mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005091 psa_key_type_t type = type_arg;
5092 size_t bits = bits_arg;
5093 psa_key_usage_t usage_flags = usage_flags_arg;
5094 psa_algorithm_t alg = alg_arg;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005095 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
Darryl Greend49a4992018-06-18 17:27:26 +01005096 unsigned char *first_export = NULL;
5097 unsigned char *second_export = NULL;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005098 size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005099 size_t first_exported_length;
5100 size_t second_exported_length;
5101
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005102 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5103 {
5104 ASSERT_ALLOC( first_export, export_size );
5105 ASSERT_ALLOC( second_export, export_size );
5106 }
Darryl Greend49a4992018-06-18 17:27:26 +01005107
Gilles Peskine8817f612018-12-18 00:18:46 +01005108 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005109
Gilles Peskinec87af662019-05-15 16:12:22 +02005110 psa_set_key_id( &attributes, key_id );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005111 psa_set_key_usage_flags( &attributes, usage_flags );
5112 psa_set_key_algorithm( &attributes, alg );
5113 psa_set_key_type( &attributes, type );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +02005114 psa_set_key_bits( &attributes, bits );
Darryl Greend49a4992018-06-18 17:27:26 +01005115
Darryl Green0c6575a2018-11-07 16:05:30 +00005116 switch( generation_method )
5117 {
5118 case IMPORT_KEY:
5119 /* Import the key */
Gilles Peskine049c7532019-05-15 20:22:09 +02005120 PSA_ASSERT( psa_import_key( &attributes, data->x, data->len,
Ronald Cron5425a212020-08-04 14:58:35 +02005121 &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005122 break;
Darryl Greend49a4992018-06-18 17:27:26 +01005123
Darryl Green0c6575a2018-11-07 16:05:30 +00005124 case GENERATE_KEY:
5125 /* Generate a key */
Ronald Cron5425a212020-08-04 14:58:35 +02005126 PSA_ASSERT( psa_generate_key( &attributes, &key ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005127 break;
5128
5129 case DERIVE_KEY:
Steven Cooreman70f654a2021-02-15 10:51:43 +01005130#if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005131 {
5132 /* Create base key */
5133 psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 );
5134 psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT;
5135 psa_set_key_usage_flags( &base_attributes,
5136 PSA_KEY_USAGE_DERIVE );
5137 psa_set_key_algorithm( &base_attributes, derive_alg );
5138 psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE );
Gilles Peskine049c7532019-05-15 20:22:09 +02005139 PSA_ASSERT( psa_import_key( &base_attributes,
5140 data->x, data->len,
5141 &base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005142 /* Derive a key. */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005143 PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005144 PSA_ASSERT( psa_key_derivation_input_key(
5145 &operation,
5146 PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005147 PSA_ASSERT( psa_key_derivation_input_bytes(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005148 &operation, PSA_KEY_DERIVATION_INPUT_INFO,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005149 NULL, 0 ) );
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005150 PSA_ASSERT( psa_key_derivation_output_key( &attributes,
5151 &operation,
Ronald Cron5425a212020-08-04 14:58:35 +02005152 &key ) );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005153 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005154 PSA_ASSERT( psa_destroy_key( base_key ) );
Ronald Cron5425a212020-08-04 14:58:35 +02005155 base_key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005156 }
Gilles Peskine6fea21d2021-01-12 00:02:15 +01005157#else
5158 TEST_ASSUME( ! "KDF not supported in this configuration" );
5159#endif
5160 break;
5161
5162 default:
5163 TEST_ASSERT( ! "generation_method not implemented in test" );
5164 break;
Darryl Green0c6575a2018-11-07 16:05:30 +00005165 }
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005166 psa_reset_key_attributes( &attributes );
Darryl Greend49a4992018-06-18 17:27:26 +01005167
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005168 /* Export the key if permitted by the key policy. */
5169 if( usage_flags & PSA_KEY_USAGE_EXPORT )
5170 {
Ronald Cron5425a212020-08-04 14:58:35 +02005171 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005172 first_export, export_size,
5173 &first_exported_length ) );
5174 if( generation_method == IMPORT_KEY )
5175 ASSERT_COMPARE( data->x, data->len,
5176 first_export, first_exported_length );
5177 }
Darryl Greend49a4992018-06-18 17:27:26 +01005178
5179 /* Shutdown and restart */
Ronald Cron5425a212020-08-04 14:58:35 +02005180 PSA_ASSERT( psa_purge_key( key ) );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005181 PSA_DONE();
Gilles Peskine8817f612018-12-18 00:18:46 +01005182 PSA_ASSERT( psa_crypto_init() );
Darryl Greend49a4992018-06-18 17:27:26 +01005183
Darryl Greend49a4992018-06-18 17:27:26 +01005184 /* Check key slot still contains key data */
Ronald Cron5425a212020-08-04 14:58:35 +02005185 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
Ronald Cronecfb2372020-07-23 17:13:42 +02005186 TEST_ASSERT( mbedtls_svc_key_id_equal(
5187 psa_get_key_id( &attributes ), key_id ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005188 TEST_EQUAL( psa_get_key_lifetime( &attributes ),
5189 PSA_KEY_LIFETIME_PERSISTENT );
5190 TEST_EQUAL( psa_get_key_type( &attributes ), type );
5191 TEST_EQUAL( psa_get_key_bits( &attributes ), bits );
gabor-mezei-arm4ff73032021-05-13 12:05:01 +02005192 TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
5193 update_key_usage_flags( usage_flags ) );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005194 TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
Darryl Greend49a4992018-06-18 17:27:26 +01005195
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005196 /* Export the key again if permitted by the key policy. */
5197 if( usage_flags & PSA_KEY_USAGE_EXPORT )
Darryl Green0c6575a2018-11-07 16:05:30 +00005198 {
Ronald Cron5425a212020-08-04 14:58:35 +02005199 PSA_ASSERT( psa_export_key( key,
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005200 second_export, export_size,
5201 &second_exported_length ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00005202 ASSERT_COMPARE( first_export, first_exported_length,
5203 second_export, second_exported_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005204 }
5205
5206 /* Do something with the key according to its type and permitted usage. */
Gilles Peskinec18e25f2021-02-12 23:48:20 +01005207 if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00005208 goto exit;
Darryl Greend49a4992018-06-18 17:27:26 +01005209
5210exit:
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005211 /*
5212 * Key attributes may have been returned by psa_get_key_attributes()
5213 * thus reset them as required.
5214 */
Gilles Peskinea1ace9c2019-04-26 16:03:33 +02005215 psa_reset_key_attributes( &attributes );
Ronald Cron3a4f0e32020-11-19 17:55:23 +01005216
Darryl Greend49a4992018-06-18 17:27:26 +01005217 mbedtls_free( first_export );
5218 mbedtls_free( second_export );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005219 psa_key_derivation_abort( &operation );
Gilles Peskine5c648ab2019-04-19 14:06:53 +02005220 psa_destroy_key( base_key );
Ronald Cron5425a212020-08-04 14:58:35 +02005221 psa_destroy_key( key );
Gilles Peskine1153e7b2019-05-28 15:10:21 +02005222 PSA_DONE();
Darryl Greend49a4992018-06-18 17:27:26 +01005223}
5224/* END_CASE */